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" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h" |
Argyrios Kyrtzidis | 556c45e | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 18 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Kristof Umann | 76a2150 | 2018-12-15 16:23:51 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" |
| 20 | #include "clang/StaticAnalyzer/Frontend/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) { |
Kristof Umann | 76a2150 | 2018-12-15 16:23:51 +0000 | [diff] [blame] | 50 | #define GET_CHECKERS |
| 51 | #define CHECKER(FULLNAME, CLASS, HELPTEXT) \ |
| 52 | addChecker(register##CLASS, FULLNAME, HELPTEXT); |
| 53 | #include "clang/StaticAnalyzer/Checkers/Checkers.inc" |
| 54 | #undef CHECKER |
| 55 | #undef GET_CHECKERS |
Jordy Rose | 93b86e4 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 56 | |
| 57 | for (ArrayRef<std::string>::iterator i = plugins.begin(), e = plugins.end(); |
| 58 | i != e; ++i) { |
| 59 | // Get access to the plugin. |
Gabor Horvath | a61bb64 | 2015-07-15 20:32:07 +0000 | [diff] [blame] | 60 | std::string err; |
| 61 | DynamicLibrary lib = DynamicLibrary::getPermanentLibrary(i->c_str(), &err); |
| 62 | if (!lib.isValid()) { |
| 63 | diags->Report(diag::err_fe_unable_to_load_plugin) << *i << err; |
| 64 | continue; |
| 65 | } |
Jordy Rose | 93b86e4 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 66 | |
| 67 | // See if it's compatible with this build of clang. |
| 68 | const char *pluginAPIVersion = |
| 69 | (const char *) lib.getAddressOfSymbol("clang_analyzerAPIVersionString"); |
Jordy Rose | 075d73b | 2011-08-17 04:56:03 +0000 | [diff] [blame] | 70 | if (!isCompatibleAPIVersion(pluginAPIVersion)) { |
| 71 | warnIncompatible(diags, *i, pluginAPIVersion); |
Jordy Rose | 93b86e4 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 72 | continue; |
Jordy Rose | 075d73b | 2011-08-17 04:56:03 +0000 | [diff] [blame] | 73 | } |
Jordy Rose | 93b86e4 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 74 | |
| 75 | // Register its checkers. |
| 76 | RegisterCheckersFn registerPluginCheckers = |
Benjamin Kramer | 8b3929d | 2011-08-17 04:22:25 +0000 | [diff] [blame] | 77 | (RegisterCheckersFn) (intptr_t) lib.getAddressOfSymbol( |
| 78 | "clang_registerCheckers"); |
Jordy Rose | 93b86e4 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 79 | if (registerPluginCheckers) |
| 80 | registerPluginCheckers(*this); |
| 81 | } |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Jordy Rose | 93b86e4 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 84 | bool ClangCheckerRegistry::isCompatibleAPIVersion(const char *versionString) { |
| 85 | // If the version string is null, it's not an analyzer plugin. |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 86 | if (!versionString) |
Jordy Rose | 93b86e4 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 87 | return false; |
| 88 | |
| 89 | // For now, none of the static analyzer API is considered stable. |
| 90 | // Versions must match exactly. |
Alexander Kornienko | 44a784f | 2015-12-28 15:19:39 +0000 | [diff] [blame] | 91 | return strcmp(versionString, CLANG_ANALYZER_API_VERSION_STRING) == 0; |
Jordy Rose | 93b86e4 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 92 | } |
| 93 | |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 94 | void ClangCheckerRegistry::warnIncompatible(DiagnosticsEngine *diags, |
Jordy Rose | 075d73b | 2011-08-17 04:56:03 +0000 | [diff] [blame] | 95 | StringRef pluginPath, |
| 96 | const char *pluginAPIVersion) { |
| 97 | if (!diags) |
| 98 | return; |
| 99 | if (!pluginAPIVersion) |
| 100 | return; |
| 101 | |
| 102 | diags->Report(diag::warn_incompatible_analyzer_plugin_api) |
| 103 | << llvm::sys::path::filename(pluginPath); |
| 104 | diags->Report(diag::note_incompatible_analyzer_plugin_api) |
| 105 | << CLANG_ANALYZER_API_VERSION_STRING |
| 106 | << pluginAPIVersion; |
| 107 | } |
| 108 | |
Alexander Kornienko | d00ed8e | 2018-06-27 14:56:12 +0000 | [diff] [blame] | 109 | std::unique_ptr<CheckerManager> ento::createCheckerManager( |
George Karpenkov | 4ece68a | 2018-08-06 23:09:07 +0000 | [diff] [blame] | 110 | ASTContext &context, |
| 111 | AnalyzerOptions &opts, |
Alexander Kornienko | d00ed8e | 2018-06-27 14:56:12 +0000 | [diff] [blame] | 112 | ArrayRef<std::string> plugins, |
| 113 | ArrayRef<std::function<void(CheckerRegistry &)>> checkerRegistrationFns, |
| 114 | DiagnosticsEngine &diags) { |
George Karpenkov | 4ece68a | 2018-08-06 23:09:07 +0000 | [diff] [blame] | 115 | auto checkerMgr = llvm::make_unique<CheckerManager>(context, opts); |
Argyrios Kyrtzidis | 556c45e | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 116 | |
Jordy Rose | 075d73b | 2011-08-17 04:56:03 +0000 | [diff] [blame] | 117 | ClangCheckerRegistry allCheckers(plugins, &diags); |
Alexander Kornienko | d00ed8e | 2018-06-27 14:56:12 +0000 | [diff] [blame] | 118 | |
| 119 | for (const auto &Fn : checkerRegistrationFns) |
| 120 | Fn(allCheckers); |
| 121 | |
Kristof Umann | 45beaa0 | 2018-11-18 12:47:03 +0000 | [diff] [blame] | 122 | allCheckers.initializeManager(*checkerMgr, opts, diags); |
Gabor Horvath | fc4c4d4 | 2015-07-09 21:43:45 +0000 | [diff] [blame] | 123 | allCheckers.validateCheckerOptions(opts, diags); |
Argyrios Kyrtzidis | a15dfec | 2011-02-28 17:36:09 +0000 | [diff] [blame] | 124 | checkerMgr->finishedCheckerRegistration(); |
| 125 | |
Richard Trieu | d4b05ce | 2015-01-17 00:46:55 +0000 | [diff] [blame] | 126 | return checkerMgr; |
Argyrios Kyrtzidis | 556c45e | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 127 | } |
Argyrios Kyrtzidis | 17bee3e | 2011-02-25 00:09:51 +0000 | [diff] [blame] | 128 | |
Jordy Rose | 59cce71 | 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 | 17bee3e | 2011-02-25 00:09:51 +0000 | [diff] [blame] | 132 | |
Jordy Rose | 93b86e4 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 133 | ClangCheckerRegistry(plugins).printHelp(out); |
Argyrios Kyrtzidis | 17bee3e | 2011-02-25 00:09:51 +0000 | [diff] [blame] | 134 | } |
Gabor Horvath | c430990 | 2016-08-08 13:41:04 +0000 | [diff] [blame] | 135 | |
| 136 | void ento::printEnabledCheckerList(raw_ostream &out, |
| 137 | ArrayRef<std::string> plugins, |
Kristof Umann | f282d27 | 2018-12-15 15:44:05 +0000 | [diff] [blame] | 138 | const AnalyzerOptions &opts, |
| 139 | DiagnosticsEngine &diags) { |
Gabor Horvath | c430990 | 2016-08-08 13:41:04 +0000 | [diff] [blame] | 140 | out << "OVERVIEW: Clang Static Analyzer Enabled Checkers List\n\n"; |
| 141 | |
Kristof Umann | f282d27 | 2018-12-15 15:44:05 +0000 | [diff] [blame] | 142 | ClangCheckerRegistry(plugins).printList(out, opts, diags); |
Gabor Horvath | c430990 | 2016-08-08 13:41:04 +0000 | [diff] [blame] | 143 | } |
Kristof Umann | f1f351c | 2018-11-02 15:59:37 +0000 | [diff] [blame] | 144 | |
| 145 | void ento::printAnalyzerConfigList(raw_ostream &out) { |
| 146 | out << "OVERVIEW: Clang Static Analyzer -analyzer-config Option List\n\n"; |
| 147 | out << "USAGE: clang -cc1 [CLANG_OPTIONS] -analyzer-config " |
| 148 | "<OPTION1=VALUE,OPTION2=VALUE,...>\n\n"; |
| 149 | out << " clang -cc1 [CLANG_OPTIONS] -analyzer-config OPTION1=VALUE, " |
| 150 | "-analyzer-config OPTION2=VALUE, ...\n\n"; |
| 151 | out << " clang [CLANG_OPTIONS] -Xclang -analyzer-config -Xclang" |
| 152 | "<OPTION1=VALUE,OPTION2=VALUE,...>\n\n"; |
| 153 | out << " clang [CLANG_OPTIONS] -Xclang -analyzer-config -Xclang " |
| 154 | "OPTION1=VALUE, -Xclang -analyzer-config -Xclang " |
| 155 | "OPTION2=VALUE, ...\n\n"; |
| 156 | out << "OPTIONS:\n\n"; |
| 157 | |
| 158 | using OptionAndDescriptionTy = std::pair<StringRef, std::string>; |
| 159 | OptionAndDescriptionTy PrintableOptions[] = { |
| 160 | #define ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC, DEFAULT_VAL) \ |
| 161 | { \ |
| 162 | CMDFLAG, \ |
| 163 | llvm::Twine(llvm::Twine() + "(" + \ |
Kristof Umann | 37829b5 | 2018-11-02 19:48:56 +0000 | [diff] [blame] | 164 | (StringRef(#TYPE) == "StringRef" ? "string" : #TYPE ) + \ |
| 165 | ") " DESC \ |
Kristof Umann | f1f351c | 2018-11-02 15:59:37 +0000 | [diff] [blame] | 166 | " (default: " #DEFAULT_VAL ")").str() \ |
| 167 | }, |
| 168 | |
| 169 | #define ANALYZER_OPTION_DEPENDS_ON_USER_MODE(TYPE, NAME, CMDFLAG, DESC, \ |
| 170 | SHALLOW_VAL, DEEP_VAL) \ |
| 171 | { \ |
| 172 | CMDFLAG, \ |
| 173 | llvm::Twine(llvm::Twine() + "(" + \ |
Kristof Umann | 37829b5 | 2018-11-02 19:48:56 +0000 | [diff] [blame] | 174 | (StringRef(#TYPE) == "StringRef" ? "string" : #TYPE ) + \ |
| 175 | ") " DESC \ |
Kristof Umann | f1f351c | 2018-11-02 15:59:37 +0000 | [diff] [blame] | 176 | " (default: " #SHALLOW_VAL " in shallow mode, " #DEEP_VAL \ |
| 177 | " in deep mode)").str() \ |
| 178 | }, |
| 179 | #include "clang/StaticAnalyzer/Core/AnalyzerOptions.def" |
| 180 | #undef ANALYZER_OPTION |
| 181 | #undef ANALYZER_OPTION_DEPENDS_ON_USER_MODE |
| 182 | }; |
| 183 | |
| 184 | llvm::sort(PrintableOptions, [](const OptionAndDescriptionTy &LHS, |
| 185 | const OptionAndDescriptionTy &RHS) { |
| 186 | return LHS.first < RHS.first; |
| 187 | }); |
| 188 | |
| 189 | constexpr size_t MinLineWidth = 70; |
| 190 | constexpr size_t PadForOpt = 2; |
| 191 | constexpr size_t OptionWidth = 30; |
| 192 | constexpr size_t PadForDesc = PadForOpt + OptionWidth; |
| 193 | static_assert(MinLineWidth > PadForDesc, "MinLineWidth must be greater!"); |
| 194 | |
| 195 | llvm::formatted_raw_ostream FOut(out); |
| 196 | |
| 197 | for (const auto &Pair : PrintableOptions) { |
| 198 | FOut.PadToColumn(PadForOpt) << Pair.first; |
| 199 | |
| 200 | // If the buffer's length is greater then PadForDesc, print a newline. |
| 201 | if (FOut.getColumn() > PadForDesc) |
| 202 | FOut << '\n'; |
| 203 | |
| 204 | FOut.PadToColumn(PadForDesc); |
| 205 | |
| 206 | for (char C : Pair.second) { |
| 207 | if (FOut.getColumn() > MinLineWidth && C == ' ') { |
| 208 | FOut << '\n'; |
| 209 | FOut.PadToColumn(PadForDesc); |
| 210 | continue; |
| 211 | } |
| 212 | FOut << C; |
| 213 | } |
| 214 | FOut << "\n\n"; |
| 215 | } |
| 216 | } |