AnalyzerStatsChecker.cpp revision 6bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//==--AnalyzerStatsChecker.cpp - Analyzer visitation statistics --*- C++ -*-==// 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The LLVM Compiler Infrastructure 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file is distributed under the University of Illinois Open Source 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// License. See LICENSE.TXT for details. 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===// 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file reports various statistics about analyzer visitation. 10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)//===----------------------------------------------------------------------===// 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ClangSACheckers.h" 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/AST/DeclObjC.h" 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/Basic/SourceManager.h" 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/StaticAnalyzer/Core/Checker.h" 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/StaticAnalyzer/Core/CheckerManager.h" 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h" 1890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" 1990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "llvm/ADT/SmallPtrSet.h" 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/ADT/SmallString.h" 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/ADT/Statistic.h" 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Support/raw_ostream.h" 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using namespace clang; 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using namespace ento; 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DEBUG_TYPE "StatsChecker" 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)STATISTIC(NumBlocks, 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "The # of blocks in top level functions"); 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)STATISTIC(NumBlocksUnreachable, 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "The # of unreachable blocks in analyzing top level functions"); 3390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace { 351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass AnalyzerStatsChecker : public Checker<check::EndAnalysis> { 36c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)public: 37c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,ExprEngine &Eng) const; 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 41void AnalyzerStatsChecker::checkEndAnalysis(ExplodedGraph &G, 42 BugReporter &B, 43 ExprEngine &Eng) const { 44 const CFG *C = nullptr; 45 const SourceManager &SM = B.getSourceManager(); 46 llvm::SmallPtrSet<const CFGBlock*, 256> reachable; 47 48 // Root node should have the location context of the top most function. 49 const ExplodedNode *GraphRoot = *G.roots_begin(); 50 const LocationContext *LC = GraphRoot->getLocation().getLocationContext(); 51 52 const Decl *D = LC->getDecl(); 53 54 // Iterate over the exploded graph. 55 for (ExplodedGraph::node_iterator I = G.nodes_begin(); 56 I != G.nodes_end(); ++I) { 57 const ProgramPoint &P = I->getLocation(); 58 59 // Only check the coverage in the top level function (optimization). 60 if (D != P.getLocationContext()->getDecl()) 61 continue; 62 63 if (Optional<BlockEntrance> BE = P.getAs<BlockEntrance>()) { 64 const CFGBlock *CB = BE->getBlock(); 65 reachable.insert(CB); 66 } 67 } 68 69 // Get the CFG and the Decl of this block. 70 C = LC->getCFG(); 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--; 86 // There is no BlockEntrance corresponding to the exit block as well, so 87 // assume it is reached as well. 88 unreachable--; 89 90 // Generate the warning string 91 SmallString<128> buf; 92 llvm::raw_svector_ostream output(buf); 93 PresumedLoc Loc = SM.getPresumedLoc(D->getLocation()); 94 if (!Loc.isValid()) 95 return; 96 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(); 103 } 104 105 NumBlocksUnreachable += unreachable; 106 NumBlocks += total; 107 std::string NameOfRootFunction = output.str(); 108 109 output << " -> Total CFGBlocks: " << total << " | Unreachable CFGBlocks: " 110 << unreachable << " | Exhausted Block: " 111 << (Eng.wasBlocksExhausted() ? "yes" : "no") 112 << " | Empty WorkList: " 113 << (Eng.hasEmptyWorkList() ? "yes" : "no"); 114 115 B.EmitBasicReport(D, this, "Analyzer Statistics", "Internal Statistics", 116 output.str(), PathDiagnosticLocation(D, SM)); 117 118 // Emit warning for each block we bailed out on. 119 typedef CoreEngine::BlocksExhausted::const_iterator ExhaustedIterator; 120 const CoreEngine &CE = Eng.getCoreEngine(); 121 for (ExhaustedIterator I = CE.blocks_exhausted_begin(), 122 E = CE.blocks_exhausted_end(); I != E; ++I) { 123 const BlockEdge &BE = I->first; 124 const CFGBlock *Exit = BE.getDst(); 125 const CFGElement &CE = Exit->front(); 126 if (Optional<CFGStmt> CS = CE.getAs<CFGStmt>()) { 127 SmallString<128> bufI; 128 llvm::raw_svector_ostream outputI(bufI); 129 outputI << "(" << NameOfRootFunction << ")" << 130 ": The analyzer generated a sink at this point"; 131 B.EmitBasicReport( 132 D, this, "Sink Point", "Internal Statistics", outputI.str(), 133 PathDiagnosticLocation::createBegin(CS->getStmt(), SM, LC)); 134 } 135 } 136} 137 138void ento::registerAnalyzerStatsChecker(CheckerManager &mgr) { 139 mgr.registerChecker<AnalyzerStatsChecker>(); 140} 141