Eugene Zelenko | 88f40cf | 2018-04-03 21:31:50 +0000 | [diff] [blame] | 1 | //===- CheckerRegistry.cpp - Maintains all available checkers -------------===// |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Kristof Umann | 76a2150 | 2018-12-15 16:23:51 +0000 | [diff] [blame] | 9 | #include "clang/StaticAnalyzer/Frontend/CheckerRegistry.h" |
Gabor Horvath | fc4c4d4 | 2015-07-09 21:43:45 +0000 | [diff] [blame] | 10 | #include "clang/Basic/Diagnostic.h" |
Eugene Zelenko | 88f40cf | 2018-04-03 21:31:50 +0000 | [diff] [blame] | 11 | #include "clang/Basic/LLVM.h" |
Kristof Umann | b0be2ab | 2018-12-15 18:11:49 +0000 | [diff] [blame] | 12 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 13 | #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" |
Gabor Horvath | fc4c4d4 | 2015-07-09 21:43:45 +0000 | [diff] [blame] | 14 | #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h" |
Kristof Umann | a57d4ea | 2019-04-18 17:32:51 +0000 | [diff] [blame] | 15 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Eugene Zelenko | 88f40cf | 2018-04-03 21:31:50 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
Anna Zaks | 3037315 | 2011-12-15 01:36:04 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SetVector.h" |
Eugene Zelenko | 88f40cf | 2018-04-03 21:31:50 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringMap.h" |
| 19 | #include "llvm/ADT/StringRef.h" |
Kristof Umann | b0be2ab | 2018-12-15 18:11:49 +0000 | [diff] [blame] | 20 | #include "llvm/Support/DynamicLibrary.h" |
| 21 | #include "llvm/Support/Path.h" |
Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 88f40cf | 2018-04-03 21:31:50 +0000 | [diff] [blame] | 23 | #include <algorithm> |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace clang; |
| 26 | using namespace ento; |
Kristof Umann | b0be2ab | 2018-12-15 18:11:49 +0000 | [diff] [blame] | 27 | using llvm::sys::DynamicLibrary; |
| 28 | |
| 29 | using RegisterCheckersFn = void (*)(CheckerRegistry &); |
| 30 | |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 31 | static bool isCompatibleAPIVersion(const char *VersionString) { |
| 32 | // If the version string is null, its not an analyzer plugin. |
| 33 | if (!VersionString) |
Kristof Umann | b0be2ab | 2018-12-15 18:11:49 +0000 | [diff] [blame] | 34 | return false; |
| 35 | |
| 36 | // For now, none of the static analyzer API is considered stable. |
| 37 | // Versions must match exactly. |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 38 | return strcmp(VersionString, CLANG_ANALYZER_API_VERSION_STRING) == 0; |
Kristof Umann | b0be2ab | 2018-12-15 18:11:49 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 41 | namespace { |
Kristof Umann | a57d4ea | 2019-04-18 17:32:51 +0000 | [diff] [blame] | 42 | template <class T> struct FullNameLT { |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 43 | bool operator()(const T &Lhs, const T &Rhs) { |
| 44 | return Lhs.FullName < Rhs.FullName; |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | using CheckerNameLT = FullNameLT<CheckerRegistry::CheckerInfo>; |
| 49 | } // end of anonymous namespace |
Kristof Umann | 3daa245 | 2019-01-26 16:35:33 +0000 | [diff] [blame] | 50 | |
Kristof Umann | 640f7b5 | 2019-04-18 17:34:45 +0000 | [diff] [blame] | 51 | template <class CheckerOrPackageInfoList> |
| 52 | static |
| 53 | typename std::conditional<std::is_const<CheckerOrPackageInfoList>::value, |
| 54 | typename CheckerOrPackageInfoList::const_iterator, |
| 55 | typename CheckerOrPackageInfoList::iterator>::type |
| 56 | binaryFind(CheckerOrPackageInfoList &Collection, StringRef FullName) { |
| 57 | |
| 58 | using CheckerOrPackage = typename CheckerOrPackageInfoList::value_type; |
| 59 | using CheckerOrPackageFullNameLT = FullNameLT<CheckerOrPackage>; |
| 60 | |
| 61 | assert(std::is_sorted(Collection.begin(), Collection.end(), |
| 62 | CheckerOrPackageFullNameLT{}) && |
| 63 | "In order to efficiently gather checkers/packages, this function " |
| 64 | "expects them to be already sorted!"); |
| 65 | |
Fangrui Song | 9206335 | 2019-04-19 01:54:36 +0000 | [diff] [blame] | 66 | return llvm::lower_bound(Collection, CheckerOrPackage(FullName), |
| 67 | CheckerOrPackageFullNameLT{}); |
Kristof Umann | 640f7b5 | 2019-04-18 17:34:45 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Kristof Umann | 3daa245 | 2019-01-26 16:35:33 +0000 | [diff] [blame] | 70 | static constexpr char PackageSeparator = '.'; |
| 71 | |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 72 | static bool isInPackage(const CheckerRegistry::CheckerInfo &Checker, |
| 73 | StringRef PackageName) { |
Kristof Umann | 3daa245 | 2019-01-26 16:35:33 +0000 | [diff] [blame] | 74 | // Does the checker's full name have the package as a prefix? |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 75 | if (!Checker.FullName.startswith(PackageName)) |
Kristof Umann | 3daa245 | 2019-01-26 16:35:33 +0000 | [diff] [blame] | 76 | return false; |
| 77 | |
| 78 | // Is the package actually just the name of a specific checker? |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 79 | if (Checker.FullName.size() == PackageName.size()) |
Kristof Umann | 3daa245 | 2019-01-26 16:35:33 +0000 | [diff] [blame] | 80 | return true; |
| 81 | |
| 82 | // Is the checker in the package (or a subpackage)? |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 83 | if (Checker.FullName[PackageName.size()] == PackageSeparator) |
Kristof Umann | 3daa245 | 2019-01-26 16:35:33 +0000 | [diff] [blame] | 84 | return true; |
| 85 | |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | CheckerRegistry::CheckerInfoListRange |
| 90 | CheckerRegistry::getMutableCheckersForCmdLineArg(StringRef CmdLineArg) { |
Kristof Umann | 640f7b5 | 2019-04-18 17:34:45 +0000 | [diff] [blame] | 91 | auto It = binaryFind(Checkers, CmdLineArg); |
Kristof Umann | 3daa245 | 2019-01-26 16:35:33 +0000 | [diff] [blame] | 92 | |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 93 | if (!isInPackage(*It, CmdLineArg)) |
Kristof Umann | a57d4ea | 2019-04-18 17:32:51 +0000 | [diff] [blame] | 94 | return {Checkers.end(), Checkers.end()}; |
Kristof Umann | 3daa245 | 2019-01-26 16:35:33 +0000 | [diff] [blame] | 95 | |
| 96 | // See how large the package is. |
| 97 | // If the package doesn't exist, assume the option refers to a single |
| 98 | // checker. |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 99 | size_t Size = 1; |
| 100 | llvm::StringMap<size_t>::const_iterator PackageSize = |
| 101 | PackageSizes.find(CmdLineArg); |
Kristof Umann | 3daa245 | 2019-01-26 16:35:33 +0000 | [diff] [blame] | 102 | |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 103 | if (PackageSize != PackageSizes.end()) |
| 104 | Size = PackageSize->getValue(); |
Kristof Umann | 3daa245 | 2019-01-26 16:35:33 +0000 | [diff] [blame] | 105 | |
Kristof Umann | a57d4ea | 2019-04-18 17:32:51 +0000 | [diff] [blame] | 106 | return {It, It + Size}; |
Kristof Umann | 3daa245 | 2019-01-26 16:35:33 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Kristof Umann | 98217ad | 2019-01-26 17:27:40 +0000 | [diff] [blame] | 109 | CheckerRegistry::CheckerRegistry( |
Kristof Umann | a57d4ea | 2019-04-18 17:32:51 +0000 | [diff] [blame] | 110 | ArrayRef<std::string> Plugins, DiagnosticsEngine &Diags, |
| 111 | AnalyzerOptions &AnOpts, const LangOptions &LangOpts, |
| 112 | ArrayRef<std::function<void(CheckerRegistry &)>> CheckerRegistrationFns) |
| 113 | : Diags(Diags), AnOpts(AnOpts), LangOpts(LangOpts) { |
Kristof Umann | 058a7a4 | 2019-01-26 14:23:08 +0000 | [diff] [blame] | 114 | |
Kristof Umann | 3daa245 | 2019-01-26 16:35:33 +0000 | [diff] [blame] | 115 | // Register builtin checkers. |
Kristof Umann | b0be2ab | 2018-12-15 18:11:49 +0000 | [diff] [blame] | 116 | #define GET_CHECKERS |
Aaron Ballman | 2f234cb | 2018-12-20 20:20:20 +0000 | [diff] [blame] | 117 | #define CHECKER(FULLNAME, CLASS, HELPTEXT, DOC_URI) \ |
Kristof Umann | 058a7a4 | 2019-01-26 14:23:08 +0000 | [diff] [blame] | 118 | addChecker(register##CLASS, shouldRegister##CLASS, FULLNAME, HELPTEXT, \ |
| 119 | DOC_URI); |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 120 | |
Kristof Umann | b0be2ab | 2018-12-15 18:11:49 +0000 | [diff] [blame] | 121 | #include "clang/StaticAnalyzer/Checkers/Checkers.inc" |
| 122 | #undef CHECKER |
| 123 | #undef GET_CHECKERS |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 124 | #undef PACKAGE |
| 125 | #undef GET_PACKAGES |
Kristof Umann | b0be2ab | 2018-12-15 18:11:49 +0000 | [diff] [blame] | 126 | |
Kristof Umann | 3daa245 | 2019-01-26 16:35:33 +0000 | [diff] [blame] | 127 | // Register checkers from plugins. |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 128 | for (const std::string &Plugin : Plugins) { |
Kristof Umann | b0be2ab | 2018-12-15 18:11:49 +0000 | [diff] [blame] | 129 | // Get access to the plugin. |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 130 | std::string ErrorMsg; |
| 131 | DynamicLibrary Lib = |
| 132 | DynamicLibrary::getPermanentLibrary(Plugin.c_str(), &ErrorMsg); |
| 133 | if (!Lib.isValid()) { |
| 134 | Diags.Report(diag::err_fe_unable_to_load_plugin) << Plugin << ErrorMsg; |
Kristof Umann | b0be2ab | 2018-12-15 18:11:49 +0000 | [diff] [blame] | 135 | continue; |
| 136 | } |
| 137 | |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 138 | // See if its compatible with this build of clang. |
| 139 | const char *PluginAPIVersion = static_cast<const char *>( |
| 140 | Lib.getAddressOfSymbol("clang_analyzerAPIVersionString")); |
| 141 | |
| 142 | if (!isCompatibleAPIVersion(PluginAPIVersion)) { |
Kristof Umann | b0be2ab | 2018-12-15 18:11:49 +0000 | [diff] [blame] | 143 | Diags.Report(diag::warn_incompatible_analyzer_plugin_api) |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 144 | << llvm::sys::path::filename(Plugin); |
Kristof Umann | b0be2ab | 2018-12-15 18:11:49 +0000 | [diff] [blame] | 145 | Diags.Report(diag::note_incompatible_analyzer_plugin_api) |
Kristof Umann | a57d4ea | 2019-04-18 17:32:51 +0000 | [diff] [blame] | 146 | << CLANG_ANALYZER_API_VERSION_STRING << PluginAPIVersion; |
Kristof Umann | b0be2ab | 2018-12-15 18:11:49 +0000 | [diff] [blame] | 147 | continue; |
| 148 | } |
| 149 | |
| 150 | // Register its checkers. |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 151 | RegisterCheckersFn RegisterPluginCheckers = |
Kristof Umann | a57d4ea | 2019-04-18 17:32:51 +0000 | [diff] [blame] | 152 | reinterpret_cast<RegisterCheckersFn>( |
| 153 | Lib.getAddressOfSymbol("clang_registerCheckers")); |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 154 | if (RegisterPluginCheckers) |
| 155 | RegisterPluginCheckers(*this); |
Kristof Umann | b0be2ab | 2018-12-15 18:11:49 +0000 | [diff] [blame] | 156 | } |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 157 | |
Kristof Umann | 98217ad | 2019-01-26 17:27:40 +0000 | [diff] [blame] | 158 | // Register statically linked checkers, that aren't generated from the tblgen |
Kristof Umann | a57d4ea | 2019-04-18 17:32:51 +0000 | [diff] [blame] | 159 | // file, but rather passed their registry function as a parameter in |
| 160 | // checkerRegistrationFns. |
Kristof Umann | 98217ad | 2019-01-26 17:27:40 +0000 | [diff] [blame] | 161 | |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 162 | for (const auto &Fn : CheckerRegistrationFns) |
Kristof Umann | 98217ad | 2019-01-26 17:27:40 +0000 | [diff] [blame] | 163 | Fn(*this); |
| 164 | |
Kristof Umann | 3daa245 | 2019-01-26 16:35:33 +0000 | [diff] [blame] | 165 | // Sort checkers for efficient collection. |
| 166 | // FIXME: Alphabetical sort puts 'experimental' in the middle. |
| 167 | // Would it be better to name it '~experimental' or something else |
| 168 | // that's ASCIIbetically last? |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 169 | llvm::sort(Checkers, CheckerNameLT{}); |
Kristof Umann | 45beaa0 | 2018-11-18 12:47:03 +0000 | [diff] [blame] | 170 | |
Kristof Umann | 8fd74eb | 2019-01-26 20:06:54 +0000 | [diff] [blame] | 171 | #define GET_CHECKER_DEPENDENCIES |
| 172 | |
| 173 | #define CHECKER_DEPENDENCY(FULLNAME, DEPENDENCY) \ |
| 174 | addDependency(FULLNAME, DEPENDENCY); |
| 175 | |
| 176 | #include "clang/StaticAnalyzer/Checkers/Checkers.inc" |
| 177 | #undef CHECKER_DEPENDENCY |
| 178 | #undef GET_CHECKER_DEPENDENCIES |
| 179 | |
Kristof Umann | cd3f147 | 2019-04-19 11:01:35 +0000 | [diff] [blame^] | 180 | resolveDependencies(); |
| 181 | |
Kristof Umann | 3daa245 | 2019-01-26 16:35:33 +0000 | [diff] [blame] | 182 | // Parse '-analyzer-checker' and '-analyzer-disable-checker' options from the |
| 183 | // command line. |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 184 | for (const std::pair<std::string, bool> &Opt : AnOpts.CheckersControlList) { |
| 185 | CheckerInfoListRange CheckerForCmdLineArg = |
Kristof Umann | a57d4ea | 2019-04-18 17:32:51 +0000 | [diff] [blame] | 186 | getMutableCheckersForCmdLineArg(Opt.first); |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 187 | |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 188 | if (CheckerForCmdLineArg.begin() == CheckerForCmdLineArg.end()) { |
| 189 | Diags.Report(diag::err_unknown_analyzer_checker) << Opt.first; |
Kristof Umann | 3daa245 | 2019-01-26 16:35:33 +0000 | [diff] [blame] | 190 | Diags.Report(diag::note_suggest_disabling_all_checkers); |
| 191 | } |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 192 | |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 193 | for (CheckerInfo &checker : CheckerForCmdLineArg) { |
Kristof Umann | a57d4ea | 2019-04-18 17:32:51 +0000 | [diff] [blame] | 194 | checker.State = Opt.second ? StateFromCmdLine::State_Enabled |
| 195 | : StateFromCmdLine::State_Disabled; |
Kristof Umann | 3daa245 | 2019-01-26 16:35:33 +0000 | [diff] [blame] | 196 | } |
| 197 | } |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Kristof Umann | 8fd74eb | 2019-01-26 20:06:54 +0000 | [diff] [blame] | 200 | /// Collects dependencies in \p ret, returns false on failure. |
Kristof Umann | a57d4ea | 2019-04-18 17:32:51 +0000 | [diff] [blame] | 201 | static bool |
| 202 | collectDependenciesImpl(const CheckerRegistry::ConstCheckerInfoList &Deps, |
| 203 | const LangOptions &LO, |
| 204 | CheckerRegistry::CheckerInfoSet &Ret); |
Kristof Umann | 8fd74eb | 2019-01-26 20:06:54 +0000 | [diff] [blame] | 205 | |
| 206 | /// Collects dependenies in \p enabledCheckers. Return None on failure. |
| 207 | LLVM_NODISCARD |
Kristof Umann | a57d4ea | 2019-04-18 17:32:51 +0000 | [diff] [blame] | 208 | static llvm::Optional<CheckerRegistry::CheckerInfoSet> |
| 209 | collectDependencies(const CheckerRegistry::CheckerInfo &checker, |
| 210 | const LangOptions &LO) { |
Kristof Umann | 8fd74eb | 2019-01-26 20:06:54 +0000 | [diff] [blame] | 211 | |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 212 | CheckerRegistry::CheckerInfoSet Ret; |
Kristof Umann | 8fd74eb | 2019-01-26 20:06:54 +0000 | [diff] [blame] | 213 | // Add dependencies to the enabled checkers only if all of them can be |
| 214 | // enabled. |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 215 | if (!collectDependenciesImpl(checker.Dependencies, LO, Ret)) |
Kristof Umann | 8fd74eb | 2019-01-26 20:06:54 +0000 | [diff] [blame] | 216 | return None; |
| 217 | |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 218 | return Ret; |
Kristof Umann | 8fd74eb | 2019-01-26 20:06:54 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Kristof Umann | a57d4ea | 2019-04-18 17:32:51 +0000 | [diff] [blame] | 221 | static bool |
| 222 | collectDependenciesImpl(const CheckerRegistry::ConstCheckerInfoList &Deps, |
| 223 | const LangOptions &LO, |
| 224 | CheckerRegistry::CheckerInfoSet &Ret) { |
Kristof Umann | 8fd74eb | 2019-01-26 20:06:54 +0000 | [diff] [blame] | 225 | |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 226 | for (const CheckerRegistry::CheckerInfo *Dependency : Deps) { |
Kristof Umann | 8fd74eb | 2019-01-26 20:06:54 +0000 | [diff] [blame] | 227 | |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 228 | if (Dependency->isDisabled(LO)) |
Kristof Umann | 8fd74eb | 2019-01-26 20:06:54 +0000 | [diff] [blame] | 229 | return false; |
| 230 | |
| 231 | // Collect dependencies recursively. |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 232 | if (!collectDependenciesImpl(Dependency->Dependencies, LO, Ret)) |
Kristof Umann | 8fd74eb | 2019-01-26 20:06:54 +0000 | [diff] [blame] | 233 | return false; |
| 234 | |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 235 | Ret.insert(Dependency); |
Kristof Umann | 8fd74eb | 2019-01-26 20:06:54 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | return true; |
| 239 | } |
| 240 | |
Kristof Umann | dd9c86e | 2019-01-26 15:59:21 +0000 | [diff] [blame] | 241 | CheckerRegistry::CheckerInfoSet CheckerRegistry::getEnabledCheckers() const { |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 242 | |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 243 | CheckerInfoSet EnabledCheckers; |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 244 | |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 245 | for (const CheckerInfo &Checker : Checkers) { |
| 246 | if (!Checker.isEnabled(LangOpts)) |
Kristof Umann | 8fd74eb | 2019-01-26 20:06:54 +0000 | [diff] [blame] | 247 | continue; |
| 248 | |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 249 | // Recursively enable its dependencies. |
| 250 | llvm::Optional<CheckerInfoSet> Deps = |
| 251 | collectDependencies(Checker, LangOpts); |
Kristof Umann | 8fd74eb | 2019-01-26 20:06:54 +0000 | [diff] [blame] | 252 | |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 253 | if (!Deps) { |
Kristof Umann | 8fd74eb | 2019-01-26 20:06:54 +0000 | [diff] [blame] | 254 | // If we failed to enable any of the dependencies, don't enable this |
| 255 | // checker. |
| 256 | continue; |
| 257 | } |
| 258 | |
| 259 | // Note that set_union also preserves the order of insertion. |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 260 | EnabledCheckers.set_union(*Deps); |
Kristof Umann | 8fd74eb | 2019-01-26 20:06:54 +0000 | [diff] [blame] | 261 | |
| 262 | // Enable the checker. |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 263 | EnabledCheckers.insert(&Checker); |
Kristof Umann | f282d27 | 2018-12-15 15:44:05 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 266 | return EnabledCheckers; |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Kristof Umann | 058a7a4 | 2019-01-26 14:23:08 +0000 | [diff] [blame] | 269 | void CheckerRegistry::addChecker(InitializationFunction Rfn, |
| 270 | ShouldRegisterFunction Sfn, StringRef Name, |
Aaron Ballman | 2f234cb | 2018-12-20 20:20:20 +0000 | [diff] [blame] | 271 | StringRef Desc, StringRef DocsUri) { |
Kristof Umann | 058a7a4 | 2019-01-26 14:23:08 +0000 | [diff] [blame] | 272 | Checkers.emplace_back(Rfn, Sfn, Name, Desc, DocsUri); |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 273 | |
| 274 | // Record the presence of the checker in its packages. |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 275 | StringRef PackageName, LeafName; |
| 276 | std::tie(PackageName, LeafName) = Name.rsplit(PackageSeparator); |
| 277 | while (!LeafName.empty()) { |
| 278 | PackageSizes[PackageName] += 1; |
| 279 | std::tie(PackageName, LeafName) = PackageName.rsplit(PackageSeparator); |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
Kristof Umann | cd3f147 | 2019-04-19 11:01:35 +0000 | [diff] [blame^] | 283 | void CheckerRegistry::resolveDependencies() { |
| 284 | for (const std::pair<StringRef, StringRef> &Entry : Dependencies) { |
| 285 | auto CheckerIt = binaryFind(Checkers, Entry.first); |
| 286 | assert(CheckerIt != Checkers.end() && CheckerIt->FullName == Entry.first && |
| 287 | "Failed to find the checker while attempting to set up its " |
| 288 | "dependencies!"); |
| 289 | |
| 290 | auto DependencyIt = binaryFind(Checkers, Entry.second); |
| 291 | assert(DependencyIt != Checkers.end() && |
| 292 | DependencyIt->FullName == Entry.second && |
| 293 | "Failed to find the dependency of a checker!"); |
| 294 | |
| 295 | CheckerIt->Dependencies.emplace_back(&*DependencyIt); |
| 296 | } |
| 297 | |
| 298 | Dependencies.clear(); |
| 299 | } |
| 300 | |
Kristof Umann | 640f7b5 | 2019-04-18 17:34:45 +0000 | [diff] [blame] | 301 | void CheckerRegistry::addDependency(StringRef FullName, StringRef Dependency) { |
Kristof Umann | cd3f147 | 2019-04-19 11:01:35 +0000 | [diff] [blame^] | 302 | Dependencies.emplace_back(FullName, Dependency); |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | void CheckerRegistry::initializeManager(CheckerManager &CheckerMgr) const { |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 306 | // Collect checkers enabled by the options. |
Kristof Umann | dd9c86e | 2019-01-26 15:59:21 +0000 | [diff] [blame] | 307 | CheckerInfoSet enabledCheckers = getEnabledCheckers(); |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 308 | |
| 309 | // Initialize the CheckerManager with all enabled checkers. |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 310 | for (const auto *Checker : enabledCheckers) { |
| 311 | CheckerMgr.setCurrentCheckName(CheckName(Checker->FullName)); |
| 312 | Checker->Initialize(CheckerMgr); |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
Kristof Umann | dd9c86e | 2019-01-26 15:59:21 +0000 | [diff] [blame] | 316 | void CheckerRegistry::validateCheckerOptions() const { |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 317 | for (const auto &Config : AnOpts.Config) { |
| 318 | size_t Pos = Config.getKey().find(':'); |
| 319 | if (Pos == StringRef::npos) |
Gabor Horvath | fc4c4d4 | 2015-07-09 21:43:45 +0000 | [diff] [blame] | 320 | continue; |
| 321 | |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 322 | bool HasChecker = false; |
| 323 | StringRef CheckerName = Config.getKey().substr(0, Pos); |
| 324 | for (const auto &Checker : Checkers) { |
| 325 | if (Checker.FullName.startswith(CheckerName) && |
| 326 | (Checker.FullName.size() == Pos || Checker.FullName[Pos] == '.')) { |
| 327 | HasChecker = true; |
Gabor Horvath | fc4c4d4 | 2015-07-09 21:43:45 +0000 | [diff] [blame] | 328 | break; |
| 329 | } |
| 330 | } |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 331 | if (!HasChecker) |
| 332 | Diags.Report(diag::err_unknown_analyzer_checker) << CheckerName; |
Gabor Horvath | fc4c4d4 | 2015-07-09 21:43:45 +0000 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 336 | void CheckerRegistry::printCheckerWithDescList(raw_ostream &Out, |
| 337 | size_t MaxNameChars) const { |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 338 | // FIXME: Print available packages. |
| 339 | |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 340 | Out << "CHECKERS:\n"; |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 341 | |
| 342 | // Find the maximum option length. |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 343 | size_t OptionFieldWidth = 0; |
| 344 | for (const auto &Checker : Checkers) { |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 345 | // Limit the amount of padding we are willing to give up for alignment. |
| 346 | // Package.Name Description [Hidden] |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 347 | size_t NameLength = Checker.FullName.size(); |
| 348 | if (NameLength <= MaxNameChars) |
| 349 | OptionFieldWidth = std::max(OptionFieldWidth, NameLength); |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 352 | const size_t InitialPad = 2; |
| 353 | for (const auto &Checker : Checkers) { |
| 354 | Out.indent(InitialPad) << Checker.FullName; |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 355 | |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 356 | int Pad = OptionFieldWidth - Checker.FullName.size(); |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 357 | |
| 358 | // Break on long option names. |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 359 | if (Pad < 0) { |
| 360 | Out << '\n'; |
| 361 | Pad = OptionFieldWidth + InitialPad; |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 362 | } |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 363 | Out.indent(Pad + 2) << Checker.Desc; |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 364 | |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 365 | Out << '\n'; |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 366 | } |
| 367 | } |
Gabor Horvath | c430990 | 2016-08-08 13:41:04 +0000 | [diff] [blame] | 368 | |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 369 | void CheckerRegistry::printEnabledCheckerList(raw_ostream &Out) const { |
Gabor Horvath | c430990 | 2016-08-08 13:41:04 +0000 | [diff] [blame] | 370 | // Collect checkers enabled by the options. |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 371 | CheckerInfoSet EnabledCheckers = getEnabledCheckers(); |
Gabor Horvath | c430990 | 2016-08-08 13:41:04 +0000 | [diff] [blame] | 372 | |
Kristof Umann | b9bc7ec | 2019-04-18 15:19:16 +0000 | [diff] [blame] | 373 | for (const auto *i : EnabledCheckers) |
| 374 | Out << i->FullName << '\n'; |
Gabor Horvath | c430990 | 2016-08-08 13:41:04 +0000 | [diff] [blame] | 375 | } |