2012-06-29 08:33:10 +08:00
|
|
|
//== TraversalChecker.cpp -------------------------------------- -*- 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
|
2012-06-29 08:33:10 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2012-07-11 07:56:23 +08:00
|
|
|
// These checkers print various aspects of the ExprEngine's traversal of the CFG
|
|
|
|
// as it builds the ExplodedGraph.
|
2012-06-29 08:33:10 +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-06-29 08:33:10 +08:00
|
|
|
#include "clang/AST/ParentMap.h"
|
|
|
|
#include "clang/AST/StmtObjC.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
2012-07-27 05:39:41 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
|
2012-06-29 08:33:10 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
2012-12-02 01:12:56 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-06-29 08:33:10 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace ento;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class TraversalDumper : public Checker< check::BranchCondition,
|
2016-02-19 09:35:10 +08:00
|
|
|
check::BeginFunction,
|
2013-01-03 08:25:29 +08:00
|
|
|
check::EndFunction > {
|
2012-06-29 08:33:10 +08:00
|
|
|
public:
|
|
|
|
void checkBranchCondition(const Stmt *Condition, CheckerContext &C) const;
|
2016-02-19 09:35:10 +08:00
|
|
|
void checkBeginFunction(CheckerContext &C) const;
|
2018-07-17 04:47:45 +08:00
|
|
|
void checkEndFunction(const ReturnStmt *RS, CheckerContext &C) const;
|
2012-06-29 08:33:10 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void TraversalDumper::checkBranchCondition(const Stmt *Condition,
|
|
|
|
CheckerContext &C) const {
|
|
|
|
// Special-case Objective-C's for-in loop, which uses the entire loop as its
|
|
|
|
// condition. We just print the collection expression.
|
|
|
|
const Stmt *Parent = dyn_cast<ObjCForCollectionStmt>(Condition);
|
|
|
|
if (!Parent) {
|
|
|
|
const ParentMap &Parents = C.getLocationContext()->getParentMap();
|
|
|
|
Parent = Parents.getParent(Condition);
|
|
|
|
}
|
|
|
|
|
|
|
|
// It is mildly evil to print directly to llvm::outs() rather than emitting
|
|
|
|
// warnings, but this ensures things do not get filtered out by the rest of
|
|
|
|
// the static analyzer machinery.
|
2018-08-10 05:08:08 +08:00
|
|
|
SourceLocation Loc = Parent->getBeginLoc();
|
2012-06-29 08:33:10 +08:00
|
|
|
llvm::outs() << C.getSourceManager().getSpellingLineNumber(Loc) << " "
|
|
|
|
<< Parent->getStmtClassName() << "\n";
|
|
|
|
}
|
|
|
|
|
2016-02-19 09:35:10 +08:00
|
|
|
void TraversalDumper::checkBeginFunction(CheckerContext &C) const {
|
|
|
|
llvm::outs() << "--BEGIN FUNCTION--\n";
|
|
|
|
}
|
|
|
|
|
2018-07-17 04:47:45 +08:00
|
|
|
void TraversalDumper::checkEndFunction(const ReturnStmt *RS,
|
|
|
|
CheckerContext &C) const {
|
2013-01-03 08:25:29 +08:00
|
|
|
llvm::outs() << "--END FUNCTION--\n";
|
2012-06-29 08:33:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ento::registerTraversalDumper(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<TraversalDumper>();
|
|
|
|
}
|
2012-07-11 07:56:23 +08:00
|
|
|
|
2020-03-27 21:29:31 +08:00
|
|
|
bool ento::shouldRegisterTraversalDumper(const CheckerManager &mgr) {
|
2019-01-26 22:23:08 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-07-11 07:56:23 +08:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace {
|
2013-03-22 02:16:59 +08:00
|
|
|
class CallDumper : public Checker< check::PreCall,
|
|
|
|
check::PostCall > {
|
2012-07-11 07:56:23 +08:00
|
|
|
public:
|
|
|
|
void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
|
2013-03-22 02:16:59 +08:00
|
|
|
void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
|
2012-07-11 07:56:23 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void CallDumper::checkPreCall(const CallEvent &Call, CheckerContext &C) const {
|
|
|
|
unsigned Indentation = 0;
|
|
|
|
for (const LocationContext *LC = C.getLocationContext()->getParent();
|
2014-05-27 10:45:47 +08:00
|
|
|
LC != nullptr; LC = LC->getParent())
|
2012-07-11 07:56:23 +08:00
|
|
|
++Indentation;
|
|
|
|
|
|
|
|
// It is mildly evil to print directly to llvm::outs() rather than emitting
|
|
|
|
// warnings, but this ensures things do not get filtered out by the rest of
|
|
|
|
// the static analyzer machinery.
|
|
|
|
llvm::outs().indent(Indentation);
|
|
|
|
Call.dump(llvm::outs());
|
|
|
|
}
|
|
|
|
|
2013-03-22 02:16:59 +08:00
|
|
|
void CallDumper::checkPostCall(const CallEvent &Call, CheckerContext &C) const {
|
|
|
|
const Expr *CallE = Call.getOriginExpr();
|
|
|
|
if (!CallE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
unsigned Indentation = 0;
|
|
|
|
for (const LocationContext *LC = C.getLocationContext()->getParent();
|
2014-05-27 10:45:47 +08:00
|
|
|
LC != nullptr; LC = LC->getParent())
|
2013-03-22 02:16:59 +08:00
|
|
|
++Indentation;
|
|
|
|
|
|
|
|
// It is mildly evil to print directly to llvm::outs() rather than emitting
|
|
|
|
// warnings, but this ensures things do not get filtered out by the rest of
|
|
|
|
// the static analyzer machinery.
|
|
|
|
llvm::outs().indent(Indentation);
|
|
|
|
if (Call.getResultType()->isVoidType())
|
|
|
|
llvm::outs() << "Returning void\n";
|
|
|
|
else
|
|
|
|
llvm::outs() << "Returning " << C.getSVal(CallE) << "\n";
|
|
|
|
}
|
|
|
|
|
2012-07-11 07:56:23 +08:00
|
|
|
void ento::registerCallDumper(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<CallDumper>();
|
|
|
|
}
|
2019-01-26 22:23:08 +08:00
|
|
|
|
2020-03-27 21:29:31 +08:00
|
|
|
bool ento::shouldRegisterCallDumper(const CheckerManager &mgr) {
|
2019-01-26 22:23:08 +08:00
|
|
|
return true;
|
|
|
|
}
|