1 //==--AnalyzerStatsChecker.cpp - Analyzer visitation statistics --*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 // This file reports various statistics about analyzer visitation.
10 //===----------------------------------------------------------------------===//
12 #include "clang/GR/PathSensitive/CheckerVisitor.h"
13 #include "clang/GR/PathSensitive/ExplodedGraph.h"
14 #include "clang/GR/BugReporter/BugReporter.h"
15 #include "GRExprEngineExperimentalChecks.h"
16 #include "clang/Basic/SourceManager.h"
17 #include "llvm/ADT/SmallPtrSet.h"
19 using namespace clang
;
22 class AnalyzerStatsChecker
: public CheckerVisitor
<AnalyzerStatsChecker
> {
24 static void *getTag();
25 void VisitEndAnalysis(ExplodedGraph
&G
, BugReporter
&B
, GRExprEngine
&Eng
);
28 llvm::SmallPtrSet
<const CFGBlock
*, 256> reachable
;
32 void *AnalyzerStatsChecker::getTag() {
37 void clang::RegisterAnalyzerStatsChecker(GRExprEngine
&Eng
) {
38 Eng
.registerCheck(new AnalyzerStatsChecker());
41 void AnalyzerStatsChecker::VisitEndAnalysis(ExplodedGraph
&G
,
46 const LocationContext
*LC
= 0;
47 const SourceManager
&SM
= B
.getSourceManager();
49 // Iterate over explodedgraph
50 for (ExplodedGraph::node_iterator I
= G
.nodes_begin();
51 I
!= G
.nodes_end(); ++I
) {
52 const ProgramPoint
&P
= I
->getLocation();
53 // Save the LocationContext if we don't have it already
55 LC
= P
.getLocationContext();
57 if (const BlockEntrance
*BE
= dyn_cast
<BlockEntrance
>(&P
)) {
58 const CFGBlock
*CB
= BE
->getBlock();
63 // Get the CFG and the Decl of this block
65 D
= LC
->getAnalysisContext()->getDecl();
67 unsigned total
= 0, unreachable
= 0;
69 // Find CFGBlocks that were not covered by any node
70 for (CFG::const_iterator I
= C
->begin(); I
!= C
->end(); ++I
) {
71 const CFGBlock
*CB
= *I
;
73 // Check if the block is unreachable
74 if (!reachable
.count(CB
)) {
79 // We never 'reach' the entry block, so correct the unreachable count
82 // Generate the warning string
83 llvm::SmallString
<128> buf
;
84 llvm::raw_svector_ostream
output(buf
);
85 PresumedLoc Loc
= SM
.getPresumedLoc(D
->getLocation());
87 output
<< Loc
.getFilename() << " : ";
89 if (isa
<FunctionDecl
>(D
) || isa
<ObjCMethodDecl
>(D
)) {
90 const NamedDecl
*ND
= cast
<NamedDecl
>(D
);
93 else if (isa
<BlockDecl
>(D
)) {
94 output
<< "block(line:" << Loc
.getLine() << ":col:" << Loc
.getColumn();
98 output
<< " -> Total CFGBlocks: " << total
<< " | Unreachable CFGBlocks: "
99 << unreachable
<< " | Aborted Block: "
100 << (Eng
.wasBlockAborted() ? "yes" : "no")
101 << " | Empty WorkList: "
102 << (Eng
.hasEmptyWorkList() ? "yes" : "no");
104 B
.EmitBasicReport("Analyzer Statistics", "Internal Statistics", output
.str(),
107 // Emit warning for each block we bailed out on
108 typedef GRCoreEngine::BlocksAborted::const_iterator AbortedIterator
;
109 const GRCoreEngine
&CE
= Eng
.getCoreEngine();
110 for (AbortedIterator I
= CE
.blocks_aborted_begin(),
111 E
= CE
.blocks_aborted_end(); I
!= E
; ++I
) {
112 const BlockEdge
&BE
= I
->first
;
113 const CFGBlock
*Exit
= BE
.getDst();
114 const CFGElement
&CE
= Exit
->front();
115 if (const CFGStmt
*CS
= dyn_cast
<CFGStmt
>(&CE
))
116 B
.EmitBasicReport("Bailout Point", "Internal Statistics", "The analyzer "
117 "stopped analyzing at this point", CS
->getStmt()->getLocStart());