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