| Ted Kremenek | e9646a0 | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 1 | //==- DeadStores.cpp - Check for stores to dead variables --------*- C++ -*-==// | 
|  | 2 | // | 
|  | 3 | //                     The LLVM Compiler Infrastructure | 
|  | 4 | // | 
|  | 5 | // This file was developed by Ted Kremenek and is distributed under | 
|  | 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. | 
|  | 7 | // | 
|  | 8 | //===----------------------------------------------------------------------===// | 
|  | 9 | // | 
|  | 10 | //  This files defines a DeadStores, a flow-sensitive checker that looks for | 
|  | 11 | //  stores to variables that are no longer live. | 
|  | 12 | // | 
|  | 13 | //===----------------------------------------------------------------------===// | 
|  | 14 |  | 
| Ted Kremenek | e9646a0 | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/LocalCheckers.h" | 
|  | 16 | #include "clang/Analysis/LiveVariables.h" | 
| Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 17 | #include "clang/Analysis/Visitors/CFGRecStmtVisitor.h" | 
| Ted Kremenek | e9646a0 | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Diagnostic.h" | 
| Ted Kremenek | 1278929 | 2007-09-11 17:24:14 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" | 
| Ted Kremenek | e9646a0 | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 20 |  | 
|  | 21 | using namespace clang; | 
|  | 22 |  | 
|  | 23 | namespace { | 
| Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 24 |  | 
|  | 25 | class DeadStoreObs : public LiveVariables::ObserverTy { | 
| Chris Lattner | 52332d0 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 26 | ASTContext &Ctx; | 
|  | 27 | Diagnostic &Diags; | 
| Ted Kremenek | e9646a0 | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 28 | public: | 
| Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 29 | DeadStoreObs(ASTContext &ctx,Diagnostic &diags) : Ctx(ctx), Diags(diags){} | 
|  | 30 | virtual ~DeadStoreObs() {} | 
|  | 31 |  | 
|  | 32 | virtual void ObserveStmt(Stmt* S, | 
|  | 33 | const LiveVariables::AnalysisDataTy& AD, | 
|  | 34 | const LiveVariables::ValTy& Live) { | 
| Ted Kremenek | 1278929 | 2007-09-11 17:24:14 +0000 | [diff] [blame] | 35 |  | 
| Ted Kremenek | e9646a0 | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 36 | if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) { | 
| Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 37 | if (!B->isAssignmentOp()) return; // Skip non-assignments. | 
| Ted Kremenek | e9646a0 | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 38 |  | 
| Ted Kremenek | 6b2b4e3 | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 39 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS())) | 
| Ted Kremenek | 4f20054 | 2007-09-28 20:48:41 +0000 | [diff] [blame^] | 40 | if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) | 
|  | 41 | if (VD->hasLocalStorage() && !Live(VD,AD)) { | 
|  | 42 | SourceRange R = B->getRHS()->getSourceRange(); | 
|  | 43 | Diags.Report(DR->getSourceRange().Begin(), diag::warn_dead_store, | 
|  | 44 | 0, 0, &R, 1); | 
| Ted Kremenek | e9646a0 | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 45 | } | 
| Ted Kremenek | e9646a0 | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 46 | } | 
| Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 47 | else if(DeclStmt* DS = dyn_cast<DeclStmt>(S)) | 
|  | 48 | // Iterate through the decls.  Warn if any initializers are complex | 
|  | 49 | // expressions that are not live (never used). | 
| Ted Kremenek | 6b2b4e3 | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 50 | for (VarDecl* V = cast<VarDecl>(DS->getDecl()); V != NULL ; | 
| Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 51 | V = cast_or_null<VarDecl>(V->getNextDeclarator())) { | 
| Ted Kremenek | 4f20054 | 2007-09-28 20:48:41 +0000 | [diff] [blame^] | 52 | if (V->hasLocalStorage()) | 
|  | 53 | if (Expr* E = V->getInit()) { | 
|  | 54 | if (!Live(DS->getDecl(),AD)) { | 
|  | 55 | // Special case: check for initializations with constants. | 
|  | 56 | // | 
|  | 57 | //  e.g. : int x = 0; | 
|  | 58 | // | 
|  | 59 | // If x is EVER assigned a new value later, don't issue | 
|  | 60 | // a warning.  This is because such initialization can be | 
|  | 61 | // due to defensive programming. | 
|  | 62 | if (!E->isConstantExpr(Ctx,NULL)) { | 
|  | 63 | // Flag a warning. | 
|  | 64 | SourceRange R = E->getSourceRange(); | 
|  | 65 | Diags.Report(V->getLocation(), diag::warn_dead_store, 0, 0, | 
|  | 66 | &R,1); | 
|  | 67 | } | 
| Ted Kremenek | 1278929 | 2007-09-11 17:24:14 +0000 | [diff] [blame] | 68 | } | 
| Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 69 | } | 
| Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 70 | } | 
| Ted Kremenek | e9646a0 | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 71 | } | 
|  | 72 | }; | 
|  | 73 |  | 
|  | 74 | } // end anonymous namespace | 
|  | 75 |  | 
|  | 76 | namespace clang { | 
|  | 77 |  | 
| Chris Lattner | 52332d0 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 78 | void CheckDeadStores(CFG& cfg, ASTContext &Ctx, Diagnostic &Diags) { | 
| Ted Kremenek | e9646a0 | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 79 | LiveVariables L; | 
|  | 80 | L.runOnCFG(cfg); | 
| Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 81 | DeadStoreObs A(Ctx, Diags); | 
|  | 82 | L.runOnAllBlocks(cfg,A); | 
| Ted Kremenek | e9646a0 | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 83 | } | 
|  | 84 |  | 
| Gabor Greif | 61ce98c | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 85 | } // end namespace clang |