Coverage Report

Created: 2020-10-24 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/StaticAnalyzer/Core/CheckerRegistryData.h
Line
Count
Source
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
79.4k
        DevelopmentStatus(DevelopmentStatus), IsHidden(IsHidden) {
58
59
79.4k
    assert((OptionType == "bool" || OptionType == "string" ||
60
79.4k
            OptionType == "int") &&
61
79.4k
           "Unknown command line option type!");
62
63
79.4k
    assert((OptionType != "bool" ||
64
79.4k
            (DefaultValStr == "true" || DefaultValStr == "false")) &&
65
79.4k
           "Invalid value for boolean command line option! Maybe incorrect "
66
79.4k
           "parameters to the addCheckerOption or addPackageOption method?");
67
68
79.4k
    int Tmp;
69
79.4k
    assert((OptionType != "int" || !DefaultValStr.getAsInteger(0, Tmp)) &&
70
79.4k
           "Invalid value for integer command line option! Maybe incorrect "
71
79.4k
           "parameters to the addCheckerOption or addPackageOption method?");
72
79.4k
    (void)Tmp;
73
74
79.4k
    assert((DevelopmentStatus == "alpha" || DevelopmentStatus == "beta" ||
75
79.4k
            DevelopmentStatus == "released") &&
76
79.4k
           "Invalid development status!");
77
79.4k
  }
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
506k
  bool isEnabled(const CheckerManager &mgr) const {
117
506k
    return State == StateFromCmdLine::State_Enabled && 
ShouldRegister(mgr)48.5k
;
118
506k
  }
119
120
8.95k
  bool isDisabled(const CheckerManager &mgr) const {
121
8.95k
    return State == StateFromCmdLine::State_Disabled || 
!ShouldRegister(mgr)8.95k
;
122
8.95k
  }
123
124
  // Since each checker must have a different full name, we can identify
125
  // CheckerInfo objects by them.
126
6.47M
  bool operator==(const CheckerInfo &Rhs) const {
127
6.47M
    return FullName == Rhs.FullName;
128
6.47M
  }
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
253k
        DocumentationUri(DocsUri), IsHidden(IsHidden) {}
134
135
  // Used for lower_bound.
136
335k
  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
42.8k
  bool operator==(const PackageInfo &Rhs) const {
153
42.8k
    return FullName == Rhs.FullName;
154
42.8k
  }
155
156
69.8k
  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
51.3M
  bool operator()(const T &Lhs, const T &Rhs) {
168
51.3M
    return Lhs.FullName < Rhs.FullName;
169
51.3M
  }
clang::ento::checker_registry::FullNameLT<clang::ento::PackageInfo>::operator()(clang::ento::PackageInfo const&, clang::ento::PackageInfo const&)
Line
Count
Source
167
423k
  bool operator()(const T &Lhs, const T &Rhs) {
168
423k
    return Lhs.FullName < Rhs.FullName;
169
423k
  }
clang::ento::checker_registry::FullNameLT<clang::ento::CheckerInfo>::operator()(clang::ento::CheckerInfo const&, clang::ento::CheckerInfo const&)
Line
Count
Source
167
50.9M
  bool operator()(const T &Lhs, const T &Rhs) {
168
50.9M
    return Lhs.FullName < Rhs.FullName;
169
50.9M
  }
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
257k
binaryFind(CheckerOrPackageInfoList &Collection, StringRef FullName) {
180
181
257k
  using CheckerOrPackage = typename CheckerOrPackageInfoList::value_type;
182
257k
  using CheckerOrPackageFullNameLT = FullNameLT<CheckerOrPackage>;
183
184
257k
  assert(llvm::is_sorted(Collection, CheckerOrPackageFullNameLT{}) &&
185
257k
         "In order to efficiently gather checkers/packages, this function "
186
257k
         "expects them to be already sorted!");
187
188
257k
  return llvm::lower_bound(Collection, CheckerOrPackage(FullName),
189
257k
                           CheckerOrPackageFullNameLT{});
190
257k
}
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.36k
binaryFind(CheckerOrPackageInfoList &Collection, StringRef FullName) {
180
181
1.36k
  using CheckerOrPackage = typename CheckerOrPackageInfoList::value_type;
182
1.36k
  using CheckerOrPackageFullNameLT = FullNameLT<CheckerOrPackage>;
183
184
1.36k
  assert(llvm::is_sorted(Collection, CheckerOrPackageFullNameLT{}) &&
185
1.36k
         "In order to efficiently gather checkers/packages, this function "
186
1.36k
         "expects them to be already sorted!");
187
188
1.36k
  return llvm::lower_bound(Collection, CheckerOrPackage(FullName),
189
1.36k
                           CheckerOrPackageFullNameLT{});
190
1.36k
}
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
256k
binaryFind(CheckerOrPackageInfoList &Collection, StringRef FullName) {
180
181
256k
  using CheckerOrPackage = typename CheckerOrPackageInfoList::value_type;
182
256k
  using CheckerOrPackageFullNameLT = FullNameLT<CheckerOrPackage>;
183
184
256k
  assert(llvm::is_sorted(Collection, CheckerOrPackageFullNameLT{}) &&
185
256k
         "In order to efficiently gather checkers/packages, this function "
186
256k
         "expects them to be already sorted!");
187
188
256k
  return llvm::lower_bound(Collection, CheckerOrPackage(FullName),
189
256k
                           CheckerOrPackageFullNameLT{});
190
256k
}
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