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