1 //==- DeadStoresChecker.cpp - Check for stores to dead variables -*- C++ -*-==//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines a DeadStores, a flow-sensitive checker that looks for
11 // stores to variables that are no longer live.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "ClangSACheckers.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/Attr.h"
18 #include "clang/AST/ParentMap.h"
19 #include "clang/AST/RecursiveASTVisitor.h"
20 #include "clang/Analysis/Analyses/LiveVariables.h"
21 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
22 #include "clang/StaticAnalyzer/Core/Checker.h"
23 #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
24 #include "llvm/ADT/BitVector.h"
25 #include "llvm/ADT/SmallString.h"
26 #include "llvm/Support/SaveAndRestore.h"
27
28 using namespace clang;
29 using namespace ento;
30
31 namespace {
32
33 /// A simple visitor to record what VarDecls occur in EH-handling code.
34 class EHCodeVisitor : public RecursiveASTVisitor<EHCodeVisitor> {
35 public:
36 bool inEH;
37 llvm::DenseSet<const VarDecl *> &S;
38
TraverseObjCAtFinallyStmt(ObjCAtFinallyStmt * S)39 bool TraverseObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
40 SaveAndRestore<bool> inFinally(inEH, true);
41 return ::RecursiveASTVisitor<EHCodeVisitor>::TraverseObjCAtFinallyStmt(S);
42 }
43
TraverseObjCAtCatchStmt(ObjCAtCatchStmt * S)44 bool TraverseObjCAtCatchStmt(ObjCAtCatchStmt *S) {
45 SaveAndRestore<bool> inCatch(inEH, true);
46 return ::RecursiveASTVisitor<EHCodeVisitor>::TraverseObjCAtCatchStmt(S);
47 }
48
TraverseCXXCatchStmt(CXXCatchStmt * S)49 bool TraverseCXXCatchStmt(CXXCatchStmt *S) {
50 SaveAndRestore<bool> inCatch(inEH, true);
51 return TraverseStmt(S->getHandlerBlock());
52 }
53
VisitDeclRefExpr(DeclRefExpr * DR)54 bool VisitDeclRefExpr(DeclRefExpr *DR) {
55 if (inEH)
56 if (const VarDecl *D = dyn_cast<VarDecl>(DR->getDecl()))
57 S.insert(D);
58 return true;
59 }
60
EHCodeVisitor(llvm::DenseSet<const VarDecl * > & S)61 EHCodeVisitor(llvm::DenseSet<const VarDecl *> &S) :
62 inEH(false), S(S) {}
63 };
64
65 // FIXME: Eventually migrate into its own file, and have it managed by
66 // AnalysisManager.
67 class ReachableCode {
68 const CFG &cfg;
69 llvm::BitVector reachable;
70 public:
ReachableCode(const CFG & cfg)71 ReachableCode(const CFG &cfg)
72 : cfg(cfg), reachable(cfg.getNumBlockIDs(), false) {}
73
74 void computeReachableBlocks();
75
isReachable(const CFGBlock * block) const76 bool isReachable(const CFGBlock *block) const {
77 return reachable[block->getBlockID()];
78 }
79 };
80 }
81
computeReachableBlocks()82 void ReachableCode::computeReachableBlocks() {
83 if (!cfg.getNumBlockIDs())
84 return;
85
86 SmallVector<const CFGBlock*, 10> worklist;
87 worklist.push_back(&cfg.getEntry());
88
89 while (!worklist.empty()) {
90 const CFGBlock *block = worklist.pop_back_val();
91 llvm::BitVector::reference isReachable = reachable[block->getBlockID()];
92 if (isReachable)
93 continue;
94 isReachable = true;
95 for (CFGBlock::const_succ_iterator i = block->succ_begin(),
96 e = block->succ_end(); i != e; ++i)
97 if (const CFGBlock *succ = *i)
98 worklist.push_back(succ);
99 }
100 }
101
102 static const Expr *
LookThroughTransitiveAssignmentsAndCommaOperators(const Expr * Ex)103 LookThroughTransitiveAssignmentsAndCommaOperators(const Expr *Ex) {
104 while (Ex) {
105 const BinaryOperator *BO =
106 dyn_cast<BinaryOperator>(Ex->IgnoreParenCasts());
107 if (!BO)
108 break;
109 if (BO->getOpcode() == BO_Assign) {
110 Ex = BO->getRHS();
111 continue;
112 }
113 if (BO->getOpcode() == BO_Comma) {
114 Ex = BO->getRHS();
115 continue;
116 }
117 break;
118 }
119 return Ex;
120 }
121
122 namespace {
123 class DeadStoreObs : public LiveVariables::Observer {
124 const CFG &cfg;
125 ASTContext &Ctx;
126 BugReporter& BR;
127 const CheckerBase *Checker;
128 AnalysisDeclContext* AC;
129 ParentMap& Parents;
130 llvm::SmallPtrSet<const VarDecl*, 20> Escaped;
131 std::unique_ptr<ReachableCode> reachableCode;
132 const CFGBlock *currentBlock;
133 std::unique_ptr<llvm::DenseSet<const VarDecl *>> InEH;
134
135 enum DeadStoreKind { Standard, Enclosing, DeadIncrement, DeadInit };
136
137 public:
DeadStoreObs(const CFG & cfg,ASTContext & ctx,BugReporter & br,const CheckerBase * checker,AnalysisDeclContext * ac,ParentMap & parents,llvm::SmallPtrSet<const VarDecl *,20> & escaped)138 DeadStoreObs(const CFG &cfg, ASTContext &ctx, BugReporter &br,
139 const CheckerBase *checker, AnalysisDeclContext *ac,
140 ParentMap &parents,
141 llvm::SmallPtrSet<const VarDecl *, 20> &escaped)
142 : cfg(cfg), Ctx(ctx), BR(br), Checker(checker), AC(ac), Parents(parents),
143 Escaped(escaped), currentBlock(nullptr) {}
144
~DeadStoreObs()145 ~DeadStoreObs() override {}
146
isLive(const LiveVariables::LivenessValues & Live,const VarDecl * D)147 bool isLive(const LiveVariables::LivenessValues &Live, const VarDecl *D) {
148 if (Live.isLive(D))
149 return true;
150 // Lazily construct the set that records which VarDecls are in
151 // EH code.
152 if (!InEH.get()) {
153 InEH.reset(new llvm::DenseSet<const VarDecl *>());
154 EHCodeVisitor V(*InEH.get());
155 V.TraverseStmt(AC->getBody());
156 }
157 // Treat all VarDecls that occur in EH code as being "always live"
158 // when considering to suppress dead stores. Frequently stores
159 // are followed by reads in EH code, but we don't have the ability
160 // to analyze that yet.
161 return InEH->count(D);
162 }
163
Report(const VarDecl * V,DeadStoreKind dsk,PathDiagnosticLocation L,SourceRange R)164 void Report(const VarDecl *V, DeadStoreKind dsk,
165 PathDiagnosticLocation L, SourceRange R) {
166 if (Escaped.count(V))
167 return;
168
169 // Compute reachable blocks within the CFG for trivial cases
170 // where a bogus dead store can be reported because itself is unreachable.
171 if (!reachableCode.get()) {
172 reachableCode.reset(new ReachableCode(cfg));
173 reachableCode->computeReachableBlocks();
174 }
175
176 if (!reachableCode->isReachable(currentBlock))
177 return;
178
179 SmallString<64> buf;
180 llvm::raw_svector_ostream os(buf);
181 const char *BugType = nullptr;
182
183 switch (dsk) {
184 case DeadInit:
185 BugType = "Dead initialization";
186 os << "Value stored to '" << *V
187 << "' during its initialization is never read";
188 break;
189
190 case DeadIncrement:
191 BugType = "Dead increment";
192 case Standard:
193 if (!BugType) BugType = "Dead assignment";
194 os << "Value stored to '" << *V << "' is never read";
195 break;
196
197 case Enclosing:
198 // Don't report issues in this case, e.g.: "if (x = foo())",
199 // where 'x' is unused later. We have yet to see a case where
200 // this is a real bug.
201 return;
202 }
203
204 BR.EmitBasicReport(AC->getDecl(), Checker, BugType, "Dead store", os.str(),
205 L, R);
206 }
207
CheckVarDecl(const VarDecl * VD,const Expr * Ex,const Expr * Val,DeadStoreKind dsk,const LiveVariables::LivenessValues & Live)208 void CheckVarDecl(const VarDecl *VD, const Expr *Ex, const Expr *Val,
209 DeadStoreKind dsk,
210 const LiveVariables::LivenessValues &Live) {
211
212 if (!VD->hasLocalStorage())
213 return;
214 // Reference types confuse the dead stores checker. Skip them
215 // for now.
216 if (VD->getType()->getAs<ReferenceType>())
217 return;
218
219 if (!isLive(Live, VD) &&
220 !(VD->hasAttr<UnusedAttr>() || VD->hasAttr<BlocksAttr>() ||
221 VD->hasAttr<ObjCPreciseLifetimeAttr>())) {
222
223 PathDiagnosticLocation ExLoc =
224 PathDiagnosticLocation::createBegin(Ex, BR.getSourceManager(), AC);
225 Report(VD, dsk, ExLoc, Val->getSourceRange());
226 }
227 }
228
CheckDeclRef(const DeclRefExpr * DR,const Expr * Val,DeadStoreKind dsk,const LiveVariables::LivenessValues & Live)229 void CheckDeclRef(const DeclRefExpr *DR, const Expr *Val, DeadStoreKind dsk,
230 const LiveVariables::LivenessValues& Live) {
231 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
232 CheckVarDecl(VD, DR, Val, dsk, Live);
233 }
234
isIncrement(VarDecl * VD,const BinaryOperator * B)235 bool isIncrement(VarDecl *VD, const BinaryOperator* B) {
236 if (B->isCompoundAssignmentOp())
237 return true;
238
239 const Expr *RHS = B->getRHS()->IgnoreParenCasts();
240 const BinaryOperator* BRHS = dyn_cast<BinaryOperator>(RHS);
241
242 if (!BRHS)
243 return false;
244
245 const DeclRefExpr *DR;
246
247 if ((DR = dyn_cast<DeclRefExpr>(BRHS->getLHS()->IgnoreParenCasts())))
248 if (DR->getDecl() == VD)
249 return true;
250
251 if ((DR = dyn_cast<DeclRefExpr>(BRHS->getRHS()->IgnoreParenCasts())))
252 if (DR->getDecl() == VD)
253 return true;
254
255 return false;
256 }
257
observeStmt(const Stmt * S,const CFGBlock * block,const LiveVariables::LivenessValues & Live)258 void observeStmt(const Stmt *S, const CFGBlock *block,
259 const LiveVariables::LivenessValues &Live) override {
260
261 currentBlock = block;
262
263 // Skip statements in macros.
264 if (S->getLocStart().isMacroID())
265 return;
266
267 // Only cover dead stores from regular assignments. ++/-- dead stores
268 // have never flagged a real bug.
269 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
270 if (!B->isAssignmentOp()) return; // Skip non-assignments.
271
272 if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(B->getLHS()))
273 if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
274 // Special case: check for assigning null to a pointer.
275 // This is a common form of defensive programming.
276 const Expr *RHS =
277 LookThroughTransitiveAssignmentsAndCommaOperators(B->getRHS());
278 RHS = RHS->IgnoreParenCasts();
279
280 QualType T = VD->getType();
281 if (T.isVolatileQualified())
282 return;
283 if (T->isPointerType() || T->isObjCObjectPointerType()) {
284 if (RHS->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNull))
285 return;
286 }
287
288 // Special case: self-assignments. These are often used to shut up
289 // "unused variable" compiler warnings.
290 if (const DeclRefExpr *RhsDR = dyn_cast<DeclRefExpr>(RHS))
291 if (VD == dyn_cast<VarDecl>(RhsDR->getDecl()))
292 return;
293
294 // Otherwise, issue a warning.
295 DeadStoreKind dsk = Parents.isConsumedExpr(B)
296 ? Enclosing
297 : (isIncrement(VD,B) ? DeadIncrement : Standard);
298
299 CheckVarDecl(VD, DR, B->getRHS(), dsk, Live);
300 }
301 }
302 else if (const UnaryOperator* U = dyn_cast<UnaryOperator>(S)) {
303 if (!U->isIncrementOp() || U->isPrefix())
304 return;
305
306 const Stmt *parent = Parents.getParentIgnoreParenCasts(U);
307 if (!parent || !isa<ReturnStmt>(parent))
308 return;
309
310 const Expr *Ex = U->getSubExpr()->IgnoreParenCasts();
311
312 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex))
313 CheckDeclRef(DR, U, DeadIncrement, Live);
314 }
315 else if (const DeclStmt *DS = dyn_cast<DeclStmt>(S))
316 // Iterate through the decls. Warn if any initializers are complex
317 // expressions that are not live (never used).
318 for (const auto *DI : DS->decls()) {
319 const auto *V = dyn_cast<VarDecl>(DI);
320
321 if (!V)
322 continue;
323
324 if (V->hasLocalStorage()) {
325 // Reference types confuse the dead stores checker. Skip them
326 // for now.
327 if (V->getType()->getAs<ReferenceType>())
328 return;
329
330 if (const Expr *E = V->getInit()) {
331 while (const ExprWithCleanups *exprClean =
332 dyn_cast<ExprWithCleanups>(E))
333 E = exprClean->getSubExpr();
334
335 // Look through transitive assignments, e.g.:
336 // int x = y = 0;
337 E = LookThroughTransitiveAssignmentsAndCommaOperators(E);
338
339 // Don't warn on C++ objects (yet) until we can show that their
340 // constructors/destructors don't have side effects.
341 if (isa<CXXConstructExpr>(E))
342 return;
343
344 // A dead initialization is a variable that is dead after it
345 // is initialized. We don't flag warnings for those variables
346 // marked 'unused' or 'objc_precise_lifetime'.
347 if (!isLive(Live, V) &&
348 !V->hasAttr<UnusedAttr>() &&
349 !V->hasAttr<ObjCPreciseLifetimeAttr>()) {
350 // Special case: check for initializations with constants.
351 //
352 // e.g. : int x = 0;
353 //
354 // If x is EVER assigned a new value later, don't issue
355 // a warning. This is because such initialization can be
356 // due to defensive programming.
357 if (E->isEvaluatable(Ctx))
358 return;
359
360 if (const DeclRefExpr *DRE =
361 dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()))
362 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
363 // Special case: check for initialization from constant
364 // variables.
365 //
366 // e.g. extern const int MyConstant;
367 // int x = MyConstant;
368 //
369 if (VD->hasGlobalStorage() &&
370 VD->getType().isConstQualified())
371 return;
372 // Special case: check for initialization from scalar
373 // parameters. This is often a form of defensive
374 // programming. Non-scalars are still an error since
375 // because it more likely represents an actual algorithmic
376 // bug.
377 if (isa<ParmVarDecl>(VD) && VD->getType()->isScalarType())
378 return;
379 }
380
381 PathDiagnosticLocation Loc =
382 PathDiagnosticLocation::create(V, BR.getSourceManager());
383 Report(V, DeadInit, Loc, E->getSourceRange());
384 }
385 }
386 }
387 }
388 }
389 };
390
391 } // end anonymous namespace
392
393 //===----------------------------------------------------------------------===//
394 // Driver function to invoke the Dead-Stores checker on a CFG.
395 //===----------------------------------------------------------------------===//
396
397 namespace {
398 class FindEscaped {
399 public:
400 llvm::SmallPtrSet<const VarDecl*, 20> Escaped;
401
operator ()(const Stmt * S)402 void operator()(const Stmt *S) {
403 // Check for '&'. Any VarDecl whose address has been taken we treat as
404 // escaped.
405 // FIXME: What about references?
406 if (auto *LE = dyn_cast<LambdaExpr>(S)) {
407 findLambdaReferenceCaptures(LE);
408 return;
409 }
410
411 const UnaryOperator *U = dyn_cast<UnaryOperator>(S);
412 if (!U)
413 return;
414 if (U->getOpcode() != UO_AddrOf)
415 return;
416
417 const Expr *E = U->getSubExpr()->IgnoreParenCasts();
418 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E))
419 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
420 Escaped.insert(VD);
421 }
422
423 // Treat local variables captured by reference in C++ lambdas as escaped.
findLambdaReferenceCaptures(const LambdaExpr * LE)424 void findLambdaReferenceCaptures(const LambdaExpr *LE) {
425 const CXXRecordDecl *LambdaClass = LE->getLambdaClass();
426 llvm::DenseMap<const VarDecl *, FieldDecl *> CaptureFields;
427 FieldDecl *ThisCaptureField;
428 LambdaClass->getCaptureFields(CaptureFields, ThisCaptureField);
429
430 for (const LambdaCapture &C : LE->captures()) {
431 if (!C.capturesVariable())
432 continue;
433
434 VarDecl *VD = C.getCapturedVar();
435 const FieldDecl *FD = CaptureFields[VD];
436 if (!FD)
437 continue;
438
439 // If the capture field is a reference type, it is capture-by-reference.
440 if (FD->getType()->isReferenceType())
441 Escaped.insert(VD);
442 }
443 }
444 };
445 } // end anonymous namespace
446
447
448 //===----------------------------------------------------------------------===//
449 // DeadStoresChecker
450 //===----------------------------------------------------------------------===//
451
452 namespace {
453 class DeadStoresChecker : public Checker<check::ASTCodeBody> {
454 public:
checkASTCodeBody(const Decl * D,AnalysisManager & mgr,BugReporter & BR) const455 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
456 BugReporter &BR) const {
457
458 // Don't do anything for template instantiations.
459 // Proving that code in a template instantiation is "dead"
460 // means proving that it is dead in all instantiations.
461 // This same problem exists with -Wunreachable-code.
462 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
463 if (FD->isTemplateInstantiation())
464 return;
465
466 if (LiveVariables *L = mgr.getAnalysis<LiveVariables>(D)) {
467 CFG &cfg = *mgr.getCFG(D);
468 AnalysisDeclContext *AC = mgr.getAnalysisDeclContext(D);
469 ParentMap &pmap = mgr.getParentMap(D);
470 FindEscaped FS;
471 cfg.VisitBlockStmts(FS);
472 DeadStoreObs A(cfg, BR.getContext(), BR, this, AC, pmap, FS.Escaped);
473 L->runOnAllBlocks(A);
474 }
475 }
476 };
477 }
478
registerDeadStoresChecker(CheckerManager & mgr)479 void ento::registerDeadStoresChecker(CheckerManager &mgr) {
480 mgr.registerChecker<DeadStoresChecker>();
481 }
482