blob: 6269acddb08a2afec7691aaad987640d751eea2b [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
Argyrios Kyrtzidis58f2e7c2011-02-28 01:26:50 +000012#include "ClangSACheckers.h"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000013#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis58f2e7c2011-02-28 01:26:50 +000014#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000015#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
Argyrios Kyrtzidis58f2e7c2011-02-28 01:26:50 +000016#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000017#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
Argyrios Kyrtzidisa7af5ea2010-12-22 18:52:56 +000018
Benjamin Kramerc35fb7d2012-01-28 12:06:22 +000019#include "clang/AST/DeclObjC.h"
Tom Care52d861c2010-09-10 00:44:44 +000020#include "clang/Basic/SourceManager.h"
21#include "llvm/ADT/SmallPtrSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000022#include "llvm/ADT/SmallString.h"
Tom Care52d861c2010-09-10 00:44:44 +000023
24using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000025using namespace ento;
Tom Care52d861c2010-09-10 00:44:44 +000026
27namespace {
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000028class AnalyzerStatsChecker : public Checker<check::EndAnalysis> {
Tom Care52d861c2010-09-10 00:44:44 +000029public:
Argyrios Kyrtzidis58f2e7c2011-02-28 01:26:50 +000030 void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,ExprEngine &Eng) const;
Tom Care52d861c2010-09-10 00:44:44 +000031};
32}
33
Argyrios Kyrtzidis58f2e7c2011-02-28 01:26:50 +000034void AnalyzerStatsChecker::checkEndAnalysis(ExplodedGraph &G,
Tom Care52d861c2010-09-10 00:44:44 +000035 BugReporter &B,
Argyrios Kyrtzidis58f2e7c2011-02-28 01:26:50 +000036 ExprEngine &Eng) const {
Tom Care52d861c2010-09-10 00:44:44 +000037 const CFG *C = 0;
38 const Decl *D = 0;
Tom Care52d861c2010-09-10 00:44:44 +000039 const SourceManager &SM = B.getSourceManager();
Argyrios Kyrtzidis58f2e7c2011-02-28 01:26:50 +000040 llvm::SmallPtrSet<const CFGBlock*, 256> reachable;
Tom Care52d861c2010-09-10 00:44:44 +000041
Anna Zaks64394e22012-03-22 21:05:57 +000042 // Root node should have the location context of the top most function.
43 const ExplodedNode *GraphRoot = *G.roots_begin();
44 const LocationContext *LC = GraphRoot->getLocation().getLocationContext();
45
46 // Iterate over the exploded graph.
Tom Care52d861c2010-09-10 00:44:44 +000047 for (ExplodedGraph::node_iterator I = G.nodes_begin();
48 I != G.nodes_end(); ++I) {
49 const ProgramPoint &P = I->getLocation();
Anna Zaks64394e22012-03-22 21:05:57 +000050
51 // Only check the coverage in the top level function.
52 if (LC != P.getLocationContext())
53 continue;
Tom Care52d861c2010-09-10 00:44:44 +000054
Tom Care2cb55202010-09-29 23:48:34 +000055 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
56 const CFGBlock *CB = BE->getBlock();
Tom Care52d861c2010-09-10 00:44:44 +000057 reachable.insert(CB);
58 }
59 }
60
61 // Get the CFG and the Decl of this block
62 C = LC->getCFG();
Ted Kremenek1d26f482011-10-24 01:32:45 +000063 D = LC->getAnalysisDeclContext()->getDecl();
Tom Care52d861c2010-09-10 00:44:44 +000064
65 unsigned total = 0, unreachable = 0;
66
67 // Find CFGBlocks that were not covered by any node
68 for (CFG::const_iterator I = C->begin(); I != C->end(); ++I) {
69 const CFGBlock *CB = *I;
70 ++total;
71 // Check if the block is unreachable
72 if (!reachable.count(CB)) {
73 ++unreachable;
74 }
75 }
76
77 // We never 'reach' the entry block, so correct the unreachable count
78 unreachable--;
Anna Zaks64394e22012-03-22 21:05:57 +000079 // There is no BlockEntrance corresponding to the exit block as well, so
80 // assume it is reached as well.
81 unreachable--;
Tom Care52d861c2010-09-10 00:44:44 +000082
83 // Generate the warning string
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +000084 SmallString<128> buf;
Tom Care52d861c2010-09-10 00:44:44 +000085 llvm::raw_svector_ostream output(buf);
86 PresumedLoc Loc = SM.getPresumedLoc(D->getLocation());
Douglas Gregorcb7b1e12010-11-12 07:15:47 +000087 if (Loc.isValid()) {
88 output << Loc.getFilename() << " : ";
Tom Care52d861c2010-09-10 00:44:44 +000089
Douglas Gregorcb7b1e12010-11-12 07:15:47 +000090 if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
91 const NamedDecl *ND = cast<NamedDecl>(D);
Benjamin Kramerb8989f22011-10-14 18:45:37 +000092 output << *ND;
Douglas Gregorcb7b1e12010-11-12 07:15:47 +000093 }
94 else if (isa<BlockDecl>(D)) {
95 output << "block(line:" << Loc.getLine() << ":col:" << Loc.getColumn();
96 }
Tom Care52d861c2010-09-10 00:44:44 +000097 }
Douglas Gregorcb7b1e12010-11-12 07:15:47 +000098
Tom Care52d861c2010-09-10 00:44:44 +000099 output << " -> Total CFGBlocks: " << total << " | Unreachable CFGBlocks: "
Ted Kremenek422ab7a2011-04-02 02:56:23 +0000100 << unreachable << " | Exhausted Block: "
101 << (Eng.wasBlocksExhausted() ? "yes" : "no")
Tom Care52d861c2010-09-10 00:44:44 +0000102 << " | Empty WorkList: "
Tom Care0e1cd942010-09-22 21:07:51 +0000103 << (Eng.hasEmptyWorkList() ? "yes" : "no");
Tom Care52d861c2010-09-10 00:44:44 +0000104
105 B.EmitBasicReport("Analyzer Statistics", "Internal Statistics", output.str(),
Anna Zaks590dd8e2011-09-20 21:38:35 +0000106 PathDiagnosticLocation(D, SM));
Tom Care2cb55202010-09-29 23:48:34 +0000107
108 // Emit warning for each block we bailed out on
Ted Kremenek422ab7a2011-04-02 02:56:23 +0000109 typedef CoreEngine::BlocksExhausted::const_iterator ExhaustedIterator;
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +0000110 const CoreEngine &CE = Eng.getCoreEngine();
Ted Kremenek422ab7a2011-04-02 02:56:23 +0000111 for (ExhaustedIterator I = CE.blocks_exhausted_begin(),
112 E = CE.blocks_exhausted_end(); I != E; ++I) {
Tom Care2cb55202010-09-29 23:48:34 +0000113 const BlockEdge &BE = I->first;
114 const CFGBlock *Exit = BE.getDst();
115 const CFGElement &CE = Exit->front();
116 if (const CFGStmt *CS = dyn_cast<CFGStmt>(&CE))
117 B.EmitBasicReport("Bailout Point", "Internal Statistics", "The analyzer "
Anna Zaks590dd8e2011-09-20 21:38:35 +0000118 "stopped analyzing at this point",
119 PathDiagnosticLocation::createBegin(CS->getStmt(), SM, LC));
Tom Care2cb55202010-09-29 23:48:34 +0000120 }
Tom Care52d861c2010-09-10 00:44:44 +0000121}
Argyrios Kyrtzidis58f2e7c2011-02-28 01:26:50 +0000122
123void ento::registerAnalyzerStatsChecker(CheckerManager &mgr) {
124 mgr.registerChecker<AnalyzerStatsChecker>();
125}