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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 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/.
*/
#if !defined _WIN32 //TODO, #include <sys/file.h>
#include <cassert>
#include <string>
#include <iostream>
#include <fstream>
#include <unordered_set>
#include <vector>
#include <algorithm>
#include <sys/file.h>
#include <unistd.h>
#include "config_clang.h"
#include "plugin.hxx"
#include "compat.hxx"
#include "check.hxx"
#if CLANG_VERSION >= 110000
#include "clang/AST/ParentMapContext.h"
#endif
/**
Look for static vars that are only assigned to once, and never written to, they can be const.
*/
namespace
{
/**
* Wrap the different kinds of callable and callee objects in the clang AST so I can define methods that handle everything.
*/
class CallerWrapper
{
const CallExpr* m_callExpr;
const CXXConstructExpr* m_cxxConstructExpr;
public:
CallerWrapper(const CallExpr* callExpr)
: m_callExpr(callExpr)
, m_cxxConstructExpr(nullptr)
{
}
CallerWrapper(const CXXConstructExpr* cxxConstructExpr)
: m_callExpr(nullptr)
, m_cxxConstructExpr(cxxConstructExpr)
{
}
unsigned getNumArgs() const
{
return m_callExpr ? m_callExpr->getNumArgs() : m_cxxConstructExpr->getNumArgs();
}
const Expr* getArg(unsigned i) const
{
return m_callExpr ? m_callExpr->getArg(i) : m_cxxConstructExpr->getArg(i);
}
};
class CalleeWrapper
{
const FunctionDecl* m_calleeFunctionDecl = nullptr;
const CXXConstructorDecl* m_cxxConstructorDecl = nullptr;
const FunctionProtoType* m_functionPrototype = nullptr;
public:
explicit CalleeWrapper(const FunctionDecl* calleeFunctionDecl)
: m_calleeFunctionDecl(calleeFunctionDecl)
{
}
explicit CalleeWrapper(const CXXConstructExpr* cxxConstructExpr)
: m_cxxConstructorDecl(cxxConstructExpr->getConstructor())
{
}
explicit CalleeWrapper(const FunctionProtoType* functionPrototype)
: m_functionPrototype(functionPrototype)
{
}
unsigned getNumParams() const
{
if (m_calleeFunctionDecl)
return m_calleeFunctionDecl->getNumParams();
else if (m_cxxConstructorDecl)
return m_cxxConstructorDecl->getNumParams();
else if (m_functionPrototype->param_type_begin() == m_functionPrototype->param_type_end())
// FunctionProtoType will assert if we call getParamTypes() and it has no params
return 0;
else
return m_functionPrototype->getParamTypes().size();
}
const QualType getParamType(unsigned i) const
{
if (m_calleeFunctionDecl)
return m_calleeFunctionDecl->getParamDecl(i)->getType();
else if (m_cxxConstructorDecl)
return m_cxxConstructorDecl->getParamDecl(i)->getType();
else
return m_functionPrototype->getParamTypes()[i];
}
std::string getNameAsString() const
{
if (m_calleeFunctionDecl)
return m_calleeFunctionDecl->getNameAsString();
else if (m_cxxConstructorDecl)
return m_cxxConstructorDecl->getNameAsString();
else
return "";
}
CXXMethodDecl const* getAsCXXMethodDecl() const
{
if (m_calleeFunctionDecl)
return dyn_cast<CXXMethodDecl>(m_calleeFunctionDecl);
return nullptr;
}
};
class ConstVars : public RecursiveASTVisitor<ConstVars>, public loplugin::Plugin
{
public:
explicit ConstVars(loplugin::InstantiationData const& data)
: Plugin(data)
{
}
virtual void run() override;
bool shouldVisitTemplateInstantiations() const { return true; }
bool shouldVisitImplicitCode() const { return true; }
bool VisitVarDecl(const VarDecl*);
bool VisitCXXForRangeStmt(const CXXForRangeStmt*);
bool VisitDeclRefExpr(const DeclRefExpr*);
bool TraverseCXXConstructorDecl(CXXConstructorDecl*);
bool TraverseCXXMethodDecl(CXXMethodDecl*);
bool TraverseFunctionDecl(FunctionDecl*);
bool TraverseIfStmt(IfStmt*);
private:
void check(const VarDecl* varDecl, const Expr* memberExpr);
bool isSomeKindOfZero(const Expr* arg);
bool IsPassedByNonConst(const VarDecl* varDecl, const Stmt* child, CallerWrapper callExpr,
CalleeWrapper calleeFunctionDecl);
llvm::Optional<CalleeWrapper> getCallee(CallExpr const*);
RecordDecl* insideMoveOrCopyDeclParent = nullptr;
// For reasons I do not understand, parentFunctionDecl() is not reliable, so
// we store the parent function on the way down the AST.
FunctionDecl* insideFunctionDecl = nullptr;
std::vector<VarDecl const*> insideConditionalCheckOfVarSet;
std::set<VarDecl const*> cannotBeConstSet;
std::set<VarDecl const*> definitionSet;
};
void ConstVars::run()
{
// clang::Expr::isCXX11ConstantExpr only works for C++
if (!compiler.getLangOpts().CPlusPlus)
return;
TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
SourceManager& SM = compiler.getSourceManager();
for (VarDecl const* v : definitionSet)
{
if (cannotBeConstSet.find(v) != cannotBeConstSet.end())
continue;
llvm::StringRef sourceString(SM.getCharacterData(v->getSourceRange().getEnd()), 50);
// Implement a marker that disables this plugins warning at a specific site
if (sourceString.contains("loplugin:constvars:ignore"))
continue;
report(DiagnosticsEngine::Warning, "var can be const", compat::getBeginLoc(v));
}
}
bool ConstVars::VisitVarDecl(const VarDecl* varDecl)
{
varDecl = varDecl->getCanonicalDecl();
if (varDecl->getLocation().isValid() && ignoreLocation(varDecl))
return true;
if (!varDecl->hasGlobalStorage())
return true;
if (isa<ParmVarDecl>(varDecl))
return true;
if (varDecl->getLinkageAndVisibility().getLinkage() == ExternalLinkage)
return true;
if (varDecl->getType().isConstQualified())
return true;
if (isa<ConstantArrayType>(varDecl->getType()))
return true;
if (loplugin::TypeCheck(varDecl->getType()).Pointer().Const())
return true;
// ignore stuff that forms part of the stable URE interface
if (isInUnoIncludeFile(compiler.getSourceManager().getSpellingLoc(varDecl->getLocation())))
return true;
if (!varDecl->getInit())
return true;
if (varDecl->getInit()->isInstantiationDependent())
return true;
if (!varDecl->getInit()->isCXX11ConstantExpr(compiler.getASTContext()))
return true;
definitionSet.insert(varDecl);
return true;
}
bool ConstVars::VisitCXXForRangeStmt(const CXXForRangeStmt* forStmt)
{
if (compat::getBeginLoc(forStmt).isValid() && ignoreLocation(forStmt))
return true;
const VarDecl* varDecl = forStmt->getLoopVariable();
if (!varDecl)
return true;
// we don't handle structured assignment properly
if (isa<DecompositionDecl>(varDecl))
return true;
auto tc = loplugin::TypeCheck(varDecl->getType());
if (!tc.LvalueReference())
return true;
if (tc.LvalueReference().Const())
return true;
definitionSet.insert(varDecl);
return true;
}
bool ConstVars::TraverseCXXConstructorDecl(CXXConstructorDecl* cxxConstructorDecl)
{
auto copy = insideMoveOrCopyDeclParent;
if (!ignoreLocation(cxxConstructorDecl) && cxxConstructorDecl->isThisDeclarationADefinition())
{
if (cxxConstructorDecl->isCopyOrMoveConstructor())
insideMoveOrCopyDeclParent = cxxConstructorDecl->getParent();
}
bool ret = RecursiveASTVisitor::TraverseCXXConstructorDecl(cxxConstructorDecl);
insideMoveOrCopyDeclParent = copy;
return ret;
}
bool ConstVars::TraverseCXXMethodDecl(CXXMethodDecl* cxxMethodDecl)
{
auto copy1 = insideMoveOrCopyDeclParent;
auto copy2 = insideFunctionDecl;
if (!ignoreLocation(cxxMethodDecl) && cxxMethodDecl->isThisDeclarationADefinition())
{
if (cxxMethodDecl->isCopyAssignmentOperator() || cxxMethodDecl->isMoveAssignmentOperator())
insideMoveOrCopyDeclParent = cxxMethodDecl->getParent();
}
insideFunctionDecl = cxxMethodDecl;
bool ret = RecursiveASTVisitor::TraverseCXXMethodDecl(cxxMethodDecl);
insideMoveOrCopyDeclParent = copy1;
insideFunctionDecl = copy2;
return ret;
}
bool ConstVars::TraverseFunctionDecl(FunctionDecl* functionDecl)
{
auto copy2 = insideFunctionDecl;
insideFunctionDecl = functionDecl;
bool ret = RecursiveASTVisitor::TraverseFunctionDecl(functionDecl);
insideFunctionDecl = copy2;
return ret;
}
bool ConstVars::TraverseIfStmt(IfStmt* ifStmt)
{
VarDecl const* varDecl = nullptr;
Expr const* cond = ifStmt->getCond()->IgnoreParenImpCasts();
if (auto declRefExpr = dyn_cast<DeclRefExpr>(cond))
{
if ((varDecl = dyn_cast<VarDecl>(declRefExpr->getDecl())))
insideConditionalCheckOfVarSet.push_back(varDecl);
}
bool ret = RecursiveASTVisitor::TraverseIfStmt(ifStmt);
if (varDecl)
insideConditionalCheckOfVarSet.pop_back();
return ret;
}
bool ConstVars::VisitDeclRefExpr(const DeclRefExpr* declRefExpr)
{
const VarDecl* varDecl = dyn_cast<VarDecl>(declRefExpr->getDecl());
if (!varDecl)
return true;
varDecl = varDecl->getCanonicalDecl();
if (ignoreLocation(varDecl))
return true;
// ignore stuff that forms part of the stable URE interface
if (isInUnoIncludeFile(compiler.getSourceManager().getSpellingLoc(varDecl->getLocation())))
return true;
if (definitionSet.find(varDecl) != definitionSet.end())
check(varDecl, declRefExpr);
return true;
}
void ConstVars::check(const VarDecl* varDecl, const Expr* memberExpr)
{
auto parentsRange = getParents(*memberExpr);
const Stmt* child = memberExpr;
const Stmt* parent
= parentsRange.begin() == parentsRange.end() ? nullptr : parentsRange.begin()->get<Stmt>();
// walk up the tree until we find something interesting
bool bCannotBeConst = false;
bool bDump = false;
auto walkUp = [&]() {
child = parent;
auto parentsRange = getParents(*parent);
parent = parentsRange.begin() == parentsRange.end() ? nullptr
: parentsRange.begin()->get<Stmt>();
};
do
{
if (!parent)
{
// check if we have an expression like
// int& r = var;
auto parentsRange = getParents(*child);
if (parentsRange.begin() != parentsRange.end())
{
auto varDecl = dyn_cast_or_null<VarDecl>(parentsRange.begin()->get<Decl>());
if (varDecl)
{
if (varDecl->isImplicit())
{
// so we can walk up from inside a for-range stmt
parentsRange = getParents(*varDecl);
if (parentsRange.begin() != parentsRange.end())
parent = parentsRange.begin()->get<Stmt>();
}
else if (loplugin::TypeCheck(varDecl->getType()).LvalueReference().NonConst())
{
bCannotBeConst = true;
break;
}
}
}
}
if (!parent)
break;
if (isa<CXXReinterpretCastExpr>(parent))
{
// once we see one of these, there is not much useful we can know
bCannotBeConst = true;
break;
}
else if (isa<CastExpr>(parent) || isa<MemberExpr>(parent) || isa<ParenExpr>(parent)
|| isa<ParenListExpr>(parent) || isa<ArrayInitLoopExpr>(parent)
|| isa<ExprWithCleanups>(parent))
{
walkUp();
}
else if (auto unaryOperator = dyn_cast<UnaryOperator>(parent))
{
UnaryOperator::Opcode op = unaryOperator->getOpcode();
if (op == UO_AddrOf || op == UO_PostInc || op == UO_PostDec || op == UO_PreInc
|| op == UO_PreDec)
{
bCannotBeConst = true;
}
walkUp();
}
else if (auto operatorCallExpr = dyn_cast<CXXOperatorCallExpr>(parent))
{
auto callee = getCallee(operatorCallExpr);
if (callee)
{
// if calling a non-const operator on the var
auto calleeMethodDecl = callee->getAsCXXMethodDecl();
if (calleeMethodDecl && operatorCallExpr->getArg(0) == child
&& !calleeMethodDecl->isConst())
{
bCannotBeConst = true;
}
else if (IsPassedByNonConst(varDecl, child, operatorCallExpr, *callee))
{
bCannotBeConst = true;
}
}
else
bCannotBeConst = true; // conservative, could improve
walkUp();
}
else if (auto cxxMemberCallExpr = dyn_cast<CXXMemberCallExpr>(parent))
{
const CXXMethodDecl* calleeMethodDecl = cxxMemberCallExpr->getMethodDecl();
if (calleeMethodDecl)
{
// if calling a non-const method on the var
const Expr* tmp = dyn_cast<Expr>(child);
if (tmp->isBoundMemberFunction(compiler.getASTContext()))
{
tmp = dyn_cast<MemberExpr>(tmp)->getBase();
}
if (cxxMemberCallExpr->getImplicitObjectArgument() == tmp
&& !calleeMethodDecl->isConst())
{
bCannotBeConst = true;
break;
}
if (IsPassedByNonConst(varDecl, child, cxxMemberCallExpr,
CalleeWrapper(calleeMethodDecl)))
bCannotBeConst = true;
}
else
bCannotBeConst = true; // can happen in templates
walkUp();
}
else if (auto cxxConstructExpr = dyn_cast<CXXConstructExpr>(parent))
{
if (IsPassedByNonConst(varDecl, child, cxxConstructExpr,
CalleeWrapper(cxxConstructExpr)))
bCannotBeConst = true;
walkUp();
}
else if (auto callExpr = dyn_cast<CallExpr>(parent))
{
auto callee = getCallee(callExpr);
if (callee)
{
if (IsPassedByNonConst(varDecl, child, callExpr, *callee))
bCannotBeConst = true;
}
else
bCannotBeConst = true; // conservative, could improve
walkUp();
}
else if (auto binaryOp = dyn_cast<BinaryOperator>(parent))
{
BinaryOperator::Opcode op = binaryOp->getOpcode();
const bool assignmentOp = op == BO_Assign || op == BO_MulAssign || op == BO_DivAssign
|| op == BO_RemAssign || op == BO_AddAssign
|| op == BO_SubAssign || op == BO_ShlAssign
|| op == BO_ShrAssign || op == BO_AndAssign
|| op == BO_XorAssign || op == BO_OrAssign;
if (assignmentOp)
{
if (binaryOp->getLHS() == child)
bCannotBeConst = true;
else if (loplugin::TypeCheck(binaryOp->getLHS()->getType())
.LvalueReference()
.NonConst())
// if the LHS is a non-const reference, we could write to the var later on
bCannotBeConst = true;
}
walkUp();
}
else if (isa<ReturnStmt>(parent))
{
if (insideFunctionDecl)
{
auto tc = loplugin::TypeCheck(insideFunctionDecl->getReturnType());
if (tc.LvalueReference().NonConst())
bCannotBeConst = true;
}
break;
}
else if (auto rangeStmt = dyn_cast<CXXForRangeStmt>(parent))
{
if (rangeStmt->getRangeStmt() == child)
{
auto tc = loplugin::TypeCheck(rangeStmt->getLoopVariable()->getType());
if (tc.LvalueReference().NonConst())
bCannotBeConst = true;
}
break;
}
else if (isa<SwitchStmt>(parent) || isa<WhileStmt>(parent) || isa<ForStmt>(parent)
|| isa<IfStmt>(parent) || isa<DoStmt>(parent) || isa<DefaultStmt>(parent))
{
break;
}
else
{
walkUp();
}
} while (true);
if (bDump)
{
report(DiagnosticsEngine::Warning, "oh dear, what can the matter be? writtenTo=%0",
compat::getBeginLoc(memberExpr))
<< bCannotBeConst << memberExpr->getSourceRange();
if (parent)
{
report(DiagnosticsEngine::Note, "parent over here", compat::getBeginLoc(parent))
<< parent->getSourceRange();
parent->dump();
}
memberExpr->dump();
varDecl->getType()->dump();
}
if (bCannotBeConst)
cannotBeConstSet.insert(varDecl);
}
bool ConstVars::IsPassedByNonConst(const VarDecl* varDecl, const Stmt* child,
CallerWrapper callExpr, CalleeWrapper calleeFunctionDecl)
{
unsigned len = std::min(callExpr.getNumArgs(), calleeFunctionDecl.getNumParams());
// if it's an array, passing it by value to a method typically means the
// callee takes a pointer and can modify the array
if (varDecl->getType()->isConstantArrayType())
{
for (unsigned i = 0; i < len; ++i)
if (callExpr.getArg(i) == child)
if (loplugin::TypeCheck(calleeFunctionDecl.getParamType(i)).Pointer().NonConst())
return true;
}
else
{
for (unsigned i = 0; i < len; ++i)
if (callExpr.getArg(i) == child)
{
auto tc = loplugin::TypeCheck(calleeFunctionDecl.getParamType(i));
if (tc.LvalueReference().NonConst() || tc.Pointer().NonConst())
return true;
}
}
return false;
}
llvm::Optional<CalleeWrapper> ConstVars::getCallee(CallExpr const* callExpr)
{
FunctionDecl const* functionDecl = callExpr->getDirectCallee();
if (functionDecl)
return CalleeWrapper(functionDecl);
// Extract the functionprototype from a type
clang::Type const* calleeType = callExpr->getCallee()->getType().getTypePtr();
if (auto pointerType = calleeType->getUnqualifiedDesugaredType()->getAs<clang::PointerType>())
{
if (auto prototype = pointerType->getPointeeType()
->getUnqualifiedDesugaredType()
->getAs<FunctionProtoType>())
{
return CalleeWrapper(prototype);
}
}
return llvm::Optional<CalleeWrapper>();
}
/** off by default because it is very expensive, it walks up the AST a lot */
loplugin::Plugin::Registration<ConstVars> X("constvars", false);
}
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|