blob: c484537e957aacc22d7e8a58d7e9bfdf6a904470 [file] [log] [blame]
Tom Care52d861c2010-09-10 00:44:44 +00001//==--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/Checker/PathSensitive/CheckerVisitor.h"
13#include "clang/Checker/PathSensitive/ExplodedGraph.h"
14#include "clang/Checker/BugReporter/BugReporter.h"
15#include "GRExprEngineExperimentalChecks.h"
16#include "clang/Basic/SourceManager.h"
17#include "llvm/ADT/SmallPtrSet.h"
18
19using namespace clang;
20
21namespace {
22class AnalyzerStatsChecker : public CheckerVisitor<AnalyzerStatsChecker> {
23public:
24 static void *getTag();
25 void VisitEndAnalysis(ExplodedGraph &G, BugReporter &B, GRExprEngine &Eng);
26
27private:
28 llvm::SmallPtrSet<const CFGBlock*, 256> reachable;
29};
30}
31
32void *AnalyzerStatsChecker::getTag() {
33 static int x = 0;
34 return &x;
35}
36
37void clang::RegisterAnalyzerStatsChecker(GRExprEngine &Eng) {
38 Eng.registerCheck(new AnalyzerStatsChecker());
39}
40
41void 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();
48
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();
56
Tom Care2cb55202010-09-29 23:48:34 +000057 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
58 const CFGBlock *CB = BE->getBlock();
Tom Care52d861c2010-09-10 00:44:44 +000059 reachable.insert(CB);
60 }
61 }
62
63 // Get the CFG and the Decl of this block
64 C = LC->getCFG();
65 D = LC->getAnalysisContext()->getDecl();
66
67 unsigned total = 0, unreachable = 0;
68
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;
76 }
77 }
78
79 // We never 'reach' the entry block, so correct the unreachable count
80 unreachable--;
81
82 // Generate the warning string
83 llvm::SmallString<128> buf;
84 llvm::raw_svector_ostream output(buf);
85 PresumedLoc Loc = SM.getPresumedLoc(D->getLocation());
Douglas Gregorcb7b1e12010-11-12 07:15:47 +000086 if (Loc.isValid()) {
87 output << Loc.getFilename() << " : ";
Tom Care52d861c2010-09-10 00:44:44 +000088
Douglas Gregorcb7b1e12010-11-12 07:15:47 +000089 if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
90 const NamedDecl *ND = cast<NamedDecl>(D);
91 output << ND;
92 }
93 else if (isa<BlockDecl>(D)) {
94 output << "block(line:" << Loc.getLine() << ":col:" << Loc.getColumn();
95 }
Tom Care52d861c2010-09-10 00:44:44 +000096 }
Douglas Gregorcb7b1e12010-11-12 07:15:47 +000097
Tom Care52d861c2010-09-10 00:44:44 +000098 output << " -> Total CFGBlocks: " << total << " | Unreachable CFGBlocks: "
99 << unreachable << " | Aborted Block: "
Tom Care0e1cd942010-09-22 21:07:51 +0000100 << (Eng.wasBlockAborted() ? "yes" : "no")
Tom Care52d861c2010-09-10 00:44:44 +0000101 << " | Empty WorkList: "
Tom Care0e1cd942010-09-22 21:07:51 +0000102 << (Eng.hasEmptyWorkList() ? "yes" : "no");
Tom Care52d861c2010-09-10 00:44:44 +0000103
104 B.EmitBasicReport("Analyzer Statistics", "Internal Statistics", output.str(),
105 D->getLocation());
Tom Care2cb55202010-09-29 23:48:34 +0000106
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());
118 }
Tom Care52d861c2010-09-10 00:44:44 +0000119}