clang  15.0.0git
AnalyzerStatsChecker.cpp
Go to the documentation of this file.
1 //==--AnalyzerStatsChecker.cpp - Analyzer visitation statistics --*- C++ -*-==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 // This file reports various statistics about analyzer visitation.
9 //===----------------------------------------------------------------------===//
11 #include "clang/AST/DeclObjC.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 
23 using namespace clang;
24 using namespace ento;
25 
26 #define DEBUG_TYPE "StatsChecker"
27 
28 STATISTIC(NumBlocks,
29  "The # of blocks in top level functions");
30 STATISTIC(NumBlocksUnreachable,
31  "The # of unreachable blocks in analyzing top level functions");
32 
33 namespace {
34 class AnalyzerStatsChecker : public Checker<check::EndAnalysis> {
35 public:
36  void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,ExprEngine &Eng) const;
37 };
38 }
39 
40 void AnalyzerStatsChecker::checkEndAnalysis(ExplodedGraph &G,
41  BugReporter &B,
42  ExprEngine &Eng) const {
43  const CFG *C = nullptr;
44  const SourceManager &SM = B.getSourceManager();
46 
47  // Root node should have the location context of the top most function.
48  const ExplodedNode *GraphRoot = *G.roots_begin();
49  const LocationContext *LC = GraphRoot->getLocation().getLocationContext();
50 
51  const Decl *D = LC->getDecl();
52 
53  // Iterate over the exploded graph.
54  for (ExplodedGraph::node_iterator I = G.nodes_begin();
55  I != G.nodes_end(); ++I) {
56  const ProgramPoint &P = I->getLocation();
57 
58  // Only check the coverage in the top level function (optimization).
59  if (D != P.getLocationContext()->getDecl())
60  continue;
61 
62  if (Optional<BlockEntrance> BE = P.getAs<BlockEntrance>()) {
63  const CFGBlock *CB = BE->getBlock();
64  reachable.insert(CB);
65  }
66  }
67 
68  // Get the CFG and the Decl of this block.
69  C = LC->getCFG();
70 
71  unsigned total = 0, unreachable = 0;
72 
73  // Find CFGBlocks that were not covered by any node
74  for (CFG::const_iterator I = C->begin(); I != C->end(); ++I) {
75  const CFGBlock *CB = *I;
76  ++total;
77  // Check if the block is unreachable
78  if (!reachable.count(CB)) {
79  ++unreachable;
80  }
81  }
82 
83  // We never 'reach' the entry block, so correct the unreachable count
84  unreachable--;
85  // There is no BlockEntrance corresponding to the exit block as well, so
86  // assume it is reached as well.
87  unreachable--;
88 
89  // Generate the warning string
90  SmallString<128> buf;
91  llvm::raw_svector_ostream output(buf);
92  PresumedLoc Loc = SM.getPresumedLoc(D->getLocation());
93  if (!Loc.isValid())
94  return;
95 
96  if (isa<FunctionDecl, ObjCMethodDecl>(D)) {
97  const NamedDecl *ND = cast<NamedDecl>(D);
98  output << *ND;
99  } else if (isa<BlockDecl>(D)) {
100  output << "block(line:" << Loc.getLine() << ":col:" << Loc.getColumn();
101  }
102 
103  NumBlocksUnreachable += unreachable;
104  NumBlocks += total;
105  std::string NameOfRootFunction = std::string(output.str());
106 
107  output << " -> Total CFGBlocks: " << total << " | Unreachable CFGBlocks: "
108  << unreachable << " | Exhausted Block: "
109  << (Eng.wasBlocksExhausted() ? "yes" : "no")
110  << " | Empty WorkList: "
111  << (Eng.hasEmptyWorkList() ? "yes" : "no");
112 
113  B.EmitBasicReport(D, this, "Analyzer Statistics", "Internal Statistics",
114  output.str(), PathDiagnosticLocation(D, SM));
115 
116  // Emit warning for each block we bailed out on.
117  typedef CoreEngine::BlocksExhausted::const_iterator ExhaustedIterator;
118  const CoreEngine &CE = Eng.getCoreEngine();
119  for (ExhaustedIterator I = CE.blocks_exhausted_begin(),
120  E = CE.blocks_exhausted_end(); I != E; ++I) {
121  const BlockEdge &BE = I->first;
122  const CFGBlock *Exit = BE.getDst();
123  if (Exit->empty())
124  continue;
125  const CFGElement &CE = Exit->front();
126  if (Optional<CFGStmt> CS = CE.getAs<CFGStmt>()) {
127  SmallString<128> bufI;
128  llvm::raw_svector_ostream outputI(bufI);
129  outputI << "(" << NameOfRootFunction << ")" <<
130  ": The analyzer generated a sink at this point";
131  B.EmitBasicReport(
132  D, this, "Sink Point", "Internal Statistics", outputI.str(),
133  PathDiagnosticLocation::createBegin(CS->getStmt(), SM, LC));
134  }
135  }
136 }
137 
138 void ento::registerAnalyzerStatsChecker(CheckerManager &mgr) {
139  mgr.registerChecker<AnalyzerStatsChecker>();
140 }
141 
142 bool ento::shouldRegisterAnalyzerStatsChecker(const CheckerManager &mgr) {
143  return true;
144 }
STATISTIC
STATISTIC(NumBlocks, "The # of blocks in top level functions")
clang::LocationContext
It wraps the AnalysisDeclContext to represent both the call stack with the help of StackFrameContext ...
Definition: AnalysisDeclContext.h:215
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::CFGBlock::empty
bool empty() const
Definition: CFG.h:918
clang::PresumedLoc::getLine
unsigned getLine() const
Return the presumed line number of this location.
Definition: SourceLocation.h:337
clang::CFGBlock::front
CFGElement front() const
Definition: CFG.h:872
clang::NamedDecl
This represents a decl that may have a name.
Definition: Decl.h:247
AttributeLangSupport::C
@ C
Definition: SemaDeclAttr.cpp:55
llvm::Optional
Definition: LLVM.h:40
llvm::SmallPtrSet
Definition: ASTContext.h:82
SourceManager.h
clang::CFG
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt.
Definition: CFG.h:1225
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:627
DeclObjC.h
clang::CFGBlock
Represents a single basic block in a source-level CFG.
Definition: CFG.h:576
BuiltinCheckerRegistration.h
CheckerManager.h
BugReporter.h
llvm::SmallString< 128 >
clang::PresumedLoc::getColumn
unsigned getColumn() const
Return the presumed column number of this location.
Definition: SourceLocation.h:345
clang::BlockEdge::getDst
const CFGBlock * getDst() const
Definition: ProgramPoint.h:514
ExplodedGraph.h
clang::ento::ExplodedGraph::node_iterator
AllNodesTy::iterator node_iterator
Definition: ExplodedGraph.h:395
clang::BlockEntrance
Definition: ProgramPoint.h:224
P
StringRef P
Definition: ASTMatchersInternal.cpp:563
clang::CFGStmt
Definition: CFG.h:132
clang::CFGElement::getAs
Optional< T > getAs() const
Convert to the specified CFGElement type, returning None if this CFGElement is not of the desired typ...
Definition: CFG.h:109
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:83
clang::CFGElement
Represents a top-level expression in a basic block.
Definition: CFG.h:55
Checker.h
ExprEngine.h
clang::PresumedLoc::isValid
bool isValid() const
Definition: SourceLocation.h:319
clang
Definition: CalledOnceCheck.h:17
clang::ento::PathDiagnosticLocation::createBegin
static PathDiagnosticLocation createBegin(const Decl *D, const SourceManager &SM)
Create a location for the beginning of the declaration.
Definition: PathDiagnostic.cpp:580
clang::BlockEdge
Definition: ProgramPoint.h:502
clang::LocationContext::getCFG
CFG * getCFG() const
Definition: AnalysisDeclContext.h:253
clang::PresumedLoc
Represents an unpacked "presumed" location which can be presented to the user.
Definition: SourceLocation.h:302
SM
#define SM(sm)
Definition: Cuda.cpp:81
clang::Decl::getLocation
SourceLocation getLocation() const
Definition: DeclBase.h:432
clang::LocationContext::getDecl
const Decl * getDecl() const
Definition: AnalysisDeclContext.h:251
clang::ProgramPoint
Definition: ProgramPoint.h:58