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 | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame^] | 20 | #include "llvm/ADT/SmallPtrSet.h" |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace clang; |
| 23 | |
| 24 | namespace { |
| 25 | |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame^] | 26 | class EverKilled : public LiveVariables::ObserverTy { |
| 27 | llvm::SmallPtrSet<const VarDecl*, 10> Killed; |
| 28 | public: |
| 29 | virtual void ObserveKill(DeclRefExpr* DR) { |
| 30 | Killed.insert(cast<VarDecl>(DR->getDecl())); |
| 31 | } |
| 32 | |
| 33 | bool hasKill(const VarDecl* V) { return Killed.count(V) != 0; } |
| 34 | }; |
| 35 | |
| 36 | class DeadStoreObs : public LiveVariables::ObserverTy { |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 37 | ASTContext &Ctx; |
| 38 | Diagnostic &Diags; |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame^] | 39 | EverKilled EK; |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 40 | public: |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame^] | 41 | DeadStoreObs(ASTContext &ctx,Diagnostic &diags) : Ctx(ctx), Diags(diags){} |
| 42 | virtual ~DeadStoreObs() {} |
| 43 | |
| 44 | virtual void ObserveStmt(Stmt* S, |
| 45 | const LiveVariables::AnalysisDataTy& AD, |
| 46 | const LiveVariables::ValTy& Live) { |
Ted Kremenek | ce1cab9 | 2007-09-11 17:24:14 +0000 | [diff] [blame] | 47 | |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 48 | if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) { |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame^] | 49 | if (!B->isAssignmentOp()) return; // Skip non-assignments. |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 50 | |
Ted Kremenek | c0576ca | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 51 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS())) |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame^] | 52 | // Is the variable NOT live? If so, flag a dead store. |
| 53 | if (!Live(AD,DR->getDecl())) { |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 54 | SourceRange R = B->getRHS()->getSourceRange(); |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 55 | Diags.Report(DR->getSourceRange().Begin(), diag::warn_dead_store, |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame^] | 56 | 0, 0, &R, 1); |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 57 | } |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 58 | } |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame^] | 59 | else if(DeclStmt* DS = dyn_cast<DeclStmt>(S)) |
| 60 | // Iterate through the decls. Warn if any initializers are complex |
| 61 | // expressions that are not live (never used). |
Ted Kremenek | c0576ca | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 62 | for (VarDecl* V = cast<VarDecl>(DS->getDecl()); V != NULL ; |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame^] | 63 | V = cast_or_null<VarDecl>(V->getNextDeclarator())) { |
| 64 | if (Expr* E = V->getInit()) { |
| 65 | if (!Live(AD,DS->getDecl())) { |
Ted Kremenek | ce1cab9 | 2007-09-11 17:24:14 +0000 | [diff] [blame] | 66 | // Special case: check for initializations with constants. |
| 67 | // |
| 68 | // e.g. : int x = 0; |
| 69 | // |
| 70 | // If x is EVER assigned a new value later, don't issue |
| 71 | // a warning. This is because such initialization can be |
| 72 | // due to defensive programming. |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame^] | 73 | if (!E->isConstantExpr(Ctx,NULL)) { |
Ted Kremenek | ce1cab9 | 2007-09-11 17:24:14 +0000 | [diff] [blame] | 74 | // Flag a warning. |
| 75 | SourceRange R = E->getSourceRange(); |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 76 | Diags.Report(V->getLocation(), diag::warn_dead_store, 0, 0, |
| 77 | &R,1); |
Ted Kremenek | ce1cab9 | 2007-09-11 17:24:14 +0000 | [diff] [blame] | 78 | } |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame^] | 79 | } |
| 80 | } |
| 81 | } |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 82 | } |
| 83 | }; |
| 84 | |
| 85 | } // end anonymous namespace |
| 86 | |
| 87 | namespace clang { |
| 88 | |
Chris Lattner | c0508f9 | 2007-09-15 23:21:08 +0000 | [diff] [blame] | 89 | void CheckDeadStores(CFG& cfg, ASTContext &Ctx, Diagnostic &Diags) { |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 90 | LiveVariables L; |
| 91 | L.runOnCFG(cfg); |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame^] | 92 | DeadStoreObs A(Ctx, Diags); |
| 93 | L.runOnAllBlocks(cfg,A); |
Ted Kremenek | 1ed6d2e | 2007-09-06 23:01:46 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 96 | } // end namespace clang |