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