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