176a21502SKristof Umann //===- CheckerRegistry.cpp - Maintains all available checkers -------------===//
276a21502SKristof Umann //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
676a21502SKristof Umann //
776a21502SKristof Umann //===----------------------------------------------------------------------===//
876a21502SKristof Umann
976a21502SKristof Umann #include "clang/StaticAnalyzer/Frontend/CheckerRegistry.h"
1076a21502SKristof Umann #include "clang/Basic/Diagnostic.h"
1176a21502SKristof Umann #include "clang/Basic/LLVM.h"
1285cf76e7SKristof Umann #include "clang/Driver/DriverDiagnostic.h"
13b0be2ab4SKristof Umann #include "clang/Frontend/FrontendDiagnostic.h"
14b0be2ab4SKristof Umann #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
1576a21502SKristof Umann #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
16a57d4ea3SKristof Umann #include "clang/StaticAnalyzer/Core/CheckerManager.h"
1776a21502SKristof Umann #include "llvm/ADT/STLExtras.h"
1876a21502SKristof Umann #include "llvm/ADT/StringMap.h"
1976a21502SKristof Umann #include "llvm/ADT/StringRef.h"
20b0be2ab4SKristof Umann #include "llvm/Support/DynamicLibrary.h"
21b0be2ab4SKristof Umann #include "llvm/Support/Path.h"
2276a21502SKristof Umann #include "llvm/Support/raw_ostream.h"
2376a21502SKristof Umann #include <algorithm>
2476a21502SKristof Umann
2576a21502SKristof Umann using namespace clang;
2676a21502SKristof Umann using namespace ento;
27b6cbe6cbSKirstóf Umann using namespace checker_registry;
28b0be2ab4SKristof Umann using llvm::sys::DynamicLibrary;
29b0be2ab4SKristof Umann
3077e1181dSKirstóf Umann //===----------------------------------------------------------------------===//
3177e1181dSKirstóf Umann // Utilities.
3277e1181dSKirstóf Umann //===----------------------------------------------------------------------===//
3377e1181dSKirstóf Umann
isCompatibleAPIVersion(const char * VersionString)34b9bc7ec3SKristof Umann static bool isCompatibleAPIVersion(const char *VersionString) {
35b9bc7ec3SKristof Umann // If the version string is null, its not an analyzer plugin.
36b9bc7ec3SKristof Umann if (!VersionString)
37b0be2ab4SKristof Umann return false;
38b0be2ab4SKristof Umann
39b0be2ab4SKristof Umann // For now, none of the static analyzer API is considered stable.
40b0be2ab4SKristof Umann // Versions must match exactly.
41b9bc7ec3SKristof Umann return strcmp(VersionString, CLANG_ANALYZER_API_VERSION_STRING) == 0;
42b0be2ab4SKristof Umann }
43b0be2ab4SKristof Umann
443daa2455SKristof Umann static constexpr char PackageSeparator = '.';
453daa2455SKristof Umann
4677e1181dSKirstóf Umann //===----------------------------------------------------------------------===//
4777e1181dSKirstóf Umann // Methods of CheckerRegistry.
4877e1181dSKirstóf Umann //===----------------------------------------------------------------------===//
4977e1181dSKirstóf Umann
CheckerRegistry(CheckerRegistryData & Data,ArrayRef<std::string> Plugins,DiagnosticsEngine & Diags,AnalyzerOptions & AnOpts,ArrayRef<std::function<void (CheckerRegistry &)>> CheckerRegistrationFns)5098217adbSKristof Umann CheckerRegistry::CheckerRegistry(
51b6cbe6cbSKirstóf Umann CheckerRegistryData &Data, ArrayRef<std::string> Plugins,
52b6cbe6cbSKirstóf Umann DiagnosticsEngine &Diags, AnalyzerOptions &AnOpts,
53a57d4ea3SKristof Umann ArrayRef<std::function<void(CheckerRegistry &)>> CheckerRegistrationFns)
54b6cbe6cbSKirstóf Umann : Data(Data), Diags(Diags), AnOpts(AnOpts) {
55058a7a45SKristof Umann
563daa2455SKristof Umann // Register builtin checkers.
57b0be2ab4SKristof Umann #define GET_CHECKERS
589f7fc983SKristof Umann #define CHECKER(FULLNAME, CLASS, HELPTEXT, DOC_URI, IS_HIDDEN) \
59058a7a45SKristof Umann addChecker(register##CLASS, shouldRegister##CLASS, FULLNAME, HELPTEXT, \
609f7fc983SKristof Umann DOC_URI, IS_HIDDEN);
61b9bc7ec3SKristof Umann
62b4788b26SKristof Umann #define GET_PACKAGES
63b4788b26SKristof Umann #define PACKAGE(FULLNAME) addPackage(FULLNAME);
64b4788b26SKristof Umann
65b0be2ab4SKristof Umann #include "clang/StaticAnalyzer/Checkers/Checkers.inc"
66b0be2ab4SKristof Umann #undef CHECKER
67b0be2ab4SKristof Umann #undef GET_CHECKERS
68b9bc7ec3SKristof Umann #undef PACKAGE
69b9bc7ec3SKristof Umann #undef GET_PACKAGES
70b0be2ab4SKristof Umann
713daa2455SKristof Umann // Register checkers from plugins.
72b9bc7ec3SKristof Umann for (const std::string &Plugin : Plugins) {
73b0be2ab4SKristof Umann // Get access to the plugin.
74b9bc7ec3SKristof Umann std::string ErrorMsg;
75b9bc7ec3SKristof Umann DynamicLibrary Lib =
76b9bc7ec3SKristof Umann DynamicLibrary::getPermanentLibrary(Plugin.c_str(), &ErrorMsg);
77b9bc7ec3SKristof Umann if (!Lib.isValid()) {
78b9bc7ec3SKristof Umann Diags.Report(diag::err_fe_unable_to_load_plugin) << Plugin << ErrorMsg;
79b0be2ab4SKristof Umann continue;
80b0be2ab4SKristof Umann }
81b0be2ab4SKristof Umann
82b9bc7ec3SKristof Umann // See if its compatible with this build of clang.
83b9bc7ec3SKristof Umann const char *PluginAPIVersion = static_cast<const char *>(
84b9bc7ec3SKristof Umann Lib.getAddressOfSymbol("clang_analyzerAPIVersionString"));
85b9bc7ec3SKristof Umann
86b9bc7ec3SKristof Umann if (!isCompatibleAPIVersion(PluginAPIVersion)) {
87b0be2ab4SKristof Umann Diags.Report(diag::warn_incompatible_analyzer_plugin_api)
88b9bc7ec3SKristof Umann << llvm::sys::path::filename(Plugin);
89b0be2ab4SKristof Umann Diags.Report(diag::note_incompatible_analyzer_plugin_api)
90a57d4ea3SKristof Umann << CLANG_ANALYZER_API_VERSION_STRING << PluginAPIVersion;
91b0be2ab4SKristof Umann continue;
92b0be2ab4SKristof Umann }
93b0be2ab4SKristof Umann
94b6cbe6cbSKirstóf Umann using RegisterPluginCheckerFn = void (*)(CheckerRegistry &);
95b0be2ab4SKristof Umann // Register its checkers.
96b6cbe6cbSKirstóf Umann RegisterPluginCheckerFn RegisterPluginCheckers =
97b6cbe6cbSKirstóf Umann reinterpret_cast<RegisterPluginCheckerFn>(
98a57d4ea3SKristof Umann Lib.getAddressOfSymbol("clang_registerCheckers"));
99b9bc7ec3SKristof Umann if (RegisterPluginCheckers)
100b9bc7ec3SKristof Umann RegisterPluginCheckers(*this);
101b0be2ab4SKristof Umann }
1023daa2455SKristof Umann
10398217adbSKristof Umann // Register statically linked checkers, that aren't generated from the tblgen
10498217adbSKristof Umann // file, but rather passed their registry function as a parameter in
10598217adbSKristof Umann // checkerRegistrationFns.
10698217adbSKristof Umann
107b9bc7ec3SKristof Umann for (const auto &Fn : CheckerRegistrationFns)
10898217adbSKristof Umann Fn(*this);
10998217adbSKristof Umann
1103daa2455SKristof Umann // Sort checkers for efficient collection.
1113daa2455SKristof Umann // FIXME: Alphabetical sort puts 'experimental' in the middle.
1123daa2455SKristof Umann // Would it be better to name it '~experimental' or something else
1133daa2455SKristof Umann // that's ASCIIbetically last?
114b6cbe6cbSKirstóf Umann llvm::sort(Data.Packages, checker_registry::PackageNameLT{});
115b6cbe6cbSKirstóf Umann llvm::sort(Data.Checkers, checker_registry::CheckerNameLT{});
1163daa2455SKristof Umann
1178fd74ebfSKristof Umann #define GET_CHECKER_DEPENDENCIES
1188fd74ebfSKristof Umann
1198fd74ebfSKristof Umann #define CHECKER_DEPENDENCY(FULLNAME, DEPENDENCY) \
1208fd74ebfSKristof Umann addDependency(FULLNAME, DEPENDENCY);
1218fd74ebfSKristof Umann
122e22f1c02SKirstóf Umann #define GET_CHECKER_WEAK_DEPENDENCIES
123e22f1c02SKirstóf Umann
124e22f1c02SKirstóf Umann #define CHECKER_WEAK_DEPENDENCY(FULLNAME, DEPENDENCY) \
125e22f1c02SKirstóf Umann addWeakDependency(FULLNAME, DEPENDENCY);
126e22f1c02SKirstóf Umann
127b4788b26SKristof Umann #define GET_CHECKER_OPTIONS
1282aac0c47SKristóf Umann #define CHECKER_OPTION(TYPE, FULLNAME, CMDFLAG, DESC, DEFAULT_VAL, \
1292aac0c47SKristóf Umann DEVELOPMENT_STATUS, IS_HIDDEN) \
1302aac0c47SKristóf Umann addCheckerOption(TYPE, FULLNAME, CMDFLAG, DEFAULT_VAL, DESC, \
1312aac0c47SKristóf Umann DEVELOPMENT_STATUS, IS_HIDDEN);
132b4788b26SKristof Umann
133b4788b26SKristof Umann #define GET_PACKAGE_OPTIONS
1342aac0c47SKristóf Umann #define PACKAGE_OPTION(TYPE, FULLNAME, CMDFLAG, DESC, DEFAULT_VAL, \
1352aac0c47SKristóf Umann DEVELOPMENT_STATUS, IS_HIDDEN) \
1362aac0c47SKristóf Umann addPackageOption(TYPE, FULLNAME, CMDFLAG, DEFAULT_VAL, DESC, \
1372aac0c47SKristóf Umann DEVELOPMENT_STATUS, IS_HIDDEN);
138b4788b26SKristof Umann
1398fd74ebfSKristof Umann #include "clang/StaticAnalyzer/Checkers/Checkers.inc"
1408fd74ebfSKristof Umann #undef CHECKER_DEPENDENCY
1418fd74ebfSKristof Umann #undef GET_CHECKER_DEPENDENCIES
142e22f1c02SKirstóf Umann #undef CHECKER_WEAK_DEPENDENCY
143e22f1c02SKirstóf Umann #undef GET_CHECKER_WEAK_DEPENDENCIES
144b4788b26SKristof Umann #undef CHECKER_OPTION
145b4788b26SKristof Umann #undef GET_CHECKER_OPTIONS
146b4788b26SKristof Umann #undef PACKAGE_OPTION
147b4788b26SKristof Umann #undef GET_PACKAGE_OPTIONS
1488fd74ebfSKristof Umann
149e22f1c02SKirstóf Umann resolveDependencies<true>();
150e22f1c02SKirstóf Umann resolveDependencies<false>();
151e22f1c02SKirstóf Umann
152e00dcf61SHaojian Wu #ifndef NDEBUG
153b6cbe6cbSKirstóf Umann for (auto &DepPair : Data.Dependencies) {
154b6cbe6cbSKirstóf Umann for (auto &WeakDepPair : Data.WeakDependencies) {
155e22f1c02SKirstóf Umann // Some assertions to enforce that strong dependencies are relations in
156e22f1c02SKirstóf Umann // between purely modeling checkers, and weak dependencies are about
157e22f1c02SKirstóf Umann // diagnostics.
158e22f1c02SKirstóf Umann assert(WeakDepPair != DepPair &&
159e22f1c02SKirstóf Umann "A checker cannot strong and weak depend on the same checker!");
160e22f1c02SKirstóf Umann assert(WeakDepPair.first != DepPair.second &&
161e22f1c02SKirstóf Umann "A strong dependency mustn't have weak dependencies!");
162e22f1c02SKirstóf Umann assert(WeakDepPair.second != DepPair.second &&
163e22f1c02SKirstóf Umann "A strong dependency mustn't be a weak dependency as well!");
164e22f1c02SKirstóf Umann }
165e22f1c02SKirstóf Umann }
166e4b3fc18SHaojian Wu #endif
167e22f1c02SKirstóf Umann
168b4788b26SKristof Umann resolveCheckerAndPackageOptions();
169cd3f1474SKristof Umann
1703daa2455SKristof Umann // Parse '-analyzer-checker' and '-analyzer-disable-checker' options from the
1713daa2455SKristof Umann // command line.
172a079a427SCsaba Dabis for (const std::pair<std::string, bool> &Opt : AnOpts.CheckersAndPackages) {
173b9bc7ec3SKristof Umann CheckerInfoListRange CheckerForCmdLineArg =
174b6cbe6cbSKirstóf Umann Data.getMutableCheckersForCmdLineArg(Opt.first);
1753daa2455SKristof Umann
176b9bc7ec3SKristof Umann if (CheckerForCmdLineArg.begin() == CheckerForCmdLineArg.end()) {
177a079a427SCsaba Dabis Diags.Report(diag::err_unknown_analyzer_checker_or_package) << Opt.first;
1783daa2455SKristof Umann Diags.Report(diag::note_suggest_disabling_all_checkers);
179b0be2ab4SKristof Umann }
18076a21502SKristof Umann
181b9bc7ec3SKristof Umann for (CheckerInfo &checker : CheckerForCmdLineArg) {
182a57d4ea3SKristof Umann checker.State = Opt.second ? StateFromCmdLine::State_Enabled
183a57d4ea3SKristof Umann : StateFromCmdLine::State_Disabled;
18476a21502SKristof Umann }
1853daa2455SKristof Umann }
1862aac0c47SKristóf Umann validateCheckerOptions();
18776a21502SKristof Umann }
18876a21502SKristof Umann
189e22f1c02SKirstóf Umann //===----------------------------------------------------------------------===//
190e22f1c02SKirstóf Umann // Dependency resolving.
191e22f1c02SKirstóf Umann //===----------------------------------------------------------------------===//
192e22f1c02SKirstóf Umann
193e22f1c02SKirstóf Umann template <typename IsEnabledFn>
194b6cbe6cbSKirstóf Umann static bool collectStrongDependencies(const ConstCheckerInfoList &Deps,
195e22f1c02SKirstóf Umann const CheckerManager &Mgr,
196b6cbe6cbSKirstóf Umann CheckerInfoSet &Ret,
197e22f1c02SKirstóf Umann IsEnabledFn IsEnabled);
198e22f1c02SKirstóf Umann
199b6cbe6cbSKirstóf Umann /// Collects weak dependencies in \p enabledData.Checkers.
200e22f1c02SKirstóf Umann template <typename IsEnabledFn>
201b6cbe6cbSKirstóf Umann static void collectWeakDependencies(const ConstCheckerInfoList &Deps,
202e22f1c02SKirstóf Umann const CheckerManager &Mgr,
203b6cbe6cbSKirstóf Umann CheckerInfoSet &Ret, IsEnabledFn IsEnabled);
2048fd74ebfSKristof Umann
initializeRegistry(const CheckerManager & Mgr)2052aac0c47SKristóf Umann void CheckerRegistry::initializeRegistry(const CheckerManager &Mgr) {
206e22f1c02SKirstóf Umann // First, we calculate the list of enabled checkers as specified by the
207e22f1c02SKirstóf Umann // invocation. Weak dependencies will not enable their unspecified strong
208e22f1c02SKirstóf Umann // depenencies, but its only after resolving strong dependencies for all
209e22f1c02SKirstóf Umann // checkers when we know whether they will be enabled.
210e22f1c02SKirstóf Umann CheckerInfoSet Tmp;
211e22f1c02SKirstóf Umann auto IsEnabledFromCmdLine = [&](const CheckerInfo *Checker) {
212e22f1c02SKirstóf Umann return !Checker->isDisabled(Mgr);
213e22f1c02SKirstóf Umann };
214b6cbe6cbSKirstóf Umann for (const CheckerInfo &Checker : Data.Checkers) {
215bda3dd0dSKirstóf Umann if (!Checker.isEnabled(Mgr))
2168fd74ebfSKristof Umann continue;
2178fd74ebfSKristof Umann
218e22f1c02SKirstóf Umann CheckerInfoSet Deps;
219e22f1c02SKirstóf Umann if (!collectStrongDependencies(Checker.Dependencies, Mgr, Deps,
220e22f1c02SKirstóf Umann IsEnabledFromCmdLine)) {
221e22f1c02SKirstóf Umann // If we failed to enable any of the dependencies, don't enable this
222e22f1c02SKirstóf Umann // checker.
223e22f1c02SKirstóf Umann continue;
224e22f1c02SKirstóf Umann }
2258fd74ebfSKristof Umann
226e22f1c02SKirstóf Umann Tmp.insert(Deps.begin(), Deps.end());
227e22f1c02SKirstóf Umann
228e22f1c02SKirstóf Umann // Enable the checker.
229e22f1c02SKirstóf Umann Tmp.insert(&Checker);
230e22f1c02SKirstóf Umann }
231e22f1c02SKirstóf Umann
232e22f1c02SKirstóf Umann // Calculate enabled checkers with the correct registration order. As this is
233e22f1c02SKirstóf Umann // done recursively, its arguably cheaper, but for sure less error prone to
234e22f1c02SKirstóf Umann // recalculate from scratch.
235e22f1c02SKirstóf Umann auto IsEnabled = [&](const CheckerInfo *Checker) {
2368bdf3878SKazu Hirata return Tmp.contains(Checker);
237e22f1c02SKirstóf Umann };
238b6cbe6cbSKirstóf Umann for (const CheckerInfo &Checker : Data.Checkers) {
239e22f1c02SKirstóf Umann if (!Checker.isEnabled(Mgr))
240e22f1c02SKirstóf Umann continue;
241e22f1c02SKirstóf Umann
242e22f1c02SKirstóf Umann CheckerInfoSet Deps;
243e22f1c02SKirstóf Umann
244e22f1c02SKirstóf Umann collectWeakDependencies(Checker.WeakDependencies, Mgr, Deps, IsEnabled);
245e22f1c02SKirstóf Umann
246e22f1c02SKirstóf Umann if (!collectStrongDependencies(Checker.Dependencies, Mgr, Deps,
247e22f1c02SKirstóf Umann IsEnabledFromCmdLine)) {
2488fd74ebfSKristof Umann // If we failed to enable any of the dependencies, don't enable this
2498fd74ebfSKristof Umann // checker.
2508fd74ebfSKristof Umann continue;
2518fd74ebfSKristof Umann }
2528fd74ebfSKristof Umann
2538fd74ebfSKristof Umann // Note that set_union also preserves the order of insertion.
254b6cbe6cbSKirstóf Umann Data.EnabledCheckers.set_union(Deps);
255b6cbe6cbSKirstóf Umann Data.EnabledCheckers.insert(&Checker);
256058a7a45SKristof Umann }
2572aac0c47SKristóf Umann }
25876a21502SKristof Umann
259e22f1c02SKirstóf Umann template <typename IsEnabledFn>
collectStrongDependencies(const ConstCheckerInfoList & Deps,const CheckerManager & Mgr,CheckerInfoSet & Ret,IsEnabledFn IsEnabled)260b6cbe6cbSKirstóf Umann static bool collectStrongDependencies(const ConstCheckerInfoList &Deps,
2612aac0c47SKristóf Umann const CheckerManager &Mgr,
262b6cbe6cbSKirstóf Umann CheckerInfoSet &Ret,
263e22f1c02SKirstóf Umann IsEnabledFn IsEnabled) {
2642aac0c47SKristóf Umann
265b6cbe6cbSKirstóf Umann for (const CheckerInfo *Dependency : Deps) {
266e22f1c02SKirstóf Umann if (!IsEnabled(Dependency))
2672aac0c47SKristóf Umann return false;
2682aac0c47SKristóf Umann
2692aac0c47SKristóf Umann // Collect dependencies recursively.
270e22f1c02SKirstóf Umann if (!collectStrongDependencies(Dependency->Dependencies, Mgr, Ret,
271e22f1c02SKirstóf Umann IsEnabled))
2722aac0c47SKristóf Umann return false;
2732aac0c47SKristóf Umann Ret.insert(Dependency);
2742aac0c47SKristóf Umann }
2752aac0c47SKristóf Umann
2762aac0c47SKristóf Umann return true;
27776a21502SKristof Umann }
27876a21502SKristof Umann
279e22f1c02SKirstóf Umann template <typename IsEnabledFn>
collectWeakDependencies(const ConstCheckerInfoList & WeakDeps,const CheckerManager & Mgr,CheckerInfoSet & Ret,IsEnabledFn IsEnabled)280b6cbe6cbSKirstóf Umann static void collectWeakDependencies(const ConstCheckerInfoList &WeakDeps,
281e22f1c02SKirstóf Umann const CheckerManager &Mgr,
282b6cbe6cbSKirstóf Umann CheckerInfoSet &Ret,
283e22f1c02SKirstóf Umann IsEnabledFn IsEnabled) {
284e22f1c02SKirstóf Umann
285b6cbe6cbSKirstóf Umann for (const CheckerInfo *Dependency : WeakDeps) {
286e22f1c02SKirstóf Umann // Don't enable this checker if strong dependencies are unsatisfied, but
287e22f1c02SKirstóf Umann // assume that weak dependencies are transitive.
288e22f1c02SKirstóf Umann collectWeakDependencies(Dependency->WeakDependencies, Mgr, Ret, IsEnabled);
289e22f1c02SKirstóf Umann
290e22f1c02SKirstóf Umann if (IsEnabled(Dependency) &&
291e22f1c02SKirstóf Umann collectStrongDependencies(Dependency->Dependencies, Mgr, Ret,
292e22f1c02SKirstóf Umann IsEnabled))
293e22f1c02SKirstóf Umann Ret.insert(Dependency);
294e22f1c02SKirstóf Umann }
295e22f1c02SKirstóf Umann }
296e22f1c02SKirstóf Umann
resolveDependencies()297e22f1c02SKirstóf Umann template <bool IsWeak> void CheckerRegistry::resolveDependencies() {
298e22f1c02SKirstóf Umann for (const std::pair<StringRef, StringRef> &Entry :
299b6cbe6cbSKirstóf Umann (IsWeak ? Data.WeakDependencies : Data.Dependencies)) {
300e22f1c02SKirstóf Umann
301b6cbe6cbSKirstóf Umann auto CheckerIt = binaryFind(Data.Checkers, Entry.first);
302b6cbe6cbSKirstóf Umann assert(CheckerIt != Data.Checkers.end() &&
303b6cbe6cbSKirstóf Umann CheckerIt->FullName == Entry.first &&
304b9bc7ec3SKristof Umann "Failed to find the checker while attempting to set up its "
305b9bc7ec3SKristof Umann "dependencies!");
306b9bc7ec3SKristof Umann
307b6cbe6cbSKirstóf Umann auto DependencyIt = binaryFind(Data.Checkers, Entry.second);
308b6cbe6cbSKirstóf Umann assert(DependencyIt != Data.Checkers.end() &&
309cd3f1474SKristof Umann DependencyIt->FullName == Entry.second &&
310b9bc7ec3SKristof Umann "Failed to find the dependency of a checker!");
311b9bc7ec3SKristof Umann
312690ff37aSKirstóf Umann // We do allow diagnostics from unit test/example dependency checkers.
313*f3dcc235SKazu Hirata assert((DependencyIt->FullName.starts_with("test") ||
314*f3dcc235SKazu Hirata DependencyIt->FullName.starts_with("example") || IsWeak ||
315690ff37aSKirstóf Umann DependencyIt->IsHidden) &&
316690ff37aSKirstóf Umann "Strong dependencies are modeling checkers, and as such "
317690ff37aSKirstóf Umann "non-user facing! Mark them hidden in Checkers.td!");
318690ff37aSKirstóf Umann
319e22f1c02SKirstóf Umann if (IsWeak)
320e22f1c02SKirstóf Umann CheckerIt->WeakDependencies.emplace_back(&*DependencyIt);
321e22f1c02SKirstóf Umann else
322640f7b58SKristof Umann CheckerIt->Dependencies.emplace_back(&*DependencyIt);
323b9bc7ec3SKristof Umann }
324cd3f1474SKristof Umann }
325cd3f1474SKristof Umann
addDependency(StringRef FullName,StringRef Dependency)326cd3f1474SKristof Umann void CheckerRegistry::addDependency(StringRef FullName, StringRef Dependency) {
327b6cbe6cbSKirstóf Umann Data.Dependencies.emplace_back(FullName, Dependency);
328cd3f1474SKristof Umann }
329cd3f1474SKristof Umann
addWeakDependency(StringRef FullName,StringRef Dependency)330e22f1c02SKirstóf Umann void CheckerRegistry::addWeakDependency(StringRef FullName,
331e22f1c02SKirstóf Umann StringRef Dependency) {
332b6cbe6cbSKirstóf Umann Data.WeakDependencies.emplace_back(FullName, Dependency);
333e22f1c02SKirstóf Umann }
334e22f1c02SKirstóf Umann
335e22f1c02SKirstóf Umann //===----------------------------------------------------------------------===//
336e22f1c02SKirstóf Umann // Checker option resolving and validating.
337e22f1c02SKirstóf Umann //===----------------------------------------------------------------------===//
338e22f1c02SKirstóf Umann
33985cf76e7SKristof Umann /// Insert the checker/package option to AnalyzerOptions' config table, and
34085cf76e7SKristof Umann /// validate it, if the user supplied it on the command line.
insertAndValidate(StringRef FullName,const CmdLineOption & Option,AnalyzerOptions & AnOpts,DiagnosticsEngine & Diags)341b6cbe6cbSKirstóf Umann static void insertAndValidate(StringRef FullName, const CmdLineOption &Option,
34285cf76e7SKristof Umann AnalyzerOptions &AnOpts,
34385cf76e7SKristof Umann DiagnosticsEngine &Diags) {
34485cf76e7SKristof Umann
34585cf76e7SKristof Umann std::string FullOption = (FullName + ":" + Option.OptionName).str();
34685cf76e7SKristof Umann
347adcd0268SBenjamin Kramer auto It =
348adcd0268SBenjamin Kramer AnOpts.Config.insert({FullOption, std::string(Option.DefaultValStr)});
34985cf76e7SKristof Umann
35085cf76e7SKristof Umann // Insertation was successful -- CmdLineOption's constructor will validate
35185cf76e7SKristof Umann // whether values received from plugins or TableGen files are correct.
35285cf76e7SKristof Umann if (It.second)
35385cf76e7SKristof Umann return;
35485cf76e7SKristof Umann
35585cf76e7SKristof Umann // Insertion failed, the user supplied this package/checker option on the
35683cc1b35SKristof Umann // command line. If the supplied value is invalid, we'll restore the option
35783cc1b35SKristof Umann // to it's default value, and if we're in non-compatibility mode, we'll also
35883cc1b35SKristof Umann // emit an error.
35985cf76e7SKristof Umann
36085cf76e7SKristof Umann StringRef SuppliedValue = It.first->getValue();
36185cf76e7SKristof Umann
36285cf76e7SKristof Umann if (Option.OptionType == "bool") {
36385cf76e7SKristof Umann if (SuppliedValue != "true" && SuppliedValue != "false") {
36485cf76e7SKristof Umann if (AnOpts.ShouldEmitErrorsOnInvalidConfigValue) {
36585cf76e7SKristof Umann Diags.Report(diag::err_analyzer_checker_option_invalid_input)
36685cf76e7SKristof Umann << FullOption << "a boolean value";
36785cf76e7SKristof Umann }
36883cc1b35SKristof Umann
369adcd0268SBenjamin Kramer It.first->setValue(std::string(Option.DefaultValStr));
37085cf76e7SKristof Umann }
37185cf76e7SKristof Umann return;
37285cf76e7SKristof Umann }
37385cf76e7SKristof Umann
37485cf76e7SKristof Umann if (Option.OptionType == "int") {
37585cf76e7SKristof Umann int Tmp;
37685cf76e7SKristof Umann bool HasFailed = SuppliedValue.getAsInteger(0, Tmp);
37785cf76e7SKristof Umann if (HasFailed) {
37885cf76e7SKristof Umann if (AnOpts.ShouldEmitErrorsOnInvalidConfigValue) {
37985cf76e7SKristof Umann Diags.Report(diag::err_analyzer_checker_option_invalid_input)
38085cf76e7SKristof Umann << FullOption << "an integer value";
38185cf76e7SKristof Umann }
38283cc1b35SKristof Umann
383adcd0268SBenjamin Kramer It.first->setValue(std::string(Option.DefaultValStr));
38485cf76e7SKristof Umann }
38585cf76e7SKristof Umann return;
38685cf76e7SKristof Umann }
38785cf76e7SKristof Umann }
38885cf76e7SKristof Umann
389b4788b26SKristof Umann template <class T>
insertOptionToCollection(StringRef FullName,T & Collection,const CmdLineOption & Option,AnalyzerOptions & AnOpts,DiagnosticsEngine & Diags)390b6cbe6cbSKirstóf Umann static void insertOptionToCollection(StringRef FullName, T &Collection,
391b6cbe6cbSKirstóf Umann const CmdLineOption &Option,
392b6cbe6cbSKirstóf Umann AnalyzerOptions &AnOpts,
393b6cbe6cbSKirstóf Umann DiagnosticsEngine &Diags) {
394b4788b26SKristof Umann auto It = binaryFind(Collection, FullName);
395b4788b26SKristof Umann assert(It != Collection.end() &&
396b4788b26SKristof Umann "Failed to find the checker while attempting to add a command line "
397b4788b26SKristof Umann "option to it!");
398b4788b26SKristof Umann
39985cf76e7SKristof Umann insertAndValidate(FullName, Option, AnOpts, Diags);
40030b2307dSKristof Umann
40130b2307dSKristof Umann It->CmdLineOptions.emplace_back(Option);
402b4788b26SKristof Umann }
403b4788b26SKristof Umann
resolveCheckerAndPackageOptions()404b4788b26SKristof Umann void CheckerRegistry::resolveCheckerAndPackageOptions() {
405b4788b26SKristof Umann for (const std::pair<StringRef, CmdLineOption> &CheckerOptEntry :
406b6cbe6cbSKirstóf Umann Data.CheckerOptions) {
407b6cbe6cbSKirstóf Umann insertOptionToCollection(CheckerOptEntry.first, Data.Checkers,
40885cf76e7SKristof Umann CheckerOptEntry.second, AnOpts, Diags);
409b4788b26SKristof Umann }
410b4788b26SKristof Umann
411b4788b26SKristof Umann for (const std::pair<StringRef, CmdLineOption> &PackageOptEntry :
412b6cbe6cbSKirstóf Umann Data.PackageOptions) {
413b6cbe6cbSKirstóf Umann insertOptionToCollection(PackageOptEntry.first, Data.Packages,
41485cf76e7SKristof Umann PackageOptEntry.second, AnOpts, Diags);
415b4788b26SKristof Umann }
416b4788b26SKristof Umann }
417b4788b26SKristof Umann
addPackage(StringRef FullName)418b4788b26SKristof Umann void CheckerRegistry::addPackage(StringRef FullName) {
419b6cbe6cbSKirstóf Umann Data.Packages.emplace_back(PackageInfo(FullName));
420b4788b26SKristof Umann }
421b4788b26SKristof Umann
addPackageOption(StringRef OptionType,StringRef PackageFullName,StringRef OptionName,StringRef DefaultValStr,StringRef Description,StringRef DevelopmentStatus,bool IsHidden)422b4788b26SKristof Umann void CheckerRegistry::addPackageOption(StringRef OptionType,
423b4788b26SKristof Umann StringRef PackageFullName,
424b4788b26SKristof Umann StringRef OptionName,
425b4788b26SKristof Umann StringRef DefaultValStr,
426ac95c865SKristof Umann StringRef Description,
427ac95c865SKristof Umann StringRef DevelopmentStatus,
428ac95c865SKristof Umann bool IsHidden) {
429b6cbe6cbSKirstóf Umann Data.PackageOptions.emplace_back(
4307e55ed84SKristof Umann PackageFullName, CmdLineOption{OptionType, OptionName, DefaultValStr,
431ac95c865SKristof Umann Description, DevelopmentStatus, IsHidden});
432b4788b26SKristof Umann }
433b4788b26SKristof Umann
addChecker(RegisterCheckerFn Rfn,ShouldRegisterFunction Sfn,StringRef Name,StringRef Desc,StringRef DocsUri,bool IsHidden)434b6cbe6cbSKirstóf Umann void CheckerRegistry::addChecker(RegisterCheckerFn Rfn,
435b4788b26SKristof Umann ShouldRegisterFunction Sfn, StringRef Name,
4369f7fc983SKristof Umann StringRef Desc, StringRef DocsUri,
4379f7fc983SKristof Umann bool IsHidden) {
438b6cbe6cbSKirstóf Umann Data.Checkers.emplace_back(Rfn, Sfn, Name, Desc, DocsUri, IsHidden);
439b4788b26SKristof Umann
440b4788b26SKristof Umann // Record the presence of the checker in its packages.
441b4788b26SKristof Umann StringRef PackageName, LeafName;
442b4788b26SKristof Umann std::tie(PackageName, LeafName) = Name.rsplit(PackageSeparator);
443b4788b26SKristof Umann while (!LeafName.empty()) {
444b6cbe6cbSKirstóf Umann Data.PackageSizes[PackageName] += 1;
445b4788b26SKristof Umann std::tie(PackageName, LeafName) = PackageName.rsplit(PackageSeparator);
446b4788b26SKristof Umann }
447b4788b26SKristof Umann }
448b4788b26SKristof Umann
addCheckerOption(StringRef OptionType,StringRef CheckerFullName,StringRef OptionName,StringRef DefaultValStr,StringRef Description,StringRef DevelopmentStatus,bool IsHidden)449b4788b26SKristof Umann void CheckerRegistry::addCheckerOption(StringRef OptionType,
450b4788b26SKristof Umann StringRef CheckerFullName,
451b4788b26SKristof Umann StringRef OptionName,
452b4788b26SKristof Umann StringRef DefaultValStr,
453ac95c865SKristof Umann StringRef Description,
454ac95c865SKristof Umann StringRef DevelopmentStatus,
455ac95c865SKristof Umann bool IsHidden) {
456b6cbe6cbSKirstóf Umann Data.CheckerOptions.emplace_back(
4577e55ed84SKristof Umann CheckerFullName, CmdLineOption{OptionType, OptionName, DefaultValStr,
458ac95c865SKristof Umann Description, DevelopmentStatus, IsHidden});
459b4788b26SKristof Umann }
460b4788b26SKristof Umann
initializeManager(CheckerManager & CheckerMgr) const461b9bc7ec3SKristof Umann void CheckerRegistry::initializeManager(CheckerManager &CheckerMgr) const {
46276a21502SKristof Umann // Initialize the CheckerManager with all enabled checkers.
463b6cbe6cbSKirstóf Umann for (const auto *Checker : Data.EnabledCheckers) {
46472649423SKristof Umann CheckerMgr.setCurrentCheckerName(CheckerNameRef(Checker->FullName));
465b9bc7ec3SKristof Umann Checker->Initialize(CheckerMgr);
46676a21502SKristof Umann }
46776a21502SKristof Umann }
46876a21502SKristof Umann
isOptionContainedIn(const CmdLineOptionList & OptionList,StringRef SuppliedChecker,StringRef SuppliedOption,const AnalyzerOptions & AnOpts,DiagnosticsEngine & Diags)469b6cbe6cbSKirstóf Umann static void isOptionContainedIn(const CmdLineOptionList &OptionList,
470b6cbe6cbSKirstóf Umann StringRef SuppliedChecker,
471b6cbe6cbSKirstóf Umann StringRef SuppliedOption,
472b6cbe6cbSKirstóf Umann const AnalyzerOptions &AnOpts,
473b6cbe6cbSKirstóf Umann DiagnosticsEngine &Diags) {
47485cf76e7SKristof Umann
47585cf76e7SKristof Umann if (!AnOpts.ShouldEmitErrorsOnInvalidConfigValue)
47685cf76e7SKristof Umann return;
47785cf76e7SKristof Umann
47885cf76e7SKristof Umann auto SameOptName = [SuppliedOption](const CmdLineOption &Opt) {
47985cf76e7SKristof Umann return Opt.OptionName == SuppliedOption;
48085cf76e7SKristof Umann };
48185cf76e7SKristof Umann
482f13019f8SKazu Hirata if (llvm::none_of(OptionList, SameOptName)) {
48385cf76e7SKristof Umann Diags.Report(diag::err_analyzer_checker_option_unknown)
48485cf76e7SKristof Umann << SuppliedChecker << SuppliedOption;
48585cf76e7SKristof Umann return;
48685cf76e7SKristof Umann }
48785cf76e7SKristof Umann }
48885cf76e7SKristof Umann
validateCheckerOptions() const489dd9c86e5SKristof Umann void CheckerRegistry::validateCheckerOptions() const {
490b9bc7ec3SKristof Umann for (const auto &Config : AnOpts.Config) {
49185cf76e7SKristof Umann
492a079a427SCsaba Dabis StringRef SuppliedCheckerOrPackage;
49385cf76e7SKristof Umann StringRef SuppliedOption;
494a079a427SCsaba Dabis std::tie(SuppliedCheckerOrPackage, SuppliedOption) =
495a079a427SCsaba Dabis Config.getKey().split(':');
49685cf76e7SKristof Umann
49785cf76e7SKristof Umann if (SuppliedOption.empty())
49876a21502SKristof Umann continue;
49976a21502SKristof Umann
50085cf76e7SKristof Umann // AnalyzerOptions' config table contains the user input, so an entry could
50185cf76e7SKristof Umann // look like this:
50285cf76e7SKristof Umann //
50385cf76e7SKristof Umann // cor:NoFalsePositives=true
50485cf76e7SKristof Umann //
50585cf76e7SKristof Umann // Since lower_bound would look for the first element *not less* than "cor",
50685cf76e7SKristof Umann // it would return with an iterator to the first checker in the core, so we
50785cf76e7SKristof Umann // we really have to use find here, which uses operator==.
508a079a427SCsaba Dabis auto CheckerIt =
509b6cbe6cbSKirstóf Umann llvm::find(Data.Checkers, CheckerInfo(SuppliedCheckerOrPackage));
510b6cbe6cbSKirstóf Umann if (CheckerIt != Data.Checkers.end()) {
511a079a427SCsaba Dabis isOptionContainedIn(CheckerIt->CmdLineOptions, SuppliedCheckerOrPackage,
51285cf76e7SKristof Umann SuppliedOption, AnOpts, Diags);
51385cf76e7SKristof Umann continue;
51476a21502SKristof Umann }
51585cf76e7SKristof Umann
516e22f1c02SKirstóf Umann const auto *PackageIt =
517b6cbe6cbSKirstóf Umann llvm::find(Data.Packages, PackageInfo(SuppliedCheckerOrPackage));
518b6cbe6cbSKirstóf Umann if (PackageIt != Data.Packages.end()) {
519a079a427SCsaba Dabis isOptionContainedIn(PackageIt->CmdLineOptions, SuppliedCheckerOrPackage,
52085cf76e7SKristof Umann SuppliedOption, AnOpts, Diags);
52185cf76e7SKristof Umann continue;
52276a21502SKristof Umann }
52385cf76e7SKristof Umann
524a079a427SCsaba Dabis Diags.Report(diag::err_unknown_analyzer_checker_or_package)
525a079a427SCsaba Dabis << SuppliedCheckerOrPackage;
52676a21502SKristof Umann }
52776a21502SKristof Umann }
528