AnalyzerStatsChecker.cpp revision a93d0f280693b8418bc88cf7a8c93325f7fcf4c6
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#include "llvm/Support/raw_ostream.h"
26
27using namespace clang;
28using namespace ento;
29
30STATISTIC(NumBlocks,
31          "The # of blocks in top level functions");
32STATISTIC(NumBlocksUnreachable,
33          "The # of unreachable blocks in analyzing top level functions");
34
35namespace {
36class AnalyzerStatsChecker : public Checker<check::EndAnalysis> {
37public:
38  void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,ExprEngine &Eng) const;
39};
40}
41
42void AnalyzerStatsChecker::checkEndAnalysis(ExplodedGraph &G,
43                                            BugReporter &B,
44                                            ExprEngine &Eng) const {
45  const CFG *C  = 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  const Decl *D = LC->getDecl();
54
55  // Iterate over the exploded graph.
56  for (ExplodedGraph::node_iterator I = G.nodes_begin();
57      I != G.nodes_end(); ++I) {
58    const ProgramPoint &P = I->getLocation();
59
60    // Only check the coverage in the top level function (optimization).
61    if (D != P.getLocationContext()->getDecl())
62      continue;
63
64    if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
65      const CFGBlock *CB = BE->getBlock();
66      reachable.insert(CB);
67    }
68  }
69
70  // Get the CFG and the Decl of this block.
71  C = LC->getCFG();
72
73  unsigned total = 0, unreachable = 0;
74
75  // Find CFGBlocks that were not covered by any node
76  for (CFG::const_iterator I = C->begin(); I != C->end(); ++I) {
77    const CFGBlock *CB = *I;
78    ++total;
79    // Check if the block is unreachable
80    if (!reachable.count(CB)) {
81      ++unreachable;
82    }
83  }
84
85  // We never 'reach' the entry block, so correct the unreachable count
86  unreachable--;
87  // There is no BlockEntrance corresponding to the exit block as well, so
88  // assume it is reached as well.
89  unreachable--;
90
91  // Generate the warning string
92  SmallString<128> buf;
93  llvm::raw_svector_ostream output(buf);
94  PresumedLoc Loc = SM.getPresumedLoc(D->getLocation());
95  if (!Loc.isValid())
96    return;
97
98  if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
99    const NamedDecl *ND = cast<NamedDecl>(D);
100    output << *ND;
101  }
102  else if (isa<BlockDecl>(D)) {
103    output << "block(line:" << Loc.getLine() << ":col:" << Loc.getColumn();
104  }
105
106  NumBlocksUnreachable += unreachable;
107  NumBlocks += total;
108  std::string NameOfRootFunction = output.str();
109
110  output << " -> Total CFGBlocks: " << total << " | Unreachable CFGBlocks: "
111      << unreachable << " | Exhausted Block: "
112      << (Eng.wasBlocksExhausted() ? "yes" : "no")
113      << " | Empty WorkList: "
114      << (Eng.hasEmptyWorkList() ? "yes" : "no");
115
116  B.EmitBasicReport(D, "Analyzer Statistics", "Internal Statistics",
117                    output.str(), PathDiagnosticLocation(D, SM));
118
119  // Emit warning for each block we bailed out on.
120  typedef CoreEngine::BlocksExhausted::const_iterator ExhaustedIterator;
121  const CoreEngine &CE = Eng.getCoreEngine();
122  for (ExhaustedIterator I = CE.blocks_exhausted_begin(),
123      E = CE.blocks_exhausted_end(); I != E; ++I) {
124    const BlockEdge &BE =  I->first;
125    const CFGBlock *Exit = BE.getDst();
126    const CFGElement &CE = Exit->front();
127    if (const CFGStmt *CS = dyn_cast<CFGStmt>(&CE)) {
128      SmallString<128> bufI;
129      llvm::raw_svector_ostream outputI(bufI);
130      outputI << "(" << NameOfRootFunction << ")" <<
131                 ": The analyzer generated a sink at this point";
132      B.EmitBasicReport(D, "Sink Point", "Internal Statistics", outputI.str(),
133                        PathDiagnosticLocation::createBegin(CS->getStmt(),
134                                                            SM, LC));
135    }
136  }
137}
138
139void ento::registerAnalyzerStatsChecker(CheckerManager &mgr) {
140  mgr.registerChecker<AnalyzerStatsChecker>();
141}
142