1e5dd7070Spatrick //===- CheckerRegistry.cpp - Maintains all available checkers -------------===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick
9e5dd7070Spatrick #include "clang/StaticAnalyzer/Frontend/CheckerRegistry.h"
10e5dd7070Spatrick #include "clang/Basic/Diagnostic.h"
11e5dd7070Spatrick #include "clang/Basic/LLVM.h"
12e5dd7070Spatrick #include "clang/Driver/DriverDiagnostic.h"
13e5dd7070Spatrick #include "clang/Frontend/FrontendDiagnostic.h"
14e5dd7070Spatrick #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
15e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
16e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/CheckerManager.h"
17e5dd7070Spatrick #include "llvm/ADT/STLExtras.h"
18e5dd7070Spatrick #include "llvm/ADT/StringMap.h"
19e5dd7070Spatrick #include "llvm/ADT/StringRef.h"
20e5dd7070Spatrick #include "llvm/Support/DynamicLibrary.h"
21e5dd7070Spatrick #include "llvm/Support/Path.h"
22e5dd7070Spatrick #include "llvm/Support/raw_ostream.h"
23e5dd7070Spatrick #include <algorithm>
24e5dd7070Spatrick
25e5dd7070Spatrick using namespace clang;
26e5dd7070Spatrick using namespace ento;
27ec727ea7Spatrick using namespace checker_registry;
28e5dd7070Spatrick using llvm::sys::DynamicLibrary;
29e5dd7070Spatrick
30ec727ea7Spatrick //===----------------------------------------------------------------------===//
31ec727ea7Spatrick // Utilities.
32ec727ea7Spatrick //===----------------------------------------------------------------------===//
33e5dd7070Spatrick
isCompatibleAPIVersion(const char * VersionString)34e5dd7070Spatrick static bool isCompatibleAPIVersion(const char *VersionString) {
35e5dd7070Spatrick // If the version string is null, its not an analyzer plugin.
36e5dd7070Spatrick if (!VersionString)
37e5dd7070Spatrick return false;
38e5dd7070Spatrick
39e5dd7070Spatrick // For now, none of the static analyzer API is considered stable.
40e5dd7070Spatrick // Versions must match exactly.
41e5dd7070Spatrick return strcmp(VersionString, CLANG_ANALYZER_API_VERSION_STRING) == 0;
42e5dd7070Spatrick }
43e5dd7070Spatrick
44e5dd7070Spatrick static constexpr char PackageSeparator = '.';
45e5dd7070Spatrick
46ec727ea7Spatrick //===----------------------------------------------------------------------===//
47ec727ea7Spatrick // Methods of CheckerRegistry.
48ec727ea7Spatrick //===----------------------------------------------------------------------===//
49e5dd7070Spatrick
CheckerRegistry(CheckerRegistryData & Data,ArrayRef<std::string> Plugins,DiagnosticsEngine & Diags,AnalyzerOptions & AnOpts,ArrayRef<std::function<void (CheckerRegistry &)>> CheckerRegistrationFns)50e5dd7070Spatrick CheckerRegistry::CheckerRegistry(
51ec727ea7Spatrick CheckerRegistryData &Data, ArrayRef<std::string> Plugins,
52ec727ea7Spatrick DiagnosticsEngine &Diags, AnalyzerOptions &AnOpts,
53e5dd7070Spatrick ArrayRef<std::function<void(CheckerRegistry &)>> CheckerRegistrationFns)
54ec727ea7Spatrick : Data(Data), Diags(Diags), AnOpts(AnOpts) {
55e5dd7070Spatrick
56e5dd7070Spatrick // Register builtin checkers.
57e5dd7070Spatrick #define GET_CHECKERS
58e5dd7070Spatrick #define CHECKER(FULLNAME, CLASS, HELPTEXT, DOC_URI, IS_HIDDEN) \
59e5dd7070Spatrick addChecker(register##CLASS, shouldRegister##CLASS, FULLNAME, HELPTEXT, \
60e5dd7070Spatrick DOC_URI, IS_HIDDEN);
61e5dd7070Spatrick
62e5dd7070Spatrick #define GET_PACKAGES
63e5dd7070Spatrick #define PACKAGE(FULLNAME) addPackage(FULLNAME);
64e5dd7070Spatrick
65e5dd7070Spatrick #include "clang/StaticAnalyzer/Checkers/Checkers.inc"
66e5dd7070Spatrick #undef CHECKER
67e5dd7070Spatrick #undef GET_CHECKERS
68e5dd7070Spatrick #undef PACKAGE
69e5dd7070Spatrick #undef GET_PACKAGES
70e5dd7070Spatrick
71e5dd7070Spatrick // Register checkers from plugins.
72e5dd7070Spatrick for (const std::string &Plugin : Plugins) {
73e5dd7070Spatrick // Get access to the plugin.
74e5dd7070Spatrick std::string ErrorMsg;
75e5dd7070Spatrick DynamicLibrary Lib =
76e5dd7070Spatrick DynamicLibrary::getPermanentLibrary(Plugin.c_str(), &ErrorMsg);
77e5dd7070Spatrick if (!Lib.isValid()) {
78e5dd7070Spatrick Diags.Report(diag::err_fe_unable_to_load_plugin) << Plugin << ErrorMsg;
79e5dd7070Spatrick continue;
80e5dd7070Spatrick }
81e5dd7070Spatrick
82e5dd7070Spatrick // See if its compatible with this build of clang.
83e5dd7070Spatrick const char *PluginAPIVersion = static_cast<const char *>(
84e5dd7070Spatrick Lib.getAddressOfSymbol("clang_analyzerAPIVersionString"));
85e5dd7070Spatrick
86e5dd7070Spatrick if (!isCompatibleAPIVersion(PluginAPIVersion)) {
87e5dd7070Spatrick Diags.Report(diag::warn_incompatible_analyzer_plugin_api)
88e5dd7070Spatrick << llvm::sys::path::filename(Plugin);
89e5dd7070Spatrick Diags.Report(diag::note_incompatible_analyzer_plugin_api)
90e5dd7070Spatrick << CLANG_ANALYZER_API_VERSION_STRING << PluginAPIVersion;
91e5dd7070Spatrick continue;
92e5dd7070Spatrick }
93e5dd7070Spatrick
94ec727ea7Spatrick using RegisterPluginCheckerFn = void (*)(CheckerRegistry &);
95e5dd7070Spatrick // Register its checkers.
96ec727ea7Spatrick RegisterPluginCheckerFn RegisterPluginCheckers =
97ec727ea7Spatrick reinterpret_cast<RegisterPluginCheckerFn>(
98e5dd7070Spatrick Lib.getAddressOfSymbol("clang_registerCheckers"));
99e5dd7070Spatrick if (RegisterPluginCheckers)
100e5dd7070Spatrick RegisterPluginCheckers(*this);
101e5dd7070Spatrick }
102e5dd7070Spatrick
103e5dd7070Spatrick // Register statically linked checkers, that aren't generated from the tblgen
104e5dd7070Spatrick // file, but rather passed their registry function as a parameter in
105e5dd7070Spatrick // checkerRegistrationFns.
106e5dd7070Spatrick
107e5dd7070Spatrick for (const auto &Fn : CheckerRegistrationFns)
108e5dd7070Spatrick Fn(*this);
109e5dd7070Spatrick
110e5dd7070Spatrick // Sort checkers for efficient collection.
111e5dd7070Spatrick // FIXME: Alphabetical sort puts 'experimental' in the middle.
112e5dd7070Spatrick // Would it be better to name it '~experimental' or something else
113e5dd7070Spatrick // that's ASCIIbetically last?
114ec727ea7Spatrick llvm::sort(Data.Packages, checker_registry::PackageNameLT{});
115ec727ea7Spatrick llvm::sort(Data.Checkers, checker_registry::CheckerNameLT{});
116e5dd7070Spatrick
117e5dd7070Spatrick #define GET_CHECKER_DEPENDENCIES
118e5dd7070Spatrick
119e5dd7070Spatrick #define CHECKER_DEPENDENCY(FULLNAME, DEPENDENCY) \
120e5dd7070Spatrick addDependency(FULLNAME, DEPENDENCY);
121e5dd7070Spatrick
122ec727ea7Spatrick #define GET_CHECKER_WEAK_DEPENDENCIES
123ec727ea7Spatrick
124ec727ea7Spatrick #define CHECKER_WEAK_DEPENDENCY(FULLNAME, DEPENDENCY) \
125ec727ea7Spatrick addWeakDependency(FULLNAME, DEPENDENCY);
126ec727ea7Spatrick
127e5dd7070Spatrick #define GET_CHECKER_OPTIONS
128ec727ea7Spatrick #define CHECKER_OPTION(TYPE, FULLNAME, CMDFLAG, DESC, DEFAULT_VAL, \
129ec727ea7Spatrick DEVELOPMENT_STATUS, IS_HIDDEN) \
130ec727ea7Spatrick addCheckerOption(TYPE, FULLNAME, CMDFLAG, DEFAULT_VAL, DESC, \
131ec727ea7Spatrick DEVELOPMENT_STATUS, IS_HIDDEN);
132e5dd7070Spatrick
133e5dd7070Spatrick #define GET_PACKAGE_OPTIONS
134ec727ea7Spatrick #define PACKAGE_OPTION(TYPE, FULLNAME, CMDFLAG, DESC, DEFAULT_VAL, \
135ec727ea7Spatrick DEVELOPMENT_STATUS, IS_HIDDEN) \
136ec727ea7Spatrick addPackageOption(TYPE, FULLNAME, CMDFLAG, DEFAULT_VAL, DESC, \
137ec727ea7Spatrick DEVELOPMENT_STATUS, IS_HIDDEN);
138e5dd7070Spatrick
139e5dd7070Spatrick #include "clang/StaticAnalyzer/Checkers/Checkers.inc"
140e5dd7070Spatrick #undef CHECKER_DEPENDENCY
141e5dd7070Spatrick #undef GET_CHECKER_DEPENDENCIES
142ec727ea7Spatrick #undef CHECKER_WEAK_DEPENDENCY
143ec727ea7Spatrick #undef GET_CHECKER_WEAK_DEPENDENCIES
144e5dd7070Spatrick #undef CHECKER_OPTION
145e5dd7070Spatrick #undef GET_CHECKER_OPTIONS
146e5dd7070Spatrick #undef PACKAGE_OPTION
147e5dd7070Spatrick #undef GET_PACKAGE_OPTIONS
148e5dd7070Spatrick
149ec727ea7Spatrick resolveDependencies<true>();
150ec727ea7Spatrick resolveDependencies<false>();
151ec727ea7Spatrick
152ec727ea7Spatrick #ifndef NDEBUG
153ec727ea7Spatrick for (auto &DepPair : Data.Dependencies) {
154ec727ea7Spatrick for (auto &WeakDepPair : Data.WeakDependencies) {
155ec727ea7Spatrick // Some assertions to enforce that strong dependencies are relations in
156ec727ea7Spatrick // between purely modeling checkers, and weak dependencies are about
157ec727ea7Spatrick // diagnostics.
158ec727ea7Spatrick assert(WeakDepPair != DepPair &&
159ec727ea7Spatrick "A checker cannot strong and weak depend on the same checker!");
160ec727ea7Spatrick assert(WeakDepPair.first != DepPair.second &&
161ec727ea7Spatrick "A strong dependency mustn't have weak dependencies!");
162ec727ea7Spatrick assert(WeakDepPair.second != DepPair.second &&
163ec727ea7Spatrick "A strong dependency mustn't be a weak dependency as well!");
164ec727ea7Spatrick }
165ec727ea7Spatrick }
166ec727ea7Spatrick #endif
167ec727ea7Spatrick
168e5dd7070Spatrick resolveCheckerAndPackageOptions();
169e5dd7070Spatrick
170e5dd7070Spatrick // Parse '-analyzer-checker' and '-analyzer-disable-checker' options from the
171e5dd7070Spatrick // command line.
172e5dd7070Spatrick for (const std::pair<std::string, bool> &Opt : AnOpts.CheckersAndPackages) {
173e5dd7070Spatrick CheckerInfoListRange CheckerForCmdLineArg =
174ec727ea7Spatrick Data.getMutableCheckersForCmdLineArg(Opt.first);
175e5dd7070Spatrick
176e5dd7070Spatrick if (CheckerForCmdLineArg.begin() == CheckerForCmdLineArg.end()) {
177e5dd7070Spatrick Diags.Report(diag::err_unknown_analyzer_checker_or_package) << Opt.first;
178e5dd7070Spatrick Diags.Report(diag::note_suggest_disabling_all_checkers);
179e5dd7070Spatrick }
180e5dd7070Spatrick
181e5dd7070Spatrick for (CheckerInfo &checker : CheckerForCmdLineArg) {
182e5dd7070Spatrick checker.State = Opt.second ? StateFromCmdLine::State_Enabled
183e5dd7070Spatrick : StateFromCmdLine::State_Disabled;
184e5dd7070Spatrick }
185e5dd7070Spatrick }
186ec727ea7Spatrick validateCheckerOptions();
187e5dd7070Spatrick }
188e5dd7070Spatrick
189ec727ea7Spatrick //===----------------------------------------------------------------------===//
190ec727ea7Spatrick // Dependency resolving.
191ec727ea7Spatrick //===----------------------------------------------------------------------===//
192e5dd7070Spatrick
193ec727ea7Spatrick template <typename IsEnabledFn>
194ec727ea7Spatrick static bool collectStrongDependencies(const ConstCheckerInfoList &Deps,
195ec727ea7Spatrick const CheckerManager &Mgr,
196ec727ea7Spatrick CheckerInfoSet &Ret,
197ec727ea7Spatrick IsEnabledFn IsEnabled);
198e5dd7070Spatrick
199ec727ea7Spatrick /// Collects weak dependencies in \p enabledData.Checkers.
200ec727ea7Spatrick template <typename IsEnabledFn>
201ec727ea7Spatrick static void collectWeakDependencies(const ConstCheckerInfoList &Deps,
202ec727ea7Spatrick const CheckerManager &Mgr,
203ec727ea7Spatrick CheckerInfoSet &Ret, IsEnabledFn IsEnabled);
204e5dd7070Spatrick
initializeRegistry(const CheckerManager & Mgr)205ec727ea7Spatrick void CheckerRegistry::initializeRegistry(const CheckerManager &Mgr) {
206ec727ea7Spatrick // First, we calculate the list of enabled checkers as specified by the
207ec727ea7Spatrick // invocation. Weak dependencies will not enable their unspecified strong
208ec727ea7Spatrick // depenencies, but its only after resolving strong dependencies for all
209ec727ea7Spatrick // checkers when we know whether they will be enabled.
210ec727ea7Spatrick CheckerInfoSet Tmp;
211ec727ea7Spatrick auto IsEnabledFromCmdLine = [&](const CheckerInfo *Checker) {
212ec727ea7Spatrick return !Checker->isDisabled(Mgr);
213ec727ea7Spatrick };
214ec727ea7Spatrick for (const CheckerInfo &Checker : Data.Checkers) {
215ec727ea7Spatrick if (!Checker.isEnabled(Mgr))
216e5dd7070Spatrick continue;
217e5dd7070Spatrick
218ec727ea7Spatrick CheckerInfoSet Deps;
219ec727ea7Spatrick if (!collectStrongDependencies(Checker.Dependencies, Mgr, Deps,
220ec727ea7Spatrick IsEnabledFromCmdLine)) {
221ec727ea7Spatrick // If we failed to enable any of the dependencies, don't enable this
222ec727ea7Spatrick // checker.
223ec727ea7Spatrick continue;
224ec727ea7Spatrick }
225e5dd7070Spatrick
226ec727ea7Spatrick Tmp.insert(Deps.begin(), Deps.end());
227ec727ea7Spatrick
228ec727ea7Spatrick // Enable the checker.
229ec727ea7Spatrick Tmp.insert(&Checker);
230ec727ea7Spatrick }
231ec727ea7Spatrick
232ec727ea7Spatrick // Calculate enabled checkers with the correct registration order. As this is
233ec727ea7Spatrick // done recursively, its arguably cheaper, but for sure less error prone to
234ec727ea7Spatrick // recalculate from scratch.
235ec727ea7Spatrick auto IsEnabled = [&](const CheckerInfo *Checker) {
236ec727ea7Spatrick return llvm::is_contained(Tmp, Checker);
237ec727ea7Spatrick };
238ec727ea7Spatrick for (const CheckerInfo &Checker : Data.Checkers) {
239ec727ea7Spatrick if (!Checker.isEnabled(Mgr))
240ec727ea7Spatrick continue;
241ec727ea7Spatrick
242ec727ea7Spatrick CheckerInfoSet Deps;
243ec727ea7Spatrick
244ec727ea7Spatrick collectWeakDependencies(Checker.WeakDependencies, Mgr, Deps, IsEnabled);
245ec727ea7Spatrick
246ec727ea7Spatrick if (!collectStrongDependencies(Checker.Dependencies, Mgr, Deps,
247ec727ea7Spatrick IsEnabledFromCmdLine)) {
248e5dd7070Spatrick // If we failed to enable any of the dependencies, don't enable this
249e5dd7070Spatrick // checker.
250e5dd7070Spatrick continue;
251e5dd7070Spatrick }
252e5dd7070Spatrick
253e5dd7070Spatrick // Note that set_union also preserves the order of insertion.
254ec727ea7Spatrick Data.EnabledCheckers.set_union(Deps);
255ec727ea7Spatrick Data.EnabledCheckers.insert(&Checker);
256ec727ea7Spatrick }
257e5dd7070Spatrick }
258e5dd7070Spatrick
259ec727ea7Spatrick template <typename IsEnabledFn>
collectStrongDependencies(const ConstCheckerInfoList & Deps,const CheckerManager & Mgr,CheckerInfoSet & Ret,IsEnabledFn IsEnabled)260ec727ea7Spatrick static bool collectStrongDependencies(const ConstCheckerInfoList &Deps,
261ec727ea7Spatrick const CheckerManager &Mgr,
262ec727ea7Spatrick CheckerInfoSet &Ret,
263ec727ea7Spatrick IsEnabledFn IsEnabled) {
264ec727ea7Spatrick
265ec727ea7Spatrick for (const CheckerInfo *Dependency : Deps) {
266ec727ea7Spatrick if (!IsEnabled(Dependency))
267ec727ea7Spatrick return false;
268ec727ea7Spatrick
269ec727ea7Spatrick // Collect dependencies recursively.
270ec727ea7Spatrick if (!collectStrongDependencies(Dependency->Dependencies, Mgr, Ret,
271ec727ea7Spatrick IsEnabled))
272ec727ea7Spatrick return false;
273ec727ea7Spatrick Ret.insert(Dependency);
274e5dd7070Spatrick }
275e5dd7070Spatrick
276ec727ea7Spatrick return true;
277ec727ea7Spatrick }
278ec727ea7Spatrick
279ec727ea7Spatrick template <typename IsEnabledFn>
collectWeakDependencies(const ConstCheckerInfoList & WeakDeps,const CheckerManager & Mgr,CheckerInfoSet & Ret,IsEnabledFn IsEnabled)280ec727ea7Spatrick static void collectWeakDependencies(const ConstCheckerInfoList &WeakDeps,
281ec727ea7Spatrick const CheckerManager &Mgr,
282ec727ea7Spatrick CheckerInfoSet &Ret,
283ec727ea7Spatrick IsEnabledFn IsEnabled) {
284ec727ea7Spatrick
285ec727ea7Spatrick for (const CheckerInfo *Dependency : WeakDeps) {
286ec727ea7Spatrick // Don't enable this checker if strong dependencies are unsatisfied, but
287ec727ea7Spatrick // assume that weak dependencies are transitive.
288ec727ea7Spatrick collectWeakDependencies(Dependency->WeakDependencies, Mgr, Ret, IsEnabled);
289ec727ea7Spatrick
290ec727ea7Spatrick if (IsEnabled(Dependency) &&
291ec727ea7Spatrick collectStrongDependencies(Dependency->Dependencies, Mgr, Ret,
292ec727ea7Spatrick IsEnabled))
293ec727ea7Spatrick Ret.insert(Dependency);
294ec727ea7Spatrick }
295ec727ea7Spatrick }
296ec727ea7Spatrick
resolveDependencies()297ec727ea7Spatrick template <bool IsWeak> void CheckerRegistry::resolveDependencies() {
298ec727ea7Spatrick for (const std::pair<StringRef, StringRef> &Entry :
299ec727ea7Spatrick (IsWeak ? Data.WeakDependencies : Data.Dependencies)) {
300ec727ea7Spatrick
301ec727ea7Spatrick auto CheckerIt = binaryFind(Data.Checkers, Entry.first);
302ec727ea7Spatrick assert(CheckerIt != Data.Checkers.end() &&
303ec727ea7Spatrick CheckerIt->FullName == Entry.first &&
304e5dd7070Spatrick "Failed to find the checker while attempting to set up its "
305e5dd7070Spatrick "dependencies!");
306e5dd7070Spatrick
307ec727ea7Spatrick auto DependencyIt = binaryFind(Data.Checkers, Entry.second);
308ec727ea7Spatrick assert(DependencyIt != Data.Checkers.end() &&
309e5dd7070Spatrick DependencyIt->FullName == Entry.second &&
310e5dd7070Spatrick "Failed to find the dependency of a checker!");
311e5dd7070Spatrick
312ec727ea7Spatrick // We do allow diagnostics from unit test/example dependency checkers.
313ec727ea7Spatrick assert((DependencyIt->FullName.startswith("test") ||
314ec727ea7Spatrick DependencyIt->FullName.startswith("example") || IsWeak ||
315ec727ea7Spatrick DependencyIt->IsHidden) &&
316ec727ea7Spatrick "Strong dependencies are modeling checkers, and as such "
317ec727ea7Spatrick "non-user facing! Mark them hidden in Checkers.td!");
318ec727ea7Spatrick
319ec727ea7Spatrick if (IsWeak)
320ec727ea7Spatrick CheckerIt->WeakDependencies.emplace_back(&*DependencyIt);
321ec727ea7Spatrick else
322e5dd7070Spatrick CheckerIt->Dependencies.emplace_back(&*DependencyIt);
323e5dd7070Spatrick }
324e5dd7070Spatrick }
325e5dd7070Spatrick
addDependency(StringRef FullName,StringRef Dependency)326e5dd7070Spatrick void CheckerRegistry::addDependency(StringRef FullName, StringRef Dependency) {
327ec727ea7Spatrick Data.Dependencies.emplace_back(FullName, Dependency);
328e5dd7070Spatrick }
329e5dd7070Spatrick
addWeakDependency(StringRef FullName,StringRef Dependency)330ec727ea7Spatrick void CheckerRegistry::addWeakDependency(StringRef FullName,
331ec727ea7Spatrick StringRef Dependency) {
332ec727ea7Spatrick Data.WeakDependencies.emplace_back(FullName, Dependency);
333ec727ea7Spatrick }
334ec727ea7Spatrick
335ec727ea7Spatrick //===----------------------------------------------------------------------===//
336ec727ea7Spatrick // Checker option resolving and validating.
337ec727ea7Spatrick //===----------------------------------------------------------------------===//
338ec727ea7Spatrick
339e5dd7070Spatrick /// Insert the checker/package option to AnalyzerOptions' config table, and
340e5dd7070Spatrick /// validate it, if the user supplied it on the command line.
insertAndValidate(StringRef FullName,const CmdLineOption & Option,AnalyzerOptions & AnOpts,DiagnosticsEngine & Diags)341ec727ea7Spatrick static void insertAndValidate(StringRef FullName, const CmdLineOption &Option,
342e5dd7070Spatrick AnalyzerOptions &AnOpts,
343e5dd7070Spatrick DiagnosticsEngine &Diags) {
344e5dd7070Spatrick
345e5dd7070Spatrick std::string FullOption = (FullName + ":" + Option.OptionName).str();
346e5dd7070Spatrick
347ec727ea7Spatrick auto It =
348ec727ea7Spatrick AnOpts.Config.insert({FullOption, std::string(Option.DefaultValStr)});
349e5dd7070Spatrick
350e5dd7070Spatrick // Insertation was successful -- CmdLineOption's constructor will validate
351e5dd7070Spatrick // whether values received from plugins or TableGen files are correct.
352e5dd7070Spatrick if (It.second)
353e5dd7070Spatrick return;
354e5dd7070Spatrick
355e5dd7070Spatrick // Insertion failed, the user supplied this package/checker option on the
356e5dd7070Spatrick // command line. If the supplied value is invalid, we'll restore the option
357e5dd7070Spatrick // to it's default value, and if we're in non-compatibility mode, we'll also
358e5dd7070Spatrick // emit an error.
359e5dd7070Spatrick
360e5dd7070Spatrick StringRef SuppliedValue = It.first->getValue();
361e5dd7070Spatrick
362e5dd7070Spatrick if (Option.OptionType == "bool") {
363e5dd7070Spatrick if (SuppliedValue != "true" && SuppliedValue != "false") {
364e5dd7070Spatrick if (AnOpts.ShouldEmitErrorsOnInvalidConfigValue) {
365e5dd7070Spatrick Diags.Report(diag::err_analyzer_checker_option_invalid_input)
366e5dd7070Spatrick << FullOption << "a boolean value";
367e5dd7070Spatrick }
368e5dd7070Spatrick
369ec727ea7Spatrick It.first->setValue(std::string(Option.DefaultValStr));
370e5dd7070Spatrick }
371e5dd7070Spatrick return;
372e5dd7070Spatrick }
373e5dd7070Spatrick
374e5dd7070Spatrick if (Option.OptionType == "int") {
375e5dd7070Spatrick int Tmp;
376e5dd7070Spatrick bool HasFailed = SuppliedValue.getAsInteger(0, Tmp);
377e5dd7070Spatrick if (HasFailed) {
378e5dd7070Spatrick if (AnOpts.ShouldEmitErrorsOnInvalidConfigValue) {
379e5dd7070Spatrick Diags.Report(diag::err_analyzer_checker_option_invalid_input)
380e5dd7070Spatrick << FullOption << "an integer value";
381e5dd7070Spatrick }
382e5dd7070Spatrick
383ec727ea7Spatrick It.first->setValue(std::string(Option.DefaultValStr));
384e5dd7070Spatrick }
385e5dd7070Spatrick return;
386e5dd7070Spatrick }
387e5dd7070Spatrick }
388e5dd7070Spatrick
389e5dd7070Spatrick template <class T>
insertOptionToCollection(StringRef FullName,T & Collection,const CmdLineOption & Option,AnalyzerOptions & AnOpts,DiagnosticsEngine & Diags)390ec727ea7Spatrick static void insertOptionToCollection(StringRef FullName, T &Collection,
391ec727ea7Spatrick const CmdLineOption &Option,
392ec727ea7Spatrick AnalyzerOptions &AnOpts,
393ec727ea7Spatrick DiagnosticsEngine &Diags) {
394e5dd7070Spatrick auto It = binaryFind(Collection, FullName);
395e5dd7070Spatrick assert(It != Collection.end() &&
396e5dd7070Spatrick "Failed to find the checker while attempting to add a command line "
397e5dd7070Spatrick "option to it!");
398e5dd7070Spatrick
399e5dd7070Spatrick insertAndValidate(FullName, Option, AnOpts, Diags);
400e5dd7070Spatrick
401e5dd7070Spatrick It->CmdLineOptions.emplace_back(Option);
402e5dd7070Spatrick }
403e5dd7070Spatrick
resolveCheckerAndPackageOptions()404e5dd7070Spatrick void CheckerRegistry::resolveCheckerAndPackageOptions() {
405e5dd7070Spatrick for (const std::pair<StringRef, CmdLineOption> &CheckerOptEntry :
406ec727ea7Spatrick Data.CheckerOptions) {
407ec727ea7Spatrick insertOptionToCollection(CheckerOptEntry.first, Data.Checkers,
408e5dd7070Spatrick CheckerOptEntry.second, AnOpts, Diags);
409e5dd7070Spatrick }
410e5dd7070Spatrick
411e5dd7070Spatrick for (const std::pair<StringRef, CmdLineOption> &PackageOptEntry :
412ec727ea7Spatrick Data.PackageOptions) {
413ec727ea7Spatrick insertOptionToCollection(PackageOptEntry.first, Data.Packages,
414e5dd7070Spatrick PackageOptEntry.second, AnOpts, Diags);
415e5dd7070Spatrick }
416e5dd7070Spatrick }
417e5dd7070Spatrick
addPackage(StringRef FullName)418e5dd7070Spatrick void CheckerRegistry::addPackage(StringRef FullName) {
419ec727ea7Spatrick Data.Packages.emplace_back(PackageInfo(FullName));
420e5dd7070Spatrick }
421e5dd7070Spatrick
addPackageOption(StringRef OptionType,StringRef PackageFullName,StringRef OptionName,StringRef DefaultValStr,StringRef Description,StringRef DevelopmentStatus,bool IsHidden)422e5dd7070Spatrick void CheckerRegistry::addPackageOption(StringRef OptionType,
423e5dd7070Spatrick StringRef PackageFullName,
424e5dd7070Spatrick StringRef OptionName,
425e5dd7070Spatrick StringRef DefaultValStr,
426e5dd7070Spatrick StringRef Description,
427e5dd7070Spatrick StringRef DevelopmentStatus,
428e5dd7070Spatrick bool IsHidden) {
429ec727ea7Spatrick Data.PackageOptions.emplace_back(
430e5dd7070Spatrick PackageFullName, CmdLineOption{OptionType, OptionName, DefaultValStr,
431e5dd7070Spatrick Description, DevelopmentStatus, IsHidden});
432e5dd7070Spatrick }
433e5dd7070Spatrick
addChecker(RegisterCheckerFn Rfn,ShouldRegisterFunction Sfn,StringRef Name,StringRef Desc,StringRef DocsUri,bool IsHidden)434ec727ea7Spatrick void CheckerRegistry::addChecker(RegisterCheckerFn Rfn,
435e5dd7070Spatrick ShouldRegisterFunction Sfn, StringRef Name,
436e5dd7070Spatrick StringRef Desc, StringRef DocsUri,
437e5dd7070Spatrick bool IsHidden) {
438ec727ea7Spatrick Data.Checkers.emplace_back(Rfn, Sfn, Name, Desc, DocsUri, IsHidden);
439e5dd7070Spatrick
440e5dd7070Spatrick // Record the presence of the checker in its packages.
441e5dd7070Spatrick StringRef PackageName, LeafName;
442e5dd7070Spatrick std::tie(PackageName, LeafName) = Name.rsplit(PackageSeparator);
443e5dd7070Spatrick while (!LeafName.empty()) {
444ec727ea7Spatrick Data.PackageSizes[PackageName] += 1;
445e5dd7070Spatrick std::tie(PackageName, LeafName) = PackageName.rsplit(PackageSeparator);
446e5dd7070Spatrick }
447e5dd7070Spatrick }
448e5dd7070Spatrick
addCheckerOption(StringRef OptionType,StringRef CheckerFullName,StringRef OptionName,StringRef DefaultValStr,StringRef Description,StringRef DevelopmentStatus,bool IsHidden)449e5dd7070Spatrick void CheckerRegistry::addCheckerOption(StringRef OptionType,
450e5dd7070Spatrick StringRef CheckerFullName,
451e5dd7070Spatrick StringRef OptionName,
452e5dd7070Spatrick StringRef DefaultValStr,
453e5dd7070Spatrick StringRef Description,
454e5dd7070Spatrick StringRef DevelopmentStatus,
455e5dd7070Spatrick bool IsHidden) {
456ec727ea7Spatrick Data.CheckerOptions.emplace_back(
457e5dd7070Spatrick CheckerFullName, CmdLineOption{OptionType, OptionName, DefaultValStr,
458e5dd7070Spatrick Description, DevelopmentStatus, IsHidden});
459e5dd7070Spatrick }
460e5dd7070Spatrick
initializeManager(CheckerManager & CheckerMgr) const461e5dd7070Spatrick void CheckerRegistry::initializeManager(CheckerManager &CheckerMgr) const {
462e5dd7070Spatrick // Initialize the CheckerManager with all enabled checkers.
463ec727ea7Spatrick for (const auto *Checker : Data.EnabledCheckers) {
464e5dd7070Spatrick CheckerMgr.setCurrentCheckerName(CheckerNameRef(Checker->FullName));
465e5dd7070Spatrick Checker->Initialize(CheckerMgr);
466e5dd7070Spatrick }
467e5dd7070Spatrick }
468e5dd7070Spatrick
isOptionContainedIn(const CmdLineOptionList & OptionList,StringRef SuppliedChecker,StringRef SuppliedOption,const AnalyzerOptions & AnOpts,DiagnosticsEngine & Diags)469ec727ea7Spatrick static void isOptionContainedIn(const CmdLineOptionList &OptionList,
470ec727ea7Spatrick StringRef SuppliedChecker,
471ec727ea7Spatrick StringRef SuppliedOption,
472ec727ea7Spatrick const AnalyzerOptions &AnOpts,
473ec727ea7Spatrick DiagnosticsEngine &Diags) {
474e5dd7070Spatrick
475e5dd7070Spatrick if (!AnOpts.ShouldEmitErrorsOnInvalidConfigValue)
476e5dd7070Spatrick return;
477e5dd7070Spatrick
478e5dd7070Spatrick auto SameOptName = [SuppliedOption](const CmdLineOption &Opt) {
479e5dd7070Spatrick return Opt.OptionName == SuppliedOption;
480e5dd7070Spatrick };
481e5dd7070Spatrick
482*12c85518Srobert if (llvm::none_of(OptionList, SameOptName)) {
483e5dd7070Spatrick Diags.Report(diag::err_analyzer_checker_option_unknown)
484e5dd7070Spatrick << SuppliedChecker << SuppliedOption;
485e5dd7070Spatrick return;
486e5dd7070Spatrick }
487e5dd7070Spatrick }
488e5dd7070Spatrick
validateCheckerOptions() const489e5dd7070Spatrick void CheckerRegistry::validateCheckerOptions() const {
490e5dd7070Spatrick for (const auto &Config : AnOpts.Config) {
491e5dd7070Spatrick
492e5dd7070Spatrick StringRef SuppliedCheckerOrPackage;
493e5dd7070Spatrick StringRef SuppliedOption;
494e5dd7070Spatrick std::tie(SuppliedCheckerOrPackage, SuppliedOption) =
495e5dd7070Spatrick Config.getKey().split(':');
496e5dd7070Spatrick
497e5dd7070Spatrick if (SuppliedOption.empty())
498e5dd7070Spatrick continue;
499e5dd7070Spatrick
500e5dd7070Spatrick // AnalyzerOptions' config table contains the user input, so an entry could
501e5dd7070Spatrick // look like this:
502e5dd7070Spatrick //
503e5dd7070Spatrick // cor:NoFalsePositives=true
504e5dd7070Spatrick //
505e5dd7070Spatrick // Since lower_bound would look for the first element *not less* than "cor",
506e5dd7070Spatrick // it would return with an iterator to the first checker in the core, so we
507e5dd7070Spatrick // we really have to use find here, which uses operator==.
508e5dd7070Spatrick auto CheckerIt =
509ec727ea7Spatrick llvm::find(Data.Checkers, CheckerInfo(SuppliedCheckerOrPackage));
510ec727ea7Spatrick if (CheckerIt != Data.Checkers.end()) {
511e5dd7070Spatrick isOptionContainedIn(CheckerIt->CmdLineOptions, SuppliedCheckerOrPackage,
512e5dd7070Spatrick SuppliedOption, AnOpts, Diags);
513e5dd7070Spatrick continue;
514e5dd7070Spatrick }
515e5dd7070Spatrick
516ec727ea7Spatrick const auto *PackageIt =
517ec727ea7Spatrick llvm::find(Data.Packages, PackageInfo(SuppliedCheckerOrPackage));
518ec727ea7Spatrick if (PackageIt != Data.Packages.end()) {
519e5dd7070Spatrick isOptionContainedIn(PackageIt->CmdLineOptions, SuppliedCheckerOrPackage,
520e5dd7070Spatrick SuppliedOption, AnOpts, Diags);
521e5dd7070Spatrick continue;
522e5dd7070Spatrick }
523e5dd7070Spatrick
524e5dd7070Spatrick Diags.Report(diag::err_unknown_analyzer_checker_or_package)
525e5dd7070Spatrick << SuppliedCheckerOrPackage;
526e5dd7070Spatrick }
527e5dd7070Spatrick }
528e5dd7070Spatrick
529