Coverage Report

Created: 2023-05-31 04:38

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/StaticAnalyzer/Core/PathSensitive/FunctionSummary.h
Line
Count
Source (jump to first uncovered line)
1
//===- FunctionSummary.h - Stores summaries of functions. -------*- 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
//
9
// This file defines a summary of a function gathered/used by static analysis.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_FUNCTIONSUMMARY_H
14
#define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_FUNCTIONSUMMARY_H
15
16
#include "clang/AST/Decl.h"
17
#include "clang/Basic/LLVM.h"
18
#include "llvm/ADT/DenseMap.h"
19
#include "llvm/ADT/DenseSet.h"
20
#include "llvm/ADT/SmallBitVector.h"
21
#include <cassert>
22
#include <deque>
23
#include <optional>
24
#include <utility>
25
26
namespace clang {
27
namespace ento {
28
29
using SetOfDecls = std::deque<Decl *>;
30
using SetOfConstDecls = llvm::DenseSet<const Decl *>;
31
32
class FunctionSummariesTy {
33
  class FunctionSummary {
34
  public:
35
    /// Marks the IDs of the basic blocks visited during the analyzes.
36
    llvm::SmallBitVector VisitedBasicBlocks;
37
38
    /// Total number of blocks in the function.
39
    unsigned TotalBasicBlocks : 30;
40
41
    /// True if this function has been checked against the rules for which
42
    /// functions may be inlined.
43
    unsigned InlineChecked : 1;
44
45
    /// True if this function may be inlined.
46
    unsigned MayInline : 1;
47
48
    /// The number of times the function has been inlined.
49
    unsigned TimesInlined : 32;
50
51
    FunctionSummary()
52
        : TotalBasicBlocks(0), InlineChecked(0), MayInline(0),
53
21.5k
          TimesInlined(0) {}
54
  };
55
56
  using MapTy = llvm::DenseMap<const Decl *, FunctionSummary>;
57
  MapTy Map;
58
59
public:
60
292k
  MapTy::iterator findOrInsertSummary(const Decl *D) {
61
292k
    MapTy::iterator I = Map.find(D);
62
292k
    if (I != Map.end())
63
270k
      return I;
64
65
21.5k
    using KVPair = std::pair<const Decl *, FunctionSummary>;
66
67
21.5k
    I = Map.insert(KVPair(D, FunctionSummary())).first;
68
21.5k
    assert(I != Map.end());
69
21.5k
    return I;
70
21.5k
  }
71
72
5.11k
  void markMayInline(const Decl *D) {
73
5.11k
    MapTy::iterator I = findOrInsertSummary(D);
74
5.11k
    I->second.InlineChecked = 1;
75
5.11k
    I->second.MayInline = 1;
76
5.11k
  }
77
78
881
  void markShouldNotInline(const Decl *D) {
79
881
    MapTy::iterator I = findOrInsertSummary(D);
80
881
    I->second.InlineChecked = 1;
81
881
    I->second.MayInline = 0;
82
881
  }
83
84
47
  void markReachedMaxBlockCount(const Decl *D) {
85
47
    markShouldNotInline(D);
86
47
  }
87
88
33.4k
  std::optional<bool> mayInline(const Decl *D) {
89
33.4k
    MapTy::const_iterator I = Map.find(D);
90
33.4k
    if (I != Map.end() && 
I->second.InlineChecked27.6k
)
91
27.5k
      return I->second.MayInline;
92
5.92k
    return std::nullopt;
93
33.4k
  }
94
95
251k
  void markVisitedBasicBlock(unsigned ID, const Decl* D, unsigned TotalIDs) {
96
251k
    MapTy::iterator I = findOrInsertSummary(D);
97
251k
    llvm::SmallBitVector &Blocks = I->second.VisitedBasicBlocks;
98
251k
    assert(ID < TotalIDs);
99
251k
    if (TotalIDs > Blocks.size()) {
100
20.7k
      Blocks.resize(TotalIDs);
101
20.7k
      I->second.TotalBasicBlocks = TotalIDs;
102
20.7k
    }
103
251k
    Blocks.set(ID);
104
251k
  }
105
106
0
  unsigned getNumVisitedBasicBlocks(const Decl* D) {
107
0
    MapTy::const_iterator I = Map.find(D);
108
0
    if (I != Map.end())
109
0
      return I->second.VisitedBasicBlocks.count();
110
0
    return 0;
111
0
  }
112
113
28.3k
  unsigned getNumTimesInlined(const Decl* D) {
114
28.3k
    MapTy::const_iterator I = Map.find(D);
115
28.3k
    if (I != Map.end())
116
28.3k
      return I->second.TimesInlined;
117
0
    return 0;
118
28.3k
  }
119
120
35.0k
  void bumpNumTimesInlined(const Decl* D) {
121
35.0k
    MapTy::iterator I = findOrInsertSummary(D);
122
35.0k
    I->second.TimesInlined++;
123
35.0k
  }
124
125
  /// Get the percentage of the reachable blocks.
126
0
  unsigned getPercentBlocksReachable(const Decl *D) {
127
0
    MapTy::const_iterator I = Map.find(D);
128
0
      if (I != Map.end())
129
0
        return ((I->second.VisitedBasicBlocks.count() * 100) /
130
0
                 I->second.TotalBasicBlocks);
131
0
    return 0;
132
0
  }
133
134
  unsigned getTotalNumBasicBlocks();
135
  unsigned getTotalNumVisitedBasicBlocks();
136
};
137
138
} // namespace ento
139
} // namespace clang
140
141
#endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_FUNCTIONSUMMARY_H