1 //===- CheckerRegistry.h - Maintains all available checkers -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_CLANG_STATICANALYZER_CORE_CHECKERREGISTRY_H
11 #define LLVM_CLANG_STATICANALYZER_CORE_CHECKERREGISTRY_H
13 #include "clang/Basic/LLVM.h"
14 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
15 #include "llvm/ADT/StringMap.h"
16 #include "llvm/ADT/StringRef.h"
20 // FIXME: move this information to an HTML file in docs/.
21 // At the very least, a checker plugin is a dynamic library that exports
22 // clang_analyzerAPIVersionString. This should be defined as follows:
25 // const char clang_analyzerAPIVersionString[] =
26 // CLANG_ANALYZER_API_VERSION_STRING;
28 // This is used to check whether the current version of the analyzer is known to
29 // be incompatible with a plugin. Plugins with incompatible version strings,
30 // or without a version string at all, will not be loaded.
32 // To add a custom checker to the analyzer, the plugin must also define the
33 // function clang_registerCheckers. For example:
36 // void clang_registerCheckers (CheckerRegistry ®istry) {
37 // registry.addChecker<MainCallChecker>("example.MainCallChecker",
38 // "Disallows calls to functions called main");
41 // The first method argument is the full name of the checker, including its
42 // enclosing package. By convention, the registered name of a checker is the
43 // name of the associated class (the template argument).
44 // The second method argument is a short human-readable description of the
47 // The clang_registerCheckers function may add any number of checkers to the
48 // registry. If any checkers require additional initialization, use the three-
49 // argument form of CheckerRegistry::addChecker.
51 // To load a checker plugin, specify the full path to the dynamic library as
52 // the argument to the -load option in the cc1 frontend. You can then enable
53 // your custom checker using the -analyzer-checker:
55 // clang -cc1 -load </path/to/plugin.dylib> -analyze
56 // -analyzer-checker=<example.MainCallChecker>
58 // For a complete working example, see examples/analyzer-plugin.
60 #ifndef CLANG_ANALYZER_API_VERSION_STRING
61 // FIXME: The Clang version string is not particularly granular;
62 // the analyzer infrastructure can change a lot between releases.
63 // Unfortunately, this string has to be statically embedded in each plugin,
64 // so we can't just use the functions defined in Version.h.
65 #include "clang/Basic/Version.h"
66 #define CLANG_ANALYZER_API_VERSION_STRING CLANG_VERSION_STRING
71 class AnalyzerOptions;
72 class DiagnosticsEngine;
78 /// Manages a set of available checkers for running a static analysis.
79 /// The checkers are organized into packages by full name, where including
80 /// a package will recursively include all subpackages and checkers within it.
81 /// For example, the checker "core.builtin.NoReturnFunctionChecker" will be
82 /// included if initializeManager() is called with an option of "core",
83 /// "core.builtin", or the full name "core.builtin.NoReturnFunctionChecker".
84 class CheckerRegistry {
86 /// Initialization functions perform any necessary setup for a checker.
87 /// They should include a call to CheckerManager::registerChecker.
88 using InitializationFunction = void (*)(CheckerManager &);
91 InitializationFunction Initialize;
95 CheckerInfo(InitializationFunction fn, StringRef name, StringRef desc)
96 : Initialize(fn), FullName(name), Desc(desc) {}
99 using CheckerInfoList = std::vector<CheckerInfo>;
102 template <typename T>
103 static void initializeManager(CheckerManager &mgr) {
104 mgr.registerChecker<T>();
108 /// Adds a checker to the registry. Use this non-templated overload when your
109 /// checker requires custom initialization.
110 void addChecker(InitializationFunction fn, StringRef fullName,
113 /// Adds a checker to the registry. Use this templated overload when your
114 /// checker does not require any custom initialization.
116 void addChecker(StringRef fullName, StringRef desc) {
117 // Avoid MSVC's Compiler Error C2276:
118 // http://msdn.microsoft.com/en-us/library/850cstw1(v=VS.80).aspx
119 addChecker(&CheckerRegistry::initializeManager<T>, fullName, desc);
122 /// Initializes a CheckerManager by calling the initialization functions for
123 /// all checkers specified by the given CheckerOptInfo list. The order of this
124 /// list is significant; later options can be used to reverse earlier ones.
125 /// This can be used to exclude certain checkers in an included package.
126 void initializeManager(CheckerManager &mgr,
127 SmallVectorImpl<CheckerOptInfo> &opts) const;
129 /// Check if every option corresponds to a specific checker or package.
130 void validateCheckerOptions(const AnalyzerOptions &opts,
131 DiagnosticsEngine &diags) const;
133 /// Prints the name and description of all checkers in this registry.
134 /// This output is not intended to be machine-parseable.
135 void printHelp(raw_ostream &out, size_t maxNameChars = 30) const;
136 void printList(raw_ostream &out,
137 SmallVectorImpl<CheckerOptInfo> &opts) const;
140 mutable CheckerInfoList Checkers;
141 mutable llvm::StringMap<size_t> Packages;
148 #endif // LLVM_CLANG_STATICANALYZER_CORE_CHECKERREGISTRY_H