CheckerRegistry.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- CheckerRegistry.h - Maintains all available checkers -----*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // Contains the logic for parsing the TableGen file Checkers.td, and parsing the
  15. // specific invocation of the analyzer (which checker/package is enabled, values
  16. // of their options, etc). This is in the frontend library because checker
  17. // registry functions are called from here but are defined in the dependent
  18. // library libStaticAnalyzerCheckers, but the actual data structure that holds
  19. // the parsed information is in the Core library.
  20. //
  21. //===----------------------------------------------------------------------===//
  22. #ifndef LLVM_CLANG_STATICANALYZER_FRONTEND_CHECKERREGISTRY_H
  23. #define LLVM_CLANG_STATICANALYZER_FRONTEND_CHECKERREGISTRY_H
  24. #include "clang/Basic/LLVM.h"
  25. #include "clang/StaticAnalyzer/Core/CheckerRegistryData.h"
  26. #include "llvm/ADT/StringRef.h"
  27. // FIXME: move this information to an HTML file in docs/.
  28. // At the very least, a checker plugin is a dynamic library that exports
  29. // clang_analyzerAPIVersionString. This should be defined as follows:
  30. //
  31. // extern "C"
  32. // const char clang_analyzerAPIVersionString[] =
  33. // CLANG_ANALYZER_API_VERSION_STRING;
  34. //
  35. // This is used to check whether the current version of the analyzer is known to
  36. // be incompatible with a plugin. Plugins with incompatible version strings,
  37. // or without a version string at all, will not be loaded.
  38. //
  39. // To add a custom checker to the analyzer, the plugin must also define the
  40. // function clang_registerCheckers. For example:
  41. //
  42. // extern "C"
  43. // void clang_registerCheckers (CheckerRegistry &registry) {
  44. // registry.addChecker<MainCallChecker>("example.MainCallChecker",
  45. // "Disallows calls to functions called main");
  46. // }
  47. //
  48. // The first method argument is the full name of the checker, including its
  49. // enclosing package. By convention, the registered name of a checker is the
  50. // name of the associated class (the template argument).
  51. // The second method argument is a short human-readable description of the
  52. // checker.
  53. //
  54. // The clang_registerCheckers function may add any number of checkers to the
  55. // registry. If any checkers require additional initialization, use the three-
  56. // argument form of CheckerRegistry::addChecker.
  57. //
  58. // To load a checker plugin, specify the full path to the dynamic library as
  59. // the argument to the -load option in the cc1 frontend. You can then enable
  60. // your custom checker using the -analyzer-checker:
  61. //
  62. // clang -cc1 -load </path/to/plugin.dylib> -analyze
  63. // -analyzer-checker=<example.MainCallChecker>
  64. //
  65. // For a complete working example, see examples/analyzer-plugin.
  66. #ifndef CLANG_ANALYZER_API_VERSION_STRING
  67. // FIXME: The Clang version string is not particularly granular;
  68. // the analyzer infrastructure can change a lot between releases.
  69. // Unfortunately, this string has to be statically embedded in each plugin,
  70. // so we can't just use the functions defined in Version.h.
  71. #include "clang/Basic/Version.h"
  72. #define CLANG_ANALYZER_API_VERSION_STRING CLANG_VERSION_STRING
  73. #endif
  74. namespace clang {
  75. class AnalyzerOptions;
  76. class DiagnosticsEngine;
  77. namespace ento {
  78. class CheckerManager;
  79. /// Manages a set of available checkers for running a static analysis.
  80. /// The checkers are organized into packages by full name, where including
  81. /// a package will recursively include all subpackages and checkers within it.
  82. /// For example, the checker "core.builtin.NoReturnFunctionChecker" will be
  83. /// included if initializeManager() is called with an option of "core",
  84. /// "core.builtin", or the full name "core.builtin.NoReturnFunctionChecker".
  85. class CheckerRegistry {
  86. public:
  87. CheckerRegistry(CheckerRegistryData &Data, ArrayRef<std::string> Plugins,
  88. DiagnosticsEngine &Diags, AnalyzerOptions &AnOpts,
  89. ArrayRef<std::function<void(CheckerRegistry &)>>
  90. CheckerRegistrationFns = {});
  91. /// Collects all enabled checkers in the field EnabledCheckers. It preserves
  92. /// the order of insertion, as dependencies have to be enabled before the
  93. /// checkers that depend on them.
  94. void initializeRegistry(const CheckerManager &Mgr);
  95. private:
  96. /// Default initialization function for checkers -- since CheckerManager
  97. /// includes this header, we need to make it a template parameter, and since
  98. /// the checker must be a template parameter as well, we can't put this in the
  99. /// cpp file.
  100. template <typename MGR, typename T> static void initializeManager(MGR &mgr) {
  101. mgr.template registerChecker<T>();
  102. }
  103. template <typename T> static bool returnTrue(const CheckerManager &mgr) {
  104. return true;
  105. }
  106. public:
  107. /// Adds a checker to the registry. Use this non-templated overload when your
  108. /// checker requires custom initialization.
  109. void addChecker(RegisterCheckerFn Fn, ShouldRegisterFunction sfn,
  110. StringRef FullName, StringRef Desc, StringRef DocsUri,
  111. bool IsHidden);
  112. /// Adds a checker to the registry. Use this templated overload when your
  113. /// checker does not require any custom initialization.
  114. /// This function isn't really needed and probably causes more headaches than
  115. /// the tiny convenience that it provides, but external plugins might use it,
  116. /// and there isn't a strong incentive to remove it.
  117. template <class T>
  118. void addChecker(StringRef FullName, StringRef Desc, StringRef DocsUri,
  119. bool IsHidden = false) {
  120. // Avoid MSVC's Compiler Error C2276:
  121. // http://msdn.microsoft.com/en-us/library/850cstw1(v=VS.80).aspx
  122. addChecker(&CheckerRegistry::initializeManager<CheckerManager, T>,
  123. &CheckerRegistry::returnTrue<T>, FullName, Desc, DocsUri,
  124. IsHidden);
  125. }
  126. /// Makes the checker with the full name \p fullName depend on the checker
  127. /// called \p dependency.
  128. void addDependency(StringRef FullName, StringRef Dependency);
  129. /// Makes the checker with the full name \p fullName weak depend on the
  130. /// checker called \p dependency.
  131. void addWeakDependency(StringRef FullName, StringRef Dependency);
  132. /// Registers an option to a given checker. A checker option will always have
  133. /// the following format:
  134. /// CheckerFullName:OptionName=Value
  135. /// And can be specified from the command line like this:
  136. /// -analyzer-config CheckerFullName:OptionName=Value
  137. ///
  138. /// Options for unknown checkers, or unknown options for a given checker, or
  139. /// invalid value types for that given option are reported as an error in
  140. /// non-compatibility mode.
  141. void addCheckerOption(StringRef OptionType, StringRef CheckerFullName,
  142. StringRef OptionName, StringRef DefaultValStr,
  143. StringRef Description, StringRef DevelopmentStatus,
  144. bool IsHidden = false);
  145. /// Adds a package to the registry.
  146. void addPackage(StringRef FullName);
  147. /// Registers an option to a given package. A package option will always have
  148. /// the following format:
  149. /// PackageFullName:OptionName=Value
  150. /// And can be specified from the command line like this:
  151. /// -analyzer-config PackageFullName:OptionName=Value
  152. ///
  153. /// Options for unknown packages, or unknown options for a given package, or
  154. /// invalid value types for that given option are reported as an error in
  155. /// non-compatibility mode.
  156. void addPackageOption(StringRef OptionType, StringRef PackageFullName,
  157. StringRef OptionName, StringRef DefaultValStr,
  158. StringRef Description, StringRef DevelopmentStatus,
  159. bool IsHidden = false);
  160. // FIXME: This *really* should be added to the frontend flag descriptions.
  161. /// Initializes a CheckerManager by calling the initialization functions for
  162. /// all checkers specified by the given CheckerOptInfo list. The order of this
  163. /// list is significant; later options can be used to reverse earlier ones.
  164. /// This can be used to exclude certain checkers in an included package.
  165. void initializeManager(CheckerManager &CheckerMgr) const;
  166. /// Check if every option corresponds to a specific checker or package.
  167. void validateCheckerOptions() const;
  168. private:
  169. template <bool IsWeak> void resolveDependencies();
  170. void resolveCheckerAndPackageOptions();
  171. CheckerRegistryData &Data;
  172. DiagnosticsEngine &Diags;
  173. AnalyzerOptions &AnOpts;
  174. };
  175. } // namespace ento
  176. } // namespace clang
  177. #endif // LLVM_CLANG_STATICANALYZER_FRONTEND_CHECKERREGISTRY_H
  178. #ifdef __GNUC__
  179. #pragma GCC diagnostic pop
  180. #endif