Argyrios Kyrtzidis | 43dee22 | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 1 | //===--- CheckerRegistration.cpp - Registration for the Analyzer Checkers -===// |
| 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 | // Defines the registration function for the analyzer checkers. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Argyrios Kyrtzidis | 27af04b | 2011-02-15 16:54:12 +0000 | [diff] [blame] | 14 | #include "clang/StaticAnalyzer/Frontend/CheckerRegistration.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 15 | #include "clang/Basic/Diagnostic.h" |
| 16 | #include "clang/Frontend/FrontendDiagnostic.h" |
Jordy Rose | 08b8653 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 17 | #include "clang/StaticAnalyzer/Checkers/ClangCheckers.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h" |
Argyrios Kyrtzidis | 43dee22 | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Jordy Rose | 08b8653 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/CheckerOptInfo.h" |
| 21 | #include "clang/StaticAnalyzer/Core/CheckerRegistry.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "clang/StaticAnalyzer/Frontend/FrontendActions.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallVector.h" |
Jordy Rose | 77a33a7 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 24 | #include "llvm/Support/DynamicLibrary.h" |
Jordy Rose | a8fd0bc | 2011-08-17 04:56:03 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Path.h" |
Argyrios Kyrtzidis | 116f364 | 2011-02-25 00:09:51 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 27 | #include <memory> |
Argyrios Kyrtzidis | 43dee22 | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace clang; |
| 30 | using namespace ento; |
Jordy Rose | 77a33a7 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 31 | using llvm::sys::DynamicLibrary; |
Argyrios Kyrtzidis | 43dee22 | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 32 | |
Jordy Rose | 77a33a7 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 33 | namespace { |
| 34 | class ClangCheckerRegistry : public CheckerRegistry { |
| 35 | typedef void (*RegisterCheckersFn)(CheckerRegistry &); |
Jordy Rose | 08b8653 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 36 | |
Jordy Rose | 77a33a7 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 37 | static bool isCompatibleAPIVersion(const char *versionString); |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 38 | static void warnIncompatible(DiagnosticsEngine *diags, StringRef pluginPath, |
Jordy Rose | a8fd0bc | 2011-08-17 04:56:03 +0000 | [diff] [blame] | 39 | const char *pluginAPIVersion); |
| 40 | |
| 41 | public: |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 42 | ClangCheckerRegistry(ArrayRef<std::string> plugins, |
| 43 | DiagnosticsEngine *diags = 0); |
Jordy Rose | 77a33a7 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | } // end anonymous namespace |
| 47 | |
Jordy Rose | a8fd0bc | 2011-08-17 04:56:03 +0000 | [diff] [blame] | 48 | ClangCheckerRegistry::ClangCheckerRegistry(ArrayRef<std::string> plugins, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 49 | DiagnosticsEngine *diags) { |
Jordy Rose | 77a33a7 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 50 | registerBuiltinCheckers(*this); |
| 51 | |
| 52 | for (ArrayRef<std::string>::iterator i = plugins.begin(), e = plugins.end(); |
| 53 | i != e; ++i) { |
| 54 | // Get access to the plugin. |
| 55 | DynamicLibrary lib = DynamicLibrary::getPermanentLibrary(i->c_str()); |
| 56 | |
| 57 | // See if it's compatible with this build of clang. |
| 58 | const char *pluginAPIVersion = |
| 59 | (const char *) lib.getAddressOfSymbol("clang_analyzerAPIVersionString"); |
Jordy Rose | a8fd0bc | 2011-08-17 04:56:03 +0000 | [diff] [blame] | 60 | if (!isCompatibleAPIVersion(pluginAPIVersion)) { |
| 61 | warnIncompatible(diags, *i, pluginAPIVersion); |
Jordy Rose | 77a33a7 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 62 | continue; |
Jordy Rose | a8fd0bc | 2011-08-17 04:56:03 +0000 | [diff] [blame] | 63 | } |
Jordy Rose | 77a33a7 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 64 | |
| 65 | // Register its checkers. |
| 66 | RegisterCheckersFn registerPluginCheckers = |
Benjamin Kramer | 6875325 | 2011-08-17 04:22:25 +0000 | [diff] [blame] | 67 | (RegisterCheckersFn) (intptr_t) lib.getAddressOfSymbol( |
| 68 | "clang_registerCheckers"); |
Jordy Rose | 77a33a7 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 69 | if (registerPluginCheckers) |
| 70 | registerPluginCheckers(*this); |
| 71 | } |
Jordy Rose | 08b8653 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Jordy Rose | 77a33a7 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 74 | bool ClangCheckerRegistry::isCompatibleAPIVersion(const char *versionString) { |
| 75 | // If the version string is null, it's not an analyzer plugin. |
| 76 | if (versionString == 0) |
| 77 | return false; |
| 78 | |
| 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) |
| 82 | return true; |
| 83 | |
Jordy Rose | 77a33a7 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 84 | return false; |
| 85 | } |
| 86 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 87 | void ClangCheckerRegistry::warnIncompatible(DiagnosticsEngine *diags, |
Jordy Rose | a8fd0bc | 2011-08-17 04:56:03 +0000 | [diff] [blame] | 88 | StringRef pluginPath, |
| 89 | const char *pluginAPIVersion) { |
| 90 | if (!diags) |
| 91 | return; |
| 92 | if (!pluginAPIVersion) |
| 93 | return; |
| 94 | |
| 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 |
| 99 | << pluginAPIVersion; |
| 100 | } |
| 101 | |
Jordy Rose | 77a33a7 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 102 | |
Ted Kremenek | 8dae128 | 2013-04-16 21:10:05 +0000 | [diff] [blame] | 103 | CheckerManager *ento::createCheckerManager(AnalyzerOptions &opts, |
Jordy Rose | 08b8653 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 104 | const LangOptions &langOpts, |
| 105 | ArrayRef<std::string> plugins, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 106 | DiagnosticsEngine &diags) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 107 | std::unique_ptr<CheckerManager> checkerMgr( |
| 108 | new CheckerManager(langOpts, &opts)); |
Argyrios Kyrtzidis | 43dee22 | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 109 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 110 | SmallVector<CheckerOptInfo, 8> checkerOpts; |
Argyrios Kyrtzidis | 43dee22 | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 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)); |
| 114 | } |
| 115 | |
Jordy Rose | a8fd0bc | 2011-08-17 04:56:03 +0000 | [diff] [blame] | 116 | ClangCheckerRegistry allCheckers(plugins, &diags); |
| 117 | allCheckers.initializeManager(*checkerMgr, checkerOpts); |
Argyrios Kyrtzidis | deb6447 | 2011-02-28 17:36:09 +0000 | [diff] [blame] | 118 | checkerMgr->finishedCheckerRegistration(); |
| 119 | |
Argyrios Kyrtzidis | 43dee22 | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 120 | for (unsigned i = 0, e = checkerOpts.size(); i != e; ++i) { |
| 121 | if (checkerOpts[i].isUnclaimed()) |
Ted Kremenek | c0a4377 | 2012-07-25 07:12:13 +0000 | [diff] [blame] | 122 | diags.Report(diag::err_unknown_analyzer_checker) |
Argyrios Kyrtzidis | 43dee22 | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 123 | << checkerOpts[i].getName(); |
| 124 | } |
| 125 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 126 | return checkerMgr.release(); |
Argyrios Kyrtzidis | 43dee22 | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 127 | } |
Argyrios Kyrtzidis | 116f364 | 2011-02-25 00:09:51 +0000 | [diff] [blame] | 128 | |
Jordy Rose | 08b8653 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 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"; |
Argyrios Kyrtzidis | 116f364 | 2011-02-25 00:09:51 +0000 | [diff] [blame] | 132 | |
Jordy Rose | 77a33a7 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 133 | ClangCheckerRegistry(plugins).printHelp(out); |
Argyrios Kyrtzidis | 116f364 | 2011-02-25 00:09:51 +0000 | [diff] [blame] | 134 | } |