clang  6.0.0svn
AnalyzerStatsChecker.cpp
Go to the documentation of this file.
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 #include "ClangSACheckers.h"
12 #include "clang/AST/DeclObjC.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/SmallString.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/Support/raw_ostream.h"
23 
24 using namespace clang;
25 using namespace ento;
26 
27 #define DEBUG_TYPE "StatsChecker"
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 = nullptr;
45  const SourceManager &SM = B.getSourceManager();
46  llvm::SmallPtrSet<const CFGBlock*, 32> 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.
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 
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);
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 
138 void ento::registerAnalyzerStatsChecker(CheckerManager &mgr) {
139  mgr.registerChecker<AnalyzerStatsChecker>();
140 }
CFGElement front() const
Definition: CFG.h:573
Defines the SourceManager interface.
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
StringRef P
roots_iterator roots_begin()
BlocksExhausted::const_iterator blocks_exhausted_end() const
Definition: CoreEngine.h:157
bool hasEmptyWorkList() const
Definition: ExprEngine.h:324
BlocksExhausted::const_iterator blocks_exhausted_begin() const
Definition: CoreEngine.h:154
bool wasBlocksExhausted() const
Definition: ExprEngine.h:323
iterator end()
Definition: CFG.h:907
const CoreEngine & getCoreEngine() const
Definition: ExprEngine.h:327
STATISTIC(NumBlocks, "The # of blocks in top level functions")
CFGBlockListTy::const_iterator const_iterator
Definition: CFG.h:899
CFGBlock - Represents a single basic block in a source-level CFG.
Definition: CFG.h:422
bool isValid() const
CFG - Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt...
Definition: CFG.h:834
unsigned getLine() const
Return the presumed line number of this location.
const CFGBlock * getDst() const
Definition: ProgramPoint.h:484
Represents an unpacked "presumed" location which can be presented to the user.
const SourceManager & SM
Definition: Format.cpp:1337
BugReporter is a utility class for generating PathDiagnostics for analysis.
Definition: BugReporter.h:403
static PathDiagnosticLocation createBegin(const Decl *D, const SourceManager &SM)
Create a location for the beginning of the declaration.
CHECKER * registerChecker()
Used to register checkers.
node_iterator nodes_begin()
unsigned getColumn() const
Return the presumed column number of this location.
void EmitBasicReport(const Decl *DeclWithIssue, const CheckerBase *Checker, StringRef BugName, StringRef BugCategory, StringRef BugStr, PathDiagnosticLocation Loc, ArrayRef< SourceRange > Ranges=None)
ProgramPoint getLocation() const
getLocation - Returns the edge associated with the given node.
iterator begin()
Definition: CFG.h:906
Optional< T > getAs() const
Convert to the specified CFGElement type, returning None if this CFGElement is not of the desired typ...
Definition: CFG.h:101
CoreEngine - Implements the core logic of the graph-reachability analysis.
Definition: CoreEngine.h:43
Dataflow Directional Tag Classes.
PresumedLoc getPresumedLoc(SourceLocation Loc, bool UseLineDirectives=true) const
Returns the "presumed" location of a SourceLocation specifies.
const Decl * getDecl() const
const LocationContext * getLocationContext() const
Definition: ProgramPoint.h:179
SourceManager & getSourceManager()
Definition: BugReporter.h:463
CFGElement - Represents a top-level expression in a basic block.
Definition: CFG.h:54
AllNodesTy::iterator node_iterator
NamedDecl - This represents a decl with a name.
Definition: Decl.h:245
Optional< T > getAs() const
Convert to the specified ProgramPoint type, returning None if this ProgramPoint is not of the desired...
Definition: ProgramPoint.h:151
This class handles loading and caching of source files into memory.
SourceLocation getLocation() const
Definition: DeclBase.h:416