clang API Documentation

AnalyzerStatsChecker.cpp
Go to the documentation of this file.
00001 //==--AnalyzerStatsChecker.cpp - Analyzer visitation statistics --*- C++ -*-==//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 // This file reports various statistics about analyzer visitation.
00010 //===----------------------------------------------------------------------===//
00011 #include "ClangSACheckers.h"
00012 #include "clang/AST/DeclObjC.h"
00013 #include "clang/Basic/SourceManager.h"
00014 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
00015 #include "clang/StaticAnalyzer/Core/Checker.h"
00016 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
00017 #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
00018 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
00019 #include "llvm/ADT/SmallPtrSet.h"
00020 #include "llvm/ADT/SmallString.h"
00021 #include "llvm/ADT/Statistic.h"
00022 #include "llvm/Support/raw_ostream.h"
00023 
00024 using namespace clang;
00025 using namespace ento;
00026 
00027 #define DEBUG_TYPE "StatsChecker"
00028 
00029 STATISTIC(NumBlocks,
00030           "The # of blocks in top level functions");
00031 STATISTIC(NumBlocksUnreachable,
00032           "The # of unreachable blocks in analyzing top level functions");
00033 
00034 namespace {
00035 class AnalyzerStatsChecker : public Checker<check::EndAnalysis> {
00036 public:
00037   void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,ExprEngine &Eng) const;
00038 };
00039 }
00040 
00041 void AnalyzerStatsChecker::checkEndAnalysis(ExplodedGraph &G,
00042                                             BugReporter &B,
00043                                             ExprEngine &Eng) const {
00044   const CFG *C = nullptr;
00045   const SourceManager &SM = B.getSourceManager();
00046   llvm::SmallPtrSet<const CFGBlock*, 256> reachable;
00047 
00048   // Root node should have the location context of the top most function.
00049   const ExplodedNode *GraphRoot = *G.roots_begin();
00050   const LocationContext *LC = GraphRoot->getLocation().getLocationContext();
00051 
00052   const Decl *D = LC->getDecl();
00053 
00054   // Iterate over the exploded graph.
00055   for (ExplodedGraph::node_iterator I = G.nodes_begin();
00056       I != G.nodes_end(); ++I) {
00057     const ProgramPoint &P = I->getLocation();
00058 
00059     // Only check the coverage in the top level function (optimization).
00060     if (D != P.getLocationContext()->getDecl())
00061       continue;
00062 
00063     if (Optional<BlockEntrance> BE = P.getAs<BlockEntrance>()) {
00064       const CFGBlock *CB = BE->getBlock();
00065       reachable.insert(CB);
00066     }
00067   }
00068 
00069   // Get the CFG and the Decl of this block.
00070   C = LC->getCFG();
00071 
00072   unsigned total = 0, unreachable = 0;
00073 
00074   // Find CFGBlocks that were not covered by any node
00075   for (CFG::const_iterator I = C->begin(); I != C->end(); ++I) {
00076     const CFGBlock *CB = *I;
00077     ++total;
00078     // Check if the block is unreachable
00079     if (!reachable.count(CB)) {
00080       ++unreachable;
00081     }
00082   }
00083 
00084   // We never 'reach' the entry block, so correct the unreachable count
00085   unreachable--;
00086   // There is no BlockEntrance corresponding to the exit block as well, so
00087   // assume it is reached as well.
00088   unreachable--;
00089 
00090   // Generate the warning string
00091   SmallString<128> buf;
00092   llvm::raw_svector_ostream output(buf);
00093   PresumedLoc Loc = SM.getPresumedLoc(D->getLocation());
00094   if (!Loc.isValid())
00095     return;
00096 
00097   if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
00098     const NamedDecl *ND = cast<NamedDecl>(D);
00099     output << *ND;
00100   }
00101   else if (isa<BlockDecl>(D)) {
00102     output << "block(line:" << Loc.getLine() << ":col:" << Loc.getColumn();
00103   }
00104   
00105   NumBlocksUnreachable += unreachable;
00106   NumBlocks += total;
00107   std::string NameOfRootFunction = output.str();
00108 
00109   output << " -> Total CFGBlocks: " << total << " | Unreachable CFGBlocks: "
00110       << unreachable << " | Exhausted Block: "
00111       << (Eng.wasBlocksExhausted() ? "yes" : "no")
00112       << " | Empty WorkList: "
00113       << (Eng.hasEmptyWorkList() ? "yes" : "no");
00114 
00115   B.EmitBasicReport(D, this, "Analyzer Statistics", "Internal Statistics",
00116                     output.str(), PathDiagnosticLocation(D, SM));
00117 
00118   // Emit warning for each block we bailed out on.
00119   typedef CoreEngine::BlocksExhausted::const_iterator ExhaustedIterator;
00120   const CoreEngine &CE = Eng.getCoreEngine();
00121   for (ExhaustedIterator I = CE.blocks_exhausted_begin(),
00122       E = CE.blocks_exhausted_end(); I != E; ++I) {
00123     const BlockEdge &BE =  I->first;
00124     const CFGBlock *Exit = BE.getDst();
00125     const CFGElement &CE = Exit->front();
00126     if (Optional<CFGStmt> CS = CE.getAs<CFGStmt>()) {
00127       SmallString<128> bufI;
00128       llvm::raw_svector_ostream outputI(bufI);
00129       outputI << "(" << NameOfRootFunction << ")" <<
00130                  ": The analyzer generated a sink at this point";
00131       B.EmitBasicReport(
00132           D, this, "Sink Point", "Internal Statistics", outputI.str(),
00133           PathDiagnosticLocation::createBegin(CS->getStmt(), SM, LC));
00134     }
00135   }
00136 }
00137 
00138 void ento::registerAnalyzerStatsChecker(CheckerManager &mgr) {
00139   mgr.registerChecker<AnalyzerStatsChecker>();
00140 }