Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 1 | //==- DeadStores.cpp - Check for stores to dead variables --------*- C++ -*-==// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Gabor Greif | 843e934 | 2008-03-06 10:40:09 +0000 | [diff] [blame] | 10 | // This file defines a DeadStores, a flow-sensitive checker that looks for |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 11 | // stores to variables that are no longer live. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/LocalCheckers.h" |
Ted Kremenek | cf6e41b | 2007-12-21 21:42:19 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/Analyses/LiveVariables.h" |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 17 | #include "clang/Analysis/Visitors/CFGRecStmtVisitor.h" |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 18 | #include "clang/Analysis/PathSensitive/BugReporter.h" |
| 19 | #include "clang/Analysis/PathSensitive/GRExprEngine.h" |
Ted Kremenek | f96f16d | 2009-04-07 05:25:24 +0000 | [diff] [blame] | 20 | #include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h" |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 21 | #include "clang/Basic/Diagnostic.h" |
Ted Kremenek | ce1cab9 | 2007-09-11 17:24:14 +0000 | [diff] [blame] | 22 | #include "clang/AST/ASTContext.h" |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 23 | #include "clang/AST/ParentMap.h" |
Ted Kremenek | f96f16d | 2009-04-07 05:25:24 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallPtrSet.h" |
Ted Kremenek | c2b51d8 | 2008-01-08 18:19:08 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace clang; |
| 28 | |
| 29 | namespace { |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 30 | |
Ted Kremenek | c2b51d8 | 2008-01-08 18:19:08 +0000 | [diff] [blame] | 31 | class VISIBILITY_HIDDEN DeadStoreObs : public LiveVariables::ObserverTy { |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 32 | ASTContext &Ctx; |
Ted Kremenek | 8f26986 | 2008-07-14 20:56:04 +0000 | [diff] [blame] | 33 | BugReporter& BR; |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 34 | ParentMap& Parents; |
Ted Kremenek | f96f16d | 2009-04-07 05:25:24 +0000 | [diff] [blame] | 35 | llvm::SmallPtrSet<VarDecl*, 20> Escaped; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 36 | |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 37 | enum DeadStoreKind { Standard, Enclosing, DeadIncrement, DeadInit }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 38 | |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 39 | public: |
Ted Kremenek | f96f16d | 2009-04-07 05:25:24 +0000 | [diff] [blame] | 40 | DeadStoreObs(ASTContext &ctx, BugReporter& br, ParentMap& parents, |
| 41 | llvm::SmallPtrSet<VarDecl*, 20> &escaped) |
| 42 | : Ctx(ctx), BR(br), Parents(parents), Escaped(escaped) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 43 | |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 44 | virtual ~DeadStoreObs() {} |
Ted Kremenek | b930d7a | 2009-04-01 06:52:48 +0000 | [diff] [blame] | 45 | |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 46 | void Report(VarDecl* V, DeadStoreKind dsk, SourceLocation L, SourceRange R) { |
Ted Kremenek | f96f16d | 2009-04-07 05:25:24 +0000 | [diff] [blame] | 47 | if (Escaped.count(V)) |
| 48 | return; |
Ted Kremenek | 8f26986 | 2008-07-14 20:56:04 +0000 | [diff] [blame] | 49 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 50 | std::string name = V->getNameAsString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 51 | |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 52 | const char* BugType = 0; |
| 53 | std::string msg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 54 | |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 55 | switch (dsk) { |
| 56 | default: |
| 57 | assert(false && "Impossible dead store type."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 58 | |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 59 | case DeadInit: |
Ted Kremenek | efc620c | 2009-04-02 22:50:16 +0000 | [diff] [blame] | 60 | BugType = "Dead initialization"; |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 61 | msg = "Value stored to '" + name + |
| 62 | "' during its initialization is never read"; |
| 63 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 64 | |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 65 | case DeadIncrement: |
Ted Kremenek | efc620c | 2009-04-02 22:50:16 +0000 | [diff] [blame] | 66 | BugType = "Dead increment"; |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 67 | case Standard: |
Ted Kremenek | efc620c | 2009-04-02 22:50:16 +0000 | [diff] [blame] | 68 | if (!BugType) BugType = "Dead assignment"; |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 69 | msg = "Value stored to '" + name + "' is never read"; |
| 70 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 71 | |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 72 | case Enclosing: |
Ted Kremenek | efc620c | 2009-04-02 22:50:16 +0000 | [diff] [blame] | 73 | BugType = "Dead nested assignment"; |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 74 | msg = "Although the value stored to '" + name + |
| 75 | "' is used in the enclosing expression, the value is never actually" |
| 76 | " read from '" + name + "'"; |
| 77 | break; |
Ted Kremenek | f9c2a5d | 2008-07-15 18:06:32 +0000 | [diff] [blame] | 78 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 79 | |
| 80 | BR.EmitBasicReport(BugType, "Dead store", msg.c_str(), L, R); |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 81 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 82 | |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 83 | void CheckVarDecl(VarDecl* VD, Expr* Ex, Expr* Val, |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 84 | DeadStoreKind dsk, |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 85 | const LiveVariables::AnalysisDataTy& AD, |
| 86 | const LiveVariables::ValTy& Live) { |
| 87 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 88 | if (VD->hasLocalStorage() && !Live(VD, AD) && !VD->getAttr<UnusedAttr>()) |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 89 | Report(VD, dsk, Ex->getSourceRange().getBegin(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 90 | Val->getSourceRange()); |
Ted Kremenek | 3eb817e | 2008-05-21 22:59:16 +0000 | [diff] [blame] | 91 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 92 | |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 93 | void CheckDeclRef(DeclRefExpr* DR, Expr* Val, DeadStoreKind dsk, |
Ted Kremenek | a23157e | 2008-05-05 23:12:21 +0000 | [diff] [blame] | 94 | const LiveVariables::AnalysisDataTy& AD, |
| 95 | const LiveVariables::ValTy& Live) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 96 | |
Ted Kremenek | a23157e | 2008-05-05 23:12:21 +0000 | [diff] [blame] | 97 | if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 98 | CheckVarDecl(VD, DR, Val, dsk, AD, Live); |
| 99 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 100 | |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 101 | bool isIncrement(VarDecl* VD, BinaryOperator* B) { |
| 102 | if (B->isCompoundAssignmentOp()) |
| 103 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 104 | |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 105 | Expr* RHS = B->getRHS()->IgnoreParenCasts(); |
| 106 | BinaryOperator* BRHS = dyn_cast<BinaryOperator>(RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 108 | if (!BRHS) |
| 109 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 110 | |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 111 | DeclRefExpr *DR; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 112 | |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 113 | if ((DR = dyn_cast<DeclRefExpr>(BRHS->getLHS()->IgnoreParenCasts()))) |
| 114 | if (DR->getDecl() == VD) |
| 115 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 | |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 117 | if ((DR = dyn_cast<DeclRefExpr>(BRHS->getRHS()->IgnoreParenCasts()))) |
| 118 | if (DR->getDecl() == VD) |
| 119 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 120 | |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 121 | return false; |
Ted Kremenek | a23157e | 2008-05-05 23:12:21 +0000 | [diff] [blame] | 122 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 123 | |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 124 | virtual void ObserveStmt(Stmt* S, |
| 125 | const LiveVariables::AnalysisDataTy& AD, |
| 126 | const LiveVariables::ValTy& Live) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 127 | |
Ted Kremenek | 1c86b15 | 2008-04-14 18:28:25 +0000 | [diff] [blame] | 128 | // Skip statements in macros. |
| 129 | if (S->getLocStart().isMacroID()) |
| 130 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | |
| 132 | if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) { |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 133 | if (!B->isAssignmentOp()) return; // Skip non-assignments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 134 | |
Ted Kremenek | c0576ca | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 135 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS())) |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 136 | if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
Ted Kremenek | 3b58786 | 2009-01-09 22:15:01 +0000 | [diff] [blame] | 137 | Expr* RHS = B->getRHS()->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 138 | |
Ted Kremenek | e12691c | 2008-08-09 00:05:14 +0000 | [diff] [blame] | 139 | // Special case: check for assigning null to a pointer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 140 | // This is a common form of defensive programming. |
Ted Kremenek | e12691c | 2008-08-09 00:05:14 +0000 | [diff] [blame] | 141 | if (VD->getType()->isPointerType()) { |
Ted Kremenek | 3b58786 | 2009-01-09 22:15:01 +0000 | [diff] [blame] | 142 | if (IntegerLiteral* L = dyn_cast<IntegerLiteral>(RHS)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | // FIXME: Probably should have an Expr::isNullPointerConstant. |
Ted Kremenek | e12691c | 2008-08-09 00:05:14 +0000 | [diff] [blame] | 144 | if (L->getValue() == 0) |
| 145 | return; |
| 146 | } |
Ted Kremenek | 3b58786 | 2009-01-09 22:15:01 +0000 | [diff] [blame] | 147 | // Special case: self-assignments. These are often used to shut up |
| 148 | // "unused variable" compiler warnings. |
| 149 | if (DeclRefExpr* RhsDR = dyn_cast<DeclRefExpr>(RHS)) |
| 150 | if (VD == dyn_cast<VarDecl>(RhsDR->getDecl())) |
| 151 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | |
Ted Kremenek | 3b58786 | 2009-01-09 22:15:01 +0000 | [diff] [blame] | 153 | // Otherwise, issue a warning. |
Ted Kremenek | b930d7a | 2009-04-01 06:52:48 +0000 | [diff] [blame] | 154 | DeadStoreKind dsk = Parents.isConsumedExpr(B) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | ? Enclosing |
Ted Kremenek | 7f5fce7 | 2009-01-20 00:47:45 +0000 | [diff] [blame] | 156 | : (isIncrement(VD,B) ? DeadIncrement : Standard); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | |
Ted Kremenek | e12691c | 2008-08-09 00:05:14 +0000 | [diff] [blame] | 158 | CheckVarDecl(VD, DR, B->getRHS(), dsk, AD, Live); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 159 | } |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 160 | } |
Ted Kremenek | a23157e | 2008-05-05 23:12:21 +0000 | [diff] [blame] | 161 | else if (UnaryOperator* U = dyn_cast<UnaryOperator>(S)) { |
| 162 | if (!U->isIncrementOp()) |
| 163 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 164 | |
Ted Kremenek | 380277e | 2008-10-15 05:23:41 +0000 | [diff] [blame] | 165 | // Handle: ++x within a subexpression. The solution is not warn |
| 166 | // about preincrements to dead variables when the preincrement occurs |
| 167 | // as a subexpression. This can lead to false negatives, e.g. "(++x);" |
| 168 | // A generalized dead code checker should find such issues. |
Ted Kremenek | b930d7a | 2009-04-01 06:52:48 +0000 | [diff] [blame] | 169 | if (U->isPrefix() && Parents.isConsumedExpr(U)) |
Ted Kremenek | 380277e | 2008-10-15 05:23:41 +0000 | [diff] [blame] | 170 | return; |
Ted Kremenek | b0f3632 | 2008-07-24 17:01:17 +0000 | [diff] [blame] | 171 | |
Ted Kremenek | a23157e | 2008-05-05 23:12:21 +0000 | [diff] [blame] | 172 | Expr *Ex = U->getSubExpr()->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 173 | |
Ted Kremenek | a23157e | 2008-05-05 23:12:21 +0000 | [diff] [blame] | 174 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(Ex)) |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 175 | CheckDeclRef(DR, U, DeadIncrement, AD, Live); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 176 | } |
Ted Kremenek | a23157e | 2008-05-05 23:12:21 +0000 | [diff] [blame] | 177 | else if (DeclStmt* DS = dyn_cast<DeclStmt>(S)) |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 178 | // Iterate through the decls. Warn if any initializers are complex |
| 179 | // expressions that are not live (never used). |
Ted Kremenek | 14f8b4f | 2008-08-05 20:46:55 +0000 | [diff] [blame] | 180 | for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end(); |
| 181 | DI != DE; ++DI) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 182 | |
Ted Kremenek | 14f8b4f | 2008-08-05 20:46:55 +0000 | [diff] [blame] | 183 | VarDecl* V = dyn_cast<VarDecl>(*DI); |
Ted Kremenek | fc7ff55 | 2008-07-25 04:47:34 +0000 | [diff] [blame] | 184 | |
| 185 | if (!V) |
| 186 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 187 | |
Ted Kremenek | c6a1faf | 2007-09-28 20:48:41 +0000 | [diff] [blame] | 188 | if (V->hasLocalStorage()) |
Ted Kremenek | fc7ff55 | 2008-07-25 04:47:34 +0000 | [diff] [blame] | 189 | if (Expr* E = V->getInit()) { |
| 190 | // A dead initialization is a variable that is dead after it |
| 191 | // is initialized. We don't flag warnings for those variables |
| 192 | // marked 'unused'. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 193 | if (!Live(V, AD) && V->getAttr<UnusedAttr>() == 0) { |
Ted Kremenek | c6a1faf | 2007-09-28 20:48:41 +0000 | [diff] [blame] | 194 | // Special case: check for initializations with constants. |
| 195 | // |
| 196 | // e.g. : int x = 0; |
| 197 | // |
| 198 | // If x is EVER assigned a new value later, don't issue |
| 199 | // a warning. This is because such initialization can be |
| 200 | // due to defensive programming. |
Ted Kremenek | d3098ee | 2009-02-09 18:01:00 +0000 | [diff] [blame] | 201 | if (E->isConstantInitializer(Ctx)) |
| 202 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 203 | |
Ted Kremenek | d3098ee | 2009-02-09 18:01:00 +0000 | [diff] [blame] | 204 | // Special case: check for initializations from constant |
| 205 | // variables. |
| 206 | // |
| 207 | // e.g. extern const int MyConstant; |
| 208 | // int x = MyConstant; |
| 209 | // |
| 210 | if (DeclRefExpr *DRE=dyn_cast<DeclRefExpr>(E->IgnoreParenCasts())) |
| 211 | if (VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) |
| 212 | if (VD->hasGlobalStorage() && |
| 213 | VD->getType().isConstQualified()) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 214 | |
Ted Kremenek | d3098ee | 2009-02-09 18:01:00 +0000 | [diff] [blame] | 215 | Report(V, DeadInit, V->getLocation(), E->getSourceRange()); |
Ted Kremenek | ce1cab9 | 2007-09-11 17:24:14 +0000 | [diff] [blame] | 216 | } |
Ted Kremenek | fc7ff55 | 2008-07-25 04:47:34 +0000 | [diff] [blame] | 217 | } |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 218 | } |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 219 | } |
| 220 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 221 | |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 222 | } // end anonymous namespace |
| 223 | |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 224 | //===----------------------------------------------------------------------===// |
Ted Kremenek | e207558 | 2008-07-02 23:16:33 +0000 | [diff] [blame] | 225 | // Driver function to invoke the Dead-Stores checker on a CFG. |
| 226 | //===----------------------------------------------------------------------===// |
| 227 | |
Ted Kremenek | f96f16d | 2009-04-07 05:25:24 +0000 | [diff] [blame] | 228 | namespace { |
| 229 | class VISIBILITY_HIDDEN FindEscaped : public CFGRecStmtDeclVisitor<FindEscaped>{ |
| 230 | CFG *cfg; |
| 231 | public: |
| 232 | FindEscaped(CFG *c) : cfg(c) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 233 | |
Ted Kremenek | f96f16d | 2009-04-07 05:25:24 +0000 | [diff] [blame] | 234 | CFG& getCFG() { return *cfg; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 235 | |
Ted Kremenek | f96f16d | 2009-04-07 05:25:24 +0000 | [diff] [blame] | 236 | llvm::SmallPtrSet<VarDecl*, 20> Escaped; |
| 237 | |
| 238 | void VisitUnaryOperator(UnaryOperator* U) { |
| 239 | // Check for '&'. Any VarDecl whose value has its address-taken we |
| 240 | // treat as escaped. |
| 241 | Expr* E = U->getSubExpr()->IgnoreParenCasts(); |
| 242 | if (U->getOpcode() == UnaryOperator::AddrOf) |
| 243 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E)) |
| 244 | if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 245 | Escaped.insert(VD); |
| 246 | return; |
| 247 | } |
| 248 | Visit(E); |
| 249 | } |
| 250 | }; |
| 251 | } // end anonymous namespace |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 252 | |
Ted Kremenek | f96f16d | 2009-04-07 05:25:24 +0000 | [diff] [blame] | 253 | |
Zhongxing Xu | b317f8f | 2009-09-10 05:44:00 +0000 | [diff] [blame] | 254 | void clang::CheckDeadStores(CFG &cfg, LiveVariables &L, ParentMap &pmap, |
| 255 | BugReporter& BR) { |
| 256 | FindEscaped FS(&cfg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 257 | FS.getCFG().VisitBlockStmts(FS); |
Zhongxing Xu | b317f8f | 2009-09-10 05:44:00 +0000 | [diff] [blame] | 258 | DeadStoreObs A(BR.getContext(), BR, pmap, FS.Escaped); |
| 259 | L.runOnAllBlocks(cfg, &A); |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 260 | } |