Path: blob/main/contrib/llvm-project/clang/lib/StaticAnalyzer/Checkers/AnalyzerStatsChecker.cpp
35266 views
//==--AnalyzerStatsChecker.cpp - Analyzer visitation statistics --*- C++ -*-==//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7// This file reports various statistics about analyzer visitation.8//===----------------------------------------------------------------------===//9#include "clang/AST/DeclObjC.h"10#include "clang/Basic/SourceManager.h"11#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"12#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"13#include "clang/StaticAnalyzer/Core/Checker.h"14#include "clang/StaticAnalyzer/Core/CheckerManager.h"15#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"16#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"17#include "llvm/ADT/STLExtras.h"18#include "llvm/ADT/SmallPtrSet.h"19#include "llvm/ADT/SmallString.h"20#include "llvm/ADT/Statistic.h"21#include "llvm/Support/raw_ostream.h"22#include <optional>2324using namespace clang;25using namespace ento;2627#define DEBUG_TYPE "StatsChecker"2829STATISTIC(NumBlocks,30"The # of blocks in top level functions");31STATISTIC(NumBlocksUnreachable,32"The # of unreachable blocks in analyzing top level functions");3334namespace {35class AnalyzerStatsChecker : public Checker<check::EndAnalysis> {36public:37void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,ExprEngine &Eng) const;38};39}4041void AnalyzerStatsChecker::checkEndAnalysis(ExplodedGraph &G,42BugReporter &B,43ExprEngine &Eng) const {44const CFG *C = nullptr;45const SourceManager &SM = B.getSourceManager();46llvm::SmallPtrSet<const CFGBlock*, 32> reachable;4748// Root node should have the location context of the top most function.49const ExplodedNode *GraphRoot = *G.roots_begin();50const LocationContext *LC = GraphRoot->getLocation().getLocationContext();5152const Decl *D = LC->getDecl();5354// Iterate over the exploded graph.55for (const ExplodedNode &N : G.nodes()) {56const ProgramPoint &P = N.getLocation();5758// Only check the coverage in the top level function (optimization).59if (D != P.getLocationContext()->getDecl())60continue;6162if (std::optional<BlockEntrance> BE = P.getAs<BlockEntrance>()) {63const CFGBlock *CB = BE->getBlock();64reachable.insert(CB);65}66}6768// Get the CFG and the Decl of this block.69C = LC->getCFG();7071unsigned total = 0, unreachable = 0;7273// Find CFGBlocks that were not covered by any node74for (CFG::const_iterator I = C->begin(); I != C->end(); ++I) {75const CFGBlock *CB = *I;76++total;77// Check if the block is unreachable78if (!reachable.count(CB)) {79++unreachable;80}81}8283// We never 'reach' the entry block, so correct the unreachable count84unreachable--;85// There is no BlockEntrance corresponding to the exit block as well, so86// assume it is reached as well.87unreachable--;8889// Generate the warning string90SmallString<128> buf;91llvm::raw_svector_ostream output(buf);92PresumedLoc Loc = SM.getPresumedLoc(D->getLocation());93if (!Loc.isValid())94return;9596if (isa<FunctionDecl, ObjCMethodDecl>(D)) {97const NamedDecl *ND = cast<NamedDecl>(D);98output << *ND;99} else if (isa<BlockDecl>(D)) {100output << "block(line:" << Loc.getLine() << ":col:" << Loc.getColumn();101}102103NumBlocksUnreachable += unreachable;104NumBlocks += total;105std::string NameOfRootFunction = std::string(output.str());106107output << " -> Total CFGBlocks: " << total << " | Unreachable CFGBlocks: "108<< unreachable << " | Exhausted Block: "109<< (Eng.wasBlocksExhausted() ? "yes" : "no")110<< " | Empty WorkList: "111<< (Eng.hasEmptyWorkList() ? "yes" : "no");112113B.EmitBasicReport(D, this, "Analyzer Statistics", "Internal Statistics",114output.str(), PathDiagnosticLocation(D, SM));115116// Emit warning for each block we bailed out on.117const CoreEngine &CE = Eng.getCoreEngine();118for (const BlockEdge &BE : make_first_range(CE.exhausted_blocks())) {119const CFGBlock *Exit = BE.getDst();120if (Exit->empty())121continue;122const CFGElement &CE = Exit->front();123if (std::optional<CFGStmt> CS = CE.getAs<CFGStmt>()) {124SmallString<128> bufI;125llvm::raw_svector_ostream outputI(bufI);126outputI << "(" << NameOfRootFunction << ")" <<127": The analyzer generated a sink at this point";128B.EmitBasicReport(129D, this, "Sink Point", "Internal Statistics", outputI.str(),130PathDiagnosticLocation::createBegin(CS->getStmt(), SM, LC));131}132}133}134135void ento::registerAnalyzerStatsChecker(CheckerManager &mgr) {136mgr.registerChecker<AnalyzerStatsChecker>();137}138139bool ento::shouldRegisterAnalyzerStatsChecker(const CheckerManager &mgr) {140return true;141}142143144