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