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 | |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/LocalCheckers.h" |
| 16 | #include "clang/Analysis/LiveVariables.h" |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 17 | #include "clang/Analysis/Visitors/CFGRecStmtVisitor.h" |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Diagnostic.h" |
Ted Kremenek | ce1cab9 | 2007-09-11 17:24:14 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace clang; |
| 22 | |
| 23 | namespace { |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 24 | |
| 25 | class DeadStoreObs : public LiveVariables::ObserverTy { |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 26 | ASTContext &Ctx; |
| 27 | Diagnostic &Diags; |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 28 | public: |
Ted Kremenek | fdd225e | 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 | ce1cab9 | 2007-09-11 17:24:14 +0000 | [diff] [blame] | 35 | |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 36 | if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) { |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 37 | if (!B->isAssignmentOp()) return; // Skip non-assignments. |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 38 | |
Ted Kremenek | c0576ca | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 39 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS())) |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 40 | // Is the variable NOT live? If so, flag a dead store. |
Ted Kremenek | f63aa45 | 2007-09-28 20:38:59 +0000 | [diff] [blame^] | 41 | if (!Live(DR->getDecl(),AD)) { |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 42 | SourceRange R = B->getRHS()->getSourceRange(); |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 43 | Diags.Report(DR->getSourceRange().Begin(), diag::warn_dead_store, |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 44 | 0, 0, &R, 1); |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 45 | } |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 46 | } |
Ted Kremenek | fdd225e | 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 | c0576ca | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 50 | for (VarDecl* V = cast<VarDecl>(DS->getDecl()); V != NULL ; |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 51 | V = cast_or_null<VarDecl>(V->getNextDeclarator())) { |
| 52 | if (Expr* E = V->getInit()) { |
Ted Kremenek | f63aa45 | 2007-09-28 20:38:59 +0000 | [diff] [blame^] | 53 | if (!Live(DS->getDecl(),AD)) { |
Ted Kremenek | ce1cab9 | 2007-09-11 17:24:14 +0000 | [diff] [blame] | 54 | // Special case: check for initializations with constants. |
| 55 | // |
| 56 | // e.g. : int x = 0; |
| 57 | // |
| 58 | // If x is EVER assigned a new value later, don't issue |
| 59 | // a warning. This is because such initialization can be |
| 60 | // due to defensive programming. |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 61 | if (!E->isConstantExpr(Ctx,NULL)) { |
Ted Kremenek | ce1cab9 | 2007-09-11 17:24:14 +0000 | [diff] [blame] | 62 | // Flag a warning. |
| 63 | SourceRange R = E->getSourceRange(); |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 64 | Diags.Report(V->getLocation(), diag::warn_dead_store, 0, 0, |
| 65 | &R,1); |
Ted Kremenek | ce1cab9 | 2007-09-11 17:24:14 +0000 | [diff] [blame] | 66 | } |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | } |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 70 | } |
| 71 | }; |
| 72 | |
| 73 | } // end anonymous namespace |
| 74 | |
| 75 | namespace clang { |
| 76 | |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 77 | void CheckDeadStores(CFG& cfg, ASTContext &Ctx, Diagnostic &Diags) { |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 78 | LiveVariables L; |
| 79 | L.runOnCFG(cfg); |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 80 | DeadStoreObs A(Ctx, Diags); |
| 81 | L.runOnAllBlocks(cfg,A); |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 84 | } // end namespace clang |