1*67e74705SXin Li //== TraversalChecker.cpp -------------------------------------- -*- C++ -*--=//
2*67e74705SXin Li //
3*67e74705SXin Li // The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li //
10*67e74705SXin Li // These checkers print various aspects of the ExprEngine's traversal of the CFG
11*67e74705SXin Li // as it builds the ExplodedGraph.
12*67e74705SXin Li //
13*67e74705SXin Li //===----------------------------------------------------------------------===//
14*67e74705SXin Li #include "ClangSACheckers.h"
15*67e74705SXin Li #include "clang/AST/ParentMap.h"
16*67e74705SXin Li #include "clang/AST/StmtObjC.h"
17*67e74705SXin Li #include "clang/StaticAnalyzer/Core/Checker.h"
18*67e74705SXin Li #include "clang/StaticAnalyzer/Core/CheckerManager.h"
19*67e74705SXin Li #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
20*67e74705SXin Li #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
21*67e74705SXin Li #include "llvm/Support/raw_ostream.h"
22*67e74705SXin Li
23*67e74705SXin Li using namespace clang;
24*67e74705SXin Li using namespace ento;
25*67e74705SXin Li
26*67e74705SXin Li namespace {
27*67e74705SXin Li class TraversalDumper : public Checker< check::BranchCondition,
28*67e74705SXin Li check::BeginFunction,
29*67e74705SXin Li check::EndFunction > {
30*67e74705SXin Li public:
31*67e74705SXin Li void checkBranchCondition(const Stmt *Condition, CheckerContext &C) const;
32*67e74705SXin Li void checkBeginFunction(CheckerContext &C) const;
33*67e74705SXin Li void checkEndFunction(CheckerContext &C) const;
34*67e74705SXin Li };
35*67e74705SXin Li }
36*67e74705SXin Li
checkBranchCondition(const Stmt * Condition,CheckerContext & C) const37*67e74705SXin Li void TraversalDumper::checkBranchCondition(const Stmt *Condition,
38*67e74705SXin Li CheckerContext &C) const {
39*67e74705SXin Li // Special-case Objective-C's for-in loop, which uses the entire loop as its
40*67e74705SXin Li // condition. We just print the collection expression.
41*67e74705SXin Li const Stmt *Parent = dyn_cast<ObjCForCollectionStmt>(Condition);
42*67e74705SXin Li if (!Parent) {
43*67e74705SXin Li const ParentMap &Parents = C.getLocationContext()->getParentMap();
44*67e74705SXin Li Parent = Parents.getParent(Condition);
45*67e74705SXin Li }
46*67e74705SXin Li
47*67e74705SXin Li // It is mildly evil to print directly to llvm::outs() rather than emitting
48*67e74705SXin Li // warnings, but this ensures things do not get filtered out by the rest of
49*67e74705SXin Li // the static analyzer machinery.
50*67e74705SXin Li SourceLocation Loc = Parent->getLocStart();
51*67e74705SXin Li llvm::outs() << C.getSourceManager().getSpellingLineNumber(Loc) << " "
52*67e74705SXin Li << Parent->getStmtClassName() << "\n";
53*67e74705SXin Li }
54*67e74705SXin Li
checkBeginFunction(CheckerContext & C) const55*67e74705SXin Li void TraversalDumper::checkBeginFunction(CheckerContext &C) const {
56*67e74705SXin Li llvm::outs() << "--BEGIN FUNCTION--\n";
57*67e74705SXin Li }
58*67e74705SXin Li
checkEndFunction(CheckerContext & C) const59*67e74705SXin Li void TraversalDumper::checkEndFunction(CheckerContext &C) const {
60*67e74705SXin Li llvm::outs() << "--END FUNCTION--\n";
61*67e74705SXin Li }
62*67e74705SXin Li
registerTraversalDumper(CheckerManager & mgr)63*67e74705SXin Li void ento::registerTraversalDumper(CheckerManager &mgr) {
64*67e74705SXin Li mgr.registerChecker<TraversalDumper>();
65*67e74705SXin Li }
66*67e74705SXin Li
67*67e74705SXin Li //------------------------------------------------------------------------------
68*67e74705SXin Li
69*67e74705SXin Li namespace {
70*67e74705SXin Li class CallDumper : public Checker< check::PreCall,
71*67e74705SXin Li check::PostCall > {
72*67e74705SXin Li public:
73*67e74705SXin Li void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
74*67e74705SXin Li void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
75*67e74705SXin Li };
76*67e74705SXin Li }
77*67e74705SXin Li
checkPreCall(const CallEvent & Call,CheckerContext & C) const78*67e74705SXin Li void CallDumper::checkPreCall(const CallEvent &Call, CheckerContext &C) const {
79*67e74705SXin Li unsigned Indentation = 0;
80*67e74705SXin Li for (const LocationContext *LC = C.getLocationContext()->getParent();
81*67e74705SXin Li LC != nullptr; LC = LC->getParent())
82*67e74705SXin Li ++Indentation;
83*67e74705SXin Li
84*67e74705SXin Li // It is mildly evil to print directly to llvm::outs() rather than emitting
85*67e74705SXin Li // warnings, but this ensures things do not get filtered out by the rest of
86*67e74705SXin Li // the static analyzer machinery.
87*67e74705SXin Li llvm::outs().indent(Indentation);
88*67e74705SXin Li Call.dump(llvm::outs());
89*67e74705SXin Li }
90*67e74705SXin Li
checkPostCall(const CallEvent & Call,CheckerContext & C) const91*67e74705SXin Li void CallDumper::checkPostCall(const CallEvent &Call, CheckerContext &C) const {
92*67e74705SXin Li const Expr *CallE = Call.getOriginExpr();
93*67e74705SXin Li if (!CallE)
94*67e74705SXin Li return;
95*67e74705SXin Li
96*67e74705SXin Li unsigned Indentation = 0;
97*67e74705SXin Li for (const LocationContext *LC = C.getLocationContext()->getParent();
98*67e74705SXin Li LC != nullptr; LC = LC->getParent())
99*67e74705SXin Li ++Indentation;
100*67e74705SXin Li
101*67e74705SXin Li // It is mildly evil to print directly to llvm::outs() rather than emitting
102*67e74705SXin Li // warnings, but this ensures things do not get filtered out by the rest of
103*67e74705SXin Li // the static analyzer machinery.
104*67e74705SXin Li llvm::outs().indent(Indentation);
105*67e74705SXin Li if (Call.getResultType()->isVoidType())
106*67e74705SXin Li llvm::outs() << "Returning void\n";
107*67e74705SXin Li else
108*67e74705SXin Li llvm::outs() << "Returning " << C.getSVal(CallE) << "\n";
109*67e74705SXin Li }
110*67e74705SXin Li
registerCallDumper(CheckerManager & mgr)111*67e74705SXin Li void ento::registerCallDumper(CheckerManager &mgr) {
112*67e74705SXin Li mgr.registerChecker<CallDumper>();
113*67e74705SXin Li }
114