Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 1 | //===--- tools/extra/clang-tidy/ClangTidyMain.cpp - Clang tidy tool -------===// |
| 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 | /// \file This file implements a clang-tidy tool. |
| 11 | /// |
| 12 | /// This tool uses the Clang Tooling infrastructure, see |
| 13 | /// http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html |
| 14 | /// for details on setting it up with LLVM source tree. |
| 15 | /// |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #include "../ClangTidy.h" |
Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 19 | #include "clang/Tooling/CommonOptionsParser.h" |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace clang::ast_matchers; |
| 22 | using namespace clang::driver; |
| 23 | using namespace clang::tooling; |
| 24 | using namespace llvm; |
| 25 | |
Alexander Kornienko | 99c9d6a | 2014-02-05 13:43:27 +0000 | [diff] [blame] | 26 | static cl::OptionCategory ClangTidyCategory("clang-tidy options"); |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 27 | |
Manuel Klimek | 814f9bd | 2013-11-14 15:49:44 +0000 | [diff] [blame] | 28 | static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage); |
Alexander Kornienko | d53d268 | 2014-09-04 14:23:36 +0000 | [diff] [blame^] | 29 | static cl::extrahelp ClangTidyHelp( |
| 30 | "Configuration files:\n" |
| 31 | " clang-tidy attempts to read configuration for each source file from a\n" |
| 32 | " .clang-tidy file located in the closest parent directory of the source\n" |
| 33 | " file. If any configuration options have a corresponding command-line\n" |
| 34 | " option, command-line option takes precedence. The effective\n" |
| 35 | " configuration can be inspected using -dump-config.\n\n"); |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 36 | |
Alexander Kornienko | 23fe959 | 2014-05-15 14:27:36 +0000 | [diff] [blame] | 37 | const char DefaultChecks[] = |
| 38 | "*," // Enable all checks, except these: |
| 39 | "-clang-analyzer-alpha*," // Too many false positives. |
| 40 | "-llvm-include-order," // Not implemented yet. |
Alexander Kornienko | 23fe959 | 2014-05-15 14:27:36 +0000 | [diff] [blame] | 41 | "-google-*,"; // Doesn't apply to LLVM. |
Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame] | 42 | |
Alexander Kornienko | 23fe959 | 2014-05-15 14:27:36 +0000 | [diff] [blame] | 43 | static cl::opt<std::string> |
Alexander Kornienko | 3ab3467 | 2014-05-16 13:07:18 +0000 | [diff] [blame] | 44 | Checks("checks", cl::desc("Comma-separated list of globs with optional '-'\n" |
| 45 | "prefix. Globs are processed in order of appearance\n" |
| 46 | "in the list. Globs without '-' prefix add checks\n" |
| 47 | "with matching names to the set, globs with the '-'\n" |
| 48 | "prefix remove checks with matching names from the\n" |
Alexander Kornienko | d53d268 | 2014-09-04 14:23:36 +0000 | [diff] [blame^] | 49 | "set of enabled checks.\n" |
| 50 | "This option's value is appended to the value read\n" |
| 51 | "from a .clang-tidy file, if any."), |
Alexander Kornienko | 23fe959 | 2014-05-15 14:27:36 +0000 | [diff] [blame] | 52 | cl::init(""), cl::cat(ClangTidyCategory)); |
Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame] | 53 | |
Alexander Kornienko | 3ab3467 | 2014-05-16 13:07:18 +0000 | [diff] [blame] | 54 | static cl::opt<std::string> |
| 55 | HeaderFilter("header-filter", |
| 56 | cl::desc("Regular expression matching the names of the\n" |
Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame] | 57 | "headers to output diagnostics from. Diagnostics\n" |
| 58 | "from the main file of each translation unit are\n" |
| 59 | "always displayed.\n" |
Alexander Kornienko | d53d268 | 2014-09-04 14:23:36 +0000 | [diff] [blame^] | 60 | "Can be used together with -line-filter.\n" |
| 61 | "This option overrides the value read from a\n" |
| 62 | ".clang-tidy file."), |
Alexander Kornienko | 3ab3467 | 2014-05-16 13:07:18 +0000 | [diff] [blame] | 63 | cl::init(""), cl::cat(ClangTidyCategory)); |
Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame] | 64 | |
| 65 | static cl::opt<std::string> |
| 66 | LineFilter("line-filter", |
| 67 | cl::desc("List of files with line ranges to filter the\n" |
| 68 | "warnings. Can be used together with\n" |
| 69 | "-header-filter. The format of the list is a JSON\n" |
| 70 | "array of objects:\n" |
| 71 | " [\n" |
| 72 | " {\"name\":\"file1.cpp\",\"lines\":[[1,3],[5,7]]},\n" |
| 73 | " {\"name\":\"file2.h\"}\n" |
| 74 | " ]"), |
| 75 | cl::init(""), cl::cat(ClangTidyCategory)); |
| 76 | |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 77 | static cl::opt<bool> Fix("fix", cl::desc("Fix detected errors if possible."), |
| 78 | cl::init(false), cl::cat(ClangTidyCategory)); |
| 79 | |
Alexander Kornienko | 3ab3467 | 2014-05-16 13:07:18 +0000 | [diff] [blame] | 80 | static cl::opt<bool> |
| 81 | ListChecks("list-checks", |
| 82 | cl::desc("List all enabled checks and exit. Use with\n" |
| 83 | "-checks='*' to list all available checks."), |
| 84 | cl::init(false), cl::cat(ClangTidyCategory)); |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 85 | |
Alexander Kornienko | 3ab3467 | 2014-05-16 13:07:18 +0000 | [diff] [blame] | 86 | static cl::opt<bool> |
Alexander Kornienko | d53d268 | 2014-09-04 14:23:36 +0000 | [diff] [blame^] | 87 | DumpConfig("dump-config", |
| 88 | cl::desc("Dumps configuration in the YAML format to stdout."), |
| 89 | cl::init(false), cl::cat(ClangTidyCategory)); |
| 90 | |
| 91 | static cl::opt<bool> AnalyzeTemporaryDtors( |
| 92 | "analyze-temporary-dtors", |
| 93 | cl::desc("Enable temporary destructor-aware analysis in\n" |
| 94 | "clang-analyzer- checks.\n" |
| 95 | "This option overrides the value read from a\n" |
| 96 | ".clang-tidy file."), |
| 97 | cl::init(false), cl::cat(ClangTidyCategory)); |
Alex McCarthy | fec08c7 | 2014-04-30 14:09:24 +0000 | [diff] [blame] | 98 | |
Benjamin Kramer | fb98b74 | 2014-09-04 10:31:23 +0000 | [diff] [blame] | 99 | static cl::opt<std::string> ExportFixes( |
| 100 | "export-fixes", |
| 101 | cl::desc("YAML file to store suggested fixes in. The\n" |
| 102 | "stored fixes can be applied to the input source\n" |
| 103 | "code with clang-apply-replacements."), |
| 104 | cl::value_desc("filename"), cl::cat(ClangTidyCategory)); |
| 105 | |
Alexander Kornienko | 5d17454 | 2014-05-07 09:06:53 +0000 | [diff] [blame] | 106 | static void printStats(const clang::tidy::ClangTidyStats &Stats) { |
Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame] | 107 | if (Stats.errorsIgnored()) { |
| 108 | llvm::errs() << "Suppressed " << Stats.errorsIgnored() << " warnings ("; |
Alexander Kornienko | 5d17454 | 2014-05-07 09:06:53 +0000 | [diff] [blame] | 109 | StringRef Separator = ""; |
| 110 | if (Stats.ErrorsIgnoredNonUserCode) { |
| 111 | llvm::errs() << Stats.ErrorsIgnoredNonUserCode << " in non-user code"; |
| 112 | Separator = ", "; |
| 113 | } |
Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame] | 114 | if (Stats.ErrorsIgnoredLineFilter) { |
| 115 | llvm::errs() << Separator << Stats.ErrorsIgnoredLineFilter |
| 116 | << " due to line filter"; |
| 117 | Separator = ", "; |
| 118 | } |
Alexander Kornienko | 5d17454 | 2014-05-07 09:06:53 +0000 | [diff] [blame] | 119 | if (Stats.ErrorsIgnoredNOLINT) { |
| 120 | llvm::errs() << Separator << Stats.ErrorsIgnoredNOLINT << " NOLINT"; |
| 121 | Separator = ", "; |
| 122 | } |
| 123 | if (Stats.ErrorsIgnoredCheckFilter) |
| 124 | llvm::errs() << Separator << Stats.ErrorsIgnoredCheckFilter |
| 125 | << " with check filters"; |
| 126 | llvm::errs() << ").\n"; |
| 127 | if (Stats.ErrorsIgnoredNonUserCode) |
| 128 | llvm::errs() << "Use -header-filter='.*' to display errors from all " |
| 129 | "non-system headers.\n"; |
| 130 | } |
| 131 | } |
| 132 | |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 133 | int main(int argc, const char **argv) { |
Alexander Kornienko | 06ff5a7 | 2013-12-12 10:01:39 +0000 | [diff] [blame] | 134 | CommonOptionsParser OptionsParser(argc, argv, ClangTidyCategory); |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 135 | |
Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 136 | clang::tidy::ClangTidyGlobalOptions GlobalOptions; |
Rafael Espindola | 15c5784 | 2014-06-12 13:32:11 +0000 | [diff] [blame] | 137 | if (std::error_code Err = |
Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 138 | clang::tidy::parseLineFilter(LineFilter, GlobalOptions)) { |
Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame] | 139 | llvm::errs() << "Invalid LineFilter: " << Err.message() << "\n\nUsage:\n"; |
| 140 | llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true); |
| 141 | return 1; |
| 142 | } |
Alexander Kornienko | 33a9bcc | 2014-04-29 15:20:10 +0000 | [diff] [blame] | 143 | |
Alexander Kornienko | d53d268 | 2014-09-04 14:23:36 +0000 | [diff] [blame^] | 144 | clang::tidy::ClangTidyOptions FallbackOptions; |
| 145 | FallbackOptions.Checks = DefaultChecks; |
| 146 | FallbackOptions.HeaderFilterRegex = HeaderFilter; |
| 147 | FallbackOptions.AnalyzeTemporaryDtors = AnalyzeTemporaryDtors; |
Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 148 | |
Alexander Kornienko | d53d268 | 2014-09-04 14:23:36 +0000 | [diff] [blame^] | 149 | clang::tidy::ClangTidyOptions OverrideOptions; |
| 150 | if (Checks.getNumOccurrences() > 0) |
| 151 | OverrideOptions.Checks = Checks; |
| 152 | if (HeaderFilter.getNumOccurrences() > 0) |
| 153 | OverrideOptions.HeaderFilterRegex = HeaderFilter; |
| 154 | if (AnalyzeTemporaryDtors.getNumOccurrences() > 0) |
| 155 | OverrideOptions.AnalyzeTemporaryDtors = AnalyzeTemporaryDtors; |
| 156 | |
| 157 | auto OptionsProvider = llvm::make_unique<clang::tidy::FileOptionsProvider>( |
| 158 | GlobalOptions, FallbackOptions, OverrideOptions); |
| 159 | |
| 160 | std::string FileName = OptionsParser.getSourcePathList().front(); |
| 161 | std::vector<std::string> EnabledChecks = |
| 162 | clang::tidy::getCheckNames(OptionsProvider->getOptions(FileName)); |
Alexander Kornienko | fbf9258 | 2014-06-02 20:32:06 +0000 | [diff] [blame] | 163 | |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 +0000 | [diff] [blame] | 164 | // FIXME: Allow using --list-checks without positional arguments. |
| 165 | if (ListChecks) { |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 +0000 | [diff] [blame] | 166 | llvm::outs() << "Enabled checks:"; |
Alexander Kornienko | fbf9258 | 2014-06-02 20:32:06 +0000 | [diff] [blame] | 167 | for (auto CheckName : EnabledChecks) |
Alexander Kornienko | 16ac6ce | 2014-03-05 13:14:32 +0000 | [diff] [blame] | 168 | llvm::outs() << "\n " << CheckName; |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 +0000 | [diff] [blame] | 169 | llvm::outs() << "\n\n"; |
| 170 | return 0; |
| 171 | } |
| 172 | |
Alexander Kornienko | d53d268 | 2014-09-04 14:23:36 +0000 | [diff] [blame^] | 173 | if (DumpConfig) { |
| 174 | llvm::outs() << clang::tidy::configurationAsText( |
| 175 | clang::tidy::ClangTidyOptions::getDefaults() |
| 176 | .mergeWith(OptionsProvider->getOptions(FileName))) |
| 177 | << "\n"; |
| 178 | return 0; |
| 179 | } |
| 180 | |
Alexander Kornienko | fbf9258 | 2014-06-02 20:32:06 +0000 | [diff] [blame] | 181 | if (EnabledChecks.empty()) { |
| 182 | llvm::errs() << "Error: no checks enabled.\n"; |
| 183 | llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true); |
| 184 | return 1; |
| 185 | } |
| 186 | |
Alexander Kornienko | 826b5ad | 2014-05-09 12:24:09 +0000 | [diff] [blame] | 187 | std::vector<clang::tidy::ClangTidyError> Errors; |
Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 188 | clang::tidy::ClangTidyStats Stats = clang::tidy::runClangTidy( |
Alexander Kornienko | d53d268 | 2014-09-04 14:23:36 +0000 | [diff] [blame^] | 189 | std::move(OptionsProvider), OptionsParser.getCompilations(), |
Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 190 | OptionsParser.getSourcePathList(), &Errors); |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 191 | clang::tidy::handleErrors(Errors, Fix); |
| 192 | |
Benjamin Kramer | fb98b74 | 2014-09-04 10:31:23 +0000 | [diff] [blame] | 193 | if (!ExportFixes.empty()) { |
| 194 | std::error_code EC; |
| 195 | llvm::raw_fd_ostream OS(ExportFixes, EC, llvm::sys::fs::F_None); |
| 196 | if (EC) { |
| 197 | llvm::errs() << "Error opening output file: " << EC.message() << '\n'; |
| 198 | return 1; |
| 199 | } |
| 200 | clang::tidy::exportReplacements(Errors, OS); |
| 201 | } |
| 202 | |
Alexander Kornienko | 5d17454 | 2014-05-07 09:06:53 +0000 | [diff] [blame] | 203 | printStats(Stats); |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 204 | return 0; |
| 205 | } |
Daniel Jasper | 89bbab0 | 2013-08-04 15:56:30 +0000 | [diff] [blame] | 206 | |
| 207 | namespace clang { |
| 208 | namespace tidy { |
| 209 | |
| 210 | // This anchor is used to force the linker to link the LLVMModule. |
| 211 | extern volatile int LLVMModuleAnchorSource; |
| 212 | static int LLVMModuleAnchorDestination = LLVMModuleAnchorSource; |
| 213 | |
| 214 | // This anchor is used to force the linker to link the GoogleModule. |
| 215 | extern volatile int GoogleModuleAnchorSource; |
| 216 | static int GoogleModuleAnchorDestination = GoogleModuleAnchorSource; |
| 217 | |
Alexander Kornienko | 16ac6ce | 2014-03-05 13:14:32 +0000 | [diff] [blame] | 218 | // This anchor is used to force the linker to link the MiscModule. |
| 219 | extern volatile int MiscModuleAnchorSource; |
| 220 | static int MiscModuleAnchorDestination = MiscModuleAnchorSource; |
| 221 | |
Daniel Jasper | 89bbab0 | 2013-08-04 15:56:30 +0000 | [diff] [blame] | 222 | } // namespace tidy |
| 223 | } // namespace clang |