clang 22.0.0git
CheckerRegistry.h
Go to the documentation of this file.
1//===- CheckerRegistry.h - Maintains all available checkers -----*- C++ -*-===//
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//
9// Contains the logic for parsing the TableGen file Checkers.td, and parsing the
10// specific invocation of the analyzer (which checker/package is enabled, values
11// of their options, etc). This is in the frontend library because checker
12// registry functions are called from here but are defined in the dependent
13// library libStaticAnalyzerCheckers, but the actual data structure that holds
14// the parsed information is in the Core library.
15//
16//===----------------------------------------------------------------------===//
17
18#ifndef LLVM_CLANG_STATICANALYZER_FRONTEND_CHECKERREGISTRY_H
19#define LLVM_CLANG_STATICANALYZER_FRONTEND_CHECKERREGISTRY_H
20
21#include "clang/Basic/LLVM.h"
23#include "llvm/ADT/StringRef.h"
24
25// FIXME: move this information to an HTML file in docs/.
26// At the very least, a checker plugin is a dynamic library that exports
27// clang_analyzerAPIVersionString. This should be defined as follows:
28//
29// extern "C"
30// const char clang_analyzerAPIVersionString[] =
31// CLANG_ANALYZER_API_VERSION_STRING;
32//
33// This is used to check whether the current version of the analyzer is known to
34// be incompatible with a plugin. Plugins with incompatible version strings,
35// or without a version string at all, will not be loaded.
36//
37// To add a custom checker to the analyzer, the plugin must also define the
38// function clang_registerCheckers. For example:
39//
40// extern "C"
41// void clang_registerCheckers(CheckerRegistry &Registry) {
42// Registry.addChecker<MainCallChecker>(
43// "example.MainCallChecker",
44// "Disallows calls to functions called main");
45// }
46//
47// The first argument of this templated method is the full name of the checker
48// (including its package), while the second argument is a short description
49// that is printed by `-analyzer-checker-help`.
50//
51// A plugin may register several separate checkers by calling `addChecker()`
52// multiple times. If a checker requires custom registration functions (e.g.
53// checker option handling) use the non-templated overload of `addChecker` that
54// takes two callback functions as the first two parameters.
55//
56// To load a checker plugin, specify the full path to the dynamic library as
57// the argument to the -load option in the cc1 frontend. You can then enable
58// your custom checker using the -analyzer-checker:
59//
60// clang -cc1 -load /path/to/plugin.dylib -analyze
61// -analyzer-checker=example.MainCallChecker
62//
63// For complete examples, see clang/lib/Analysis/plugins/SampleAnalyzer
64
65#ifndef CLANG_ANALYZER_API_VERSION_STRING
66// FIXME: The Clang version string is not particularly granular;
67// the analyzer infrastructure can change a lot between releases.
68// Unfortunately, this string has to be statically embedded in each plugin,
69// so we can't just use the functions defined in Version.h.
70#include "clang/Basic/Version.h"
71#define CLANG_ANALYZER_API_VERSION_STRING CLANG_VERSION_STRING
72#endif
73
74namespace clang {
75
76class AnalyzerOptions;
77class DiagnosticsEngine;
78
79namespace ento {
80
81class CheckerManager;
82
83/// Manages a set of available checkers for running a static analysis.
84/// The checkers are organized into packages by full name, where including
85/// a package will recursively include all subpackages and checkers within it.
86/// For example, the checker "core.builtin.NoReturnFunctionChecker" will be
87/// included if initializeManager() is called with an option of "core",
88/// "core.builtin", or the full name "core.builtin.NoReturnFunctionChecker".
90public:
92 DiagnosticsEngine &Diags, AnalyzerOptions &AnOpts,
94 CheckerRegistrationFns = {});
95
96 /// Collects all enabled checkers in the field EnabledCheckers. It preserves
97 /// the order of insertion, as dependencies have to be enabled before the
98 /// checkers that depend on them.
99 void initializeRegistry(const CheckerManager &Mgr);
100
101
102private:
103 /// Default initialization function for checkers -- since CheckerManager
104 /// includes this header, we need to make it a template parameter, and since
105 /// the checker must be a template parameter as well, we can't put this in the
106 /// cpp file.
107 template <typename MGR, typename T> static void initializeManager(MGR &mgr) {
108 mgr.template registerChecker<T>();
109 }
110
111 static bool returnTrue(const CheckerManager &) { return true; }
112
113public:
114 /// Adds a checker to the registry.
115 /// Use this for a checker defined in a plugin if it requires custom
116 /// registration functions (e.g. for handling checker options).
117 /// NOTE: As of now `DocsUri` is never queried from the checker registry.
119 StringRef FullName, StringRef Desc,
120 StringRef DocsUri = "NoDocsUri", bool IsHidden = false);
121
122 /// Adds a checker to the registry.
123 /// Use this for a checker defined in a plugin if it doesn't require custom
124 /// registration functions.
125 template <class T>
126 void addChecker(StringRef FullName, StringRef Desc,
127 StringRef DocsUri = "NoDocsUri", bool IsHidden = false) {
128 addChecker(&CheckerRegistry::initializeManager<CheckerManager, T>,
129 &CheckerRegistry::returnTrue, FullName, Desc, DocsUri, IsHidden);
130 }
131
132 /// Makes the checker with the full name \p fullName depend on the checker
133 /// called \p dependency.
134 void addDependency(StringRef FullName, StringRef Dependency);
135
136 /// Makes the checker with the full name \p fullName weak depend on the
137 /// checker called \p dependency.
138 void addWeakDependency(StringRef FullName, StringRef Dependency);
139
140 /// Registers an option to a given checker. A checker option will always have
141 /// the following format:
142 /// CheckerFullName:OptionName=Value
143 /// And can be specified from the command line like this:
144 /// -analyzer-config CheckerFullName:OptionName=Value
145 ///
146 /// Options for unknown checkers, or unknown options for a given checker, or
147 /// invalid value types for that given option are reported as an error in
148 /// non-compatibility mode.
149 void addCheckerOption(StringRef OptionType, StringRef CheckerFullName,
150 StringRef OptionName, StringRef DefaultValStr,
151 StringRef Description, StringRef DevelopmentStatus,
152 bool IsHidden = false);
153
154 /// Adds a package to the registry.
155 void addPackage(StringRef FullName);
156
157 /// Registers an option to a given package. A package option will always have
158 /// the following format:
159 /// PackageFullName:OptionName=Value
160 /// And can be specified from the command line like this:
161 /// -analyzer-config PackageFullName:OptionName=Value
162 ///
163 /// Options for unknown packages, or unknown options for a given package, or
164 /// invalid value types for that given option are reported as an error in
165 /// non-compatibility mode.
166 void addPackageOption(StringRef OptionType, StringRef PackageFullName,
167 StringRef OptionName, StringRef DefaultValStr,
168 StringRef Description, StringRef DevelopmentStatus,
169 bool IsHidden = false);
170
171 // FIXME: This *really* should be added to the frontend flag descriptions.
172 /// Initializes a CheckerManager by calling the initialization functions for
173 /// all checkers specified by the given CheckerOptInfo list. The order of this
174 /// list is significant; later options can be used to reverse earlier ones.
175 /// This can be used to exclude certain checkers in an included package.
176 void initializeManager(CheckerManager &CheckerMgr) const;
177
178 /// Check if every option corresponds to a specific checker or package.
179 void validateCheckerOptions() const;
180
181private:
182 template <bool IsWeak> void resolveDependencies();
183 void resolveCheckerAndPackageOptions();
184
186
187 DiagnosticsEngine &Diags;
188 AnalyzerOptions &AnOpts;
189};
190
191} // namespace ento
192} // namespace clang
193
194#endif // LLVM_CLANG_STATICANALYZER_FRONTEND_CHECKERREGISTRY_H
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
int32_t FullName
Definition: SemaARM.cpp:1180
const char * Data
Defines version macros and version-related utility functions for Clang.
Stores options for the analyzer from the command line.
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:231
Manages a set of available checkers for running a static analysis.
void addCheckerOption(StringRef OptionType, StringRef CheckerFullName, StringRef OptionName, StringRef DefaultValStr, StringRef Description, StringRef DevelopmentStatus, bool IsHidden=false)
Registers an option to a given checker.
void addWeakDependency(StringRef FullName, StringRef Dependency)
Makes the checker with the full name fullName weak depend on the checker called dependency.
void addPackageOption(StringRef OptionType, StringRef PackageFullName, StringRef OptionName, StringRef DefaultValStr, StringRef Description, StringRef DevelopmentStatus, bool IsHidden=false)
Registers an option to a given package.
void initializeRegistry(const CheckerManager &Mgr)
Collects all enabled checkers in the field EnabledCheckers.
void addChecker(RegisterCheckerFn Fn, ShouldRegisterFunction Sfn, StringRef FullName, StringRef Desc, StringRef DocsUri="NoDocsUri", bool IsHidden=false)
Adds a checker to the registry.
void addPackage(StringRef FullName)
Adds a package to the registry.
void validateCheckerOptions() const
Check if every option corresponds to a specific checker or package.
void addDependency(StringRef FullName, StringRef Dependency)
Makes the checker with the full name fullName depend on the checker called dependency.
void addChecker(StringRef FullName, StringRef Desc, StringRef DocsUri="NoDocsUri", bool IsHidden=false)
Adds a checker to the registry.
bool(*)(const CheckerManager &) ShouldRegisterFunction
void(*)(CheckerManager &) RegisterCheckerFn
Initialization functions perform any necessary setup for a checker.
The JSON file list parser is used to communicate input to InstallAPI.
int const char * function
Definition: c++config.h:31