AnalyzerStatsChecker.cpp revision 65552ca127ac5d9b767c5f0b09d86e17cb3e9e5e
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 =
52          GraphRoot->getLocation().getLocationContext()->getCurrentStackFrame();
53
54  // Iterate over the exploded graph.
55  for (ExplodedGraph::node_iterator I = G.nodes_begin();
56      I != G.nodes_end(); ++I) {
57    const ProgramPoint &P = I->getLocation();
58
59    // Only check the coverage in the top level function.
60    if (LC != P.getLocationContext()->getCurrentStackFrame())
61      continue;
62
63    if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
64      const CFGBlock *CB = BE->getBlock();
65      reachable.insert(CB);
66    }
67  }
68
69  // Get the CFG and the Decl of this block
70  C = LC->getCFG();
71  D = LC->getAnalysisDeclContext()->getDecl();
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("Analyzer Statistics", "Internal Statistics", output.str(),
117      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("Sink Point", "Internal Statistics", outputI.str(),
133          PathDiagnosticLocation::createBegin(CS->getStmt(), SM, LC));
134    }
135  }
136}
137
138void ento::registerAnalyzerStatsChecker(CheckerManager &mgr) {
139  mgr.registerChecker<AnalyzerStatsChecker>();
140}
141