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