clang 20.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
29namespace clang {
30
31class AnalyzerOptions;
32
33namespace ento {
34
35class CheckerManager;
36
37/// Initialization functions perform any necessary setup for a checker.
38/// They should include a call to CheckerManager::registerChecker.
41
42/// Specifies a command line option. It may either belong to a checker or a
43/// package.
45 StringRef OptionType;
46 StringRef OptionName;
47 StringRef DefaultValStr;
48 StringRef Description;
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
85struct CheckerInfo;
86
87using CheckerInfoList = std::vector<CheckerInfo>;
88using CheckerInfoListRange = llvm::iterator_range<CheckerInfoList::iterator>;
90using 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.
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;
110 bool IsHidden = false;
111 StateFromCmdLine State = StateFromCmdLine::State_Unspecified;
112
115
116 bool isEnabled(const CheckerManager &mgr) const {
117 return State == StateFromCmdLine::State_Enabled && ShouldRegister(mgr);
118 }
119
120 bool isDisabled(const CheckerManager &mgr) const {
121 return State == StateFromCmdLine::State_Disabled || !ShouldRegister(mgr);
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),
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.
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
164namespace checker_registry {
165
166template <class T> struct FullNameLT {
167 bool operator()(const T &Lhs, const T &Rhs) {
168 return Lhs.FullName < Rhs.FullName;
169 }
170};
171
174
175template <class CheckerOrPackageInfoList>
176std::conditional_t<std::is_const<CheckerOrPackageInfoList>::value,
177 typename CheckerOrPackageInfoList::const_iterator,
178 typename CheckerOrPackageInfoList::iterator>
179binaryFind(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
194public:
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
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
int32_t FullName
Definition: SemaARM.cpp:1144
Stores options for the analyzer from the command line.
std::conditional_t< std::is_const< CheckerOrPackageInfoList >::value, typename CheckerOrPackageInfoList::const_iterator, typename CheckerOrPackageInfoList::iterator > binaryFind(CheckerOrPackageInfoList &Collection, StringRef FullName)
llvm::iterator_range< CheckerInfoList::iterator > CheckerInfoListRange
bool(*)(const CheckerManager &) ShouldRegisterFunction
std::vector< CheckerInfo > CheckerInfoList
void(*)(CheckerManager &) RegisterCheckerFn
Initialization functions perform any necessary setup for a checker.
llvm::SetVector< const CheckerInfo * > CheckerInfoSet
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T
#define bool
Definition: stdbool.h:24
Specifies a checker.
ConstCheckerInfoList WeakDependencies
ConstCheckerInfoList Dependencies
bool isEnabled(const CheckerManager &mgr) const
bool operator==(const CheckerInfo &Rhs) const
ShouldRegisterFunction ShouldRegister
CmdLineOptionList CmdLineOptions
CheckerInfo(StringRef FullName)
LLVM_DUMP_METHOD void dumpToStream(llvm::raw_ostream &Out) const
bool isDisabled(const CheckerManager &mgr) const
LLVM_DUMP_METHOD void dump() const
CheckerInfo(RegisterCheckerFn Fn, ShouldRegisterFunction sfn, StringRef Name, StringRef Desc, StringRef DocsUri, bool IsHidden)
llvm::SmallVector< std::pair< StringRef, CmdLineOption >, 0 > PackageOptions
Contains all (FullName, CmdLineOption) pairs.
CheckerInfoListRange getMutableCheckersForCmdLineArg(StringRef CmdLineArg)
void printCheckerOptionList(const AnalyzerOptions &AnOpts, raw_ostream &Out) const
llvm::SmallVector< std::pair< StringRef, StringRef >, 0 > Dependencies
llvm::SmallVector< std::pair< StringRef, StringRef >, 0 > WeakDependencies
void printCheckerWithDescList(const AnalyzerOptions &AnOpts, raw_ostream &Out, size_t MaxNameChars=30) const
Prints the name and description of all checkers in this registry.
void printEnabledCheckerList(raw_ostream &Out) const
llvm::SmallVector< std::pair< StringRef, CmdLineOption >, 0 > CheckerOptions
llvm::StringMap< size_t > PackageSizes
Used for counting how many checkers belong to a certain package in the Checkers field.
Specifies a command line option.
CmdLineOption(StringRef OptionType, StringRef OptionName, StringRef DefaultValStr, StringRef Description, StringRef DevelopmentStatus, bool IsHidden)
LLVM_DUMP_METHOD void dump() const
LLVM_DUMP_METHOD void dumpToStream(llvm::raw_ostream &Out) const
CmdLineOptionList CmdLineOptions
PackageInfo(StringRef FullName)
LLVM_DUMP_METHOD void dumpToStream(llvm::raw_ostream &Out) const
bool operator==(const PackageInfo &Rhs) const
LLVM_DUMP_METHOD void dump() const
bool operator()(const T &Lhs, const T &Rhs)