AnalyzerStatsChecker.cpp revision 9b663716449b618ba0390b1dbebc54fa8e971124
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
12#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerVisitor.h"
13#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
14#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
15
16// FIXME: Restructure checker registration.
17#include "ExperimentalChecks.h"
18
19#include "clang/Basic/SourceManager.h"
20#include "llvm/ADT/SmallPtrSet.h"
21
22using namespace clang;
23using namespace ento;
24
25namespace {
26class AnalyzerStatsChecker : public CheckerVisitor<AnalyzerStatsChecker> {
27public:
28  static void *getTag();
29  void VisitEndAnalysis(ExplodedGraph &G, BugReporter &B, ExprEngine &Eng);
30
31private:
32  llvm::SmallPtrSet<const CFGBlock*, 256> reachable;
33};
34}
35
36void *AnalyzerStatsChecker::getTag() {
37  static int x = 0;
38  return &x;
39}
40
41void ento::RegisterAnalyzerStatsChecker(ExprEngine &Eng) {
42  Eng.registerCheck(new AnalyzerStatsChecker());
43}
44
45void AnalyzerStatsChecker::VisitEndAnalysis(ExplodedGraph &G,
46                                            BugReporter &B,
47                                            ExprEngine &Eng) {
48  const CFG *C  = 0;
49  const Decl *D = 0;
50  const LocationContext *LC = 0;
51  const SourceManager &SM = B.getSourceManager();
52
53  // Iterate over explodedgraph
54  for (ExplodedGraph::node_iterator I = G.nodes_begin();
55      I != G.nodes_end(); ++I) {
56    const ProgramPoint &P = I->getLocation();
57    // Save the LocationContext if we don't have it already
58    if (!LC)
59      LC = P.getLocationContext();
60
61    if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
62      const CFGBlock *CB = BE->getBlock();
63      reachable.insert(CB);
64    }
65  }
66
67  // Get the CFG and the Decl of this block
68  C = LC->getCFG();
69  D = LC->getAnalysisContext()->getDecl();
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
86  // Generate the warning string
87  llvm::SmallString<128> buf;
88  llvm::raw_svector_ostream output(buf);
89  PresumedLoc Loc = SM.getPresumedLoc(D->getLocation());
90  if (Loc.isValid()) {
91    output << Loc.getFilename() << " : ";
92
93    if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
94      const NamedDecl *ND = cast<NamedDecl>(D);
95      output << ND;
96    }
97    else if (isa<BlockDecl>(D)) {
98      output << "block(line:" << Loc.getLine() << ":col:" << Loc.getColumn();
99    }
100  }
101
102  output << " -> Total CFGBlocks: " << total << " | Unreachable CFGBlocks: "
103      << unreachable << " | Aborted Block: "
104      << (Eng.wasBlockAborted() ? "yes" : "no")
105      << " | Empty WorkList: "
106      << (Eng.hasEmptyWorkList() ? "yes" : "no");
107
108  B.EmitBasicReport("Analyzer Statistics", "Internal Statistics", output.str(),
109      D->getLocation());
110
111  // Emit warning for each block we bailed out on
112  typedef CoreEngine::BlocksAborted::const_iterator AbortedIterator;
113  const CoreEngine &CE = Eng.getCoreEngine();
114  for (AbortedIterator I = CE.blocks_aborted_begin(),
115      E = CE.blocks_aborted_end(); I != E; ++I) {
116    const BlockEdge &BE =  I->first;
117    const CFGBlock *Exit = BE.getDst();
118    const CFGElement &CE = Exit->front();
119    if (const CFGStmt *CS = dyn_cast<CFGStmt>(&CE))
120      B.EmitBasicReport("Bailout Point", "Internal Statistics", "The analyzer "
121          "stopped analyzing at this point", CS->getStmt()->getLocStart());
122  }
123}
124