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