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" |
Argyrios Kyrtzidis | 116f364 | 2011-02-25 00:09:51 +0000 | [diff] [blame] | 15 | #include "clang/StaticAnalyzer/Frontend/FrontendActions.h" |
Jordy Rose | 08b8653 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 16 | #include "clang/StaticAnalyzer/Checkers/ClangCheckers.h" |
Argyrios Kyrtzidis | 43dee22 | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 17 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Jordy Rose | 08b8653 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 18 | #include "clang/StaticAnalyzer/Core/CheckerOptInfo.h" |
| 19 | #include "clang/StaticAnalyzer/Core/CheckerRegistry.h" |
Argyrios Kyrtzidis | 43dee22 | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 20 | #include "clang/Frontend/AnalyzerOptions.h" |
| 21 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 22 | #include "clang/Basic/Diagnostic.h" |
Jordy Rose | 77a33a7 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 23 | #include "llvm/Support/DynamicLibrary.h" |
Jordy Rose | a8fd0bc | 2011-08-17 04:56:03 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Path.h" |
Argyrios Kyrtzidis | 116f364 | 2011-02-25 00:09:51 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Argyrios Kyrtzidis | 43dee22 | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/OwningPtr.h" |
| 27 | #include "llvm/ADT/SmallVector.h" |
| 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); |
Jordy Rose | a8fd0bc | 2011-08-17 04:56:03 +0000 | [diff] [blame] | 38 | static void warnIncompatible(Diagnostic *diags, StringRef pluginPath, |
| 39 | const char *pluginAPIVersion); |
| 40 | |
| 41 | public: |
| 42 | ClangCheckerRegistry(ArrayRef<std::string> plugins, Diagnostic *diags = 0); |
Jordy Rose | 77a33a7 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | } // end anonymous namespace |
| 46 | |
Jordy Rose | a8fd0bc | 2011-08-17 04:56:03 +0000 | [diff] [blame] | 47 | ClangCheckerRegistry::ClangCheckerRegistry(ArrayRef<std::string> plugins, |
| 48 | Diagnostic *diags) { |
Jordy Rose | 77a33a7 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 49 | registerBuiltinCheckers(*this); |
| 50 | |
| 51 | for (ArrayRef<std::string>::iterator i = plugins.begin(), e = plugins.end(); |
| 52 | i != e; ++i) { |
| 53 | // Get access to the plugin. |
| 54 | DynamicLibrary lib = DynamicLibrary::getPermanentLibrary(i->c_str()); |
| 55 | |
| 56 | // See if it's compatible with this build of clang. |
| 57 | const char *pluginAPIVersion = |
| 58 | (const char *) lib.getAddressOfSymbol("clang_analyzerAPIVersionString"); |
Jordy Rose | a8fd0bc | 2011-08-17 04:56:03 +0000 | [diff] [blame] | 59 | if (!isCompatibleAPIVersion(pluginAPIVersion)) { |
| 60 | warnIncompatible(diags, *i, pluginAPIVersion); |
Jordy Rose | 77a33a7 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 61 | continue; |
Jordy Rose | a8fd0bc | 2011-08-17 04:56:03 +0000 | [diff] [blame] | 62 | } |
Jordy Rose | 77a33a7 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 63 | |
| 64 | // Register its checkers. |
| 65 | RegisterCheckersFn registerPluginCheckers = |
Benjamin Kramer | 6875325 | 2011-08-17 04:22:25 +0000 | [diff] [blame] | 66 | (RegisterCheckersFn) (intptr_t) lib.getAddressOfSymbol( |
| 67 | "clang_registerCheckers"); |
Jordy Rose | 77a33a7 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 68 | if (registerPluginCheckers) |
| 69 | registerPluginCheckers(*this); |
| 70 | } |
Jordy Rose | 08b8653 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Jordy Rose | 77a33a7 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 73 | bool ClangCheckerRegistry::isCompatibleAPIVersion(const char *versionString) { |
| 74 | // If the version string is null, it's not an analyzer plugin. |
| 75 | if (versionString == 0) |
| 76 | return false; |
| 77 | |
| 78 | // For now, none of the static analyzer API is considered stable. |
| 79 | // Versions must match exactly. |
| 80 | if (strcmp(versionString, CLANG_ANALYZER_API_VERSION_STRING) == 0) |
| 81 | return true; |
| 82 | |
Jordy Rose | 77a33a7 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 83 | return false; |
| 84 | } |
| 85 | |
Jordy Rose | a8fd0bc | 2011-08-17 04:56:03 +0000 | [diff] [blame] | 86 | void ClangCheckerRegistry::warnIncompatible(Diagnostic *diags, |
| 87 | StringRef pluginPath, |
| 88 | const char *pluginAPIVersion) { |
| 89 | if (!diags) |
| 90 | return; |
| 91 | if (!pluginAPIVersion) |
| 92 | return; |
| 93 | |
| 94 | diags->Report(diag::warn_incompatible_analyzer_plugin_api) |
| 95 | << llvm::sys::path::filename(pluginPath); |
| 96 | diags->Report(diag::note_incompatible_analyzer_plugin_api) |
| 97 | << CLANG_ANALYZER_API_VERSION_STRING |
| 98 | << pluginAPIVersion; |
| 99 | } |
| 100 | |
Jordy Rose | 77a33a7 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 101 | |
Jordy Rose | 08b8653 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 102 | CheckerManager *ento::createCheckerManager(const AnalyzerOptions &opts, |
| 103 | const LangOptions &langOpts, |
| 104 | ArrayRef<std::string> plugins, |
| 105 | Diagnostic &diags) { |
Argyrios Kyrtzidis | 2e471a3 | 2011-02-23 07:19:14 +0000 | [diff] [blame] | 106 | llvm::OwningPtr<CheckerManager> checkerMgr(new CheckerManager(langOpts)); |
Argyrios Kyrtzidis | 43dee22 | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 107 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 108 | SmallVector<CheckerOptInfo, 8> checkerOpts; |
Argyrios Kyrtzidis | 43dee22 | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 109 | for (unsigned i = 0, e = opts.CheckersControlList.size(); i != e; ++i) { |
| 110 | const std::pair<std::string, bool> &opt = opts.CheckersControlList[i]; |
| 111 | checkerOpts.push_back(CheckerOptInfo(opt.first.c_str(), opt.second)); |
| 112 | } |
| 113 | |
Jordy Rose | a8fd0bc | 2011-08-17 04:56:03 +0000 | [diff] [blame] | 114 | ClangCheckerRegistry allCheckers(plugins, &diags); |
| 115 | allCheckers.initializeManager(*checkerMgr, checkerOpts); |
Argyrios Kyrtzidis | deb6447 | 2011-02-28 17:36:09 +0000 | [diff] [blame] | 116 | checkerMgr->finishedCheckerRegistration(); |
| 117 | |
Argyrios Kyrtzidis | 43dee22 | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 118 | for (unsigned i = 0, e = checkerOpts.size(); i != e; ++i) { |
| 119 | if (checkerOpts[i].isUnclaimed()) |
Jordy Rose | b7b0608 | 2011-08-17 05:00:56 +0000 | [diff] [blame] | 120 | diags.Report(diag::warn_unknown_analyzer_checker) |
Argyrios Kyrtzidis | 43dee22 | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 121 | << checkerOpts[i].getName(); |
| 122 | } |
| 123 | |
| 124 | return checkerMgr.take(); |
| 125 | } |
Argyrios Kyrtzidis | 116f364 | 2011-02-25 00:09:51 +0000 | [diff] [blame] | 126 | |
Jordy Rose | 08b8653 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 127 | void ento::printCheckerHelp(raw_ostream &out, ArrayRef<std::string> plugins) { |
| 128 | out << "OVERVIEW: Clang Static Analyzer Checkers List\n\n"; |
| 129 | out << "USAGE: -analyzer-checker <CHECKER or PACKAGE,...>\n\n"; |
Argyrios Kyrtzidis | 116f364 | 2011-02-25 00:09:51 +0000 | [diff] [blame] | 130 | |
Jordy Rose | 77a33a7 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 131 | ClangCheckerRegistry(plugins).printHelp(out); |
Argyrios Kyrtzidis | 116f364 | 2011-02-25 00:09:51 +0000 | [diff] [blame] | 132 | } |