AnalyzerStatsChecker.cpp revision 749bbe6f5f23676244f12a0d41511c8e73516feb
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//===----------------------------------------------------------------------===//
11#define DEBUG_TYPE "StatsChecker"
12
13#include "ClangSACheckers.h"
14#include "clang/StaticAnalyzer/Core/Checker.h"
15#include "clang/StaticAnalyzer/Core/CheckerManager.h"
16#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
17#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
18#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
19
20#include "clang/AST/DeclObjC.h"
21#include "clang/Basic/SourceManager.h"
22#include "llvm/ADT/SmallPtrSet.h"
23#include "llvm/ADT/SmallString.h"
24#include "llvm/ADT/Statistic.h"
25
26using namespace clang;
27using namespace ento;
28
29STATISTIC(NumBlocks,
30          "The # of blocks in top level functions");
31STATISTIC(NumBlocksUnreachable,
32          "The # of unreachable blocks in analyzing top level functions");
33
34namespace {
35class AnalyzerStatsChecker : public Checker<check::EndAnalysis> {
36public:
37  void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,ExprEngine &Eng) const;
38};
39}
40
41void AnalyzerStatsChecker::checkEndAnalysis(ExplodedGraph &G,
42                                            BugReporter &B,
43                                            ExprEngine &Eng) const {
44  const CFG *C  = 0;
45  const Decl *D = 0;
46  const SourceManager &SM = B.getSourceManager();
47  llvm::SmallPtrSet<const CFGBlock*, 256> reachable;
48
49  // Root node should have the location context of the top most function.
50  const ExplodedNode *GraphRoot = *G.roots_begin();
51  const LocationContext *LC = GraphRoot->getLocation().getLocationContext();
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.
59    if (LC != P.getLocationContext())
60      continue;
61
62    if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
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  D = LC->getAnalysisDeclContext()->getDecl();
71
72  unsigned total = 0, unreachable = 0;
73
74  // Find CFGBlocks that were not covered by any node
75  for (CFG::const_iterator I = C->begin(); I != C->end(); ++I) {
76    const CFGBlock *CB = *I;
77    ++total;
78    // Check if the block is unreachable
79    if (!reachable.count(CB)) {
80      ++unreachable;
81    }
82  }
83
84  // We never 'reach' the entry block, so correct the unreachable count
85  unreachable--;
86  // There is no BlockEntrance corresponding to the exit block as well, so
87  // assume it is reached as well.
88  unreachable--;
89
90  // Generate the warning string
91  SmallString<128> buf;
92  llvm::raw_svector_ostream output(buf);
93  PresumedLoc Loc = SM.getPresumedLoc(D->getLocation());
94  if (Loc.isValid()) {
95    output << Loc.getFilename() << " : ";
96
97    if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
98      const NamedDecl *ND = cast<NamedDecl>(D);
99      output << *ND;
100    }
101    else if (isa<BlockDecl>(D)) {
102      output << "block(line:" << Loc.getLine() << ":col:" << Loc.getColumn();
103    }
104  }
105
106  NumBlocksUnreachable += unreachable;
107  NumBlocks += total;
108
109  output << " -> Total CFGBlocks: " << total << " | Unreachable CFGBlocks: "
110      << unreachable << " | Exhausted Block: "
111      << (Eng.wasBlocksExhausted() ? "yes" : "no")
112      << " | Empty WorkList: "
113      << (Eng.hasEmptyWorkList() ? "yes" : "no");
114
115  B.EmitBasicReport("Analyzer Statistics", "Internal Statistics", output.str(),
116      PathDiagnosticLocation(D, SM));
117
118  // Emit warning for each block we bailed out on
119  typedef CoreEngine::BlocksExhausted::const_iterator ExhaustedIterator;
120  const CoreEngine &CE = Eng.getCoreEngine();
121  for (ExhaustedIterator I = CE.blocks_exhausted_begin(),
122      E = CE.blocks_exhausted_end(); I != E; ++I) {
123    const BlockEdge &BE =  I->first;
124    const CFGBlock *Exit = BE.getDst();
125    const CFGElement &CE = Exit->front();
126    if (const CFGStmt *CS = dyn_cast<CFGStmt>(&CE))
127      B.EmitBasicReport("Bailout Point", "Internal Statistics", "The analyzer "
128          "stopped analyzing at this point",
129          PathDiagnosticLocation::createBegin(CS->getStmt(), SM, LC));
130  }
131}
132
133void ento::registerAnalyzerStatsChecker(CheckerManager &mgr) {
134  mgr.registerChecker<AnalyzerStatsChecker>();
135}
136