2011-01-14 04:58:56 +08:00
|
|
|
//==- DeadStoresChecker.cpp - Check for stores to dead variables -*- C++ -*-==//
|
2007-09-07 07:01:46 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-09-07 07:01:46 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-03-06 18:40:09 +08:00
|
|
|
// This file defines a DeadStores, a flow-sensitive checker that looks for
|
2007-09-07 07:01:46 +08:00
|
|
|
// stores to variables that are no longer live.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-18 05:39:33 +08:00
|
|
|
#include "ClangSACheckers.h"
|
2011-03-01 09:16:21 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
2007-12-22 05:42:19 +08:00
|
|
|
#include "clang/Analysis/Analyses/LiveVariables.h"
|
2007-09-25 12:31:27 +08:00
|
|
|
#include "clang/Analysis/Visitors/CFGRecStmtVisitor.h"
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
|
2009-04-07 13:25:24 +08:00
|
|
|
#include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h"
|
2007-09-07 07:01:46 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2007-09-12 01:24:14 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2008-06-21 05:45:25 +08:00
|
|
|
#include "clang/AST/ParentMap.h"
|
2009-04-07 13:25:24 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2012-02-04 21:45:25 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2007-09-07 07:01:46 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2007-09-07 07:01:46 +08:00
|
|
|
|
|
|
|
namespace {
|
2008-06-21 05:45:25 +08:00
|
|
|
|
2011-02-12 07:24:26 +08:00
|
|
|
// FIXME: Eventually migrate into its own file, and have it managed by
|
|
|
|
// AnalysisManager.
|
|
|
|
class ReachableCode {
|
|
|
|
const CFG &cfg;
|
|
|
|
llvm::BitVector reachable;
|
|
|
|
public:
|
|
|
|
ReachableCode(const CFG &cfg)
|
|
|
|
: cfg(cfg), reachable(cfg.getNumBlockIDs(), false) {}
|
|
|
|
|
|
|
|
void computeReachableBlocks();
|
|
|
|
|
|
|
|
bool isReachable(const CFGBlock *block) const {
|
|
|
|
return reachable[block->getBlockID()];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReachableCode::computeReachableBlocks() {
|
|
|
|
if (!cfg.getNumBlockIDs())
|
|
|
|
return;
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<const CFGBlock*, 10> worklist;
|
2011-02-12 07:24:26 +08:00
|
|
|
worklist.push_back(&cfg.getEntry());
|
|
|
|
|
|
|
|
while (!worklist.empty()) {
|
|
|
|
const CFGBlock *block = worklist.back();
|
|
|
|
worklist.pop_back();
|
|
|
|
llvm::BitVector::reference isReachable = reachable[block->getBlockID()];
|
|
|
|
if (isReachable)
|
|
|
|
continue;
|
|
|
|
isReachable = true;
|
|
|
|
for (CFGBlock::const_succ_iterator i = block->succ_begin(),
|
|
|
|
e = block->succ_end(); i != e; ++i)
|
|
|
|
if (const CFGBlock *succ = *i)
|
|
|
|
worklist.push_back(succ);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-05 03:58:03 +08:00
|
|
|
static const Expr *LookThroughTransitiveAssignments(const Expr *Ex) {
|
|
|
|
while (Ex) {
|
|
|
|
const BinaryOperator *BO =
|
|
|
|
dyn_cast<BinaryOperator>(Ex->IgnoreParenCasts());
|
|
|
|
if (!BO)
|
|
|
|
break;
|
|
|
|
if (BO->getOpcode() == BO_Assign) {
|
|
|
|
Ex = BO->getRHS();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return Ex;
|
|
|
|
}
|
|
|
|
|
2011-02-12 07:24:26 +08:00
|
|
|
namespace {
|
2011-07-29 07:07:59 +08:00
|
|
|
class DeadStoreObs : public LiveVariables::Observer {
|
2011-02-12 07:24:26 +08:00
|
|
|
const CFG &cfg;
|
2007-09-16 07:21:08 +08:00
|
|
|
ASTContext &Ctx;
|
2008-07-15 04:56:04 +08:00
|
|
|
BugReporter& BR;
|
2011-10-24 09:32:45 +08:00
|
|
|
AnalysisDeclContext* AC;
|
2008-06-21 05:45:25 +08:00
|
|
|
ParentMap& Parents;
|
2011-07-29 07:07:59 +08:00
|
|
|
llvm::SmallPtrSet<const VarDecl*, 20> Escaped;
|
2012-02-05 10:12:40 +08:00
|
|
|
OwningPtr<ReachableCode> reachableCode;
|
2011-02-12 07:24:26 +08:00
|
|
|
const CFGBlock *currentBlock;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-24 05:16:38 +08:00
|
|
|
enum DeadStoreKind { Standard, Enclosing, DeadIncrement, DeadInit };
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-07 07:01:46 +08:00
|
|
|
public:
|
2011-02-12 07:24:26 +08:00
|
|
|
DeadStoreObs(const CFG &cfg, ASTContext &ctx,
|
2011-10-24 09:32:45 +08:00
|
|
|
BugReporter& br, AnalysisDeclContext* ac, ParentMap& parents,
|
2011-07-29 07:07:59 +08:00
|
|
|
llvm::SmallPtrSet<const VarDecl*, 20> &escaped)
|
2011-09-21 05:38:35 +08:00
|
|
|
: cfg(cfg), Ctx(ctx), BR(br), AC(ac), Parents(parents),
|
2011-02-12 07:24:26 +08:00
|
|
|
Escaped(escaped), currentBlock(0) {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-25 12:31:27 +08:00
|
|
|
virtual ~DeadStoreObs() {}
|
2009-04-01 14:52:48 +08:00
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void Report(const VarDecl *V, DeadStoreKind dsk,
|
2011-09-21 05:38:35 +08:00
|
|
|
PathDiagnosticLocation L, SourceRange R) {
|
2009-04-07 13:25:24 +08:00
|
|
|
if (Escaped.count(V))
|
|
|
|
return;
|
2011-02-12 07:24:26 +08:00
|
|
|
|
|
|
|
// Compute reachable blocks within the CFG for trivial cases
|
|
|
|
// where a bogus dead store can be reported because itself is unreachable.
|
|
|
|
if (!reachableCode.get()) {
|
|
|
|
reachableCode.reset(new ReachableCode(cfg));
|
|
|
|
reachableCode->computeReachableBlocks();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!reachableCode->isReachable(currentBlock))
|
|
|
|
return;
|
2008-07-15 04:56:04 +08:00
|
|
|
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<64> buf;
|
2011-08-21 13:25:15 +08:00
|
|
|
llvm::raw_svector_ostream os(buf);
|
|
|
|
const char *BugType = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-24 05:16:38 +08:00
|
|
|
switch (dsk) {
|
|
|
|
case DeadInit:
|
2009-04-03 06:50:16 +08:00
|
|
|
BugType = "Dead initialization";
|
2011-10-15 02:45:37 +08:00
|
|
|
os << "Value stored to '" << *V
|
2011-08-21 13:25:15 +08:00
|
|
|
<< "' during its initialization is never read";
|
2008-07-24 05:16:38 +08:00
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-24 05:16:38 +08:00
|
|
|
case DeadIncrement:
|
2009-04-03 06:50:16 +08:00
|
|
|
BugType = "Dead increment";
|
2008-07-24 05:16:38 +08:00
|
|
|
case Standard:
|
2009-04-03 06:50:16 +08:00
|
|
|
if (!BugType) BugType = "Dead assignment";
|
2011-10-15 02:45:37 +08:00
|
|
|
os << "Value stored to '" << *V << "' is never read";
|
2008-07-24 05:16:38 +08:00
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-24 05:16:38 +08:00
|
|
|
case Enclosing:
|
2011-01-14 04:58:56 +08:00
|
|
|
// Don't report issues in this case, e.g.: "if (x = foo())",
|
|
|
|
// where 'x' is unused later. We have yet to see a case where
|
|
|
|
// this is a real bug.
|
|
|
|
return;
|
2008-07-16 02:06:32 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-04-05 02:11:35 +08:00
|
|
|
BR.EmitBasicReport(AC->getDecl(), BugType, "Dead store", os.str(), L, R);
|
2008-06-21 05:45:25 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void CheckVarDecl(const VarDecl *VD, const Expr *Ex, const Expr *Val,
|
2008-07-24 05:16:38 +08:00
|
|
|
DeadStoreKind dsk,
|
2011-07-29 07:07:59 +08:00
|
|
|
const LiveVariables::LivenessValues &Live) {
|
2008-06-21 05:45:25 +08:00
|
|
|
|
Add (initial?) static analyzer support for handling C++ references.
This change was a lot bigger than I originally anticipated; among
other things it requires us storing more information in the CFG to
record what block-level expressions need to be evaluated as lvalues.
The big change is that CFGBlocks no longer contain Stmt*'s by
CFGElements. Currently CFGElements just wrap Stmt*, but they also
store a bit indicating whether the block-level expression should be
evalauted as an lvalue. DeclStmts involving the initialization of a
reference require us treating the initialization expression as an
lvalue, even though that information isn't recorded in the AST.
Conceptually this change isn't that complicated, but it required
bubbling up the data through the CFGBuilder, to GRCoreEngine, and
eventually to GRExprEngine.
The addition of CFGElement is also useful for when we want to handle
more control-flow constructs or other data we want to keep in the CFG
that isn't represented well with just a block of statements.
In GRExprEngine, this patch introduces logic for evaluating the
lvalues of references, which currently retrieves the internal "pointer
value" that the reference represents. EvalLoad does a two stage load
to catch null dereferences involving an invalid reference (although
this could possibly be caught earlier during the initialization of a
reference).
Symbols are currently symbolicated using the reference type, instead
of a pointer type, and special handling is required creating
ElementRegions that layer on SymbolicRegions (see the changes to
RegionStoreManager).
Along the way, the DeadStoresChecker also silences warnings involving
dead stores to references. This was the original change I introduced
(which I wrote test cases for) that I realized caused GRExprEngine to
crash.
llvm-svn: 91501
2009-12-16 11:18:58 +08:00
|
|
|
if (!VD->hasLocalStorage())
|
|
|
|
return;
|
|
|
|
// Reference types confuse the dead stores checker. Skip them
|
|
|
|
// for now.
|
|
|
|
if (VD->getType()->getAs<ReferenceType>())
|
|
|
|
return;
|
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
if (!Live.isLive(VD) &&
|
2011-09-21 05:38:35 +08:00
|
|
|
!(VD->getAttr<UnusedAttr>() || VD->getAttr<BlocksAttr>())) {
|
|
|
|
|
|
|
|
PathDiagnosticLocation ExLoc =
|
|
|
|
PathDiagnosticLocation::createBegin(Ex, BR.getSourceManager(), AC);
|
|
|
|
Report(VD, dsk, ExLoc, Val->getSourceRange());
|
|
|
|
}
|
2008-05-22 06:59:16 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void CheckDeclRef(const DeclRefExpr *DR, const Expr *Val, DeadStoreKind dsk,
|
2011-07-29 07:07:59 +08:00
|
|
|
const LiveVariables::LivenessValues& Live) {
|
2011-08-13 07:37:29 +08:00
|
|
|
if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
|
2011-07-29 07:07:59 +08:00
|
|
|
CheckVarDecl(VD, DR, Val, dsk, Live);
|
2008-07-24 05:16:38 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
bool isIncrement(VarDecl *VD, const BinaryOperator* B) {
|
2008-07-24 05:16:38 +08:00
|
|
|
if (B->isCompoundAssignmentOp())
|
|
|
|
return true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
const Expr *RHS = B->getRHS()->IgnoreParenCasts();
|
2011-07-29 07:07:59 +08:00
|
|
|
const BinaryOperator* BRHS = dyn_cast<BinaryOperator>(RHS);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-24 05:16:38 +08:00
|
|
|
if (!BRHS)
|
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
const DeclRefExpr *DR;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-24 05:16:38 +08:00
|
|
|
if ((DR = dyn_cast<DeclRefExpr>(BRHS->getLHS()->IgnoreParenCasts())))
|
|
|
|
if (DR->getDecl() == VD)
|
|
|
|
return true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-24 05:16:38 +08:00
|
|
|
if ((DR = dyn_cast<DeclRefExpr>(BRHS->getRHS()->IgnoreParenCasts())))
|
|
|
|
if (DR->getDecl() == VD)
|
|
|
|
return true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-24 05:16:38 +08:00
|
|
|
return false;
|
2008-05-06 07:12:21 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
virtual void observeStmt(const Stmt *S, const CFGBlock *block,
|
2011-07-29 07:07:59 +08:00
|
|
|
const LiveVariables::LivenessValues &Live) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-12 07:24:26 +08:00
|
|
|
currentBlock = block;
|
|
|
|
|
2008-04-15 02:28:25 +08:00
|
|
|
// Skip statements in macros.
|
|
|
|
if (S->getLocStart().isMacroID())
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-12 08:17:19 +08:00
|
|
|
// Only cover dead stores from regular assignments. ++/-- dead stores
|
|
|
|
// have never flagged a real bug.
|
2011-07-29 07:07:59 +08:00
|
|
|
if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
|
2007-09-25 12:31:27 +08:00
|
|
|
if (!B->isAssignmentOp()) return; // Skip non-assignments.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(B->getLHS()))
|
2008-06-21 05:45:25 +08:00
|
|
|
if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
|
2008-08-09 08:05:14 +08:00
|
|
|
// Special case: check for assigning null to a pointer.
|
2009-09-09 23:08:12 +08:00
|
|
|
// This is a common form of defensive programming.
|
2012-04-05 03:58:03 +08:00
|
|
|
const Expr *RHS = LookThroughTransitiveAssignments(B->getRHS());
|
|
|
|
|
2010-02-24 05:19:33 +08:00
|
|
|
QualType T = VD->getType();
|
|
|
|
if (T->isPointerType() || T->isObjCObjectPointerType()) {
|
2012-04-05 03:58:03 +08:00
|
|
|
if (RHS->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNull))
|
2009-11-23 04:26:21 +08:00
|
|
|
return;
|
2008-08-09 08:05:14 +08:00
|
|
|
}
|
2009-11-23 04:26:21 +08:00
|
|
|
|
2012-04-05 03:58:03 +08:00
|
|
|
RHS = RHS->IgnoreParenCasts();
|
2009-01-10 06:15:01 +08:00
|
|
|
// Special case: self-assignments. These are often used to shut up
|
|
|
|
// "unused variable" compiler warnings.
|
2012-04-05 03:58:03 +08:00
|
|
|
if (const DeclRefExpr *RhsDR = dyn_cast<DeclRefExpr>(RHS))
|
2009-01-10 06:15:01 +08:00
|
|
|
if (VD == dyn_cast<VarDecl>(RhsDR->getDecl()))
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-10 06:15:01 +08:00
|
|
|
// Otherwise, issue a warning.
|
2009-04-01 14:52:48 +08:00
|
|
|
DeadStoreKind dsk = Parents.isConsumedExpr(B)
|
2009-09-09 23:08:12 +08:00
|
|
|
? Enclosing
|
2009-01-20 08:47:45 +08:00
|
|
|
: (isIncrement(VD,B) ? DeadIncrement : Standard);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
CheckVarDecl(VD, DR, B->getRHS(), dsk, Live);
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2007-09-07 07:01:46 +08:00
|
|
|
}
|
2011-07-29 07:07:59 +08:00
|
|
|
else if (const UnaryOperator* U = dyn_cast<UnaryOperator>(S)) {
|
2011-02-12 08:17:19 +08:00
|
|
|
if (!U->isIncrementOp() || U->isPrefix())
|
2008-05-06 07:12:21 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
const Stmt *parent = Parents.getParentIgnoreParenCasts(U);
|
2011-02-12 08:17:19 +08:00
|
|
|
if (!parent || !isa<ReturnStmt>(parent))
|
2008-10-15 13:23:41 +08:00
|
|
|
return;
|
2008-07-25 01:01:17 +08:00
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
const Expr *Ex = U->getSubExpr()->IgnoreParenCasts();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex))
|
2011-07-29 07:07:59 +08:00
|
|
|
CheckDeclRef(DR, U, DeadIncrement, Live);
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2011-08-13 07:37:29 +08:00
|
|
|
else if (const DeclStmt *DS = dyn_cast<DeclStmt>(S))
|
2007-09-25 12:31:27 +08:00
|
|
|
// Iterate through the decls. Warn if any initializers are complex
|
|
|
|
// expressions that are not live (never used).
|
2011-07-29 07:07:59 +08:00
|
|
|
for (DeclStmt::const_decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
|
2008-08-06 04:46:55 +08:00
|
|
|
DI != DE; ++DI) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
VarDecl *V = dyn_cast<VarDecl>(*DI);
|
2008-07-25 12:47:34 +08:00
|
|
|
|
|
|
|
if (!V)
|
|
|
|
continue;
|
Add (initial?) static analyzer support for handling C++ references.
This change was a lot bigger than I originally anticipated; among
other things it requires us storing more information in the CFG to
record what block-level expressions need to be evaluated as lvalues.
The big change is that CFGBlocks no longer contain Stmt*'s by
CFGElements. Currently CFGElements just wrap Stmt*, but they also
store a bit indicating whether the block-level expression should be
evalauted as an lvalue. DeclStmts involving the initialization of a
reference require us treating the initialization expression as an
lvalue, even though that information isn't recorded in the AST.
Conceptually this change isn't that complicated, but it required
bubbling up the data through the CFGBuilder, to GRCoreEngine, and
eventually to GRExprEngine.
The addition of CFGElement is also useful for when we want to handle
more control-flow constructs or other data we want to keep in the CFG
that isn't represented well with just a block of statements.
In GRExprEngine, this patch introduces logic for evaluating the
lvalues of references, which currently retrieves the internal "pointer
value" that the reference represents. EvalLoad does a two stage load
to catch null dereferences involving an invalid reference (although
this could possibly be caught earlier during the initialization of a
reference).
Symbols are currently symbolicated using the reference type, instead
of a pointer type, and special handling is required creating
ElementRegions that layer on SymbolicRegions (see the changes to
RegionStoreManager).
Along the way, the DeadStoresChecker also silences warnings involving
dead stores to references. This was the original change I introduced
(which I wrote test cases for) that I realized caused GRExprEngine to
crash.
llvm-svn: 91501
2009-12-16 11:18:58 +08:00
|
|
|
|
|
|
|
if (V->hasLocalStorage()) {
|
|
|
|
// Reference types confuse the dead stores checker. Skip them
|
|
|
|
// for now.
|
|
|
|
if (V->getType()->getAs<ReferenceType>())
|
|
|
|
return;
|
|
|
|
|
2012-04-05 03:58:03 +08:00
|
|
|
if (const Expr *E = V->getInit()) {
|
|
|
|
while (const ExprWithCleanups *exprClean =
|
|
|
|
dyn_cast<ExprWithCleanups>(E))
|
2011-06-16 07:02:42 +08:00
|
|
|
E = exprClean->getSubExpr();
|
|
|
|
|
2012-04-05 03:58:03 +08:00
|
|
|
// Look through transitive assignments, e.g.:
|
|
|
|
// int x = y = 0;
|
|
|
|
E = LookThroughTransitiveAssignments(E);
|
|
|
|
|
2009-12-15 12:12:12 +08:00
|
|
|
// Don't warn on C++ objects (yet) until we can show that their
|
|
|
|
// constructors/destructors don't have side effects.
|
|
|
|
if (isa<CXXConstructExpr>(E))
|
|
|
|
return;
|
2009-12-23 12:11:44 +08:00
|
|
|
|
2008-07-25 12:47:34 +08:00
|
|
|
// A dead initialization is a variable that is dead after it
|
|
|
|
// is initialized. We don't flag warnings for those variables
|
|
|
|
// marked 'unused'.
|
2011-07-29 07:07:59 +08:00
|
|
|
if (!Live.isLive(V) && V->getAttr<UnusedAttr>() == 0) {
|
2007-09-29 04:48:41 +08:00
|
|
|
// Special case: check for initializations with constants.
|
|
|
|
//
|
|
|
|
// e.g. : int x = 0;
|
|
|
|
//
|
|
|
|
// If x is EVER assigned a new value later, don't issue
|
|
|
|
// a warning. This is because such initialization can be
|
|
|
|
// due to defensive programming.
|
2011-12-07 07:25:15 +08:00
|
|
|
if (E->isEvaluatable(Ctx))
|
2009-02-10 02:01:00 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-04-05 03:58:03 +08:00
|
|
|
if (const DeclRefExpr *DRE =
|
|
|
|
dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()))
|
|
|
|
if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
|
2010-03-18 09:22:39 +08:00
|
|
|
// Special case: check for initialization from constant
|
|
|
|
// variables.
|
|
|
|
//
|
|
|
|
// e.g. extern const int MyConstant;
|
|
|
|
// int x = MyConstant;
|
|
|
|
//
|
2009-02-10 02:01:00 +08:00
|
|
|
if (VD->hasGlobalStorage() &&
|
2010-03-18 09:22:39 +08:00
|
|
|
VD->getType().isConstQualified())
|
|
|
|
return;
|
|
|
|
// Special case: check for initialization from scalar
|
|
|
|
// parameters. This is often a form of defensive
|
|
|
|
// programming. Non-scalars are still an error since
|
|
|
|
// because it more likely represents an actual algorithmic
|
|
|
|
// bug.
|
|
|
|
if (isa<ParmVarDecl>(VD) && VD->getType()->isScalarType())
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-09-21 05:38:35 +08:00
|
|
|
PathDiagnosticLocation Loc =
|
|
|
|
PathDiagnosticLocation::create(V, BR.getSourceManager());
|
|
|
|
Report(V, DeadInit, Loc, E->getSourceRange());
|
2007-09-12 01:24:14 +08:00
|
|
|
}
|
2008-07-25 12:47:34 +08:00
|
|
|
}
|
Add (initial?) static analyzer support for handling C++ references.
This change was a lot bigger than I originally anticipated; among
other things it requires us storing more information in the CFG to
record what block-level expressions need to be evaluated as lvalues.
The big change is that CFGBlocks no longer contain Stmt*'s by
CFGElements. Currently CFGElements just wrap Stmt*, but they also
store a bit indicating whether the block-level expression should be
evalauted as an lvalue. DeclStmts involving the initialization of a
reference require us treating the initialization expression as an
lvalue, even though that information isn't recorded in the AST.
Conceptually this change isn't that complicated, but it required
bubbling up the data through the CFGBuilder, to GRCoreEngine, and
eventually to GRExprEngine.
The addition of CFGElement is also useful for when we want to handle
more control-flow constructs or other data we want to keep in the CFG
that isn't represented well with just a block of statements.
In GRExprEngine, this patch introduces logic for evaluating the
lvalues of references, which currently retrieves the internal "pointer
value" that the reference represents. EvalLoad does a two stage load
to catch null dereferences involving an invalid reference (although
this could possibly be caught earlier during the initialization of a
reference).
Symbols are currently symbolicated using the reference type, instead
of a pointer type, and special handling is required creating
ElementRegions that layer on SymbolicRegions (see the changes to
RegionStoreManager).
Along the way, the DeadStoresChecker also silences warnings involving
dead stores to references. This was the original change I introduced
(which I wrote test cases for) that I realized caused GRExprEngine to
crash.
llvm-svn: 91501
2009-12-16 11:18:58 +08:00
|
|
|
}
|
2007-09-25 12:31:27 +08:00
|
|
|
}
|
2007-09-07 07:01:46 +08:00
|
|
|
}
|
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-07 07:01:46 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2008-07-03 07:16:33 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Driver function to invoke the Dead-Stores checker on a CFG.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-04-07 13:25:24 +08:00
|
|
|
namespace {
|
2009-11-28 14:07:30 +08:00
|
|
|
class FindEscaped : public CFGRecStmtDeclVisitor<FindEscaped>{
|
2009-04-07 13:25:24 +08:00
|
|
|
CFG *cfg;
|
|
|
|
public:
|
|
|
|
FindEscaped(CFG *c) : cfg(c) {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-07 13:25:24 +08:00
|
|
|
CFG& getCFG() { return *cfg; }
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
llvm::SmallPtrSet<const VarDecl*, 20> Escaped;
|
2009-04-07 13:25:24 +08:00
|
|
|
|
|
|
|
void VisitUnaryOperator(UnaryOperator* U) {
|
|
|
|
// Check for '&'. Any VarDecl whose value has its address-taken we
|
|
|
|
// treat as escaped.
|
2011-08-13 07:37:29 +08:00
|
|
|
Expr *E = U->getSubExpr()->IgnoreParenCasts();
|
2010-08-25 19:45:40 +08:00
|
|
|
if (U->getOpcode() == UO_AddrOf)
|
2011-08-13 07:37:29 +08:00
|
|
|
if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E))
|
|
|
|
if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
|
2009-04-07 13:25:24 +08:00
|
|
|
Escaped.insert(VD);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Visit(E);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-07 13:25:24 +08:00
|
|
|
|
2011-02-18 05:39:33 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// DeadStoresChecker
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2011-03-01 09:16:21 +08:00
|
|
|
class DeadStoresChecker : public Checker<check::ASTCodeBody> {
|
2011-02-18 05:39:33 +08:00
|
|
|
public:
|
|
|
|
void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
|
|
|
|
BugReporter &BR) const {
|
2011-10-08 06:21:02 +08:00
|
|
|
if (LiveVariables *L = mgr.getAnalysis<LiveVariables>(D)) {
|
2011-02-18 05:39:33 +08:00
|
|
|
CFG &cfg = *mgr.getCFG(D);
|
2011-10-24 09:32:45 +08:00
|
|
|
AnalysisDeclContext *AC = mgr.getAnalysisDeclContext(D);
|
2011-02-18 05:39:33 +08:00
|
|
|
ParentMap &pmap = mgr.getParentMap(D);
|
|
|
|
FindEscaped FS(&cfg);
|
|
|
|
FS.getCFG().VisitBlockStmts(FS);
|
2011-09-21 05:38:35 +08:00
|
|
|
DeadStoreObs A(cfg, BR.getContext(), BR, AC, pmap, FS.Escaped);
|
2011-07-29 07:07:59 +08:00
|
|
|
L->runOnAllBlocks(A);
|
2011-02-18 05:39:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void ento::registerDeadStoresChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<DeadStoresChecker>();
|
2008-04-15 01:39:48 +08:00
|
|
|
}
|