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 | |
| 31 | static bool isCompatibleAPIVersion(const char *versionString) { |
| 32 | // If the version string is null, it's not an analyzer plugin. |
| 33 | if (!versionString) |
| 34 | return false; |
| 35 | |
| 36 | // For now, none of the static analyzer API is considered stable. |
| 37 | // Versions must match exactly. |
| 38 | return strcmp(versionString, CLANG_ANALYZER_API_VERSION_STRING) == 0; |
| 39 | } |
| 40 | |
| 41 | CheckerRegistry::CheckerRegistry(ArrayRef<std::string> plugins, |
Kristof Umann | 058a7a4 | 2019-01-26 14:23:08 +0000 | [diff] [blame] | 42 | DiagnosticsEngine &diags, |
| 43 | const LangOptions &LangOpts) |
| 44 | : Diags(diags), LangOpts(LangOpts) { |
| 45 | |
Kristof Umann | b0be2ab | 2018-12-15 18:11:49 +0000 | [diff] [blame] | 46 | #define GET_CHECKERS |
Aaron Ballman | 2f234cb | 2018-12-20 20:20:20 +0000 | [diff] [blame] | 47 | #define CHECKER(FULLNAME, CLASS, HELPTEXT, DOC_URI) \ |
Kristof Umann | 058a7a4 | 2019-01-26 14:23:08 +0000 | [diff] [blame] | 48 | addChecker(register##CLASS, shouldRegister##CLASS, FULLNAME, HELPTEXT, \ |
| 49 | DOC_URI); |
Kristof Umann | b0be2ab | 2018-12-15 18:11:49 +0000 | [diff] [blame] | 50 | #include "clang/StaticAnalyzer/Checkers/Checkers.inc" |
| 51 | #undef CHECKER |
| 52 | #undef GET_CHECKERS |
| 53 | |
| 54 | for (ArrayRef<std::string>::iterator i = plugins.begin(), e = plugins.end(); |
| 55 | i != e; ++i) { |
| 56 | // Get access to the plugin. |
| 57 | std::string err; |
| 58 | DynamicLibrary lib = DynamicLibrary::getPermanentLibrary(i->c_str(), &err); |
| 59 | if (!lib.isValid()) { |
| 60 | diags.Report(diag::err_fe_unable_to_load_plugin) << *i << err; |
| 61 | continue; |
| 62 | } |
| 63 | |
| 64 | // See if it's compatible with this build of clang. |
| 65 | const char *pluginAPIVersion = |
| 66 | (const char *) lib.getAddressOfSymbol("clang_analyzerAPIVersionString"); |
| 67 | if (!isCompatibleAPIVersion(pluginAPIVersion)) { |
| 68 | Diags.Report(diag::warn_incompatible_analyzer_plugin_api) |
| 69 | << llvm::sys::path::filename(*i); |
| 70 | Diags.Report(diag::note_incompatible_analyzer_plugin_api) |
| 71 | << CLANG_ANALYZER_API_VERSION_STRING |
| 72 | << pluginAPIVersion; |
| 73 | continue; |
| 74 | } |
| 75 | |
| 76 | // Register its checkers. |
| 77 | RegisterCheckersFn registerPluginCheckers = |
| 78 | (RegisterCheckersFn) (intptr_t) lib.getAddressOfSymbol( |
| 79 | "clang_registerCheckers"); |
| 80 | if (registerPluginCheckers) |
| 81 | registerPluginCheckers(*this); |
| 82 | } |
| 83 | } |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 84 | |
Kristof Umann | f282d27 | 2018-12-15 15:44:05 +0000 | [diff] [blame] | 85 | static constexpr char PackageSeparator = '.'; |
Kristof Umann | 45beaa0 | 2018-11-18 12:47:03 +0000 | [diff] [blame] | 86 | |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 87 | static bool checkerNameLT(const CheckerRegistry::CheckerInfo &a, |
| 88 | const CheckerRegistry::CheckerInfo &b) { |
| 89 | return a.FullName < b.FullName; |
| 90 | } |
| 91 | |
| 92 | static bool isInPackage(const CheckerRegistry::CheckerInfo &checker, |
| 93 | StringRef packageName) { |
| 94 | // Does the checker's full name have the package as a prefix? |
| 95 | if (!checker.FullName.startswith(packageName)) |
| 96 | return false; |
| 97 | |
| 98 | // Is the package actually just the name of a specific checker? |
| 99 | if (checker.FullName.size() == packageName.size()) |
| 100 | return true; |
| 101 | |
| 102 | // Is the checker in the package (or a subpackage)? |
| 103 | if (checker.FullName[packageName.size()] == PackageSeparator) |
| 104 | return true; |
| 105 | |
| 106 | return false; |
| 107 | } |
| 108 | |
Kristof Umann | f282d27 | 2018-12-15 15:44:05 +0000 | [diff] [blame] | 109 | CheckerRegistry::CheckerInfoSet CheckerRegistry::getEnabledCheckers( |
Kristof Umann | b0be2ab | 2018-12-15 18:11:49 +0000 | [diff] [blame] | 110 | const AnalyzerOptions &Opts) const { |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 111 | |
Kristof Umann | f282d27 | 2018-12-15 15:44:05 +0000 | [diff] [blame] | 112 | assert(std::is_sorted(Checkers.begin(), Checkers.end(), checkerNameLT) && |
| 113 | "In order to efficiently gather checkers, this function expects them " |
| 114 | "to be already sorted!"); |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 115 | |
Kristof Umann | f282d27 | 2018-12-15 15:44:05 +0000 | [diff] [blame] | 116 | CheckerInfoSet enabledCheckers; |
| 117 | const auto end = Checkers.cend(); |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 118 | |
Kristof Umann | f282d27 | 2018-12-15 15:44:05 +0000 | [diff] [blame] | 119 | for (const std::pair<std::string, bool> &opt : Opts.CheckersControlList) { |
| 120 | // Use a binary search to find the possible start of the package. |
Kristof Umann | 058a7a4 | 2019-01-26 14:23:08 +0000 | [diff] [blame] | 121 | CheckerRegistry::CheckerInfo |
| 122 | packageInfo(nullptr, nullptr, opt.first, "", ""); |
Kristof Umann | f282d27 | 2018-12-15 15:44:05 +0000 | [diff] [blame] | 123 | auto firstRelatedChecker = |
| 124 | std::lower_bound(Checkers.cbegin(), end, packageInfo, checkerNameLT); |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 125 | |
Kristof Umann | f282d27 | 2018-12-15 15:44:05 +0000 | [diff] [blame] | 126 | if (firstRelatedChecker == end || |
| 127 | !isInPackage(*firstRelatedChecker, opt.first)) { |
Kristof Umann | b0be2ab | 2018-12-15 18:11:49 +0000 | [diff] [blame] | 128 | Diags.Report(diag::err_unknown_analyzer_checker) << opt.first; |
| 129 | Diags.Report(diag::note_suggest_disabling_all_checkers); |
Kristof Umann | f282d27 | 2018-12-15 15:44:05 +0000 | [diff] [blame] | 130 | return {}; |
| 131 | } |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 132 | |
Kristof Umann | f282d27 | 2018-12-15 15:44:05 +0000 | [diff] [blame] | 133 | // See how large the package is. |
| 134 | // If the package doesn't exist, assume the option refers to a single |
| 135 | // checker. |
| 136 | size_t size = 1; |
| 137 | llvm::StringMap<size_t>::const_iterator packageSize = |
| 138 | Packages.find(opt.first); |
| 139 | if (packageSize != Packages.end()) |
| 140 | size = packageSize->getValue(); |
| 141 | |
| 142 | // Step through all the checkers in the package. |
| 143 | for (auto lastRelatedChecker = firstRelatedChecker+size; |
| 144 | firstRelatedChecker != lastRelatedChecker; ++firstRelatedChecker) |
Kristof Umann | 058a7a4 | 2019-01-26 14:23:08 +0000 | [diff] [blame] | 145 | if (opt.second) { |
| 146 | if (firstRelatedChecker->ShouldRegister(LangOpts)) |
| 147 | enabledCheckers.insert(&*firstRelatedChecker); |
| 148 | } else { |
Kristof Umann | f282d27 | 2018-12-15 15:44:05 +0000 | [diff] [blame] | 149 | enabledCheckers.remove(&*firstRelatedChecker); |
Kristof Umann | 058a7a4 | 2019-01-26 14:23:08 +0000 | [diff] [blame] | 150 | } |
Kristof Umann | f282d27 | 2018-12-15 15:44:05 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | return enabledCheckers; |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Kristof Umann | 058a7a4 | 2019-01-26 14:23:08 +0000 | [diff] [blame] | 156 | void CheckerRegistry::addChecker(InitializationFunction Rfn, |
| 157 | ShouldRegisterFunction Sfn, StringRef Name, |
Aaron Ballman | 2f234cb | 2018-12-20 20:20:20 +0000 | [diff] [blame] | 158 | StringRef Desc, StringRef DocsUri) { |
Kristof Umann | 058a7a4 | 2019-01-26 14:23:08 +0000 | [diff] [blame] | 159 | Checkers.emplace_back(Rfn, Sfn, Name, Desc, DocsUri); |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 160 | |
| 161 | // Record the presence of the checker in its packages. |
| 162 | StringRef packageName, leafName; |
Aaron Ballman | 2f234cb | 2018-12-20 20:20:20 +0000 | [diff] [blame] | 163 | std::tie(packageName, leafName) = Name.rsplit(PackageSeparator); |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 164 | while (!leafName.empty()) { |
| 165 | Packages[packageName] += 1; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 166 | std::tie(packageName, leafName) = packageName.rsplit(PackageSeparator); |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 170 | void CheckerRegistry::initializeManager(CheckerManager &checkerMgr, |
Kristof Umann | b0be2ab | 2018-12-15 18:11:49 +0000 | [diff] [blame] | 171 | const AnalyzerOptions &Opts) const { |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 172 | // Sort checkers for efficient collection. |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 173 | llvm::sort(Checkers, checkerNameLT); |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 174 | |
| 175 | // Collect checkers enabled by the options. |
Kristof Umann | b0be2ab | 2018-12-15 18:11:49 +0000 | [diff] [blame] | 176 | CheckerInfoSet enabledCheckers = getEnabledCheckers(Opts); |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 177 | |
| 178 | // Initialize the CheckerManager with all enabled checkers. |
Kristof Umann | f282d27 | 2018-12-15 15:44:05 +0000 | [diff] [blame] | 179 | for (const auto *i : enabledCheckers) { |
Eugene Zelenko | 88f40cf | 2018-04-03 21:31:50 +0000 | [diff] [blame] | 180 | checkerMgr.setCurrentCheckName(CheckName(i->FullName)); |
| 181 | i->Initialize(checkerMgr); |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
Kristof Umann | b0be2ab | 2018-12-15 18:11:49 +0000 | [diff] [blame] | 185 | void CheckerRegistry::validateCheckerOptions( |
| 186 | const AnalyzerOptions &opts) const { |
Eugene Zelenko | 88f40cf | 2018-04-03 21:31:50 +0000 | [diff] [blame] | 187 | for (const auto &config : opts.Config) { |
Gabor Horvath | fc4c4d4 | 2015-07-09 21:43:45 +0000 | [diff] [blame] | 188 | size_t pos = config.getKey().find(':'); |
| 189 | if (pos == StringRef::npos) |
| 190 | continue; |
| 191 | |
| 192 | bool hasChecker = false; |
| 193 | StringRef checkerName = config.getKey().substr(0, pos); |
Eugene Zelenko | 88f40cf | 2018-04-03 21:31:50 +0000 | [diff] [blame] | 194 | for (const auto &checker : Checkers) { |
Gabor Horvath | fc4c4d4 | 2015-07-09 21:43:45 +0000 | [diff] [blame] | 195 | if (checker.FullName.startswith(checkerName) && |
| 196 | (checker.FullName.size() == pos || checker.FullName[pos] == '.')) { |
| 197 | hasChecker = true; |
| 198 | break; |
| 199 | } |
| 200 | } |
Eugene Zelenko | 88f40cf | 2018-04-03 21:31:50 +0000 | [diff] [blame] | 201 | if (!hasChecker) |
Kristof Umann | b0be2ab | 2018-12-15 18:11:49 +0000 | [diff] [blame] | 202 | Diags.Report(diag::err_unknown_analyzer_checker) << checkerName; |
Gabor Horvath | fc4c4d4 | 2015-07-09 21:43:45 +0000 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 206 | void CheckerRegistry::printHelp(raw_ostream &out, |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 207 | size_t maxNameChars) const { |
| 208 | // FIXME: Alphabetical sort puts 'experimental' in the middle. |
| 209 | // Would it be better to name it '~experimental' or something else |
| 210 | // that's ASCIIbetically last? |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 211 | llvm::sort(Checkers, checkerNameLT); |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 212 | |
| 213 | // FIXME: Print available packages. |
| 214 | |
| 215 | out << "CHECKERS:\n"; |
| 216 | |
| 217 | // Find the maximum option length. |
| 218 | size_t optionFieldWidth = 0; |
Eugene Zelenko | 88f40cf | 2018-04-03 21:31:50 +0000 | [diff] [blame] | 219 | for (const auto &i : Checkers) { |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 220 | // Limit the amount of padding we are willing to give up for alignment. |
| 221 | // Package.Name Description [Hidden] |
Eugene Zelenko | 88f40cf | 2018-04-03 21:31:50 +0000 | [diff] [blame] | 222 | size_t nameLength = i.FullName.size(); |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 223 | if (nameLength <= maxNameChars) |
| 224 | optionFieldWidth = std::max(optionFieldWidth, nameLength); |
| 225 | } |
| 226 | |
| 227 | const size_t initialPad = 2; |
Eugene Zelenko | 88f40cf | 2018-04-03 21:31:50 +0000 | [diff] [blame] | 228 | for (const auto &i : Checkers) { |
| 229 | out.indent(initialPad) << i.FullName; |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 230 | |
Eugene Zelenko | 88f40cf | 2018-04-03 21:31:50 +0000 | [diff] [blame] | 231 | int pad = optionFieldWidth - i.FullName.size(); |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 232 | |
| 233 | // Break on long option names. |
| 234 | if (pad < 0) { |
| 235 | out << '\n'; |
| 236 | pad = optionFieldWidth + initialPad; |
| 237 | } |
Eugene Zelenko | 88f40cf | 2018-04-03 21:31:50 +0000 | [diff] [blame] | 238 | out.indent(pad + 2) << i.Desc; |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 239 | |
| 240 | out << '\n'; |
| 241 | } |
| 242 | } |
Gabor Horvath | c430990 | 2016-08-08 13:41:04 +0000 | [diff] [blame] | 243 | |
Kristof Umann | 45beaa0 | 2018-11-18 12:47:03 +0000 | [diff] [blame] | 244 | void CheckerRegistry::printList(raw_ostream &out, |
Kristof Umann | b0be2ab | 2018-12-15 18:11:49 +0000 | [diff] [blame] | 245 | const AnalyzerOptions &opts) const { |
Kristof Umann | f282d27 | 2018-12-15 15:44:05 +0000 | [diff] [blame] | 246 | // Sort checkers for efficient collection. |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 247 | llvm::sort(Checkers, checkerNameLT); |
Gabor Horvath | c430990 | 2016-08-08 13:41:04 +0000 | [diff] [blame] | 248 | |
| 249 | // Collect checkers enabled by the options. |
Kristof Umann | b0be2ab | 2018-12-15 18:11:49 +0000 | [diff] [blame] | 250 | CheckerInfoSet enabledCheckers = getEnabledCheckers(opts); |
Gabor Horvath | c430990 | 2016-08-08 13:41:04 +0000 | [diff] [blame] | 251 | |
Eugene Zelenko | 88f40cf | 2018-04-03 21:31:50 +0000 | [diff] [blame] | 252 | for (const auto *i : enabledCheckers) |
| 253 | out << i->FullName << '\n'; |
Gabor Horvath | c430990 | 2016-08-08 13:41:04 +0000 | [diff] [blame] | 254 | } |