blob: aa6f97b2fa8f73815786fb8f2e2e3cfa0608c5ee [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//===----------------------------------------------------------------------===//
Anna Zaks749bbe62012-03-22 21:06:03 +000011#define DEBUG_TYPE "StatsChecker"
Tom Care52d861c2010-09-10 00:44:44 +000012
Argyrios Kyrtzidis58f2e7c2011-02-28 01:26:50 +000013#include "ClangSACheckers.h"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000014#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis58f2e7c2011-02-28 01:26:50 +000015#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000016#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
Argyrios Kyrtzidis58f2e7c2011-02-28 01:26:50 +000017#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000018#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
Argyrios Kyrtzidisa7af5ea2010-12-22 18:52:56 +000019
Benjamin Kramerc35fb7d2012-01-28 12:06:22 +000020#include "clang/AST/DeclObjC.h"
Tom Care52d861c2010-09-10 00:44:44 +000021#include "clang/Basic/SourceManager.h"
22#include "llvm/ADT/SmallPtrSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000023#include "llvm/ADT/SmallString.h"
Anna Zaks749bbe62012-03-22 21:06:03 +000024#include "llvm/ADT/Statistic.h"
Tom Care52d861c2010-09-10 00:44:44 +000025
26using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000027using namespace ento;
Tom Care52d861c2010-09-10 00:44:44 +000028
Anna Zaks749bbe62012-03-22 21:06:03 +000029STATISTIC(NumBlocks,
30 "The # of blocks in top level functions");
31STATISTIC(NumBlocksUnreachable,
32 "The # of unreachable blocks in analyzing top level functions");
33
Tom Care52d861c2010-09-10 00:44:44 +000034namespace {
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000035class AnalyzerStatsChecker : public Checker<check::EndAnalysis> {
Tom Care52d861c2010-09-10 00:44:44 +000036public:
Argyrios Kyrtzidis58f2e7c2011-02-28 01:26:50 +000037 void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,ExprEngine &Eng) const;
Tom Care52d861c2010-09-10 00:44:44 +000038};
39}
40
Argyrios Kyrtzidis58f2e7c2011-02-28 01:26:50 +000041void AnalyzerStatsChecker::checkEndAnalysis(ExplodedGraph &G,
Tom Care52d861c2010-09-10 00:44:44 +000042 BugReporter &B,
Argyrios Kyrtzidis58f2e7c2011-02-28 01:26:50 +000043 ExprEngine &Eng) const {
Tom Care52d861c2010-09-10 00:44:44 +000044 const CFG *C = 0;
Tom Care52d861c2010-09-10 00:44:44 +000045 const SourceManager &SM = B.getSourceManager();
Argyrios Kyrtzidis58f2e7c2011-02-28 01:26:50 +000046 llvm::SmallPtrSet<const CFGBlock*, 256> reachable;
Tom Care52d861c2010-09-10 00:44:44 +000047
Anna Zaks64394e22012-03-22 21:05:57 +000048 // Root node should have the location context of the top most function.
49 const ExplodedNode *GraphRoot = *G.roots_begin();
Anna Zaks64ee9d02012-03-28 17:05:46 +000050 const LocationContext *LC = GraphRoot->getLocation().getLocationContext();
51
52 const Decl *D = LC->getDecl();
Anna Zaks64394e22012-03-22 21:05:57 +000053
54 // Iterate over the exploded graph.
Tom Care52d861c2010-09-10 00:44:44 +000055 for (ExplodedGraph::node_iterator I = G.nodes_begin();
56 I != G.nodes_end(); ++I) {
57 const ProgramPoint &P = I->getLocation();
Anna Zaks64394e22012-03-22 21:05:57 +000058
Anna Zaks64ee9d02012-03-28 17:05:46 +000059 // Only check the coverage in the top level function (optimization).
60 if (D != P.getLocationContext()->getDecl())
Anna Zaks64394e22012-03-22 21:05:57 +000061 continue;
Tom Care52d861c2010-09-10 00:44:44 +000062
Tom Care2cb55202010-09-29 23:48:34 +000063 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
64 const CFGBlock *CB = BE->getBlock();
Tom Care52d861c2010-09-10 00:44:44 +000065 reachable.insert(CB);
66 }
67 }
68
Anna Zaks64ee9d02012-03-28 17:05:46 +000069 // Get the CFG and the Decl of this block.
Tom Care52d861c2010-09-10 00:44:44 +000070 C = LC->getCFG();
Tom Care52d861c2010-09-10 00:44:44 +000071
72 unsigned total = 0, unreachable = 0;
73
74 // Find CFGBlocks that were not covered by any node
75 for (CFG::const_iterator I = C->begin(); I != C->end(); ++I) {
76 const CFGBlock *CB = *I;
77 ++total;
78 // Check if the block is unreachable
79 if (!reachable.count(CB)) {
80 ++unreachable;
81 }
82 }
83
84 // We never 'reach' the entry block, so correct the unreachable count
85 unreachable--;
Anna Zaks64394e22012-03-22 21:05:57 +000086 // There is no BlockEntrance corresponding to the exit block as well, so
87 // assume it is reached as well.
88 unreachable--;
Tom Care52d861c2010-09-10 00:44:44 +000089
90 // Generate the warning string
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +000091 SmallString<128> buf;
Tom Care52d861c2010-09-10 00:44:44 +000092 llvm::raw_svector_ostream output(buf);
93 PresumedLoc Loc = SM.getPresumedLoc(D->getLocation());
Anna Zaks65552ca2012-03-27 20:02:44 +000094 if (!Loc.isValid())
95 return;
Tom Care52d861c2010-09-10 00:44:44 +000096
Anna Zaks65552ca2012-03-27 20:02:44 +000097 if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
98 const NamedDecl *ND = cast<NamedDecl>(D);
99 output << *ND;
100 }
101 else if (isa<BlockDecl>(D)) {
102 output << "block(line:" << Loc.getLine() << ":col:" << Loc.getColumn();
Tom Care52d861c2010-09-10 00:44:44 +0000103 }
Douglas Gregorcb7b1e12010-11-12 07:15:47 +0000104
Anna Zaks749bbe62012-03-22 21:06:03 +0000105 NumBlocksUnreachable += unreachable;
106 NumBlocks += total;
Anna Zaks65552ca2012-03-27 20:02:44 +0000107 std::string NameOfRootFunction = output.str();
Anna Zaks749bbe62012-03-22 21:06:03 +0000108
Tom Care52d861c2010-09-10 00:44:44 +0000109 output << " -> Total CFGBlocks: " << total << " | Unreachable CFGBlocks: "
Ted Kremenek422ab7a2011-04-02 02:56:23 +0000110 << unreachable << " | Exhausted Block: "
111 << (Eng.wasBlocksExhausted() ? "yes" : "no")
Tom Care52d861c2010-09-10 00:44:44 +0000112 << " | Empty WorkList: "
Tom Care0e1cd942010-09-22 21:07:51 +0000113 << (Eng.hasEmptyWorkList() ? "yes" : "no");
Tom Care52d861c2010-09-10 00:44:44 +0000114
Ted Kremenek07189522012-04-04 18:11:35 +0000115 B.EmitBasicReport(D, "Analyzer Statistics", "Internal Statistics",
116 output.str(), PathDiagnosticLocation(D, SM));
Tom Care2cb55202010-09-29 23:48:34 +0000117
Anna Zaks65552ca2012-03-27 20:02:44 +0000118 // Emit warning for each block we bailed out on.
Ted Kremenek422ab7a2011-04-02 02:56:23 +0000119 typedef CoreEngine::BlocksExhausted::const_iterator ExhaustedIterator;
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +0000120 const CoreEngine &CE = Eng.getCoreEngine();
Ted Kremenek422ab7a2011-04-02 02:56:23 +0000121 for (ExhaustedIterator I = CE.blocks_exhausted_begin(),
122 E = CE.blocks_exhausted_end(); I != E; ++I) {
Tom Care2cb55202010-09-29 23:48:34 +0000123 const BlockEdge &BE = I->first;
124 const CFGBlock *Exit = BE.getDst();
125 const CFGElement &CE = Exit->front();
Anna Zaks65552ca2012-03-27 20:02:44 +0000126 if (const CFGStmt *CS = dyn_cast<CFGStmt>(&CE)) {
127 SmallString<128> bufI;
128 llvm::raw_svector_ostream outputI(bufI);
129 outputI << "(" << NameOfRootFunction << ")" <<
130 ": The analyzer generated a sink at this point";
Ted Kremenek07189522012-04-04 18:11:35 +0000131 B.EmitBasicReport(D, "Sink Point", "Internal Statistics", outputI.str(),
132 PathDiagnosticLocation::createBegin(CS->getStmt(),
133 SM, LC));
Anna Zaks65552ca2012-03-27 20:02:44 +0000134 }
Tom Care2cb55202010-09-29 23:48:34 +0000135 }
Tom Care52d861c2010-09-10 00:44:44 +0000136}
Argyrios Kyrtzidis58f2e7c2011-02-28 01:26:50 +0000137
138void ento::registerAnalyzerStatsChecker(CheckerManager &mgr) {
139 mgr.registerChecker<AnalyzerStatsChecker>();
140}