Jordan Rose | 8d0f528 | 2012-06-29 00:33:10 +0000 | [diff] [blame] | 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 | // |
Jordan Rose | 0ffbfd1 | 2012-07-10 23:56:23 +0000 | [diff] [blame] | 10 | // These checkers print various aspects of the ExprEngine's traversal of the CFG |
| 11 | // as it builds the ExplodedGraph. |
Jordan Rose | 8d0f528 | 2012-06-29 00:33:10 +0000 | [diff] [blame] | 12 | // |
| 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" |
Jordan Rose | f540c54 | 2012-07-26 21:39:41 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" |
Jordan Rose | 8d0f528 | 2012-06-29 00:33:10 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Benjamin Kramer | a93d0f2 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Jordan Rose | 8d0f528 | 2012-06-29 00:33:10 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace clang; |
| 24 | using namespace ento; |
| 25 | |
| 26 | namespace { |
| 27 | class TraversalDumper : public Checker< check::BranchCondition, |
| 28 | check::EndPath > { |
| 29 | public: |
| 30 | void checkBranchCondition(const Stmt *Condition, CheckerContext &C) const; |
| 31 | void checkEndPath(CheckerContext &C) const; |
| 32 | }; |
| 33 | } |
| 34 | |
| 35 | void TraversalDumper::checkBranchCondition(const Stmt *Condition, |
| 36 | CheckerContext &C) const { |
| 37 | // Special-case Objective-C's for-in loop, which uses the entire loop as its |
| 38 | // condition. We just print the collection expression. |
| 39 | const Stmt *Parent = dyn_cast<ObjCForCollectionStmt>(Condition); |
| 40 | if (!Parent) { |
| 41 | const ParentMap &Parents = C.getLocationContext()->getParentMap(); |
| 42 | Parent = Parents.getParent(Condition); |
| 43 | } |
| 44 | |
| 45 | // It is mildly evil to print directly to llvm::outs() rather than emitting |
| 46 | // warnings, but this ensures things do not get filtered out by the rest of |
| 47 | // the static analyzer machinery. |
| 48 | SourceLocation Loc = Parent->getLocStart(); |
| 49 | llvm::outs() << C.getSourceManager().getSpellingLineNumber(Loc) << " " |
| 50 | << Parent->getStmtClassName() << "\n"; |
| 51 | } |
| 52 | |
| 53 | void TraversalDumper::checkEndPath(CheckerContext &C) const { |
| 54 | llvm::outs() << "--END PATH--\n"; |
| 55 | } |
| 56 | |
| 57 | void ento::registerTraversalDumper(CheckerManager &mgr) { |
| 58 | mgr.registerChecker<TraversalDumper>(); |
| 59 | } |
Jordan Rose | 0ffbfd1 | 2012-07-10 23:56:23 +0000 | [diff] [blame] | 60 | |
| 61 | //------------------------------------------------------------------------------ |
| 62 | |
| 63 | namespace { |
| 64 | class CallDumper : public Checker< check::PreCall > { |
| 65 | public: |
| 66 | void checkPreCall(const CallEvent &Call, CheckerContext &C) const; |
| 67 | }; |
| 68 | } |
| 69 | |
| 70 | void CallDumper::checkPreCall(const CallEvent &Call, CheckerContext &C) const { |
| 71 | unsigned Indentation = 0; |
| 72 | for (const LocationContext *LC = C.getLocationContext()->getParent(); |
| 73 | LC != 0; LC = LC->getParent()) |
| 74 | ++Indentation; |
| 75 | |
| 76 | // It is mildly evil to print directly to llvm::outs() rather than emitting |
| 77 | // warnings, but this ensures things do not get filtered out by the rest of |
| 78 | // the static analyzer machinery. |
| 79 | llvm::outs().indent(Indentation); |
| 80 | Call.dump(llvm::outs()); |
| 81 | } |
| 82 | |
| 83 | void ento::registerCallDumper(CheckerManager &mgr) { |
| 84 | mgr.registerChecker<CallDumper>(); |
| 85 | } |