clang API Documentation
00001 //===--- CheckerRegistry.h - Maintains all available checkers ---*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 00010 #ifndef LLVM_CLANG_STATICANALYZER_CORE_CHECKERREGISTRY_H 00011 #define LLVM_CLANG_STATICANALYZER_CORE_CHECKERREGISTRY_H 00012 00013 #include "clang/StaticAnalyzer/Core/CheckerManager.h" 00014 #include "clang/Basic/LLVM.h" 00015 #include <vector> 00016 00017 // FIXME: move this information to an HTML file in docs/. 00018 // At the very least, a checker plugin is a dynamic library that exports 00019 // clang_analyzerAPIVersionString. This should be defined as follows: 00020 // 00021 // extern "C" 00022 // const char clang_analyzerAPIVersionString[] = 00023 // CLANG_ANALYZER_API_VERSION_STRING; 00024 // 00025 // This is used to check whether the current version of the analyzer is known to 00026 // be incompatible with a plugin. Plugins with incompatible version strings, 00027 // or without a version string at all, will not be loaded. 00028 // 00029 // To add a custom checker to the analyzer, the plugin must also define the 00030 // function clang_registerCheckers. For example: 00031 // 00032 // extern "C" 00033 // void clang_registerCheckers (CheckerRegistry ®istry) { 00034 // registry.addChecker<MainCallChecker>("example.MainCallChecker", 00035 // "Disallows calls to functions called main"); 00036 // } 00037 // 00038 // The first method argument is the full name of the checker, including its 00039 // enclosing package. By convention, the registered name of a checker is the 00040 // name of the associated class (the template argument). 00041 // The second method argument is a short human-readable description of the 00042 // checker. 00043 // 00044 // The clang_registerCheckers function may add any number of checkers to the 00045 // registry. If any checkers require additional initialization, use the three- 00046 // argument form of CheckerRegistry::addChecker. 00047 // 00048 // To load a checker plugin, specify the full path to the dynamic library as 00049 // the argument to the -load option in the cc1 frontend. You can then enable 00050 // your custom checker using the -analyzer-checker: 00051 // 00052 // clang -cc1 -load </path/to/plugin.dylib> -analyze 00053 // -analyzer-checker=<example.MainCallChecker> 00054 // 00055 // For a complete working example, see examples/analyzer-plugin. 00056 00057 00058 namespace clang { 00059 namespace ento { 00060 00061 #ifndef CLANG_ANALYZER_API_VERSION_STRING 00062 // FIXME: The Clang version string is not particularly granular; 00063 // the analyzer infrastructure can change a lot between releases. 00064 // Unfortunately, this string has to be statically embedded in each plugin, 00065 // so we can't just use the functions defined in Version.h. 00066 #include "clang/Basic/Version.h" 00067 #define CLANG_ANALYZER_API_VERSION_STRING CLANG_VERSION_STRING 00068 #endif 00069 00070 class CheckerOptInfo; 00071 00072 /// Manages a set of available checkers for running a static analysis. 00073 /// The checkers are organized into packages by full name, where including 00074 /// a package will recursively include all subpackages and checkers within it. 00075 /// For example, the checker "core.builtin.NoReturnFunctionChecker" will be 00076 /// included if initializeManager() is called with an option of "core", 00077 /// "core.builtin", or the full name "core.builtin.NoReturnFunctionChecker". 00078 class CheckerRegistry { 00079 public: 00080 /// Initialization functions perform any necessary setup for a checker. 00081 /// They should include a call to CheckerManager::registerChecker. 00082 typedef void (*InitializationFunction)(CheckerManager &); 00083 struct CheckerInfo { 00084 InitializationFunction Initialize; 00085 StringRef FullName; 00086 StringRef Desc; 00087 00088 CheckerInfo(InitializationFunction fn, StringRef name, StringRef desc) 00089 : Initialize(fn), FullName(name), Desc(desc) {} 00090 }; 00091 00092 typedef std::vector<CheckerInfo> CheckerInfoList; 00093 00094 private: 00095 template <typename T> 00096 static void initializeManager(CheckerManager &mgr) { 00097 mgr.registerChecker<T>(); 00098 } 00099 00100 public: 00101 /// Adds a checker to the registry. Use this non-templated overload when your 00102 /// checker requires custom initialization. 00103 void addChecker(InitializationFunction fn, StringRef fullName, 00104 StringRef desc); 00105 00106 /// Adds a checker to the registry. Use this templated overload when your 00107 /// checker does not require any custom initialization. 00108 template <class T> 00109 void addChecker(StringRef fullName, StringRef desc) { 00110 // Avoid MSVC's Compiler Error C2276: 00111 // http://msdn.microsoft.com/en-us/library/850cstw1(v=VS.80).aspx 00112 addChecker(&CheckerRegistry::initializeManager<T>, fullName, desc); 00113 } 00114 00115 /// Initializes a CheckerManager by calling the initialization functions for 00116 /// all checkers specified by the given CheckerOptInfo list. The order of this 00117 /// list is significant; later options can be used to reverse earlier ones. 00118 /// This can be used to exclude certain checkers in an included package. 00119 void initializeManager(CheckerManager &mgr, 00120 SmallVectorImpl<CheckerOptInfo> &opts) const; 00121 00122 /// Prints the name and description of all checkers in this registry. 00123 /// This output is not intended to be machine-parseable. 00124 void printHelp(raw_ostream &out, size_t maxNameChars = 30) const ; 00125 00126 private: 00127 mutable CheckerInfoList Checkers; 00128 mutable llvm::StringMap<size_t> Packages; 00129 }; 00130 00131 } // end namespace ento 00132 } // end namespace clang 00133 00134 #endif