blob: e64214e6948aff5036d89d1c88c46bdcc5eb145e [file] [log] [blame]
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +00001//==- DeadStores.cpp - Check for stores to dead variables --------*- C++ -*-==//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +00007//
8//===----------------------------------------------------------------------===//
9//
Gabor Greif843e9342008-03-06 10:40:09 +000010// This file defines a DeadStores, a flow-sensitive checker that looks for
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000011// stores to variables that are no longer live.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000015#include "clang/Analysis/LocalCheckers.h"
Ted Kremenekcf6e41b2007-12-21 21:42:19 +000016#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenekfdd225e2007-09-25 04:31:27 +000017#include "clang/Analysis/Visitors/CFGRecStmtVisitor.h"
Ted Kremenekd2f642b2008-04-14 17:39:48 +000018#include "clang/Analysis/PathSensitive/BugReporter.h"
19#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000020#include "clang/Basic/Diagnostic.h"
Ted Kremenekce1cab92007-09-11 17:24:14 +000021#include "clang/AST/ASTContext.h"
Ted Kremenekc2b51d82008-01-08 18:19:08 +000022#include "llvm/Support/Compiler.h"
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000023
24using namespace clang;
25
26namespace {
Ted Kremenekfdd225e2007-09-25 04:31:27 +000027
Ted Kremenekc2b51d82008-01-08 18:19:08 +000028class VISIBILITY_HIDDEN DeadStoreObs : public LiveVariables::ObserverTy {
Chris Lattnerc0508f92007-09-15 23:21:08 +000029 ASTContext &Ctx;
30 Diagnostic &Diags;
Ted Kremenekd2f642b2008-04-14 17:39:48 +000031 DiagnosticClient &Client;
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000032public:
Ted Kremenekd2f642b2008-04-14 17:39:48 +000033 DeadStoreObs(ASTContext &ctx, Diagnostic &diags, DiagnosticClient &client)
34 : Ctx(ctx), Diags(diags), Client(client) {}
35
Ted Kremenekfdd225e2007-09-25 04:31:27 +000036 virtual ~DeadStoreObs() {}
37
38 virtual void ObserveStmt(Stmt* S,
39 const LiveVariables::AnalysisDataTy& AD,
40 const LiveVariables::ValTy& Live) {
Ted Kremenekce1cab92007-09-11 17:24:14 +000041
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000042 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Ted Kremenekfdd225e2007-09-25 04:31:27 +000043 if (!B->isAssignmentOp()) return; // Skip non-assignments.
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000044
Ted Kremenekc0576ca2007-09-10 17:36:42 +000045 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()))
Ted Kremenekc6a1faf2007-09-28 20:48:41 +000046 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl()))
47 if (VD->hasLocalStorage() && !Live(VD,AD)) {
48 SourceRange R = B->getRHS()->getSourceRange();
Ted Kremenekd2f642b2008-04-14 17:39:48 +000049 Diags.Report(&Client,
50 Ctx.getFullLoc(DR->getSourceRange().getBegin()),
Ted Kremenek9c728dc2007-12-12 22:39:36 +000051 diag::warn_dead_store, 0, 0, &R, 1);
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000052 }
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000053 }
Ted Kremenekfdd225e2007-09-25 04:31:27 +000054 else if(DeclStmt* DS = dyn_cast<DeclStmt>(S))
55 // Iterate through the decls. Warn if any initializers are complex
56 // expressions that are not live (never used).
Ted Kremenekc967c9d2008-04-14 17:52:13 +000057 for (ScopedDecl* SD = DS->getDecl(); SD; SD = SD->getNextDeclarator()) {
58
59 VarDecl* V = dyn_cast<VarDecl>(SD);
60 if (!V) continue;
61
Ted Kremenekc6a1faf2007-09-28 20:48:41 +000062 if (V->hasLocalStorage())
63 if (Expr* E = V->getInit()) {
64 if (!Live(DS->getDecl(),AD)) {
65 // Special case: check for initializations with constants.
66 //
67 // e.g. : int x = 0;
68 //
69 // If x is EVER assigned a new value later, don't issue
70 // a warning. This is because such initialization can be
71 // due to defensive programming.
72 if (!E->isConstantExpr(Ctx,NULL)) {
73 // Flag a warning.
74 SourceRange R = E->getSourceRange();
Ted Kremenekd2f642b2008-04-14 17:39:48 +000075 Diags.Report(&Client,
76 Ctx.getFullLoc(V->getLocation()),
Ted Kremenek9c728dc2007-12-12 22:39:36 +000077 diag::warn_dead_store, 0, 0, &R, 1);
Ted Kremenekc6a1faf2007-09-28 20:48:41 +000078 }
Ted Kremenekce1cab92007-09-11 17:24:14 +000079 }
Ted Kremenekfdd225e2007-09-25 04:31:27 +000080 }
Ted Kremenekfdd225e2007-09-25 04:31:27 +000081 }
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000082 }
83};
84
85} // end anonymous namespace
86
Ted Kremenekd2f642b2008-04-14 17:39:48 +000087//===----------------------------------------------------------------------===//
88// Driver function to invoke the Dead-Stores checker on a CFG.
89//===----------------------------------------------------------------------===//
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000090
Ted Kremenekd2f642b2008-04-14 17:39:48 +000091void clang::CheckDeadStores(CFG& cfg, ASTContext &Ctx, Diagnostic &Diags) {
Ted Kremenek7cb15932008-03-13 16:55:07 +000092 LiveVariables L(cfg);
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000093 L.runOnCFG(cfg);
Ted Kremenekd2f642b2008-04-14 17:39:48 +000094 DeadStoreObs A(Ctx, Diags, Diags.getClient());
Ted Kremenek7cb15932008-03-13 16:55:07 +000095 L.runOnAllBlocks(cfg, &A);
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000096}
97
Ted Kremenekd2f642b2008-04-14 17:39:48 +000098//===----------------------------------------------------------------------===//
99// BugReporter-based invocation of the Dead-Stores checker.
100//===----------------------------------------------------------------------===//
101
102namespace {
103
104class VISIBILITY_HIDDEN DiagBugReport : public RangedBugReport {
105 std::list<std::string> Strs;
106 FullSourceLoc L;
107public:
108 DiagBugReport(const BugType& D, FullSourceLoc l) :
109 RangedBugReport(D, NULL), L(l) {}
110
111 virtual ~DiagBugReport() {}
112 virtual FullSourceLoc getLocation(SourceManager&) { return L; }
113
114 void addString(const std::string& s) { Strs.push_back(s); }
115
116 typedef std::list<std::string>::const_iterator str_iterator;
117 str_iterator str_begin() const { return Strs.begin(); }
118 str_iterator str_end() const { return Strs.end(); }
119};
120
121class VISIBILITY_HIDDEN DiagCollector : public DiagnosticClient {
122 std::list<DiagBugReport> Reports;
123 const BugType& D;
124public:
125 DiagCollector(BugType& d) : D(d) {}
126
127 virtual ~DiagCollector() {}
128
129 virtual void HandleDiagnostic(Diagnostic &Diags,
130 Diagnostic::Level DiagLevel,
131 FullSourceLoc Pos,
132 diag::kind ID,
133 const std::string *Strs,
134 unsigned NumStrs,
135 const SourceRange *Ranges,
136 unsigned NumRanges) {
137
138 // FIXME: Use a map from diag::kind to BugType, instead of having just
139 // one BugType.
140
141 Reports.push_back(DiagBugReport(D, Pos));
142 DiagBugReport& R = Reports.back();
143
144 for ( ; NumRanges ; --NumRanges, ++Ranges)
145 R.addRange(*Ranges);
146
147 for ( ; NumStrs ; --NumStrs, ++Strs)
148 R.addString(*Strs);
149 }
150
151 // Iterators.
152
153 typedef std::list<DiagBugReport>::iterator iterator;
154 iterator begin() { return Reports.begin(); }
155 iterator end() { return Reports.end(); }
156};
157
158class VISIBILITY_HIDDEN DeadStoresChecker : public BugType {
159public:
160 virtual const char* getName() const {
161 return "dead store";
162 }
163
164 virtual const char* getDescription() const {
165 return "Value stored to variable is never used.";
166 }
167
168 virtual void EmitWarnings(BugReporter& BR) {
169
170 // Run the dead store checker and collect the diagnostics.
171 DiagCollector C(*this);
172 DeadStoreObs A(BR.getContext(), BR.getDiagnostic(), C);
173 GRExprEngine& Eng = BR.getEngine();
174 Eng.getLiveness().runOnAllBlocks(Eng.getCFG(), &A);
175
176 // Emit the bug reports.
177
178 for (DiagCollector::iterator I = C.begin(), E = C.end(); I != E; ++I)
179 BR.EmitWarning(*I);
180 }
181};
182} // end anonymous namespace
183
184BugType* clang::MakeDeadStoresChecker() {
185 return new DeadStoresChecker();
186}