1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" |
14 | #include "clang/AST/ParentMap.h" |
15 | #include "clang/AST/StmtObjC.h" |
16 | #include "clang/StaticAnalyzer/Core/Checker.h" |
17 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
18 | #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" |
19 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
20 | #include "llvm/Support/raw_ostream.h" |
21 | |
22 | using namespace clang; |
23 | using namespace ento; |
24 | |
25 | namespace { |
26 | class TraversalDumper : public Checker< check::BranchCondition, |
27 | check::BeginFunction, |
28 | check::EndFunction > { |
29 | public: |
30 | void checkBranchCondition(const Stmt *Condition, CheckerContext &C) const; |
31 | void checkBeginFunction(CheckerContext &C) const; |
32 | void checkEndFunction(const ReturnStmt *RS, CheckerContext &C) const; |
33 | }; |
34 | } |
35 | |
36 | void TraversalDumper::checkBranchCondition(const Stmt *Condition, |
37 | CheckerContext &C) const { |
38 | |
39 | |
40 | const Stmt *Parent = dyn_cast<ObjCForCollectionStmt>(Condition); |
41 | if (!Parent) { |
42 | const ParentMap &Parents = C.getLocationContext()->getParentMap(); |
43 | Parent = Parents.getParent(Condition); |
44 | } |
45 | |
46 | |
47 | |
48 | |
49 | SourceLocation Loc = Parent->getBeginLoc(); |
50 | llvm::outs() << C.getSourceManager().getSpellingLineNumber(Loc) << " " |
51 | << Parent->getStmtClassName() << "\n"; |
52 | } |
53 | |
54 | void TraversalDumper::checkBeginFunction(CheckerContext &C) const { |
55 | llvm::outs() << "--BEGIN FUNCTION--\n"; |
56 | } |
57 | |
58 | void TraversalDumper::checkEndFunction(const ReturnStmt *RS, |
59 | CheckerContext &C) const { |
60 | llvm::outs() << "--END FUNCTION--\n"; |
61 | } |
62 | |
63 | void ento::registerTraversalDumper(CheckerManager &mgr) { |
64 | mgr.registerChecker<TraversalDumper>(); |
65 | } |
66 | |
67 | bool ento::shouldRegisterTraversalDumper(const LangOptions &LO) { |
68 | return true; |
69 | } |
70 | |
71 | |
72 | |
73 | namespace { |
74 | class CallDumper : public Checker< check::PreCall, |
75 | check::PostCall > { |
76 | public: |
77 | void checkPreCall(const CallEvent &Call, CheckerContext &C) const; |
78 | void checkPostCall(const CallEvent &Call, CheckerContext &C) const; |
79 | }; |
80 | } |
81 | |
82 | void CallDumper::checkPreCall(const CallEvent &Call, CheckerContext &C) const { |
83 | unsigned Indentation = 0; |
84 | for (const LocationContext *LC = C.getLocationContext()->getParent(); |
85 | LC != nullptr; LC = LC->getParent()) |
86 | ++Indentation; |
87 | |
88 | |
89 | |
90 | |
91 | llvm::outs().indent(Indentation); |
92 | Call.dump(llvm::outs()); |
93 | } |
94 | |
95 | void CallDumper::checkPostCall(const CallEvent &Call, CheckerContext &C) const { |
96 | const Expr *CallE = Call.getOriginExpr(); |
97 | if (!CallE) |
98 | return; |
99 | |
100 | unsigned Indentation = 0; |
101 | for (const LocationContext *LC = C.getLocationContext()->getParent(); |
102 | LC != nullptr; LC = LC->getParent()) |
103 | ++Indentation; |
104 | |
105 | |
106 | |
107 | |
108 | llvm::outs().indent(Indentation); |
109 | if (Call.getResultType()->isVoidType()) |
110 | llvm::outs() << "Returning void\n"; |
111 | else |
112 | llvm::outs() << "Returning " << C.getSVal(CallE) << "\n"; |
113 | } |
114 | |
115 | void ento::registerCallDumper(CheckerManager &mgr) { |
116 | mgr.registerChecker<CallDumper>(); |
117 | } |
118 | |
119 | bool ento::shouldRegisterCallDumper(const LangOptions &LO) { |
120 | return true; |
121 | } |
122 | |