Argyrios Kyrtzidis | 556c45e | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 1 | //===--- CheckerRegistration.cpp - Registration for the Analyzer Checkers -===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Argyrios Kyrtzidis | 556c45e | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Defines the registration function for the analyzer checkers. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Argyrios Kyrtzidis | 6fa0d20 | 2011-02-15 16:54:12 +0000 | [diff] [blame] | 13 | #include "clang/StaticAnalyzer/Frontend/CheckerRegistration.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 14 | #include "clang/Basic/Diagnostic.h" |
| 15 | #include "clang/Frontend/FrontendDiagnostic.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h" |
Argyrios Kyrtzidis | 556c45e | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 17 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Kristof Umann | 76a2150 | 2018-12-15 16:23:51 +0000 | [diff] [blame] | 18 | #include "clang/StaticAnalyzer/Frontend/CheckerRegistry.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Frontend/FrontendActions.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallVector.h" |
Kristof Umann | f1f351c | 2018-11-02 15:59:37 +0000 | [diff] [blame] | 21 | #include "llvm/Support/FormattedStream.h" |
Argyrios Kyrtzidis | 17bee3e | 2011-02-25 00:09:51 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Ahmed Charles | dfca6f9 | 2014-03-09 11:36:40 +0000 | [diff] [blame] | 23 | #include <memory> |
Argyrios Kyrtzidis | 556c45e | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace clang; |
| 26 | using namespace ento; |
Jordy Rose | 075d73b | 2011-08-17 04:56:03 +0000 | [diff] [blame] | 27 | |
Alexander Kornienko | d00ed8e | 2018-06-27 14:56:12 +0000 | [diff] [blame] | 28 | std::unique_ptr<CheckerManager> ento::createCheckerManager( |
George Karpenkov | 4ece68a | 2018-08-06 23:09:07 +0000 | [diff] [blame] | 29 | ASTContext &context, |
| 30 | AnalyzerOptions &opts, |
Alexander Kornienko | d00ed8e | 2018-06-27 14:56:12 +0000 | [diff] [blame] | 31 | ArrayRef<std::string> plugins, |
| 32 | ArrayRef<std::function<void(CheckerRegistry &)>> checkerRegistrationFns, |
| 33 | DiagnosticsEngine &diags) { |
George Karpenkov | 4ece68a | 2018-08-06 23:09:07 +0000 | [diff] [blame] | 34 | auto checkerMgr = llvm::make_unique<CheckerManager>(context, opts); |
Argyrios Kyrtzidis | 556c45e | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 35 | |
Kristof Umann | 058a7a4 | 2019-01-26 14:23:08 +0000 | [diff] [blame^] | 36 | CheckerRegistry allCheckers(plugins, diags, context.getLangOpts()); |
Alexander Kornienko | d00ed8e | 2018-06-27 14:56:12 +0000 | [diff] [blame] | 37 | |
| 38 | for (const auto &Fn : checkerRegistrationFns) |
| 39 | Fn(allCheckers); |
| 40 | |
Kristof Umann | b0be2ab | 2018-12-15 18:11:49 +0000 | [diff] [blame] | 41 | allCheckers.initializeManager(*checkerMgr, opts); |
| 42 | allCheckers.validateCheckerOptions(opts); |
Argyrios Kyrtzidis | a15dfec | 2011-02-28 17:36:09 +0000 | [diff] [blame] | 43 | checkerMgr->finishedCheckerRegistration(); |
| 44 | |
Richard Trieu | d4b05ce | 2015-01-17 00:46:55 +0000 | [diff] [blame] | 45 | return checkerMgr; |
Argyrios Kyrtzidis | 556c45e | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 46 | } |
Argyrios Kyrtzidis | 17bee3e | 2011-02-25 00:09:51 +0000 | [diff] [blame] | 47 | |
Kristof Umann | b0be2ab | 2018-12-15 18:11:49 +0000 | [diff] [blame] | 48 | void ento::printCheckerHelp(raw_ostream &out, ArrayRef<std::string> plugins, |
Kristof Umann | 058a7a4 | 2019-01-26 14:23:08 +0000 | [diff] [blame^] | 49 | DiagnosticsEngine &diags, |
| 50 | const LangOptions &langOpts) { |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 51 | out << "OVERVIEW: Clang Static Analyzer Checkers List\n\n"; |
| 52 | out << "USAGE: -analyzer-checker <CHECKER or PACKAGE,...>\n\n"; |
Argyrios Kyrtzidis | 17bee3e | 2011-02-25 00:09:51 +0000 | [diff] [blame] | 53 | |
Kristof Umann | 058a7a4 | 2019-01-26 14:23:08 +0000 | [diff] [blame^] | 54 | CheckerRegistry(plugins, diags, langOpts).printHelp(out); |
Argyrios Kyrtzidis | 17bee3e | 2011-02-25 00:09:51 +0000 | [diff] [blame] | 55 | } |
Gabor Horvath | c430990 | 2016-08-08 13:41:04 +0000 | [diff] [blame] | 56 | |
| 57 | void ento::printEnabledCheckerList(raw_ostream &out, |
| 58 | ArrayRef<std::string> plugins, |
Kristof Umann | f282d27 | 2018-12-15 15:44:05 +0000 | [diff] [blame] | 59 | const AnalyzerOptions &opts, |
Kristof Umann | 058a7a4 | 2019-01-26 14:23:08 +0000 | [diff] [blame^] | 60 | DiagnosticsEngine &diags, |
| 61 | const LangOptions &langOpts) { |
Gabor Horvath | c430990 | 2016-08-08 13:41:04 +0000 | [diff] [blame] | 62 | out << "OVERVIEW: Clang Static Analyzer Enabled Checkers List\n\n"; |
| 63 | |
Kristof Umann | 058a7a4 | 2019-01-26 14:23:08 +0000 | [diff] [blame^] | 64 | CheckerRegistry(plugins, diags, langOpts).printList(out, opts); |
Gabor Horvath | c430990 | 2016-08-08 13:41:04 +0000 | [diff] [blame] | 65 | } |
Kristof Umann | f1f351c | 2018-11-02 15:59:37 +0000 | [diff] [blame] | 66 | |
| 67 | void ento::printAnalyzerConfigList(raw_ostream &out) { |
| 68 | out << "OVERVIEW: Clang Static Analyzer -analyzer-config Option List\n\n"; |
| 69 | out << "USAGE: clang -cc1 [CLANG_OPTIONS] -analyzer-config " |
| 70 | "<OPTION1=VALUE,OPTION2=VALUE,...>\n\n"; |
| 71 | out << " clang -cc1 [CLANG_OPTIONS] -analyzer-config OPTION1=VALUE, " |
| 72 | "-analyzer-config OPTION2=VALUE, ...\n\n"; |
| 73 | out << " clang [CLANG_OPTIONS] -Xclang -analyzer-config -Xclang" |
| 74 | "<OPTION1=VALUE,OPTION2=VALUE,...>\n\n"; |
| 75 | out << " clang [CLANG_OPTIONS] -Xclang -analyzer-config -Xclang " |
| 76 | "OPTION1=VALUE, -Xclang -analyzer-config -Xclang " |
| 77 | "OPTION2=VALUE, ...\n\n"; |
| 78 | out << "OPTIONS:\n\n"; |
| 79 | |
| 80 | using OptionAndDescriptionTy = std::pair<StringRef, std::string>; |
| 81 | OptionAndDescriptionTy PrintableOptions[] = { |
| 82 | #define ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC, DEFAULT_VAL) \ |
| 83 | { \ |
| 84 | CMDFLAG, \ |
| 85 | llvm::Twine(llvm::Twine() + "(" + \ |
Kristof Umann | 37829b5 | 2018-11-02 19:48:56 +0000 | [diff] [blame] | 86 | (StringRef(#TYPE) == "StringRef" ? "string" : #TYPE ) + \ |
| 87 | ") " DESC \ |
Kristof Umann | f1f351c | 2018-11-02 15:59:37 +0000 | [diff] [blame] | 88 | " (default: " #DEFAULT_VAL ")").str() \ |
| 89 | }, |
| 90 | |
| 91 | #define ANALYZER_OPTION_DEPENDS_ON_USER_MODE(TYPE, NAME, CMDFLAG, DESC, \ |
| 92 | SHALLOW_VAL, DEEP_VAL) \ |
| 93 | { \ |
| 94 | CMDFLAG, \ |
| 95 | llvm::Twine(llvm::Twine() + "(" + \ |
Kristof Umann | 37829b5 | 2018-11-02 19:48:56 +0000 | [diff] [blame] | 96 | (StringRef(#TYPE) == "StringRef" ? "string" : #TYPE ) + \ |
| 97 | ") " DESC \ |
Kristof Umann | f1f351c | 2018-11-02 15:59:37 +0000 | [diff] [blame] | 98 | " (default: " #SHALLOW_VAL " in shallow mode, " #DEEP_VAL \ |
| 99 | " in deep mode)").str() \ |
| 100 | }, |
| 101 | #include "clang/StaticAnalyzer/Core/AnalyzerOptions.def" |
| 102 | #undef ANALYZER_OPTION |
| 103 | #undef ANALYZER_OPTION_DEPENDS_ON_USER_MODE |
| 104 | }; |
| 105 | |
| 106 | llvm::sort(PrintableOptions, [](const OptionAndDescriptionTy &LHS, |
| 107 | const OptionAndDescriptionTy &RHS) { |
| 108 | return LHS.first < RHS.first; |
| 109 | }); |
| 110 | |
| 111 | constexpr size_t MinLineWidth = 70; |
| 112 | constexpr size_t PadForOpt = 2; |
| 113 | constexpr size_t OptionWidth = 30; |
| 114 | constexpr size_t PadForDesc = PadForOpt + OptionWidth; |
| 115 | static_assert(MinLineWidth > PadForDesc, "MinLineWidth must be greater!"); |
| 116 | |
| 117 | llvm::formatted_raw_ostream FOut(out); |
| 118 | |
| 119 | for (const auto &Pair : PrintableOptions) { |
| 120 | FOut.PadToColumn(PadForOpt) << Pair.first; |
| 121 | |
| 122 | // If the buffer's length is greater then PadForDesc, print a newline. |
| 123 | if (FOut.getColumn() > PadForDesc) |
| 124 | FOut << '\n'; |
| 125 | |
| 126 | FOut.PadToColumn(PadForDesc); |
| 127 | |
| 128 | for (char C : Pair.second) { |
| 129 | if (FOut.getColumn() > MinLineWidth && C == ' ') { |
| 130 | FOut << '\n'; |
| 131 | FOut.PadToColumn(PadForDesc); |
| 132 | continue; |
| 133 | } |
| 134 | FOut << C; |
| 135 | } |
| 136 | FOut << "\n\n"; |
| 137 | } |
| 138 | } |