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 <vector>
16
17// FIXME: move this information to an HTML file in docs/.
18// At the very least, a checker plugin is a dynamic library that exports
19// clang_analyzerAPIVersionString. This should be defined as follows:
20//
21//   extern "C"
22//   const char clang_analyzerAPIVersionString[] =
23//     CLANG_ANALYZER_API_VERSION_STRING;
24//
25// This is used to check whether the current version of the analyzer is known to
26// be incompatible with a plugin. Plugins with incompatible version strings,
27// or without a version string at all, will not be loaded.
28//
29// To add a custom checker to the analyzer, the plugin must also define the
30// function clang_registerCheckers. For example:
31//
32//    extern "C"
33//    void clang_registerCheckers (CheckerRegistry &registry) {
34//      registry.addChecker<MainCallChecker>("example.MainCallChecker",
35//        "Disallows calls to functions called main");
36//    }
37//
38// The first method argument is the full name of the checker, including its
39// enclosing package. By convention, the registered name of a checker is the
40// name of the associated class (the template argument).
41// The second method argument is a short human-readable description of the
42// checker.
43//
44// The clang_registerCheckers function may add any number of checkers to the
45// registry. If any checkers require additional initialization, use the three-
46// argument form of CheckerRegistry::addChecker.
47//
48// To load a checker plugin, specify the full path to the dynamic library as
49// the argument to the -load option in the cc1 frontend. You can then enable
50// your custom checker using the -analyzer-checker:
51//
52//   clang -cc1 -load </path/to/plugin.dylib> -analyze
53//     -analyzer-checker=<example.MainCallChecker>
54//
55// For a complete working example, see examples/analyzer-plugin.
56
57#ifndef CLANG_ANALYZER_API_VERSION_STRING
58// FIXME: The Clang version string is not particularly granular;
59// the analyzer infrastructure can change a lot between releases.
60// Unfortunately, this string has to be statically embedded in each plugin,
61// so we can't just use the functions defined in Version.h.
62#include "clang/Basic/Version.h"
63#define CLANG_ANALYZER_API_VERSION_STRING CLANG_VERSION_STRING
64#endif
65
66namespace clang {
67namespace ento {
68
69class CheckerOptInfo;
70
71/// Manages a set of available checkers for running a static analysis.
72/// The checkers are organized into packages by full name, where including
73/// a package will recursively include all subpackages and checkers within it.
74/// For example, the checker "core.builtin.NoReturnFunctionChecker" will be
75/// included if initializeManager() is called with an option of "core",
76/// "core.builtin", or the full name "core.builtin.NoReturnFunctionChecker".
77class CheckerRegistry {
78public:
79  /// Initialization functions perform any necessary setup for a checker.
80  /// They should include a call to CheckerManager::registerChecker.
81  typedef void (*InitializationFunction)(CheckerManager &);
82  struct CheckerInfo {
83    InitializationFunction Initialize;
84    StringRef FullName;
85    StringRef Desc;
86
87    CheckerInfo(InitializationFunction fn, StringRef name, StringRef desc)
88    : Initialize(fn), FullName(name), Desc(desc) {}
89  };
90
91  typedef std::vector<CheckerInfo> CheckerInfoList;
92
93private:
94  template <typename T>
95  static void initializeManager(CheckerManager &mgr) {
96    mgr.registerChecker<T>();
97  }
98
99public:
100  /// Adds a checker to the registry. Use this non-templated overload when your
101  /// checker requires custom initialization.
102  void addChecker(InitializationFunction fn, StringRef fullName,
103                  StringRef desc);
104
105  /// Adds a checker to the registry. Use this templated overload when your
106  /// checker does not require any custom initialization.
107  template <class T>
108  void addChecker(StringRef fullName, StringRef desc) {
109    // Avoid MSVC's Compiler Error C2276:
110    // http://msdn.microsoft.com/en-us/library/850cstw1(v=VS.80).aspx
111    addChecker(&CheckerRegistry::initializeManager<T>, fullName, desc);
112  }
113
114  /// Initializes a CheckerManager by calling the initialization functions for
115  /// all checkers specified by the given CheckerOptInfo list. The order of this
116  /// list is significant; later options can be used to reverse earlier ones.
117  /// This can be used to exclude certain checkers in an included package.
118  void initializeManager(CheckerManager &mgr,
119                         SmallVectorImpl<CheckerOptInfo> &opts) const;
120
121  /// Prints the name and description of all checkers in this registry.
122  /// This output is not intended to be machine-parseable.
123  void printHelp(raw_ostream &out, size_t maxNameChars = 30) const ;
124
125private:
126  mutable CheckerInfoList Checkers;
127  mutable llvm::StringMap<size_t> Packages;
128};
129
130} // end namespace ento
131} // end namespace clang
132
133#endif
134