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