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