2010-07-24 07:04:53 +08:00
|
|
|
//==- UnreachableCodeChecker.cpp - Generalized dead code checker -*- C++ -*-==//
|
|
|
|
//
|
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
|
2010-07-24 07:04:53 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// 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
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[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"
|
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
|
|
|
|
|
|
|
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:
|
2016-01-30 09:27:06 +08:00
|
|
|
typedef llvm::SmallSet<unsigned, 32> CFGBlocksSet;
|
2011-02-23 15:19:23 +08:00
|
|
|
|
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
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
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;
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2010-08-03 09:55:07 +08:00
|
|
|
if (Eng.hasWorkRemaining())
|
2010-07-24 07:04:53 +08:00
|
|
|
return;
|
|
|
|
|
2014-05-27 10:45:47 +08:00
|
|
|
const Decl *D = nullptr;
|
|
|
|
CFG *C = nullptr;
|
|
|
|
ParentMap *PM = nullptr;
|
|
|
|
const LocationContext *LC = nullptr;
|
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;
|
2015-09-08 11:50:52 +08:00
|
|
|
|
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
|
2017-08-02 16:26:56 +08:00
|
|
|
if (isInvalidPath(CB, *PM))
|
2010-07-28 07:30:21 +08:00
|
|
|
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())) {
|
2018-02-14 05:31:47 +08:00
|
|
|
if (CE->getBuiltinCallee() == Builtin::BI__builtin_unreachable ||
|
|
|
|
CE->isBuiltinAssumeFalse(Eng.getContext())) {
|
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)) {
|
2016-10-18 21:16:53 +08:00
|
|
|
// In macros, 'do {...} while (0)' is often used. Don't warn about the
|
|
|
|
// condition 0 when it is unreachable.
|
2018-08-10 05:08:08 +08:00
|
|
|
if (S->getBeginLoc().isMacroID())
|
2016-10-18 21:16:53 +08:00
|
|
|
if (const auto *I = dyn_cast<IntegerLiteral>(S))
|
|
|
|
if (I->getValue() == 0ULL)
|
|
|
|
if (const Stmt *Parent = PM->getParent(S))
|
|
|
|
if (isa<DoStmt>(Parent))
|
|
|
|
continue;
|
2010-08-04 05:24:13 +08:00
|
|
|
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;
|
|
|
|
|
2014-02-12 05:49:21 +08:00
|
|
|
B.EmitBasicReport(D, this, "Unreachable code", "Dead code",
|
2012-04-05 02:11:35 +08:00
|
|
|
"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) {
|
2014-02-28 05:56:41 +08:00
|
|
|
if (!*I)
|
|
|
|
continue;
|
|
|
|
|
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) {
|
2016-09-28 18:39:53 +08:00
|
|
|
if (Optional<CFGStmt> S = I->getAs<CFGStmt>()) {
|
|
|
|
if (!isa<DeclStmt>(S->getStmt()))
|
|
|
|
return S->getStmt();
|
|
|
|
}
|
2010-09-16 09:25:47 +08:00
|
|
|
}
|
2019-05-24 09:34:22 +08:00
|
|
|
if (const Stmt *S = CB->getTerminatorStmt())
|
2010-08-04 05:24:13 +08:00
|
|
|
return S;
|
2010-07-24 07:04:53 +08:00
|
|
|
else
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
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();
|
2014-02-28 05:56:41 +08:00
|
|
|
if (!pred)
|
|
|
|
return false;
|
2010-08-04 05:24:13 +08:00
|
|
|
|
Misc typos fixes in ./lib folder
Summary: Found via `codespell -q 3 -I ../clang-whitelist.txt -L uint,importd,crasher,gonna,cant,ue,ons,orign,ned`
Reviewers: teemperor
Reviewed By: teemperor
Subscribers: teemperor, jholewinski, jvesely, nhaehnle, whisperity, jfb, cfe-commits
Differential Revision: https://reviews.llvm.org/D55475
llvm-svn: 348755
2018-12-10 20:37:46 +08:00
|
|
|
// Get the predecessor block's terminator condition
|
2010-08-04 05:24:13 +08:00
|
|
|
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
|
2015-12-28 21:06:58 +08:00
|
|
|
return containsMacro(cond) || containsEnum(cond) ||
|
|
|
|
containsStaticLocal(cond) || containsBuiltinOffsetOf(cond) ||
|
|
|
|
containsStmt<UnaryExprOrTypeTraitExpr>(cond);
|
2010-07-28 07:30:21 +08:00
|
|
|
}
|
2010-08-13 07:01:06 +08:00
|
|
|
|
|
|
|
// Returns true if the given CFGBlock is empty
|
|
|
|
bool UnreachableCodeChecker::isEmptyCFGBlock(const CFGBlock *CB) {
|
2014-05-27 10:45:47 +08:00
|
|
|
return CB->getLabel() == nullptr // No labels
|
2010-08-13 07:01:06 +08:00
|
|
|
&& CB->size() == 0 // No statements
|
2019-05-24 09:34:22 +08:00
|
|
|
&& !CB->getTerminatorStmt(); // No terminator
|
2010-08-13 07:01:06 +08:00
|
|
|
}
|
2011-02-23 15:19:23 +08:00
|
|
|
|
|
|
|
void ento::registerUnreachableCodeChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<UnreachableCodeChecker>();
|
|
|
|
}
|
2019-01-26 22:23:08 +08:00
|
|
|
|
|
|
|
bool ento::shouldRegisterUnreachableCodeChecker(const LangOptions &LO) {
|
|
|
|
return true;
|
|
|
|
}
|