clang  9.0.0svn
CheckerRegistry.cpp
Go to the documentation of this file.
1 //===- CheckerRegistry.cpp - Maintains all available checkers -------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "clang/Basic/Diagnostic.h"
11 #include "clang/Basic/LLVM.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SetVector.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/Support/DynamicLibrary.h"
22 #include "llvm/Support/Path.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include <algorithm>
25 
26 using namespace clang;
27 using namespace ento;
28 using llvm::sys::DynamicLibrary;
29 
30 using RegisterCheckersFn = void (*)(CheckerRegistry &);
31 
32 static bool isCompatibleAPIVersion(const char *VersionString) {
33  // If the version string is null, its not an analyzer plugin.
34  if (!VersionString)
35  return false;
36 
37  // For now, none of the static analyzer API is considered stable.
38  // Versions must match exactly.
39  return strcmp(VersionString, CLANG_ANALYZER_API_VERSION_STRING) == 0;
40 }
41 
42 namespace {
43 template <class T> struct FullNameLT {
44  bool operator()(const T &Lhs, const T &Rhs) {
45  return Lhs.FullName < Rhs.FullName;
46  }
47 };
48 
49 using PackageNameLT = FullNameLT<CheckerRegistry::PackageInfo>;
50 using CheckerNameLT = FullNameLT<CheckerRegistry::CheckerInfo>;
51 } // end of anonymous namespace
52 
53 template <class CheckerOrPackageInfoList>
54 static
55  typename std::conditional<std::is_const<CheckerOrPackageInfoList>::value,
56  typename CheckerOrPackageInfoList::const_iterator,
57  typename CheckerOrPackageInfoList::iterator>::type
58  binaryFind(CheckerOrPackageInfoList &Collection, StringRef FullName) {
59 
60  using CheckerOrPackage = typename CheckerOrPackageInfoList::value_type;
61  using CheckerOrPackageFullNameLT = FullNameLT<CheckerOrPackage>;
62 
63  assert(std::is_sorted(Collection.begin(), Collection.end(),
64  CheckerOrPackageFullNameLT{}) &&
65  "In order to efficiently gather checkers/packages, this function "
66  "expects them to be already sorted!");
67 
68  return llvm::lower_bound(Collection, CheckerOrPackage(FullName),
69  CheckerOrPackageFullNameLT{});
70 }
71 
72 static constexpr char PackageSeparator = '.';
73 
74 static bool isInPackage(const CheckerRegistry::CheckerInfo &Checker,
75  StringRef PackageName) {
76  // Does the checker's full name have the package as a prefix?
77  if (!Checker.FullName.startswith(PackageName))
78  return false;
79 
80  // Is the package actually just the name of a specific checker?
81  if (Checker.FullName.size() == PackageName.size())
82  return true;
83 
84  // Is the checker in the package (or a subpackage)?
85  if (Checker.FullName[PackageName.size()] == PackageSeparator)
86  return true;
87 
88  return false;
89 }
90 
92 CheckerRegistry::getMutableCheckersForCmdLineArg(StringRef CmdLineArg) {
93  auto It = binaryFind(Checkers, CmdLineArg);
94 
95  if (!isInPackage(*It, CmdLineArg))
96  return {Checkers.end(), Checkers.end()};
97 
98  // See how large the package is.
99  // If the package doesn't exist, assume the option refers to a single
100  // checker.
101  size_t Size = 1;
102  llvm::StringMap<size_t>::const_iterator PackageSize =
103  PackageSizes.find(CmdLineArg);
104 
105  if (PackageSize != PackageSizes.end())
106  Size = PackageSize->getValue();
107 
108  return {It, It + Size};
109 }
110 
113  AnalyzerOptions &AnOpts, const LangOptions &LangOpts,
114  ArrayRef<std::function<void(CheckerRegistry &)>> CheckerRegistrationFns)
115  : Diags(Diags), AnOpts(AnOpts), LangOpts(LangOpts) {
116 
117  // Register builtin checkers.
118 #define GET_CHECKERS
119 #define CHECKER(FULLNAME, CLASS, HELPTEXT, DOC_URI, IS_HIDDEN) \
120  addChecker(register##CLASS, shouldRegister##CLASS, FULLNAME, HELPTEXT, \
121  DOC_URI, IS_HIDDEN);
122 
123 #define GET_PACKAGES
124 #define PACKAGE(FULLNAME) addPackage(FULLNAME);
125 
126 #include "clang/StaticAnalyzer/Checkers/Checkers.inc"
127 #undef CHECKER
128 #undef GET_CHECKERS
129 #undef PACKAGE
130 #undef GET_PACKAGES
131 
132  // Register checkers from plugins.
133  for (const std::string &Plugin : Plugins) {
134  // Get access to the plugin.
135  std::string ErrorMsg;
136  DynamicLibrary Lib =
137  DynamicLibrary::getPermanentLibrary(Plugin.c_str(), &ErrorMsg);
138  if (!Lib.isValid()) {
139  Diags.Report(diag::err_fe_unable_to_load_plugin) << Plugin << ErrorMsg;
140  continue;
141  }
142 
143  // See if its compatible with this build of clang.
144  const char *PluginAPIVersion = static_cast<const char *>(
145  Lib.getAddressOfSymbol("clang_analyzerAPIVersionString"));
146 
147  if (!isCompatibleAPIVersion(PluginAPIVersion)) {
148  Diags.Report(diag::warn_incompatible_analyzer_plugin_api)
149  << llvm::sys::path::filename(Plugin);
150  Diags.Report(diag::note_incompatible_analyzer_plugin_api)
151  << CLANG_ANALYZER_API_VERSION_STRING << PluginAPIVersion;
152  continue;
153  }
154 
155  // Register its checkers.
156  RegisterCheckersFn RegisterPluginCheckers =
157  reinterpret_cast<RegisterCheckersFn>(
158  Lib.getAddressOfSymbol("clang_registerCheckers"));
159  if (RegisterPluginCheckers)
160  RegisterPluginCheckers(*this);
161  }
162 
163  // Register statically linked checkers, that aren't generated from the tblgen
164  // file, but rather passed their registry function as a parameter in
165  // checkerRegistrationFns.
166 
167  for (const auto &Fn : CheckerRegistrationFns)
168  Fn(*this);
169 
170  // Sort checkers for efficient collection.
171  // FIXME: Alphabetical sort puts 'experimental' in the middle.
172  // Would it be better to name it '~experimental' or something else
173  // that's ASCIIbetically last?
174  llvm::sort(Packages, PackageNameLT{});
175  llvm::sort(Checkers, CheckerNameLT{});
176 
177 #define GET_CHECKER_DEPENDENCIES
178 
179 #define CHECKER_DEPENDENCY(FULLNAME, DEPENDENCY) \
180  addDependency(FULLNAME, DEPENDENCY);
181 
182 #define GET_CHECKER_OPTIONS
183 #define CHECKER_OPTION(TYPE, FULLNAME, CMDFLAG, DESC, DEFAULT_VAL) \
184  addCheckerOption(TYPE, FULLNAME, CMDFLAG, DEFAULT_VAL, DESC);
185 
186 #define GET_PACKAGE_OPTIONS
187 #define PACKAGE_OPTION(TYPE, FULLNAME, CMDFLAG, DESC, DEFAULT_VAL) \
188  addPackageOption(TYPE, FULLNAME, CMDFLAG, DEFAULT_VAL, DESC);
189 
190 #include "clang/StaticAnalyzer/Checkers/Checkers.inc"
191 #undef CHECKER_DEPENDENCY
192 #undef GET_CHECKER_DEPENDENCIES
193 #undef CHECKER_OPTION
194 #undef GET_CHECKER_OPTIONS
195 #undef PACKAGE_OPTION
196 #undef GET_PACKAGE_OPTIONS
197 
198  resolveDependencies();
199  resolveCheckerAndPackageOptions();
200 
201  // Parse '-analyzer-checker' and '-analyzer-disable-checker' options from the
202  // command line.
203  for (const std::pair<std::string, bool> &Opt : AnOpts.CheckersControlList) {
204  CheckerInfoListRange CheckerForCmdLineArg =
205  getMutableCheckersForCmdLineArg(Opt.first);
206 
207  if (CheckerForCmdLineArg.begin() == CheckerForCmdLineArg.end()) {
208  Diags.Report(diag::err_unknown_analyzer_checker) << Opt.first;
209  Diags.Report(diag::note_suggest_disabling_all_checkers);
210  }
211 
212  for (CheckerInfo &checker : CheckerForCmdLineArg) {
213  checker.State = Opt.second ? StateFromCmdLine::State_Enabled
215  }
216  }
217 }
218 
219 /// Collects dependencies in \p ret, returns false on failure.
220 static bool
222  const LangOptions &LO,
224 
225 /// Collects dependenies in \p enabledCheckers. Return None on failure.
226 LLVM_NODISCARD
228 collectDependencies(const CheckerRegistry::CheckerInfo &checker,
229  const LangOptions &LO) {
230 
232  // Add dependencies to the enabled checkers only if all of them can be
233  // enabled.
234  if (!collectDependenciesImpl(checker.Dependencies, LO, Ret))
235  return None;
236 
237  return Ret;
238 }
239 
240 static bool
242  const LangOptions &LO,
244 
245  for (const CheckerRegistry::CheckerInfo *Dependency : Deps) {
246 
247  if (Dependency->isDisabled(LO))
248  return false;
249 
250  // Collect dependencies recursively.
251  if (!collectDependenciesImpl(Dependency->Dependencies, LO, Ret))
252  return false;
253 
254  Ret.insert(Dependency);
255  }
256 
257  return true;
258 }
259 
260 CheckerRegistry::CheckerInfoSet CheckerRegistry::getEnabledCheckers() const {
261 
262  CheckerInfoSet EnabledCheckers;
263 
264  for (const CheckerInfo &Checker : Checkers) {
265  if (!Checker.isEnabled(LangOpts))
266  continue;
267 
268  // Recursively enable its dependencies.
270  collectDependencies(Checker, LangOpts);
271 
272  if (!Deps) {
273  // If we failed to enable any of the dependencies, don't enable this
274  // checker.
275  continue;
276  }
277 
278  // Note that set_union also preserves the order of insertion.
279  EnabledCheckers.set_union(*Deps);
280 
281  // Enable the checker.
282  EnabledCheckers.insert(&Checker);
283  }
284 
285  return EnabledCheckers;
286 }
287 
288 void CheckerRegistry::resolveDependencies() {
289  for (const std::pair<StringRef, StringRef> &Entry : Dependencies) {
290  auto CheckerIt = binaryFind(Checkers, Entry.first);
291  assert(CheckerIt != Checkers.end() && CheckerIt->FullName == Entry.first &&
292  "Failed to find the checker while attempting to set up its "
293  "dependencies!");
294 
295  auto DependencyIt = binaryFind(Checkers, Entry.second);
296  assert(DependencyIt != Checkers.end() &&
297  DependencyIt->FullName == Entry.second &&
298  "Failed to find the dependency of a checker!");
299 
300  CheckerIt->Dependencies.emplace_back(&*DependencyIt);
301  }
302 
303  Dependencies.clear();
304 }
305 
306 void CheckerRegistry::addDependency(StringRef FullName, StringRef Dependency) {
307  Dependencies.emplace_back(FullName, Dependency);
308 }
309 
310 /// Insert the checker/package option to AnalyzerOptions' config table, and
311 /// validate it, if the user supplied it on the command line.
312 static void insertAndValidate(StringRef FullName,
313  const CheckerRegistry::CmdLineOption &Option,
314  AnalyzerOptions &AnOpts,
315  DiagnosticsEngine &Diags) {
316 
317  std::string FullOption = (FullName + ":" + Option.OptionName).str();
318 
319  auto It = AnOpts.Config.insert({FullOption, Option.DefaultValStr});
320 
321  // Insertation was successful -- CmdLineOption's constructor will validate
322  // whether values received from plugins or TableGen files are correct.
323  if (It.second)
324  return;
325 
326  // Insertion failed, the user supplied this package/checker option on the
327  // command line. If the supplied value is invalid, we'll restore the option
328  // to it's default value, and if we're in non-compatibility mode, we'll also
329  // emit an error.
330 
331  StringRef SuppliedValue = It.first->getValue();
332 
333  if (Option.OptionType == "bool") {
334  if (SuppliedValue != "true" && SuppliedValue != "false") {
336  Diags.Report(diag::err_analyzer_checker_option_invalid_input)
337  << FullOption << "a boolean value";
338  }
339 
340  It.first->setValue(Option.DefaultValStr);
341  }
342  return;
343  }
344 
345  if (Option.OptionType == "int") {
346  int Tmp;
347  bool HasFailed = SuppliedValue.getAsInteger(0, Tmp);
348  if (HasFailed) {
350  Diags.Report(diag::err_analyzer_checker_option_invalid_input)
351  << FullOption << "an integer value";
352  }
353 
354  It.first->setValue(Option.DefaultValStr);
355  }
356  return;
357  }
358 }
359 
360 template <class T>
361 static void
362 insertOptionToCollection(StringRef FullName, T &Collection,
363  const CheckerRegistry::CmdLineOption &Option,
364  AnalyzerOptions &AnOpts, DiagnosticsEngine &Diags) {
365  auto It = binaryFind(Collection, FullName);
366  assert(It != Collection.end() &&
367  "Failed to find the checker while attempting to add a command line "
368  "option to it!");
369 
370  insertAndValidate(FullName, Option, AnOpts, Diags);
371 
372  It->CmdLineOptions.emplace_back(Option);
373 }
374 
375 void CheckerRegistry::resolveCheckerAndPackageOptions() {
376  for (const std::pair<StringRef, CmdLineOption> &CheckerOptEntry :
377  CheckerOptions) {
378  insertOptionToCollection(CheckerOptEntry.first, Checkers,
379  CheckerOptEntry.second, AnOpts, Diags);
380  }
381  CheckerOptions.clear();
382 
383  for (const std::pair<StringRef, CmdLineOption> &PackageOptEntry :
384  PackageOptions) {
385  insertOptionToCollection(PackageOptEntry.first, Packages,
386  PackageOptEntry.second, AnOpts, Diags);
387  }
388  PackageOptions.clear();
389 }
390 
391 void CheckerRegistry::addPackage(StringRef FullName) {
392  Packages.emplace_back(PackageInfo(FullName));
393 }
394 
395 void CheckerRegistry::addPackageOption(StringRef OptionType,
396  StringRef PackageFullName,
397  StringRef OptionName,
398  StringRef DefaultValStr,
399  StringRef Description) {
400  PackageOptions.emplace_back(
401  PackageFullName,
402  CmdLineOption{OptionType, OptionName, DefaultValStr, Description});
403 }
404 
406  ShouldRegisterFunction Sfn, StringRef Name,
407  StringRef Desc, StringRef DocsUri,
408  bool IsHidden) {
409  Checkers.emplace_back(Rfn, Sfn, Name, Desc, DocsUri, IsHidden);
410 
411  // Record the presence of the checker in its packages.
412  StringRef PackageName, LeafName;
413  std::tie(PackageName, LeafName) = Name.rsplit(PackageSeparator);
414  while (!LeafName.empty()) {
415  PackageSizes[PackageName] += 1;
416  std::tie(PackageName, LeafName) = PackageName.rsplit(PackageSeparator);
417  }
418 }
419 
420 void CheckerRegistry::addCheckerOption(StringRef OptionType,
421  StringRef CheckerFullName,
422  StringRef OptionName,
423  StringRef DefaultValStr,
424  StringRef Description) {
425  CheckerOptions.emplace_back(
426  CheckerFullName,
427  CmdLineOption{OptionType, OptionName, DefaultValStr, Description});
428 }
429 
430 void CheckerRegistry::initializeManager(CheckerManager &CheckerMgr) const {
431  // Collect checkers enabled by the options.
432  CheckerInfoSet enabledCheckers = getEnabledCheckers();
433 
434  // Initialize the CheckerManager with all enabled checkers.
435  for (const auto *Checker : enabledCheckers) {
436  CheckerMgr.setCurrentCheckName(CheckName(Checker->FullName));
437  Checker->Initialize(CheckerMgr);
438  }
439 }
440 
441 static void
443  StringRef SuppliedChecker, StringRef SuppliedOption,
444  const AnalyzerOptions &AnOpts, DiagnosticsEngine &Diags) {
445 
447  return;
448 
449  using CmdLineOption = CheckerRegistry::CmdLineOption;
450 
451  auto SameOptName = [SuppliedOption](const CmdLineOption &Opt) {
452  return Opt.OptionName == SuppliedOption;
453  };
454 
455  auto OptionIt = llvm::find_if(OptionList, SameOptName);
456 
457  if (OptionIt == OptionList.end()) {
458  Diags.Report(diag::err_analyzer_checker_option_unknown)
459  << SuppliedChecker << SuppliedOption;
460  return;
461  }
462 }
463 
465  for (const auto &Config : AnOpts.Config) {
466 
467  StringRef SuppliedChecker;
468  StringRef SuppliedOption;
469  std::tie(SuppliedChecker, SuppliedOption) = Config.getKey().split(':');
470 
471  if (SuppliedOption.empty())
472  continue;
473 
474  // AnalyzerOptions' config table contains the user input, so an entry could
475  // look like this:
476  //
477  // cor:NoFalsePositives=true
478  //
479  // Since lower_bound would look for the first element *not less* than "cor",
480  // it would return with an iterator to the first checker in the core, so we
481  // we really have to use find here, which uses operator==.
482  auto CheckerIt = llvm::find(Checkers, CheckerInfo(SuppliedChecker));
483  if (CheckerIt != Checkers.end()) {
484  isOptionContainedIn(CheckerIt->CmdLineOptions, SuppliedChecker,
485  SuppliedOption, AnOpts, Diags);
486  continue;
487  }
488 
489  auto PackageIt = llvm::find(Packages, PackageInfo(SuppliedChecker));
490  if (PackageIt != Packages.end()) {
491  isOptionContainedIn(PackageIt->CmdLineOptions, SuppliedChecker,
492  SuppliedOption, AnOpts, Diags);
493  continue;
494  }
495 
496  Diags.Report(diag::err_unknown_analyzer_checker) << SuppliedChecker;
497  }
498 }
499 
501  size_t MaxNameChars) const {
502  // FIXME: Print available packages.
503 
504  Out << "CHECKERS:\n";
505 
506  // Find the maximum option length.
507  size_t OptionFieldWidth = 0;
508  for (const auto &Checker : Checkers) {
509  // Limit the amount of padding we are willing to give up for alignment.
510  // Package.Name Description [Hidden]
511  size_t NameLength = Checker.FullName.size();
512  if (NameLength <= MaxNameChars)
513  OptionFieldWidth = std::max(OptionFieldWidth, NameLength);
514  }
515 
516  const size_t InitialPad = 2;
517  for (const auto &Checker : Checkers) {
518  if (!AnOpts.ShowCheckerHelpHidden && Checker.IsHidden)
519  continue;
520 
521  Out.indent(InitialPad) << Checker.FullName;
522 
523  int Pad = OptionFieldWidth - Checker.FullName.size();
524 
525  // Break on long option names.
526  if (Pad < 0) {
527  Out << '\n';
528  Pad = OptionFieldWidth + InitialPad;
529  }
530  Out.indent(Pad + 2) << Checker.Desc;
531 
532  Out << '\n';
533  }
534 }
535 
536 void CheckerRegistry::printEnabledCheckerList(raw_ostream &Out) const {
537  // Collect checkers enabled by the options.
538  CheckerInfoSet EnabledCheckers = getEnabledCheckers();
539 
540  for (const auto *i : EnabledCheckers)
541  Out << i->FullName << '\n';
542 }
Manages a set of available checkers for running a static analysis.
void addCheckerOption(StringRef OptionType, StringRef CheckerFullName, StringRef OptionName, StringRef DefaultValStr, StringRef Description)
Registers an option to a given checker.
void addChecker(InitializationFunction Fn, ShouldRegisterFunction sfn, StringRef FullName, StringRef Desc, StringRef DocsUri, bool IsHidden)
Adds a checker to the registry.
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1296
void printCheckerWithDescList(raw_ostream &Out, size_t MaxNameChars=30) const
Prints the name and description of all checkers in this registry.
void printEnabledCheckerList(raw_ostream &Out) const
#define CLANG_ANALYZER_API_VERSION_STRING
static bool isCompatibleAPIVersion(const char *VersionString)
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:49
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
void setCurrentCheckName(CheckName name)
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:148
void addPackage(StringRef FullName)
Adds a package to the registry.
Defines the Diagnostic-related interfaces.
std::vector< std::pair< std::string, bool > > CheckersControlList
Pair of checker name and enable/disable.
void(*)(CheckerRegistry &) RegisterCheckersFn
static void insertAndValidate(StringRef FullName, const CheckerRegistry::CmdLineOption &Option, AnalyzerOptions &AnOpts, DiagnosticsEngine &Diags)
Insert the checker/package option to AnalyzerOptions&#39; config table, and validate it, if the user supplied it on the command line.
return Out str()
static void insertOptionToCollection(StringRef FullName, T &Collection, const CheckerRegistry::CmdLineOption &Option, AnalyzerOptions &AnOpts, DiagnosticsEngine &Diags)
static LLVM_NODISCARD llvm::Optional< CheckerRegistry::CheckerInfoSet > collectDependencies(const CheckerRegistry::CheckerInfo &checker, const LangOptions &LO)
Collects dependenies in enabledCheckers. Return None on failure.
static bool isInPackage(const CheckerRegistry::CheckerInfo &Checker, StringRef PackageName)
static void isOptionContainedIn(const CheckerRegistry::CmdLineOptionList &OptionList, StringRef SuppliedChecker, StringRef SuppliedOption, const AnalyzerOptions &AnOpts, DiagnosticsEngine &Diags)
llvm::SmallVector< const CheckerInfo *, 0 > ConstCheckerInfoList
bool(*)(const LangOptions &) ShouldRegisterFunction
ConfigTable Config
A key-value table of use-specified configuration values.
llvm::SmallVector< CmdLineOption, 0 > CmdLineOptionList
llvm::SetVector< const CheckerInfo * > CheckerInfoSet
void addPackageOption(StringRef OptionType, StringRef PackageFullName, StringRef OptionName, StringRef DefaultValStr, StringRef Description)
Registers an option to a given package.
Dataflow Directional Tag Classes.
llvm::iterator_range< CheckerInfoList::iterator > CheckerInfoListRange
void addDependency(StringRef FullName, StringRef Dependency)
Makes the checker with the full name fullName depends on the checker called dependency.
static bool collectDependenciesImpl(const CheckerRegistry::ConstCheckerInfoList &Deps, const LangOptions &LO, CheckerRegistry::CheckerInfoSet &Ret)
Collects dependencies in ret, returns false on failure.
void(*)(CheckerManager &) InitializationFunction
Initialization functions perform any necessary setup for a checker.
static std::conditional< std::is_const< CheckerOrPackageInfoList >::value, typename CheckerOrPackageInfoList::const_iterator, typename CheckerOrPackageInfoList::iterator >::type binaryFind(CheckerOrPackageInfoList &Collection, StringRef FullName)
CheckerRegistry(ArrayRef< std::string > plugins, DiagnosticsEngine &diags, AnalyzerOptions &AnOpts, const LangOptions &LangOpts, ArrayRef< std::function< void(CheckerRegistry &)>> checkerRegistrationFns={})
Stores options for the analyzer from the command line.
Specifies a command line option.
void validateCheckerOptions() const
Check if every option corresponds to a specific checker or package.
__DEVICE__ int max(int __a, int __b)
unsigned ShouldEmitErrorsOnInvalidConfigValue
static constexpr char PackageSeparator