blob: 8521c18c5480d2e11e4d26e1f9167ecc6b4c5786 [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
Ted Kremenek21142582010-12-23 19:38:26 +000012#include "clang/StaticAnalyzer/PathSensitive/CheckerVisitor.h"
13#include "clang/StaticAnalyzer/PathSensitive/ExplodedGraph.h"
14#include "clang/StaticAnalyzer/BugReporter/BugReporter.h"
Argyrios Kyrtzidisa7af5ea2010-12-22 18:52:56 +000015
16// FIXME: Restructure checker registration.
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +000017#include "Checkers/ExprEngineExperimentalChecks.h"
Argyrios Kyrtzidisa7af5ea2010-12-22 18:52:56 +000018
Tom Care52d861c2010-09-10 00:44:44 +000019#include "clang/Basic/SourceManager.h"
20#include "llvm/ADT/SmallPtrSet.h"
21
22using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000023using namespace ento;
Tom Care52d861c2010-09-10 00:44:44 +000024
25namespace {
26class AnalyzerStatsChecker : public CheckerVisitor<AnalyzerStatsChecker> {
27public:
28 static void *getTag();
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +000029 void VisitEndAnalysis(ExplodedGraph &G, BugReporter &B, ExprEngine &Eng);
Tom Care52d861c2010-09-10 00:44:44 +000030
31private:
32 llvm::SmallPtrSet<const CFGBlock*, 256> reachable;
33};
34}
35
36void *AnalyzerStatsChecker::getTag() {
37 static int x = 0;
38 return &x;
39}
40
Ted Kremenek9ef65372010-12-23 07:20:52 +000041void ento::RegisterAnalyzerStatsChecker(ExprEngine &Eng) {
Tom Care52d861c2010-09-10 00:44:44 +000042 Eng.registerCheck(new AnalyzerStatsChecker());
43}
44
45void AnalyzerStatsChecker::VisitEndAnalysis(ExplodedGraph &G,
46 BugReporter &B,
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +000047 ExprEngine &Eng) {
Tom Care52d861c2010-09-10 00:44:44 +000048 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
Tom Care2cb55202010-09-29 23:48:34 +000061 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
62 const CFGBlock *CB = BE->getBlock();
Tom Care52d861c2010-09-10 00:44:44 +000063 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());
Douglas Gregorcb7b1e12010-11-12 07:15:47 +000090 if (Loc.isValid()) {
91 output << Loc.getFilename() << " : ";
Tom Care52d861c2010-09-10 00:44:44 +000092
Douglas Gregorcb7b1e12010-11-12 07:15:47 +000093 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 }
Tom Care52d861c2010-09-10 00:44:44 +0000100 }
Douglas Gregorcb7b1e12010-11-12 07:15:47 +0000101
Tom Care52d861c2010-09-10 00:44:44 +0000102 output << " -> Total CFGBlocks: " << total << " | Unreachable CFGBlocks: "
103 << unreachable << " | Aborted Block: "
Tom Care0e1cd942010-09-22 21:07:51 +0000104 << (Eng.wasBlockAborted() ? "yes" : "no")
Tom Care52d861c2010-09-10 00:44:44 +0000105 << " | Empty WorkList: "
Tom Care0e1cd942010-09-22 21:07:51 +0000106 << (Eng.hasEmptyWorkList() ? "yes" : "no");
Tom Care52d861c2010-09-10 00:44:44 +0000107
108 B.EmitBasicReport("Analyzer Statistics", "Internal Statistics", output.str(),
109 D->getLocation());
Tom Care2cb55202010-09-29 23:48:34 +0000110
111 // Emit warning for each block we bailed out on
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +0000112 typedef CoreEngine::BlocksAborted::const_iterator AbortedIterator;
113 const CoreEngine &CE = Eng.getCoreEngine();
Tom Care2cb55202010-09-29 23:48:34 +0000114 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 }
Tom Care52d861c2010-09-10 00:44:44 +0000123}