blob: a69ba178f5db62d3ba59b510fffb89e9e85ec60f [file] [log] [blame]
Daniel Jasperd07c8402013-07-29 08:19:24 +00001//===--- 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 Klimek814f9bd2013-11-14 15:49:44 +000019#include "clang/Tooling/CommonOptionsParser.h"
Alexander Kornienkoe9951542014-09-24 18:36:03 +000020#include "llvm/Support/Process.h"
Daniel Jasperd07c8402013-07-29 08:19:24 +000021
22using namespace clang::ast_matchers;
23using namespace clang::driver;
24using namespace clang::tooling;
25using namespace llvm;
26
Alexander Kornienko99c9d6a2014-02-05 13:43:27 +000027static cl::OptionCategory ClangTidyCategory("clang-tidy options");
Daniel Jasperd07c8402013-07-29 08:19:24 +000028
Manuel Klimek814f9bd2013-11-14 15:49:44 +000029static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
Alexander Kornienkod53d2682014-09-04 14:23:36 +000030static 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 Jasperd07c8402013-07-29 08:19:24 +000037
Alexander Kornienko23fe9592014-05-15 14:27:36 +000038const char DefaultChecks[] =
39 "*," // Enable all checks, except these:
40 "-clang-analyzer-alpha*," // Too many false positives.
41 "-llvm-include-order," // Not implemented yet.
Alexander Kornienko23fe9592014-05-15 14:27:36 +000042 "-google-*,"; // Doesn't apply to LLVM.
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000043
Alexander Kornienko23fe9592014-05-15 14:27:36 +000044static cl::opt<std::string>
Alexander Kornienko3ab34672014-05-16 13:07:18 +000045Checks("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 Kornienkod53d2682014-09-04 14:23:36 +000050 "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 Kornienko23fe9592014-05-15 14:27:36 +000053 cl::init(""), cl::cat(ClangTidyCategory));
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000054
Alexander Kornienko3ab34672014-05-16 13:07:18 +000055static cl::opt<std::string>
56HeaderFilter("header-filter",
57 cl::desc("Regular expression matching the names of the\n"
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000058 "headers to output diagnostics from. Diagnostics\n"
59 "from the main file of each translation unit are\n"
60 "always displayed.\n"
Alexander Kornienkod53d2682014-09-04 14:23:36 +000061 "Can be used together with -line-filter.\n"
62 "This option overrides the value read from a\n"
63 ".clang-tidy file."),
Alexander Kornienko3ab34672014-05-16 13:07:18 +000064 cl::init(""), cl::cat(ClangTidyCategory));
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000065
66static cl::opt<std::string>
67LineFilter("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 Jasperd07c8402013-07-29 08:19:24 +000078static cl::opt<bool> Fix("fix", cl::desc("Fix detected errors if possible."),
79 cl::init(false), cl::cat(ClangTidyCategory));
80
Alexander Kornienko3ab34672014-05-16 13:07:18 +000081static cl::opt<bool>
82ListChecks("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 Jasperd07c8402013-07-29 08:19:24 +000086
Alexander Kornienkoeff4e922014-09-26 11:42:29 +000087static 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 Kornienko3ab34672014-05-16 13:07:18 +000096static cl::opt<bool>
Alexander Kornienkod53d2682014-09-04 14:23:36 +000097DumpConfig("dump-config",
98 cl::desc("Dumps configuration in the YAML format to stdout."),
99 cl::init(false), cl::cat(ClangTidyCategory));
100
101static 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 McCarthyfec08c72014-04-30 14:09:24 +0000108
Benjamin Kramerfb98b742014-09-04 10:31:23 +0000109static 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 Kornienkoc28c32d2014-09-10 11:43:09 +0000116namespace clang {
117namespace tidy {
118
119static void printStats(const ClangTidyStats &Stats) {
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000120 if (Stats.errorsIgnored()) {
121 llvm::errs() << "Suppressed " << Stats.errorsIgnored() << " warnings (";
Alexander Kornienko5d174542014-05-07 09:06:53 +0000122 StringRef Separator = "";
123 if (Stats.ErrorsIgnoredNonUserCode) {
124 llvm::errs() << Stats.ErrorsIgnoredNonUserCode << " in non-user code";
125 Separator = ", ";
126 }
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000127 if (Stats.ErrorsIgnoredLineFilter) {
128 llvm::errs() << Separator << Stats.ErrorsIgnoredLineFilter
129 << " due to line filter";
130 Separator = ", ";
131 }
Alexander Kornienko5d174542014-05-07 09:06:53 +0000132 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 Kornienkoeff4e922014-09-26 11:42:29 +0000146std::unique_ptr<ClangTidyOptionsProvider> createOptionsProvider() {
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000147 ClangTidyGlobalOptions GlobalOptions;
148 if (std::error_code Err = parseLineFilter(LineFilter, GlobalOptions)) {
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000149 llvm::errs() << "Invalid LineFilter: " << Err.message() << "\n\nUsage:\n";
150 llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true);
Alexander Kornienkoeff4e922014-09-26 11:42:29 +0000151 return nullptr;
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000152 }
Alexander Kornienko33a9bcc2014-04-29 15:20:10 +0000153
Alexander Kornienkoe9951542014-09-24 18:36:03 +0000154 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 Kornienkoa4695222014-06-05 13:31:45 +0000162
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000163 ClangTidyOptions OverrideOptions;
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000164 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 Kornienkoeff4e922014-09-26 11:42:29 +0000171 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
189int clangTidyMain(int argc, const char **argv) {
190 CommonOptionsParser OptionsParser(argc, argv, ClangTidyCategory);
191
192 auto OptionsProvider = createOptionsProvider();
193 if (!OptionsProvider)
194 return 1;
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000195
196 std::string FileName = OptionsParser.getSourcePathList().front();
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000197 ClangTidyOptions EffectiveOptions = OptionsProvider->getOptions(FileName);
198 std::vector<std::string> EnabledChecks = getCheckNames(EffectiveOptions);
Alexander Kornienkofbf92582014-06-02 20:32:06 +0000199
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +0000200 // FIXME: Allow using --list-checks without positional arguments.
201 if (ListChecks) {
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +0000202 llvm::outs() << "Enabled checks:";
Alexander Kornienkofbf92582014-06-02 20:32:06 +0000203 for (auto CheckName : EnabledChecks)
Alexander Kornienko16ac6ce2014-03-05 13:14:32 +0000204 llvm::outs() << "\n " << CheckName;
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +0000205 llvm::outs() << "\n\n";
206 return 0;
207 }
208
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000209 if (DumpConfig) {
Alexander Kornienko6e0cbc82014-09-12 08:53:36 +0000210 EffectiveOptions.CheckOptions = getCheckOptions(EffectiveOptions);
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000211 llvm::outs() << configurationAsText(ClangTidyOptions::getDefaults()
212 .mergeWith(EffectiveOptions))
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000213 << "\n";
214 return 0;
215 }
216
Alexander Kornienkofbf92582014-06-02 20:32:06 +0000217 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 Kornienkoc28c32d2014-09-10 11:43:09 +0000223 std::vector<ClangTidyError> Errors;
224 ClangTidyStats Stats =
225 runClangTidy(std::move(OptionsProvider), OptionsParser.getCompilations(),
226 OptionsParser.getSourcePathList(), &Errors);
227 handleErrors(Errors, Fix);
Daniel Jasperd07c8402013-07-29 08:19:24 +0000228
Alexander Kornienko4153da22014-09-04 15:19:49 +0000229 if (!ExportFixes.empty() && !Errors.empty()) {
Benjamin Kramerfb98b742014-09-04 10:31:23 +0000230 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 Kornienkoc28c32d2014-09-10 11:43:09 +0000236 exportReplacements(Errors, OS);
Benjamin Kramerfb98b742014-09-04 10:31:23 +0000237 }
238
Alexander Kornienko5d174542014-05-07 09:06:53 +0000239 printStats(Stats);
Daniel Jasperd07c8402013-07-29 08:19:24 +0000240 return 0;
241}
Daniel Jasper89bbab02013-08-04 15:56:30 +0000242
Daniel Jasper89bbab02013-08-04 15:56:30 +0000243// This anchor is used to force the linker to link the LLVMModule.
244extern volatile int LLVMModuleAnchorSource;
245static int LLVMModuleAnchorDestination = LLVMModuleAnchorSource;
246
247// This anchor is used to force the linker to link the GoogleModule.
248extern volatile int GoogleModuleAnchorSource;
249static int GoogleModuleAnchorDestination = GoogleModuleAnchorSource;
250
Alexander Kornienko16ac6ce2014-03-05 13:14:32 +0000251// This anchor is used to force the linker to link the MiscModule.
252extern volatile int MiscModuleAnchorSource;
253static int MiscModuleAnchorDestination = MiscModuleAnchorSource;
254
Daniel Jasper89bbab02013-08-04 15:56:30 +0000255} // namespace tidy
256} // namespace clang
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000257
258int main(int argc, const char **argv) {
259 return clang::tidy::clangTidyMain(argc, argv);
260}