Ted Kremenek | 56b1f71 | 2011-01-13 20:58:56 +0000 | [diff] [blame] | 1 | //==- DeadStoresChecker.cpp - Check for stores to dead variables -*- C++ -*-==// |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Gabor Greif | 843e934 | 2008-03-06 10:40:09 +0000 | [diff] [blame] | 10 | // This file defines a DeadStores, a flow-sensitive checker that looks for |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 11 | // stores to variables that are no longer live. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Argyrios Kyrtzidis | 7dd445e | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 15 | #include "ClangSACheckers.h" |
Ted Kremenek | ce1cab9 | 2007-09-11 17:24:14 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 17 | #include "clang/AST/ParentMap.h" |
Ted Kremenek | 2827f5a | 2012-09-06 22:32:48 +0000 | [diff] [blame] | 18 | #include "clang/AST/RecursiveASTVisitor.h" |
Ted Kremenek | 19948ac | 2012-10-30 04:43:51 +0000 | [diff] [blame^] | 19 | #include "clang/Analysis/Analyses/LiveVariables.h" |
| 20 | #include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h" |
| 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 | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallString.h" |
Ted Kremenek | 2827f5a | 2012-09-06 22:32:48 +0000 | [diff] [blame] | 26 | #include "llvm/Support/SaveAndRestore.h" |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 29 | using namespace ento; |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 30 | |
Ted Kremenek | 2827f5a | 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 | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 64 | |
Ted Kremenek | 848ec83 | 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 | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 86 | SmallVector<const CFGBlock*, 10> worklist; |
Ted Kremenek | 848ec83 | 2011-02-11 23:24:26 +0000 | [diff] [blame] | 87 | worklist.push_back(&cfg.getEntry()); |
| 88 | |
| 89 | while (!worklist.empty()) { |
| 90 | const CFGBlock *block = worklist.back(); |
| 91 | worklist.pop_back(); |
| 92 | llvm::BitVector::reference isReachable = reachable[block->getBlockID()]; |
| 93 | if (isReachable) |
| 94 | continue; |
| 95 | isReachable = true; |
| 96 | for (CFGBlock::const_succ_iterator i = block->succ_begin(), |
| 97 | e = block->succ_end(); i != e; ++i) |
| 98 | if (const CFGBlock *succ = *i) |
| 99 | worklist.push_back(succ); |
| 100 | } |
| 101 | } |
| 102 | |
Ted Kremenek | bb811ca | 2012-04-04 19:58:03 +0000 | [diff] [blame] | 103 | static const Expr *LookThroughTransitiveAssignments(const Expr *Ex) { |
| 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 | } |
| 113 | break; |
| 114 | } |
| 115 | return Ex; |
| 116 | } |
| 117 | |
Ted Kremenek | 848ec83 | 2011-02-11 23:24:26 +0000 | [diff] [blame] | 118 | namespace { |
Ted Kremenek | 8829989 | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 119 | class DeadStoreObs : public LiveVariables::Observer { |
Ted Kremenek | 848ec83 | 2011-02-11 23:24:26 +0000 | [diff] [blame] | 120 | const CFG &cfg; |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 121 | ASTContext &Ctx; |
Ted Kremenek | 8f26986 | 2008-07-14 20:56:04 +0000 | [diff] [blame] | 122 | BugReporter& BR; |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 123 | AnalysisDeclContext* AC; |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 124 | ParentMap& Parents; |
Ted Kremenek | 8829989 | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 125 | llvm::SmallPtrSet<const VarDecl*, 20> Escaped; |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 126 | OwningPtr<ReachableCode> reachableCode; |
Ted Kremenek | 848ec83 | 2011-02-11 23:24:26 +0000 | [diff] [blame] | 127 | const CFGBlock *currentBlock; |
Ted Kremenek | 2827f5a | 2012-09-06 22:32:48 +0000 | [diff] [blame] | 128 | llvm::OwningPtr<llvm::DenseSet<const VarDecl *> > InEH; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 130 | enum DeadStoreKind { Standard, Enclosing, DeadIncrement, DeadInit }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 132 | public: |
Ted Kremenek | 848ec83 | 2011-02-11 23:24:26 +0000 | [diff] [blame] | 133 | DeadStoreObs(const CFG &cfg, ASTContext &ctx, |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 134 | BugReporter& br, AnalysisDeclContext* ac, ParentMap& parents, |
Ted Kremenek | 8829989 | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 135 | llvm::SmallPtrSet<const VarDecl*, 20> &escaped) |
Anna Zaks | 590dd8e | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 136 | : cfg(cfg), Ctx(ctx), BR(br), AC(ac), Parents(parents), |
Ted Kremenek | 848ec83 | 2011-02-11 23:24:26 +0000 | [diff] [blame] | 137 | Escaped(escaped), currentBlock(0) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 138 | |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 139 | virtual ~DeadStoreObs() {} |
Ted Kremenek | b930d7a | 2009-04-01 06:52:48 +0000 | [diff] [blame] | 140 | |
Ted Kremenek | 2827f5a | 2012-09-06 22:32:48 +0000 | [diff] [blame] | 141 | bool isLive(const LiveVariables::LivenessValues &Live, const VarDecl *D) { |
| 142 | if (Live.isLive(D)) |
| 143 | return true; |
| 144 | // Lazily construct the set that records which VarDecls are in |
| 145 | // EH code. |
| 146 | if (!InEH.get()) { |
| 147 | InEH.reset(new llvm::DenseSet<const VarDecl *>()); |
| 148 | EHCodeVisitor V(*InEH.get()); |
| 149 | V.TraverseStmt(AC->getBody()); |
| 150 | } |
| 151 | // Treat all VarDecls that occur in EH code as being "always live" |
| 152 | // when considering to suppress dead stores. Frequently stores |
| 153 | // are followed by reads in EH code, but we don't have the ability |
| 154 | // to analyze that yet. |
| 155 | return InEH->count(D); |
| 156 | } |
| 157 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 158 | void Report(const VarDecl *V, DeadStoreKind dsk, |
Anna Zaks | 590dd8e | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 159 | PathDiagnosticLocation L, SourceRange R) { |
Ted Kremenek | f96f16d | 2009-04-07 05:25:24 +0000 | [diff] [blame] | 160 | if (Escaped.count(V)) |
| 161 | return; |
Ted Kremenek | 848ec83 | 2011-02-11 23:24:26 +0000 | [diff] [blame] | 162 | |
| 163 | // Compute reachable blocks within the CFG for trivial cases |
| 164 | // where a bogus dead store can be reported because itself is unreachable. |
| 165 | if (!reachableCode.get()) { |
| 166 | reachableCode.reset(new ReachableCode(cfg)); |
| 167 | reachableCode->computeReachableBlocks(); |
| 168 | } |
| 169 | |
| 170 | if (!reachableCode->isReachable(currentBlock)) |
| 171 | return; |
Ted Kremenek | 8f26986 | 2008-07-14 20:56:04 +0000 | [diff] [blame] | 172 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 173 | SmallString<64> buf; |
Jordy Rose | 7df1234 | 2011-08-21 05:25:15 +0000 | [diff] [blame] | 174 | llvm::raw_svector_ostream os(buf); |
| 175 | const char *BugType = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 176 | |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 177 | switch (dsk) { |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 178 | case DeadInit: |
Ted Kremenek | efc620c | 2009-04-02 22:50:16 +0000 | [diff] [blame] | 179 | BugType = "Dead initialization"; |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 180 | os << "Value stored to '" << *V |
Jordy Rose | 7df1234 | 2011-08-21 05:25:15 +0000 | [diff] [blame] | 181 | << "' during its initialization is never read"; |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 182 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 183 | |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 184 | case DeadIncrement: |
Ted Kremenek | efc620c | 2009-04-02 22:50:16 +0000 | [diff] [blame] | 185 | BugType = "Dead increment"; |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 186 | case Standard: |
Ted Kremenek | efc620c | 2009-04-02 22:50:16 +0000 | [diff] [blame] | 187 | if (!BugType) BugType = "Dead assignment"; |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 188 | os << "Value stored to '" << *V << "' is never read"; |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 189 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 191 | case Enclosing: |
Ted Kremenek | 56b1f71 | 2011-01-13 20:58:56 +0000 | [diff] [blame] | 192 | // Don't report issues in this case, e.g.: "if (x = foo())", |
| 193 | // where 'x' is unused later. We have yet to see a case where |
| 194 | // this is a real bug. |
| 195 | return; |
Ted Kremenek | f9c2a5d | 2008-07-15 18:06:32 +0000 | [diff] [blame] | 196 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 197 | |
Ted Kremenek | 0718952 | 2012-04-04 18:11:35 +0000 | [diff] [blame] | 198 | BR.EmitBasicReport(AC->getDecl(), BugType, "Dead store", os.str(), L, R); |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 199 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 200 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 201 | void CheckVarDecl(const VarDecl *VD, const Expr *Ex, const Expr *Val, |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 202 | DeadStoreKind dsk, |
Ted Kremenek | 8829989 | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 203 | const LiveVariables::LivenessValues &Live) { |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 204 | |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 205 | if (!VD->hasLocalStorage()) |
| 206 | return; |
| 207 | // Reference types confuse the dead stores checker. Skip them |
| 208 | // for now. |
| 209 | if (VD->getType()->getAs<ReferenceType>()) |
| 210 | return; |
| 211 | |
Ted Kremenek | 2827f5a | 2012-09-06 22:32:48 +0000 | [diff] [blame] | 212 | if (!isLive(Live, VD) && |
Anna Zaks | 590dd8e | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 213 | !(VD->getAttr<UnusedAttr>() || VD->getAttr<BlocksAttr>())) { |
| 214 | |
| 215 | PathDiagnosticLocation ExLoc = |
| 216 | PathDiagnosticLocation::createBegin(Ex, BR.getSourceManager(), AC); |
| 217 | Report(VD, dsk, ExLoc, Val->getSourceRange()); |
| 218 | } |
Ted Kremenek | 3eb817e | 2008-05-21 22:59:16 +0000 | [diff] [blame] | 219 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 220 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 221 | void CheckDeclRef(const DeclRefExpr *DR, const Expr *Val, DeadStoreKind dsk, |
Ted Kremenek | 8829989 | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 222 | const LiveVariables::LivenessValues& Live) { |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 223 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) |
Ted Kremenek | 8829989 | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 224 | CheckVarDecl(VD, DR, Val, dsk, Live); |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 225 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 226 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 227 | bool isIncrement(VarDecl *VD, const BinaryOperator* B) { |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 228 | if (B->isCompoundAssignmentOp()) |
| 229 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 230 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 231 | const Expr *RHS = B->getRHS()->IgnoreParenCasts(); |
Ted Kremenek | 8829989 | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 232 | const BinaryOperator* BRHS = dyn_cast<BinaryOperator>(RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 233 | |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 234 | if (!BRHS) |
| 235 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 236 | |
Ted Kremenek | 8829989 | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 237 | const DeclRefExpr *DR; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 238 | |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 239 | if ((DR = dyn_cast<DeclRefExpr>(BRHS->getLHS()->IgnoreParenCasts()))) |
| 240 | if (DR->getDecl() == VD) |
| 241 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 242 | |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 243 | if ((DR = dyn_cast<DeclRefExpr>(BRHS->getRHS()->IgnoreParenCasts()))) |
| 244 | if (DR->getDecl() == VD) |
| 245 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 246 | |
Ted Kremenek | 2cfac22 | 2008-07-23 21:16:38 +0000 | [diff] [blame] | 247 | return false; |
Ted Kremenek | a23157e | 2008-05-05 23:12:21 +0000 | [diff] [blame] | 248 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 249 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 250 | virtual void observeStmt(const Stmt *S, const CFGBlock *block, |
Ted Kremenek | 8829989 | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 251 | const LiveVariables::LivenessValues &Live) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 252 | |
Ted Kremenek | 848ec83 | 2011-02-11 23:24:26 +0000 | [diff] [blame] | 253 | currentBlock = block; |
| 254 | |
Ted Kremenek | 1c86b15 | 2008-04-14 18:28:25 +0000 | [diff] [blame] | 255 | // Skip statements in macros. |
| 256 | if (S->getLocStart().isMacroID()) |
| 257 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 258 | |
Ted Kremenek | f4e532b | 2011-02-12 00:17:19 +0000 | [diff] [blame] | 259 | // Only cover dead stores from regular assignments. ++/-- dead stores |
| 260 | // have never flagged a real bug. |
Ted Kremenek | 8829989 | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 261 | if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) { |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 262 | if (!B->isAssignmentOp()) return; // Skip non-assignments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 263 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 264 | if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(B->getLHS())) |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 265 | if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
Ted Kremenek | e12691c | 2008-08-09 00:05:14 +0000 | [diff] [blame] | 266 | // Special case: check for assigning null to a pointer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 267 | // This is a common form of defensive programming. |
Ted Kremenek | bb811ca | 2012-04-04 19:58:03 +0000 | [diff] [blame] | 268 | const Expr *RHS = LookThroughTransitiveAssignments(B->getRHS()); |
| 269 | |
Ted Kremenek | 8913220 | 2010-02-23 21:19:33 +0000 | [diff] [blame] | 270 | QualType T = VD->getType(); |
| 271 | if (T->isPointerType() || T->isObjCObjectPointerType()) { |
Ted Kremenek | bb811ca | 2012-04-04 19:58:03 +0000 | [diff] [blame] | 272 | if (RHS->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNull)) |
Ted Kremenek | 93fab7c | 2009-11-22 20:26:21 +0000 | [diff] [blame] | 273 | return; |
Ted Kremenek | e12691c | 2008-08-09 00:05:14 +0000 | [diff] [blame] | 274 | } |
Ted Kremenek | 93fab7c | 2009-11-22 20:26:21 +0000 | [diff] [blame] | 275 | |
Ted Kremenek | bb811ca | 2012-04-04 19:58:03 +0000 | [diff] [blame] | 276 | RHS = RHS->IgnoreParenCasts(); |
Ted Kremenek | 3b58786 | 2009-01-09 22:15:01 +0000 | [diff] [blame] | 277 | // Special case: self-assignments. These are often used to shut up |
| 278 | // "unused variable" compiler warnings. |
Ted Kremenek | bb811ca | 2012-04-04 19:58:03 +0000 | [diff] [blame] | 279 | if (const DeclRefExpr *RhsDR = dyn_cast<DeclRefExpr>(RHS)) |
Ted Kremenek | 3b58786 | 2009-01-09 22:15:01 +0000 | [diff] [blame] | 280 | if (VD == dyn_cast<VarDecl>(RhsDR->getDecl())) |
| 281 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 282 | |
Ted Kremenek | 3b58786 | 2009-01-09 22:15:01 +0000 | [diff] [blame] | 283 | // Otherwise, issue a warning. |
Ted Kremenek | b930d7a | 2009-04-01 06:52:48 +0000 | [diff] [blame] | 284 | DeadStoreKind dsk = Parents.isConsumedExpr(B) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 285 | ? Enclosing |
Ted Kremenek | 7f5fce7 | 2009-01-20 00:47:45 +0000 | [diff] [blame] | 286 | : (isIncrement(VD,B) ? DeadIncrement : Standard); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 287 | |
Ted Kremenek | 8829989 | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 288 | CheckVarDecl(VD, DR, B->getRHS(), dsk, Live); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 289 | } |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 290 | } |
Ted Kremenek | 8829989 | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 291 | else if (const UnaryOperator* U = dyn_cast<UnaryOperator>(S)) { |
Ted Kremenek | f4e532b | 2011-02-12 00:17:19 +0000 | [diff] [blame] | 292 | if (!U->isIncrementOp() || U->isPrefix()) |
Ted Kremenek | a23157e | 2008-05-05 23:12:21 +0000 | [diff] [blame] | 293 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 294 | |
Ted Kremenek | 8829989 | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 295 | const Stmt *parent = Parents.getParentIgnoreParenCasts(U); |
Ted Kremenek | f4e532b | 2011-02-12 00:17:19 +0000 | [diff] [blame] | 296 | if (!parent || !isa<ReturnStmt>(parent)) |
Ted Kremenek | 380277e | 2008-10-15 05:23:41 +0000 | [diff] [blame] | 297 | return; |
Ted Kremenek | b0f3632 | 2008-07-24 17:01:17 +0000 | [diff] [blame] | 298 | |
Ted Kremenek | 8829989 | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 299 | const Expr *Ex = U->getSubExpr()->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 300 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 301 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex)) |
Ted Kremenek | 8829989 | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 302 | CheckDeclRef(DR, U, DeadIncrement, Live); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 303 | } |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 304 | else if (const DeclStmt *DS = dyn_cast<DeclStmt>(S)) |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 305 | // Iterate through the decls. Warn if any initializers are complex |
| 306 | // expressions that are not live (never used). |
Ted Kremenek | 8829989 | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 307 | for (DeclStmt::const_decl_iterator DI=DS->decl_begin(), DE=DS->decl_end(); |
Ted Kremenek | 14f8b4f | 2008-08-05 20:46:55 +0000 | [diff] [blame] | 308 | DI != DE; ++DI) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 309 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 310 | VarDecl *V = dyn_cast<VarDecl>(*DI); |
Ted Kremenek | fc7ff55 | 2008-07-25 04:47:34 +0000 | [diff] [blame] | 311 | |
| 312 | if (!V) |
| 313 | continue; |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 314 | |
| 315 | if (V->hasLocalStorage()) { |
| 316 | // Reference types confuse the dead stores checker. Skip them |
| 317 | // for now. |
| 318 | if (V->getType()->getAs<ReferenceType>()) |
| 319 | return; |
| 320 | |
Ted Kremenek | bb811ca | 2012-04-04 19:58:03 +0000 | [diff] [blame] | 321 | if (const Expr *E = V->getInit()) { |
| 322 | while (const ExprWithCleanups *exprClean = |
| 323 | dyn_cast<ExprWithCleanups>(E)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 324 | E = exprClean->getSubExpr(); |
| 325 | |
Ted Kremenek | bb811ca | 2012-04-04 19:58:03 +0000 | [diff] [blame] | 326 | // Look through transitive assignments, e.g.: |
| 327 | // int x = y = 0; |
| 328 | E = LookThroughTransitiveAssignments(E); |
| 329 | |
Ted Kremenek | 43f19e3 | 2009-12-15 04:12:12 +0000 | [diff] [blame] | 330 | // Don't warn on C++ objects (yet) until we can show that their |
| 331 | // constructors/destructors don't have side effects. |
| 332 | if (isa<CXXConstructExpr>(E)) |
| 333 | return; |
Ted Kremenek | 604d939 | 2009-12-23 04:11:44 +0000 | [diff] [blame] | 334 | |
Ted Kremenek | fc7ff55 | 2008-07-25 04:47:34 +0000 | [diff] [blame] | 335 | // A dead initialization is a variable that is dead after it |
| 336 | // is initialized. We don't flag warnings for those variables |
| 337 | // marked 'unused'. |
Ted Kremenek | 2827f5a | 2012-09-06 22:32:48 +0000 | [diff] [blame] | 338 | if (!isLive(Live, V) && V->getAttr<UnusedAttr>() == 0) { |
Ted Kremenek | c6a1faf | 2007-09-28 20:48:41 +0000 | [diff] [blame] | 339 | // Special case: check for initializations with constants. |
| 340 | // |
| 341 | // e.g. : int x = 0; |
| 342 | // |
| 343 | // If x is EVER assigned a new value later, don't issue |
| 344 | // a warning. This is because such initialization can be |
| 345 | // due to defensive programming. |
Richard Smith | 0e35b4e | 2011-12-06 23:25:15 +0000 | [diff] [blame] | 346 | if (E->isEvaluatable(Ctx)) |
Ted Kremenek | d3098ee | 2009-02-09 18:01:00 +0000 | [diff] [blame] | 347 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 348 | |
Ted Kremenek | bb811ca | 2012-04-04 19:58:03 +0000 | [diff] [blame] | 349 | if (const DeclRefExpr *DRE = |
| 350 | dyn_cast<DeclRefExpr>(E->IgnoreParenCasts())) |
| 351 | if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) { |
Ted Kremenek | ebd42f4 | 2010-03-18 01:22:39 +0000 | [diff] [blame] | 352 | // Special case: check for initialization from constant |
| 353 | // variables. |
| 354 | // |
| 355 | // e.g. extern const int MyConstant; |
| 356 | // int x = MyConstant; |
| 357 | // |
Ted Kremenek | d3098ee | 2009-02-09 18:01:00 +0000 | [diff] [blame] | 358 | if (VD->hasGlobalStorage() && |
Ted Kremenek | ebd42f4 | 2010-03-18 01:22:39 +0000 | [diff] [blame] | 359 | VD->getType().isConstQualified()) |
| 360 | return; |
| 361 | // Special case: check for initialization from scalar |
| 362 | // parameters. This is often a form of defensive |
| 363 | // programming. Non-scalars are still an error since |
| 364 | // because it more likely represents an actual algorithmic |
| 365 | // bug. |
| 366 | if (isa<ParmVarDecl>(VD) && VD->getType()->isScalarType()) |
| 367 | return; |
| 368 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 369 | |
Anna Zaks | 590dd8e | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 370 | PathDiagnosticLocation Loc = |
| 371 | PathDiagnosticLocation::create(V, BR.getSourceManager()); |
| 372 | Report(V, DeadInit, Loc, E->getSourceRange()); |
Ted Kremenek | ce1cab9 | 2007-09-11 17:24:14 +0000 | [diff] [blame] | 373 | } |
Ted Kremenek | fc7ff55 | 2008-07-25 04:47:34 +0000 | [diff] [blame] | 374 | } |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 375 | } |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 376 | } |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 377 | } |
| 378 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 379 | |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 380 | } // end anonymous namespace |
| 381 | |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 382 | //===----------------------------------------------------------------------===// |
Ted Kremenek | e207558 | 2008-07-02 23:16:33 +0000 | [diff] [blame] | 383 | // Driver function to invoke the Dead-Stores checker on a CFG. |
| 384 | //===----------------------------------------------------------------------===// |
| 385 | |
Ted Kremenek | f96f16d | 2009-04-07 05:25:24 +0000 | [diff] [blame] | 386 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 387 | class FindEscaped : public CFGRecStmtDeclVisitor<FindEscaped>{ |
Ted Kremenek | f96f16d | 2009-04-07 05:25:24 +0000 | [diff] [blame] | 388 | CFG *cfg; |
| 389 | public: |
| 390 | FindEscaped(CFG *c) : cfg(c) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 391 | |
Ted Kremenek | f96f16d | 2009-04-07 05:25:24 +0000 | [diff] [blame] | 392 | CFG& getCFG() { return *cfg; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 393 | |
Ted Kremenek | 8829989 | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 394 | llvm::SmallPtrSet<const VarDecl*, 20> Escaped; |
Ted Kremenek | f96f16d | 2009-04-07 05:25:24 +0000 | [diff] [blame] | 395 | |
| 396 | void VisitUnaryOperator(UnaryOperator* U) { |
| 397 | // Check for '&'. Any VarDecl whose value has its address-taken we |
| 398 | // treat as escaped. |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 399 | Expr *E = U->getSubExpr()->IgnoreParenCasts(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 400 | if (U->getOpcode() == UO_AddrOf) |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 401 | if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E)) |
| 402 | if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
Ted Kremenek | f96f16d | 2009-04-07 05:25:24 +0000 | [diff] [blame] | 403 | Escaped.insert(VD); |
| 404 | return; |
| 405 | } |
| 406 | Visit(E); |
| 407 | } |
| 408 | }; |
| 409 | } // end anonymous namespace |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 410 | |
Ted Kremenek | f96f16d | 2009-04-07 05:25:24 +0000 | [diff] [blame] | 411 | |
Argyrios Kyrtzidis | 7dd445e | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 412 | //===----------------------------------------------------------------------===// |
| 413 | // DeadStoresChecker |
| 414 | //===----------------------------------------------------------------------===// |
| 415 | |
| 416 | namespace { |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 417 | class DeadStoresChecker : public Checker<check::ASTCodeBody> { |
Argyrios Kyrtzidis | 7dd445e | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 418 | public: |
| 419 | void checkASTCodeBody(const Decl *D, AnalysisManager& mgr, |
| 420 | BugReporter &BR) const { |
Ted Kremenek | a5937bb | 2011-10-07 22:21:02 +0000 | [diff] [blame] | 421 | if (LiveVariables *L = mgr.getAnalysis<LiveVariables>(D)) { |
Argyrios Kyrtzidis | 7dd445e | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 422 | CFG &cfg = *mgr.getCFG(D); |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 423 | AnalysisDeclContext *AC = mgr.getAnalysisDeclContext(D); |
Argyrios Kyrtzidis | 7dd445e | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 424 | ParentMap &pmap = mgr.getParentMap(D); |
| 425 | FindEscaped FS(&cfg); |
| 426 | FS.getCFG().VisitBlockStmts(FS); |
Anna Zaks | 590dd8e | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 427 | DeadStoreObs A(cfg, BR.getContext(), BR, AC, pmap, FS.Escaped); |
Ted Kremenek | 8829989 | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 428 | L->runOnAllBlocks(A); |
Argyrios Kyrtzidis | 7dd445e | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | }; |
| 432 | } |
| 433 | |
| 434 | void ento::registerDeadStoresChecker(CheckerManager &mgr) { |
| 435 | mgr.registerChecker<DeadStoresChecker>(); |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 436 | } |