blob: e7077ecae3116e50a887a8484a235b91794fa0f8 [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//
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
15#include "clang/AST/Expr.h"
16#include "clang/Analysis/LocalCheckers.h"
17#include "clang/Analysis/LiveVariables.h"
18#include "clang/AST/CFG.h"
19#include "clang/Basic/Diagnostic.h"
20#include "clang/Lex/Preprocessor.h"
21
22using namespace clang;
23
24namespace {
25
26class DeadStoreAuditor : public LiveVariablesAuditor {
27 Preprocessor& PP;
28public:
29 DeadStoreAuditor(Preprocessor& pp) : PP(pp) {}
30 virtual ~DeadStoreAuditor() {}
31
32 virtual void AuditStmt(Stmt* S, LiveVariables& L, llvm::BitVector& Live) {
33 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
34 // Is this an assignment?
35 if (!B->isAssignmentOp())
36 return;
37
38 // Is this an assignment to a variable?
39 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS())) {
40 // Is the variable live?
41 if (!L.isLive(Live,DR->getDecl())) {
42 SourceRange R = B->getRHS()->getSourceRange();
43 PP.getDiagnostics().Report(DR->getSourceRange().Begin(),
44 diag::warn_dead_store, 0, 0,
45 &R,1);
46
47 }
48 }
49 }
50 }
51};
52
53} // end anonymous namespace
54
55namespace clang {
56
57void CheckDeadStores(CFG& cfg, LiveVariables& L, Preprocessor& PP) {
58 DeadStoreAuditor A(PP);
59
60 for (CFG::iterator I = cfg.begin(), E = cfg.end(); I != E; ++I)
61 L.runOnBlock(&(*I),&A);
62}
63
64void CheckDeadStores(CFG& cfg, Preprocessor& PP) {
65 LiveVariables L;
66 L.runOnCFG(cfg);
67 CheckDeadStores(cfg,L,PP);
68}
69
70} // end namespace clang