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 
CmdLineOptionCmdLineOption52   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         DevelopmentStatus(DevelopmentStatus), IsHidden(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 
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 
isEnabledCheckerInfo116   bool isEnabled(const CheckerManager &mgr) const {
117     return State == StateFromCmdLine::State_Enabled && ShouldRegister(mgr);
118   }
119 
isDisabledCheckerInfo120   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 
CheckerInfoCheckerInfo130   CheckerInfo(RegisterCheckerFn Fn, ShouldRegisterFunction sfn, StringRef Name,
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.
CheckerInfoCheckerInfo136   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   bool operator==(const PackageInfo &Rhs) const {
153     return FullName == Rhs.FullName;
154   }
155 
PackageInfoPackageInfo156   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 {
operatorFullNameLT167   bool operator()(const T &Lhs, const T &Rhs) {
168     return Lhs.FullName < Rhs.FullName;
169   }
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>
binaryFind(CheckerOrPackageInfoList & Collection,StringRef FullName)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 
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
227