Ted Kremenek | f224820 | 2011-01-13 20:58:56 +0000 | [diff] [blame] | 1 | //==- DeadStoresChecker.cpp - Check for stores to dead variables -*- C++ -*-==// |
Ted Kremenek | 1bb9f25 | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 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 | 1bb9f25 | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Gabor Greif | 3a8edd8 | 2008-03-06 10:40:09 +0000 | [diff] [blame] | 10 | // This file defines a DeadStores, a flow-sensitive checker that looks for |
Ted Kremenek | 1bb9f25 | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 11 | // stores to variables that are no longer live. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Argyrios Kyrtzidis | af45aca | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 15 | #include "ClangSACheckers.h" |
Ted Kremenek | 2f1a79d | 2007-09-11 17:24:14 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 17 | #include "clang/AST/Attr.h" |
Ted Kremenek | 34a6917 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 18 | #include "clang/AST/ParentMap.h" |
Ted Kremenek | cadd9f1 | 2012-09-06 22:32:48 +0000 | [diff] [blame] | 19 | #include "clang/AST/RecursiveASTVisitor.h" |
Ted Kremenek | 8de92c0 | 2012-10-30 04:43:51 +0000 | [diff] [blame] | 20 | #include "clang/Analysis/Analyses/LiveVariables.h" |
| 21 | #include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h" |
| 22 | #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" |
| 23 | #include "clang/StaticAnalyzer/Core/Checker.h" |
| 24 | #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" |
| 25 | #include "llvm/ADT/BitVector.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallString.h" |
Ted Kremenek | cadd9f1 | 2012-09-06 22:32:48 +0000 | [diff] [blame] | 27 | #include "llvm/Support/SaveAndRestore.h" |
Ted Kremenek | 1bb9f25 | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace clang; |
Ted Kremenek | 98857c9 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 30 | using namespace ento; |
Ted Kremenek | 1bb9f25 | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 31 | |
Ted Kremenek | cadd9f1 | 2012-09-06 22:32:48 +0000 | [diff] [blame] | 32 | namespace { |
| 33 | |
| 34 | /// A simple visitor to record what VarDecls occur in EH-handling code. |
| 35 | class EHCodeVisitor : public RecursiveASTVisitor<EHCodeVisitor> { |
| 36 | public: |
| 37 | bool inEH; |
| 38 | llvm::DenseSet<const VarDecl *> &S; |
| 39 | |
| 40 | bool TraverseObjCAtFinallyStmt(ObjCAtFinallyStmt *S) { |
| 41 | SaveAndRestore<bool> inFinally(inEH, true); |
| 42 | return ::RecursiveASTVisitor<EHCodeVisitor>::TraverseObjCAtFinallyStmt(S); |
| 43 | } |
| 44 | |
| 45 | bool TraverseObjCAtCatchStmt(ObjCAtCatchStmt *S) { |
| 46 | SaveAndRestore<bool> inCatch(inEH, true); |
| 47 | return ::RecursiveASTVisitor<EHCodeVisitor>::TraverseObjCAtCatchStmt(S); |
| 48 | } |
| 49 | |
| 50 | bool TraverseCXXCatchStmt(CXXCatchStmt *S) { |
| 51 | SaveAndRestore<bool> inCatch(inEH, true); |
| 52 | return TraverseStmt(S->getHandlerBlock()); |
| 53 | } |
| 54 | |
| 55 | bool VisitDeclRefExpr(DeclRefExpr *DR) { |
| 56 | if (inEH) |
| 57 | if (const VarDecl *D = dyn_cast<VarDecl>(DR->getDecl())) |
| 58 | S.insert(D); |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | EHCodeVisitor(llvm::DenseSet<const VarDecl *> &S) : |
| 63 | inEH(false), S(S) {} |
| 64 | }; |
Ted Kremenek | 34a6917 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 65 | |
Ted Kremenek | 9865d7f | 2011-02-11 23:24:26 +0000 | [diff] [blame] | 66 | // FIXME: Eventually migrate into its own file, and have it managed by |
| 67 | // AnalysisManager. |
| 68 | class ReachableCode { |
| 69 | const CFG &cfg; |
| 70 | llvm::BitVector reachable; |
| 71 | public: |
| 72 | ReachableCode(const CFG &cfg) |
| 73 | : cfg(cfg), reachable(cfg.getNumBlockIDs(), false) {} |
| 74 | |
| 75 | void computeReachableBlocks(); |
| 76 | |
| 77 | bool isReachable(const CFGBlock *block) const { |
| 78 | return reachable[block->getBlockID()]; |
| 79 | } |
| 80 | }; |
| 81 | } |
| 82 | |
| 83 | void ReachableCode::computeReachableBlocks() { |
| 84 | if (!cfg.getNumBlockIDs()) |
| 85 | return; |
| 86 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 87 | SmallVector<const CFGBlock*, 10> worklist; |
Ted Kremenek | 9865d7f | 2011-02-11 23:24:26 +0000 | [diff] [blame] | 88 | worklist.push_back(&cfg.getEntry()); |
| 89 | |
| 90 | while (!worklist.empty()) { |
| 91 | const CFGBlock *block = worklist.back(); |
| 92 | worklist.pop_back(); |
| 93 | llvm::BitVector::reference isReachable = reachable[block->getBlockID()]; |
| 94 | if (isReachable) |
| 95 | continue; |
| 96 | isReachable = true; |
| 97 | for (CFGBlock::const_succ_iterator i = block->succ_begin(), |
| 98 | e = block->succ_end(); i != e; ++i) |
| 99 | if (const CFGBlock *succ = *i) |
| 100 | worklist.push_back(succ); |
| 101 | } |
| 102 | } |
| 103 | |
Ted Kremenek | dc53f00 | 2012-04-04 19:58:03 +0000 | [diff] [blame] | 104 | static const Expr *LookThroughTransitiveAssignments(const Expr *Ex) { |
| 105 | while (Ex) { |
| 106 | const BinaryOperator *BO = |
| 107 | dyn_cast<BinaryOperator>(Ex->IgnoreParenCasts()); |
| 108 | if (!BO) |
| 109 | break; |
| 110 | if (BO->getOpcode() == BO_Assign) { |
| 111 | Ex = BO->getRHS(); |
| 112 | continue; |
| 113 | } |
| 114 | break; |
| 115 | } |
| 116 | return Ex; |
| 117 | } |
| 118 | |
Ted Kremenek | 9865d7f | 2011-02-11 23:24:26 +0000 | [diff] [blame] | 119 | namespace { |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 120 | class DeadStoreObs : public LiveVariables::Observer { |
Ted Kremenek | 9865d7f | 2011-02-11 23:24:26 +0000 | [diff] [blame] | 121 | const CFG &cfg; |
Chris Lattner | 254987c | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 122 | ASTContext &Ctx; |
Ted Kremenek | c18255d | 2008-07-14 20:56:04 +0000 | [diff] [blame] | 123 | BugReporter& BR; |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 124 | AnalysisDeclContext* AC; |
Ted Kremenek | 34a6917 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 125 | ParentMap& Parents; |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 126 | llvm::SmallPtrSet<const VarDecl*, 20> Escaped; |
Dylan Noblesmith | e277899 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 127 | OwningPtr<ReachableCode> reachableCode; |
Ted Kremenek | 9865d7f | 2011-02-11 23:24:26 +0000 | [diff] [blame] | 128 | const CFGBlock *currentBlock; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 129 | OwningPtr<llvm::DenseSet<const VarDecl *> > InEH; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 130 | |
Ted Kremenek | ecc851b | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 131 | enum DeadStoreKind { Standard, Enclosing, DeadIncrement, DeadInit }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 132 | |
Ted Kremenek | 1bb9f25 | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 133 | public: |
Ted Kremenek | 9865d7f | 2011-02-11 23:24:26 +0000 | [diff] [blame] | 134 | DeadStoreObs(const CFG &cfg, ASTContext &ctx, |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 135 | BugReporter& br, AnalysisDeclContext* ac, ParentMap& parents, |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 136 | llvm::SmallPtrSet<const VarDecl*, 20> &escaped) |
Anna Zaks | c29bed3 | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 137 | : cfg(cfg), Ctx(ctx), BR(br), AC(ac), Parents(parents), |
Ted Kremenek | 9865d7f | 2011-02-11 23:24:26 +0000 | [diff] [blame] | 138 | Escaped(escaped), currentBlock(0) {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 139 | |
Ted Kremenek | ad8bce0 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 140 | virtual ~DeadStoreObs() {} |
Ted Kremenek | 8b0dba3 | 2009-04-01 06:52:48 +0000 | [diff] [blame] | 141 | |
Ted Kremenek | cadd9f1 | 2012-09-06 22:32:48 +0000 | [diff] [blame] | 142 | bool isLive(const LiveVariables::LivenessValues &Live, const VarDecl *D) { |
| 143 | if (Live.isLive(D)) |
| 144 | return true; |
| 145 | // Lazily construct the set that records which VarDecls are in |
| 146 | // EH code. |
| 147 | if (!InEH.get()) { |
| 148 | InEH.reset(new llvm::DenseSet<const VarDecl *>()); |
| 149 | EHCodeVisitor V(*InEH.get()); |
| 150 | V.TraverseStmt(AC->getBody()); |
| 151 | } |
| 152 | // Treat all VarDecls that occur in EH code as being "always live" |
| 153 | // when considering to suppress dead stores. Frequently stores |
| 154 | // are followed by reads in EH code, but we don't have the ability |
| 155 | // to analyze that yet. |
| 156 | return InEH->count(D); |
| 157 | } |
| 158 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 159 | void Report(const VarDecl *V, DeadStoreKind dsk, |
Anna Zaks | c29bed3 | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 160 | PathDiagnosticLocation L, SourceRange R) { |
Ted Kremenek | 4d947fa | 2009-04-07 05:25:24 +0000 | [diff] [blame] | 161 | if (Escaped.count(V)) |
| 162 | return; |
Ted Kremenek | 9865d7f | 2011-02-11 23:24:26 +0000 | [diff] [blame] | 163 | |
| 164 | // Compute reachable blocks within the CFG for trivial cases |
| 165 | // where a bogus dead store can be reported because itself is unreachable. |
| 166 | if (!reachableCode.get()) { |
| 167 | reachableCode.reset(new ReachableCode(cfg)); |
| 168 | reachableCode->computeReachableBlocks(); |
| 169 | } |
| 170 | |
| 171 | if (!reachableCode->isReachable(currentBlock)) |
| 172 | return; |
Ted Kremenek | c18255d | 2008-07-14 20:56:04 +0000 | [diff] [blame] | 173 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 174 | SmallString<64> buf; |
Jordy Rose | 82c673d | 2011-08-21 05:25:15 +0000 | [diff] [blame] | 175 | llvm::raw_svector_ostream os(buf); |
| 176 | const char *BugType = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 177 | |
Ted Kremenek | ecc851b | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 178 | switch (dsk) { |
Ted Kremenek | ecc851b | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 179 | case DeadInit: |
Ted Kremenek | 6e4c284 | 2009-04-02 22:50:16 +0000 | [diff] [blame] | 180 | BugType = "Dead initialization"; |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 181 | os << "Value stored to '" << *V |
Jordy Rose | 82c673d | 2011-08-21 05:25:15 +0000 | [diff] [blame] | 182 | << "' during its initialization is never read"; |
Ted Kremenek | ecc851b | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 183 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 184 | |
Ted Kremenek | ecc851b | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 185 | case DeadIncrement: |
Ted Kremenek | 6e4c284 | 2009-04-02 22:50:16 +0000 | [diff] [blame] | 186 | BugType = "Dead increment"; |
Ted Kremenek | ecc851b | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 187 | case Standard: |
Ted Kremenek | 6e4c284 | 2009-04-02 22:50:16 +0000 | [diff] [blame] | 188 | if (!BugType) BugType = "Dead assignment"; |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 189 | os << "Value stored to '" << *V << "' is never read"; |
Ted Kremenek | ecc851b | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 190 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 191 | |
Ted Kremenek | ecc851b | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 192 | case Enclosing: |
Ted Kremenek | f224820 | 2011-01-13 20:58:56 +0000 | [diff] [blame] | 193 | // Don't report issues in this case, e.g.: "if (x = foo())", |
| 194 | // where 'x' is unused later. We have yet to see a case where |
| 195 | // this is a real bug. |
| 196 | return; |
Ted Kremenek | 81bfc07 | 2008-07-15 18:06:32 +0000 | [diff] [blame] | 197 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 198 | |
Ted Kremenek | 5a10f08 | 2012-04-04 18:11:35 +0000 | [diff] [blame] | 199 | BR.EmitBasicReport(AC->getDecl(), BugType, "Dead store", os.str(), L, R); |
Ted Kremenek | 34a6917 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 200 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 201 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 202 | void CheckVarDecl(const VarDecl *VD, const Expr *Ex, const Expr *Val, |
Ted Kremenek | ecc851b | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 203 | DeadStoreKind dsk, |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 204 | const LiveVariables::LivenessValues &Live) { |
Ted Kremenek | 34a6917 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 205 | |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 206 | if (!VD->hasLocalStorage()) |
| 207 | return; |
| 208 | // Reference types confuse the dead stores checker. Skip them |
| 209 | // for now. |
| 210 | if (VD->getType()->getAs<ReferenceType>()) |
| 211 | return; |
| 212 | |
Ted Kremenek | cadd9f1 | 2012-09-06 22:32:48 +0000 | [diff] [blame] | 213 | if (!isLive(Live, VD) && |
Anna Zaks | c29bed3 | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 214 | !(VD->getAttr<UnusedAttr>() || VD->getAttr<BlocksAttr>())) { |
| 215 | |
| 216 | PathDiagnosticLocation ExLoc = |
| 217 | PathDiagnosticLocation::createBegin(Ex, BR.getSourceManager(), AC); |
| 218 | Report(VD, dsk, ExLoc, Val->getSourceRange()); |
| 219 | } |
Ted Kremenek | 91f035c | 2008-05-21 22:59:16 +0000 | [diff] [blame] | 220 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 221 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 222 | void CheckDeclRef(const DeclRefExpr *DR, const Expr *Val, DeadStoreKind dsk, |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 223 | const LiveVariables::LivenessValues& Live) { |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 224 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 225 | CheckVarDecl(VD, DR, Val, dsk, Live); |
Ted Kremenek | ecc851b | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 226 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 227 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 228 | bool isIncrement(VarDecl *VD, const BinaryOperator* B) { |
Ted Kremenek | ecc851b | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 229 | if (B->isCompoundAssignmentOp()) |
| 230 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 231 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 232 | const Expr *RHS = B->getRHS()->IgnoreParenCasts(); |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 233 | const BinaryOperator* BRHS = dyn_cast<BinaryOperator>(RHS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 234 | |
Ted Kremenek | ecc851b | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 235 | if (!BRHS) |
| 236 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 237 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 238 | const DeclRefExpr *DR; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 239 | |
Ted Kremenek | ecc851b | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 240 | if ((DR = dyn_cast<DeclRefExpr>(BRHS->getLHS()->IgnoreParenCasts()))) |
| 241 | if (DR->getDecl() == VD) |
| 242 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 243 | |
Ted Kremenek | ecc851b | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 244 | if ((DR = dyn_cast<DeclRefExpr>(BRHS->getRHS()->IgnoreParenCasts()))) |
| 245 | if (DR->getDecl() == VD) |
| 246 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 247 | |
Ted Kremenek | ecc851b | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 248 | return false; |
Ted Kremenek | f15cd14 | 2008-05-05 23:12:21 +0000 | [diff] [blame] | 249 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 250 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 251 | virtual void observeStmt(const Stmt *S, const CFGBlock *block, |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 252 | const LiveVariables::LivenessValues &Live) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 253 | |
Ted Kremenek | 9865d7f | 2011-02-11 23:24:26 +0000 | [diff] [blame] | 254 | currentBlock = block; |
| 255 | |
Ted Kremenek | 87bfc03 | 2008-04-14 18:28:25 +0000 | [diff] [blame] | 256 | // Skip statements in macros. |
| 257 | if (S->getLocStart().isMacroID()) |
| 258 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 259 | |
Ted Kremenek | b1c392a | 2011-02-12 00:17:19 +0000 | [diff] [blame] | 260 | // Only cover dead stores from regular assignments. ++/-- dead stores |
| 261 | // have never flagged a real bug. |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 262 | if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) { |
Ted Kremenek | ad8bce0 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 263 | if (!B->isAssignmentOp()) return; // Skip non-assignments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 264 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 265 | if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(B->getLHS())) |
Ted Kremenek | 34a6917 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 266 | if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
Ted Kremenek | 0216b83 | 2008-08-09 00:05:14 +0000 | [diff] [blame] | 267 | // Special case: check for assigning null to a pointer. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 268 | // This is a common form of defensive programming. |
Ted Kremenek | dc53f00 | 2012-04-04 19:58:03 +0000 | [diff] [blame] | 269 | const Expr *RHS = LookThroughTransitiveAssignments(B->getRHS()); |
| 270 | |
Ted Kremenek | b4331a9 | 2010-02-23 21:19:33 +0000 | [diff] [blame] | 271 | QualType T = VD->getType(); |
| 272 | if (T->isPointerType() || T->isObjCObjectPointerType()) { |
Ted Kremenek | dc53f00 | 2012-04-04 19:58:03 +0000 | [diff] [blame] | 273 | if (RHS->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNull)) |
Ted Kremenek | 12b6495 | 2009-11-22 20:26:21 +0000 | [diff] [blame] | 274 | return; |
Ted Kremenek | 0216b83 | 2008-08-09 00:05:14 +0000 | [diff] [blame] | 275 | } |
Ted Kremenek | 12b6495 | 2009-11-22 20:26:21 +0000 | [diff] [blame] | 276 | |
Ted Kremenek | dc53f00 | 2012-04-04 19:58:03 +0000 | [diff] [blame] | 277 | RHS = RHS->IgnoreParenCasts(); |
Ted Kremenek | 890d44e | 2009-01-09 22:15:01 +0000 | [diff] [blame] | 278 | // Special case: self-assignments. These are often used to shut up |
| 279 | // "unused variable" compiler warnings. |
Ted Kremenek | dc53f00 | 2012-04-04 19:58:03 +0000 | [diff] [blame] | 280 | if (const DeclRefExpr *RhsDR = dyn_cast<DeclRefExpr>(RHS)) |
Ted Kremenek | 890d44e | 2009-01-09 22:15:01 +0000 | [diff] [blame] | 281 | if (VD == dyn_cast<VarDecl>(RhsDR->getDecl())) |
| 282 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 283 | |
Ted Kremenek | 890d44e | 2009-01-09 22:15:01 +0000 | [diff] [blame] | 284 | // Otherwise, issue a warning. |
Ted Kremenek | 8b0dba3 | 2009-04-01 06:52:48 +0000 | [diff] [blame] | 285 | DeadStoreKind dsk = Parents.isConsumedExpr(B) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 286 | ? Enclosing |
Ted Kremenek | e5fe617 | 2009-01-20 00:47:45 +0000 | [diff] [blame] | 287 | : (isIncrement(VD,B) ? DeadIncrement : Standard); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 288 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 289 | CheckVarDecl(VD, DR, B->getRHS(), dsk, Live); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 290 | } |
Ted Kremenek | 1bb9f25 | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 291 | } |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 292 | else if (const UnaryOperator* U = dyn_cast<UnaryOperator>(S)) { |
Ted Kremenek | b1c392a | 2011-02-12 00:17:19 +0000 | [diff] [blame] | 293 | if (!U->isIncrementOp() || U->isPrefix()) |
Ted Kremenek | f15cd14 | 2008-05-05 23:12:21 +0000 | [diff] [blame] | 294 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 295 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 296 | const Stmt *parent = Parents.getParentIgnoreParenCasts(U); |
Ted Kremenek | b1c392a | 2011-02-12 00:17:19 +0000 | [diff] [blame] | 297 | if (!parent || !isa<ReturnStmt>(parent)) |
Ted Kremenek | bb7818b | 2008-10-15 05:23:41 +0000 | [diff] [blame] | 298 | return; |
Ted Kremenek | 87b16f4 | 2008-07-24 17:01:17 +0000 | [diff] [blame] | 299 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 300 | const Expr *Ex = U->getSubExpr()->IgnoreParenCasts(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 301 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 302 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex)) |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 303 | CheckDeclRef(DR, U, DeadIncrement, Live); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 304 | } |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 305 | else if (const DeclStmt *DS = dyn_cast<DeclStmt>(S)) |
Ted Kremenek | ad8bce0 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 306 | // Iterate through the decls. Warn if any initializers are complex |
| 307 | // expressions that are not live (never used). |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 308 | for (DeclStmt::const_decl_iterator DI=DS->decl_begin(), DE=DS->decl_end(); |
Ted Kremenek | 4f8792b | 2008-08-05 20:46:55 +0000 | [diff] [blame] | 309 | DI != DE; ++DI) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 310 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 311 | VarDecl *V = dyn_cast<VarDecl>(*DI); |
Ted Kremenek | 092ec76 | 2008-07-25 04:47:34 +0000 | [diff] [blame] | 312 | |
| 313 | if (!V) |
| 314 | continue; |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 315 | |
| 316 | if (V->hasLocalStorage()) { |
| 317 | // Reference types confuse the dead stores checker. Skip them |
| 318 | // for now. |
| 319 | if (V->getType()->getAs<ReferenceType>()) |
| 320 | return; |
| 321 | |
Ted Kremenek | dc53f00 | 2012-04-04 19:58:03 +0000 | [diff] [blame] | 322 | if (const Expr *E = V->getInit()) { |
| 323 | while (const ExprWithCleanups *exprClean = |
| 324 | dyn_cast<ExprWithCleanups>(E)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 325 | E = exprClean->getSubExpr(); |
| 326 | |
Ted Kremenek | dc53f00 | 2012-04-04 19:58:03 +0000 | [diff] [blame] | 327 | // Look through transitive assignments, e.g.: |
| 328 | // int x = y = 0; |
| 329 | E = LookThroughTransitiveAssignments(E); |
| 330 | |
Ted Kremenek | 29f3808 | 2009-12-15 04:12:12 +0000 | [diff] [blame] | 331 | // Don't warn on C++ objects (yet) until we can show that their |
| 332 | // constructors/destructors don't have side effects. |
| 333 | if (isa<CXXConstructExpr>(E)) |
| 334 | return; |
Ted Kremenek | 857f41c | 2009-12-23 04:11:44 +0000 | [diff] [blame] | 335 | |
Ted Kremenek | 092ec76 | 2008-07-25 04:47:34 +0000 | [diff] [blame] | 336 | // A dead initialization is a variable that is dead after it |
| 337 | // is initialized. We don't flag warnings for those variables |
| 338 | // marked 'unused'. |
Ted Kremenek | cadd9f1 | 2012-09-06 22:32:48 +0000 | [diff] [blame] | 339 | if (!isLive(Live, V) && V->getAttr<UnusedAttr>() == 0) { |
Ted Kremenek | a6ef56e | 2007-09-28 20:48:41 +0000 | [diff] [blame] | 340 | // Special case: check for initializations with constants. |
| 341 | // |
| 342 | // e.g. : int x = 0; |
| 343 | // |
| 344 | // If x is EVER assigned a new value later, don't issue |
| 345 | // a warning. This is because such initialization can be |
| 346 | // due to defensive programming. |
Richard Smith | 1e1f5ab | 2011-12-06 23:25:15 +0000 | [diff] [blame] | 347 | if (E->isEvaluatable(Ctx)) |
Ted Kremenek | 0203db7 | 2009-02-09 18:01:00 +0000 | [diff] [blame] | 348 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 349 | |
Ted Kremenek | dc53f00 | 2012-04-04 19:58:03 +0000 | [diff] [blame] | 350 | if (const DeclRefExpr *DRE = |
| 351 | dyn_cast<DeclRefExpr>(E->IgnoreParenCasts())) |
| 352 | if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) { |
Ted Kremenek | e174fda | 2010-03-18 01:22:39 +0000 | [diff] [blame] | 353 | // Special case: check for initialization from constant |
| 354 | // variables. |
| 355 | // |
| 356 | // e.g. extern const int MyConstant; |
| 357 | // int x = MyConstant; |
| 358 | // |
Ted Kremenek | 0203db7 | 2009-02-09 18:01:00 +0000 | [diff] [blame] | 359 | if (VD->hasGlobalStorage() && |
Ted Kremenek | e174fda | 2010-03-18 01:22:39 +0000 | [diff] [blame] | 360 | VD->getType().isConstQualified()) |
| 361 | return; |
| 362 | // Special case: check for initialization from scalar |
| 363 | // parameters. This is often a form of defensive |
| 364 | // programming. Non-scalars are still an error since |
| 365 | // because it more likely represents an actual algorithmic |
| 366 | // bug. |
| 367 | if (isa<ParmVarDecl>(VD) && VD->getType()->isScalarType()) |
| 368 | return; |
| 369 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 370 | |
Anna Zaks | c29bed3 | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 371 | PathDiagnosticLocation Loc = |
| 372 | PathDiagnosticLocation::create(V, BR.getSourceManager()); |
| 373 | Report(V, DeadInit, Loc, E->getSourceRange()); |
Ted Kremenek | 2f1a79d | 2007-09-11 17:24:14 +0000 | [diff] [blame] | 374 | } |
Ted Kremenek | 092ec76 | 2008-07-25 04:47:34 +0000 | [diff] [blame] | 375 | } |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 376 | } |
Ted Kremenek | ad8bce0 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 377 | } |
Ted Kremenek | 1bb9f25 | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 378 | } |
| 379 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 380 | |
Ted Kremenek | 1bb9f25 | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 381 | } // end anonymous namespace |
| 382 | |
Ted Kremenek | 7e15130 | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 383 | //===----------------------------------------------------------------------===// |
Ted Kremenek | c7efb53 | 2008-07-02 23:16:33 +0000 | [diff] [blame] | 384 | // Driver function to invoke the Dead-Stores checker on a CFG. |
| 385 | //===----------------------------------------------------------------------===// |
| 386 | |
Ted Kremenek | 4d947fa | 2009-04-07 05:25:24 +0000 | [diff] [blame] | 387 | namespace { |
Kovarththanan Rajaratnam | 65c6566 | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 388 | class FindEscaped : public CFGRecStmtDeclVisitor<FindEscaped>{ |
Ted Kremenek | 4d947fa | 2009-04-07 05:25:24 +0000 | [diff] [blame] | 389 | CFG *cfg; |
| 390 | public: |
| 391 | FindEscaped(CFG *c) : cfg(c) {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 392 | |
Ted Kremenek | 4d947fa | 2009-04-07 05:25:24 +0000 | [diff] [blame] | 393 | CFG& getCFG() { return *cfg; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 394 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 395 | llvm::SmallPtrSet<const VarDecl*, 20> Escaped; |
Ted Kremenek | 4d947fa | 2009-04-07 05:25:24 +0000 | [diff] [blame] | 396 | |
| 397 | void VisitUnaryOperator(UnaryOperator* U) { |
| 398 | // Check for '&'. Any VarDecl whose value has its address-taken we |
| 399 | // treat as escaped. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 400 | Expr *E = U->getSubExpr()->IgnoreParenCasts(); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 401 | if (U->getOpcode() == UO_AddrOf) |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 402 | if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E)) |
| 403 | if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
Ted Kremenek | 4d947fa | 2009-04-07 05:25:24 +0000 | [diff] [blame] | 404 | Escaped.insert(VD); |
| 405 | return; |
| 406 | } |
| 407 | Visit(E); |
| 408 | } |
| 409 | }; |
| 410 | } // end anonymous namespace |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 411 | |
Ted Kremenek | 4d947fa | 2009-04-07 05:25:24 +0000 | [diff] [blame] | 412 | |
Argyrios Kyrtzidis | af45aca | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 413 | //===----------------------------------------------------------------------===// |
| 414 | // DeadStoresChecker |
| 415 | //===----------------------------------------------------------------------===// |
| 416 | |
| 417 | namespace { |
Argyrios Kyrtzidis | 6a5674f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 418 | class DeadStoresChecker : public Checker<check::ASTCodeBody> { |
Argyrios Kyrtzidis | af45aca | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 419 | public: |
| 420 | void checkASTCodeBody(const Decl *D, AnalysisManager& mgr, |
| 421 | BugReporter &BR) const { |
Ted Kremenek | 3e05be9 | 2013-02-18 07:18:28 +0000 | [diff] [blame^] | 422 | |
| 423 | // Don't do anything for template instantiations. |
| 424 | // Proving that code in a template instantiation is "dead" |
| 425 | // means proving that it is dead in all instantiations. |
| 426 | // This same problem exists with -Wunreachable-code. |
| 427 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 428 | if (FD->isTemplateInstantiation()) |
| 429 | return; |
| 430 | |
Ted Kremenek | dccc2b2 | 2011-10-07 22:21:02 +0000 | [diff] [blame] | 431 | if (LiveVariables *L = mgr.getAnalysis<LiveVariables>(D)) { |
Argyrios Kyrtzidis | af45aca | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 432 | CFG &cfg = *mgr.getCFG(D); |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 433 | AnalysisDeclContext *AC = mgr.getAnalysisDeclContext(D); |
Argyrios Kyrtzidis | af45aca | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 434 | ParentMap &pmap = mgr.getParentMap(D); |
| 435 | FindEscaped FS(&cfg); |
| 436 | FS.getCFG().VisitBlockStmts(FS); |
Anna Zaks | c29bed3 | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 437 | DeadStoreObs A(cfg, BR.getContext(), BR, AC, pmap, FS.Escaped); |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 438 | L->runOnAllBlocks(A); |
Argyrios Kyrtzidis | af45aca | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | }; |
| 442 | } |
| 443 | |
| 444 | void ento::registerDeadStoresChecker(CheckerManager &mgr) { |
| 445 | mgr.registerChecker<DeadStoresChecker>(); |
Ted Kremenek | 7e15130 | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 446 | } |