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; |
| 33 | |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 34 | public: |
Ted Kremenek | 8f26986 | 2008-07-14 20:56:04 +0000 | [diff] [blame] | 35 | DeadStoreObs(ASTContext &ctx, BugReporter& br, ParentMap& parents) |
| 36 | : Ctx(ctx), BR(br), Parents(parents) {} |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 37 | |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 38 | virtual ~DeadStoreObs() {} |
| 39 | |
Ted Kremenek | f9c2a5d | 2008-07-15 18:06:32 +0000 | [diff] [blame] | 40 | void Report(VarDecl* V, bool inEnclosing, SourceLocation L, SourceRange R, |
| 41 | bool isInitialization = false) { |
Ted Kremenek | 8f26986 | 2008-07-14 20:56:04 +0000 | [diff] [blame] | 42 | |
Ted Kremenek | f9c2a5d | 2008-07-15 18:06:32 +0000 | [diff] [blame] | 43 | std::string name(V->getName()); |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 44 | |
Ted Kremenek | f9c2a5d | 2008-07-15 18:06:32 +0000 | [diff] [blame] | 45 | if (isInitialization) { |
| 46 | std::string msg = "Value stored to '" + name + |
| 47 | "' during its initialization is never read"; |
| 48 | |
| 49 | BR.EmitBasicReport("dead initialization", msg.c_str(), L, R); |
| 50 | } |
| 51 | else { |
| 52 | std::string msg = inEnclosing |
| 53 | ? "Although the value stored to '" + name + |
| 54 | "' is used in the enclosing expression, the value is never actually" |
| 55 | " read from '" + name + "'" |
| 56 | : "Value stored to '" + name + "' is never read"; |
| 57 | |
| 58 | BR.EmitBasicReport("dead store", msg.c_str(), L, R); |
| 59 | } |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void CheckVarDecl(VarDecl* VD, Expr* Ex, Expr* Val, |
| 63 | bool hasEnclosing, |
| 64 | const LiveVariables::AnalysisDataTy& AD, |
| 65 | const LiveVariables::ValTy& Live) { |
| 66 | |
Ted Kremenek | 8f26986 | 2008-07-14 20:56:04 +0000 | [diff] [blame] | 67 | if (VD->hasLocalStorage() && !Live(VD, AD)) |
| 68 | Report(VD, hasEnclosing, Ex->getSourceRange().getBegin(), |
| 69 | Val->getSourceRange()); |
Ted Kremenek | 3eb817e | 2008-05-21 22:59:16 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Ted Kremenek | a23157e | 2008-05-05 23:12:21 +0000 | [diff] [blame] | 72 | void CheckDeclRef(DeclRefExpr* DR, Expr* Val, |
| 73 | const LiveVariables::AnalysisDataTy& AD, |
| 74 | const LiveVariables::ValTy& Live) { |
| 75 | |
| 76 | if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 77 | CheckVarDecl(VD, DR, Val, false, AD, Live); |
Ted Kremenek | a23157e | 2008-05-05 23:12:21 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 80 | virtual void ObserveStmt(Stmt* S, |
| 81 | const LiveVariables::AnalysisDataTy& AD, |
| 82 | const LiveVariables::ValTy& Live) { |
Ted Kremenek | ce1cab9 | 2007-09-11 17:24:14 +0000 | [diff] [blame] | 83 | |
Ted Kremenek | 1c86b15 | 2008-04-14 18:28:25 +0000 | [diff] [blame] | 84 | // Skip statements in macros. |
| 85 | if (S->getLocStart().isMacroID()) |
| 86 | return; |
| 87 | |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 88 | if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) { |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 89 | if (!B->isAssignmentOp()) return; // Skip non-assignments. |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 90 | |
Ted Kremenek | c0576ca | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 91 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS())) |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 92 | if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 93 | |
| 94 | // Special case: check for assigning null to a pointer. This |
| 95 | // is a common form of defensive programming. |
| 96 | // FIXME: Make this optional? |
| 97 | |
| 98 | Expr* Val = B->getRHS(); |
| 99 | llvm::APSInt Result(Ctx.getTypeSize(Val->getType())); |
| 100 | |
| 101 | if (VD->getType()->isPointerType() && |
| 102 | Val->IgnoreParenCasts()->isIntegerConstantExpr(Result, Ctx, 0)) |
| 103 | if (Result == 0) |
| 104 | return; |
| 105 | |
| 106 | CheckVarDecl(VD, DR, Val, Parents.isSubExpr(B), AD, Live); |
| 107 | } |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 108 | } |
Ted Kremenek | a23157e | 2008-05-05 23:12:21 +0000 | [diff] [blame] | 109 | else if (UnaryOperator* U = dyn_cast<UnaryOperator>(S)) { |
| 110 | if (!U->isIncrementOp()) |
| 111 | return; |
| 112 | |
| 113 | Expr *Ex = U->getSubExpr()->IgnoreParenCasts(); |
| 114 | |
| 115 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(Ex)) |
| 116 | CheckDeclRef(DR, U, AD, Live); |
| 117 | } |
| 118 | else if (DeclStmt* DS = dyn_cast<DeclStmt>(S)) |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 119 | // Iterate through the decls. Warn if any initializers are complex |
| 120 | // expressions that are not live (never used). |
Ted Kremenek | c967c9d | 2008-04-14 17:52:13 +0000 | [diff] [blame] | 121 | for (ScopedDecl* SD = DS->getDecl(); SD; SD = SD->getNextDeclarator()) { |
| 122 | |
| 123 | VarDecl* V = dyn_cast<VarDecl>(SD); |
| 124 | if (!V) continue; |
| 125 | |
Ted Kremenek | c6a1faf | 2007-09-28 20:48:41 +0000 | [diff] [blame] | 126 | if (V->hasLocalStorage()) |
Ted Kremenek | 8f26986 | 2008-07-14 20:56:04 +0000 | [diff] [blame] | 127 | if (Expr* E = V->getInit()) |
Ted Kremenek | 9d7af51 | 2008-04-15 04:11:48 +0000 | [diff] [blame] | 128 | if (!Live(V, AD)) { |
Ted Kremenek | c6a1faf | 2007-09-28 20:48:41 +0000 | [diff] [blame] | 129 | // Special case: check for initializations with constants. |
| 130 | // |
| 131 | // e.g. : int x = 0; |
| 132 | // |
| 133 | // If x is EVER assigned a new value later, don't issue |
| 134 | // a warning. This is because such initialization can be |
| 135 | // due to defensive programming. |
Ted Kremenek | 8f26986 | 2008-07-14 20:56:04 +0000 | [diff] [blame] | 136 | if (!E->isConstantExpr(Ctx,NULL)) |
Ted Kremenek | f9c2a5d | 2008-07-15 18:06:32 +0000 | [diff] [blame] | 137 | Report(V, false, V->getLocation(), E->getSourceRange(), true); |
Ted Kremenek | ce1cab9 | 2007-09-11 17:24:14 +0000 | [diff] [blame] | 138 | } |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 139 | } |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 140 | } |
| 141 | }; |
| 142 | |
| 143 | } // end anonymous namespace |
| 144 | |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 145 | //===----------------------------------------------------------------------===// |
Ted Kremenek | e207558 | 2008-07-02 23:16:33 +0000 | [diff] [blame] | 146 | // Driver function to invoke the Dead-Stores checker on a CFG. |
| 147 | //===----------------------------------------------------------------------===// |
| 148 | |
| 149 | void clang::CheckDeadStores(LiveVariables& L, BugReporter& BR) { |
Ted Kremenek | 8f26986 | 2008-07-14 20:56:04 +0000 | [diff] [blame] | 150 | DeadStoreObs A(BR.getContext(), BR, BR.getParentMap()); |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 151 | L.runOnAllBlocks(*BR.getCFG(), &A); |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 152 | } |