1*0b57cec5SDimitry Andric //== TraversalChecker.cpp -------------------------------------- -*- C++ -*--=// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // These checkers print various aspects of the ExprEngine's traversal of the CFG 10*0b57cec5SDimitry Andric // as it builds the ExplodedGraph. 11*0b57cec5SDimitry Andric // 12*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 13*0b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" 14*0b57cec5SDimitry Andric #include "clang/AST/ParentMap.h" 15*0b57cec5SDimitry Andric #include "clang/AST/StmtObjC.h" 16*0b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/Checker.h" 17*0b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/CheckerManager.h" 18*0b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" 19*0b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" 20*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 21*0b57cec5SDimitry Andric 22*0b57cec5SDimitry Andric using namespace clang; 23*0b57cec5SDimitry Andric using namespace ento; 24*0b57cec5SDimitry Andric 25*0b57cec5SDimitry Andric namespace { 26*0b57cec5SDimitry Andric class TraversalDumper : public Checker< check::BranchCondition, 27*0b57cec5SDimitry Andric check::BeginFunction, 28*0b57cec5SDimitry Andric check::EndFunction > { 29*0b57cec5SDimitry Andric public: 30*0b57cec5SDimitry Andric void checkBranchCondition(const Stmt *Condition, CheckerContext &C) const; 31*0b57cec5SDimitry Andric void checkBeginFunction(CheckerContext &C) const; 32*0b57cec5SDimitry Andric void checkEndFunction(const ReturnStmt *RS, CheckerContext &C) const; 33*0b57cec5SDimitry Andric }; 34*0b57cec5SDimitry Andric } 35*0b57cec5SDimitry Andric 36*0b57cec5SDimitry Andric void TraversalDumper::checkBranchCondition(const Stmt *Condition, 37*0b57cec5SDimitry Andric CheckerContext &C) const { 38*0b57cec5SDimitry Andric // Special-case Objective-C's for-in loop, which uses the entire loop as its 39*0b57cec5SDimitry Andric // condition. We just print the collection expression. 40*0b57cec5SDimitry Andric const Stmt *Parent = dyn_cast<ObjCForCollectionStmt>(Condition); 41*0b57cec5SDimitry Andric if (!Parent) { 42*0b57cec5SDimitry Andric const ParentMap &Parents = C.getLocationContext()->getParentMap(); 43*0b57cec5SDimitry Andric Parent = Parents.getParent(Condition); 44*0b57cec5SDimitry Andric } 45*0b57cec5SDimitry Andric 46*0b57cec5SDimitry Andric // It is mildly evil to print directly to llvm::outs() rather than emitting 47*0b57cec5SDimitry Andric // warnings, but this ensures things do not get filtered out by the rest of 48*0b57cec5SDimitry Andric // the static analyzer machinery. 49*0b57cec5SDimitry Andric SourceLocation Loc = Parent->getBeginLoc(); 50*0b57cec5SDimitry Andric llvm::outs() << C.getSourceManager().getSpellingLineNumber(Loc) << " " 51*0b57cec5SDimitry Andric << Parent->getStmtClassName() << "\n"; 52*0b57cec5SDimitry Andric } 53*0b57cec5SDimitry Andric 54*0b57cec5SDimitry Andric void TraversalDumper::checkBeginFunction(CheckerContext &C) const { 55*0b57cec5SDimitry Andric llvm::outs() << "--BEGIN FUNCTION--\n"; 56*0b57cec5SDimitry Andric } 57*0b57cec5SDimitry Andric 58*0b57cec5SDimitry Andric void TraversalDumper::checkEndFunction(const ReturnStmt *RS, 59*0b57cec5SDimitry Andric CheckerContext &C) const { 60*0b57cec5SDimitry Andric llvm::outs() << "--END FUNCTION--\n"; 61*0b57cec5SDimitry Andric } 62*0b57cec5SDimitry Andric 63*0b57cec5SDimitry Andric void ento::registerTraversalDumper(CheckerManager &mgr) { 64*0b57cec5SDimitry Andric mgr.registerChecker<TraversalDumper>(); 65*0b57cec5SDimitry Andric } 66*0b57cec5SDimitry Andric 67*0b57cec5SDimitry Andric bool ento::shouldRegisterTraversalDumper(const LangOptions &LO) { 68*0b57cec5SDimitry Andric return true; 69*0b57cec5SDimitry Andric } 70*0b57cec5SDimitry Andric 71*0b57cec5SDimitry Andric //------------------------------------------------------------------------------ 72*0b57cec5SDimitry Andric 73*0b57cec5SDimitry Andric namespace { 74*0b57cec5SDimitry Andric class CallDumper : public Checker< check::PreCall, 75*0b57cec5SDimitry Andric check::PostCall > { 76*0b57cec5SDimitry Andric public: 77*0b57cec5SDimitry Andric void checkPreCall(const CallEvent &Call, CheckerContext &C) const; 78*0b57cec5SDimitry Andric void checkPostCall(const CallEvent &Call, CheckerContext &C) const; 79*0b57cec5SDimitry Andric }; 80*0b57cec5SDimitry Andric } 81*0b57cec5SDimitry Andric 82*0b57cec5SDimitry Andric void CallDumper::checkPreCall(const CallEvent &Call, CheckerContext &C) const { 83*0b57cec5SDimitry Andric unsigned Indentation = 0; 84*0b57cec5SDimitry Andric for (const LocationContext *LC = C.getLocationContext()->getParent(); 85*0b57cec5SDimitry Andric LC != nullptr; LC = LC->getParent()) 86*0b57cec5SDimitry Andric ++Indentation; 87*0b57cec5SDimitry Andric 88*0b57cec5SDimitry Andric // It is mildly evil to print directly to llvm::outs() rather than emitting 89*0b57cec5SDimitry Andric // warnings, but this ensures things do not get filtered out by the rest of 90*0b57cec5SDimitry Andric // the static analyzer machinery. 91*0b57cec5SDimitry Andric llvm::outs().indent(Indentation); 92*0b57cec5SDimitry Andric Call.dump(llvm::outs()); 93*0b57cec5SDimitry Andric } 94*0b57cec5SDimitry Andric 95*0b57cec5SDimitry Andric void CallDumper::checkPostCall(const CallEvent &Call, CheckerContext &C) const { 96*0b57cec5SDimitry Andric const Expr *CallE = Call.getOriginExpr(); 97*0b57cec5SDimitry Andric if (!CallE) 98*0b57cec5SDimitry Andric return; 99*0b57cec5SDimitry Andric 100*0b57cec5SDimitry Andric unsigned Indentation = 0; 101*0b57cec5SDimitry Andric for (const LocationContext *LC = C.getLocationContext()->getParent(); 102*0b57cec5SDimitry Andric LC != nullptr; LC = LC->getParent()) 103*0b57cec5SDimitry Andric ++Indentation; 104*0b57cec5SDimitry Andric 105*0b57cec5SDimitry Andric // It is mildly evil to print directly to llvm::outs() rather than emitting 106*0b57cec5SDimitry Andric // warnings, but this ensures things do not get filtered out by the rest of 107*0b57cec5SDimitry Andric // the static analyzer machinery. 108*0b57cec5SDimitry Andric llvm::outs().indent(Indentation); 109*0b57cec5SDimitry Andric if (Call.getResultType()->isVoidType()) 110*0b57cec5SDimitry Andric llvm::outs() << "Returning void\n"; 111*0b57cec5SDimitry Andric else 112*0b57cec5SDimitry Andric llvm::outs() << "Returning " << C.getSVal(CallE) << "\n"; 113*0b57cec5SDimitry Andric } 114*0b57cec5SDimitry Andric 115*0b57cec5SDimitry Andric void ento::registerCallDumper(CheckerManager &mgr) { 116*0b57cec5SDimitry Andric mgr.registerChecker<CallDumper>(); 117*0b57cec5SDimitry Andric } 118*0b57cec5SDimitry Andric 119*0b57cec5SDimitry Andric bool ento::shouldRegisterCallDumper(const LangOptions &LO) { 120*0b57cec5SDimitry Andric return true; 121*0b57cec5SDimitry Andric } 122