TraversalChecker.cpp revision 239313
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 "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"
21
22using namespace clang;
23using namespace ento;
24
25namespace {
26class TraversalDumper : public Checker< check::BranchCondition,
27                                        check::EndPath > {
28public:
29  void checkBranchCondition(const Stmt *Condition, CheckerContext &C) const;
30  void checkEndPath(CheckerContext &C) const;
31};
32}
33
34void 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);
39  if (!Parent) {
40    const ParentMap &Parents = C.getLocationContext()->getParentMap();
41    Parent = Parents.getParent(Condition);
42  }
43
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";
50}
51
52void TraversalDumper::checkEndPath(CheckerContext &C) const {
53  llvm::outs() << "--END PATH--\n";
54}
55
56void ento::registerTraversalDumper(CheckerManager &mgr) {
57  mgr.registerChecker<TraversalDumper>();
58}
59
60//------------------------------------------------------------------------------
61
62namespace {
63class CallDumper : public Checker< check::PreCall > {
64public:
65  void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
66};
67}
68
69void 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())
73    ++Indentation;
74
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());
80}
81
82void ento::registerCallDumper(CheckerManager &mgr) {
83  mgr.registerChecker<CallDumper>();
84}
85