xref: /netbsd-src/external/apache2/llvm/dist/clang/lib/StaticAnalyzer/Checkers/AnalyzerStatsChecker.cpp (revision e038c9c4676b0f19b1b7dd08a940c6ed64a6d5ae)
17330f729Sjoerg //==--AnalyzerStatsChecker.cpp - Analyzer visitation statistics --*- C++ -*-==//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg // This file reports various statistics about analyzer visitation.
97330f729Sjoerg //===----------------------------------------------------------------------===//
107330f729Sjoerg #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
117330f729Sjoerg #include "clang/AST/DeclObjC.h"
127330f729Sjoerg #include "clang/Basic/SourceManager.h"
137330f729Sjoerg #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
147330f729Sjoerg #include "clang/StaticAnalyzer/Core/Checker.h"
157330f729Sjoerg #include "clang/StaticAnalyzer/Core/CheckerManager.h"
167330f729Sjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
177330f729Sjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
187330f729Sjoerg #include "llvm/ADT/SmallPtrSet.h"
197330f729Sjoerg #include "llvm/ADT/SmallString.h"
207330f729Sjoerg #include "llvm/ADT/Statistic.h"
217330f729Sjoerg #include "llvm/Support/raw_ostream.h"
227330f729Sjoerg 
237330f729Sjoerg using namespace clang;
247330f729Sjoerg using namespace ento;
257330f729Sjoerg 
267330f729Sjoerg #define DEBUG_TYPE "StatsChecker"
277330f729Sjoerg 
287330f729Sjoerg STATISTIC(NumBlocks,
297330f729Sjoerg           "The # of blocks in top level functions");
307330f729Sjoerg STATISTIC(NumBlocksUnreachable,
317330f729Sjoerg           "The # of unreachable blocks in analyzing top level functions");
327330f729Sjoerg 
337330f729Sjoerg namespace {
347330f729Sjoerg class AnalyzerStatsChecker : public Checker<check::EndAnalysis> {
357330f729Sjoerg public:
367330f729Sjoerg   void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,ExprEngine &Eng) const;
377330f729Sjoerg };
387330f729Sjoerg }
397330f729Sjoerg 
checkEndAnalysis(ExplodedGraph & G,BugReporter & B,ExprEngine & Eng) const407330f729Sjoerg void AnalyzerStatsChecker::checkEndAnalysis(ExplodedGraph &G,
417330f729Sjoerg                                             BugReporter &B,
427330f729Sjoerg                                             ExprEngine &Eng) const {
437330f729Sjoerg   const CFG *C = nullptr;
447330f729Sjoerg   const SourceManager &SM = B.getSourceManager();
457330f729Sjoerg   llvm::SmallPtrSet<const CFGBlock*, 32> reachable;
467330f729Sjoerg 
477330f729Sjoerg   // Root node should have the location context of the top most function.
487330f729Sjoerg   const ExplodedNode *GraphRoot = *G.roots_begin();
497330f729Sjoerg   const LocationContext *LC = GraphRoot->getLocation().getLocationContext();
507330f729Sjoerg 
517330f729Sjoerg   const Decl *D = LC->getDecl();
527330f729Sjoerg 
537330f729Sjoerg   // Iterate over the exploded graph.
547330f729Sjoerg   for (ExplodedGraph::node_iterator I = G.nodes_begin();
557330f729Sjoerg       I != G.nodes_end(); ++I) {
567330f729Sjoerg     const ProgramPoint &P = I->getLocation();
577330f729Sjoerg 
587330f729Sjoerg     // Only check the coverage in the top level function (optimization).
597330f729Sjoerg     if (D != P.getLocationContext()->getDecl())
607330f729Sjoerg       continue;
617330f729Sjoerg 
627330f729Sjoerg     if (Optional<BlockEntrance> BE = P.getAs<BlockEntrance>()) {
637330f729Sjoerg       const CFGBlock *CB = BE->getBlock();
647330f729Sjoerg       reachable.insert(CB);
657330f729Sjoerg     }
667330f729Sjoerg   }
677330f729Sjoerg 
687330f729Sjoerg   // Get the CFG and the Decl of this block.
697330f729Sjoerg   C = LC->getCFG();
707330f729Sjoerg 
717330f729Sjoerg   unsigned total = 0, unreachable = 0;
727330f729Sjoerg 
737330f729Sjoerg   // Find CFGBlocks that were not covered by any node
747330f729Sjoerg   for (CFG::const_iterator I = C->begin(); I != C->end(); ++I) {
757330f729Sjoerg     const CFGBlock *CB = *I;
767330f729Sjoerg     ++total;
777330f729Sjoerg     // Check if the block is unreachable
787330f729Sjoerg     if (!reachable.count(CB)) {
797330f729Sjoerg       ++unreachable;
807330f729Sjoerg     }
817330f729Sjoerg   }
827330f729Sjoerg 
837330f729Sjoerg   // We never 'reach' the entry block, so correct the unreachable count
847330f729Sjoerg   unreachable--;
857330f729Sjoerg   // There is no BlockEntrance corresponding to the exit block as well, so
867330f729Sjoerg   // assume it is reached as well.
877330f729Sjoerg   unreachable--;
887330f729Sjoerg 
897330f729Sjoerg   // Generate the warning string
907330f729Sjoerg   SmallString<128> buf;
917330f729Sjoerg   llvm::raw_svector_ostream output(buf);
927330f729Sjoerg   PresumedLoc Loc = SM.getPresumedLoc(D->getLocation());
937330f729Sjoerg   if (!Loc.isValid())
947330f729Sjoerg     return;
957330f729Sjoerg 
967330f729Sjoerg   if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
977330f729Sjoerg     const NamedDecl *ND = cast<NamedDecl>(D);
987330f729Sjoerg     output << *ND;
997330f729Sjoerg   }
1007330f729Sjoerg   else if (isa<BlockDecl>(D)) {
1017330f729Sjoerg     output << "block(line:" << Loc.getLine() << ":col:" << Loc.getColumn();
1027330f729Sjoerg   }
1037330f729Sjoerg 
1047330f729Sjoerg   NumBlocksUnreachable += unreachable;
1057330f729Sjoerg   NumBlocks += total;
106*e038c9c4Sjoerg   std::string NameOfRootFunction = std::string(output.str());
1077330f729Sjoerg 
1087330f729Sjoerg   output << " -> Total CFGBlocks: " << total << " | Unreachable CFGBlocks: "
1097330f729Sjoerg       << unreachable << " | Exhausted Block: "
1107330f729Sjoerg       << (Eng.wasBlocksExhausted() ? "yes" : "no")
1117330f729Sjoerg       << " | Empty WorkList: "
1127330f729Sjoerg       << (Eng.hasEmptyWorkList() ? "yes" : "no");
1137330f729Sjoerg 
1147330f729Sjoerg   B.EmitBasicReport(D, this, "Analyzer Statistics", "Internal Statistics",
1157330f729Sjoerg                     output.str(), PathDiagnosticLocation(D, SM));
1167330f729Sjoerg 
1177330f729Sjoerg   // Emit warning for each block we bailed out on.
1187330f729Sjoerg   typedef CoreEngine::BlocksExhausted::const_iterator ExhaustedIterator;
1197330f729Sjoerg   const CoreEngine &CE = Eng.getCoreEngine();
1207330f729Sjoerg   for (ExhaustedIterator I = CE.blocks_exhausted_begin(),
1217330f729Sjoerg       E = CE.blocks_exhausted_end(); I != E; ++I) {
1227330f729Sjoerg     const BlockEdge &BE =  I->first;
1237330f729Sjoerg     const CFGBlock *Exit = BE.getDst();
1247330f729Sjoerg     if (Exit->empty())
1257330f729Sjoerg       continue;
1267330f729Sjoerg     const CFGElement &CE = Exit->front();
1277330f729Sjoerg     if (Optional<CFGStmt> CS = CE.getAs<CFGStmt>()) {
1287330f729Sjoerg       SmallString<128> bufI;
1297330f729Sjoerg       llvm::raw_svector_ostream outputI(bufI);
1307330f729Sjoerg       outputI << "(" << NameOfRootFunction << ")" <<
1317330f729Sjoerg                  ": The analyzer generated a sink at this point";
1327330f729Sjoerg       B.EmitBasicReport(
1337330f729Sjoerg           D, this, "Sink Point", "Internal Statistics", outputI.str(),
1347330f729Sjoerg           PathDiagnosticLocation::createBegin(CS->getStmt(), SM, LC));
1357330f729Sjoerg     }
1367330f729Sjoerg   }
1377330f729Sjoerg }
1387330f729Sjoerg 
registerAnalyzerStatsChecker(CheckerManager & mgr)1397330f729Sjoerg void ento::registerAnalyzerStatsChecker(CheckerManager &mgr) {
1407330f729Sjoerg   mgr.registerChecker<AnalyzerStatsChecker>();
1417330f729Sjoerg }
1427330f729Sjoerg 
shouldRegisterAnalyzerStatsChecker(const CheckerManager & mgr)143*e038c9c4Sjoerg bool ento::shouldRegisterAnalyzerStatsChecker(const CheckerManager &mgr) {
1447330f729Sjoerg   return true;
1457330f729Sjoerg }
146