2010-07-24 07:04:53 +08:00
|
|
|
//==- UnreachableCodeChecker.cpp - Generalized dead code checker -*- C++ -*-==//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// This file implements a generalized unreachable code checker using a
|
|
|
|
// path-sensitive analysis. We mark any path visited, and then walk the CFG as a
|
|
|
|
// post-analysis to determine what was never visited.
|
|
|
|
//
|
2010-07-27 11:39:53 +08:00
|
|
|
// A similar flow-sensitive only check exists in Analysis/ReachableCode.cpp
|
2010-07-24 07:04:53 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-16 05:25:03 +08:00
|
|
|
#include "ClangSACheckers.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/ParentMap.h"
|
|
|
|
#include "clang/Basic/Builtins.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
|
2011-03-01 09:16:21 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
2011-02-18 05:39:17 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
2011-02-23 15:19:23 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h"
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
|
2012-04-02 03:30:51 +08:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2010-07-24 07:04:53 +08:00
|
|
|
|
|
|
|
// The number of CFGBlock pointers we want to reserve memory for. This is used
|
|
|
|
// once for each function we analyze.
|
|
|
|
#define DEFAULT_CFGBLOCKS 256
|
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2010-07-24 07:04:53 +08:00
|
|
|
|
|
|
|
namespace {
|
2011-03-01 09:16:21 +08:00
|
|
|
class UnreachableCodeChecker : public Checker<check::EndAnalysis> {
|
2010-07-24 07:04:53 +08:00
|
|
|
public:
|
2011-02-23 15:19:23 +08:00
|
|
|
void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,
|
|
|
|
ExprEngine &Eng) const;
|
2010-07-24 07:04:53 +08:00
|
|
|
private:
|
2011-02-23 15:19:23 +08:00
|
|
|
typedef llvm::SmallSet<unsigned, DEFAULT_CFGBLOCKS> CFGBlocksSet;
|
|
|
|
|
2010-08-04 05:24:13 +08:00
|
|
|
static inline const Stmt *getUnreachableStmt(const CFGBlock *CB);
|
2011-02-23 15:19:23 +08:00
|
|
|
static void FindUnreachableEntryPoints(const CFGBlock *CB,
|
|
|
|
CFGBlocksSet &reachable,
|
|
|
|
CFGBlocksSet &visited);
|
2010-07-28 07:30:21 +08:00
|
|
|
static bool isInvalidPath(const CFGBlock *CB, const ParentMap &PM);
|
2010-08-13 07:01:06 +08:00
|
|
|
static inline bool isEmptyCFGBlock(const CFGBlock *CB);
|
2010-07-24 07:04:53 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-02-23 15:19:23 +08:00
|
|
|
void UnreachableCodeChecker::checkEndAnalysis(ExplodedGraph &G,
|
2010-07-24 07:04:53 +08:00
|
|
|
BugReporter &B,
|
2011-02-23 15:19:23 +08:00
|
|
|
ExprEngine &Eng) const {
|
|
|
|
CFGBlocksSet reachable, visited;
|
2011-12-01 08:59:17 +08:00
|
|
|
|
2010-08-03 09:55:07 +08:00
|
|
|
if (Eng.hasWorkRemaining())
|
2010-07-24 07:04:53 +08:00
|
|
|
return;
|
|
|
|
|
2011-12-01 08:59:17 +08:00
|
|
|
const Decl *D = 0;
|
2010-07-24 07:04:53 +08:00
|
|
|
CFG *C = 0;
|
2010-07-28 07:30:21 +08:00
|
|
|
ParentMap *PM = 0;
|
2011-09-21 05:38:35 +08:00
|
|
|
const LocationContext *LC = 0;
|
2010-07-24 07:04:53 +08:00
|
|
|
// Iterate over ExplodedGraph
|
2010-08-13 07:01:06 +08:00
|
|
|
for (ExplodedGraph::node_iterator I = G.nodes_begin(), E = G.nodes_end();
|
|
|
|
I != E; ++I) {
|
2010-07-24 07:04:53 +08:00
|
|
|
const ProgramPoint &P = I->getLocation();
|
2011-09-21 05:38:35 +08:00
|
|
|
LC = P.getLocationContext();
|
2013-08-20 01:03:12 +08:00
|
|
|
if (!LC->inTopFrame())
|
|
|
|
continue;
|
2010-07-24 07:04:53 +08:00
|
|
|
|
2011-12-01 08:59:17 +08:00
|
|
|
if (!D)
|
|
|
|
D = LC->getAnalysisDeclContext()->getDecl();
|
2013-08-20 01:03:12 +08:00
|
|
|
|
2010-07-24 07:04:53 +08:00
|
|
|
// Save the CFG if we don't have it already
|
|
|
|
if (!C)
|
2011-10-24 09:32:45 +08:00
|
|
|
C = LC->getAnalysisDeclContext()->getUnoptimizedCFG();
|
2010-07-28 07:30:21 +08:00
|
|
|
if (!PM)
|
|
|
|
PM = &LC->getParentMap();
|
2010-07-24 07:04:53 +08:00
|
|
|
|
2013-02-22 06:23:56 +08:00
|
|
|
if (Optional<BlockEntrance> BE = P.getAs<BlockEntrance>()) {
|
2010-07-24 07:04:53 +08:00
|
|
|
const CFGBlock *CB = BE->getBlock();
|
2010-08-04 05:24:13 +08:00
|
|
|
reachable.insert(CB->getBlockID());
|
2010-07-24 07:04:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-28 07:30:21 +08:00
|
|
|
// Bail out if we didn't get the CFG or the ParentMap.
|
2011-12-01 08:59:17 +08:00
|
|
|
if (!D || !C || !PM)
|
2010-07-24 07:04:53 +08:00
|
|
|
return;
|
2011-12-01 08:59:17 +08:00
|
|
|
|
|
|
|
// Don't do anything for template instantiations. Proving that code
|
|
|
|
// in a template instantiation is unreachable means proving that it is
|
|
|
|
// unreachable in all instantiations.
|
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
|
|
|
|
if (FD->isTemplateInstantiation())
|
|
|
|
return;
|
2010-07-24 07:04:53 +08:00
|
|
|
|
|
|
|
// Find CFGBlocks that were not covered by any node
|
2010-10-07 07:02:25 +08:00
|
|
|
for (CFG::const_iterator I = C->begin(), E = C->end(); I != E; ++I) {
|
2010-07-24 07:04:53 +08:00
|
|
|
const CFGBlock *CB = *I;
|
|
|
|
// Check if the block is unreachable
|
2010-08-04 05:24:13 +08:00
|
|
|
if (reachable.count(CB->getBlockID()))
|
2010-07-28 07:30:21 +08:00
|
|
|
continue;
|
|
|
|
|
2010-08-13 07:01:06 +08:00
|
|
|
// Check if the block is empty (an artificial block)
|
|
|
|
if (isEmptyCFGBlock(CB))
|
|
|
|
continue;
|
|
|
|
|
2010-07-28 07:30:21 +08:00
|
|
|
// Find the entry points for this block
|
2010-10-07 07:02:25 +08:00
|
|
|
if (!visited.count(CB->getBlockID()))
|
2011-02-23 15:19:23 +08:00
|
|
|
FindUnreachableEntryPoints(CB, reachable, visited);
|
2010-07-28 07:30:21 +08:00
|
|
|
|
|
|
|
// This block may have been pruned; check if we still want to report it
|
2010-08-04 05:24:13 +08:00
|
|
|
if (reachable.count(CB->getBlockID()))
|
2010-07-28 07:30:21 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Check for false positives
|
|
|
|
if (CB->size() > 0 && isInvalidPath(CB, *PM))
|
|
|
|
continue;
|
|
|
|
|
2012-02-29 14:05:28 +08:00
|
|
|
// It is good practice to always have a "default" label in a "switch", even
|
|
|
|
// if we should never get there. It can be used to detect errors, for
|
|
|
|
// instance. Unreachable code directly under a "default" label is therefore
|
|
|
|
// likely to be a false positive.
|
|
|
|
if (const Stmt *label = CB->getLabel())
|
|
|
|
if (label->getStmtClass() == Stmt::DefaultStmtClass)
|
|
|
|
continue;
|
|
|
|
|
2010-07-28 07:30:21 +08:00
|
|
|
// Special case for __builtin_unreachable.
|
|
|
|
// FIXME: This should be extended to include other unreachable markers,
|
|
|
|
// such as llvm_unreachable.
|
|
|
|
if (!CB->empty()) {
|
2011-07-29 07:07:59 +08:00
|
|
|
bool foundUnreachable = false;
|
|
|
|
for (CFGBlock::const_iterator ci = CB->begin(), ce = CB->end();
|
|
|
|
ci != ce; ++ci) {
|
2013-02-23 08:29:34 +08:00
|
|
|
if (Optional<CFGStmt> S = (*ci).getAs<CFGStmt>())
|
|
|
|
if (const CallExpr *CE = dyn_cast<CallExpr>(S->getStmt())) {
|
2013-12-29 05:59:02 +08:00
|
|
|
if (CE->getBuiltinCallee() == Builtin::BI__builtin_unreachable) {
|
2011-07-29 07:07:59 +08:00
|
|
|
foundUnreachable = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-07-27 11:39:53 +08:00
|
|
|
}
|
2011-07-29 07:07:59 +08:00
|
|
|
if (foundUnreachable)
|
|
|
|
continue;
|
2010-07-24 07:04:53 +08:00
|
|
|
}
|
2010-07-28 07:30:21 +08:00
|
|
|
|
2010-08-04 05:24:13 +08:00
|
|
|
// We found a block that wasn't covered - find the statement to report
|
|
|
|
SourceRange SR;
|
2011-09-21 05:38:35 +08:00
|
|
|
PathDiagnosticLocation DL;
|
2010-08-04 05:24:13 +08:00
|
|
|
SourceLocation SL;
|
|
|
|
if (const Stmt *S = getUnreachableStmt(CB)) {
|
|
|
|
SR = S->getSourceRange();
|
2011-09-21 05:38:35 +08:00
|
|
|
DL = PathDiagnosticLocation::createBegin(S, B.getSourceManager(), LC);
|
|
|
|
SL = DL.asLocation();
|
|
|
|
if (SR.isInvalid() || !SL.isValid())
|
2010-08-04 05:24:13 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Check if the SourceLocation is in a system header
|
|
|
|
const SourceManager &SM = B.getSourceManager();
|
|
|
|
if (SM.isInSystemHeader(SL) || SM.isInExternCSystemHeader(SL))
|
|
|
|
continue;
|
|
|
|
|
2012-04-05 02:11:35 +08:00
|
|
|
B.EmitBasicReport(D, "Unreachable code", "Dead code",
|
|
|
|
"This statement is never executed", DL, SR);
|
2010-07-24 07:04:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recursively finds the entry point(s) for this dead CFGBlock.
|
2011-02-23 15:19:23 +08:00
|
|
|
void UnreachableCodeChecker::FindUnreachableEntryPoints(const CFGBlock *CB,
|
|
|
|
CFGBlocksSet &reachable,
|
|
|
|
CFGBlocksSet &visited) {
|
2010-08-04 05:24:13 +08:00
|
|
|
visited.insert(CB->getBlockID());
|
2010-07-24 07:04:53 +08:00
|
|
|
|
2010-10-07 07:02:25 +08:00
|
|
|
for (CFGBlock::const_pred_iterator I = CB->pred_begin(), E = CB->pred_end();
|
|
|
|
I != E; ++I) {
|
2010-08-06 01:53:44 +08:00
|
|
|
if (!reachable.count((*I)->getBlockID())) {
|
2010-10-07 07:02:25 +08:00
|
|
|
// If we find an unreachable predecessor, mark this block as reachable so
|
|
|
|
// we don't report this block
|
|
|
|
reachable.insert(CB->getBlockID());
|
2010-08-06 01:53:44 +08:00
|
|
|
if (!visited.count((*I)->getBlockID()))
|
2010-10-07 07:02:25 +08:00
|
|
|
// If we haven't previously visited the unreachable predecessor, recurse
|
2011-02-23 15:19:23 +08:00
|
|
|
FindUnreachableEntryPoints(*I, reachable, visited);
|
2010-07-24 07:04:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-04 05:24:13 +08:00
|
|
|
// Find the Stmt* in a CFGBlock for reporting a warning
|
|
|
|
const Stmt *UnreachableCodeChecker::getUnreachableStmt(const CFGBlock *CB) {
|
2010-09-16 09:25:47 +08:00
|
|
|
for (CFGBlock::const_iterator I = CB->begin(), E = CB->end(); I != E; ++I) {
|
2013-02-23 08:29:34 +08:00
|
|
|
if (Optional<CFGStmt> S = I->getAs<CFGStmt>())
|
|
|
|
return S->getStmt();
|
2010-09-16 09:25:47 +08:00
|
|
|
}
|
|
|
|
if (const Stmt *S = CB->getTerminator())
|
2010-08-04 05:24:13 +08:00
|
|
|
return S;
|
2010-07-24 07:04:53 +08:00
|
|
|
else
|
2010-08-04 05:24:13 +08:00
|
|
|
return 0;
|
2010-07-24 07:04:53 +08:00
|
|
|
}
|
2010-07-28 07:30:21 +08:00
|
|
|
|
2010-08-04 05:24:13 +08:00
|
|
|
// Determines if the path to this CFGBlock contained an element that infers this
|
|
|
|
// block is a false positive. We assume that FindUnreachableEntryPoints has
|
|
|
|
// already marked only the entry points to any dead code, so we need only to
|
|
|
|
// find the condition that led to this block (the predecessor of this block.)
|
|
|
|
// There will never be more than one predecessor.
|
2010-07-28 07:30:21 +08:00
|
|
|
bool UnreachableCodeChecker::isInvalidPath(const CFGBlock *CB,
|
|
|
|
const ParentMap &PM) {
|
2010-08-28 06:37:31 +08:00
|
|
|
// We only expect a predecessor size of 0 or 1. If it is >1, then an external
|
|
|
|
// condition has broken our assumption (for example, a sink being placed by
|
|
|
|
// another check). In these cases, we choose not to report.
|
|
|
|
if (CB->pred_size() > 1)
|
|
|
|
return true;
|
2010-07-28 07:30:21 +08:00
|
|
|
|
2010-08-04 05:24:13 +08:00
|
|
|
// If there are no predecessors, then this block is trivially unreachable
|
|
|
|
if (CB->pred_size() == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const CFGBlock *pred = *CB->pred_begin();
|
|
|
|
|
|
|
|
// Get the predecessor block's terminator conditon
|
|
|
|
const Stmt *cond = pred->getTerminatorCondition();
|
2010-08-13 07:01:06 +08:00
|
|
|
|
|
|
|
//assert(cond && "CFGBlock's predecessor has a terminator condition");
|
|
|
|
// The previous assertion is invalid in some cases (eg do/while). Leaving
|
|
|
|
// reporting of these situations on at the moment to help triage these cases.
|
|
|
|
if (!cond)
|
|
|
|
return false;
|
2010-08-04 05:24:13 +08:00
|
|
|
|
|
|
|
// Run each of the checks on the conditions
|
|
|
|
if (containsMacro(cond) || containsEnum(cond)
|
|
|
|
|| containsStaticLocal(cond) || containsBuiltinOffsetOf(cond)
|
2011-03-12 03:24:49 +08:00
|
|
|
|| containsStmt<UnaryExprOrTypeTraitExpr>(cond))
|
2010-08-04 05:24:13 +08:00
|
|
|
return true;
|
2010-07-28 07:30:21 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2010-08-13 07:01:06 +08:00
|
|
|
|
|
|
|
// Returns true if the given CFGBlock is empty
|
|
|
|
bool UnreachableCodeChecker::isEmptyCFGBlock(const CFGBlock *CB) {
|
|
|
|
return CB->getLabel() == 0 // No labels
|
|
|
|
&& CB->size() == 0 // No statements
|
|
|
|
&& CB->getTerminator() == 0; // No terminator
|
|
|
|
}
|
2011-02-23 15:19:23 +08:00
|
|
|
|
|
|
|
void ento::registerUnreachableCodeChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<UnreachableCodeChecker>();
|
|
|
|
}
|