blob: e547989f6b8bff12c05c98bf6ef9b5fb4cf0349f [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?
Ted Kremenekc0576ca2007-09-10 17:36:42 +000039 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()))
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000040 // Is the variable live?
Ted Kremenekc0576ca2007-09-10 17:36:42 +000041 if (!L.isLive(Live,cast<VarDecl>(DR->getDecl()))) {
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000042 SourceRange R = B->getRHS()->getSourceRange();
43 PP.getDiagnostics().Report(DR->getSourceRange().Begin(),
44 diag::warn_dead_store, 0, 0,
45 &R,1);
46
47 }
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000048 }
Ted Kremenek83522a32007-09-06 23:39:53 +000049 else if(DeclStmt* DS = dyn_cast<DeclStmt>(S)) {
50 // Iterate through the decls. Warn if any of them (which have
51 // initializers) are not live.
Ted Kremenekc0576ca2007-09-10 17:36:42 +000052 for (VarDecl* V = cast<VarDecl>(DS->getDecl()); V != NULL ;
Ted Kremeneka5965332007-09-11 17:00:40 +000053 V = cast_or_null<VarDecl>(V->getNextDeclarator()))
Ted Kremenekc0576ca2007-09-10 17:36:42 +000054 if (Expr* E = V->getInit())
55 if (!L.isLive(Live,V)) {
56 SourceRange R = E->getSourceRange();
57 PP.getDiagnostics().Report(V->getLocation(),
58 diag::warn_dead_store, 0, 0,
59 &R,1);
60 }
Ted Kremenek83522a32007-09-06 23:39:53 +000061 }
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000062 }
63};
64
65} // end anonymous namespace
66
67namespace clang {
68
69void CheckDeadStores(CFG& cfg, LiveVariables& L, Preprocessor& PP) {
Ted Kremenekb00c95e2007-09-10 15:56:38 +000070 DeadStoreObserver A(PP);
Ted Kremenek1ed6d2e2007-09-06 23:01:46 +000071
72 for (CFG::iterator I = cfg.begin(), E = cfg.end(); I != E; ++I)
73 L.runOnBlock(&(*I),&A);
74}
75
76void CheckDeadStores(CFG& cfg, Preprocessor& PP) {
77 LiveVariables L;
78 L.runOnCFG(cfg);
79 CheckDeadStores(cfg,L,PP);
80}
81
Gabor Greif84675832007-09-11 15:32:40 +000082} // end namespace clang