blob: 00bff53f993cf7e51b642796d1d413789abab985 [file] [log] [blame]
Ted Kremenek56b1f712011-01-13 20:58:56 +00001//==- DeadStoresChecker.cpp - Check for stores to dead variables -*- C++ -*-==//
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +00007//
8//===----------------------------------------------------------------------===//
9//
Gabor Greif843e9342008-03-06 10:40:09 +000010// This file defines a DeadStores, a flow-sensitive checker that looks for
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000011// stores to variables that are no longer live.
12//
13//===----------------------------------------------------------------------===//
14
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +000015#include "ClangSACheckers.h"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000016#include "clang/StaticAnalyzer/Core/Checker.h"
Ted Kremenekcf6e41b2007-12-21 21:42:19 +000017#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenekfdd225e2007-09-25 04:31:27 +000018#include "clang/Analysis/Visitors/CFGRecStmtVisitor.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000019#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
20#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Ted Kremenekf96f16d2009-04-07 05:25:24 +000021#include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h"
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000022#include "clang/Basic/Diagnostic.h"
Ted Kremenekce1cab92007-09-11 17:24:14 +000023#include "clang/AST/ASTContext.h"
Ted Kremenek1a654b62008-06-20 21:45:25 +000024#include "clang/AST/ParentMap.h"
Ted Kremenek2827f5a2012-09-06 22:32:48 +000025#include "clang/AST/RecursiveASTVisitor.h"
Ted Kremenekf96f16d2009-04-07 05:25:24 +000026#include "llvm/ADT/SmallPtrSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000027#include "llvm/ADT/SmallString.h"
Ted Kremenek2827f5a2012-09-06 22:32:48 +000028#include "llvm/Support/SaveAndRestore.h"
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000029
30using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000031using namespace ento;
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000032
Ted Kremenek2827f5a2012-09-06 22:32:48 +000033namespace {
34
35/// A simple visitor to record what VarDecls occur in EH-handling code.
36class EHCodeVisitor : public RecursiveASTVisitor<EHCodeVisitor> {
37public:
38 bool inEH;
39 llvm::DenseSet<const VarDecl *> &S;
40
41 bool TraverseObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
42 SaveAndRestore<bool> inFinally(inEH, true);
43 return ::RecursiveASTVisitor<EHCodeVisitor>::TraverseObjCAtFinallyStmt(S);
44 }
45
46 bool TraverseObjCAtCatchStmt(ObjCAtCatchStmt *S) {
47 SaveAndRestore<bool> inCatch(inEH, true);
48 return ::RecursiveASTVisitor<EHCodeVisitor>::TraverseObjCAtCatchStmt(S);
49 }
50
51 bool TraverseCXXCatchStmt(CXXCatchStmt *S) {
52 SaveAndRestore<bool> inCatch(inEH, true);
53 return TraverseStmt(S->getHandlerBlock());
54 }
55
56 bool VisitDeclRefExpr(DeclRefExpr *DR) {
57 if (inEH)
58 if (const VarDecl *D = dyn_cast<VarDecl>(DR->getDecl()))
59 S.insert(D);
60 return true;
61 }
62
63 EHCodeVisitor(llvm::DenseSet<const VarDecl *> &S) :
64 inEH(false), S(S) {}
65};
Ted Kremenek1a654b62008-06-20 21:45:25 +000066
Ted Kremenek848ec832011-02-11 23:24:26 +000067// FIXME: Eventually migrate into its own file, and have it managed by
68// AnalysisManager.
69class ReachableCode {
70 const CFG &cfg;
71 llvm::BitVector reachable;
72public:
73 ReachableCode(const CFG &cfg)
74 : cfg(cfg), reachable(cfg.getNumBlockIDs(), false) {}
75
76 void computeReachableBlocks();
77
78 bool isReachable(const CFGBlock *block) const {
79 return reachable[block->getBlockID()];
80 }
81};
82}
83
84void ReachableCode::computeReachableBlocks() {
85 if (!cfg.getNumBlockIDs())
86 return;
87
Chris Lattner5f9e2722011-07-23 10:55:15 +000088 SmallVector<const CFGBlock*, 10> worklist;
Ted Kremenek848ec832011-02-11 23:24:26 +000089 worklist.push_back(&cfg.getEntry());
90
91 while (!worklist.empty()) {
92 const CFGBlock *block = worklist.back();
93 worklist.pop_back();
94 llvm::BitVector::reference isReachable = reachable[block->getBlockID()];
95 if (isReachable)
96 continue;
97 isReachable = true;
98 for (CFGBlock::const_succ_iterator i = block->succ_begin(),
99 e = block->succ_end(); i != e; ++i)
100 if (const CFGBlock *succ = *i)
101 worklist.push_back(succ);
102 }
103}
104
Ted Kremenekbb811ca2012-04-04 19:58:03 +0000105static const Expr *LookThroughTransitiveAssignments(const Expr *Ex) {
106 while (Ex) {
107 const BinaryOperator *BO =
108 dyn_cast<BinaryOperator>(Ex->IgnoreParenCasts());
109 if (!BO)
110 break;
111 if (BO->getOpcode() == BO_Assign) {
112 Ex = BO->getRHS();
113 continue;
114 }
115 break;
116 }
117 return Ex;
118}
119
Ted Kremenek848ec832011-02-11 23:24:26 +0000120namespace {
Ted Kremenek88299892011-07-28 23:07:59 +0000121class DeadStoreObs : public LiveVariables::Observer {
Ted Kremenek848ec832011-02-11 23:24:26 +0000122 const CFG &cfg;
Chris Lattnerc0508f92007-09-15 23:21:08 +0000123 ASTContext &Ctx;
Ted Kremenek8f269862008-07-14 20:56:04 +0000124 BugReporter& BR;
Ted Kremenek1d26f482011-10-24 01:32:45 +0000125 AnalysisDeclContext* AC;
Ted Kremenek1a654b62008-06-20 21:45:25 +0000126 ParentMap& Parents;
Ted Kremenek88299892011-07-28 23:07:59 +0000127 llvm::SmallPtrSet<const VarDecl*, 20> Escaped;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000128 OwningPtr<ReachableCode> reachableCode;
Ted Kremenek848ec832011-02-11 23:24:26 +0000129 const CFGBlock *currentBlock;
Ted Kremenek2827f5a2012-09-06 22:32:48 +0000130 llvm::OwningPtr<llvm::DenseSet<const VarDecl *> > InEH;
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Ted Kremenek2cfac222008-07-23 21:16:38 +0000132 enum DeadStoreKind { Standard, Enclosing, DeadIncrement, DeadInit };
Mike Stump1eb44332009-09-09 15:08:12 +0000133
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000134public:
Ted Kremenek848ec832011-02-11 23:24:26 +0000135 DeadStoreObs(const CFG &cfg, ASTContext &ctx,
Ted Kremenek1d26f482011-10-24 01:32:45 +0000136 BugReporter& br, AnalysisDeclContext* ac, ParentMap& parents,
Ted Kremenek88299892011-07-28 23:07:59 +0000137 llvm::SmallPtrSet<const VarDecl*, 20> &escaped)
Anna Zaks590dd8e2011-09-20 21:38:35 +0000138 : cfg(cfg), Ctx(ctx), BR(br), AC(ac), Parents(parents),
Ted Kremenek848ec832011-02-11 23:24:26 +0000139 Escaped(escaped), currentBlock(0) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000141 virtual ~DeadStoreObs() {}
Ted Kremenekb930d7a2009-04-01 06:52:48 +0000142
Ted Kremenek2827f5a2012-09-06 22:32:48 +0000143 bool isLive(const LiveVariables::LivenessValues &Live, const VarDecl *D) {
144 if (Live.isLive(D))
145 return true;
146 // Lazily construct the set that records which VarDecls are in
147 // EH code.
148 if (!InEH.get()) {
149 InEH.reset(new llvm::DenseSet<const VarDecl *>());
150 EHCodeVisitor V(*InEH.get());
151 V.TraverseStmt(AC->getBody());
152 }
153 // Treat all VarDecls that occur in EH code as being "always live"
154 // when considering to suppress dead stores. Frequently stores
155 // are followed by reads in EH code, but we don't have the ability
156 // to analyze that yet.
157 return InEH->count(D);
158 }
159
Ted Kremenek9c378f72011-08-12 23:37:29 +0000160 void Report(const VarDecl *V, DeadStoreKind dsk,
Anna Zaks590dd8e2011-09-20 21:38:35 +0000161 PathDiagnosticLocation L, SourceRange R) {
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000162 if (Escaped.count(V))
163 return;
Ted Kremenek848ec832011-02-11 23:24:26 +0000164
165 // Compute reachable blocks within the CFG for trivial cases
166 // where a bogus dead store can be reported because itself is unreachable.
167 if (!reachableCode.get()) {
168 reachableCode.reset(new ReachableCode(cfg));
169 reachableCode->computeReachableBlocks();
170 }
171
172 if (!reachableCode->isReachable(currentBlock))
173 return;
Ted Kremenek8f269862008-07-14 20:56:04 +0000174
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000175 SmallString<64> buf;
Jordy Rose7df12342011-08-21 05:25:15 +0000176 llvm::raw_svector_ostream os(buf);
177 const char *BugType = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Ted Kremenek2cfac222008-07-23 21:16:38 +0000179 switch (dsk) {
Ted Kremenek2cfac222008-07-23 21:16:38 +0000180 case DeadInit:
Ted Kremenekefc620c2009-04-02 22:50:16 +0000181 BugType = "Dead initialization";
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000182 os << "Value stored to '" << *V
Jordy Rose7df12342011-08-21 05:25:15 +0000183 << "' during its initialization is never read";
Ted Kremenek2cfac222008-07-23 21:16:38 +0000184 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Ted Kremenek2cfac222008-07-23 21:16:38 +0000186 case DeadIncrement:
Ted Kremenekefc620c2009-04-02 22:50:16 +0000187 BugType = "Dead increment";
Ted Kremenek2cfac222008-07-23 21:16:38 +0000188 case Standard:
Ted Kremenekefc620c2009-04-02 22:50:16 +0000189 if (!BugType) BugType = "Dead assignment";
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000190 os << "Value stored to '" << *V << "' is never read";
Ted Kremenek2cfac222008-07-23 21:16:38 +0000191 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Ted Kremenek2cfac222008-07-23 21:16:38 +0000193 case Enclosing:
Ted Kremenek56b1f712011-01-13 20:58:56 +0000194 // Don't report issues in this case, e.g.: "if (x = foo())",
195 // where 'x' is unused later. We have yet to see a case where
196 // this is a real bug.
197 return;
Ted Kremenekf9c2a5d2008-07-15 18:06:32 +0000198 }
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Ted Kremenek07189522012-04-04 18:11:35 +0000200 BR.EmitBasicReport(AC->getDecl(), BugType, "Dead store", os.str(), L, R);
Ted Kremenek1a654b62008-06-20 21:45:25 +0000201 }
Mike Stump1eb44332009-09-09 15:08:12 +0000202
Ted Kremenek9c378f72011-08-12 23:37:29 +0000203 void CheckVarDecl(const VarDecl *VD, const Expr *Ex, const Expr *Val,
Ted Kremenek2cfac222008-07-23 21:16:38 +0000204 DeadStoreKind dsk,
Ted Kremenek88299892011-07-28 23:07:59 +0000205 const LiveVariables::LivenessValues &Live) {
Ted Kremenek1a654b62008-06-20 21:45:25 +0000206
Ted Kremenek852274d2009-12-16 03:18:58 +0000207 if (!VD->hasLocalStorage())
208 return;
209 // Reference types confuse the dead stores checker. Skip them
210 // for now.
211 if (VD->getType()->getAs<ReferenceType>())
212 return;
213
Ted Kremenek2827f5a2012-09-06 22:32:48 +0000214 if (!isLive(Live, VD) &&
Anna Zaks590dd8e2011-09-20 21:38:35 +0000215 !(VD->getAttr<UnusedAttr>() || VD->getAttr<BlocksAttr>())) {
216
217 PathDiagnosticLocation ExLoc =
218 PathDiagnosticLocation::createBegin(Ex, BR.getSourceManager(), AC);
219 Report(VD, dsk, ExLoc, Val->getSourceRange());
220 }
Ted Kremenek3eb817e2008-05-21 22:59:16 +0000221 }
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Ted Kremenek9c378f72011-08-12 23:37:29 +0000223 void CheckDeclRef(const DeclRefExpr *DR, const Expr *Val, DeadStoreKind dsk,
Ted Kremenek88299892011-07-28 23:07:59 +0000224 const LiveVariables::LivenessValues& Live) {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000225 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremenek88299892011-07-28 23:07:59 +0000226 CheckVarDecl(VD, DR, Val, dsk, Live);
Ted Kremenek2cfac222008-07-23 21:16:38 +0000227 }
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Ted Kremenek9c378f72011-08-12 23:37:29 +0000229 bool isIncrement(VarDecl *VD, const BinaryOperator* B) {
Ted Kremenek2cfac222008-07-23 21:16:38 +0000230 if (B->isCompoundAssignmentOp())
231 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Ted Kremenek9c378f72011-08-12 23:37:29 +0000233 const Expr *RHS = B->getRHS()->IgnoreParenCasts();
Ted Kremenek88299892011-07-28 23:07:59 +0000234 const BinaryOperator* BRHS = dyn_cast<BinaryOperator>(RHS);
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Ted Kremenek2cfac222008-07-23 21:16:38 +0000236 if (!BRHS)
237 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Ted Kremenek88299892011-07-28 23:07:59 +0000239 const DeclRefExpr *DR;
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Ted Kremenek2cfac222008-07-23 21:16:38 +0000241 if ((DR = dyn_cast<DeclRefExpr>(BRHS->getLHS()->IgnoreParenCasts())))
242 if (DR->getDecl() == VD)
243 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Ted Kremenek2cfac222008-07-23 21:16:38 +0000245 if ((DR = dyn_cast<DeclRefExpr>(BRHS->getRHS()->IgnoreParenCasts())))
246 if (DR->getDecl() == VD)
247 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Ted Kremenek2cfac222008-07-23 21:16:38 +0000249 return false;
Ted Kremeneka23157e2008-05-05 23:12:21 +0000250 }
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Ted Kremenek9c378f72011-08-12 23:37:29 +0000252 virtual void observeStmt(const Stmt *S, const CFGBlock *block,
Ted Kremenek88299892011-07-28 23:07:59 +0000253 const LiveVariables::LivenessValues &Live) {
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Ted Kremenek848ec832011-02-11 23:24:26 +0000255 currentBlock = block;
256
Ted Kremenek1c86b152008-04-14 18:28:25 +0000257 // Skip statements in macros.
258 if (S->getLocStart().isMacroID())
259 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Ted Kremenekf4e532b2011-02-12 00:17:19 +0000261 // Only cover dead stores from regular assignments. ++/-- dead stores
262 // have never flagged a real bug.
Ted Kremenek88299892011-07-28 23:07:59 +0000263 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000264 if (!B->isAssignmentOp()) return; // Skip non-assignments.
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Ted Kremenek9c378f72011-08-12 23:37:29 +0000266 if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(B->getLHS()))
Ted Kremenek1a654b62008-06-20 21:45:25 +0000267 if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremeneke12691c2008-08-09 00:05:14 +0000268 // Special case: check for assigning null to a pointer.
Mike Stump1eb44332009-09-09 15:08:12 +0000269 // This is a common form of defensive programming.
Ted Kremenekbb811ca2012-04-04 19:58:03 +0000270 const Expr *RHS = LookThroughTransitiveAssignments(B->getRHS());
271
Ted Kremenek89132202010-02-23 21:19:33 +0000272 QualType T = VD->getType();
273 if (T->isPointerType() || T->isObjCObjectPointerType()) {
Ted Kremenekbb811ca2012-04-04 19:58:03 +0000274 if (RHS->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNull))
Ted Kremenek93fab7c2009-11-22 20:26:21 +0000275 return;
Ted Kremeneke12691c2008-08-09 00:05:14 +0000276 }
Ted Kremenek93fab7c2009-11-22 20:26:21 +0000277
Ted Kremenekbb811ca2012-04-04 19:58:03 +0000278 RHS = RHS->IgnoreParenCasts();
Ted Kremenek3b587862009-01-09 22:15:01 +0000279 // Special case: self-assignments. These are often used to shut up
280 // "unused variable" compiler warnings.
Ted Kremenekbb811ca2012-04-04 19:58:03 +0000281 if (const DeclRefExpr *RhsDR = dyn_cast<DeclRefExpr>(RHS))
Ted Kremenek3b587862009-01-09 22:15:01 +0000282 if (VD == dyn_cast<VarDecl>(RhsDR->getDecl()))
283 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Ted Kremenek3b587862009-01-09 22:15:01 +0000285 // Otherwise, issue a warning.
Ted Kremenekb930d7a2009-04-01 06:52:48 +0000286 DeadStoreKind dsk = Parents.isConsumedExpr(B)
Mike Stump1eb44332009-09-09 15:08:12 +0000287 ? Enclosing
Ted Kremenek7f5fce72009-01-20 00:47:45 +0000288 : (isIncrement(VD,B) ? DeadIncrement : Standard);
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Ted Kremenek88299892011-07-28 23:07:59 +0000290 CheckVarDecl(VD, DR, B->getRHS(), dsk, Live);
Mike Stump1eb44332009-09-09 15:08:12 +0000291 }
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000292 }
Ted Kremenek88299892011-07-28 23:07:59 +0000293 else if (const UnaryOperator* U = dyn_cast<UnaryOperator>(S)) {
Ted Kremenekf4e532b2011-02-12 00:17:19 +0000294 if (!U->isIncrementOp() || U->isPrefix())
Ted Kremeneka23157e2008-05-05 23:12:21 +0000295 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Ted Kremenek88299892011-07-28 23:07:59 +0000297 const Stmt *parent = Parents.getParentIgnoreParenCasts(U);
Ted Kremenekf4e532b2011-02-12 00:17:19 +0000298 if (!parent || !isa<ReturnStmt>(parent))
Ted Kremenek380277e2008-10-15 05:23:41 +0000299 return;
Ted Kremenekb0f36322008-07-24 17:01:17 +0000300
Ted Kremenek88299892011-07-28 23:07:59 +0000301 const Expr *Ex = U->getSubExpr()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Ted Kremenek9c378f72011-08-12 23:37:29 +0000303 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex))
Ted Kremenek88299892011-07-28 23:07:59 +0000304 CheckDeclRef(DR, U, DeadIncrement, Live);
Mike Stump1eb44332009-09-09 15:08:12 +0000305 }
Ted Kremenek9c378f72011-08-12 23:37:29 +0000306 else if (const DeclStmt *DS = dyn_cast<DeclStmt>(S))
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000307 // Iterate through the decls. Warn if any initializers are complex
308 // expressions that are not live (never used).
Ted Kremenek88299892011-07-28 23:07:59 +0000309 for (DeclStmt::const_decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000310 DI != DE; ++DI) {
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Ted Kremenek9c378f72011-08-12 23:37:29 +0000312 VarDecl *V = dyn_cast<VarDecl>(*DI);
Ted Kremenekfc7ff552008-07-25 04:47:34 +0000313
314 if (!V)
315 continue;
Ted Kremenek852274d2009-12-16 03:18:58 +0000316
317 if (V->hasLocalStorage()) {
318 // Reference types confuse the dead stores checker. Skip them
319 // for now.
320 if (V->getType()->getAs<ReferenceType>())
321 return;
322
Ted Kremenekbb811ca2012-04-04 19:58:03 +0000323 if (const Expr *E = V->getInit()) {
324 while (const ExprWithCleanups *exprClean =
325 dyn_cast<ExprWithCleanups>(E))
John McCallf85e1932011-06-15 23:02:42 +0000326 E = exprClean->getSubExpr();
327
Ted Kremenekbb811ca2012-04-04 19:58:03 +0000328 // Look through transitive assignments, e.g.:
329 // int x = y = 0;
330 E = LookThroughTransitiveAssignments(E);
331
Ted Kremenek43f19e32009-12-15 04:12:12 +0000332 // Don't warn on C++ objects (yet) until we can show that their
333 // constructors/destructors don't have side effects.
334 if (isa<CXXConstructExpr>(E))
335 return;
Ted Kremenek604d9392009-12-23 04:11:44 +0000336
Ted Kremenekfc7ff552008-07-25 04:47:34 +0000337 // A dead initialization is a variable that is dead after it
338 // is initialized. We don't flag warnings for those variables
339 // marked 'unused'.
Ted Kremenek2827f5a2012-09-06 22:32:48 +0000340 if (!isLive(Live, V) && V->getAttr<UnusedAttr>() == 0) {
Ted Kremenekc6a1faf2007-09-28 20:48:41 +0000341 // Special case: check for initializations with constants.
342 //
343 // e.g. : int x = 0;
344 //
345 // If x is EVER assigned a new value later, don't issue
346 // a warning. This is because such initialization can be
347 // due to defensive programming.
Richard Smith0e35b4e2011-12-06 23:25:15 +0000348 if (E->isEvaluatable(Ctx))
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000349 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Ted Kremenekbb811ca2012-04-04 19:58:03 +0000351 if (const DeclRefExpr *DRE =
352 dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()))
353 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
Ted Kremenekebd42f42010-03-18 01:22:39 +0000354 // Special case: check for initialization from constant
355 // variables.
356 //
357 // e.g. extern const int MyConstant;
358 // int x = MyConstant;
359 //
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000360 if (VD->hasGlobalStorage() &&
Ted Kremenekebd42f42010-03-18 01:22:39 +0000361 VD->getType().isConstQualified())
362 return;
363 // Special case: check for initialization from scalar
364 // parameters. This is often a form of defensive
365 // programming. Non-scalars are still an error since
366 // because it more likely represents an actual algorithmic
367 // bug.
368 if (isa<ParmVarDecl>(VD) && VD->getType()->isScalarType())
369 return;
370 }
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Anna Zaks590dd8e2011-09-20 21:38:35 +0000372 PathDiagnosticLocation Loc =
373 PathDiagnosticLocation::create(V, BR.getSourceManager());
374 Report(V, DeadInit, Loc, E->getSourceRange());
Ted Kremenekce1cab92007-09-11 17:24:14 +0000375 }
Ted Kremenekfc7ff552008-07-25 04:47:34 +0000376 }
Ted Kremenek852274d2009-12-16 03:18:58 +0000377 }
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000378 }
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000379 }
380};
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000382} // end anonymous namespace
383
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000384//===----------------------------------------------------------------------===//
Ted Kremeneke2075582008-07-02 23:16:33 +0000385// Driver function to invoke the Dead-Stores checker on a CFG.
386//===----------------------------------------------------------------------===//
387
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000388namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000389class FindEscaped : public CFGRecStmtDeclVisitor<FindEscaped>{
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000390 CFG *cfg;
391public:
392 FindEscaped(CFG *c) : cfg(c) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000394 CFG& getCFG() { return *cfg; }
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Ted Kremenek88299892011-07-28 23:07:59 +0000396 llvm::SmallPtrSet<const VarDecl*, 20> Escaped;
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000397
398 void VisitUnaryOperator(UnaryOperator* U) {
399 // Check for '&'. Any VarDecl whose value has its address-taken we
400 // treat as escaped.
Ted Kremenek9c378f72011-08-12 23:37:29 +0000401 Expr *E = U->getSubExpr()->IgnoreParenCasts();
John McCall2de56d12010-08-25 11:45:40 +0000402 if (U->getOpcode() == UO_AddrOf)
Ted Kremenek9c378f72011-08-12 23:37:29 +0000403 if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E))
404 if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000405 Escaped.insert(VD);
406 return;
407 }
408 Visit(E);
409 }
410};
411} // end anonymous namespace
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000413
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000414//===----------------------------------------------------------------------===//
415// DeadStoresChecker
416//===----------------------------------------------------------------------===//
417
418namespace {
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +0000419class DeadStoresChecker : public Checker<check::ASTCodeBody> {
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000420public:
421 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
422 BugReporter &BR) const {
Ted Kremeneka5937bb2011-10-07 22:21:02 +0000423 if (LiveVariables *L = mgr.getAnalysis<LiveVariables>(D)) {
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000424 CFG &cfg = *mgr.getCFG(D);
Ted Kremenek1d26f482011-10-24 01:32:45 +0000425 AnalysisDeclContext *AC = mgr.getAnalysisDeclContext(D);
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000426 ParentMap &pmap = mgr.getParentMap(D);
427 FindEscaped FS(&cfg);
428 FS.getCFG().VisitBlockStmts(FS);
Anna Zaks590dd8e2011-09-20 21:38:35 +0000429 DeadStoreObs A(cfg, BR.getContext(), BR, AC, pmap, FS.Escaped);
Ted Kremenek88299892011-07-28 23:07:59 +0000430 L->runOnAllBlocks(A);
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000431 }
432 }
433};
434}
435
436void ento::registerDeadStoresChecker(CheckerManager &mgr) {
437 mgr.registerChecker<DeadStoresChecker>();
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000438}