1f4a2713aSLionel Sambuc //==--AnalyzerStatsChecker.cpp - Analyzer visitation statistics --*- C++ -*-==//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc // This file reports various statistics about analyzer visitation.
10f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
11f4a2713aSLionel Sambuc #include "ClangSACheckers.h"
12f4a2713aSLionel Sambuc #include "clang/AST/DeclObjC.h"
13f4a2713aSLionel Sambuc #include "clang/Basic/SourceManager.h"
14f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
15f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/Checker.h"
16f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/CheckerManager.h"
17f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
18f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
19f4a2713aSLionel Sambuc #include "llvm/ADT/SmallPtrSet.h"
20f4a2713aSLionel Sambuc #include "llvm/ADT/SmallString.h"
21f4a2713aSLionel Sambuc #include "llvm/ADT/Statistic.h"
22f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
23f4a2713aSLionel Sambuc
24f4a2713aSLionel Sambuc using namespace clang;
25f4a2713aSLionel Sambuc using namespace ento;
26f4a2713aSLionel Sambuc
27*0a6a1f1dSLionel Sambuc #define DEBUG_TYPE "StatsChecker"
28*0a6a1f1dSLionel Sambuc
29f4a2713aSLionel Sambuc STATISTIC(NumBlocks,
30f4a2713aSLionel Sambuc "The # of blocks in top level functions");
31f4a2713aSLionel Sambuc STATISTIC(NumBlocksUnreachable,
32f4a2713aSLionel Sambuc "The # of unreachable blocks in analyzing top level functions");
33f4a2713aSLionel Sambuc
34f4a2713aSLionel Sambuc namespace {
35f4a2713aSLionel Sambuc class AnalyzerStatsChecker : public Checker<check::EndAnalysis> {
36f4a2713aSLionel Sambuc public:
37f4a2713aSLionel Sambuc void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,ExprEngine &Eng) const;
38f4a2713aSLionel Sambuc };
39f4a2713aSLionel Sambuc }
40f4a2713aSLionel Sambuc
checkEndAnalysis(ExplodedGraph & G,BugReporter & B,ExprEngine & Eng) const41f4a2713aSLionel Sambuc void AnalyzerStatsChecker::checkEndAnalysis(ExplodedGraph &G,
42f4a2713aSLionel Sambuc BugReporter &B,
43f4a2713aSLionel Sambuc ExprEngine &Eng) const {
44*0a6a1f1dSLionel Sambuc const CFG *C = nullptr;
45f4a2713aSLionel Sambuc const SourceManager &SM = B.getSourceManager();
46f4a2713aSLionel Sambuc llvm::SmallPtrSet<const CFGBlock*, 256> reachable;
47f4a2713aSLionel Sambuc
48f4a2713aSLionel Sambuc // Root node should have the location context of the top most function.
49f4a2713aSLionel Sambuc const ExplodedNode *GraphRoot = *G.roots_begin();
50f4a2713aSLionel Sambuc const LocationContext *LC = GraphRoot->getLocation().getLocationContext();
51f4a2713aSLionel Sambuc
52f4a2713aSLionel Sambuc const Decl *D = LC->getDecl();
53f4a2713aSLionel Sambuc
54f4a2713aSLionel Sambuc // Iterate over the exploded graph.
55f4a2713aSLionel Sambuc for (ExplodedGraph::node_iterator I = G.nodes_begin();
56f4a2713aSLionel Sambuc I != G.nodes_end(); ++I) {
57f4a2713aSLionel Sambuc const ProgramPoint &P = I->getLocation();
58f4a2713aSLionel Sambuc
59f4a2713aSLionel Sambuc // Only check the coverage in the top level function (optimization).
60f4a2713aSLionel Sambuc if (D != P.getLocationContext()->getDecl())
61f4a2713aSLionel Sambuc continue;
62f4a2713aSLionel Sambuc
63f4a2713aSLionel Sambuc if (Optional<BlockEntrance> BE = P.getAs<BlockEntrance>()) {
64f4a2713aSLionel Sambuc const CFGBlock *CB = BE->getBlock();
65f4a2713aSLionel Sambuc reachable.insert(CB);
66f4a2713aSLionel Sambuc }
67f4a2713aSLionel Sambuc }
68f4a2713aSLionel Sambuc
69f4a2713aSLionel Sambuc // Get the CFG and the Decl of this block.
70f4a2713aSLionel Sambuc C = LC->getCFG();
71f4a2713aSLionel Sambuc
72f4a2713aSLionel Sambuc unsigned total = 0, unreachable = 0;
73f4a2713aSLionel Sambuc
74f4a2713aSLionel Sambuc // Find CFGBlocks that were not covered by any node
75f4a2713aSLionel Sambuc for (CFG::const_iterator I = C->begin(); I != C->end(); ++I) {
76f4a2713aSLionel Sambuc const CFGBlock *CB = *I;
77f4a2713aSLionel Sambuc ++total;
78f4a2713aSLionel Sambuc // Check if the block is unreachable
79f4a2713aSLionel Sambuc if (!reachable.count(CB)) {
80f4a2713aSLionel Sambuc ++unreachable;
81f4a2713aSLionel Sambuc }
82f4a2713aSLionel Sambuc }
83f4a2713aSLionel Sambuc
84f4a2713aSLionel Sambuc // We never 'reach' the entry block, so correct the unreachable count
85f4a2713aSLionel Sambuc unreachable--;
86f4a2713aSLionel Sambuc // There is no BlockEntrance corresponding to the exit block as well, so
87f4a2713aSLionel Sambuc // assume it is reached as well.
88f4a2713aSLionel Sambuc unreachable--;
89f4a2713aSLionel Sambuc
90f4a2713aSLionel Sambuc // Generate the warning string
91f4a2713aSLionel Sambuc SmallString<128> buf;
92f4a2713aSLionel Sambuc llvm::raw_svector_ostream output(buf);
93f4a2713aSLionel Sambuc PresumedLoc Loc = SM.getPresumedLoc(D->getLocation());
94f4a2713aSLionel Sambuc if (!Loc.isValid())
95f4a2713aSLionel Sambuc return;
96f4a2713aSLionel Sambuc
97f4a2713aSLionel Sambuc if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
98f4a2713aSLionel Sambuc const NamedDecl *ND = cast<NamedDecl>(D);
99f4a2713aSLionel Sambuc output << *ND;
100f4a2713aSLionel Sambuc }
101f4a2713aSLionel Sambuc else if (isa<BlockDecl>(D)) {
102f4a2713aSLionel Sambuc output << "block(line:" << Loc.getLine() << ":col:" << Loc.getColumn();
103f4a2713aSLionel Sambuc }
104f4a2713aSLionel Sambuc
105f4a2713aSLionel Sambuc NumBlocksUnreachable += unreachable;
106f4a2713aSLionel Sambuc NumBlocks += total;
107f4a2713aSLionel Sambuc std::string NameOfRootFunction = output.str();
108f4a2713aSLionel Sambuc
109f4a2713aSLionel Sambuc output << " -> Total CFGBlocks: " << total << " | Unreachable CFGBlocks: "
110f4a2713aSLionel Sambuc << unreachable << " | Exhausted Block: "
111f4a2713aSLionel Sambuc << (Eng.wasBlocksExhausted() ? "yes" : "no")
112f4a2713aSLionel Sambuc << " | Empty WorkList: "
113f4a2713aSLionel Sambuc << (Eng.hasEmptyWorkList() ? "yes" : "no");
114f4a2713aSLionel Sambuc
115*0a6a1f1dSLionel Sambuc B.EmitBasicReport(D, this, "Analyzer Statistics", "Internal Statistics",
116f4a2713aSLionel Sambuc output.str(), PathDiagnosticLocation(D, SM));
117f4a2713aSLionel Sambuc
118f4a2713aSLionel Sambuc // Emit warning for each block we bailed out on.
119f4a2713aSLionel Sambuc typedef CoreEngine::BlocksExhausted::const_iterator ExhaustedIterator;
120f4a2713aSLionel Sambuc const CoreEngine &CE = Eng.getCoreEngine();
121f4a2713aSLionel Sambuc for (ExhaustedIterator I = CE.blocks_exhausted_begin(),
122f4a2713aSLionel Sambuc E = CE.blocks_exhausted_end(); I != E; ++I) {
123f4a2713aSLionel Sambuc const BlockEdge &BE = I->first;
124f4a2713aSLionel Sambuc const CFGBlock *Exit = BE.getDst();
125f4a2713aSLionel Sambuc const CFGElement &CE = Exit->front();
126f4a2713aSLionel Sambuc if (Optional<CFGStmt> CS = CE.getAs<CFGStmt>()) {
127f4a2713aSLionel Sambuc SmallString<128> bufI;
128f4a2713aSLionel Sambuc llvm::raw_svector_ostream outputI(bufI);
129f4a2713aSLionel Sambuc outputI << "(" << NameOfRootFunction << ")" <<
130f4a2713aSLionel Sambuc ": The analyzer generated a sink at this point";
131f4a2713aSLionel Sambuc B.EmitBasicReport(
132*0a6a1f1dSLionel Sambuc D, this, "Sink Point", "Internal Statistics", outputI.str(),
133f4a2713aSLionel Sambuc PathDiagnosticLocation::createBegin(CS->getStmt(), SM, LC));
134f4a2713aSLionel Sambuc }
135f4a2713aSLionel Sambuc }
136f4a2713aSLionel Sambuc }
137f4a2713aSLionel Sambuc
registerAnalyzerStatsChecker(CheckerManager & mgr)138f4a2713aSLionel Sambuc void ento::registerAnalyzerStatsChecker(CheckerManager &mgr) {
139f4a2713aSLionel Sambuc mgr.registerChecker<AnalyzerStatsChecker>();
140f4a2713aSLionel Sambuc }
141