1 //== TraversalChecker.cpp -------------------------------------- -*- C++ -*--=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // These checkers print various aspects of the ExprEngine's traversal of the CFG
11 // as it builds the ExplodedGraph.
13 //===----------------------------------------------------------------------===//
14 #include "ClangSACheckers.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"
22 using namespace clang;
26 class TraversalDumper : public Checker< check::BranchCondition,
29 void checkBranchCondition(const Stmt *Condition, CheckerContext &C) const;
30 void checkEndPath(CheckerContext &C) const;
34 void TraversalDumper::checkBranchCondition(const Stmt *Condition,
35 CheckerContext &C) const {
36 // Special-case Objective-C's for-in loop, which uses the entire loop as its
37 // condition. We just print the collection expression.
38 const Stmt *Parent = dyn_cast<ObjCForCollectionStmt>(Condition);
40 const ParentMap &Parents = C.getLocationContext()->getParentMap();
41 Parent = Parents.getParent(Condition);
44 // It is mildly evil to print directly to llvm::outs() rather than emitting
45 // warnings, but this ensures things do not get filtered out by the rest of
46 // the static analyzer machinery.
47 SourceLocation Loc = Parent->getLocStart();
48 llvm::outs() << C.getSourceManager().getSpellingLineNumber(Loc) << " "
49 << Parent->getStmtClassName() << "\n";
52 void TraversalDumper::checkEndPath(CheckerContext &C) const {
53 llvm::outs() << "--END PATH--\n";
56 void ento::registerTraversalDumper(CheckerManager &mgr) {
57 mgr.registerChecker<TraversalDumper>();
60 //------------------------------------------------------------------------------
63 class CallDumper : public Checker< check::PreCall > {
65 void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
69 void CallDumper::checkPreCall(const CallEvent &Call, CheckerContext &C) const {
70 unsigned Indentation = 0;
71 for (const LocationContext *LC = C.getLocationContext()->getParent();
72 LC != 0; LC = LC->getParent())
75 // It is mildly evil to print directly to llvm::outs() rather than emitting
76 // warnings, but this ensures things do not get filtered out by the rest of
77 // the static analyzer machinery.
78 llvm::outs().indent(Indentation);
79 Call.dump(llvm::outs());
82 void ento::registerCallDumper(CheckerManager &mgr) {
83 mgr.registerChecker<CallDumper>();