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