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
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-09-07 04:55:29 +08:00
|
|
|
#include "clang/Lex/Lexer.h"
|
[analyzer][NFC] Move CheckerRegistry from the Core directory to Frontend
ClangCheckerRegistry is a very non-obvious, poorly documented, weird concept.
It derives from CheckerRegistry, and is placed in lib/StaticAnalyzer/Frontend,
whereas it's base is located in lib/StaticAnalyzer/Core. It was, from what I can
imagine, used to circumvent the problem that the registry functions of the
checkers are located in the clangStaticAnalyzerCheckers library, but that
library depends on clangStaticAnalyzerCore. However, clangStaticAnalyzerFrontend
depends on both of those libraries.
One can make the observation however, that CheckerRegistry has no place in Core,
it isn't used there at all! The only place where it is used is Frontend, which
is where it ultimately belongs.
This move implies that since
include/clang/StaticAnalyzer/Checkers/ClangCheckers.h only contained a single function:
class CheckerRegistry;
void registerBuiltinCheckers(CheckerRegistry ®istry);
it had to re purposed, as CheckerRegistry is no longer available to
clangStaticAnalyzerCheckers. It was renamed to BuiltinCheckerRegistration.h,
which actually describes it a lot better -- it does not contain the registration
functions for checkers, but only those generated by the tblgen files.
Differential Revision: https://reviews.llvm.org/D54436
llvm-svn: 349275
2018-12-16 00:23:51 +08:00
|
|
|
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
|
2007-09-12 01:24:14 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2012-12-01 23:09:41 +08:00
|
|
|
#include "clang/AST/Attr.h"
|
2008-06-21 05:45:25 +08:00
|
|
|
#include "clang/AST/ParentMap.h"
|
2012-09-07 06:32:48 +08:00
|
|
|
#include "clang/AST/RecursiveASTVisitor.h"
|
2012-10-30 12:43:51 +08:00
|
|
|
#include "clang/Analysis/Analyses/LiveVariables.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
|
|
|
|
#include "llvm/ADT/BitVector.h"
|
2012-02-04 21:45:25 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2012-09-07 06:32:48 +08:00
|
|
|
#include "llvm/Support/SaveAndRestore.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
|
|
|
|
2015-09-08 11:50:52 +08:00
|
|
|
namespace {
|
|
|
|
|
2012-09-07 06:32:48 +08:00
|
|
|
/// A simple visitor to record what VarDecls occur in EH-handling code.
|
|
|
|
class EHCodeVisitor : public RecursiveASTVisitor<EHCodeVisitor> {
|
|
|
|
public:
|
|
|
|
bool inEH;
|
|
|
|
llvm::DenseSet<const VarDecl *> &S;
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-09-07 06:32:48 +08:00
|
|
|
bool TraverseObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
|
|
|
|
SaveAndRestore<bool> inFinally(inEH, true);
|
|
|
|
return ::RecursiveASTVisitor<EHCodeVisitor>::TraverseObjCAtFinallyStmt(S);
|
|
|
|
}
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-09-07 06:32:48 +08:00
|
|
|
bool TraverseObjCAtCatchStmt(ObjCAtCatchStmt *S) {
|
|
|
|
SaveAndRestore<bool> inCatch(inEH, true);
|
|
|
|
return ::RecursiveASTVisitor<EHCodeVisitor>::TraverseObjCAtCatchStmt(S);
|
|
|
|
}
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-09-07 06:32:48 +08:00
|
|
|
bool TraverseCXXCatchStmt(CXXCatchStmt *S) {
|
|
|
|
SaveAndRestore<bool> inCatch(inEH, true);
|
|
|
|
return TraverseStmt(S->getHandlerBlock());
|
|
|
|
}
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-09-07 06:32:48 +08:00
|
|
|
bool VisitDeclRefExpr(DeclRefExpr *DR) {
|
|
|
|
if (inEH)
|
|
|
|
if (const VarDecl *D = dyn_cast<VarDecl>(DR->getDecl()))
|
|
|
|
S.insert(D);
|
|
|
|
return true;
|
|
|
|
}
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-09-07 06:32:48 +08:00
|
|
|
EHCodeVisitor(llvm::DenseSet<const VarDecl *> &S) :
|
|
|
|
inEH(false), S(S) {}
|
|
|
|
};
|
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) {}
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2011-02-12 07:24:26 +08:00
|
|
|
void computeReachableBlocks();
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2011-02-12 07:24:26 +08:00
|
|
|
bool isReachable(const CFGBlock *block) const {
|
|
|
|
return reachable[block->getBlockID()];
|
|
|
|
}
|
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2011-02-12 07:24:26 +08:00
|
|
|
|
|
|
|
void ReachableCode::computeReachableBlocks() {
|
|
|
|
if (!cfg.getNumBlockIDs())
|
|
|
|
return;
|
2015-09-08 11:50:52 +08:00
|
|
|
|
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());
|
2013-08-24 00:11:15 +08:00
|
|
|
|
2011-02-12 07:24:26 +08:00
|
|
|
while (!worklist.empty()) {
|
2013-08-24 00:11:15 +08:00
|
|
|
const CFGBlock *block = worklist.pop_back_val();
|
2011-02-12 07:24:26 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-26 05:52:35 +08:00
|
|
|
static const Expr *
|
|
|
|
LookThroughTransitiveAssignmentsAndCommaOperators(const Expr *Ex) {
|
2012-04-05 03:58:03 +08:00
|
|
|
while (Ex) {
|
|
|
|
const BinaryOperator *BO =
|
|
|
|
dyn_cast<BinaryOperator>(Ex->IgnoreParenCasts());
|
|
|
|
if (!BO)
|
|
|
|
break;
|
|
|
|
if (BO->getOpcode() == BO_Assign) {
|
|
|
|
Ex = BO->getRHS();
|
|
|
|
continue;
|
|
|
|
}
|
2013-04-26 05:52:35 +08:00
|
|
|
if (BO->getOpcode() == BO_Comma) {
|
|
|
|
Ex = BO->getRHS();
|
|
|
|
continue;
|
|
|
|
}
|
2012-04-05 03:58:03 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return Ex;
|
|
|
|
}
|
|
|
|
|
2011-02-12 07:24:26 +08:00
|
|
|
namespace {
|
2019-09-07 04:55:29 +08:00
|
|
|
class DeadStoresChecker : public Checker<check::ASTCodeBody> {
|
|
|
|
public:
|
|
|
|
bool ShowFixIts = false;
|
|
|
|
bool WarnForDeadNestedAssignments = true;
|
|
|
|
|
|
|
|
void checkASTCodeBody(const Decl *D, AnalysisManager &Mgr,
|
|
|
|
BugReporter &BR) const;
|
|
|
|
};
|
|
|
|
|
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;
|
2019-09-07 04:55:29 +08:00
|
|
|
const DeadStoresChecker *Checker;
|
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;
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<ReachableCode> reachableCode;
|
2011-02-12 07:24:26 +08:00
|
|
|
const CFGBlock *currentBlock;
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<llvm::DenseSet<const VarDecl *>> InEH;
|
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:
|
2014-02-12 05:49:21 +08:00
|
|
|
DeadStoreObs(const CFG &cfg, ASTContext &ctx, BugReporter &br,
|
2019-09-07 04:55:29 +08:00
|
|
|
const DeadStoresChecker *checker, AnalysisDeclContext *ac,
|
2014-02-12 05:49:21 +08:00
|
|
|
ParentMap &parents,
|
2019-09-03 23:22:43 +08:00
|
|
|
llvm::SmallPtrSet<const VarDecl *, 20> &escaped,
|
|
|
|
bool warnForDeadNestedAssignments)
|
2014-02-12 05:49:21 +08:00
|
|
|
: cfg(cfg), Ctx(ctx), BR(br), Checker(checker), AC(ac), Parents(parents),
|
2019-09-07 04:55:29 +08:00
|
|
|
Escaped(escaped), currentBlock(nullptr) {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2015-10-20 21:23:58 +08:00
|
|
|
~DeadStoreObs() override {}
|
2009-04-01 14:52:48 +08:00
|
|
|
|
2012-09-07 06:32:48 +08:00
|
|
|
bool isLive(const LiveVariables::LivenessValues &Live, const VarDecl *D) {
|
|
|
|
if (Live.isLive(D))
|
|
|
|
return true;
|
|
|
|
// Lazily construct the set that records which VarDecls are in
|
|
|
|
// EH code.
|
|
|
|
if (!InEH.get()) {
|
|
|
|
InEH.reset(new llvm::DenseSet<const VarDecl *>());
|
|
|
|
EHCodeVisitor V(*InEH.get());
|
|
|
|
V.TraverseStmt(AC->getBody());
|
|
|
|
}
|
|
|
|
// Treat all VarDecls that occur in EH code as being "always live"
|
|
|
|
// when considering to suppress dead stores. Frequently stores
|
|
|
|
// are followed by reads in EH code, but we don't have the ability
|
|
|
|
// to analyze that yet.
|
|
|
|
return InEH->count(D);
|
|
|
|
}
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2019-06-20 07:33:39 +08:00
|
|
|
bool isSuppressed(SourceRange R) {
|
|
|
|
SourceManager &SMgr = Ctx.getSourceManager();
|
|
|
|
SourceLocation Loc = R.getBegin();
|
|
|
|
if (!Loc.isValid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
FileID FID = SMgr.getFileID(Loc);
|
|
|
|
bool Invalid = false;
|
|
|
|
StringRef Data = SMgr.getBufferData(FID, &Invalid);
|
|
|
|
if (Invalid)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Files autogenerated by DriverKit IIG contain some dead stores that
|
|
|
|
// we don't want to report.
|
2019-06-21 06:29:40 +08:00
|
|
|
if (Data.startswith("/* iig"))
|
2019-06-20 07:33:39 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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;
|
2015-09-08 11:50:52 +08:00
|
|
|
|
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();
|
|
|
|
}
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2011-02-12 07:24:26 +08:00
|
|
|
if (!reachableCode->isReachable(currentBlock))
|
|
|
|
return;
|
2008-07-15 04:56:04 +08:00
|
|
|
|
2019-06-20 07:33:39 +08:00
|
|
|
if (isSuppressed(R))
|
|
|
|
return;
|
|
|
|
|
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);
|
2014-05-27 10:45:47 +08:00
|
|
|
const char *BugType = nullptr;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2019-09-07 04:55:29 +08:00
|
|
|
SmallVector<FixItHint, 1> Fixits;
|
|
|
|
|
2008-07-24 05:16:38 +08:00
|
|
|
switch (dsk) {
|
2019-09-07 04:55:29 +08:00
|
|
|
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";
|
2019-09-07 04:55:29 +08:00
|
|
|
|
|
|
|
ASTContext &ACtx = V->getASTContext();
|
|
|
|
if (Checker->ShowFixIts) {
|
|
|
|
if (V->getInit()->HasSideEffects(ACtx,
|
|
|
|
/*IncludePossibleEffects=*/true)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
SourceManager &SM = ACtx.getSourceManager();
|
|
|
|
const LangOptions &LO = ACtx.getLangOpts();
|
|
|
|
SourceLocation L1 =
|
|
|
|
Lexer::findNextToken(
|
|
|
|
V->getTypeSourceInfo()->getTypeLoc().getEndLoc(),
|
|
|
|
SM, LO)->getEndLoc();
|
|
|
|
SourceLocation L2 =
|
|
|
|
Lexer::getLocForEndOfToken(V->getInit()->getEndLoc(), 1, SM, LO);
|
|
|
|
Fixits.push_back(FixItHint::CreateRemoval({L1, L2}));
|
|
|
|
}
|
2008-07-24 05:16:38 +08:00
|
|
|
break;
|
2019-09-07 04:55:29 +08:00
|
|
|
}
|
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";
|
2017-06-03 14:26:27 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
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
|
|
|
|
2019-09-03 23:22:43 +08:00
|
|
|
// eg.: f((x = foo()))
|
2008-07-24 05:16:38 +08:00
|
|
|
case Enclosing:
|
2019-09-07 04:55:29 +08:00
|
|
|
if (!Checker->WarnForDeadNestedAssignments)
|
2019-09-03 23:22:43 +08:00
|
|
|
return;
|
|
|
|
BugType = "Dead nested assignment";
|
|
|
|
os << "Although the value stored to '" << *V
|
|
|
|
<< "' is used in the enclosing expression, the value is never "
|
|
|
|
"actually read from '"
|
|
|
|
<< *V << "'";
|
|
|
|
break;
|
2008-07-16 02:06:32 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-02-12 05:49:21 +08:00
|
|
|
BR.EmitBasicReport(AC->getDecl(), Checker, BugType, "Dead store", os.str(),
|
2019-09-07 04:55:29 +08:00
|
|
|
L, R, Fixits);
|
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;
|
|
|
|
|
2012-09-07 06:32:48 +08:00
|
|
|
if (!isLive(Live, VD) &&
|
2014-01-15 08:59:23 +08:00
|
|
|
!(VD->hasAttr<UnusedAttr>() || VD->hasAttr<BlocksAttr>() ||
|
|
|
|
VD->hasAttr<ObjCPreciseLifetimeAttr>())) {
|
2011-09-21 05:38:35 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
2014-03-15 12:29:04 +08:00
|
|
|
void observeStmt(const Stmt *S, const CFGBlock *block,
|
|
|
|
const LiveVariables::LivenessValues &Live) override {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-12 07:24:26 +08:00
|
|
|
currentBlock = block;
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2008-04-15 02:28:25 +08:00
|
|
|
// Skip statements in macros.
|
2018-08-10 05:08:08 +08:00
|
|
|
if (S->getBeginLoc().isMacroID())
|
2008-04-15 02:28:25 +08:00
|
|
|
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.
|
2013-04-26 05:52:35 +08:00
|
|
|
const Expr *RHS =
|
|
|
|
LookThroughTransitiveAssignmentsAndCommaOperators(B->getRHS());
|
|
|
|
RHS = RHS->IgnoreParenCasts();
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2010-02-24 05:19:33 +08:00
|
|
|
QualType T = VD->getType();
|
2016-06-25 00:26:43 +08:00
|
|
|
if (T.isVolatileQualified())
|
|
|
|
return;
|
2010-02-24 05:19:33 +08:00
|
|
|
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
|
|
|
|
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).
|
2014-03-15 01:01:24 +08:00
|
|
|
for (const auto *DI : DS->decls()) {
|
|
|
|
const auto *V = dyn_cast<VarDecl>(DI);
|
2008-07-25 12:47:34 +08:00
|
|
|
|
|
|
|
if (!V)
|
|
|
|
continue;
|
2015-09-08 11:50:52 +08:00
|
|
|
|
|
|
|
if (V->hasLocalStorage()) {
|
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
|
|
|
// Reference types confuse the dead stores checker. Skip them
|
|
|
|
// for now.
|
|
|
|
if (V->getType()->getAs<ReferenceType>())
|
|
|
|
return;
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-04-05 03:58:03 +08:00
|
|
|
if (const Expr *E = V->getInit()) {
|
2018-10-31 11:48:47 +08:00
|
|
|
while (const FullExpr *FE = dyn_cast<FullExpr>(E))
|
|
|
|
E = FE->getSubExpr();
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-04-05 03:58:03 +08:00
|
|
|
// Look through transitive assignments, e.g.:
|
|
|
|
// int x = y = 0;
|
2013-04-26 05:52:35 +08:00
|
|
|
E = LookThroughTransitiveAssignmentsAndCommaOperators(E);
|
2015-09-08 11:50:52 +08:00
|
|
|
|
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;
|
2015-09-08 11:50:52 +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
|
2014-01-15 08:59:23 +08:00
|
|
|
// marked 'unused' or 'objc_precise_lifetime'.
|
|
|
|
if (!isLive(Live, V) &&
|
|
|
|
!V->hasAttr<UnusedAttr>() &&
|
|
|
|
!V->hasAttr<ObjCPreciseLifetimeAttr>()) {
|
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 {
|
2013-05-16 07:22:55 +08:00
|
|
|
class FindEscaped {
|
2009-04-07 13:25:24 +08:00
|
|
|
public:
|
2011-07-29 07:07:59 +08:00
|
|
|
llvm::SmallPtrSet<const VarDecl*, 20> Escaped;
|
2009-04-07 13:25:24 +08:00
|
|
|
|
2013-05-16 07:22:55 +08:00
|
|
|
void operator()(const Stmt *S) {
|
|
|
|
// Check for '&'. Any VarDecl whose address has been taken we treat as
|
|
|
|
// escaped.
|
|
|
|
// FIXME: What about references?
|
2015-11-20 09:53:44 +08:00
|
|
|
if (auto *LE = dyn_cast<LambdaExpr>(S)) {
|
|
|
|
findLambdaReferenceCaptures(LE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-16 07:22:55 +08:00
|
|
|
const UnaryOperator *U = dyn_cast<UnaryOperator>(S);
|
|
|
|
if (!U)
|
|
|
|
return;
|
|
|
|
if (U->getOpcode() != UO_AddrOf)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const Expr *E = U->getSubExpr()->IgnoreParenCasts();
|
|
|
|
if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E))
|
|
|
|
if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
|
|
|
|
Escaped.insert(VD);
|
2009-04-07 13:25:24 +08:00
|
|
|
}
|
2015-11-20 09:53:44 +08:00
|
|
|
|
|
|
|
// Treat local variables captured by reference in C++ lambdas as escaped.
|
|
|
|
void findLambdaReferenceCaptures(const LambdaExpr *LE) {
|
|
|
|
const CXXRecordDecl *LambdaClass = LE->getLambdaClass();
|
|
|
|
llvm::DenseMap<const VarDecl *, FieldDecl *> CaptureFields;
|
|
|
|
FieldDecl *ThisCaptureField;
|
|
|
|
LambdaClass->getCaptureFields(CaptureFields, ThisCaptureField);
|
|
|
|
|
|
|
|
for (const LambdaCapture &C : LE->captures()) {
|
|
|
|
if (!C.capturesVariable())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
VarDecl *VD = C.getCapturedVar();
|
|
|
|
const FieldDecl *FD = CaptureFields[VD];
|
|
|
|
if (!FD)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// If the capture field is a reference type, it is capture-by-reference.
|
|
|
|
if (FD->getType()->isReferenceType())
|
|
|
|
Escaped.insert(VD);
|
|
|
|
}
|
|
|
|
}
|
2009-04-07 13:25:24 +08:00
|
|
|
};
|
|
|
|
} // 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
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-09-07 04:55:29 +08:00
|
|
|
void DeadStoresChecker::checkASTCodeBody(const Decl *D, AnalysisManager &mgr,
|
|
|
|
BugReporter &BR) const {
|
2013-02-18 15:18:28 +08:00
|
|
|
|
2019-09-07 04:55:29 +08:00
|
|
|
// Don't do anything for template instantiations.
|
|
|
|
// Proving that code in a template instantiation is "dead"
|
|
|
|
// means proving that it is dead in all instantiations.
|
|
|
|
// This same problem exists with -Wunreachable-code.
|
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
|
|
|
|
if (FD->isTemplateInstantiation())
|
|
|
|
return;
|
2013-02-18 15:18:28 +08:00
|
|
|
|
2019-09-07 04:55:29 +08:00
|
|
|
if (LiveVariables *L = mgr.getAnalysis<LiveVariables>(D)) {
|
|
|
|
CFG &cfg = *mgr.getCFG(D);
|
|
|
|
AnalysisDeclContext *AC = mgr.getAnalysisDeclContext(D);
|
|
|
|
ParentMap &pmap = mgr.getParentMap(D);
|
|
|
|
FindEscaped FS;
|
|
|
|
cfg.VisitBlockStmts(FS);
|
|
|
|
DeadStoreObs A(cfg, BR.getContext(), BR, this, AC, pmap, FS.Escaped,
|
|
|
|
WarnForDeadNestedAssignments);
|
|
|
|
L->runOnAllBlocks(A);
|
2011-02-18 05:39:33 +08:00
|
|
|
}
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2011-02-18 05:39:33 +08:00
|
|
|
|
2019-09-03 23:22:43 +08:00
|
|
|
void ento::registerDeadStoresChecker(CheckerManager &Mgr) {
|
2019-09-07 04:55:29 +08:00
|
|
|
auto *Chk = Mgr.registerChecker<DeadStoresChecker>();
|
2019-09-03 23:22:43 +08:00
|
|
|
|
|
|
|
const AnalyzerOptions &AnOpts = Mgr.getAnalyzerOptions();
|
|
|
|
Chk->WarnForDeadNestedAssignments =
|
|
|
|
AnOpts.getCheckerBooleanOption(Chk, "WarnForDeadNestedAssignments");
|
2019-09-07 04:55:29 +08:00
|
|
|
Chk->ShowFixIts =
|
|
|
|
AnOpts.getCheckerBooleanOption(Chk, "ShowFixIts");
|
2008-04-15 01:39:48 +08:00
|
|
|
}
|
2019-01-26 22:23:08 +08:00
|
|
|
|
2020-03-27 21:29:31 +08:00
|
|
|
bool ento::shouldRegisterDeadStoresChecker(const CheckerManager &mgr) {
|
2019-01-26 22:23:08 +08:00
|
|
|
return true;
|
|
|
|
}
|