xref: /freebsd-src/contrib/llvm-project/clang/lib/StaticAnalyzer/Checkers/TraversalChecker.cpp (revision e25152834cdf3b353892835a4f3b157e066a8ed4)
10b57cec5SDimitry Andric //== TraversalChecker.cpp -------------------------------------- -*- C++ -*--=//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // These checkers print various aspects of the ExprEngine's traversal of the CFG
100b57cec5SDimitry Andric // as it builds the ExplodedGraph.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
140b57cec5SDimitry Andric #include "clang/AST/ParentMap.h"
150b57cec5SDimitry Andric #include "clang/AST/StmtObjC.h"
160b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/Checker.h"
170b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/CheckerManager.h"
180b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
190b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
200b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric using namespace clang;
230b57cec5SDimitry Andric using namespace ento;
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric namespace {
260b57cec5SDimitry Andric class TraversalDumper : public Checker< check::BranchCondition,
270b57cec5SDimitry Andric                                         check::BeginFunction,
280b57cec5SDimitry Andric                                         check::EndFunction > {
290b57cec5SDimitry Andric public:
300b57cec5SDimitry Andric   void checkBranchCondition(const Stmt *Condition, CheckerContext &C) const;
310b57cec5SDimitry Andric   void checkBeginFunction(CheckerContext &C) const;
320b57cec5SDimitry Andric   void checkEndFunction(const ReturnStmt *RS, CheckerContext &C) const;
330b57cec5SDimitry Andric };
340b57cec5SDimitry Andric }
350b57cec5SDimitry Andric 
checkBranchCondition(const Stmt * Condition,CheckerContext & C) const360b57cec5SDimitry Andric void TraversalDumper::checkBranchCondition(const Stmt *Condition,
370b57cec5SDimitry Andric                                            CheckerContext &C) const {
380b57cec5SDimitry Andric   // Special-case Objective-C's for-in loop, which uses the entire loop as its
390b57cec5SDimitry Andric   // condition. We just print the collection expression.
400b57cec5SDimitry Andric   const Stmt *Parent = dyn_cast<ObjCForCollectionStmt>(Condition);
410b57cec5SDimitry Andric   if (!Parent) {
420b57cec5SDimitry Andric     const ParentMap &Parents = C.getLocationContext()->getParentMap();
430b57cec5SDimitry Andric     Parent = Parents.getParent(Condition);
440b57cec5SDimitry Andric   }
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric   // It is mildly evil to print directly to llvm::outs() rather than emitting
470b57cec5SDimitry Andric   // warnings, but this ensures things do not get filtered out by the rest of
480b57cec5SDimitry Andric   // the static analyzer machinery.
490b57cec5SDimitry Andric   SourceLocation Loc = Parent->getBeginLoc();
500b57cec5SDimitry Andric   llvm::outs() << C.getSourceManager().getSpellingLineNumber(Loc) << " "
510b57cec5SDimitry Andric                << Parent->getStmtClassName() << "\n";
520b57cec5SDimitry Andric }
530b57cec5SDimitry Andric 
checkBeginFunction(CheckerContext & C) const540b57cec5SDimitry Andric void TraversalDumper::checkBeginFunction(CheckerContext &C) const {
550b57cec5SDimitry Andric   llvm::outs() << "--BEGIN FUNCTION--\n";
560b57cec5SDimitry Andric }
570b57cec5SDimitry Andric 
checkEndFunction(const ReturnStmt * RS,CheckerContext & C) const580b57cec5SDimitry Andric void TraversalDumper::checkEndFunction(const ReturnStmt *RS,
590b57cec5SDimitry Andric                                        CheckerContext &C) const {
600b57cec5SDimitry Andric   llvm::outs() << "--END FUNCTION--\n";
610b57cec5SDimitry Andric }
620b57cec5SDimitry Andric 
registerTraversalDumper(CheckerManager & mgr)630b57cec5SDimitry Andric void ento::registerTraversalDumper(CheckerManager &mgr) {
640b57cec5SDimitry Andric   mgr.registerChecker<TraversalDumper>();
650b57cec5SDimitry Andric }
660b57cec5SDimitry Andric 
shouldRegisterTraversalDumper(const CheckerManager & mgr)67*5ffd83dbSDimitry Andric bool ento::shouldRegisterTraversalDumper(const CheckerManager &mgr) {
680b57cec5SDimitry Andric   return true;
690b57cec5SDimitry Andric }
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric //------------------------------------------------------------------------------
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric namespace {
740b57cec5SDimitry Andric class CallDumper : public Checker< check::PreCall,
750b57cec5SDimitry Andric                                    check::PostCall > {
760b57cec5SDimitry Andric public:
770b57cec5SDimitry Andric   void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
780b57cec5SDimitry Andric   void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
790b57cec5SDimitry Andric };
800b57cec5SDimitry Andric }
810b57cec5SDimitry Andric 
checkPreCall(const CallEvent & Call,CheckerContext & C) const820b57cec5SDimitry Andric void CallDumper::checkPreCall(const CallEvent &Call, CheckerContext &C) const {
830b57cec5SDimitry Andric   unsigned Indentation = 0;
840b57cec5SDimitry Andric   for (const LocationContext *LC = C.getLocationContext()->getParent();
850b57cec5SDimitry Andric        LC != nullptr; LC = LC->getParent())
860b57cec5SDimitry Andric     ++Indentation;
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric   // It is mildly evil to print directly to llvm::outs() rather than emitting
890b57cec5SDimitry Andric   // warnings, but this ensures things do not get filtered out by the rest of
900b57cec5SDimitry Andric   // the static analyzer machinery.
910b57cec5SDimitry Andric   llvm::outs().indent(Indentation);
920b57cec5SDimitry Andric   Call.dump(llvm::outs());
930b57cec5SDimitry Andric }
940b57cec5SDimitry Andric 
checkPostCall(const CallEvent & Call,CheckerContext & C) const950b57cec5SDimitry Andric void CallDumper::checkPostCall(const CallEvent &Call, CheckerContext &C) const {
960b57cec5SDimitry Andric   const Expr *CallE = Call.getOriginExpr();
970b57cec5SDimitry Andric   if (!CallE)
980b57cec5SDimitry Andric     return;
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric   unsigned Indentation = 0;
1010b57cec5SDimitry Andric   for (const LocationContext *LC = C.getLocationContext()->getParent();
1020b57cec5SDimitry Andric        LC != nullptr; LC = LC->getParent())
1030b57cec5SDimitry Andric     ++Indentation;
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric   // It is mildly evil to print directly to llvm::outs() rather than emitting
1060b57cec5SDimitry Andric   // warnings, but this ensures things do not get filtered out by the rest of
1070b57cec5SDimitry Andric   // the static analyzer machinery.
1080b57cec5SDimitry Andric   llvm::outs().indent(Indentation);
1090b57cec5SDimitry Andric   if (Call.getResultType()->isVoidType())
1100b57cec5SDimitry Andric     llvm::outs() << "Returning void\n";
1110b57cec5SDimitry Andric   else
1120b57cec5SDimitry Andric     llvm::outs() << "Returning " << C.getSVal(CallE) << "\n";
1130b57cec5SDimitry Andric }
1140b57cec5SDimitry Andric 
registerCallDumper(CheckerManager & mgr)1150b57cec5SDimitry Andric void ento::registerCallDumper(CheckerManager &mgr) {
1160b57cec5SDimitry Andric   mgr.registerChecker<CallDumper>();
1170b57cec5SDimitry Andric }
1180b57cec5SDimitry Andric 
shouldRegisterCallDumper(const CheckerManager & mgr)119*5ffd83dbSDimitry Andric bool ento::shouldRegisterCallDumper(const CheckerManager &mgr) {
1200b57cec5SDimitry Andric   return true;
1210b57cec5SDimitry Andric }
122