blob: d7cd4072a8db7423c52a328a2f276f319092afa1 [file] [log] [blame]
Eugene Zelenko88f40cf2018-04-03 21:31:50 +00001//===- CheckerRegistry.cpp - Maintains all available checkers -------------===//
Jordy Rose59cce712011-08-16 21:24:21 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Rose59cce712011-08-16 21:24:21 +00006//
7//===----------------------------------------------------------------------===//
8
Kristof Umann76a21502018-12-15 16:23:51 +00009#include "clang/StaticAnalyzer/Frontend/CheckerRegistry.h"
Gabor Horvathfc4c4d42015-07-09 21:43:45 +000010#include "clang/Basic/Diagnostic.h"
Eugene Zelenko88f40cf2018-04-03 21:31:50 +000011#include "clang/Basic/LLVM.h"
Kristof Umannb0be2ab2018-12-15 18:11:49 +000012#include "clang/Frontend/FrontendDiagnostic.h"
13#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
Eugene Zelenko88f40cf2018-04-03 21:31:50 +000014#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Gabor Horvathfc4c4d42015-07-09 21:43:45 +000015#include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
Eugene Zelenko88f40cf2018-04-03 21:31:50 +000016#include "llvm/ADT/STLExtras.h"
Anna Zaks30373152011-12-15 01:36:04 +000017#include "llvm/ADT/SetVector.h"
Eugene Zelenko88f40cf2018-04-03 21:31:50 +000018#include "llvm/ADT/StringMap.h"
19#include "llvm/ADT/StringRef.h"
Kristof Umannb0be2ab2018-12-15 18:11:49 +000020#include "llvm/Support/DynamicLibrary.h"
21#include "llvm/Support/Path.h"
Benjamin Kramer444a1302012-12-01 17:12:56 +000022#include "llvm/Support/raw_ostream.h"
Eugene Zelenko88f40cf2018-04-03 21:31:50 +000023#include <algorithm>
Jordy Rose59cce712011-08-16 21:24:21 +000024
25using namespace clang;
26using namespace ento;
Kristof Umannb0be2ab2018-12-15 18:11:49 +000027using llvm::sys::DynamicLibrary;
28
29using RegisterCheckersFn = void (*)(CheckerRegistry &);
30
31static 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
41CheckerRegistry::CheckerRegistry(ArrayRef<std::string> plugins,
Kristof Umann058a7a42019-01-26 14:23:08 +000042 DiagnosticsEngine &diags,
Kristof Umanndd9c86e2019-01-26 15:59:21 +000043 AnalyzerOptions &AnOpts,
Kristof Umann058a7a42019-01-26 14:23:08 +000044 const LangOptions &LangOpts)
Kristof Umanndd9c86e2019-01-26 15:59:21 +000045 : Diags(diags), AnOpts(AnOpts), LangOpts(LangOpts) {
Kristof Umann058a7a42019-01-26 14:23:08 +000046
Kristof Umannb0be2ab2018-12-15 18:11:49 +000047#define GET_CHECKERS
Aaron Ballman2f234cb2018-12-20 20:20:20 +000048#define CHECKER(FULLNAME, CLASS, HELPTEXT, DOC_URI) \
Kristof Umann058a7a42019-01-26 14:23:08 +000049 addChecker(register##CLASS, shouldRegister##CLASS, FULLNAME, HELPTEXT, \
50 DOC_URI);
Kristof Umannb0be2ab2018-12-15 18:11:49 +000051#include "clang/StaticAnalyzer/Checkers/Checkers.inc"
52#undef CHECKER
53#undef GET_CHECKERS
54
55 for (ArrayRef<std::string>::iterator i = plugins.begin(), e = plugins.end();
56 i != e; ++i) {
57 // Get access to the plugin.
58 std::string err;
59 DynamicLibrary lib = DynamicLibrary::getPermanentLibrary(i->c_str(), &err);
60 if (!lib.isValid()) {
61 diags.Report(diag::err_fe_unable_to_load_plugin) << *i << err;
62 continue;
63 }
64
65 // See if it's compatible with this build of clang.
66 const char *pluginAPIVersion =
67 (const char *) lib.getAddressOfSymbol("clang_analyzerAPIVersionString");
68 if (!isCompatibleAPIVersion(pluginAPIVersion)) {
69 Diags.Report(diag::warn_incompatible_analyzer_plugin_api)
70 << llvm::sys::path::filename(*i);
71 Diags.Report(diag::note_incompatible_analyzer_plugin_api)
72 << CLANG_ANALYZER_API_VERSION_STRING
73 << pluginAPIVersion;
74 continue;
75 }
76
77 // Register its checkers.
78 RegisterCheckersFn registerPluginCheckers =
79 (RegisterCheckersFn) (intptr_t) lib.getAddressOfSymbol(
80 "clang_registerCheckers");
81 if (registerPluginCheckers)
82 registerPluginCheckers(*this);
83 }
84}
Jordy Rose59cce712011-08-16 21:24:21 +000085
Kristof Umannf282d272018-12-15 15:44:05 +000086static constexpr char PackageSeparator = '.';
Kristof Umann45beaa02018-11-18 12:47:03 +000087
Jordy Rose59cce712011-08-16 21:24:21 +000088static bool checkerNameLT(const CheckerRegistry::CheckerInfo &a,
89 const CheckerRegistry::CheckerInfo &b) {
90 return a.FullName < b.FullName;
91}
92
93static bool isInPackage(const CheckerRegistry::CheckerInfo &checker,
94 StringRef packageName) {
95 // Does the checker's full name have the package as a prefix?
96 if (!checker.FullName.startswith(packageName))
97 return false;
98
99 // Is the package actually just the name of a specific checker?
100 if (checker.FullName.size() == packageName.size())
101 return true;
102
103 // Is the checker in the package (or a subpackage)?
104 if (checker.FullName[packageName.size()] == PackageSeparator)
105 return true;
106
107 return false;
108}
109
Kristof Umanndd9c86e2019-01-26 15:59:21 +0000110CheckerRegistry::CheckerInfoSet CheckerRegistry::getEnabledCheckers() const {
Jordy Rose59cce712011-08-16 21:24:21 +0000111
Kristof Umannf282d272018-12-15 15:44:05 +0000112 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 Rose59cce712011-08-16 21:24:21 +0000115
Kristof Umannf282d272018-12-15 15:44:05 +0000116 CheckerInfoSet enabledCheckers;
117 const auto end = Checkers.cend();
Jordy Rose59cce712011-08-16 21:24:21 +0000118
Kristof Umanndd9c86e2019-01-26 15:59:21 +0000119 for (const std::pair<std::string, bool> &opt : AnOpts.CheckersControlList) {
Kristof Umannf282d272018-12-15 15:44:05 +0000120 // Use a binary search to find the possible start of the package.
Kristof Umann058a7a42019-01-26 14:23:08 +0000121 CheckerRegistry::CheckerInfo
122 packageInfo(nullptr, nullptr, opt.first, "", "");
Kristof Umannf282d272018-12-15 15:44:05 +0000123 auto firstRelatedChecker =
124 std::lower_bound(Checkers.cbegin(), end, packageInfo, checkerNameLT);
Jordy Rose59cce712011-08-16 21:24:21 +0000125
Kristof Umannf282d272018-12-15 15:44:05 +0000126 if (firstRelatedChecker == end ||
127 !isInPackage(*firstRelatedChecker, opt.first)) {
Kristof Umannb0be2ab2018-12-15 18:11:49 +0000128 Diags.Report(diag::err_unknown_analyzer_checker) << opt.first;
129 Diags.Report(diag::note_suggest_disabling_all_checkers);
Kristof Umannf282d272018-12-15 15:44:05 +0000130 return {};
131 }
Jordy Rose59cce712011-08-16 21:24:21 +0000132
Kristof Umannf282d272018-12-15 15:44:05 +0000133 // 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 Umann058a7a42019-01-26 14:23:08 +0000145 if (opt.second) {
146 if (firstRelatedChecker->ShouldRegister(LangOpts))
147 enabledCheckers.insert(&*firstRelatedChecker);
148 } else {
Kristof Umannf282d272018-12-15 15:44:05 +0000149 enabledCheckers.remove(&*firstRelatedChecker);
Kristof Umann058a7a42019-01-26 14:23:08 +0000150 }
Kristof Umannf282d272018-12-15 15:44:05 +0000151 }
152
153 return enabledCheckers;
Jordy Rose59cce712011-08-16 21:24:21 +0000154}
155
Kristof Umann058a7a42019-01-26 14:23:08 +0000156void CheckerRegistry::addChecker(InitializationFunction Rfn,
157 ShouldRegisterFunction Sfn, StringRef Name,
Aaron Ballman2f234cb2018-12-20 20:20:20 +0000158 StringRef Desc, StringRef DocsUri) {
Kristof Umann058a7a42019-01-26 14:23:08 +0000159 Checkers.emplace_back(Rfn, Sfn, Name, Desc, DocsUri);
Jordy Rose59cce712011-08-16 21:24:21 +0000160
161 // Record the presence of the checker in its packages.
162 StringRef packageName, leafName;
Aaron Ballman2f234cb2018-12-20 20:20:20 +0000163 std::tie(packageName, leafName) = Name.rsplit(PackageSeparator);
Jordy Rose59cce712011-08-16 21:24:21 +0000164 while (!leafName.empty()) {
165 Packages[packageName] += 1;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +0000166 std::tie(packageName, leafName) = packageName.rsplit(PackageSeparator);
Jordy Rose59cce712011-08-16 21:24:21 +0000167 }
168}
169
Kristof Umanndd9c86e2019-01-26 15:59:21 +0000170void CheckerRegistry::initializeManager(CheckerManager &checkerMgr) const {
Jordy Rose59cce712011-08-16 21:24:21 +0000171 // Sort checkers for efficient collection.
Fangrui Song55fab262018-09-26 22:16:28 +0000172 llvm::sort(Checkers, checkerNameLT);
Jordy Rose59cce712011-08-16 21:24:21 +0000173
174 // Collect checkers enabled by the options.
Kristof Umanndd9c86e2019-01-26 15:59:21 +0000175 CheckerInfoSet enabledCheckers = getEnabledCheckers();
Jordy Rose59cce712011-08-16 21:24:21 +0000176
177 // Initialize the CheckerManager with all enabled checkers.
Kristof Umannf282d272018-12-15 15:44:05 +0000178 for (const auto *i : enabledCheckers) {
Eugene Zelenko88f40cf2018-04-03 21:31:50 +0000179 checkerMgr.setCurrentCheckName(CheckName(i->FullName));
180 i->Initialize(checkerMgr);
Jordy Rose59cce712011-08-16 21:24:21 +0000181 }
182}
183
Kristof Umanndd9c86e2019-01-26 15:59:21 +0000184void CheckerRegistry::validateCheckerOptions() const {
185 for (const auto &config : AnOpts.Config) {
Gabor Horvathfc4c4d42015-07-09 21:43:45 +0000186 size_t pos = config.getKey().find(':');
187 if (pos == StringRef::npos)
188 continue;
189
190 bool hasChecker = false;
191 StringRef checkerName = config.getKey().substr(0, pos);
Eugene Zelenko88f40cf2018-04-03 21:31:50 +0000192 for (const auto &checker : Checkers) {
Gabor Horvathfc4c4d42015-07-09 21:43:45 +0000193 if (checker.FullName.startswith(checkerName) &&
194 (checker.FullName.size() == pos || checker.FullName[pos] == '.')) {
195 hasChecker = true;
196 break;
197 }
198 }
Eugene Zelenko88f40cf2018-04-03 21:31:50 +0000199 if (!hasChecker)
Kristof Umannb0be2ab2018-12-15 18:11:49 +0000200 Diags.Report(diag::err_unknown_analyzer_checker) << checkerName;
Gabor Horvathfc4c4d42015-07-09 21:43:45 +0000201 }
202}
203
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000204void CheckerRegistry::printHelp(raw_ostream &out,
Jordy Rose59cce712011-08-16 21:24:21 +0000205 size_t maxNameChars) const {
206 // FIXME: Alphabetical sort puts 'experimental' in the middle.
207 // Would it be better to name it '~experimental' or something else
208 // that's ASCIIbetically last?
Fangrui Song55fab262018-09-26 22:16:28 +0000209 llvm::sort(Checkers, checkerNameLT);
Jordy Rose59cce712011-08-16 21:24:21 +0000210
211 // FIXME: Print available packages.
212
213 out << "CHECKERS:\n";
214
215 // Find the maximum option length.
216 size_t optionFieldWidth = 0;
Eugene Zelenko88f40cf2018-04-03 21:31:50 +0000217 for (const auto &i : Checkers) {
Jordy Rose59cce712011-08-16 21:24:21 +0000218 // Limit the amount of padding we are willing to give up for alignment.
219 // Package.Name Description [Hidden]
Eugene Zelenko88f40cf2018-04-03 21:31:50 +0000220 size_t nameLength = i.FullName.size();
Jordy Rose59cce712011-08-16 21:24:21 +0000221 if (nameLength <= maxNameChars)
222 optionFieldWidth = std::max(optionFieldWidth, nameLength);
223 }
224
225 const size_t initialPad = 2;
Eugene Zelenko88f40cf2018-04-03 21:31:50 +0000226 for (const auto &i : Checkers) {
227 out.indent(initialPad) << i.FullName;
Jordy Rose59cce712011-08-16 21:24:21 +0000228
Eugene Zelenko88f40cf2018-04-03 21:31:50 +0000229 int pad = optionFieldWidth - i.FullName.size();
Jordy Rose59cce712011-08-16 21:24:21 +0000230
231 // Break on long option names.
232 if (pad < 0) {
233 out << '\n';
234 pad = optionFieldWidth + initialPad;
235 }
Eugene Zelenko88f40cf2018-04-03 21:31:50 +0000236 out.indent(pad + 2) << i.Desc;
Jordy Rose59cce712011-08-16 21:24:21 +0000237
238 out << '\n';
239 }
240}
Gabor Horvathc4309902016-08-08 13:41:04 +0000241
Kristof Umanndd9c86e2019-01-26 15:59:21 +0000242void CheckerRegistry::printList(raw_ostream &out) const {
Kristof Umannf282d272018-12-15 15:44:05 +0000243 // Sort checkers for efficient collection.
Fangrui Song55fab262018-09-26 22:16:28 +0000244 llvm::sort(Checkers, checkerNameLT);
Gabor Horvathc4309902016-08-08 13:41:04 +0000245
246 // Collect checkers enabled by the options.
Kristof Umanndd9c86e2019-01-26 15:59:21 +0000247 CheckerInfoSet enabledCheckers = getEnabledCheckers();
Gabor Horvathc4309902016-08-08 13:41:04 +0000248
Eugene Zelenko88f40cf2018-04-03 21:31:50 +0000249 for (const auto *i : enabledCheckers)
250 out << i->FullName << '\n';
Gabor Horvathc4309902016-08-08 13:41:04 +0000251}