AnalyzerStatsChecker.cpp revision 55fc873017f10f6f566b182b70f6fc22aefa3464
11e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)//==--AnalyzerStatsChecker.cpp - Analyzer visitation statistics --*- C++ -*-==//
21e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)//
31e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)//                     The LLVM Compiler Infrastructure
41e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)//
51e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// This file is distributed under the University of Illinois Open Source
61e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// License. See LICENSE.TXT for details.
71e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)//
8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)//===----------------------------------------------------------------------===//
9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// This file reports various statistics about analyzer visitation.
10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)//===----------------------------------------------------------------------===//
11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#define DEBUG_TYPE "StatsChecker"
120f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
130f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "ClangSACheckers.h"
141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "clang/AST/DeclObjC.h"
15f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "clang/Basic/SourceManager.h"
161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "clang/StaticAnalyzer/Core/Checker.h"
181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "clang/StaticAnalyzer/Core/CheckerManager.h"
191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "llvm/ADT/SmallPtrSet.h"
221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "llvm/ADT/SmallString.h"
231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "llvm/ADT/Statistic.h"
241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "llvm/Support/raw_ostream.h"
251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
26f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)using namespace clang;
271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)using namespace ento;
28f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
290f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)STATISTIC(NumBlocks,
30f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          "The # of blocks in top level functions");
311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)STATISTIC(NumBlocksUnreachable,
32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          "The # of unreachable blocks in analyzing top level functions");
33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)namespace {
35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class AnalyzerStatsChecker : public Checker<check::EndAnalysis> {
36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)public:
371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,ExprEngine &Eng) const;
38f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)};
39f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
41c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochvoid AnalyzerStatsChecker::checkEndAnalysis(ExplodedGraph &G,
42c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                            BugReporter &B,
43c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                            ExprEngine &Eng) const {
44c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  const CFG *C  = 0;
45f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const SourceManager &SM = B.getSourceManager();
46f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  llvm::SmallPtrSet<const CFGBlock*, 256> reachable;
47f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Root node should have the location context of the top most function.
49f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const ExplodedNode *GraphRoot = *G.roots_begin();
501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  const LocationContext *LC = GraphRoot->getLocation().getLocationContext();
510f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
52c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  const Decl *D = LC->getDecl();
53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Iterate over the exploded graph.
55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  for (ExplodedGraph::node_iterator I = G.nodes_begin();
56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      I != G.nodes_end(); ++I) {
57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const ProgramPoint &P = I->getLocation();
58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Only check the coverage in the top level function (optimization).
60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (D != P.getLocationContext()->getDecl())
61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      continue;
62a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
63a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      const CFGBlock *CB = BE->getBlock();
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      reachable.insert(CB);
66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Get the CFG and the Decl of this block.
70a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  C = LC->getCFG();
71a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
72a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  unsigned total = 0, unreachable = 0;
73a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
74a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Find CFGBlocks that were not covered by any node
75a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  for (CFG::const_iterator I = C->begin(); I != C->end(); ++I) {
76a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const CFGBlock *CB = *I;
77a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ++total;
78a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Check if the block is unreachable
79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (!reachable.count(CB)) {
80a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      ++unreachable;
81a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
82a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
831e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
84f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // We never 'reach' the entry block, so correct the unreachable count
851e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  unreachable--;
861e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // There is no BlockEntrance corresponding to the exit block as well, so
871e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // assume it is reached as well.
881e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  unreachable--;
891e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
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
138void ento::registerAnalyzerStatsChecker(CheckerManager &mgr) {
139  mgr.registerChecker<AnalyzerStatsChecker>();
140}
141