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