blob: 6858e3ab370c40d3edd9d8730a72908899eeb9cf [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 Kremenek1c86b152008-04-14 18:28:25 +000042 // Skip statements in macros.
43 if (S->getLocStart().isMacroID())
44 return;
45
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000046 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
Ted Kremenekfdd225e2007-09-25 04:31:27 +000047 if (!B->isAssignmentOp()) return; // Skip non-assignments.
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000048
Ted Kremenekc0576ca2007-09-10 17:36:42 +000049 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()))
Ted Kremenekc6a1faf2007-09-28 20:48:41 +000050 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl()))
51 if (VD->hasLocalStorage() && !Live(VD,AD)) {
52 SourceRange R = B->getRHS()->getSourceRange();
Ted Kremenekd2f642b2008-04-14 17:39:48 +000053 Diags.Report(&Client,
54 Ctx.getFullLoc(DR->getSourceRange().getBegin()),
Ted Kremenek9c728dc2007-12-12 22:39:36 +000055 diag::warn_dead_store, 0, 0, &R, 1);
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000056 }
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000057 }
Ted Kremenekfdd225e2007-09-25 04:31:27 +000058 else if(DeclStmt* DS = dyn_cast<DeclStmt>(S))
59 // Iterate through the decls. Warn if any initializers are complex
60 // expressions that are not live (never used).
Ted Kremenekc967c9d2008-04-14 17:52:13 +000061 for (ScopedDecl* SD = DS->getDecl(); SD; SD = SD->getNextDeclarator()) {
62
63 VarDecl* V = dyn_cast<VarDecl>(SD);
64 if (!V) continue;
65
Ted Kremenekc6a1faf2007-09-28 20:48:41 +000066 if (V->hasLocalStorage())
67 if (Expr* E = V->getInit()) {
Ted Kremenek9d7af512008-04-15 04:11:48 +000068 if (!Live(V, AD)) {
Ted Kremenekc6a1faf2007-09-28 20:48:41 +000069 // Special case: check for initializations with constants.
70 //
71 // e.g. : int x = 0;
72 //
73 // If x is EVER assigned a new value later, don't issue
74 // a warning. This is because such initialization can be
75 // due to defensive programming.
76 if (!E->isConstantExpr(Ctx,NULL)) {
77 // Flag a warning.
78 SourceRange R = E->getSourceRange();
Ted Kremenekd2f642b2008-04-14 17:39:48 +000079 Diags.Report(&Client,
80 Ctx.getFullLoc(V->getLocation()),
Ted Kremenek9c728dc2007-12-12 22:39:36 +000081 diag::warn_dead_store, 0, 0, &R, 1);
Ted Kremenekc6a1faf2007-09-28 20:48:41 +000082 }
Ted Kremenekce1cab92007-09-11 17:24:14 +000083 }
Ted Kremenekfdd225e2007-09-25 04:31:27 +000084 }
Ted Kremenekfdd225e2007-09-25 04:31:27 +000085 }
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000086 }
87};
88
89} // end anonymous namespace
90
Ted Kremenekd2f642b2008-04-14 17:39:48 +000091//===----------------------------------------------------------------------===//
92// Driver function to invoke the Dead-Stores checker on a CFG.
93//===----------------------------------------------------------------------===//
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000094
Ted Kremenekd2f642b2008-04-14 17:39:48 +000095void clang::CheckDeadStores(CFG& cfg, ASTContext &Ctx, Diagnostic &Diags) {
Ted Kremenek7cb15932008-03-13 16:55:07 +000096 LiveVariables L(cfg);
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000097 L.runOnCFG(cfg);
Ted Kremenekd2f642b2008-04-14 17:39:48 +000098 DeadStoreObs A(Ctx, Diags, Diags.getClient());
Ted Kremenek7cb15932008-03-13 16:55:07 +000099 L.runOnAllBlocks(cfg, &A);
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +0000100}
101
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000102//===----------------------------------------------------------------------===//
103// BugReporter-based invocation of the Dead-Stores checker.
104//===----------------------------------------------------------------------===//
105
106namespace {
107
108class VISIBILITY_HIDDEN DiagBugReport : public RangedBugReport {
109 std::list<std::string> Strs;
110 FullSourceLoc L;
111public:
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000112 DiagBugReport(BugType& D, FullSourceLoc l) :
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000113 RangedBugReport(D, NULL), L(l) {}
114
115 virtual ~DiagBugReport() {}
116 virtual FullSourceLoc getLocation(SourceManager&) { return L; }
117
118 void addString(const std::string& s) { Strs.push_back(s); }
119
120 typedef std::list<std::string>::const_iterator str_iterator;
121 str_iterator str_begin() const { return Strs.begin(); }
122 str_iterator str_end() const { return Strs.end(); }
123};
124
125class VISIBILITY_HIDDEN DiagCollector : public DiagnosticClient {
126 std::list<DiagBugReport> Reports;
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000127 BugType& D;
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000128public:
129 DiagCollector(BugType& d) : D(d) {}
130
131 virtual ~DiagCollector() {}
132
133 virtual void HandleDiagnostic(Diagnostic &Diags,
134 Diagnostic::Level DiagLevel,
135 FullSourceLoc Pos,
136 diag::kind ID,
137 const std::string *Strs,
138 unsigned NumStrs,
139 const SourceRange *Ranges,
140 unsigned NumRanges) {
141
142 // FIXME: Use a map from diag::kind to BugType, instead of having just
143 // one BugType.
144
145 Reports.push_back(DiagBugReport(D, Pos));
146 DiagBugReport& R = Reports.back();
147
148 for ( ; NumRanges ; --NumRanges, ++Ranges)
149 R.addRange(*Ranges);
150
151 for ( ; NumStrs ; --NumStrs, ++Strs)
152 R.addString(*Strs);
153 }
154
155 // Iterators.
156
157 typedef std::list<DiagBugReport>::iterator iterator;
158 iterator begin() { return Reports.begin(); }
159 iterator end() { return Reports.end(); }
160};
161
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000162class VISIBILITY_HIDDEN DeadStoresChecker : public BugTypeCacheLocation {
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000163public:
164 virtual const char* getName() const {
165 return "dead store";
166 }
167
168 virtual const char* getDescription() const {
Ted Kremenek1d5d1da2008-04-15 05:31:00 +0000169 return "Value stored to variable is never subsequently read.";
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000170 }
171
172 virtual void EmitWarnings(BugReporter& BR) {
173
174 // Run the dead store checker and collect the diagnostics.
175 DiagCollector C(*this);
176 DeadStoreObs A(BR.getContext(), BR.getDiagnostic(), C);
177 GRExprEngine& Eng = BR.getEngine();
178 Eng.getLiveness().runOnAllBlocks(Eng.getCFG(), &A);
179
180 // Emit the bug reports.
181
182 for (DiagCollector::iterator I = C.begin(), E = C.end(); I != E; ++I)
183 BR.EmitWarning(*I);
184 }
185};
186} // end anonymous namespace
187
188BugType* clang::MakeDeadStoresChecker() {
189 return new DeadStoresChecker();
190}