Coverage Report

Created: 2021-08-24 07:12

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/StaticAnalyzer/Core/CheckerRegistryData.h
Line
Count
Source (jump to first uncovered line)
1
//===- CheckerRegistryData.h ------------------------------------*- 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 contains the data structures to which the TableGen file Checkers.td
10
// maps to, as well as what was parsed from the the specific invocation (whether
11
// a checker/package is enabled, their options values, etc).
12
//
13
// The parsing of the invocation is done by CheckerRegistry, which is found in
14
// the Frontend library. This allows the Core and Checkers libraries to utilize
15
// this information, such as enforcing rules on checker dependency bug emission,
16
// ensuring all checker options were queried, etc.
17
//
18
//===----------------------------------------------------------------------===//
19
20
#ifndef LLVM_CLANG_STATICANALYZER_CORE_CHECKERREGISTRYDATA_H
21
#define LLVM_CLANG_STATICANALYZER_CORE_CHECKERREGISTRYDATA_H
22
23
#include "clang/Basic/LLVM.h"
24
#include "llvm/ADT/SetVector.h"
25
#include "llvm/ADT/StringMap.h"
26
#include "llvm/ADT/StringRef.h"
27
#include "llvm/Support/raw_ostream.h"
28
29
namespace clang {
30
31
class AnalyzerOptions;
32
33
namespace ento {
34
35
class CheckerManager;
36
37
/// Initialization functions perform any necessary setup for a checker.
38
/// They should include a call to CheckerManager::registerChecker.
39
using RegisterCheckerFn = void (*)(CheckerManager &);
40
using ShouldRegisterFunction = bool (*)(const CheckerManager &);
41
42
/// Specifies a command line option. It may either belong to a checker or a
43
/// package.
44
struct CmdLineOption {
45
  StringRef OptionType;
46
  StringRef OptionName;
47
  StringRef DefaultValStr;
48
  StringRef Description;
49
  StringRef DevelopmentStatus;
50
  bool IsHidden;
51
52
  CmdLineOption(StringRef OptionType, StringRef OptionName,
53
                StringRef DefaultValStr, StringRef Description,
54
                StringRef DevelopmentStatus, bool IsHidden)
55
      : OptionType(OptionType), OptionName(OptionName),
56
        DefaultValStr(DefaultValStr), Description(Description),
57
83.6k
        DevelopmentStatus(DevelopmentStatus), IsHidden(IsHidden) {
58
59
83.6k
    assert((OptionType == "bool" || OptionType == "string" ||
60
83.6k
            OptionType == "int") &&
61
83.6k
           "Unknown command line option type!");
62
63
0
    assert((OptionType != "bool" ||
64
83.6k
            (DefaultValStr == "true" || DefaultValStr == "false")) &&
65
83.6k
           "Invalid value for boolean command line option! Maybe incorrect "
66
83.6k
           "parameters to the addCheckerOption or addPackageOption method?");
67
68
0
    int Tmp;
69
83.6k
    assert((OptionType != "int" || !DefaultValStr.getAsInteger(0, Tmp)) &&
70
83.6k
           "Invalid value for integer command line option! Maybe incorrect "
71
83.6k
           "parameters to the addCheckerOption or addPackageOption method?");
72
0
    (void)Tmp;
73
74
83.6k
    assert((DevelopmentStatus == "alpha" || DevelopmentStatus == "beta" ||
75
83.6k
            DevelopmentStatus == "released") &&
76
83.6k
           "Invalid development status!");
77
83.6k
  }
78
79
  LLVM_DUMP_METHOD void dump() const;
80
  LLVM_DUMP_METHOD void dumpToStream(llvm::raw_ostream &Out) const;
81
};
82
83
using CmdLineOptionList = llvm::SmallVector<CmdLineOption, 0>;
84
85
struct CheckerInfo;
86
87
using CheckerInfoList = std::vector<CheckerInfo>;
88
using CheckerInfoListRange = llvm::iterator_range<CheckerInfoList::iterator>;
89
using ConstCheckerInfoList = llvm::SmallVector<const CheckerInfo *, 0>;
90
using CheckerInfoSet = llvm::SetVector<const CheckerInfo *>;
91
92
/// Specifies a checker. Note that this isn't what we call a checker object,
93
/// it merely contains everything required to create one.
94
struct CheckerInfo {
95
  enum class StateFromCmdLine {
96
    // This checker wasn't explicitly enabled or disabled.
97
    State_Unspecified,
98
    // This checker was explicitly disabled.
99
    State_Disabled,
100
    // This checker was explicitly enabled.
101
    State_Enabled
102
  };
103
104
  RegisterCheckerFn Initialize = nullptr;
105
  ShouldRegisterFunction ShouldRegister = nullptr;
106
  StringRef FullName;
107
  StringRef Desc;
108
  StringRef DocumentationUri;
109
  CmdLineOptionList CmdLineOptions;
110
  bool IsHidden = false;
111
  StateFromCmdLine State = StateFromCmdLine::State_Unspecified;
112
113
  ConstCheckerInfoList Dependencies;
114
  ConstCheckerInfoList WeakDependencies;
115
116
524k
  bool isEnabled(const CheckerManager &mgr) const {
117
524k
    return State == StateFromCmdLine::State_Enabled && 
ShouldRegister(mgr)50.0k
;
118
524k
  }
119
120
9.24k
  bool isDisabled(const CheckerManager &mgr) const {
121
9.24k
    return State == StateFromCmdLine::State_Disabled || 
!ShouldRegister(mgr)9.24k
;
122
9.24k
  }
123
124
  // Since each checker must have a different full name, we can identify
125
  // CheckerInfo objects by them.
126
6.94M
  bool operator==(const CheckerInfo &Rhs) const {
127
6.94M
    return FullName == Rhs.FullName;
128
6.94M
  }
129
130
  CheckerInfo(RegisterCheckerFn Fn, ShouldRegisterFunction sfn, StringRef Name,
131
              StringRef Desc, StringRef DocsUri, bool IsHidden)
132
      : Initialize(Fn), ShouldRegister(sfn), FullName(Name), Desc(Desc),
133
262k
        DocumentationUri(DocsUri), IsHidden(IsHidden) {}
134
135
  // Used for lower_bound.
136
350k
  explicit CheckerInfo(StringRef FullName) : FullName(FullName) {}
137
138
  LLVM_DUMP_METHOD void dump() const;
139
  LLVM_DUMP_METHOD void dumpToStream(llvm::raw_ostream &Out) const;
140
};
141
142
using StateFromCmdLine = CheckerInfo::StateFromCmdLine;
143
144
/// Specifies a package. Each package option is implicitly an option for all
145
/// checkers within the package.
146
struct PackageInfo {
147
  StringRef FullName;
148
  CmdLineOptionList CmdLineOptions;
149
150
  // Since each package must have a different full name, we can identify
151
  // CheckerInfo objects by them.
152
44.3k
  bool operator==(const PackageInfo &Rhs) const {
153
44.3k
    return FullName == Rhs.FullName;
154
44.3k
  }
155
156
72.2k
  explicit PackageInfo(StringRef FullName) : FullName(FullName) {}
157
158
  LLVM_DUMP_METHOD void dump() const;
159
  LLVM_DUMP_METHOD void dumpToStream(llvm::raw_ostream &Out) const;
160
};
161
162
using PackageInfoList = llvm::SmallVector<PackageInfo, 0>;
163
164
namespace checker_registry {
165
166
template <class T> struct FullNameLT {
167
53.4M
  bool operator()(const T &Lhs, const T &Rhs) {
168
53.4M
    return Lhs.FullName < Rhs.FullName;
169
53.4M
  }
clang::ento::checker_registry::FullNameLT<clang::ento::PackageInfo>::operator()(clang::ento::PackageInfo const&, clang::ento::PackageInfo const&)
Line
Count
Source
167
437k
  bool operator()(const T &Lhs, const T &Rhs) {
168
437k
    return Lhs.FullName < Rhs.FullName;
169
437k
  }
clang::ento::checker_registry::FullNameLT<clang::ento::CheckerInfo>::operator()(clang::ento::CheckerInfo const&, clang::ento::CheckerInfo const&)
Line
Count
Source
167
53.0M
  bool operator()(const T &Lhs, const T &Rhs) {
168
53.0M
    return Lhs.FullName < Rhs.FullName;
169
53.0M
  }
170
};
171
172
using PackageNameLT = FullNameLT<PackageInfo>;
173
using CheckerNameLT = FullNameLT<CheckerInfo>;
174
175
template <class CheckerOrPackageInfoList>
176
std::conditional_t<std::is_const<CheckerOrPackageInfoList>::value,
177
                   typename CheckerOrPackageInfoList::const_iterator,
178
                   typename CheckerOrPackageInfoList::iterator>
179
267k
binaryFind(CheckerOrPackageInfoList &Collection, StringRef FullName) {
180
181
267k
  using CheckerOrPackage = typename CheckerOrPackageInfoList::value_type;
182
267k
  using CheckerOrPackageFullNameLT = FullNameLT<CheckerOrPackage>;
183
184
267k
  assert(llvm::is_sorted(Collection, CheckerOrPackageFullNameLT{}) &&
185
267k
         "In order to efficiently gather checkers/packages, this function "
186
267k
         "expects them to be already sorted!");
187
188
0
  return llvm::lower_bound(Collection, CheckerOrPackage(FullName),
189
267k
                           CheckerOrPackageFullNameLT{});
190
267k
}
std::__1::conditional<std::is_const<std::__1::vector<clang::ento::CheckerInfo, std::__1::allocator<clang::ento::CheckerInfo> > >::value, std::__1::vector<clang::ento::CheckerInfo, std::__1::allocator<clang::ento::CheckerInfo> >::const_iterator, std::__1::vector<clang::ento::CheckerInfo, std::__1::allocator<clang::ento::CheckerInfo> >::iterator>::type clang::ento::checker_registry::binaryFind<std::__1::vector<clang::ento::CheckerInfo, std::__1::allocator<clang::ento::CheckerInfo> > >(std::__1::vector<clang::ento::CheckerInfo, std::__1::allocator<clang::ento::CheckerInfo> >&, llvm::StringRef)
Line
Count
Source
179
266k
binaryFind(CheckerOrPackageInfoList &Collection, StringRef FullName) {
180
181
266k
  using CheckerOrPackage = typename CheckerOrPackageInfoList::value_type;
182
266k
  using CheckerOrPackageFullNameLT = FullNameLT<CheckerOrPackage>;
183
184
266k
  assert(llvm::is_sorted(Collection, CheckerOrPackageFullNameLT{}) &&
185
266k
         "In order to efficiently gather checkers/packages, this function "
186
266k
         "expects them to be already sorted!");
187
188
0
  return llvm::lower_bound(Collection, CheckerOrPackage(FullName),
189
266k
                           CheckerOrPackageFullNameLT{});
190
266k
}
std::__1::conditional<std::is_const<llvm::SmallVector<clang::ento::PackageInfo, 0u> >::value, llvm::SmallVector<clang::ento::PackageInfo, 0u>::const_iterator, llvm::SmallVector<clang::ento::PackageInfo, 0u>::iterator>::type clang::ento::checker_registry::binaryFind<llvm::SmallVector<clang::ento::PackageInfo, 0u> >(llvm::SmallVector<clang::ento::PackageInfo, 0u>&, llvm::StringRef)
Line
Count
Source
179
1.41k
binaryFind(CheckerOrPackageInfoList &Collection, StringRef FullName) {
180
181
1.41k
  using CheckerOrPackage = typename CheckerOrPackageInfoList::value_type;
182
1.41k
  using CheckerOrPackageFullNameLT = FullNameLT<CheckerOrPackage>;
183
184
1.41k
  assert(llvm::is_sorted(Collection, CheckerOrPackageFullNameLT{}) &&
185
1.41k
         "In order to efficiently gather checkers/packages, this function "
186
1.41k
         "expects them to be already sorted!");
187
188
0
  return llvm::lower_bound(Collection, CheckerOrPackage(FullName),
189
1.41k
                           CheckerOrPackageFullNameLT{});
190
1.41k
}
191
} // namespace checker_registry
192
193
struct CheckerRegistryData {
194
public:
195
  CheckerInfoSet EnabledCheckers;
196
197
  CheckerInfoList Checkers;
198
  PackageInfoList Packages;
199
  /// Used for counting how many checkers belong to a certain package in the
200
  /// \c Checkers field. For convenience purposes.
201
  llvm::StringMap<size_t> PackageSizes;
202
203
  /// Contains all (FullName, CmdLineOption) pairs. Similarly to dependencies,
204
  /// we only modify the actual CheckerInfo and PackageInfo objects once all
205
  /// of them have been added.
206
  llvm::SmallVector<std::pair<StringRef, CmdLineOption>, 0> PackageOptions;
207
  llvm::SmallVector<std::pair<StringRef, CmdLineOption>, 0> CheckerOptions;
208
209
  llvm::SmallVector<std::pair<StringRef, StringRef>, 0> Dependencies;
210
  llvm::SmallVector<std::pair<StringRef, StringRef>, 0> WeakDependencies;
211
212
  CheckerInfoListRange getMutableCheckersForCmdLineArg(StringRef CmdLineArg);
213
214
  /// Prints the name and description of all checkers in this registry.
215
  /// This output is not intended to be machine-parseable.
216
  void printCheckerWithDescList(const AnalyzerOptions &AnOpts, raw_ostream &Out,
217
                                size_t MaxNameChars = 30) const;
218
  void printEnabledCheckerList(raw_ostream &Out) const;
219
  void printCheckerOptionList(const AnalyzerOptions &AnOpts,
220
                              raw_ostream &Out) const;
221
};
222
223
} // namespace ento
224
} // namespace clang
225
226
#endif // LLVM_CLANG_STATICANALYZER_CORE_CHECKERREGISTRYDATA_H