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 | c2b51d8 | 2008-01-08 18:19:08 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | 3eb817e | 2008-05-21 22:59:16 +0000 | [diff] [blame^] | 23 | #include <sstream> |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace clang; |
| 26 | |
| 27 | namespace { |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +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 | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 33 | public: |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 34 | DeadStoreObs(ASTContext &ctx, Diagnostic &diags, DiagnosticClient &client) |
| 35 | : Ctx(ctx), Diags(diags), Client(client) {} |
| 36 | |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 37 | virtual ~DeadStoreObs() {} |
| 38 | |
Ted Kremenek | 3eb817e | 2008-05-21 22:59:16 +0000 | [diff] [blame^] | 39 | unsigned GetDiag(VarDecl* VD) { |
| 40 | std::ostringstream os; |
| 41 | os << "value stored to '" << VD->getName() << "' is never used"; |
| 42 | return Diags.getCustomDiagID(Diagnostic::Warning, os.str().c_str()); |
| 43 | } |
| 44 | |
Ted Kremenek | a23157e | 2008-05-05 23:12:21 +0000 | [diff] [blame] | 45 | void CheckDeclRef(DeclRefExpr* DR, Expr* Val, |
| 46 | const LiveVariables::AnalysisDataTy& AD, |
| 47 | const LiveVariables::ValTy& Live) { |
| 48 | |
| 49 | if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) |
| 50 | if (VD->hasLocalStorage() && !Live(VD, AD)) { |
Ted Kremenek | 3eb817e | 2008-05-21 22:59:16 +0000 | [diff] [blame^] | 51 | SourceRange R = Val->getSourceRange(); |
Ted Kremenek | a23157e | 2008-05-05 23:12:21 +0000 | [diff] [blame] | 52 | Diags.Report(&Client, |
| 53 | Ctx.getFullLoc(DR->getSourceRange().getBegin()), |
Ted Kremenek | 3eb817e | 2008-05-21 22:59:16 +0000 | [diff] [blame^] | 54 | GetDiag(VD), 0, 0, &R, 1); |
Ted Kremenek | a23157e | 2008-05-05 23:12:21 +0000 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 58 | virtual void ObserveStmt(Stmt* S, |
| 59 | const LiveVariables::AnalysisDataTy& AD, |
| 60 | const LiveVariables::ValTy& Live) { |
Ted Kremenek | ce1cab9 | 2007-09-11 17:24:14 +0000 | [diff] [blame] | 61 | |
Ted Kremenek | 1c86b15 | 2008-04-14 18:28:25 +0000 | [diff] [blame] | 62 | // Skip statements in macros. |
| 63 | if (S->getLocStart().isMacroID()) |
| 64 | return; |
| 65 | |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 66 | if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) { |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 67 | if (!B->isAssignmentOp()) return; // Skip non-assignments. |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 68 | |
Ted Kremenek | c0576ca | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 69 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS())) |
Ted Kremenek | a23157e | 2008-05-05 23:12:21 +0000 | [diff] [blame] | 70 | CheckDeclRef(DR, B->getRHS(), AD, Live); |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 71 | } |
Ted Kremenek | a23157e | 2008-05-05 23:12:21 +0000 | [diff] [blame] | 72 | else if (UnaryOperator* U = dyn_cast<UnaryOperator>(S)) { |
| 73 | if (!U->isIncrementOp()) |
| 74 | return; |
| 75 | |
| 76 | Expr *Ex = U->getSubExpr()->IgnoreParenCasts(); |
| 77 | |
| 78 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(Ex)) |
| 79 | CheckDeclRef(DR, U, AD, Live); |
| 80 | } |
| 81 | else if (DeclStmt* DS = dyn_cast<DeclStmt>(S)) |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 82 | // Iterate through the decls. Warn if any initializers are complex |
| 83 | // expressions that are not live (never used). |
Ted Kremenek | c967c9d | 2008-04-14 17:52:13 +0000 | [diff] [blame] | 84 | for (ScopedDecl* SD = DS->getDecl(); SD; SD = SD->getNextDeclarator()) { |
| 85 | |
| 86 | VarDecl* V = dyn_cast<VarDecl>(SD); |
| 87 | if (!V) continue; |
| 88 | |
Ted Kremenek | c6a1faf | 2007-09-28 20:48:41 +0000 | [diff] [blame] | 89 | if (V->hasLocalStorage()) |
| 90 | if (Expr* E = V->getInit()) { |
Ted Kremenek | 9d7af51 | 2008-04-15 04:11:48 +0000 | [diff] [blame] | 91 | if (!Live(V, AD)) { |
Ted Kremenek | c6a1faf | 2007-09-28 20:48:41 +0000 | [diff] [blame] | 92 | // Special case: check for initializations with constants. |
| 93 | // |
| 94 | // e.g. : int x = 0; |
| 95 | // |
| 96 | // If x is EVER assigned a new value later, don't issue |
| 97 | // a warning. This is because such initialization can be |
| 98 | // due to defensive programming. |
| 99 | if (!E->isConstantExpr(Ctx,NULL)) { |
| 100 | // Flag a warning. |
| 101 | SourceRange R = E->getSourceRange(); |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 102 | Diags.Report(&Client, |
| 103 | Ctx.getFullLoc(V->getLocation()), |
Ted Kremenek | 3eb817e | 2008-05-21 22:59:16 +0000 | [diff] [blame^] | 104 | GetDiag(V), 0, 0, &R, 1); |
Ted Kremenek | c6a1faf | 2007-09-28 20:48:41 +0000 | [diff] [blame] | 105 | } |
Ted Kremenek | ce1cab9 | 2007-09-11 17:24:14 +0000 | [diff] [blame] | 106 | } |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 107 | } |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 108 | } |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 109 | } |
| 110 | }; |
| 111 | |
| 112 | } // end anonymous namespace |
| 113 | |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 114 | //===----------------------------------------------------------------------===// |
| 115 | // Driver function to invoke the Dead-Stores checker on a CFG. |
| 116 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 117 | |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 118 | void clang::CheckDeadStores(CFG& cfg, ASTContext &Ctx, Diagnostic &Diags) { |
Ted Kremenek | 7cb1593 | 2008-03-13 16:55:07 +0000 | [diff] [blame] | 119 | LiveVariables L(cfg); |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 120 | L.runOnCFG(cfg); |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 121 | DeadStoreObs A(Ctx, Diags, Diags.getClient()); |
Ted Kremenek | 7cb1593 | 2008-03-13 16:55:07 +0000 | [diff] [blame] | 122 | L.runOnAllBlocks(cfg, &A); |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 125 | //===----------------------------------------------------------------------===// |
| 126 | // BugReporter-based invocation of the Dead-Stores checker. |
| 127 | //===----------------------------------------------------------------------===// |
| 128 | |
| 129 | namespace { |
| 130 | |
| 131 | class VISIBILITY_HIDDEN DiagBugReport : public RangedBugReport { |
| 132 | std::list<std::string> Strs; |
| 133 | FullSourceLoc L; |
| 134 | public: |
Ted Kremenek | 95cc1ba | 2008-04-18 20:54:29 +0000 | [diff] [blame] | 135 | DiagBugReport(BugType& D, FullSourceLoc l) : |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 136 | RangedBugReport(D, NULL), L(l) {} |
| 137 | |
| 138 | virtual ~DiagBugReport() {} |
| 139 | virtual FullSourceLoc getLocation(SourceManager&) { return L; } |
| 140 | |
| 141 | void addString(const std::string& s) { Strs.push_back(s); } |
| 142 | |
| 143 | typedef std::list<std::string>::const_iterator str_iterator; |
| 144 | str_iterator str_begin() const { return Strs.begin(); } |
| 145 | str_iterator str_end() const { return Strs.end(); } |
| 146 | }; |
| 147 | |
| 148 | class VISIBILITY_HIDDEN DiagCollector : public DiagnosticClient { |
| 149 | std::list<DiagBugReport> Reports; |
Ted Kremenek | 95cc1ba | 2008-04-18 20:54:29 +0000 | [diff] [blame] | 150 | BugType& D; |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 151 | public: |
| 152 | DiagCollector(BugType& d) : D(d) {} |
| 153 | |
| 154 | virtual ~DiagCollector() {} |
| 155 | |
| 156 | virtual void HandleDiagnostic(Diagnostic &Diags, |
| 157 | Diagnostic::Level DiagLevel, |
| 158 | FullSourceLoc Pos, |
| 159 | diag::kind ID, |
| 160 | const std::string *Strs, |
| 161 | unsigned NumStrs, |
| 162 | const SourceRange *Ranges, |
| 163 | unsigned NumRanges) { |
| 164 | |
| 165 | // FIXME: Use a map from diag::kind to BugType, instead of having just |
| 166 | // one BugType. |
| 167 | |
| 168 | Reports.push_back(DiagBugReport(D, Pos)); |
| 169 | DiagBugReport& R = Reports.back(); |
| 170 | |
| 171 | for ( ; NumRanges ; --NumRanges, ++Ranges) |
| 172 | R.addRange(*Ranges); |
| 173 | |
| 174 | for ( ; NumStrs ; --NumStrs, ++Strs) |
| 175 | R.addString(*Strs); |
| 176 | } |
| 177 | |
| 178 | // Iterators. |
| 179 | |
| 180 | typedef std::list<DiagBugReport>::iterator iterator; |
| 181 | iterator begin() { return Reports.begin(); } |
| 182 | iterator end() { return Reports.end(); } |
| 183 | }; |
| 184 | |
Ted Kremenek | 95cc1ba | 2008-04-18 20:54:29 +0000 | [diff] [blame] | 185 | class VISIBILITY_HIDDEN DeadStoresChecker : public BugTypeCacheLocation { |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 186 | public: |
| 187 | virtual const char* getName() const { |
| 188 | return "dead store"; |
| 189 | } |
| 190 | |
| 191 | virtual const char* getDescription() const { |
Ted Kremenek | 1d5d1da | 2008-04-15 05:31:00 +0000 | [diff] [blame] | 192 | return "Value stored to variable is never subsequently read."; |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | virtual void EmitWarnings(BugReporter& BR) { |
| 196 | |
| 197 | // Run the dead store checker and collect the diagnostics. |
| 198 | DiagCollector C(*this); |
| 199 | DeadStoreObs A(BR.getContext(), BR.getDiagnostic(), C); |
| 200 | GRExprEngine& Eng = BR.getEngine(); |
| 201 | Eng.getLiveness().runOnAllBlocks(Eng.getCFG(), &A); |
| 202 | |
| 203 | // Emit the bug reports. |
| 204 | |
| 205 | for (DiagCollector::iterator I = C.begin(), E = C.end(); I != E; ++I) |
| 206 | BR.EmitWarning(*I); |
| 207 | } |
| 208 | }; |
| 209 | } // end anonymous namespace |
| 210 | |
| 211 | BugType* clang::MakeDeadStoresChecker() { |
| 212 | return new DeadStoresChecker(); |
| 213 | } |