2012-06-29 08:33:10 +08:00
|
|
|
//== TraversalChecker.cpp -------------------------------------- -*- C++ -*--=//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
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
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
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>();
|
|
|
|
}
|