Home | History | Annotate | Download | only in Checkers
      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 #define DEBUG_TYPE "StatsChecker"
     12 
     13 #include "ClangSACheckers.h"
     14 #include "clang/StaticAnalyzer/Core/Checker.h"
     15 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
     16 #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
     17 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
     18 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
     19 
     20 #include "clang/AST/DeclObjC.h"
     21 #include "clang/Basic/SourceManager.h"
     22 #include "llvm/ADT/SmallPtrSet.h"
     23 #include "llvm/ADT/SmallString.h"
     24 #include "llvm/ADT/Statistic.h"
     25 
     26 using namespace clang;
     27 using namespace ento;
     28 
     29 STATISTIC(NumBlocks,
     30           "The # of blocks in top level functions");
     31 STATISTIC(NumBlocksUnreachable,
     32           "The # of unreachable blocks in analyzing top level functions");
     33 
     34 namespace {
     35 class AnalyzerStatsChecker : public Checker<check::EndAnalysis> {
     36 public:
     37   void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,ExprEngine &Eng) const;
     38 };
     39 }
     40 
     41 void AnalyzerStatsChecker::checkEndAnalysis(ExplodedGraph &G,
     42                                             BugReporter &B,
     43                                             ExprEngine &Eng) const {
     44   const CFG *C  = 0;
     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 (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
     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, "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 (const CFGStmt *CS = dyn_cast<CFGStmt>(&CE)) {
    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(D, "Sink Point", "Internal Statistics", outputI.str(),
    132                         PathDiagnosticLocation::createBegin(CS->getStmt(),
    133                                                             SM, LC));
    134     }
    135   }
    136 }
    137 
    138 void ento::registerAnalyzerStatsChecker(CheckerManager &mgr) {
    139   mgr.registerChecker<AnalyzerStatsChecker>();
    140 }
    141