blob: 4073eccf60b75dbda081b171e16c8c6ddff680fa [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
Ted Kremenekb00c95e2007-09-10 15:56:38 +000026class DeadStoreObserver : public LiveVariablesObserver {
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000027 Preprocessor& PP;
28public:
Ted Kremenekb00c95e2007-09-10 15:56:38 +000029 DeadStoreObserver(Preprocessor& pp) : PP(pp) {}
30 virtual ~DeadStoreObserver() {}
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000031
Ted Kremenekb00c95e2007-09-10 15:56:38 +000032 virtual void ObserveStmt(Stmt* S, LiveVariables& L, llvm::BitVector& Live) {
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000033 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 }
Ted Kremenek83522a32007-09-06 23:39:53 +000050 else if(DeclStmt* DS = dyn_cast<DeclStmt>(S)) {
51 // Iterate through the decls. Warn if any of them (which have
52 // initializers) are not live.
53 for (Decl* D = DS->getDecl() ; D != NULL ; D = D->getNextDeclarator())
54 if (VarDecl* V = dyn_cast<VarDecl>(D))
55 if (Expr* E = V->getInit())
56 if (!L.isLive(Live,D)) {
57 SourceRange R = E->getSourceRange();
58 PP.getDiagnostics().Report(D->getLocation(),
59 diag::warn_dead_store, 0, 0,
60 &R,1);
61 }
62 }
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000063 }
64};
65
66} // end anonymous namespace
67
68namespace clang {
69
70void CheckDeadStores(CFG& cfg, LiveVariables& L, Preprocessor& PP) {
Ted Kremenekb00c95e2007-09-10 15:56:38 +000071 DeadStoreObserver A(PP);
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000072
73 for (CFG::iterator I = cfg.begin(), E = cfg.end(); I != E; ++I)
74 L.runOnBlock(&(*I),&A);
75}
76
77void CheckDeadStores(CFG& cfg, Preprocessor& PP) {
78 LiveVariables L;
79 L.runOnCFG(cfg);
80 CheckDeadStores(cfg,L,PP);
81}
82
83} // end namespace clang