Ted Kremenek | 1ed6d2e | 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 | |
| 15 | #include "clang/AST/Expr.h" |
| 16 | #include "clang/Analysis/LocalCheckers.h" |
| 17 | #include "clang/Analysis/LiveVariables.h" |
| 18 | #include "clang/AST/CFG.h" |
| 19 | #include "clang/Basic/Diagnostic.h" |
| 20 | #include "clang/Lex/Preprocessor.h" |
| 21 | |
| 22 | using namespace clang; |
| 23 | |
| 24 | namespace { |
| 25 | |
Ted Kremenek | b00c95e | 2007-09-10 15:56:38 +0000 | [diff] [blame^] | 26 | class DeadStoreObserver : public LiveVariablesObserver { |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 27 | Preprocessor& PP; |
| 28 | public: |
Ted Kremenek | b00c95e | 2007-09-10 15:56:38 +0000 | [diff] [blame^] | 29 | DeadStoreObserver(Preprocessor& pp) : PP(pp) {} |
| 30 | virtual ~DeadStoreObserver() {} |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 31 | |
Ted Kremenek | b00c95e | 2007-09-10 15:56:38 +0000 | [diff] [blame^] | 32 | virtual void ObserveStmt(Stmt* S, LiveVariables& L, llvm::BitVector& Live) { |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 33 | if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) { |
| 34 | // Is this an assignment? |
| 35 | if (!B->isAssignmentOp()) |
| 36 | return; |
| 37 | |
| 38 | // Is this an assignment to a variable? |
| 39 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS())) { |
| 40 | // Is the variable live? |
| 41 | if (!L.isLive(Live,DR->getDecl())) { |
| 42 | SourceRange R = B->getRHS()->getSourceRange(); |
| 43 | PP.getDiagnostics().Report(DR->getSourceRange().Begin(), |
| 44 | diag::warn_dead_store, 0, 0, |
| 45 | &R,1); |
| 46 | |
| 47 | } |
| 48 | } |
| 49 | } |
Ted Kremenek | 83522a3 | 2007-09-06 23:39:53 +0000 | [diff] [blame] | 50 | else if(DeclStmt* DS = dyn_cast<DeclStmt>(S)) { |
| 51 | // Iterate through the decls. Warn if any of them (which have |
| 52 | // initializers) are not live. |
| 53 | for (Decl* D = DS->getDecl() ; D != NULL ; D = D->getNextDeclarator()) |
| 54 | if (VarDecl* V = dyn_cast<VarDecl>(D)) |
| 55 | if (Expr* E = V->getInit()) |
| 56 | if (!L.isLive(Live,D)) { |
| 57 | SourceRange R = E->getSourceRange(); |
| 58 | PP.getDiagnostics().Report(D->getLocation(), |
| 59 | diag::warn_dead_store, 0, 0, |
| 60 | &R,1); |
| 61 | } |
| 62 | } |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 63 | } |
| 64 | }; |
| 65 | |
| 66 | } // end anonymous namespace |
| 67 | |
| 68 | namespace clang { |
| 69 | |
| 70 | void CheckDeadStores(CFG& cfg, LiveVariables& L, Preprocessor& PP) { |
Ted Kremenek | b00c95e | 2007-09-10 15:56:38 +0000 | [diff] [blame^] | 71 | DeadStoreObserver A(PP); |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 72 | |
| 73 | for (CFG::iterator I = cfg.begin(), E = cfg.end(); I != E; ++I) |
| 74 | L.runOnBlock(&(*I),&A); |
| 75 | } |
| 76 | |
| 77 | void CheckDeadStores(CFG& cfg, Preprocessor& PP) { |
| 78 | LiveVariables L; |
| 79 | L.runOnCFG(cfg); |
| 80 | CheckDeadStores(cfg,L,PP); |
| 81 | } |
| 82 | |
| 83 | } // end namespace clang |