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