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