clang  14.0.0git
CheckerRegistryData.h
Go to the documentation of this file.
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 &);
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)
58 
59  assert((OptionType == "bool" || OptionType == "string" ||
60  OptionType == "int") &&
61  "Unknown command line option type!");
62 
63  assert((OptionType != "bool" ||
64  (DefaultValStr == "true" || DefaultValStr == "false")) &&
65  "Invalid value for boolean command line option! Maybe incorrect "
66  "parameters to the addCheckerOption or addPackageOption method?");
67 
68  int Tmp;
69  assert((OptionType != "int" || !DefaultValStr.getAsInteger(0, Tmp)) &&
70  "Invalid value for integer command line option! Maybe incorrect "
71  "parameters to the addCheckerOption or addPackageOption method?");
72  (void)Tmp;
73 
74  assert((DevelopmentStatus == "alpha" || DevelopmentStatus == "beta" ||
75  DevelopmentStatus == "released") &&
76  "Invalid development status!");
77  }
78 
79  LLVM_DUMP_METHOD void dump() const;
80  LLVM_DUMP_METHOD void dumpToStream(llvm::raw_ostream &Out) const;
81 };
82 
84 
85 struct CheckerInfo;
86 
87 using CheckerInfoList = std::vector<CheckerInfo>;
88 using CheckerInfoListRange = llvm::iterator_range<CheckerInfoList::iterator>;
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.
98  // This checker was explicitly disabled.
100  // This checker was explicitly enabled.
102  };
103 
106  StringRef FullName;
107  StringRef Desc;
108  StringRef DocumentationUri;
110  bool IsHidden = false;
112 
115 
116  bool isEnabled(const CheckerManager &mgr) const {
118  }
119 
120  bool isDisabled(const CheckerManager &mgr) const {
122  }
123 
124  // Since each checker must have a different full name, we can identify
125  // CheckerInfo objects by them.
126  bool operator==(const CheckerInfo &Rhs) const {
127  return FullName == Rhs.FullName;
128  }
129 
131  StringRef Desc, StringRef DocsUri, bool IsHidden)
132  : Initialize(Fn), ShouldRegister(sfn), FullName(Name), Desc(Desc),
133  DocumentationUri(DocsUri), IsHidden(IsHidden) {}
134 
135  // Used for lower_bound.
136  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 
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;
149 
150  // Since each package must have a different full name, we can identify
151  // CheckerInfo objects by them.
152  bool operator==(const PackageInfo &Rhs) const {
153  return FullName == Rhs.FullName;
154  }
155 
156  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 
163 
164 namespace checker_registry {
165 
166 template <class T> struct FullNameLT {
167  bool operator()(const T &Lhs, const T &Rhs) {
168  return Lhs.FullName < Rhs.FullName;
169  }
170 };
171 
174 
175 template <class CheckerOrPackageInfoList>
176 std::conditional_t<std::is_const<CheckerOrPackageInfoList>::value,
177  typename CheckerOrPackageInfoList::const_iterator,
178  typename CheckerOrPackageInfoList::iterator>
179 binaryFind(CheckerOrPackageInfoList &Collection, StringRef FullName) {
180 
181  using CheckerOrPackage = typename CheckerOrPackageInfoList::value_type;
182  using CheckerOrPackageFullNameLT = FullNameLT<CheckerOrPackage>;
183 
184  assert(llvm::is_sorted(Collection, CheckerOrPackageFullNameLT{}) &&
185  "In order to efficiently gather checkers/packages, this function "
186  "expects them to be already sorted!");
187 
188  return llvm::lower_bound(Collection, CheckerOrPackage(FullName),
189  CheckerOrPackageFullNameLT{});
190 }
191 } // namespace checker_registry
192 
194 public:
196 
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.
208 
211 
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
clang::ento::CheckerRegistryData::getMutableCheckersForCmdLineArg
CheckerInfoListRange getMutableCheckersForCmdLineArg(StringRef CmdLineArg)
Definition: CheckerRegistryData.cpp:100
clang::ento::PackageInfo::dump
LLVM_DUMP_METHOD void dump() const
Definition: CheckerRegistryData.cpp:69
clang::ento::CheckerInfo::StateFromCmdLine::State_Unspecified
@ State_Unspecified
clang::ento::CheckerRegistryData::PackageSizes
llvm::StringMap< size_t > PackageSizes
Used for counting how many checkers belong to a certain package in the Checkers field.
Definition: CheckerRegistryData.h:201
clang::ento::CheckerRegistryData::Packages
PackageInfoList Packages
Definition: CheckerRegistryData.h:198
clang::ento::CheckerRegistryData
Definition: CheckerRegistryData.h:193
llvm::SmallVector< CmdLineOption, 0 >
clang::ento::CheckerInfo::operator==
bool operator==(const CheckerInfo &Rhs) const
Definition: CheckerRegistryData.h:126
clang::ento::checker_registry::FullNameLT
Definition: CheckerRegistryData.h:166
clang::ento::CheckerRegistryData::EnabledCheckers
CheckerInfoSet EnabledCheckers
Definition: CheckerRegistryData.h:195
clang::ento::PackageInfo::FullName
StringRef FullName
Definition: CheckerRegistryData.h:147
clang::ento::CheckerRegistryData::printCheckerOptionList
void printCheckerOptionList(const AnalyzerOptions &AnOpts, raw_ostream &Out) const
Definition: CheckerRegistryData.cpp:178
clang::ento::CheckerInfoList
std::vector< CheckerInfo > CheckerInfoList
Definition: CheckerRegistryData.h:87
clang::ento::CheckerRegistryData::printCheckerWithDescList
void printCheckerWithDescList(const AnalyzerOptions &AnOpts, raw_ostream &Out, size_t MaxNameChars=30) const
Prints the name and description of all checkers in this registry.
Definition: CheckerRegistryData.cpp:122
clang::ento::CmdLineOption::DefaultValStr
StringRef DefaultValStr
Definition: CheckerRegistryData.h:47
clang::ento::CmdLineOption::DevelopmentStatus
StringRef DevelopmentStatus
Definition: CheckerRegistryData.h:49
clang::ento::CheckerRegistryData::CheckerOptions
llvm::SmallVector< std::pair< StringRef, CmdLineOption >, 0 > CheckerOptions
Definition: CheckerRegistryData.h:207
clang::ento::CheckerInfo::CheckerInfo
CheckerInfo(StringRef FullName)
Definition: CheckerRegistryData.h:136
clang::ento::CheckerInfoListRange
llvm::iterator_range< CheckerInfoList::iterator > CheckerInfoListRange
Definition: CheckerRegistryData.h:88
clang::ento::CheckerRegistryData::WeakDependencies
llvm::SmallVector< std::pair< StringRef, StringRef >, 0 > WeakDependencies
Definition: CheckerRegistryData.h:210
clang::ento::CheckerRegistryData::Checkers
CheckerInfoList Checkers
Definition: CheckerRegistryData.h:197
clang::ento::CheckerInfo::DocumentationUri
StringRef DocumentationUri
Definition: CheckerRegistryData.h:108
clang::ento::CheckerInfoSet
llvm::SetVector< const CheckerInfo * > CheckerInfoSet
Definition: CheckerRegistryData.h:90
clang::ento::PackageInfo::dumpToStream
LLVM_DUMP_METHOD void dumpToStream(llvm::raw_ostream &Out) const
Definition: CheckerRegistryData.cpp:71
clang::ento::CheckerRegistryData::printEnabledCheckerList
void printEnabledCheckerList(raw_ostream &Out) const
Definition: CheckerRegistryData.cpp:173
clang::ento::RegisterCheckerFn
void(*)(CheckerManager &) RegisterCheckerFn
Initialization functions perform any necessary setup for a checker.
Definition: CheckerRegistryData.h:39
clang::ento::CheckerInfo::isEnabled
bool isEnabled(const CheckerManager &mgr) const
Definition: CheckerRegistryData.h:116
bool
#define bool
Definition: stdbool.h:15
clang::ento::CmdLineOption
Specifies a command line option.
Definition: CheckerRegistryData.h:44
clang::ento::CheckerInfo::dump
LLVM_DUMP_METHOD void dump() const
Definition: CheckerRegistryData.cpp:46
clang::ento::CmdLineOption::OptionName
StringRef OptionName
Definition: CheckerRegistryData.h:46
clang::ento::CheckerInfo::isDisabled
bool isDisabled(const CheckerManager &mgr) const
Definition: CheckerRegistryData.h:120
clang::ento::CmdLineOption::dumpToStream
LLVM_DUMP_METHOD void dumpToStream(llvm::raw_ostream &Out) const
Definition: CheckerRegistryData.cpp:26
clang::ento::CheckerInfo::StateFromCmdLine::State_Disabled
@ State_Disabled
clang::ento::CheckerRegistryData::Dependencies
llvm::SmallVector< std::pair< StringRef, StringRef >, 0 > Dependencies
Definition: CheckerRegistryData.h:209
clang::ento::CheckerInfo::dumpToStream
LLVM_DUMP_METHOD void dumpToStream(llvm::raw_ostream &Out) const
Definition: CheckerRegistryData.cpp:48
clang::ento::checker_registry::FullNameLT::operator()
bool operator()(const T &Lhs, const T &Rhs)
Definition: CheckerRegistryData.h:167
clang::ento::CheckerInfo::WeakDependencies
ConstCheckerInfoList WeakDependencies
Definition: CheckerRegistryData.h:114
clang::ento::CheckerInfo::CmdLineOptions
CmdLineOptionList CmdLineOptions
Definition: CheckerRegistryData.h:109
clang::ento::CmdLineOption::CmdLineOption
CmdLineOption(StringRef OptionType, StringRef OptionName, StringRef DefaultValStr, StringRef Description, StringRef DevelopmentStatus, bool IsHidden)
Definition: CheckerRegistryData.h:52
clang::ento::PackageInfo::CmdLineOptions
CmdLineOptionList CmdLineOptions
Definition: CheckerRegistryData.h:148
clang::ento::CheckerInfo::Desc
StringRef Desc
Definition: CheckerRegistryData.h:107
clang::ento::CmdLineOption::Description
StringRef Description
Definition: CheckerRegistryData.h:48
clang::ento::CheckerInfo::ShouldRegister
ShouldRegisterFunction ShouldRegister
Definition: CheckerRegistryData.h:105
clang::ento::PackageInfo::operator==
bool operator==(const PackageInfo &Rhs) const
Definition: CheckerRegistryData.h:152
LLVM.h
clang::ento::ShouldRegisterFunction
bool(*)(const CheckerManager &) ShouldRegisterFunction
Definition: CheckerRegistryData.h:40
clang::ento::CheckerInfo::Dependencies
ConstCheckerInfoList Dependencies
Definition: CheckerRegistryData.h:113
clang::ento::CheckerInfo::StateFromCmdLine
StateFromCmdLine
Definition: CheckerRegistryData.h:95
clang::AnalyzerOptions
Stores options for the analyzer from the command line.
Definition: AnalyzerOptions.h:163
clang::ento::CheckerInfo::FullName
StringRef FullName
Definition: CheckerRegistryData.h:106
clang::ento::CheckerInfo::CheckerInfo
CheckerInfo(RegisterCheckerFn Fn, ShouldRegisterFunction sfn, StringRef Name, StringRef Desc, StringRef DocsUri, bool IsHidden)
Definition: CheckerRegistryData.h:130
clang::ento::CheckerManager
Definition: CheckerManager.h:127
clang
Definition: CalledOnceCheck.h:17
clang::ento::CheckerInfo::State
StateFromCmdLine State
Definition: CheckerRegistryData.h:111
clang::ento::CmdLineOption::IsHidden
bool IsHidden
Definition: CheckerRegistryData.h:50
clang::ento::PackageInfo::PackageInfo
PackageInfo(StringRef FullName)
Definition: CheckerRegistryData.h:156
clang::ento::checker_registry::binaryFind
std::conditional_t< std::is_const< CheckerOrPackageInfoList >::value, typename CheckerOrPackageInfoList::const_iterator, typename CheckerOrPackageInfoList::iterator > binaryFind(CheckerOrPackageInfoList &Collection, StringRef FullName)
Definition: CheckerRegistryData.h:179
clang::ento::CheckerInfo::IsHidden
bool IsHidden
Definition: CheckerRegistryData.h:110
clang::ento::CheckerInfo
Specifies a checker.
Definition: CheckerRegistryData.h:94
clang::ento::PackageInfo
Specifies a package.
Definition: CheckerRegistryData.h:146
clang::ento::CheckerInfo::Initialize
RegisterCheckerFn Initialize
Definition: CheckerRegistryData.h:104
clang::ento::CheckerRegistryData::PackageOptions
llvm::SmallVector< std::pair< StringRef, CmdLineOption >, 0 > PackageOptions
Contains all (FullName, CmdLineOption) pairs.
Definition: CheckerRegistryData.h:206
clang::ento::CmdLineOption::OptionType
StringRef OptionType
Definition: CheckerRegistryData.h:45
clang::ento::CmdLineOption::dump
LLVM_DUMP_METHOD void dump() const
Definition: CheckerRegistryData.cpp:21
clang::ento::CheckerInfo::StateFromCmdLine::State_Enabled
@ State_Enabled