clang API Documentation

CheckerRegistry.h
Go to the documentation of this file.
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/Basic/LLVM.h"
00014 #include "clang/StaticAnalyzer/Core/CheckerManager.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 &registry) {
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 #ifndef CLANG_ANALYZER_API_VERSION_STRING
00058 // FIXME: The Clang version string is not particularly granular;
00059 // the analyzer infrastructure can change a lot between releases.
00060 // Unfortunately, this string has to be statically embedded in each plugin,
00061 // so we can't just use the functions defined in Version.h.
00062 #include "clang/Basic/Version.h"
00063 #define CLANG_ANALYZER_API_VERSION_STRING CLANG_VERSION_STRING
00064 #endif
00065 
00066 namespace clang {
00067 namespace ento {
00068 
00069 class CheckerOptInfo;
00070 
00071 /// Manages a set of available checkers for running a static analysis.
00072 /// The checkers are organized into packages by full name, where including
00073 /// a package will recursively include all subpackages and checkers within it.
00074 /// For example, the checker "core.builtin.NoReturnFunctionChecker" will be
00075 /// included if initializeManager() is called with an option of "core",
00076 /// "core.builtin", or the full name "core.builtin.NoReturnFunctionChecker".
00077 class CheckerRegistry {
00078 public:
00079   /// Initialization functions perform any necessary setup for a checker.
00080   /// They should include a call to CheckerManager::registerChecker.
00081   typedef void (*InitializationFunction)(CheckerManager &);
00082   struct CheckerInfo {
00083     InitializationFunction Initialize;
00084     StringRef FullName;
00085     StringRef Desc;
00086 
00087     CheckerInfo(InitializationFunction fn, StringRef name, StringRef desc)
00088     : Initialize(fn), FullName(name), Desc(desc) {}
00089   };
00090 
00091   typedef std::vector<CheckerInfo> CheckerInfoList;
00092 
00093 private:
00094   template <typename T>
00095   static void initializeManager(CheckerManager &mgr) {
00096     mgr.registerChecker<T>();
00097   }
00098 
00099 public:
00100   /// Adds a checker to the registry. Use this non-templated overload when your
00101   /// checker requires custom initialization.
00102   void addChecker(InitializationFunction fn, StringRef fullName,
00103                   StringRef desc);
00104 
00105   /// Adds a checker to the registry. Use this templated overload when your
00106   /// checker does not require any custom initialization.
00107   template <class T>
00108   void addChecker(StringRef fullName, StringRef desc) {
00109     // Avoid MSVC's Compiler Error C2276:
00110     // http://msdn.microsoft.com/en-us/library/850cstw1(v=VS.80).aspx
00111     addChecker(&CheckerRegistry::initializeManager<T>, fullName, desc);
00112   }
00113 
00114   /// Initializes a CheckerManager by calling the initialization functions for
00115   /// all checkers specified by the given CheckerOptInfo list. The order of this
00116   /// list is significant; later options can be used to reverse earlier ones.
00117   /// This can be used to exclude certain checkers in an included package.
00118   void initializeManager(CheckerManager &mgr,
00119                          SmallVectorImpl<CheckerOptInfo> &opts) const;
00120 
00121   /// Prints the name and description of all checkers in this registry.
00122   /// This output is not intended to be machine-parseable.
00123   void printHelp(raw_ostream &out, size_t maxNameChars = 30) const ;
00124 
00125 private:
00126   mutable CheckerInfoList Checkers;
00127   mutable llvm::StringMap<size_t> Packages;
00128 };
00129 
00130 } // end namespace ento
00131 } // end namespace clang
00132 
00133 #endif