Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/StaticAnalyzer/Checkers/AnalyzerStatsChecker.cpp
Line
Count
Source (jump to first uncovered line)
1
//==--AnalyzerStatsChecker.cpp - Analyzer visitation statistics --*- C++ -*-==//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
// This file reports various statistics about analyzer visitation.
9
//===----------------------------------------------------------------------===//
10
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
11
#include "clang/AST/DeclObjC.h"
12
#include "clang/Basic/SourceManager.h"
13
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.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 "llvm/ADT/SmallPtrSet.h"
19
#include "llvm/ADT/SmallString.h"
20
#include "llvm/ADT/Statistic.h"
21
#include "llvm/Support/raw_ostream.h"
22
23
using namespace clang;
24
using namespace ento;
25
26
#define DEBUG_TYPE "StatsChecker"
27
28
STATISTIC(NumBlocks,
29
          "The # of blocks in top level functions");
30
STATISTIC(NumBlocksUnreachable,
31
          "The # of unreachable blocks in analyzing top level functions");
32
33
namespace {
34
class AnalyzerStatsChecker : public Checker<check::EndAnalysis> {
35
public:
36
  void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,ExprEngine &Eng) const;
37
};
38
}
39
40
void AnalyzerStatsChecker::checkEndAnalysis(ExplodedGraph &G,
41
                                            BugReporter &B,
42
4
                                            ExprEngine &Eng) const {
43
4
  const CFG *C = nullptr;
44
4
  const SourceManager &SM = B.getSourceManager();
45
4
  llvm::SmallPtrSet<const CFGBlock*, 32> reachable;
46
4
47
4
  // Root node should have the location context of the top most function.
48
4
  const ExplodedNode *GraphRoot = *G.roots_begin();
49
4
  const LocationContext *LC = GraphRoot->getLocation().getLocationContext();
50
4
51
4
  const Decl *D = LC->getDecl();
52
4
53
4
  // Iterate over the exploded graph.
54
4
  for (ExplodedGraph::node_iterator I = G.nodes_begin();
55
261
      I != G.nodes_end(); 
++I257
) {
56
257
    const ProgramPoint &P = I->getLocation();
57
257
58
257
    // Only check the coverage in the top level function (optimization).
59
257
    if (D != P.getLocationContext()->getDecl())
60
2
      continue;
61
255
62
255
    if (Optional<BlockEntrance> BE = P.getAs<BlockEntrance>()) {
63
32
      const CFGBlock *CB = BE->getBlock();
64
32
      reachable.insert(CB);
65
32
    }
66
255
  }
67
4
68
4
  // Get the CFG and the Decl of this block.
69
4
  C = LC->getCFG();
70
4
71
4
  unsigned total = 0, unreachable = 0;
72
4
73
4
  // Find CFGBlocks that were not covered by any node
74
25
  for (CFG::const_iterator I = C->begin(); I != C->end(); 
++I21
) {
75
21
    const CFGBlock *CB = *I;
76
21
    ++total;
77
21
    // Check if the block is unreachable
78
21
    if (!reachable.count(CB)) {
79
9
      ++unreachable;
80
9
    }
81
21
  }
82
4
83
4
  // We never 'reach' the entry block, so correct the unreachable count
84
4
  unreachable--;
85
4
  // There is no BlockEntrance corresponding to the exit block as well, so
86
4
  // assume it is reached as well.
87
4
  unreachable--;
88
4
89
4
  // Generate the warning string
90
4
  SmallString<128> buf;
91
4
  llvm::raw_svector_ostream output(buf);
92
4
  PresumedLoc Loc = SM.getPresumedLoc(D->getLocation());
93
4
  if (!Loc.isValid())
94
0
    return;
95
4
96
4
  if (isa<FunctionDecl>(D) || 
isa<ObjCMethodDecl>(D)0
) {
97
4
    const NamedDecl *ND = cast<NamedDecl>(D);
98
4
    output << *ND;
99
4
  }
100
0
  else if (isa<BlockDecl>(D)) {
101
0
    output << "block(line:" << Loc.getLine() << ":col:" << Loc.getColumn();
102
0
  }
103
4
104
4
  NumBlocksUnreachable += unreachable;
105
4
  NumBlocks += total;
106
4
  std::string NameOfRootFunction = output.str();
107
4
108
4
  output << " -> Total CFGBlocks: " << total << " | Unreachable CFGBlocks: "
109
4
      << unreachable << " | Exhausted Block: "
110
4
      << (Eng.wasBlocksExhausted() ? 
"yes"2
:
"no"2
)
111
4
      << " | Empty WorkList: "
112
4
      << (Eng.hasEmptyWorkList() ? "yes" : 
"no"0
);
113
4
114
4
  B.EmitBasicReport(D, this, "Analyzer Statistics", "Internal Statistics",
115
4
                    output.str(), PathDiagnosticLocation(D, SM));
116
4
117
4
  // Emit warning for each block we bailed out on.
118
4
  typedef CoreEngine::BlocksExhausted::const_iterator ExhaustedIterator;
119
4
  const CoreEngine &CE = Eng.getCoreEngine();
120
4
  for (ExhaustedIterator I = CE.blocks_exhausted_begin(),
121
6
      E = CE.blocks_exhausted_end(); I != E; 
++I2
) {
122
2
    const BlockEdge &BE =  I->first;
123
2
    const CFGBlock *Exit = BE.getDst();
124
2
    if (Exit->empty())
125
1
      continue;
126
1
    const CFGElement &CE = Exit->front();
127
1
    if (Optional<CFGStmt> CS = CE.getAs<CFGStmt>()) {
128
1
      SmallString<128> bufI;
129
1
      llvm::raw_svector_ostream outputI(bufI);
130
1
      outputI << "(" << NameOfRootFunction << ")" <<
131
1
                 ": The analyzer generated a sink at this point";
132
1
      B.EmitBasicReport(
133
1
          D, this, "Sink Point", "Internal Statistics", outputI.str(),
134
1
          PathDiagnosticLocation::createBegin(CS->getStmt(), SM, LC));
135
1
    }
136
1
  }
137
4
}
138
139
2
void ento::registerAnalyzerStatsChecker(CheckerManager &mgr) {
140
2
  mgr.registerChecker<AnalyzerStatsChecker>();
141
2
}
142
143
2
bool ento::shouldRegisterAnalyzerStatsChecker(const LangOptions &LO) {
144
2
  return true;
145
2
}