1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328  /* * Mode: C++; tabwidth: 4; indenttabsmode: nil; cbasicoffset: 4 * */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <cassert>
#include <string>
#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include "plugin.hxx"
#include "check.hxx"
#include "clang/AST/CXXInheritance.h"
#include "clang/AST/StmtVisitor.h"
/**
This is a kind of simplified deadstore analysis.
We are looking for patterns like:
Foo x = a;
x = b;
which can be simplified to
x = b
or
Foo x = a;
x = f(x)
which can be simplified to
Foo x = f(a)
TODO Improve this plugin to make it safer. We should really be checking the following
conditions inside the RHS of the second statement:
If the variable is having it's address taken, or a nonconst method called on it,
on passed by nonconstref.
*/
namespace
{
//static bool startswith(const std::string& rStr, const char* pSubStr) {
// return rStr.compare(0, strlen(pSubStr), pSubStr) == 0;
//}
class SequentialAssign : public loplugin::FilteringPlugin<SequentialAssign>
{
public:
explicit SequentialAssign(loplugin::InstantiationData const& data)
: FilteringPlugin(data)
{
}
virtual void run() override
{
std::string fn(handler.getMainFileName());
loplugin::normalizeDotDotInFilePath(fn);
// places where the existing code style just looks better
// TODO lots of these would be unnecessary if I taught the plugin
// to ignore vars which are assigned to repeatedly
if (fn == SRCDIR "/vcl/source/helper/commandinfoprovider.cxx"
 fn == SRCDIR "/basegfx/source/polygon/b2dpolygonclipper.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/i18nlangtag/source/isolang/insys.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/vcl/unx/generic/fontmanager/fontconfig.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/svtools/source/filter/exportdialog.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/svtools/source/control/ruler.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/basic/qa/cppunit/test_scanner.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/basic/source/uno/namecont.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/test/source/sheet/xnamedrange.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/i18npool/qa/cppunit/test_breakiterator.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/i18npool/source/localedata/LocaleNode.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/i18npool/source/transliteration/transliteration_Ignore.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/i18npool/qa/cppunit/test_textsearch.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/framework/source/jobs/jobdata.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/framework/source/services/pathsettings.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/xmloff/source/chart/SchXMLTools.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/svx/source/tbxctrls/Palette.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/svx/source/sdr/contact/objectcontactofpageview.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/svx/source/form/fmservs.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/svx/source/svdraw/svdograf.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/svx/source/accessibility/AccessibleShape.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/svx/source/fmcomp/fmgridcl.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/chart2/source/tools/CharacterProperties.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/svx/source/dialog/dialcontrol.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/connectivity/source/commontools/TTableHelper.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/svx/source/dialog/_bmpmask.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/media/noel/disk2/libo4/cui/source/dialogs/SignSignatureLineDialog.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/filter/source/msfilter/msdffimp.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/filter/source/flash/swffilter.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/cui/source/dialogs/SignSignatureLineDialog.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/cui/source/dialogs/screenshotannotationdlg.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/cui/source/options/optupdt.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/dbaccess/source/ui/querydesign/querycontroller.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/dbaccess/source/ui/dlg/DbAdminImpl.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/dbaccess/source/ui/querydesign/JoinController.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/dbaccess/source/ui/misc/TokenWriter.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/dbaccess/source/ui/misc/TokenWriter.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/dbaccess/source/ui/misc/dbsubcomponentcontroller.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/dbaccess/source/core/api/querycomposer.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/desktop/source/lib/init.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/lotuswordpro/source/filter/lwpfribmark.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/tools/qa/cppunit/test_color.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/sc/qa/unit/ucalc.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/sc/source/ui/view/printfun.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/sc/source/ui/view/preview.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/sw/source/core/doc/tblafmt.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/sw/source/core/draw/dflyobj.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/sw/source/core/doc/DocumentDrawModelManager.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/sw/source/core/edit/edfcol.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/sw/source/filter/ww8/ww8toolbar.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/sw/source/ui/fldui/fldvar.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/sw/source/filter/ww8/ww8atr.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/sd/source/ui/accessibility/AccessiblePageShape.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/xmlsecurity/source/xmlsec/nss/nssinitializer.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/sd/source/ui/slidesorter/cache/SlsRequestFactory.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/sd/source/ui/framework/configuration/ResourceId.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/sd/source/filter/html/htmlex.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/starmath/source/cfgitem.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/ucb/source/ucp/ftp/ftpurl.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/starmath/source/node.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/ucb/source/ucp/cmis/certvalidation_handler.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/reportdesign/source/ui/inspection/GeometryHandler.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/reportdesign/source/core/api/ReportDefinition.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/test/source/table/tablerow.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/basegfx/test/B2DHomMatrixTest.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/comphelper/qa/unit/base64_test.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/testtools/source/bridgetest/bridgetest.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/comphelper/qa/string/test_string.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/cppu/qa/test_unotype.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/cppu/qa/cppumaker/test_cppumaker.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/o3tl/qa/testlru_map.cxx"  fn == SRCDIR "/svl/qa/unit/svl.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/chart2/qa/extras/PivotChartTest.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/chart2/qa/extras/chart2export.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/writerfilter/qa/cppunittests/misc/misc.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/sw/qa/extras/ww8export/ww8export.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/sw/qa/extras/uiwriter/uiwriter.cxx")< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
return;
// inside ifdef
if (fn == SRCDIR "/vcl/source/filter/png/pngread.cxx"
 fn == SRCDIR "/vcl/source/window/syschild.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/sal/osl/unx/security.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/vcl/source/filter/png/pngwrite.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/svtools/source/control/inettbc.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/canvas/source/cairo/cairo_textlayout.cxx"< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
 fn == SRCDIR "/sal/qa/osl/file/osl_File.cxx")< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
return;
// taking address of variable
if (fn == SRCDIR "/vcl/unx/generic/dtrans/X11_selection.cxx")< first condition< first condition
return;
// other
if (fn == SRCDIR "/sc/source/core/tool/scmatrix.cxx"< second condition
 fn == SRCDIR "/sal/qa/rtl/oustringbuffer/test_oustringbuffer_assign.cxx")< second condition< Same expression on both sides of ''. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
return;
TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
}
bool VisitCompoundStmt(CompoundStmt const*);
private:
const VarDecl* findSimpleAssign(Stmt const*);
bool isSimpleRHS(Expr const*);
Expr const* ignore(Expr const*);
void checkForSecondAssign(Stmt const* stmt, VarDecl const* varDecl);
};
bool SequentialAssign::VisitCompoundStmt(CompoundStmt const* compoundStmt)
{
if (ignoreLocation(compoundStmt))
return true;
auto it = compoundStmt>body_begin();
while (true)
{
if (it == compoundStmt>body_end())
break;
auto firstStmt = *it;
const VarDecl* foundVars = findSimpleAssign(firstStmt);
// reference types have slightly weird behaviour
if (!foundVars  foundVars>getType()>isReferenceType())
{
++it;
continue;
}
++it;
if (it == compoundStmt>body_end())
break;
checkForSecondAssign(*it, foundVars);
}
return true;
}
void SequentialAssign::checkForSecondAssign(Stmt const* stmt, VarDecl const* varDecl)
{
if (auto exprCleanup = dyn_cast<ExprWithCleanups>(stmt))
stmt = exprCleanup>getSubExpr();
if (auto operatorCall = dyn_cast<CXXOperatorCallExpr>(stmt))
{
if (operatorCall>getOperator() == OO_Equal)
{
if (auto declRefExprLHS = dyn_cast<DeclRefExpr>(ignore(operatorCall>getArg(0))))
if (declRefExprLHS>getDecl() == varDecl)
{
report(DiagnosticsEngine::Warning,
"simplify by merging with the preceding assignment",
compat::getBeginLoc(stmt))
<< stmt>getSourceRange();
}
}
return;
}
if (auto binaryOp = dyn_cast<BinaryOperator>(stmt))
{
if (binaryOp>getOpcode() == BO_Assign)
{
if (auto declRefExpr = dyn_cast<DeclRefExpr>(ignore(binaryOp>getLHS())))
if (declRefExpr>getDecl() == varDecl)
{
report(DiagnosticsEngine::Warning,
"simplify by merging with the preceding assignment",
compat::getBeginLoc(stmt))
<< stmt>getSourceRange();
}
}
}
}
const VarDecl* SequentialAssign::findSimpleAssign(Stmt const* stmt)
{
if (auto declStmt = dyn_cast<DeclStmt>(stmt))
if (declStmt>isSingleDecl())
if (auto varDeclLHS = dyn_cast_or_null<VarDecl>(declStmt>getSingleDecl()))
{
if (varDeclLHS>getStorageDuration() == SD_Static)
return nullptr;
// if it's callstyle init (e.g. OUString s("xxx")), we only treat
// it as simple if it only contains a variable in the call
// (e.g. OUString s(x))
if (varDeclLHS>getInitStyle() == VarDecl::InitializationStyle::CallInit)
{
auto cxxConstructExpr
= dyn_cast<CXXConstructExpr>(ignore(varDeclLHS>getInit()));
if (cxxConstructExpr)
{
if (cxxConstructExpr>getNumArgs() == 0)
return varDeclLHS;
if (cxxConstructExpr>getNumArgs() > 1)
return nullptr;
if (!isa<DeclRefExpr>(ignore(cxxConstructExpr>getArg(0))))
return nullptr;
}
}
if (auto init = varDeclLHS>getInit())
if (isSimpleRHS(init))
return varDeclLHS;
}
if (auto operatorCall = dyn_cast<CXXOperatorCallExpr>(stmt))
if (operatorCall>getOperator() == OO_Equal)
if (auto declRefExprLHS = dyn_cast<DeclRefExpr>(ignore(operatorCall>getArg(0))))
if (auto varDeclLHS = dyn_cast<VarDecl>(declRefExprLHS>getDecl()))
if (isSimpleRHS(operatorCall>getArg(1)))
return varDeclLHS;
if (auto binaryOp = dyn_cast<BinaryOperator>(stmt))
if (binaryOp>getOpcode() == BO_Assign)
if (auto declRefExprLHS = dyn_cast<DeclRefExpr>(ignore(binaryOp>getLHS())))
if (auto varDeclLHS = dyn_cast<VarDecl>(declRefExprLHS>getDecl()))
if (isSimpleRHS(binaryOp>getRHS()))
return varDeclLHS;
return nullptr;
}
/**
Does the first statement have a relatively simply RHS we can inline into the second statement?
*/
bool SequentialAssign::isSimpleRHS(Expr const* expr)
{
expr = ignore(expr);
// code like
// Point aCurPos = rGlyphs
// always has a CXXConstructExpr wrapping the RHS
if (auto cxxConstructExpr = dyn_cast<CXXConstructExpr>(expr))
if (cxxConstructExpr>getNumArgs() == 1)
expr = ignore(cxxConstructExpr>getArg(0));
if (!expr>isValueDependent()
&& expr>isConstantInitializer(compiler.getASTContext(), false /*ForRef*/))
return true;
if (isa<CXXMemberCallExpr>(expr))
return false;
if (isa<CXXOperatorCallExpr>(expr))
return false;
if (isa<UserDefinedLiteral>(expr))
return true;
if (isa<CallExpr>(expr))
return false;
if (isa<CastExpr>(expr))
return false;
if (isa<ArraySubscriptExpr>(expr))
return false;
if (isa<BinaryOperator>(expr))
return false;
if (isa<ConditionalOperator>(expr))
return false;
if (isa<UnaryOperator>(expr))
return false;
if (isa<CXXNewExpr>(expr))
return false;
if (auto memberExpr = dyn_cast<MemberExpr>(expr))
return isSimpleRHS(memberExpr>getBase());
return true;
}
Expr const* SequentialAssign::ignore(Expr const* expr)
{
return expr>IgnoreImplicit()>IgnoreParens()>IgnoreImplicit();
}
// Off by default because of safety concerns, see TODO at top
loplugin::Plugin::Registration<SequentialAssign> X("sequentialassign", false);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
