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 | c28c32d | 2014-09-10 11:43:09 +0000 | [diff] [blame] | 106 | namespace clang { |
| 107 | namespace tidy { |
| 108 | |
| 109 | static void printStats(const ClangTidyStats &Stats) { |
Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame] | 110 | if (Stats.errorsIgnored()) { |
| 111 | llvm::errs() << "Suppressed " << Stats.errorsIgnored() << " warnings ("; |
Alexander Kornienko | 5d17454 | 2014-05-07 09:06:53 +0000 | [diff] [blame] | 112 | StringRef Separator = ""; |
| 113 | if (Stats.ErrorsIgnoredNonUserCode) { |
| 114 | llvm::errs() << Stats.ErrorsIgnoredNonUserCode << " in non-user code"; |
| 115 | Separator = ", "; |
| 116 | } |
Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame] | 117 | if (Stats.ErrorsIgnoredLineFilter) { |
| 118 | llvm::errs() << Separator << Stats.ErrorsIgnoredLineFilter |
| 119 | << " due to line filter"; |
| 120 | Separator = ", "; |
| 121 | } |
Alexander Kornienko | 5d17454 | 2014-05-07 09:06:53 +0000 | [diff] [blame] | 122 | if (Stats.ErrorsIgnoredNOLINT) { |
| 123 | llvm::errs() << Separator << Stats.ErrorsIgnoredNOLINT << " NOLINT"; |
| 124 | Separator = ", "; |
| 125 | } |
| 126 | if (Stats.ErrorsIgnoredCheckFilter) |
| 127 | llvm::errs() << Separator << Stats.ErrorsIgnoredCheckFilter |
| 128 | << " with check filters"; |
| 129 | llvm::errs() << ").\n"; |
| 130 | if (Stats.ErrorsIgnoredNonUserCode) |
| 131 | llvm::errs() << "Use -header-filter='.*' to display errors from all " |
| 132 | "non-system headers.\n"; |
| 133 | } |
| 134 | } |
| 135 | |
Alexander Kornienko | c28c32d | 2014-09-10 11:43:09 +0000 | [diff] [blame] | 136 | int clangTidyMain(int argc, const char **argv) { |
Alexander Kornienko | 06ff5a7 | 2013-12-12 10:01:39 +0000 | [diff] [blame] | 137 | CommonOptionsParser OptionsParser(argc, argv, ClangTidyCategory); |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 138 | |
Alexander Kornienko | c28c32d | 2014-09-10 11:43:09 +0000 | [diff] [blame] | 139 | ClangTidyGlobalOptions GlobalOptions; |
| 140 | if (std::error_code Err = parseLineFilter(LineFilter, GlobalOptions)) { |
Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame] | 141 | llvm::errs() << "Invalid LineFilter: " << Err.message() << "\n\nUsage:\n"; |
| 142 | llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true); |
| 143 | return 1; |
| 144 | } |
Alexander Kornienko | 33a9bcc | 2014-04-29 15:20:10 +0000 | [diff] [blame] | 145 | |
Alexander Kornienko | c28c32d | 2014-09-10 11:43:09 +0000 | [diff] [blame] | 146 | ClangTidyOptions FallbackOptions; |
Alexander Kornienko | d53d268 | 2014-09-04 14:23:36 +0000 | [diff] [blame] | 147 | FallbackOptions.Checks = DefaultChecks; |
| 148 | FallbackOptions.HeaderFilterRegex = HeaderFilter; |
| 149 | FallbackOptions.AnalyzeTemporaryDtors = AnalyzeTemporaryDtors; |
Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 150 | |
Alexander Kornienko | c28c32d | 2014-09-10 11:43:09 +0000 | [diff] [blame] | 151 | ClangTidyOptions OverrideOptions; |
Alexander Kornienko | d53d268 | 2014-09-04 14:23:36 +0000 | [diff] [blame] | 152 | if (Checks.getNumOccurrences() > 0) |
| 153 | OverrideOptions.Checks = Checks; |
| 154 | if (HeaderFilter.getNumOccurrences() > 0) |
| 155 | OverrideOptions.HeaderFilterRegex = HeaderFilter; |
| 156 | if (AnalyzeTemporaryDtors.getNumOccurrences() > 0) |
| 157 | OverrideOptions.AnalyzeTemporaryDtors = AnalyzeTemporaryDtors; |
| 158 | |
Alexander Kornienko | c28c32d | 2014-09-10 11:43:09 +0000 | [diff] [blame] | 159 | auto OptionsProvider = llvm::make_unique<FileOptionsProvider>( |
Alexander Kornienko | d53d268 | 2014-09-04 14:23:36 +0000 | [diff] [blame] | 160 | GlobalOptions, FallbackOptions, OverrideOptions); |
| 161 | |
| 162 | std::string FileName = OptionsParser.getSourcePathList().front(); |
Alexander Kornienko | c28c32d | 2014-09-10 11:43:09 +0000 | [diff] [blame] | 163 | ClangTidyOptions EffectiveOptions = OptionsProvider->getOptions(FileName); |
| 164 | std::vector<std::string> EnabledChecks = getCheckNames(EffectiveOptions); |
Alexander Kornienko | fbf9258 | 2014-06-02 20:32:06 +0000 | [diff] [blame] | 165 | |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 +0000 | [diff] [blame] | 166 | // FIXME: Allow using --list-checks without positional arguments. |
| 167 | if (ListChecks) { |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 +0000 | [diff] [blame] | 168 | llvm::outs() << "Enabled checks:"; |
Alexander Kornienko | fbf9258 | 2014-06-02 20:32:06 +0000 | [diff] [blame] | 169 | for (auto CheckName : EnabledChecks) |
Alexander Kornienko | 16ac6ce | 2014-03-05 13:14:32 +0000 | [diff] [blame] | 170 | llvm::outs() << "\n " << CheckName; |
Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 +0000 | [diff] [blame] | 171 | llvm::outs() << "\n\n"; |
| 172 | return 0; |
| 173 | } |
| 174 | |
Alexander Kornienko | d53d268 | 2014-09-04 14:23:36 +0000 | [diff] [blame] | 175 | if (DumpConfig) { |
Alexander Kornienko | 6e0cbc8 | 2014-09-12 08:53:36 +0000 | [diff] [blame^] | 176 | EffectiveOptions.CheckOptions = getCheckOptions(EffectiveOptions); |
Alexander Kornienko | c28c32d | 2014-09-10 11:43:09 +0000 | [diff] [blame] | 177 | llvm::outs() << configurationAsText(ClangTidyOptions::getDefaults() |
| 178 | .mergeWith(EffectiveOptions)) |
Alexander Kornienko | d53d268 | 2014-09-04 14:23:36 +0000 | [diff] [blame] | 179 | << "\n"; |
| 180 | return 0; |
| 181 | } |
| 182 | |
Alexander Kornienko | fbf9258 | 2014-06-02 20:32:06 +0000 | [diff] [blame] | 183 | if (EnabledChecks.empty()) { |
| 184 | llvm::errs() << "Error: no checks enabled.\n"; |
| 185 | llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true); |
| 186 | return 1; |
| 187 | } |
| 188 | |
Alexander Kornienko | c28c32d | 2014-09-10 11:43:09 +0000 | [diff] [blame] | 189 | std::vector<ClangTidyError> Errors; |
| 190 | ClangTidyStats Stats = |
| 191 | runClangTidy(std::move(OptionsProvider), OptionsParser.getCompilations(), |
| 192 | OptionsParser.getSourcePathList(), &Errors); |
| 193 | handleErrors(Errors, Fix); |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 194 | |
Alexander Kornienko | 4153da2 | 2014-09-04 15:19:49 +0000 | [diff] [blame] | 195 | if (!ExportFixes.empty() && !Errors.empty()) { |
Benjamin Kramer | fb98b74 | 2014-09-04 10:31:23 +0000 | [diff] [blame] | 196 | std::error_code EC; |
| 197 | llvm::raw_fd_ostream OS(ExportFixes, EC, llvm::sys::fs::F_None); |
| 198 | if (EC) { |
| 199 | llvm::errs() << "Error opening output file: " << EC.message() << '\n'; |
| 200 | return 1; |
| 201 | } |
Alexander Kornienko | c28c32d | 2014-09-10 11:43:09 +0000 | [diff] [blame] | 202 | exportReplacements(Errors, OS); |
Benjamin Kramer | fb98b74 | 2014-09-04 10:31:23 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Alexander Kornienko | 5d17454 | 2014-05-07 09:06:53 +0000 | [diff] [blame] | 205 | printStats(Stats); |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 206 | return 0; |
| 207 | } |
Daniel Jasper | 89bbab0 | 2013-08-04 15:56:30 +0000 | [diff] [blame] | 208 | |
Daniel Jasper | 89bbab0 | 2013-08-04 15:56:30 +0000 | [diff] [blame] | 209 | // This anchor is used to force the linker to link the LLVMModule. |
| 210 | extern volatile int LLVMModuleAnchorSource; |
| 211 | static int LLVMModuleAnchorDestination = LLVMModuleAnchorSource; |
| 212 | |
| 213 | // This anchor is used to force the linker to link the GoogleModule. |
| 214 | extern volatile int GoogleModuleAnchorSource; |
| 215 | static int GoogleModuleAnchorDestination = GoogleModuleAnchorSource; |
| 216 | |
Alexander Kornienko | 16ac6ce | 2014-03-05 13:14:32 +0000 | [diff] [blame] | 217 | // This anchor is used to force the linker to link the MiscModule. |
| 218 | extern volatile int MiscModuleAnchorSource; |
| 219 | static int MiscModuleAnchorDestination = MiscModuleAnchorSource; |
| 220 | |
Daniel Jasper | 89bbab0 | 2013-08-04 15:56:30 +0000 | [diff] [blame] | 221 | } // namespace tidy |
| 222 | } // namespace clang |
Alexander Kornienko | c28c32d | 2014-09-10 11:43:09 +0000 | [diff] [blame] | 223 | |
| 224 | int main(int argc, const char **argv) { |
| 225 | return clang::tidy::clangTidyMain(argc, argv); |
| 226 | } |