2011-02-18 05:39:39 +08:00
|
|
|
//==- DebugCheckers.cpp - Debugging Checkers ---------------------*- 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
|
2011-02-18 05:39:39 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2013-04-12 08:44:24 +08:00
|
|
|
// This file defines checkers that display debugging information.
|
2011-02-18 05:39:39 +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"
|
2011-10-25 08:25:24 +08:00
|
|
|
#include "clang/Analysis/Analyses/Dominators.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Analysis/Analyses/LiveVariables.h"
|
2012-03-08 08:42:23 +08:00
|
|
|
#include "clang/Analysis/CallGraph.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
2015-10-22 19:53:04 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
|
2015-10-22 19:53:04 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
2013-06-25 02:12:12 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
|
2011-12-23 07:33:52 +08:00
|
|
|
#include "llvm/Support/Process.h"
|
2011-02-18 05:39:39 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace ento;
|
|
|
|
|
2011-10-25 08:25:24 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// DominatorsTreeDumper
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class DominatorsTreeDumper : public Checker<check::ASTCodeBody> {
|
|
|
|
public:
|
|
|
|
void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
|
|
|
|
BugReporter &BR) const {
|
|
|
|
if (AnalysisDeclContext *AC = mgr.getAnalysisDeclContext(D)) {
|
2019-07-05 20:17:44 +08:00
|
|
|
CFGDomTree Dom;
|
|
|
|
Dom.buildDominatorTree(AC->getCFG());
|
|
|
|
Dom.dump();
|
2011-10-25 08:25:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2011-10-25 08:25:24 +08:00
|
|
|
|
|
|
|
void ento::registerDominatorsTreeDumper(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<DominatorsTreeDumper>();
|
|
|
|
}
|
|
|
|
|
2020-03-27 21:29:31 +08:00
|
|
|
bool ento::shouldRegisterDominatorsTreeDumper(const CheckerManager &mgr) {
|
2019-01-26 22:23:08 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-02-18 05:39:39 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-07-03 19:39:12 +08:00
|
|
|
// PostDominatorsTreeDumper
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class PostDominatorsTreeDumper : public Checker<check::ASTCodeBody> {
|
|
|
|
public:
|
|
|
|
void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
|
|
|
|
BugReporter &BR) const {
|
|
|
|
if (AnalysisDeclContext *AC = mgr.getAnalysisDeclContext(D)) {
|
2019-07-05 20:17:44 +08:00
|
|
|
CFGPostDomTree Dom;
|
|
|
|
Dom.buildDominatorTree(AC->getCFG());
|
|
|
|
Dom.dump();
|
2019-07-03 19:39:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void ento::registerPostDominatorsTreeDumper(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<PostDominatorsTreeDumper>();
|
|
|
|
}
|
|
|
|
|
2020-03-27 21:29:31 +08:00
|
|
|
bool ento::shouldRegisterPostDominatorsTreeDumper(const CheckerManager &mgr) {
|
2019-07-03 19:39:12 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2019-07-05 20:17:44 +08:00
|
|
|
// ControlDependencyTreeDumper
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class ControlDependencyTreeDumper : public Checker<check::ASTCodeBody> {
|
|
|
|
public:
|
|
|
|
void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
|
|
|
|
BugReporter &BR) const {
|
|
|
|
if (AnalysisDeclContext *AC = mgr.getAnalysisDeclContext(D)) {
|
|
|
|
ControlDependencyCalculator Dom(AC->getCFG());
|
|
|
|
Dom.dump();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void ento::registerControlDependencyTreeDumper(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<ControlDependencyTreeDumper>();
|
|
|
|
}
|
|
|
|
|
2020-03-27 21:29:31 +08:00
|
|
|
bool ento::shouldRegisterControlDependencyTreeDumper(const CheckerManager &mgr) {
|
2019-07-05 20:17:44 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2011-02-18 05:39:39 +08:00
|
|
|
// LiveVariablesDumper
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2011-03-01 09:16:21 +08:00
|
|
|
class LiveVariablesDumper : public Checker<check::ASTCodeBody> {
|
2011-02-18 05:39:39 +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:39 +08:00
|
|
|
L->dumpBlockLiveness(mgr.getSourceManager());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2011-02-18 05:39:39 +08:00
|
|
|
|
|
|
|
void ento::registerLiveVariablesDumper(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<LiveVariablesDumper>();
|
|
|
|
}
|
|
|
|
|
2020-03-27 21:29:31 +08:00
|
|
|
bool ento::shouldRegisterLiveVariablesDumper(const CheckerManager &mgr) {
|
2019-01-26 22:23:08 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-17 07:44:06 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// LiveStatementsDumper
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2020-09-15 23:43:02 +08:00
|
|
|
class LiveExpressionsDumper : public Checker<check::ASTCodeBody> {
|
2018-12-17 07:44:06 +08:00
|
|
|
public:
|
|
|
|
void checkASTCodeBody(const Decl *D, AnalysisManager& Mgr,
|
|
|
|
BugReporter &BR) const {
|
|
|
|
if (LiveVariables *L = Mgr.getAnalysis<RelaxedLiveVariables>(D))
|
2020-09-15 23:43:02 +08:00
|
|
|
L->dumpExprLiveness(Mgr.getSourceManager());
|
2018-12-17 07:44:06 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-09-15 23:43:02 +08:00
|
|
|
void ento::registerLiveExpressionsDumper(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<LiveExpressionsDumper>();
|
2018-12-17 07:44:06 +08:00
|
|
|
}
|
|
|
|
|
2020-09-15 23:43:02 +08:00
|
|
|
bool ento::shouldRegisterLiveExpressionsDumper(const CheckerManager &mgr) {
|
2019-01-26 22:23:08 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-02-18 05:39:39 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CFGViewer
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2011-03-01 09:16:21 +08:00
|
|
|
class CFGViewer : public Checker<check::ASTCodeBody> {
|
2011-02-18 05:39:39 +08:00
|
|
|
public:
|
|
|
|
void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
|
|
|
|
BugReporter &BR) const {
|
|
|
|
if (CFG *cfg = mgr.getCFG(D)) {
|
2012-03-11 15:00:24 +08:00
|
|
|
cfg->viewCFG(mgr.getLangOpts());
|
2011-02-18 05:39:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2011-02-18 05:39:39 +08:00
|
|
|
|
|
|
|
void ento::registerCFGViewer(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<CFGViewer>();
|
|
|
|
}
|
|
|
|
|
2020-03-27 21:29:31 +08:00
|
|
|
bool ento::shouldRegisterCFGViewer(const CheckerManager &mgr) {
|
2019-01-26 22:23:08 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-02-18 05:39:39 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CFGDumper
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2011-03-01 09:16:21 +08:00
|
|
|
class CFGDumper : public Checker<check::ASTCodeBody> {
|
2011-02-18 05:39:39 +08:00
|
|
|
public:
|
|
|
|
void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
|
|
|
|
BugReporter &BR) const {
|
2014-01-16 01:25:05 +08:00
|
|
|
PrintingPolicy Policy(mgr.getLangOpts());
|
|
|
|
Policy.TerseOutput = true;
|
|
|
|
Policy.PolishForDeclaration = true;
|
|
|
|
D->print(llvm::errs(), Policy);
|
|
|
|
|
2011-02-18 05:39:39 +08:00
|
|
|
if (CFG *cfg = mgr.getCFG(D)) {
|
2012-03-11 15:00:24 +08:00
|
|
|
cfg->dump(mgr.getLangOpts(),
|
2011-12-23 07:33:52 +08:00
|
|
|
llvm::sys::Process::StandardErrHasColors());
|
2011-02-18 05:39:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2011-02-18 05:39:39 +08:00
|
|
|
|
|
|
|
void ento::registerCFGDumper(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<CFGDumper>();
|
|
|
|
}
|
2012-03-08 08:42:23 +08:00
|
|
|
|
2020-03-27 21:29:31 +08:00
|
|
|
bool ento::shouldRegisterCFGDumper(const CheckerManager &mgr) {
|
2019-01-26 22:23:08 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-08 08:42:23 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CallGraphViewer
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class CallGraphViewer : public Checker< check::ASTDecl<TranslationUnitDecl> > {
|
|
|
|
public:
|
|
|
|
void checkASTDecl(const TranslationUnitDecl *TU, AnalysisManager& mgr,
|
|
|
|
BugReporter &BR) const {
|
|
|
|
CallGraph CG;
|
|
|
|
CG.addToCallGraph(const_cast<TranslationUnitDecl*>(TU));
|
|
|
|
CG.viewGraph();
|
|
|
|
}
|
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2012-03-08 08:42:23 +08:00
|
|
|
|
|
|
|
void ento::registerCallGraphViewer(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<CallGraphViewer>();
|
|
|
|
}
|
|
|
|
|
2020-03-27 21:29:31 +08:00
|
|
|
bool ento::shouldRegisterCallGraphViewer(const CheckerManager &mgr) {
|
2019-01-26 22:23:08 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-08 08:42:23 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CallGraphDumper
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class CallGraphDumper : public Checker< check::ASTDecl<TranslationUnitDecl> > {
|
|
|
|
public:
|
|
|
|
void checkASTDecl(const TranslationUnitDecl *TU, AnalysisManager& mgr,
|
|
|
|
BugReporter &BR) const {
|
|
|
|
CallGraph CG;
|
|
|
|
CG.addToCallGraph(const_cast<TranslationUnitDecl*>(TU));
|
|
|
|
CG.dump();
|
|
|
|
}
|
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2012-03-08 08:42:23 +08:00
|
|
|
|
|
|
|
void ento::registerCallGraphDumper(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<CallGraphDumper>();
|
|
|
|
}
|
2012-10-02 02:28:14 +08:00
|
|
|
|
2020-03-27 21:29:31 +08:00
|
|
|
bool ento::shouldRegisterCallGraphDumper(const CheckerManager &mgr) {
|
2019-01-26 22:23:08 +08:00
|
|
|
return true;
|
|
|
|
}
|
2012-10-02 02:28:14 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ConfigDumper
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class ConfigDumper : public Checker< check::EndOfTranslationUnit > {
|
2013-09-22 20:41:24 +08:00
|
|
|
typedef AnalyzerOptions::ConfigTable Table;
|
|
|
|
|
2014-03-08 05:51:58 +08:00
|
|
|
static int compareEntry(const Table::MapEntryTy *const *LHS,
|
|
|
|
const Table::MapEntryTy *const *RHS) {
|
|
|
|
return (*LHS)->getKey().compare((*RHS)->getKey());
|
|
|
|
}
|
|
|
|
|
2012-10-02 02:28:14 +08:00
|
|
|
public:
|
|
|
|
void checkEndOfTranslationUnit(const TranslationUnitDecl *TU,
|
|
|
|
AnalysisManager& mgr,
|
|
|
|
BugReporter &BR) const {
|
2013-09-22 20:41:24 +08:00
|
|
|
const Table &Config = mgr.options.Config;
|
2012-10-02 02:28:14 +08:00
|
|
|
|
2013-09-22 20:41:24 +08:00
|
|
|
SmallVector<const Table::MapEntryTy *, 32> Keys;
|
|
|
|
for (Table::const_iterator I = Config.begin(), E = Config.end(); I != E;
|
|
|
|
++I)
|
|
|
|
Keys.push_back(&*I);
|
2014-03-08 05:51:58 +08:00
|
|
|
llvm::array_pod_sort(Keys.begin(), Keys.end(), compareEntry);
|
2012-10-02 02:28:14 +08:00
|
|
|
|
|
|
|
llvm::errs() << "[config]\n";
|
2013-09-22 20:41:24 +08:00
|
|
|
for (unsigned I = 0, E = Keys.size(); I != E; ++I)
|
[analyzer] Evaluate all non-checker config options before analysis
In earlier patches regarding AnalyzerOptions, a lot of effort went into
gathering all config options, and changing the interface so that potential
misuse can be eliminited.
Up until this point, AnalyzerOptions only evaluated an option when it was
querried. For example, if we had a "-no-false-positives" flag, AnalyzerOptions
would store an Optional field for it that would be None up until somewhere in
the code until the flag's getter function is called.
However, now that we're confident that we've gathered all configs, we can
evaluate off of them before analysis, so we can emit a error on invalid input
even if that prticular flag will not matter in that particular run of the
analyzer. Another very big benefit of this is that debug.ConfigDumper will now
show the value of all configs every single time.
Also, almost all options related class have a similar interface, so uniformity
is also a benefit.
The implementation for errors on invalid input will be commited shorty.
Differential Revision: https://reviews.llvm.org/D53692
llvm-svn: 348031
2018-12-01 04:44:00 +08:00
|
|
|
llvm::errs() << Keys[I]->getKey() << " = "
|
|
|
|
<< (Keys[I]->second.empty() ? "\"\"" : Keys[I]->second)
|
|
|
|
<< '\n';
|
2012-10-02 02:28:14 +08:00
|
|
|
}
|
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2012-10-02 02:28:14 +08:00
|
|
|
|
|
|
|
void ento::registerConfigDumper(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<ConfigDumper>();
|
|
|
|
}
|
2013-06-25 02:12:12 +08:00
|
|
|
|
2020-03-27 21:29:31 +08:00
|
|
|
bool ento::shouldRegisterConfigDumper(const CheckerManager &mgr) {
|
2019-01-26 22:23:08 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-25 02:12:12 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ExplodedGraph Viewer
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class ExplodedGraphViewer : public Checker< check::EndAnalysis > {
|
|
|
|
public:
|
|
|
|
ExplodedGraphViewer() {}
|
|
|
|
void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,ExprEngine &Eng) const {
|
|
|
|
Eng.ViewGraph(0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ento::registerExplodedGraphViewer(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<ExplodedGraphViewer>();
|
|
|
|
}
|
2015-10-22 19:53:04 +08:00
|
|
|
|
2020-03-27 21:29:31 +08:00
|
|
|
bool ento::shouldRegisterExplodedGraphViewer(const CheckerManager &mgr) {
|
2019-01-26 22:23:08 +08:00
|
|
|
return true;
|
|
|
|
}
|
2019-03-15 00:10:29 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Emits a report for every Stmt that the analyzer visits.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class ReportStmts : public Checker<check::PreStmt<Stmt>> {
|
|
|
|
BuiltinBug BT_stmtLoc{this, "Statement"};
|
|
|
|
|
|
|
|
public:
|
|
|
|
void checkPreStmt(const Stmt *S, CheckerContext &C) const {
|
|
|
|
ExplodedNode *Node = C.generateNonFatalErrorNode();
|
|
|
|
if (!Node)
|
|
|
|
return;
|
|
|
|
|
2019-09-10 04:34:40 +08:00
|
|
|
auto Report =
|
|
|
|
std::make_unique<PathSensitiveBugReport>(BT_stmtLoc, "Statement", Node);
|
2019-03-15 00:10:29 +08:00
|
|
|
|
|
|
|
C.emitReport(std::move(Report));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end of anonymous namespace
|
|
|
|
|
|
|
|
void ento::registerReportStmts(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<ReportStmts>();
|
|
|
|
}
|
|
|
|
|
2020-03-27 21:29:31 +08:00
|
|
|
bool ento::shouldRegisterReportStmts(const CheckerManager &mgr) {
|
2019-03-15 00:10:29 +08:00
|
|
|
return true;
|
|
|
|
}
|