From 1ed6d2e4fda310af2de2ec09cdfedfa280cce948 Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Thu, 6 Sep 2007 23:01:46 +0000 Subject: [PATCH] Forgot to check in the actual "dead stores" checker in the last commit! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@41755 91177308-0d34-0410-b5e6-96231b3b80d8 --- Analysis/DeadStores.cpp | 70 ++++++++++++++++++++++++++ include/clang/Analysis/LocalCheckers.h | 29 +++++++++++ 2 files changed, 99 insertions(+) create mode 100644 Analysis/DeadStores.cpp create mode 100644 include/clang/Analysis/LocalCheckers.h diff --git a/Analysis/DeadStores.cpp b/Analysis/DeadStores.cpp new file mode 100644 index 0000000000..e7077ecae3 --- /dev/null +++ b/Analysis/DeadStores.cpp @@ -0,0 +1,70 @@ +//==- DeadStores.cpp - Check for stores to dead variables --------*- C++ -*-==// +// +// The LLVM Compiler Infrastructure +// +// This file was developed by Ted Kremenek and is distributed under +// the University of Illinois Open Source License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This files defines a DeadStores, a flow-sensitive checker that looks for +// stores to variables that are no longer live. +// +//===----------------------------------------------------------------------===// + +#include "clang/AST/Expr.h" +#include "clang/Analysis/LocalCheckers.h" +#include "clang/Analysis/LiveVariables.h" +#include "clang/AST/CFG.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Lex/Preprocessor.h" + +using namespace clang; + +namespace { + +class DeadStoreAuditor : public LiveVariablesAuditor { + Preprocessor& PP; +public: + DeadStoreAuditor(Preprocessor& pp) : PP(pp) {} + virtual ~DeadStoreAuditor() {} + + virtual void AuditStmt(Stmt* S, LiveVariables& L, llvm::BitVector& Live) { + if (BinaryOperator* B = dyn_cast(S)) { + // Is this an assignment? + if (!B->isAssignmentOp()) + return; + + // Is this an assignment to a variable? + if (DeclRefExpr* DR = dyn_cast(B->getLHS())) { + // Is the variable live? + if (!L.isLive(Live,DR->getDecl())) { + SourceRange R = B->getRHS()->getSourceRange(); + PP.getDiagnostics().Report(DR->getSourceRange().Begin(), + diag::warn_dead_store, 0, 0, + &R,1); + + } + } + } + } +}; + +} // end anonymous namespace + +namespace clang { + +void CheckDeadStores(CFG& cfg, LiveVariables& L, Preprocessor& PP) { + DeadStoreAuditor A(PP); + + for (CFG::iterator I = cfg.begin(), E = cfg.end(); I != E; ++I) + L.runOnBlock(&(*I),&A); +} + +void CheckDeadStores(CFG& cfg, Preprocessor& PP) { + LiveVariables L; + L.runOnCFG(cfg); + CheckDeadStores(cfg,L,PP); +} + +} // end namespace clang \ No newline at end of file diff --git a/include/clang/Analysis/LocalCheckers.h b/include/clang/Analysis/LocalCheckers.h new file mode 100644 index 0000000000..346945d6e8 --- /dev/null +++ b/include/clang/Analysis/LocalCheckers.h @@ -0,0 +1,29 @@ +//==- LocalCheckers.h - Intra-Procedural+Flow-Sensitive Checkers -*- C++ -*-==// +// +// The LLVM Compiler Infrastructure +// +// This file was developed by Ted Kremenek and is distributed under +// the University of Illinois Open Source License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines the interface to call a set of intra-procedural (local) +// checkers that use flow/path-sensitive analyses to find bugs. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_ANALYSIS_LOCALCHECKERS_H +#define LLVM_CLANG_ANALYSIS_LOCALCHECKERS_H + +namespace clang { + +class Preprocessor; +class CFG; +class LiveVariables; + +void CheckDeadStores(CFG& cfg, LiveVariables& L, Preprocessor& PP); +void CheckDeadStores(CFG& cfg, Preprocessor& PP); + +} // end namespace clang + +#endif -- 2.50.1