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/CheckerRegistry.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "clang/StaticAnalyzer/Frontend/FrontendActions.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallVector.h" |
Jordy Rose | 93b86e4 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 23 | #include "llvm/Support/DynamicLibrary.h" |
Kristof Umann | f1f351c | 2018-11-02 15:59:37 +0000 | [diff] [blame] | 24 | #include "llvm/Support/FormattedStream.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 | |
Alexander Kornienko | d00ed8e | 2018-06-27 14:56:12 +0000 | [diff] [blame] | 104 | std::unique_ptr<CheckerManager> ento::createCheckerManager( |
George Karpenkov | 4ece68a | 2018-08-06 23:09:07 +0000 | [diff] [blame] | 105 | ASTContext &context, |
| 106 | AnalyzerOptions &opts, |
Alexander Kornienko | d00ed8e | 2018-06-27 14:56:12 +0000 | [diff] [blame] | 107 | ArrayRef<std::string> plugins, |
| 108 | ArrayRef<std::function<void(CheckerRegistry &)>> checkerRegistrationFns, |
| 109 | DiagnosticsEngine &diags) { |
George Karpenkov | 4ece68a | 2018-08-06 23:09:07 +0000 | [diff] [blame] | 110 | auto checkerMgr = llvm::make_unique<CheckerManager>(context, opts); |
Argyrios Kyrtzidis | 556c45e | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 111 | |
Jordy Rose | 075d73b | 2011-08-17 04:56:03 +0000 | [diff] [blame] | 112 | ClangCheckerRegistry allCheckers(plugins, &diags); |
Alexander Kornienko | d00ed8e | 2018-06-27 14:56:12 +0000 | [diff] [blame] | 113 | |
| 114 | for (const auto &Fn : checkerRegistrationFns) |
| 115 | Fn(allCheckers); |
| 116 | |
Kristof Umann | 45beaa0 | 2018-11-18 12:47:03 +0000 | [diff] [blame] | 117 | allCheckers.initializeManager(*checkerMgr, opts, diags); |
Gabor Horvath | fc4c4d4 | 2015-07-09 21:43:45 +0000 | [diff] [blame] | 118 | allCheckers.validateCheckerOptions(opts, diags); |
Argyrios Kyrtzidis | a15dfec | 2011-02-28 17:36:09 +0000 | [diff] [blame] | 119 | checkerMgr->finishedCheckerRegistration(); |
| 120 | |
Richard Trieu | d4b05ce | 2015-01-17 00:46:55 +0000 | [diff] [blame] | 121 | return checkerMgr; |
Argyrios Kyrtzidis | 556c45e | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 122 | } |
Argyrios Kyrtzidis | 17bee3e | 2011-02-25 00:09:51 +0000 | [diff] [blame] | 123 | |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 124 | void ento::printCheckerHelp(raw_ostream &out, ArrayRef<std::string> plugins) { |
| 125 | out << "OVERVIEW: Clang Static Analyzer Checkers List\n\n"; |
| 126 | out << "USAGE: -analyzer-checker <CHECKER or PACKAGE,...>\n\n"; |
Argyrios Kyrtzidis | 17bee3e | 2011-02-25 00:09:51 +0000 | [diff] [blame] | 127 | |
Jordy Rose | 93b86e4 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 128 | ClangCheckerRegistry(plugins).printHelp(out); |
Argyrios Kyrtzidis | 17bee3e | 2011-02-25 00:09:51 +0000 | [diff] [blame] | 129 | } |
Gabor Horvath | c430990 | 2016-08-08 13:41:04 +0000 | [diff] [blame] | 130 | |
| 131 | void ento::printEnabledCheckerList(raw_ostream &out, |
| 132 | ArrayRef<std::string> plugins, |
| 133 | const AnalyzerOptions &opts) { |
| 134 | out << "OVERVIEW: Clang Static Analyzer Enabled Checkers List\n\n"; |
| 135 | |
Kristof Umann | 45beaa0 | 2018-11-18 12:47:03 +0000 | [diff] [blame] | 136 | ClangCheckerRegistry(plugins).printList(out, opts); |
Gabor Horvath | c430990 | 2016-08-08 13:41:04 +0000 | [diff] [blame] | 137 | } |
Kristof Umann | f1f351c | 2018-11-02 15:59:37 +0000 | [diff] [blame] | 138 | |
| 139 | void ento::printAnalyzerConfigList(raw_ostream &out) { |
| 140 | out << "OVERVIEW: Clang Static Analyzer -analyzer-config Option List\n\n"; |
| 141 | out << "USAGE: clang -cc1 [CLANG_OPTIONS] -analyzer-config " |
| 142 | "<OPTION1=VALUE,OPTION2=VALUE,...>\n\n"; |
| 143 | out << " clang -cc1 [CLANG_OPTIONS] -analyzer-config OPTION1=VALUE, " |
| 144 | "-analyzer-config OPTION2=VALUE, ...\n\n"; |
| 145 | out << " clang [CLANG_OPTIONS] -Xclang -analyzer-config -Xclang" |
| 146 | "<OPTION1=VALUE,OPTION2=VALUE,...>\n\n"; |
| 147 | out << " clang [CLANG_OPTIONS] -Xclang -analyzer-config -Xclang " |
| 148 | "OPTION1=VALUE, -Xclang -analyzer-config -Xclang " |
| 149 | "OPTION2=VALUE, ...\n\n"; |
| 150 | out << "OPTIONS:\n\n"; |
| 151 | |
| 152 | using OptionAndDescriptionTy = std::pair<StringRef, std::string>; |
| 153 | OptionAndDescriptionTy PrintableOptions[] = { |
| 154 | #define ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC, DEFAULT_VAL) \ |
| 155 | { \ |
| 156 | CMDFLAG, \ |
| 157 | llvm::Twine(llvm::Twine() + "(" + \ |
Kristof Umann | 37829b5 | 2018-11-02 19:48:56 +0000 | [diff] [blame] | 158 | (StringRef(#TYPE) == "StringRef" ? "string" : #TYPE ) + \ |
| 159 | ") " DESC \ |
Kristof Umann | f1f351c | 2018-11-02 15:59:37 +0000 | [diff] [blame] | 160 | " (default: " #DEFAULT_VAL ")").str() \ |
| 161 | }, |
| 162 | |
| 163 | #define ANALYZER_OPTION_DEPENDS_ON_USER_MODE(TYPE, NAME, CMDFLAG, DESC, \ |
| 164 | SHALLOW_VAL, DEEP_VAL) \ |
| 165 | { \ |
| 166 | CMDFLAG, \ |
| 167 | llvm::Twine(llvm::Twine() + "(" + \ |
Kristof Umann | 37829b5 | 2018-11-02 19:48:56 +0000 | [diff] [blame] | 168 | (StringRef(#TYPE) == "StringRef" ? "string" : #TYPE ) + \ |
| 169 | ") " DESC \ |
Kristof Umann | f1f351c | 2018-11-02 15:59:37 +0000 | [diff] [blame] | 170 | " (default: " #SHALLOW_VAL " in shallow mode, " #DEEP_VAL \ |
| 171 | " in deep mode)").str() \ |
| 172 | }, |
| 173 | #include "clang/StaticAnalyzer/Core/AnalyzerOptions.def" |
| 174 | #undef ANALYZER_OPTION |
| 175 | #undef ANALYZER_OPTION_DEPENDS_ON_USER_MODE |
| 176 | }; |
| 177 | |
| 178 | llvm::sort(PrintableOptions, [](const OptionAndDescriptionTy &LHS, |
| 179 | const OptionAndDescriptionTy &RHS) { |
| 180 | return LHS.first < RHS.first; |
| 181 | }); |
| 182 | |
| 183 | constexpr size_t MinLineWidth = 70; |
| 184 | constexpr size_t PadForOpt = 2; |
| 185 | constexpr size_t OptionWidth = 30; |
| 186 | constexpr size_t PadForDesc = PadForOpt + OptionWidth; |
| 187 | static_assert(MinLineWidth > PadForDesc, "MinLineWidth must be greater!"); |
| 188 | |
| 189 | llvm::formatted_raw_ostream FOut(out); |
| 190 | |
| 191 | for (const auto &Pair : PrintableOptions) { |
| 192 | FOut.PadToColumn(PadForOpt) << Pair.first; |
| 193 | |
| 194 | // If the buffer's length is greater then PadForDesc, print a newline. |
| 195 | if (FOut.getColumn() > PadForDesc) |
| 196 | FOut << '\n'; |
| 197 | |
| 198 | FOut.PadToColumn(PadForDesc); |
| 199 | |
| 200 | for (char C : Pair.second) { |
| 201 | if (FOut.getColumn() > MinLineWidth && C == ' ') { |
| 202 | FOut << '\n'; |
| 203 | FOut.PadToColumn(PadForDesc); |
| 204 | continue; |
| 205 | } |
| 206 | FOut << C; |
| 207 | } |
| 208 | FOut << "\n\n"; |
| 209 | } |
| 210 | } |