Shih-wei Liao | f8fd82b | 2010-02-10 11:10:31 -0800 | [diff] [blame^] | 1 | //==- DeadStores.cpp - Check for stores to dead variables --------*- C++ -*-==// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines a DeadStores, a flow-sensitive checker that looks for |
| 11 | // stores to variables that are no longer live. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Checker/Checkers/LocalCheckers.h" |
| 16 | #include "clang/Analysis/Analyses/LiveVariables.h" |
| 17 | #include "clang/Analysis/Visitors/CFGRecStmtVisitor.h" |
| 18 | #include "clang/Checker/BugReporter/BugReporter.h" |
| 19 | #include "clang/Checker/PathSensitive/GRExprEngine.h" |
| 20 | #include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h" |
| 21 | #include "clang/Basic/Diagnostic.h" |
| 22 | #include "clang/AST/ASTContext.h" |
| 23 | #include "clang/AST/ParentMap.h" |
| 24 | #include "llvm/ADT/SmallPtrSet.h" |
| 25 | |
| 26 | using namespace clang; |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | class DeadStoreObs : public LiveVariables::ObserverTy { |
| 31 | ASTContext &Ctx; |
| 32 | BugReporter& BR; |
| 33 | ParentMap& Parents; |
| 34 | llvm::SmallPtrSet<VarDecl*, 20> Escaped; |
| 35 | |
| 36 | enum DeadStoreKind { Standard, Enclosing, DeadIncrement, DeadInit }; |
| 37 | |
| 38 | public: |
| 39 | DeadStoreObs(ASTContext &ctx, BugReporter& br, ParentMap& parents, |
| 40 | llvm::SmallPtrSet<VarDecl*, 20> &escaped) |
| 41 | : Ctx(ctx), BR(br), Parents(parents), Escaped(escaped) {} |
| 42 | |
| 43 | virtual ~DeadStoreObs() {} |
| 44 | |
| 45 | void Report(VarDecl* V, DeadStoreKind dsk, SourceLocation L, SourceRange R) { |
| 46 | if (Escaped.count(V)) |
| 47 | return; |
| 48 | |
| 49 | std::string name = V->getNameAsString(); |
| 50 | |
| 51 | const char* BugType = 0; |
| 52 | std::string msg; |
| 53 | |
| 54 | switch (dsk) { |
| 55 | default: |
| 56 | assert(false && "Impossible dead store type."); |
| 57 | |
| 58 | case DeadInit: |
| 59 | BugType = "Dead initialization"; |
| 60 | msg = "Value stored to '" + name + |
| 61 | "' during its initialization is never read"; |
| 62 | break; |
| 63 | |
| 64 | case DeadIncrement: |
| 65 | BugType = "Dead increment"; |
| 66 | case Standard: |
| 67 | if (!BugType) BugType = "Dead assignment"; |
| 68 | msg = "Value stored to '" + name + "' is never read"; |
| 69 | break; |
| 70 | |
| 71 | case Enclosing: |
| 72 | BugType = "Dead nested assignment"; |
| 73 | msg = "Although the value stored to '" + name + |
| 74 | "' is used in the enclosing expression, the value is never actually" |
| 75 | " read from '" + name + "'"; |
| 76 | break; |
| 77 | } |
| 78 | |
| 79 | BR.EmitBasicReport(BugType, "Dead store", msg, L, R); |
| 80 | } |
| 81 | |
| 82 | void CheckVarDecl(VarDecl* VD, Expr* Ex, Expr* Val, |
| 83 | DeadStoreKind dsk, |
| 84 | const LiveVariables::AnalysisDataTy& AD, |
| 85 | const LiveVariables::ValTy& Live) { |
| 86 | |
| 87 | if (!VD->hasLocalStorage()) |
| 88 | return; |
| 89 | // Reference types confuse the dead stores checker. Skip them |
| 90 | // for now. |
| 91 | if (VD->getType()->getAs<ReferenceType>()) |
| 92 | return; |
| 93 | |
| 94 | if (!Live(VD, AD) && |
| 95 | !(VD->getAttr<UnusedAttr>() || VD->getAttr<BlocksAttr>())) |
| 96 | Report(VD, dsk, Ex->getSourceRange().getBegin(), |
| 97 | Val->getSourceRange()); |
| 98 | } |
| 99 | |
| 100 | void CheckDeclRef(DeclRefExpr* DR, Expr* Val, DeadStoreKind dsk, |
| 101 | const LiveVariables::AnalysisDataTy& AD, |
| 102 | const LiveVariables::ValTy& Live) { |
| 103 | if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) |
| 104 | CheckVarDecl(VD, DR, Val, dsk, AD, Live); |
| 105 | } |
| 106 | |
| 107 | bool isIncrement(VarDecl* VD, BinaryOperator* B) { |
| 108 | if (B->isCompoundAssignmentOp()) |
| 109 | return true; |
| 110 | |
| 111 | Expr* RHS = B->getRHS()->IgnoreParenCasts(); |
| 112 | BinaryOperator* BRHS = dyn_cast<BinaryOperator>(RHS); |
| 113 | |
| 114 | if (!BRHS) |
| 115 | return false; |
| 116 | |
| 117 | DeclRefExpr *DR; |
| 118 | |
| 119 | if ((DR = dyn_cast<DeclRefExpr>(BRHS->getLHS()->IgnoreParenCasts()))) |
| 120 | if (DR->getDecl() == VD) |
| 121 | return true; |
| 122 | |
| 123 | if ((DR = dyn_cast<DeclRefExpr>(BRHS->getRHS()->IgnoreParenCasts()))) |
| 124 | if (DR->getDecl() == VD) |
| 125 | return true; |
| 126 | |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | virtual void ObserveStmt(Stmt* S, |
| 131 | const LiveVariables::AnalysisDataTy& AD, |
| 132 | const LiveVariables::ValTy& Live) { |
| 133 | |
| 134 | // Skip statements in macros. |
| 135 | if (S->getLocStart().isMacroID()) |
| 136 | return; |
| 137 | |
| 138 | if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) { |
| 139 | if (!B->isAssignmentOp()) return; // Skip non-assignments. |
| 140 | |
| 141 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS())) |
| 142 | if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 143 | // Special case: check for assigning null to a pointer. |
| 144 | // This is a common form of defensive programming. |
| 145 | if (VD->getType()->isPointerType()) { |
| 146 | if (B->getRHS()->isNullPointerConstant(Ctx, |
| 147 | Expr::NPC_ValueDependentIsNull)) |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | Expr* RHS = B->getRHS()->IgnoreParenCasts(); |
| 152 | // Special case: self-assignments. These are often used to shut up |
| 153 | // "unused variable" compiler warnings. |
| 154 | if (DeclRefExpr* RhsDR = dyn_cast<DeclRefExpr>(RHS)) |
| 155 | if (VD == dyn_cast<VarDecl>(RhsDR->getDecl())) |
| 156 | return; |
| 157 | |
| 158 | // Otherwise, issue a warning. |
| 159 | DeadStoreKind dsk = Parents.isConsumedExpr(B) |
| 160 | ? Enclosing |
| 161 | : (isIncrement(VD,B) ? DeadIncrement : Standard); |
| 162 | |
| 163 | CheckVarDecl(VD, DR, B->getRHS(), dsk, AD, Live); |
| 164 | } |
| 165 | } |
| 166 | else if (UnaryOperator* U = dyn_cast<UnaryOperator>(S)) { |
| 167 | if (!U->isIncrementOp()) |
| 168 | return; |
| 169 | |
| 170 | // Handle: ++x within a subexpression. The solution is not warn |
| 171 | // about preincrements to dead variables when the preincrement occurs |
| 172 | // as a subexpression. This can lead to false negatives, e.g. "(++x);" |
| 173 | // A generalized dead code checker should find such issues. |
| 174 | if (U->isPrefix() && Parents.isConsumedExpr(U)) |
| 175 | return; |
| 176 | |
| 177 | Expr *Ex = U->getSubExpr()->IgnoreParenCasts(); |
| 178 | |
| 179 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(Ex)) |
| 180 | CheckDeclRef(DR, U, DeadIncrement, AD, Live); |
| 181 | } |
| 182 | else if (DeclStmt* DS = dyn_cast<DeclStmt>(S)) |
| 183 | // Iterate through the decls. Warn if any initializers are complex |
| 184 | // expressions that are not live (never used). |
| 185 | for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end(); |
| 186 | DI != DE; ++DI) { |
| 187 | |
| 188 | VarDecl* V = dyn_cast<VarDecl>(*DI); |
| 189 | |
| 190 | if (!V) |
| 191 | continue; |
| 192 | |
| 193 | if (V->hasLocalStorage()) { |
| 194 | // Reference types confuse the dead stores checker. Skip them |
| 195 | // for now. |
| 196 | if (V->getType()->getAs<ReferenceType>()) |
| 197 | return; |
| 198 | |
| 199 | if (Expr* E = V->getInit()) { |
| 200 | // Don't warn on C++ objects (yet) until we can show that their |
| 201 | // constructors/destructors don't have side effects. |
| 202 | if (isa<CXXConstructExpr>(E)) |
| 203 | return; |
| 204 | |
| 205 | if (isa<CXXExprWithTemporaries>(E)) |
| 206 | return; |
| 207 | |
| 208 | // A dead initialization is a variable that is dead after it |
| 209 | // is initialized. We don't flag warnings for those variables |
| 210 | // marked 'unused'. |
| 211 | if (!Live(V, AD) && V->getAttr<UnusedAttr>() == 0) { |
| 212 | // Special case: check for initializations with constants. |
| 213 | // |
| 214 | // e.g. : int x = 0; |
| 215 | // |
| 216 | // If x is EVER assigned a new value later, don't issue |
| 217 | // a warning. This is because such initialization can be |
| 218 | // due to defensive programming. |
| 219 | if (E->isConstantInitializer(Ctx)) |
| 220 | return; |
| 221 | |
| 222 | // Special case: check for initializations from constant |
| 223 | // variables. |
| 224 | // |
| 225 | // e.g. extern const int MyConstant; |
| 226 | // int x = MyConstant; |
| 227 | // |
| 228 | if (DeclRefExpr *DRE=dyn_cast<DeclRefExpr>(E->IgnoreParenCasts())) |
| 229 | if (VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) |
| 230 | if (VD->hasGlobalStorage() && |
| 231 | VD->getType().isConstQualified()) return; |
| 232 | |
| 233 | Report(V, DeadInit, V->getLocation(), E->getSourceRange()); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | }; |
| 240 | |
| 241 | } // end anonymous namespace |
| 242 | |
| 243 | //===----------------------------------------------------------------------===// |
| 244 | // Driver function to invoke the Dead-Stores checker on a CFG. |
| 245 | //===----------------------------------------------------------------------===// |
| 246 | |
| 247 | namespace { |
| 248 | class FindEscaped : public CFGRecStmtDeclVisitor<FindEscaped>{ |
| 249 | CFG *cfg; |
| 250 | public: |
| 251 | FindEscaped(CFG *c) : cfg(c) {} |
| 252 | |
| 253 | CFG& getCFG() { return *cfg; } |
| 254 | |
| 255 | llvm::SmallPtrSet<VarDecl*, 20> Escaped; |
| 256 | |
| 257 | void VisitUnaryOperator(UnaryOperator* U) { |
| 258 | // Check for '&'. Any VarDecl whose value has its address-taken we |
| 259 | // treat as escaped. |
| 260 | Expr* E = U->getSubExpr()->IgnoreParenCasts(); |
| 261 | if (U->getOpcode() == UnaryOperator::AddrOf) |
| 262 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E)) |
| 263 | if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 264 | Escaped.insert(VD); |
| 265 | return; |
| 266 | } |
| 267 | Visit(E); |
| 268 | } |
| 269 | }; |
| 270 | } // end anonymous namespace |
| 271 | |
| 272 | |
| 273 | void clang::CheckDeadStores(CFG &cfg, LiveVariables &L, ParentMap &pmap, |
| 274 | BugReporter& BR) { |
| 275 | FindEscaped FS(&cfg); |
| 276 | FS.getCFG().VisitBlockStmts(FS); |
| 277 | DeadStoreObs A(BR.getContext(), BR, pmap, FS.Escaped); |
| 278 | L.runOnAllBlocks(cfg, &A); |
| 279 | } |