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