clang 8.0.0

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