Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 1 | //==--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 Zaks | 749bbe6 | 2012-03-22 21:06:03 +0000 | [diff] [blame] | 11 | #define DEBUG_TYPE "StatsChecker" |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 12 | |
Argyrios Kyrtzidis | 58f2e7c | 2011-02-28 01:26:50 +0000 | [diff] [blame] | 13 | #include "ClangSACheckers.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 14 | #include "clang/AST/DeclObjC.h" |
| 15 | #include "clang/Basic/SourceManager.h" |
| 16 | #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 17 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 58f2e7c | 2011-02-28 01:26:50 +0000 | [diff] [blame] | 18 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Ted Kremenek | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h" |
Argyrios Kyrtzidis | 58f2e7c | 2011-02-28 01:26:50 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallPtrSet.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallString.h" |
Anna Zaks | 749bbe6 | 2012-03-22 21:06:03 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/Statistic.h" |
Benjamin Kramer | a93d0f2 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 27 | using namespace ento; |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 28 | |
Anna Zaks | 749bbe6 | 2012-03-22 21:06:03 +0000 | [diff] [blame] | 29 | STATISTIC(NumBlocks, |
| 30 | "The # of blocks in top level functions"); |
| 31 | STATISTIC(NumBlocksUnreachable, |
| 32 | "The # of unreachable blocks in analyzing top level functions"); |
| 33 | |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 34 | namespace { |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 35 | class AnalyzerStatsChecker : public Checker<check::EndAnalysis> { |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 36 | public: |
Argyrios Kyrtzidis | 58f2e7c | 2011-02-28 01:26:50 +0000 | [diff] [blame] | 37 | void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,ExprEngine &Eng) const; |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 38 | }; |
| 39 | } |
| 40 | |
Argyrios Kyrtzidis | 58f2e7c | 2011-02-28 01:26:50 +0000 | [diff] [blame] | 41 | void AnalyzerStatsChecker::checkEndAnalysis(ExplodedGraph &G, |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 42 | BugReporter &B, |
Argyrios Kyrtzidis | 58f2e7c | 2011-02-28 01:26:50 +0000 | [diff] [blame] | 43 | ExprEngine &Eng) const { |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 44 | const CFG *C = 0; |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 45 | const SourceManager &SM = B.getSourceManager(); |
Argyrios Kyrtzidis | 58f2e7c | 2011-02-28 01:26:50 +0000 | [diff] [blame] | 46 | llvm::SmallPtrSet<const CFGBlock*, 256> reachable; |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 47 | |
Anna Zaks | 64394e2 | 2012-03-22 21:05:57 +0000 | [diff] [blame] | 48 | // Root node should have the location context of the top most function. |
| 49 | const ExplodedNode *GraphRoot = *G.roots_begin(); |
Anna Zaks | 64ee9d0 | 2012-03-28 17:05:46 +0000 | [diff] [blame] | 50 | const LocationContext *LC = GraphRoot->getLocation().getLocationContext(); |
| 51 | |
| 52 | const Decl *D = LC->getDecl(); |
Anna Zaks | 64394e2 | 2012-03-22 21:05:57 +0000 | [diff] [blame] | 53 | |
| 54 | // Iterate over the exploded graph. |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 55 | for (ExplodedGraph::node_iterator I = G.nodes_begin(); |
| 56 | I != G.nodes_end(); ++I) { |
| 57 | const ProgramPoint &P = I->getLocation(); |
Anna Zaks | 64394e2 | 2012-03-22 21:05:57 +0000 | [diff] [blame] | 58 | |
Anna Zaks | 64ee9d0 | 2012-03-28 17:05:46 +0000 | [diff] [blame] | 59 | // Only check the coverage in the top level function (optimization). |
| 60 | if (D != P.getLocationContext()->getDecl()) |
Anna Zaks | 64394e2 | 2012-03-22 21:05:57 +0000 | [diff] [blame] | 61 | continue; |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 62 | |
David Blaikie | 7a95de6 | 2013-02-21 22:23:56 +0000 | [diff] [blame] | 63 | if (Optional<BlockEntrance> BE = P.getAs<BlockEntrance>()) { |
Tom Care | 2cb5520 | 2010-09-29 23:48:34 +0000 | [diff] [blame] | 64 | const CFGBlock *CB = BE->getBlock(); |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 65 | reachable.insert(CB); |
| 66 | } |
| 67 | } |
| 68 | |
Anna Zaks | 64ee9d0 | 2012-03-28 17:05:46 +0000 | [diff] [blame] | 69 | // Get the CFG and the Decl of this block. |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 70 | C = LC->getCFG(); |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 71 | |
| 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 Zaks | 64394e2 | 2012-03-22 21:05:57 +0000 | [diff] [blame] | 86 | // There is no BlockEntrance corresponding to the exit block as well, so |
| 87 | // assume it is reached as well. |
| 88 | unreachable--; |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 89 | |
| 90 | // Generate the warning string |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 91 | SmallString<128> buf; |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 92 | llvm::raw_svector_ostream output(buf); |
| 93 | PresumedLoc Loc = SM.getPresumedLoc(D->getLocation()); |
Anna Zaks | 65552ca | 2012-03-27 20:02:44 +0000 | [diff] [blame] | 94 | if (!Loc.isValid()) |
| 95 | return; |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 96 | |
Anna Zaks | 65552ca | 2012-03-27 20:02:44 +0000 | [diff] [blame] | 97 | 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 Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 103 | } |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 104 | |
Anna Zaks | 749bbe6 | 2012-03-22 21:06:03 +0000 | [diff] [blame] | 105 | NumBlocksUnreachable += unreachable; |
| 106 | NumBlocks += total; |
Anna Zaks | 65552ca | 2012-03-27 20:02:44 +0000 | [diff] [blame] | 107 | std::string NameOfRootFunction = output.str(); |
Anna Zaks | 749bbe6 | 2012-03-22 21:06:03 +0000 | [diff] [blame] | 108 | |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 109 | output << " -> Total CFGBlocks: " << total << " | Unreachable CFGBlocks: " |
Ted Kremenek | 422ab7a | 2011-04-02 02:56:23 +0000 | [diff] [blame] | 110 | << unreachable << " | Exhausted Block: " |
| 111 | << (Eng.wasBlocksExhausted() ? "yes" : "no") |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 112 | << " | Empty WorkList: " |
Tom Care | 0e1cd94 | 2010-09-22 21:07:51 +0000 | [diff] [blame] | 113 | << (Eng.hasEmptyWorkList() ? "yes" : "no"); |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 114 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 115 | B.EmitBasicReport(D, this, "Analyzer Statistics", "Internal Statistics", |
Ted Kremenek | 0718952 | 2012-04-04 18:11:35 +0000 | [diff] [blame] | 116 | output.str(), PathDiagnosticLocation(D, SM)); |
Tom Care | 2cb5520 | 2010-09-29 23:48:34 +0000 | [diff] [blame] | 117 | |
Anna Zaks | 65552ca | 2012-03-27 20:02:44 +0000 | [diff] [blame] | 118 | // Emit warning for each block we bailed out on. |
Ted Kremenek | 422ab7a | 2011-04-02 02:56:23 +0000 | [diff] [blame] | 119 | typedef CoreEngine::BlocksExhausted::const_iterator ExhaustedIterator; |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame] | 120 | const CoreEngine &CE = Eng.getCoreEngine(); |
Ted Kremenek | 422ab7a | 2011-04-02 02:56:23 +0000 | [diff] [blame] | 121 | for (ExhaustedIterator I = CE.blocks_exhausted_begin(), |
| 122 | E = CE.blocks_exhausted_end(); I != E; ++I) { |
Tom Care | 2cb5520 | 2010-09-29 23:48:34 +0000 | [diff] [blame] | 123 | const BlockEdge &BE = I->first; |
| 124 | const CFGBlock *Exit = BE.getDst(); |
| 125 | const CFGElement &CE = Exit->front(); |
David Blaikie | b078054 | 2013-02-23 00:29:34 +0000 | [diff] [blame] | 126 | if (Optional<CFGStmt> CS = CE.getAs<CFGStmt>()) { |
Anna Zaks | 65552ca | 2012-03-27 20:02:44 +0000 | [diff] [blame] | 127 | SmallString<128> bufI; |
| 128 | llvm::raw_svector_ostream outputI(bufI); |
| 129 | outputI << "(" << NameOfRootFunction << ")" << |
| 130 | ": The analyzer generated a sink at this point"; |
David Blaikie | b078054 | 2013-02-23 00:29:34 +0000 | [diff] [blame] | 131 | B.EmitBasicReport( |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 132 | D, this, "Sink Point", "Internal Statistics", outputI.str(), |
David Blaikie | b078054 | 2013-02-23 00:29:34 +0000 | [diff] [blame] | 133 | PathDiagnosticLocation::createBegin(CS->getStmt(), SM, LC)); |
Anna Zaks | 65552ca | 2012-03-27 20:02:44 +0000 | [diff] [blame] | 134 | } |
Tom Care | 2cb5520 | 2010-09-29 23:48:34 +0000 | [diff] [blame] | 135 | } |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 136 | } |
Argyrios Kyrtzidis | 58f2e7c | 2011-02-28 01:26:50 +0000 | [diff] [blame] | 137 | |
| 138 | void ento::registerAnalyzerStatsChecker(CheckerManager &mgr) { |
| 139 | mgr.registerChecker<AnalyzerStatsChecker>(); |
| 140 | } |