]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/StaticAnalyzer/Core/CheckerRegistry.h
MFC r345703:
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / include / clang / StaticAnalyzer / Core / CheckerRegistry.h
1 //===- CheckerRegistry.h - Maintains all available checkers -----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_CLANG_STATICANALYZER_CORE_CHECKERREGISTRY_H
11 #define LLVM_CLANG_STATICANALYZER_CORE_CHECKERREGISTRY_H
12
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"
17 #include <cstddef>
18 #include <vector>
19
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:
23 //
24 //   extern "C"
25 //   const char clang_analyzerAPIVersionString[] =
26 //     CLANG_ANALYZER_API_VERSION_STRING;
27 //
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.
31 //
32 // To add a custom checker to the analyzer, the plugin must also define the
33 // function clang_registerCheckers. For example:
34 //
35 //    extern "C"
36 //    void clang_registerCheckers (CheckerRegistry &registry) {
37 //      registry.addChecker<MainCallChecker>("example.MainCallChecker",
38 //        "Disallows calls to functions called main");
39 //    }
40 //
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
45 // checker.
46 //
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.
50 //
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:
54 //
55 //   clang -cc1 -load </path/to/plugin.dylib> -analyze
56 //     -analyzer-checker=<example.MainCallChecker>
57 //
58 // For a complete working example, see examples/analyzer-plugin.
59
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
67 #endif
68
69 namespace clang {
70
71 class AnalyzerOptions;
72 class DiagnosticsEngine;
73
74 namespace ento {
75
76 class CheckerOptInfo;
77
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 {
85 public:
86   /// Initialization functions perform any necessary setup for a checker.
87   /// They should include a call to CheckerManager::registerChecker.
88   using InitializationFunction = void (*)(CheckerManager &);
89
90   struct CheckerInfo {
91     InitializationFunction Initialize;
92     StringRef FullName;
93     StringRef Desc;
94
95     CheckerInfo(InitializationFunction fn, StringRef name, StringRef desc)
96         : Initialize(fn), FullName(name), Desc(desc) {}
97   };
98
99   using CheckerInfoList = std::vector<CheckerInfo>;
100
101 private:
102   template <typename T>
103   static void initializeManager(CheckerManager &mgr) {
104     mgr.registerChecker<T>();
105   }
106
107 public:
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,
111                   StringRef desc);
112
113   /// Adds a checker to the registry. Use this templated overload when your
114   /// checker does not require any custom initialization.
115   template <class T>
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);
120   }
121
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;
128
129   /// Check if every option corresponds to a specific checker or package.
130   void validateCheckerOptions(const AnalyzerOptions &opts,
131                               DiagnosticsEngine &diags) const;
132
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;
138
139 private:
140   mutable CheckerInfoList Checkers;
141   mutable llvm::StringMap<size_t> Packages;
142 };
143
144 } // namespace ento
145
146 } // namespace clang
147
148 #endif // LLVM_CLANG_STATICANALYZER_CORE_CHECKERREGISTRY_H