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 | //===----------------------------------------------------------------------===// |
| 11 | |
Argyrios Kyrtzidis | 58f2e7c | 2011-02-28 01:26:50 +0000 | [diff] [blame] | 12 | #include "ClangSACheckers.h" |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 13 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 58f2e7c | 2011-02-28 01:26:50 +0000 | [diff] [blame] | 14 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Ted Kremenek | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 15 | #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h" |
Argyrios Kyrtzidis | 58f2e7c | 2011-02-28 01:26:50 +0000 | [diff] [blame] | 16 | #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" |
Ted Kremenek | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 17 | #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" |
Argyrios Kyrtzidis | a7af5ea | 2010-12-22 18:52:56 +0000 | [diff] [blame] | 18 | |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 19 | #include "clang/Basic/SourceManager.h" |
| 20 | #include "llvm/ADT/SmallPtrSet.h" |
| 21 | |
| 22 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 23 | using namespace ento; |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 24 | |
| 25 | namespace { |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 26 | class AnalyzerStatsChecker : public Checker<check::EndAnalysis> { |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 27 | public: |
Argyrios Kyrtzidis | 58f2e7c | 2011-02-28 01:26:50 +0000 | [diff] [blame] | 28 | void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,ExprEngine &Eng) const; |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 29 | }; |
| 30 | } |
| 31 | |
Argyrios Kyrtzidis | 58f2e7c | 2011-02-28 01:26:50 +0000 | [diff] [blame] | 32 | void AnalyzerStatsChecker::checkEndAnalysis(ExplodedGraph &G, |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 33 | BugReporter &B, |
Argyrios Kyrtzidis | 58f2e7c | 2011-02-28 01:26:50 +0000 | [diff] [blame] | 34 | ExprEngine &Eng) const { |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 35 | const CFG *C = 0; |
| 36 | const Decl *D = 0; |
| 37 | const LocationContext *LC = 0; |
| 38 | const SourceManager &SM = B.getSourceManager(); |
Argyrios Kyrtzidis | 58f2e7c | 2011-02-28 01:26:50 +0000 | [diff] [blame] | 39 | llvm::SmallPtrSet<const CFGBlock*, 256> reachable; |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 40 | |
| 41 | // Iterate over explodedgraph |
| 42 | for (ExplodedGraph::node_iterator I = G.nodes_begin(); |
| 43 | I != G.nodes_end(); ++I) { |
| 44 | const ProgramPoint &P = I->getLocation(); |
| 45 | // Save the LocationContext if we don't have it already |
| 46 | if (!LC) |
| 47 | LC = P.getLocationContext(); |
| 48 | |
Tom Care | 2cb5520 | 2010-09-29 23:48:34 +0000 | [diff] [blame] | 49 | if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) { |
| 50 | const CFGBlock *CB = BE->getBlock(); |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 51 | reachable.insert(CB); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // Get the CFG and the Decl of this block |
| 56 | C = LC->getCFG(); |
| 57 | D = LC->getAnalysisContext()->getDecl(); |
| 58 | |
| 59 | unsigned total = 0, unreachable = 0; |
| 60 | |
| 61 | // Find CFGBlocks that were not covered by any node |
| 62 | for (CFG::const_iterator I = C->begin(); I != C->end(); ++I) { |
| 63 | const CFGBlock *CB = *I; |
| 64 | ++total; |
| 65 | // Check if the block is unreachable |
| 66 | if (!reachable.count(CB)) { |
| 67 | ++unreachable; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // We never 'reach' the entry block, so correct the unreachable count |
| 72 | unreachable--; |
| 73 | |
| 74 | // Generate the warning string |
| 75 | llvm::SmallString<128> buf; |
| 76 | llvm::raw_svector_ostream output(buf); |
| 77 | PresumedLoc Loc = SM.getPresumedLoc(D->getLocation()); |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 78 | if (Loc.isValid()) { |
| 79 | output << Loc.getFilename() << " : "; |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 80 | |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 81 | if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) { |
| 82 | const NamedDecl *ND = cast<NamedDecl>(D); |
| 83 | output << ND; |
| 84 | } |
| 85 | else if (isa<BlockDecl>(D)) { |
| 86 | output << "block(line:" << Loc.getLine() << ":col:" << Loc.getColumn(); |
| 87 | } |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 88 | } |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 89 | |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 90 | output << " -> Total CFGBlocks: " << total << " | Unreachable CFGBlocks: " |
Ted Kremenek | 422ab7a | 2011-04-02 02:56:23 +0000 | [diff] [blame] | 91 | << unreachable << " | Exhausted Block: " |
| 92 | << (Eng.wasBlocksExhausted() ? "yes" : "no") |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 93 | << " | Empty WorkList: " |
Tom Care | 0e1cd94 | 2010-09-22 21:07:51 +0000 | [diff] [blame] | 94 | << (Eng.hasEmptyWorkList() ? "yes" : "no"); |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 95 | |
| 96 | B.EmitBasicReport("Analyzer Statistics", "Internal Statistics", output.str(), |
Anna Zaks | 590dd8e | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 97 | PathDiagnosticLocation(D, SM)); |
Tom Care | 2cb5520 | 2010-09-29 23:48:34 +0000 | [diff] [blame] | 98 | |
| 99 | // Emit warning for each block we bailed out on |
Ted Kremenek | 422ab7a | 2011-04-02 02:56:23 +0000 | [diff] [blame] | 100 | typedef CoreEngine::BlocksExhausted::const_iterator ExhaustedIterator; |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame] | 101 | const CoreEngine &CE = Eng.getCoreEngine(); |
Ted Kremenek | 422ab7a | 2011-04-02 02:56:23 +0000 | [diff] [blame] | 102 | for (ExhaustedIterator I = CE.blocks_exhausted_begin(), |
| 103 | E = CE.blocks_exhausted_end(); I != E; ++I) { |
Tom Care | 2cb5520 | 2010-09-29 23:48:34 +0000 | [diff] [blame] | 104 | const BlockEdge &BE = I->first; |
| 105 | const CFGBlock *Exit = BE.getDst(); |
| 106 | const CFGElement &CE = Exit->front(); |
| 107 | if (const CFGStmt *CS = dyn_cast<CFGStmt>(&CE)) |
| 108 | B.EmitBasicReport("Bailout Point", "Internal Statistics", "The analyzer " |
Anna Zaks | 590dd8e | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 109 | "stopped analyzing at this point", |
| 110 | PathDiagnosticLocation::createBegin(CS->getStmt(), SM, LC)); |
Tom Care | 2cb5520 | 2010-09-29 23:48:34 +0000 | [diff] [blame] | 111 | } |
Tom Care | 52d861c | 2010-09-10 00:44:44 +0000 | [diff] [blame] | 112 | } |
Argyrios Kyrtzidis | 58f2e7c | 2011-02-28 01:26:50 +0000 | [diff] [blame] | 113 | |
| 114 | void ento::registerAnalyzerStatsChecker(CheckerManager &mgr) { |
| 115 | mgr.registerChecker<AnalyzerStatsChecker>(); |
| 116 | } |