blob: 59e03ecd5c6192113ca1a9f044bac8f5df3222d2 [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"
Ted Kremenekce1cab92007-09-11 17:24:14 +000016#include "clang/AST/ASTContext.h"
Ted Kremenek1a654b62008-06-20 21:45:25 +000017#include "clang/AST/ParentMap.h"
Ted Kremenek2827f5a2012-09-06 22:32:48 +000018#include "clang/AST/RecursiveASTVisitor.h"
Ted Kremenek19948ac2012-10-30 04:43:51 +000019#include "clang/Analysis/Analyses/LiveVariables.h"
20#include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.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"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000025#include "llvm/ADT/SmallString.h"
Ted Kremenek2827f5a2012-09-06 22:32:48 +000026#include "llvm/Support/SaveAndRestore.h"
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000027
28using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000029using namespace ento;
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000030
Ted Kremenek2827f5a2012-09-06 22:32:48 +000031namespace {
32
33/// A simple visitor to record what VarDecls occur in EH-handling code.
34class EHCodeVisitor : public RecursiveASTVisitor<EHCodeVisitor> {
35public:
36 bool inEH;
37 llvm::DenseSet<const VarDecl *> &S;
38
39 bool TraverseObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
40 SaveAndRestore<bool> inFinally(inEH, true);
41 return ::RecursiveASTVisitor<EHCodeVisitor>::TraverseObjCAtFinallyStmt(S);
42 }
43
44 bool TraverseObjCAtCatchStmt(ObjCAtCatchStmt *S) {
45 SaveAndRestore<bool> inCatch(inEH, true);
46 return ::RecursiveASTVisitor<EHCodeVisitor>::TraverseObjCAtCatchStmt(S);
47 }
48
49 bool TraverseCXXCatchStmt(CXXCatchStmt *S) {
50 SaveAndRestore<bool> inCatch(inEH, true);
51 return TraverseStmt(S->getHandlerBlock());
52 }
53
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
61 EHCodeVisitor(llvm::DenseSet<const VarDecl *> &S) :
62 inEH(false), S(S) {}
63};
Ted Kremenek1a654b62008-06-20 21:45:25 +000064
Ted Kremenek848ec832011-02-11 23:24:26 +000065// FIXME: Eventually migrate into its own file, and have it managed by
66// AnalysisManager.
67class ReachableCode {
68 const CFG &cfg;
69 llvm::BitVector reachable;
70public:
71 ReachableCode(const CFG &cfg)
72 : cfg(cfg), reachable(cfg.getNumBlockIDs(), false) {}
73
74 void computeReachableBlocks();
75
76 bool isReachable(const CFGBlock *block) const {
77 return reachable[block->getBlockID()];
78 }
79};
80}
81
82void ReachableCode::computeReachableBlocks() {
83 if (!cfg.getNumBlockIDs())
84 return;
85
Chris Lattner5f9e2722011-07-23 10:55:15 +000086 SmallVector<const CFGBlock*, 10> worklist;
Ted Kremenek848ec832011-02-11 23:24:26 +000087 worklist.push_back(&cfg.getEntry());
88
89 while (!worklist.empty()) {
90 const CFGBlock *block = worklist.back();
91 worklist.pop_back();
92 llvm::BitVector::reference isReachable = reachable[block->getBlockID()];
93 if (isReachable)
94 continue;
95 isReachable = true;
96 for (CFGBlock::const_succ_iterator i = block->succ_begin(),
97 e = block->succ_end(); i != e; ++i)
98 if (const CFGBlock *succ = *i)
99 worklist.push_back(succ);
100 }
101}
102
Ted Kremenekbb811ca2012-04-04 19:58:03 +0000103static const Expr *LookThroughTransitiveAssignments(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 break;
114 }
115 return Ex;
116}
117
Ted Kremenek848ec832011-02-11 23:24:26 +0000118namespace {
Ted Kremenek88299892011-07-28 23:07:59 +0000119class DeadStoreObs : public LiveVariables::Observer {
Ted Kremenek848ec832011-02-11 23:24:26 +0000120 const CFG &cfg;
Chris Lattnerc0508f92007-09-15 23:21:08 +0000121 ASTContext &Ctx;
Ted Kremenek8f269862008-07-14 20:56:04 +0000122 BugReporter& BR;
Ted Kremenek1d26f482011-10-24 01:32:45 +0000123 AnalysisDeclContext* AC;
Ted Kremenek1a654b62008-06-20 21:45:25 +0000124 ParentMap& Parents;
Ted Kremenek88299892011-07-28 23:07:59 +0000125 llvm::SmallPtrSet<const VarDecl*, 20> Escaped;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000126 OwningPtr<ReachableCode> reachableCode;
Ted Kremenek848ec832011-02-11 23:24:26 +0000127 const CFGBlock *currentBlock;
Ted Kremenek2827f5a2012-09-06 22:32:48 +0000128 llvm::OwningPtr<llvm::DenseSet<const VarDecl *> > InEH;
Mike Stump1eb44332009-09-09 15:08:12 +0000129
Ted Kremenek2cfac222008-07-23 21:16:38 +0000130 enum DeadStoreKind { Standard, Enclosing, DeadIncrement, DeadInit };
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000132public:
Ted Kremenek848ec832011-02-11 23:24:26 +0000133 DeadStoreObs(const CFG &cfg, ASTContext &ctx,
Ted Kremenek1d26f482011-10-24 01:32:45 +0000134 BugReporter& br, AnalysisDeclContext* ac, ParentMap& parents,
Ted Kremenek88299892011-07-28 23:07:59 +0000135 llvm::SmallPtrSet<const VarDecl*, 20> &escaped)
Anna Zaks590dd8e2011-09-20 21:38:35 +0000136 : cfg(cfg), Ctx(ctx), BR(br), AC(ac), Parents(parents),
Ted Kremenek848ec832011-02-11 23:24:26 +0000137 Escaped(escaped), currentBlock(0) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000139 virtual ~DeadStoreObs() {}
Ted Kremenekb930d7a2009-04-01 06:52:48 +0000140
Ted Kremenek2827f5a2012-09-06 22:32:48 +0000141 bool isLive(const LiveVariables::LivenessValues &Live, const VarDecl *D) {
142 if (Live.isLive(D))
143 return true;
144 // Lazily construct the set that records which VarDecls are in
145 // EH code.
146 if (!InEH.get()) {
147 InEH.reset(new llvm::DenseSet<const VarDecl *>());
148 EHCodeVisitor V(*InEH.get());
149 V.TraverseStmt(AC->getBody());
150 }
151 // Treat all VarDecls that occur in EH code as being "always live"
152 // when considering to suppress dead stores. Frequently stores
153 // are followed by reads in EH code, but we don't have the ability
154 // to analyze that yet.
155 return InEH->count(D);
156 }
157
Ted Kremenek9c378f72011-08-12 23:37:29 +0000158 void Report(const VarDecl *V, DeadStoreKind dsk,
Anna Zaks590dd8e2011-09-20 21:38:35 +0000159 PathDiagnosticLocation L, SourceRange R) {
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000160 if (Escaped.count(V))
161 return;
Ted Kremenek848ec832011-02-11 23:24:26 +0000162
163 // Compute reachable blocks within the CFG for trivial cases
164 // where a bogus dead store can be reported because itself is unreachable.
165 if (!reachableCode.get()) {
166 reachableCode.reset(new ReachableCode(cfg));
167 reachableCode->computeReachableBlocks();
168 }
169
170 if (!reachableCode->isReachable(currentBlock))
171 return;
Ted Kremenek8f269862008-07-14 20:56:04 +0000172
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000173 SmallString<64> buf;
Jordy Rose7df12342011-08-21 05:25:15 +0000174 llvm::raw_svector_ostream os(buf);
175 const char *BugType = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Ted Kremenek2cfac222008-07-23 21:16:38 +0000177 switch (dsk) {
Ted Kremenek2cfac222008-07-23 21:16:38 +0000178 case DeadInit:
Ted Kremenekefc620c2009-04-02 22:50:16 +0000179 BugType = "Dead initialization";
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000180 os << "Value stored to '" << *V
Jordy Rose7df12342011-08-21 05:25:15 +0000181 << "' during its initialization is never read";
Ted Kremenek2cfac222008-07-23 21:16:38 +0000182 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Ted Kremenek2cfac222008-07-23 21:16:38 +0000184 case DeadIncrement:
Ted Kremenekefc620c2009-04-02 22:50:16 +0000185 BugType = "Dead increment";
Ted Kremenek2cfac222008-07-23 21:16:38 +0000186 case Standard:
Ted Kremenekefc620c2009-04-02 22:50:16 +0000187 if (!BugType) BugType = "Dead assignment";
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000188 os << "Value stored to '" << *V << "' is never read";
Ted Kremenek2cfac222008-07-23 21:16:38 +0000189 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Ted Kremenek2cfac222008-07-23 21:16:38 +0000191 case Enclosing:
Ted Kremenek56b1f712011-01-13 20:58:56 +0000192 // Don't report issues in this case, e.g.: "if (x = foo())",
193 // where 'x' is unused later. We have yet to see a case where
194 // this is a real bug.
195 return;
Ted Kremenekf9c2a5d2008-07-15 18:06:32 +0000196 }
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Ted Kremenek07189522012-04-04 18:11:35 +0000198 BR.EmitBasicReport(AC->getDecl(), BugType, "Dead store", os.str(), L, R);
Ted Kremenek1a654b62008-06-20 21:45:25 +0000199 }
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Ted Kremenek9c378f72011-08-12 23:37:29 +0000201 void CheckVarDecl(const VarDecl *VD, const Expr *Ex, const Expr *Val,
Ted Kremenek2cfac222008-07-23 21:16:38 +0000202 DeadStoreKind dsk,
Ted Kremenek88299892011-07-28 23:07:59 +0000203 const LiveVariables::LivenessValues &Live) {
Ted Kremenek1a654b62008-06-20 21:45:25 +0000204
Ted Kremenek852274d2009-12-16 03:18:58 +0000205 if (!VD->hasLocalStorage())
206 return;
207 // Reference types confuse the dead stores checker. Skip them
208 // for now.
209 if (VD->getType()->getAs<ReferenceType>())
210 return;
211
Ted Kremenek2827f5a2012-09-06 22:32:48 +0000212 if (!isLive(Live, VD) &&
Anna Zaks590dd8e2011-09-20 21:38:35 +0000213 !(VD->getAttr<UnusedAttr>() || VD->getAttr<BlocksAttr>())) {
214
215 PathDiagnosticLocation ExLoc =
216 PathDiagnosticLocation::createBegin(Ex, BR.getSourceManager(), AC);
217 Report(VD, dsk, ExLoc, Val->getSourceRange());
218 }
Ted Kremenek3eb817e2008-05-21 22:59:16 +0000219 }
Mike Stump1eb44332009-09-09 15:08:12 +0000220
Ted Kremenek9c378f72011-08-12 23:37:29 +0000221 void CheckDeclRef(const DeclRefExpr *DR, const Expr *Val, DeadStoreKind dsk,
Ted Kremenek88299892011-07-28 23:07:59 +0000222 const LiveVariables::LivenessValues& Live) {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000223 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremenek88299892011-07-28 23:07:59 +0000224 CheckVarDecl(VD, DR, Val, dsk, Live);
Ted Kremenek2cfac222008-07-23 21:16:38 +0000225 }
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Ted Kremenek9c378f72011-08-12 23:37:29 +0000227 bool isIncrement(VarDecl *VD, const BinaryOperator* B) {
Ted Kremenek2cfac222008-07-23 21:16:38 +0000228 if (B->isCompoundAssignmentOp())
229 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000230
Ted Kremenek9c378f72011-08-12 23:37:29 +0000231 const Expr *RHS = B->getRHS()->IgnoreParenCasts();
Ted Kremenek88299892011-07-28 23:07:59 +0000232 const BinaryOperator* BRHS = dyn_cast<BinaryOperator>(RHS);
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Ted Kremenek2cfac222008-07-23 21:16:38 +0000234 if (!BRHS)
235 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Ted Kremenek88299892011-07-28 23:07:59 +0000237 const DeclRefExpr *DR;
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Ted Kremenek2cfac222008-07-23 21:16:38 +0000239 if ((DR = dyn_cast<DeclRefExpr>(BRHS->getLHS()->IgnoreParenCasts())))
240 if (DR->getDecl() == VD)
241 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Ted Kremenek2cfac222008-07-23 21:16:38 +0000243 if ((DR = dyn_cast<DeclRefExpr>(BRHS->getRHS()->IgnoreParenCasts())))
244 if (DR->getDecl() == VD)
245 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Ted Kremenek2cfac222008-07-23 21:16:38 +0000247 return false;
Ted Kremeneka23157e2008-05-05 23:12:21 +0000248 }
Mike Stump1eb44332009-09-09 15:08:12 +0000249
Ted Kremenek9c378f72011-08-12 23:37:29 +0000250 virtual void observeStmt(const Stmt *S, const CFGBlock *block,
Ted Kremenek88299892011-07-28 23:07:59 +0000251 const LiveVariables::LivenessValues &Live) {
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Ted Kremenek848ec832011-02-11 23:24:26 +0000253 currentBlock = block;
254
Ted Kremenek1c86b152008-04-14 18:28:25 +0000255 // Skip statements in macros.
256 if (S->getLocStart().isMacroID())
257 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Ted Kremenekf4e532b2011-02-12 00:17:19 +0000259 // Only cover dead stores from regular assignments. ++/-- dead stores
260 // have never flagged a real bug.
Ted Kremenek88299892011-07-28 23:07:59 +0000261 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000262 if (!B->isAssignmentOp()) return; // Skip non-assignments.
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Ted Kremenek9c378f72011-08-12 23:37:29 +0000264 if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(B->getLHS()))
Ted Kremenek1a654b62008-06-20 21:45:25 +0000265 if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremeneke12691c2008-08-09 00:05:14 +0000266 // Special case: check for assigning null to a pointer.
Mike Stump1eb44332009-09-09 15:08:12 +0000267 // This is a common form of defensive programming.
Ted Kremenekbb811ca2012-04-04 19:58:03 +0000268 const Expr *RHS = LookThroughTransitiveAssignments(B->getRHS());
269
Ted Kremenek89132202010-02-23 21:19:33 +0000270 QualType T = VD->getType();
271 if (T->isPointerType() || T->isObjCObjectPointerType()) {
Ted Kremenekbb811ca2012-04-04 19:58:03 +0000272 if (RHS->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNull))
Ted Kremenek93fab7c2009-11-22 20:26:21 +0000273 return;
Ted Kremeneke12691c2008-08-09 00:05:14 +0000274 }
Ted Kremenek93fab7c2009-11-22 20:26:21 +0000275
Ted Kremenekbb811ca2012-04-04 19:58:03 +0000276 RHS = RHS->IgnoreParenCasts();
Ted Kremenek3b587862009-01-09 22:15:01 +0000277 // Special case: self-assignments. These are often used to shut up
278 // "unused variable" compiler warnings.
Ted Kremenekbb811ca2012-04-04 19:58:03 +0000279 if (const DeclRefExpr *RhsDR = dyn_cast<DeclRefExpr>(RHS))
Ted Kremenek3b587862009-01-09 22:15:01 +0000280 if (VD == dyn_cast<VarDecl>(RhsDR->getDecl()))
281 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Ted Kremenek3b587862009-01-09 22:15:01 +0000283 // Otherwise, issue a warning.
Ted Kremenekb930d7a2009-04-01 06:52:48 +0000284 DeadStoreKind dsk = Parents.isConsumedExpr(B)
Mike Stump1eb44332009-09-09 15:08:12 +0000285 ? Enclosing
Ted Kremenek7f5fce72009-01-20 00:47:45 +0000286 : (isIncrement(VD,B) ? DeadIncrement : Standard);
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Ted Kremenek88299892011-07-28 23:07:59 +0000288 CheckVarDecl(VD, DR, B->getRHS(), dsk, Live);
Mike Stump1eb44332009-09-09 15:08:12 +0000289 }
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000290 }
Ted Kremenek88299892011-07-28 23:07:59 +0000291 else if (const UnaryOperator* U = dyn_cast<UnaryOperator>(S)) {
Ted Kremenekf4e532b2011-02-12 00:17:19 +0000292 if (!U->isIncrementOp() || U->isPrefix())
Ted Kremeneka23157e2008-05-05 23:12:21 +0000293 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Ted Kremenek88299892011-07-28 23:07:59 +0000295 const Stmt *parent = Parents.getParentIgnoreParenCasts(U);
Ted Kremenekf4e532b2011-02-12 00:17:19 +0000296 if (!parent || !isa<ReturnStmt>(parent))
Ted Kremenek380277e2008-10-15 05:23:41 +0000297 return;
Ted Kremenekb0f36322008-07-24 17:01:17 +0000298
Ted Kremenek88299892011-07-28 23:07:59 +0000299 const Expr *Ex = U->getSubExpr()->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Ted Kremenek9c378f72011-08-12 23:37:29 +0000301 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex))
Ted Kremenek88299892011-07-28 23:07:59 +0000302 CheckDeclRef(DR, U, DeadIncrement, Live);
Mike Stump1eb44332009-09-09 15:08:12 +0000303 }
Ted Kremenek9c378f72011-08-12 23:37:29 +0000304 else if (const DeclStmt *DS = dyn_cast<DeclStmt>(S))
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000305 // Iterate through the decls. Warn if any initializers are complex
306 // expressions that are not live (never used).
Ted Kremenek88299892011-07-28 23:07:59 +0000307 for (DeclStmt::const_decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000308 DI != DE; ++DI) {
Mike Stump1eb44332009-09-09 15:08:12 +0000309
Ted Kremenek9c378f72011-08-12 23:37:29 +0000310 VarDecl *V = dyn_cast<VarDecl>(*DI);
Ted Kremenekfc7ff552008-07-25 04:47:34 +0000311
312 if (!V)
313 continue;
Ted Kremenek852274d2009-12-16 03:18:58 +0000314
315 if (V->hasLocalStorage()) {
316 // Reference types confuse the dead stores checker. Skip them
317 // for now.
318 if (V->getType()->getAs<ReferenceType>())
319 return;
320
Ted Kremenekbb811ca2012-04-04 19:58:03 +0000321 if (const Expr *E = V->getInit()) {
322 while (const ExprWithCleanups *exprClean =
323 dyn_cast<ExprWithCleanups>(E))
John McCallf85e1932011-06-15 23:02:42 +0000324 E = exprClean->getSubExpr();
325
Ted Kremenekbb811ca2012-04-04 19:58:03 +0000326 // Look through transitive assignments, e.g.:
327 // int x = y = 0;
328 E = LookThroughTransitiveAssignments(E);
329
Ted Kremenek43f19e32009-12-15 04:12:12 +0000330 // Don't warn on C++ objects (yet) until we can show that their
331 // constructors/destructors don't have side effects.
332 if (isa<CXXConstructExpr>(E))
333 return;
Ted Kremenek604d9392009-12-23 04:11:44 +0000334
Ted Kremenekfc7ff552008-07-25 04:47:34 +0000335 // A dead initialization is a variable that is dead after it
336 // is initialized. We don't flag warnings for those variables
337 // marked 'unused'.
Ted Kremenek2827f5a2012-09-06 22:32:48 +0000338 if (!isLive(Live, V) && V->getAttr<UnusedAttr>() == 0) {
Ted Kremenekc6a1faf2007-09-28 20:48:41 +0000339 // Special case: check for initializations with constants.
340 //
341 // e.g. : int x = 0;
342 //
343 // If x is EVER assigned a new value later, don't issue
344 // a warning. This is because such initialization can be
345 // due to defensive programming.
Richard Smith0e35b4e2011-12-06 23:25:15 +0000346 if (E->isEvaluatable(Ctx))
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000347 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Ted Kremenekbb811ca2012-04-04 19:58:03 +0000349 if (const DeclRefExpr *DRE =
350 dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()))
351 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
Ted Kremenekebd42f42010-03-18 01:22:39 +0000352 // Special case: check for initialization from constant
353 // variables.
354 //
355 // e.g. extern const int MyConstant;
356 // int x = MyConstant;
357 //
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000358 if (VD->hasGlobalStorage() &&
Ted Kremenekebd42f42010-03-18 01:22:39 +0000359 VD->getType().isConstQualified())
360 return;
361 // Special case: check for initialization from scalar
362 // parameters. This is often a form of defensive
363 // programming. Non-scalars are still an error since
364 // because it more likely represents an actual algorithmic
365 // bug.
366 if (isa<ParmVarDecl>(VD) && VD->getType()->isScalarType())
367 return;
368 }
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Anna Zaks590dd8e2011-09-20 21:38:35 +0000370 PathDiagnosticLocation Loc =
371 PathDiagnosticLocation::create(V, BR.getSourceManager());
372 Report(V, DeadInit, Loc, E->getSourceRange());
Ted Kremenekce1cab92007-09-11 17:24:14 +0000373 }
Ted Kremenekfc7ff552008-07-25 04:47:34 +0000374 }
Ted Kremenek852274d2009-12-16 03:18:58 +0000375 }
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000376 }
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000377 }
378};
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000380} // end anonymous namespace
381
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000382//===----------------------------------------------------------------------===//
Ted Kremeneke2075582008-07-02 23:16:33 +0000383// Driver function to invoke the Dead-Stores checker on a CFG.
384//===----------------------------------------------------------------------===//
385
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000386namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000387class FindEscaped : public CFGRecStmtDeclVisitor<FindEscaped>{
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000388 CFG *cfg;
389public:
390 FindEscaped(CFG *c) : cfg(c) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000392 CFG& getCFG() { return *cfg; }
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Ted Kremenek88299892011-07-28 23:07:59 +0000394 llvm::SmallPtrSet<const VarDecl*, 20> Escaped;
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000395
396 void VisitUnaryOperator(UnaryOperator* U) {
397 // Check for '&'. Any VarDecl whose value has its address-taken we
398 // treat as escaped.
Ted Kremenek9c378f72011-08-12 23:37:29 +0000399 Expr *E = U->getSubExpr()->IgnoreParenCasts();
John McCall2de56d12010-08-25 11:45:40 +0000400 if (U->getOpcode() == UO_AddrOf)
Ted Kremenek9c378f72011-08-12 23:37:29 +0000401 if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E))
402 if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000403 Escaped.insert(VD);
404 return;
405 }
406 Visit(E);
407 }
408};
409} // end anonymous namespace
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Ted Kremenekf96f16d2009-04-07 05:25:24 +0000411
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000412//===----------------------------------------------------------------------===//
413// DeadStoresChecker
414//===----------------------------------------------------------------------===//
415
416namespace {
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +0000417class DeadStoresChecker : public Checker<check::ASTCodeBody> {
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000418public:
419 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
420 BugReporter &BR) const {
Ted Kremeneka5937bb2011-10-07 22:21:02 +0000421 if (LiveVariables *L = mgr.getAnalysis<LiveVariables>(D)) {
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000422 CFG &cfg = *mgr.getCFG(D);
Ted Kremenek1d26f482011-10-24 01:32:45 +0000423 AnalysisDeclContext *AC = mgr.getAnalysisDeclContext(D);
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000424 ParentMap &pmap = mgr.getParentMap(D);
425 FindEscaped FS(&cfg);
426 FS.getCFG().VisitBlockStmts(FS);
Anna Zaks590dd8e2011-09-20 21:38:35 +0000427 DeadStoreObs A(cfg, BR.getContext(), BR, AC, pmap, FS.Escaped);
Ted Kremenek88299892011-07-28 23:07:59 +0000428 L->runOnAllBlocks(A);
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000429 }
430 }
431};
432}
433
434void ento::registerDeadStoresChecker(CheckerManager &mgr) {
435 mgr.registerChecker<DeadStoresChecker>();
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000436}