1 //===--- CheckerRegistration.cpp - Registration for the Analyzer Checkers -===//
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 // Defines the registration function for the analyzer checkers.
12 //===----------------------------------------------------------------------===//
14 #include "clang/StaticAnalyzer/Frontend/CheckerRegistration.h"
15 #include "clang/Basic/Diagnostic.h"
16 #include "clang/Frontend/FrontendDiagnostic.h"
17 #include "clang/StaticAnalyzer/Checkers/ClangCheckers.h"
18 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
19 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
20 #include "clang/StaticAnalyzer/Core/CheckerOptInfo.h"
21 #include "clang/StaticAnalyzer/Core/CheckerRegistry.h"
22 #include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
23 #include "llvm/ADT/OwningPtr.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/Support/DynamicLibrary.h"
26 #include "llvm/Support/Path.h"
27 #include "llvm/Support/raw_ostream.h"
29 using namespace clang;
31 using llvm::sys::DynamicLibrary;
34 class ClangCheckerRegistry : public CheckerRegistry {
35 typedef void (*RegisterCheckersFn)(CheckerRegistry &);
37 static bool isCompatibleAPIVersion(const char *versionString);
38 static void warnIncompatible(DiagnosticsEngine *diags, StringRef pluginPath,
39 const char *pluginAPIVersion);
42 ClangCheckerRegistry(ArrayRef<std::string> plugins,
43 DiagnosticsEngine *diags = 0);
46 } // end anonymous namespace
48 ClangCheckerRegistry::ClangCheckerRegistry(ArrayRef<std::string> plugins,
49 DiagnosticsEngine *diags) {
50 registerBuiltinCheckers(*this);
52 for (ArrayRef<std::string>::iterator i = plugins.begin(), e = plugins.end();
54 // Get access to the plugin.
55 DynamicLibrary lib = DynamicLibrary::getPermanentLibrary(i->c_str());
57 // See if it's compatible with this build of clang.
58 const char *pluginAPIVersion =
59 (const char *) lib.getAddressOfSymbol("clang_analyzerAPIVersionString");
60 if (!isCompatibleAPIVersion(pluginAPIVersion)) {
61 warnIncompatible(diags, *i, pluginAPIVersion);
65 // Register its checkers.
66 RegisterCheckersFn registerPluginCheckers =
67 (RegisterCheckersFn) (intptr_t) lib.getAddressOfSymbol(
68 "clang_registerCheckers");
69 if (registerPluginCheckers)
70 registerPluginCheckers(*this);
74 bool ClangCheckerRegistry::isCompatibleAPIVersion(const char *versionString) {
75 // If the version string is null, it's not an analyzer plugin.
76 if (versionString == 0)
79 // For now, none of the static analyzer API is considered stable.
80 // Versions must match exactly.
81 if (strcmp(versionString, CLANG_ANALYZER_API_VERSION_STRING) == 0)
87 void ClangCheckerRegistry::warnIncompatible(DiagnosticsEngine *diags,
89 const char *pluginAPIVersion) {
92 if (!pluginAPIVersion)
95 diags->Report(diag::warn_incompatible_analyzer_plugin_api)
96 << llvm::sys::path::filename(pluginPath);
97 diags->Report(diag::note_incompatible_analyzer_plugin_api)
98 << CLANG_ANALYZER_API_VERSION_STRING
103 CheckerManager *ento::createCheckerManager(AnalyzerOptions &opts,
104 const LangOptions &langOpts,
105 ArrayRef<std::string> plugins,
106 DiagnosticsEngine &diags) {
107 OwningPtr<CheckerManager> checkerMgr(new CheckerManager(langOpts,
110 SmallVector<CheckerOptInfo, 8> checkerOpts;
111 for (unsigned i = 0, e = opts.CheckersControlList.size(); i != e; ++i) {
112 const std::pair<std::string, bool> &opt = opts.CheckersControlList[i];
113 checkerOpts.push_back(CheckerOptInfo(opt.first.c_str(), opt.second));
116 ClangCheckerRegistry allCheckers(plugins, &diags);
117 allCheckers.initializeManager(*checkerMgr, checkerOpts);
118 checkerMgr->finishedCheckerRegistration();
120 for (unsigned i = 0, e = checkerOpts.size(); i != e; ++i) {
121 if (checkerOpts[i].isUnclaimed())
122 diags.Report(diag::err_unknown_analyzer_checker)
123 << checkerOpts[i].getName();
126 return checkerMgr.take();
129 void ento::printCheckerHelp(raw_ostream &out, ArrayRef<std::string> plugins) {
130 out << "OVERVIEW: Clang Static Analyzer Checkers List\n\n";
131 out << "USAGE: -analyzer-checker <CHECKER or PACKAGE,...>\n\n";
133 ClangCheckerRegistry(plugins).printHelp(out);