Argyrios Kyrtzidis | 556c45e | 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 | 6fa0d20 | 2011-02-15 16:54:12 +0000 | [diff] [blame] | 14 | #include "clang/StaticAnalyzer/Frontend/CheckerRegistration.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 15 | #include "clang/Basic/Diagnostic.h" |
| 16 | #include "clang/Frontend/FrontendDiagnostic.h" |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 17 | #include "clang/StaticAnalyzer/Checkers/ClangCheckers.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h" |
Argyrios Kyrtzidis | 556c45e | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Jordy Rose | 59cce71 | 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 | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "clang/StaticAnalyzer/Frontend/FrontendActions.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallVector.h" |
Jordy Rose | 93b86e4 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 24 | #include "llvm/Support/DynamicLibrary.h" |
Jordy Rose | 075d73b | 2011-08-17 04:56:03 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Path.h" |
Argyrios Kyrtzidis | 17bee3e | 2011-02-25 00:09:51 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Ahmed Charles | dfca6f9 | 2014-03-09 11:36:40 +0000 | [diff] [blame] | 27 | #include <memory> |
Argyrios Kyrtzidis | 556c45e | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace clang; |
| 30 | using namespace ento; |
Jordy Rose | 93b86e4 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 31 | using llvm::sys::DynamicLibrary; |
Argyrios Kyrtzidis | 556c45e | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 32 | |
Jordy Rose | 93b86e4 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 33 | namespace { |
| 34 | class ClangCheckerRegistry : public CheckerRegistry { |
| 35 | typedef void (*RegisterCheckersFn)(CheckerRegistry &); |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 36 | |
Jordy Rose | 93b86e4 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 37 | static bool isCompatibleAPIVersion(const char *versionString); |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 38 | static void warnIncompatible(DiagnosticsEngine *diags, StringRef pluginPath, |
Jordy Rose | 075d73b | 2011-08-17 04:56:03 +0000 | [diff] [blame] | 39 | const char *pluginAPIVersion); |
| 40 | |
| 41 | public: |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 42 | ClangCheckerRegistry(ArrayRef<std::string> plugins, |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 43 | DiagnosticsEngine *diags = nullptr); |
Jordy Rose | 93b86e4 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 44 | }; |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 45 | |
Jordy Rose | 93b86e4 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 46 | } // end anonymous namespace |
| 47 | |
Jordy Rose | 075d73b | 2011-08-17 04:56:03 +0000 | [diff] [blame] | 48 | ClangCheckerRegistry::ClangCheckerRegistry(ArrayRef<std::string> plugins, |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 49 | DiagnosticsEngine *diags) { |
Jordy Rose | 93b86e4 | 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. |
Gabor Horvath | a61bb64 | 2015-07-15 20:32:07 +0000 | [diff] [blame] | 55 | std::string err; |
| 56 | DynamicLibrary lib = DynamicLibrary::getPermanentLibrary(i->c_str(), &err); |
| 57 | if (!lib.isValid()) { |
| 58 | diags->Report(diag::err_fe_unable_to_load_plugin) << *i << err; |
| 59 | continue; |
| 60 | } |
Jordy Rose | 93b86e4 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 61 | |
| 62 | // See if it's compatible with this build of clang. |
| 63 | const char *pluginAPIVersion = |
| 64 | (const char *) lib.getAddressOfSymbol("clang_analyzerAPIVersionString"); |
Jordy Rose | 075d73b | 2011-08-17 04:56:03 +0000 | [diff] [blame] | 65 | if (!isCompatibleAPIVersion(pluginAPIVersion)) { |
| 66 | warnIncompatible(diags, *i, pluginAPIVersion); |
Jordy Rose | 93b86e4 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 67 | continue; |
Jordy Rose | 075d73b | 2011-08-17 04:56:03 +0000 | [diff] [blame] | 68 | } |
Jordy Rose | 93b86e4 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 69 | |
| 70 | // Register its checkers. |
| 71 | RegisterCheckersFn registerPluginCheckers = |
Benjamin Kramer | 8b3929d | 2011-08-17 04:22:25 +0000 | [diff] [blame] | 72 | (RegisterCheckersFn) (intptr_t) lib.getAddressOfSymbol( |
| 73 | "clang_registerCheckers"); |
Jordy Rose | 93b86e4 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 74 | if (registerPluginCheckers) |
| 75 | registerPluginCheckers(*this); |
| 76 | } |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Jordy Rose | 93b86e4 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 79 | bool ClangCheckerRegistry::isCompatibleAPIVersion(const char *versionString) { |
| 80 | // If the version string is null, it's not an analyzer plugin. |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 81 | if (!versionString) |
Jordy Rose | 93b86e4 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 82 | return false; |
| 83 | |
| 84 | // For now, none of the static analyzer API is considered stable. |
| 85 | // Versions must match exactly. |
Alexander Kornienko | 44a784f | 2015-12-28 15:19:39 +0000 | [diff] [blame] | 86 | return strcmp(versionString, CLANG_ANALYZER_API_VERSION_STRING) == 0; |
Jordy Rose | 93b86e4 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 87 | } |
| 88 | |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 89 | void ClangCheckerRegistry::warnIncompatible(DiagnosticsEngine *diags, |
Jordy Rose | 075d73b | 2011-08-17 04:56:03 +0000 | [diff] [blame] | 90 | StringRef pluginPath, |
| 91 | const char *pluginAPIVersion) { |
| 92 | if (!diags) |
| 93 | return; |
| 94 | if (!pluginAPIVersion) |
| 95 | return; |
| 96 | |
| 97 | diags->Report(diag::warn_incompatible_analyzer_plugin_api) |
| 98 | << llvm::sys::path::filename(pluginPath); |
| 99 | diags->Report(diag::note_incompatible_analyzer_plugin_api) |
| 100 | << CLANG_ANALYZER_API_VERSION_STRING |
| 101 | << pluginAPIVersion; |
| 102 | } |
| 103 | |
Gabor Horvath | c430990 | 2016-08-08 13:41:04 +0000 | [diff] [blame] | 104 | static SmallVector<CheckerOptInfo, 8> |
| 105 | getCheckerOptList(const AnalyzerOptions &opts) { |
| 106 | SmallVector<CheckerOptInfo, 8> checkerOpts; |
| 107 | for (unsigned i = 0, e = opts.CheckersControlList.size(); i != e; ++i) { |
| 108 | const std::pair<std::string, bool> &opt = opts.CheckersControlList[i]; |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 109 | checkerOpts.push_back(CheckerOptInfo(opt.first, opt.second)); |
Gabor Horvath | c430990 | 2016-08-08 13:41:04 +0000 | [diff] [blame] | 110 | } |
| 111 | return checkerOpts; |
| 112 | } |
| 113 | |
David Blaikie | 53dd8fe | 2014-08-29 20:11:03 +0000 | [diff] [blame] | 114 | std::unique_ptr<CheckerManager> |
| 115 | ento::createCheckerManager(AnalyzerOptions &opts, const LangOptions &langOpts, |
| 116 | ArrayRef<std::string> plugins, |
| 117 | DiagnosticsEngine &diags) { |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 118 | std::unique_ptr<CheckerManager> checkerMgr( |
David Blaikie | 43a3845 | 2017-01-04 22:36:39 +0000 | [diff] [blame] | 119 | new CheckerManager(langOpts, opts)); |
Argyrios Kyrtzidis | 556c45e | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 120 | |
Gabor Horvath | c430990 | 2016-08-08 13:41:04 +0000 | [diff] [blame] | 121 | SmallVector<CheckerOptInfo, 8> checkerOpts = getCheckerOptList(opts); |
Argyrios Kyrtzidis | 556c45e | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 122 | |
Jordy Rose | 075d73b | 2011-08-17 04:56:03 +0000 | [diff] [blame] | 123 | ClangCheckerRegistry allCheckers(plugins, &diags); |
| 124 | allCheckers.initializeManager(*checkerMgr, checkerOpts); |
Gabor Horvath | fc4c4d4 | 2015-07-09 21:43:45 +0000 | [diff] [blame] | 125 | allCheckers.validateCheckerOptions(opts, diags); |
Argyrios Kyrtzidis | a15dfec | 2011-02-28 17:36:09 +0000 | [diff] [blame] | 126 | checkerMgr->finishedCheckerRegistration(); |
| 127 | |
Argyrios Kyrtzidis | 556c45e | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 128 | for (unsigned i = 0, e = checkerOpts.size(); i != e; ++i) { |
Anna Zaks | d5478fd | 2014-08-29 20:01:38 +0000 | [diff] [blame] | 129 | if (checkerOpts[i].isUnclaimed()) { |
Ted Kremenek | 2b61966 | 2012-07-25 07:12:13 +0000 | [diff] [blame] | 130 | diags.Report(diag::err_unknown_analyzer_checker) |
Argyrios Kyrtzidis | 556c45e | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 131 | << checkerOpts[i].getName(); |
Anna Zaks | d5478fd | 2014-08-29 20:01:38 +0000 | [diff] [blame] | 132 | diags.Report(diag::note_suggest_disabling_all_checkers); |
| 133 | } |
| 134 | |
Argyrios Kyrtzidis | 556c45e | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Richard Trieu | d4b05ce | 2015-01-17 00:46:55 +0000 | [diff] [blame] | 137 | return checkerMgr; |
Argyrios Kyrtzidis | 556c45e | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 138 | } |
Argyrios Kyrtzidis | 17bee3e | 2011-02-25 00:09:51 +0000 | [diff] [blame] | 139 | |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 140 | void ento::printCheckerHelp(raw_ostream &out, ArrayRef<std::string> plugins) { |
| 141 | out << "OVERVIEW: Clang Static Analyzer Checkers List\n\n"; |
| 142 | out << "USAGE: -analyzer-checker <CHECKER or PACKAGE,...>\n\n"; |
Argyrios Kyrtzidis | 17bee3e | 2011-02-25 00:09:51 +0000 | [diff] [blame] | 143 | |
Jordy Rose | 93b86e4 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 144 | ClangCheckerRegistry(plugins).printHelp(out); |
Argyrios Kyrtzidis | 17bee3e | 2011-02-25 00:09:51 +0000 | [diff] [blame] | 145 | } |
Gabor Horvath | c430990 | 2016-08-08 13:41:04 +0000 | [diff] [blame] | 146 | |
| 147 | void ento::printEnabledCheckerList(raw_ostream &out, |
| 148 | ArrayRef<std::string> plugins, |
| 149 | const AnalyzerOptions &opts) { |
| 150 | out << "OVERVIEW: Clang Static Analyzer Enabled Checkers List\n\n"; |
| 151 | |
| 152 | SmallVector<CheckerOptInfo, 8> checkerOpts = getCheckerOptList(opts); |
| 153 | ClangCheckerRegistry(plugins).printList(out, checkerOpts); |
| 154 | } |