1 //==- DeadStoresChecker.cpp - Check for stores to dead variables -*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a DeadStores, a flow-sensitive checker that looks for
11 // stores to variables that are no longer live.
13 //===----------------------------------------------------------------------===//
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"
28 using namespace clang;
33 /// A simple visitor to record what VarDecls occur in EH-handling code.
34 class EHCodeVisitor : public RecursiveASTVisitor<EHCodeVisitor> {
37 llvm::DenseSet<const VarDecl *> &S;
39 bool TraverseObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
40 SaveAndRestore<bool> inFinally(inEH, true);
41 return ::RecursiveASTVisitor<EHCodeVisitor>::TraverseObjCAtFinallyStmt(S);
44 bool TraverseObjCAtCatchStmt(ObjCAtCatchStmt *S) {
45 SaveAndRestore<bool> inCatch(inEH, true);
46 return ::RecursiveASTVisitor<EHCodeVisitor>::TraverseObjCAtCatchStmt(S);
49 bool TraverseCXXCatchStmt(CXXCatchStmt *S) {
50 SaveAndRestore<bool> inCatch(inEH, true);
51 return TraverseStmt(S->getHandlerBlock());
54 bool VisitDeclRefExpr(DeclRefExpr *DR) {
56 if (const VarDecl *D = dyn_cast<VarDecl>(DR->getDecl()))
61 EHCodeVisitor(llvm::DenseSet<const VarDecl *> &S) :
65 // FIXME: Eventually migrate into its own file, and have it managed by
69 llvm::BitVector reachable;
71 ReachableCode(const CFG &cfg)
72 : cfg(cfg), reachable(cfg.getNumBlockIDs(), false) {}
74 void computeReachableBlocks();
76 bool isReachable(const CFGBlock *block) const {
77 return reachable[block->getBlockID()];
82 void ReachableCode::computeReachableBlocks() {
83 if (!cfg.getNumBlockIDs())
86 SmallVector<const CFGBlock*, 10> worklist;
87 worklist.push_back(&cfg.getEntry());
89 while (!worklist.empty()) {
90 const CFGBlock *block = worklist.pop_back_val();
91 llvm::BitVector::reference isReachable = reachable[block->getBlockID()];
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);
103 LookThroughTransitiveAssignmentsAndCommaOperators(const Expr *Ex) {
105 const BinaryOperator *BO =
106 dyn_cast<BinaryOperator>(Ex->IgnoreParenCasts());
109 if (BO->getOpcode() == BO_Assign) {
113 if (BO->getOpcode() == BO_Comma) {
123 class DeadStoreObs : public LiveVariables::Observer {
127 const CheckerBase *Checker;
128 AnalysisDeclContext* AC;
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;
135 enum DeadStoreKind { Standard, Enclosing, DeadIncrement, DeadInit };
138 DeadStoreObs(const CFG &cfg, ASTContext &ctx, BugReporter &br,
139 const CheckerBase *checker, AnalysisDeclContext *ac,
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) {}
145 ~DeadStoreObs() override {}
147 bool isLive(const LiveVariables::LivenessValues &Live, const VarDecl *D) {
150 // Lazily construct the set that records which VarDecls are in
153 InEH.reset(new llvm::DenseSet<const VarDecl *>());
154 EHCodeVisitor V(*InEH.get());
155 V.TraverseStmt(AC->getBody());
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);
164 void Report(const VarDecl *V, DeadStoreKind dsk,
165 PathDiagnosticLocation L, SourceRange R) {
166 if (Escaped.count(V))
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();
176 if (!reachableCode->isReachable(currentBlock))
180 llvm::raw_svector_ostream os(buf);
181 const char *BugType = nullptr;
185 BugType = "Dead initialization";
186 os << "Value stored to '" << *V
187 << "' during its initialization is never read";
191 BugType = "Dead increment";
194 if (!BugType) BugType = "Dead assignment";
195 os << "Value stored to '" << *V << "' is never read";
199 // Don't report issues in this case, e.g.: "if (x = foo())",
200 // where 'x' is unused later. We have yet to see a case where
201 // this is a real bug.
205 BR.EmitBasicReport(AC->getDecl(), Checker, BugType, "Dead store", os.str(),
209 void CheckVarDecl(const VarDecl *VD, const Expr *Ex, const Expr *Val,
211 const LiveVariables::LivenessValues &Live) {
213 if (!VD->hasLocalStorage())
215 // Reference types confuse the dead stores checker. Skip them
217 if (VD->getType()->getAs<ReferenceType>())
220 if (!isLive(Live, VD) &&
221 !(VD->hasAttr<UnusedAttr>() || VD->hasAttr<BlocksAttr>() ||
222 VD->hasAttr<ObjCPreciseLifetimeAttr>())) {
224 PathDiagnosticLocation ExLoc =
225 PathDiagnosticLocation::createBegin(Ex, BR.getSourceManager(), AC);
226 Report(VD, dsk, ExLoc, Val->getSourceRange());
230 void CheckDeclRef(const DeclRefExpr *DR, const Expr *Val, DeadStoreKind dsk,
231 const LiveVariables::LivenessValues& Live) {
232 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
233 CheckVarDecl(VD, DR, Val, dsk, Live);
236 bool isIncrement(VarDecl *VD, const BinaryOperator* B) {
237 if (B->isCompoundAssignmentOp())
240 const Expr *RHS = B->getRHS()->IgnoreParenCasts();
241 const BinaryOperator* BRHS = dyn_cast<BinaryOperator>(RHS);
246 const DeclRefExpr *DR;
248 if ((DR = dyn_cast<DeclRefExpr>(BRHS->getLHS()->IgnoreParenCasts())))
249 if (DR->getDecl() == VD)
252 if ((DR = dyn_cast<DeclRefExpr>(BRHS->getRHS()->IgnoreParenCasts())))
253 if (DR->getDecl() == VD)
259 void observeStmt(const Stmt *S, const CFGBlock *block,
260 const LiveVariables::LivenessValues &Live) override {
262 currentBlock = block;
264 // Skip statements in macros.
265 if (S->getLocStart().isMacroID())
268 // Only cover dead stores from regular assignments. ++/-- dead stores
269 // have never flagged a real bug.
270 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
271 if (!B->isAssignmentOp()) return; // Skip non-assignments.
273 if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(B->getLHS()))
274 if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
275 // Special case: check for assigning null to a pointer.
276 // This is a common form of defensive programming.
278 LookThroughTransitiveAssignmentsAndCommaOperators(B->getRHS());
279 RHS = RHS->IgnoreParenCasts();
281 QualType T = VD->getType();
282 if (T.isVolatileQualified())
284 if (T->isPointerType() || T->isObjCObjectPointerType()) {
285 if (RHS->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNull))
289 // Special case: self-assignments. These are often used to shut up
290 // "unused variable" compiler warnings.
291 if (const DeclRefExpr *RhsDR = dyn_cast<DeclRefExpr>(RHS))
292 if (VD == dyn_cast<VarDecl>(RhsDR->getDecl()))
295 // Otherwise, issue a warning.
296 DeadStoreKind dsk = Parents.isConsumedExpr(B)
298 : (isIncrement(VD,B) ? DeadIncrement : Standard);
300 CheckVarDecl(VD, DR, B->getRHS(), dsk, Live);
303 else if (const UnaryOperator* U = dyn_cast<UnaryOperator>(S)) {
304 if (!U->isIncrementOp() || U->isPrefix())
307 const Stmt *parent = Parents.getParentIgnoreParenCasts(U);
308 if (!parent || !isa<ReturnStmt>(parent))
311 const Expr *Ex = U->getSubExpr()->IgnoreParenCasts();
313 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex))
314 CheckDeclRef(DR, U, DeadIncrement, Live);
316 else if (const DeclStmt *DS = dyn_cast<DeclStmt>(S))
317 // Iterate through the decls. Warn if any initializers are complex
318 // expressions that are not live (never used).
319 for (const auto *DI : DS->decls()) {
320 const auto *V = dyn_cast<VarDecl>(DI);
325 if (V->hasLocalStorage()) {
326 // Reference types confuse the dead stores checker. Skip them
328 if (V->getType()->getAs<ReferenceType>())
331 if (const Expr *E = V->getInit()) {
332 while (const ExprWithCleanups *exprClean =
333 dyn_cast<ExprWithCleanups>(E))
334 E = exprClean->getSubExpr();
336 // Look through transitive assignments, e.g.:
338 E = LookThroughTransitiveAssignmentsAndCommaOperators(E);
340 // Don't warn on C++ objects (yet) until we can show that their
341 // constructors/destructors don't have side effects.
342 if (isa<CXXConstructExpr>(E))
345 // A dead initialization is a variable that is dead after it
346 // is initialized. We don't flag warnings for those variables
347 // marked 'unused' or 'objc_precise_lifetime'.
348 if (!isLive(Live, V) &&
349 !V->hasAttr<UnusedAttr>() &&
350 !V->hasAttr<ObjCPreciseLifetimeAttr>()) {
351 // Special case: check for initializations with constants.
355 // If x is EVER assigned a new value later, don't issue
356 // a warning. This is because such initialization can be
357 // due to defensive programming.
358 if (E->isEvaluatable(Ctx))
361 if (const DeclRefExpr *DRE =
362 dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()))
363 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
364 // Special case: check for initialization from constant
367 // e.g. extern const int MyConstant;
368 // int x = MyConstant;
370 if (VD->hasGlobalStorage() &&
371 VD->getType().isConstQualified())
373 // Special case: check for initialization from scalar
374 // parameters. This is often a form of defensive
375 // programming. Non-scalars are still an error since
376 // because it more likely represents an actual algorithmic
378 if (isa<ParmVarDecl>(VD) && VD->getType()->isScalarType())
382 PathDiagnosticLocation Loc =
383 PathDiagnosticLocation::create(V, BR.getSourceManager());
384 Report(V, DeadInit, Loc, E->getSourceRange());
392 } // end anonymous namespace
394 //===----------------------------------------------------------------------===//
395 // Driver function to invoke the Dead-Stores checker on a CFG.
396 //===----------------------------------------------------------------------===//
401 llvm::SmallPtrSet<const VarDecl*, 20> Escaped;
403 void operator()(const Stmt *S) {
404 // Check for '&'. Any VarDecl whose address has been taken we treat as
406 // FIXME: What about references?
407 if (auto *LE = dyn_cast<LambdaExpr>(S)) {
408 findLambdaReferenceCaptures(LE);
412 const UnaryOperator *U = dyn_cast<UnaryOperator>(S);
415 if (U->getOpcode() != UO_AddrOf)
418 const Expr *E = U->getSubExpr()->IgnoreParenCasts();
419 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E))
420 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
424 // Treat local variables captured by reference in C++ lambdas as escaped.
425 void findLambdaReferenceCaptures(const LambdaExpr *LE) {
426 const CXXRecordDecl *LambdaClass = LE->getLambdaClass();
427 llvm::DenseMap<const VarDecl *, FieldDecl *> CaptureFields;
428 FieldDecl *ThisCaptureField;
429 LambdaClass->getCaptureFields(CaptureFields, ThisCaptureField);
431 for (const LambdaCapture &C : LE->captures()) {
432 if (!C.capturesVariable())
435 VarDecl *VD = C.getCapturedVar();
436 const FieldDecl *FD = CaptureFields[VD];
440 // If the capture field is a reference type, it is capture-by-reference.
441 if (FD->getType()->isReferenceType())
446 } // end anonymous namespace
449 //===----------------------------------------------------------------------===//
451 //===----------------------------------------------------------------------===//
454 class DeadStoresChecker : public Checker<check::ASTCodeBody> {
456 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
457 BugReporter &BR) const {
459 // Don't do anything for template instantiations.
460 // Proving that code in a template instantiation is "dead"
461 // means proving that it is dead in all instantiations.
462 // This same problem exists with -Wunreachable-code.
463 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
464 if (FD->isTemplateInstantiation())
467 if (LiveVariables *L = mgr.getAnalysis<LiveVariables>(D)) {
468 CFG &cfg = *mgr.getCFG(D);
469 AnalysisDeclContext *AC = mgr.getAnalysisDeclContext(D);
470 ParentMap &pmap = mgr.getParentMap(D);
472 cfg.VisitBlockStmts(FS);
473 DeadStoreObs A(cfg, BR.getContext(), BR, this, AC, pmap, FS.Escaped);
474 L->runOnAllBlocks(A);
480 void ento::registerDeadStoresChecker(CheckerManager &mgr) {
481 mgr.registerChecker<DeadStoresChecker>();