[analyzer] Refactoring: include/clang/Checker -> include/clang/GR
[clang.git] / lib / Checker / AnalyzerStatsChecker.cpp
blob2a229cc7e9c1cd54de582ce1eb5b91dd421bb497
1 //==--AnalyzerStatsChecker.cpp - Analyzer visitation statistics --*- C++ -*-==//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
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;
21 namespace {
22 class AnalyzerStatsChecker : public CheckerVisitor<AnalyzerStatsChecker> {
23 public:
24 static void *getTag();
25 void VisitEndAnalysis(ExplodedGraph &G, BugReporter &B, GRExprEngine &Eng);
27 private:
28 llvm::SmallPtrSet<const CFGBlock*, 256> reachable;
32 void *AnalyzerStatsChecker::getTag() {
33 static int x = 0;
34 return &x;
37 void clang::RegisterAnalyzerStatsChecker(GRExprEngine &Eng) {
38 Eng.registerCheck(new AnalyzerStatsChecker());
41 void AnalyzerStatsChecker::VisitEndAnalysis(ExplodedGraph &G,
42 BugReporter &B,
43 GRExprEngine &Eng) {
44 const CFG *C = 0;
45 const Decl *D = 0;
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
54 if (!LC)
55 LC = P.getLocationContext();
57 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
58 const CFGBlock *CB = BE->getBlock();
59 reachable.insert(CB);
63 // Get the CFG and the Decl of this block
64 C = LC->getCFG();
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;
72 ++total;
73 // Check if the block is unreachable
74 if (!reachable.count(CB)) {
75 ++unreachable;
79 // We never 'reach' the entry block, so correct the unreachable count
80 unreachable--;
82 // Generate the warning string
83 llvm::SmallString<128> buf;
84 llvm::raw_svector_ostream output(buf);
85 PresumedLoc Loc = SM.getPresumedLoc(D->getLocation());
86 if (Loc.isValid()) {
87 output << Loc.getFilename() << " : ";
89 if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
90 const NamedDecl *ND = cast<NamedDecl>(D);
91 output << ND;
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(),
105 D->getLocation());
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());