blob: 79c0d0b3b381cd260b4bd1b1fc75902e94d98f1c [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"
Zachary Turnerfbdca1d2017-10-20 23:00:51 +000021#include "llvm/Support/TargetSelect.h"
Daniel Jasperd07c8402013-07-29 08:19:24 +000022
23using namespace clang::ast_matchers;
24using namespace clang::driver;
25using namespace clang::tooling;
26using namespace llvm;
27
Alexander Kornienko99c9d6a2014-02-05 13:43:27 +000028static cl::OptionCategory ClangTidyCategory("clang-tidy options");
Daniel Jasperd07c8402013-07-29 08:19:24 +000029
Manuel Klimek814f9bd2013-11-14 15:49:44 +000030static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +000031static cl::extrahelp ClangTidyHelp(R"(
32Configuration files:
33 clang-tidy attempts to read configuration for each source file from a
34 .clang-tidy file located in the closest parent directory of the source
35 file. If any configuration options have a corresponding command-line
36 option, command-line option takes precedence. The effective
37 configuration can be inspected using -dump-config:
38
Alexander Kornienko215604c2017-04-06 14:27:00 +000039 $ clang-tidy -dump-config
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +000040 ---
41 Checks: '-*,some-check'
42 WarningsAsErrors: ''
43 HeaderFilterRegex: ''
44 AnalyzeTemporaryDtors: false
Alexander Kornienko215604c2017-04-06 14:27:00 +000045 FormatStyle: none
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +000046 User: user
47 CheckOptions:
48 - key: some-check.SomeOption
49 value: 'some value'
50 ...
51
52)");
Daniel Jasperd07c8402013-07-29 08:19:24 +000053
Jonas Devlieghere73191842016-11-30 18:06:42 +000054const char DefaultChecks[] = // Enable these checks by default:
55 "clang-diagnostic-*," // * compiler diagnostics
56 "clang-analyzer-*"; // * Static Analyzer checks
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000057
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +000058static cl::opt<std::string> Checks("checks", cl::desc(R"(
59Comma-separated list of globs with optional '-'
60prefix. Globs are processed in order of
61appearance in the list. Globs without '-'
62prefix add checks with matching names to the
63set, globs with the '-' prefix remove checks
64with matching names from the set of enabled
Alexander Kornienko17a4b232017-03-03 11:16:34 +000065checks. This option's value is appended to the
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +000066value of the 'Checks' option in .clang-tidy
67file, if any.
68)"),
69 cl::init(""), cl::cat(ClangTidyCategory));
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000070
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +000071static cl::opt<std::string> WarningsAsErrors("warnings-as-errors", cl::desc(R"(
72Upgrades warnings to errors. Same format as
73'-checks'.
74This option's value is appended to the value of
75the 'WarningsAsErrors' option in .clang-tidy
76file, if any.
77)"),
78 cl::init(""),
79 cl::cat(ClangTidyCategory));
Jonathan Roelofsd60388a2016-01-13 17:36:41 +000080
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +000081static cl::opt<std::string> HeaderFilter("header-filter", cl::desc(R"(
82Regular expression matching the names of the
83headers to output diagnostics from. Diagnostics
84from the main file of each translation unit are
85always displayed.
86Can be used together with -line-filter.
87This option overrides the 'HeaderFilter' option
88in .clang-tidy file, if any.
89)"),
90 cl::init(""),
91 cl::cat(ClangTidyCategory));
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000092
Alexander Kornienko37f7abe2014-10-28 22:16:13 +000093static cl::opt<bool>
94 SystemHeaders("system-headers",
Alexander Kornienko5eac3c62014-11-03 14:06:31 +000095 cl::desc("Display the errors from system headers."),
Alexander Kornienko37f7abe2014-10-28 22:16:13 +000096 cl::init(false), cl::cat(ClangTidyCategory));
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +000097static cl::opt<std::string> LineFilter("line-filter", cl::desc(R"(
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +000098List of files with line ranges to filter the
99warnings. Can be used together with
100-header-filter. The format of the list is a
101JSON array of objects:
102 [
103 {"name":"file1.cpp","lines":[[1,3],[5,7]]},
104 {"name":"file2.h"}
105 ]
106)"),
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000107 cl::init(""),
108 cl::cat(ClangTidyCategory));
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000109
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +0000110static cl::opt<bool> Fix("fix", cl::desc(R"(
111Apply suggested fixes. Without -fix-errors
112clang-tidy will bail out if any compilation
113errors were found.
114)"),
115 cl::init(false), cl::cat(ClangTidyCategory));
Alexander Kornienko5eac3c62014-11-03 14:06:31 +0000116
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +0000117static cl::opt<bool> FixErrors("fix-errors", cl::desc(R"(
118Apply suggested fixes even if compilation
119errors were found. If compiler errors have
120attached fix-its, clang-tidy will apply them as
121well.
122)"),
123 cl::init(false), cl::cat(ClangTidyCategory));
Daniel Jasperd07c8402013-07-29 08:19:24 +0000124
Alexander Kornienko17a4b232017-03-03 11:16:34 +0000125static cl::opt<std::string> FormatStyle("format-style", cl::desc(R"(
126Style for formatting code around applied fixes:
127 - 'none' (default) turns off formatting
128 - 'file' (literally 'file', not a placeholder)
129 uses .clang-format file in the closest parent
130 directory
131 - '{ <json> }' specifies options inline, e.g.
132 -format-style='{BasedOnStyle: llvm, IndentWidth: 8}'
133 - 'llvm', 'google', 'webkit', 'mozilla'
134See clang-format documentation for the up-to-date
135information about formatting styles and options.
Alexander Kornienko215604c2017-04-06 14:27:00 +0000136This option overrides the 'FormatStyle` option in
137.clang-tidy file, if any.
Jonas Devlieghere73191842016-11-30 18:06:42 +0000138)"),
Alexander Kornienko17a4b232017-03-03 11:16:34 +0000139 cl::init("none"),
140 cl::cat(ClangTidyCategory));
Jonas Devlieghere73191842016-11-30 18:06:42 +0000141
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +0000142static cl::opt<bool> ListChecks("list-checks", cl::desc(R"(
143List all enabled checks and exit. Use with
144-checks=* to list all available checks.
145)"),
146 cl::init(false), cl::cat(ClangTidyCategory));
Daniel Jasperd07c8402013-07-29 08:19:24 +0000147
Haojian Wu12e6b8f2016-04-27 09:15:01 +0000148static cl::opt<bool> ExplainConfig("explain-config", cl::desc(R"(
Haojian Wue5555e22016-09-22 14:36:43 +0000149For each enabled check explains, where it is
150enabled, i.e. in clang-tidy binary, command
151line or a specific configuration file.
Haojian Wu12e6b8f2016-04-27 09:15:01 +0000152)"),
153 cl::init(false), cl::cat(ClangTidyCategory));
154
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +0000155static cl::opt<std::string> Config("config", cl::desc(R"(
156Specifies a configuration in YAML/JSON format:
157 -config="{Checks: '*',
158 CheckOptions: [{key: x,
159 value: y}]}"
160When the value is empty, clang-tidy will
161attempt to find a file named .clang-tidy for
162each source file in its parent directories.
163)"),
164 cl::init(""), cl::cat(ClangTidyCategory));
Alexander Kornienkoeff4e922014-09-26 11:42:29 +0000165
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +0000166static cl::opt<bool> DumpConfig("dump-config", cl::desc(R"(
167Dumps configuration in the YAML format to
168stdout. This option can be used along with a
169file name (and '--' if the file is outside of a
170project with configured compilation database).
171The configuration used for this file will be
172printed.
173Use along with -checks=* to include
174configuration of all checks.
175)"),
176 cl::init(false), cl::cat(ClangTidyCategory));
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000177
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +0000178static cl::opt<bool> EnableCheckProfile("enable-check-profile", cl::desc(R"(
179Enable per-check timing profiles, and print a
180report to stderr.
181)"),
182 cl::init(false),
183 cl::cat(ClangTidyCategory));
Samuel Benzaquenaedd9942014-10-23 17:23:20 +0000184
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +0000185static cl::opt<bool> AnalyzeTemporaryDtors("analyze-temporary-dtors",
186 cl::desc(R"(
187Enable temporary destructor-aware analysis in
188clang-analyzer- checks.
189This option overrides the value read from a
190.clang-tidy file.
191)"),
192 cl::init(false),
193 cl::cat(ClangTidyCategory));
Alex McCarthyfec08c72014-04-30 14:09:24 +0000194
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +0000195static cl::opt<std::string> ExportFixes("export-fixes", cl::desc(R"(
196YAML file to store suggested fixes in. The
Kirill Bobyrev405699e2016-08-19 09:36:14 +0000197stored fixes can be applied to the input source
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +0000198code with clang-apply-replacements.
199)"),
200 cl::value_desc("filename"),
201 cl::cat(ClangTidyCategory));
Benjamin Kramerfb98b742014-09-04 10:31:23 +0000202
Ehsan Akhgarib7418d32017-02-09 18:32:02 +0000203static cl::opt<bool> Quiet("quiet", cl::desc(R"(
Alexander Kornienko17a4b232017-03-03 11:16:34 +0000204Run clang-tidy in quiet mode. This suppresses
Ehsan Akhgarib7418d32017-02-09 18:32:02 +0000205printing statistics about ignored warnings and
206warnings treated as errors if the respective
207options are specified.
208)"),
209 cl::init(false),
210 cl::cat(ClangTidyCategory));
211
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000212namespace clang {
213namespace tidy {
214
215static void printStats(const ClangTidyStats &Stats) {
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000216 if (Stats.errorsIgnored()) {
217 llvm::errs() << "Suppressed " << Stats.errorsIgnored() << " warnings (";
Alexander Kornienko5d174542014-05-07 09:06:53 +0000218 StringRef Separator = "";
219 if (Stats.ErrorsIgnoredNonUserCode) {
220 llvm::errs() << Stats.ErrorsIgnoredNonUserCode << " in non-user code";
221 Separator = ", ";
222 }
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000223 if (Stats.ErrorsIgnoredLineFilter) {
224 llvm::errs() << Separator << Stats.ErrorsIgnoredLineFilter
225 << " due to line filter";
226 Separator = ", ";
227 }
Alexander Kornienko5d174542014-05-07 09:06:53 +0000228 if (Stats.ErrorsIgnoredNOLINT) {
229 llvm::errs() << Separator << Stats.ErrorsIgnoredNOLINT << " NOLINT";
230 Separator = ", ";
231 }
232 if (Stats.ErrorsIgnoredCheckFilter)
233 llvm::errs() << Separator << Stats.ErrorsIgnoredCheckFilter
234 << " with check filters";
235 llvm::errs() << ").\n";
236 if (Stats.ErrorsIgnoredNonUserCode)
Aaron Ballman5a4892b2015-07-27 13:41:30 +0000237 llvm::errs() << "Use -header-filter=.* to display errors from all "
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +0000238 "non-system headers. Use -system-headers to display "
239 "errors from system headers as well.\n";
Alexander Kornienko5d174542014-05-07 09:06:53 +0000240 }
241}
242
Samuel Benzaquenaedd9942014-10-23 17:23:20 +0000243static void printProfileData(const ProfileData &Profile,
244 llvm::raw_ostream &OS) {
245 // Time is first to allow for sorting by it.
246 std::vector<std::pair<llvm::TimeRecord, StringRef>> Timers;
247 TimeRecord Total;
248
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000249 for (const auto &P : Profile.Records) {
Samuel Benzaquenaedd9942014-10-23 17:23:20 +0000250 Timers.emplace_back(P.getValue(), P.getKey());
251 Total += P.getValue();
252 }
253
254 std::sort(Timers.begin(), Timers.end());
255
256 std::string Line = "===" + std::string(73, '-') + "===\n";
257 OS << Line;
258
259 if (Total.getUserTime())
260 OS << " ---User Time---";
261 if (Total.getSystemTime())
262 OS << " --System Time--";
263 if (Total.getProcessTime())
264 OS << " --User+System--";
265 OS << " ---Wall Time---";
266 if (Total.getMemUsed())
267 OS << " ---Mem---";
268 OS << " --- Name ---\n";
269
270 // Loop through all of the timing data, printing it out.
271 for (auto I = Timers.rbegin(), E = Timers.rend(); I != E; ++I) {
272 I->first.print(Total, OS);
273 OS << I->second << '\n';
274 }
275
276 Total.print(Total, OS);
277 OS << "Total\n";
278 OS << Line << "\n";
279 OS.flush();
280}
281
Benjamin Kramere7103712015-03-23 12:49:15 +0000282static std::unique_ptr<ClangTidyOptionsProvider> createOptionsProvider() {
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000283 ClangTidyGlobalOptions GlobalOptions;
284 if (std::error_code Err = parseLineFilter(LineFilter, GlobalOptions)) {
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000285 llvm::errs() << "Invalid LineFilter: " << Err.message() << "\n\nUsage:\n";
286 llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true);
Alexander Kornienkoeff4e922014-09-26 11:42:29 +0000287 return nullptr;
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000288 }
Alexander Kornienko33a9bcc2014-04-29 15:20:10 +0000289
Alexander Kornienkoe9951542014-09-24 18:36:03 +0000290 ClangTidyOptions DefaultOptions;
291 DefaultOptions.Checks = DefaultChecks;
Jonathan Roelofsd60388a2016-01-13 17:36:41 +0000292 DefaultOptions.WarningsAsErrors = "";
Alexander Kornienkoe9951542014-09-24 18:36:03 +0000293 DefaultOptions.HeaderFilterRegex = HeaderFilter;
Alexander Kornienko37f7abe2014-10-28 22:16:13 +0000294 DefaultOptions.SystemHeaders = SystemHeaders;
Alexander Kornienkoe9951542014-09-24 18:36:03 +0000295 DefaultOptions.AnalyzeTemporaryDtors = AnalyzeTemporaryDtors;
Alexander Kornienko25613202017-04-06 13:41:29 +0000296 DefaultOptions.FormatStyle = FormatStyle;
Alexander Kornienkoe9951542014-09-24 18:36:03 +0000297 DefaultOptions.User = llvm::sys::Process::GetEnv("USER");
298 // USERNAME is used on Windows.
299 if (!DefaultOptions.User)
300 DefaultOptions.User = llvm::sys::Process::GetEnv("USERNAME");
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000301
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000302 ClangTidyOptions OverrideOptions;
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000303 if (Checks.getNumOccurrences() > 0)
304 OverrideOptions.Checks = Checks;
Jonathan Roelofsd60388a2016-01-13 17:36:41 +0000305 if (WarningsAsErrors.getNumOccurrences() > 0)
306 OverrideOptions.WarningsAsErrors = WarningsAsErrors;
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000307 if (HeaderFilter.getNumOccurrences() > 0)
308 OverrideOptions.HeaderFilterRegex = HeaderFilter;
Alexander Kornienko37f7abe2014-10-28 22:16:13 +0000309 if (SystemHeaders.getNumOccurrences() > 0)
310 OverrideOptions.SystemHeaders = SystemHeaders;
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000311 if (AnalyzeTemporaryDtors.getNumOccurrences() > 0)
312 OverrideOptions.AnalyzeTemporaryDtors = AnalyzeTemporaryDtors;
Alexander Kornienko25613202017-04-06 13:41:29 +0000313 if (FormatStyle.getNumOccurrences() > 0)
314 OverrideOptions.FormatStyle = FormatStyle;
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000315
Alexander Kornienkoeff4e922014-09-26 11:42:29 +0000316 if (!Config.empty()) {
317 if (llvm::ErrorOr<ClangTidyOptions> ParsedConfig =
318 parseConfiguration(Config)) {
Haojian Wu12e6b8f2016-04-27 09:15:01 +0000319 return llvm::make_unique<ConfigOptionsProvider>(
320 GlobalOptions,
321 ClangTidyOptions::getDefaults().mergeWith(DefaultOptions),
322 *ParsedConfig, OverrideOptions);
Alexander Kornienkoeff4e922014-09-26 11:42:29 +0000323 } else {
324 llvm::errs() << "Error: invalid configuration specified.\n"
325 << ParsedConfig.getError().message() << "\n";
326 return nullptr;
327 }
328 }
329 return llvm::make_unique<FileOptionsProvider>(GlobalOptions, DefaultOptions,
330 OverrideOptions);
331}
332
Benjamin Kramere7103712015-03-23 12:49:15 +0000333static int clangTidyMain(int argc, const char **argv) {
Alexander Kornienko65eccb42015-08-17 10:03:27 +0000334 CommonOptionsParser OptionsParser(argc, argv, ClangTidyCategory,
335 cl::ZeroOrMore);
Alexander Kornienkoeff4e922014-09-26 11:42:29 +0000336
Alexander Kornienko25613202017-04-06 13:41:29 +0000337 auto OwningOptionsProvider = createOptionsProvider();
338 auto *OptionsProvider = OwningOptionsProvider.get();
Alexander Kornienkoeff4e922014-09-26 11:42:29 +0000339 if (!OptionsProvider)
340 return 1;
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000341
Alexander Kornienko65eccb42015-08-17 10:03:27 +0000342 StringRef FileName("dummy");
343 auto PathList = OptionsParser.getSourcePathList();
344 if (!PathList.empty()) {
Alexander Kornienkoe0c900e2015-08-17 11:27:11 +0000345 FileName = PathList.front();
Alexander Kornienko65eccb42015-08-17 10:03:27 +0000346 }
Haojian Wud1218752016-07-11 07:47:04 +0000347
348 SmallString<256> FilePath(FileName);
349 if (std::error_code EC = llvm::sys::fs::make_absolute(FilePath)) {
350 llvm::errs() << "Can't make absolute path from " << FileName << ": "
351 << EC.message() << "\n";
352 }
353 ClangTidyOptions EffectiveOptions = OptionsProvider->getOptions(FilePath);
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000354 std::vector<std::string> EnabledChecks = getCheckNames(EffectiveOptions);
Alexander Kornienkofbf92582014-06-02 20:32:06 +0000355
Haojian Wu12e6b8f2016-04-27 09:15:01 +0000356 if (ExplainConfig) {
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000357 // FIXME: Show other ClangTidyOptions' fields, like ExtraArg.
Haojian Wu12e6b8f2016-04-27 09:15:01 +0000358 std::vector<clang::tidy::ClangTidyOptionsProvider::OptionsSource>
Haojian Wud1218752016-07-11 07:47:04 +0000359 RawOptions = OptionsProvider->getRawOptions(FilePath);
Haojian Wu12e6b8f2016-04-27 09:15:01 +0000360 for (const std::string &Check : EnabledChecks) {
361 for (auto It = RawOptions.rbegin(); It != RawOptions.rend(); ++It) {
362 if (It->first.Checks && GlobList(*It->first.Checks).contains(Check)) {
363 llvm::outs() << "'" << Check << "' is enabled in the " << It->second
364 << ".\n";
365 break;
366 }
367 }
368 }
369 return 0;
370 }
371
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +0000372 if (ListChecks) {
Alexander Kornienko493db092016-04-27 11:45:14 +0000373 if (EnabledChecks.empty()) {
374 llvm::errs() << "No checks enabled.\n";
375 return 1;
376 }
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +0000377 llvm::outs() << "Enabled checks:";
Benjamin Kramer51a9cc92016-06-15 15:46:10 +0000378 for (const auto &CheckName : EnabledChecks)
Alexander Kornienko16ac6ce2014-03-05 13:14:32 +0000379 llvm::outs() << "\n " << CheckName;
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +0000380 llvm::outs() << "\n\n";
381 return 0;
382 }
383
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000384 if (DumpConfig) {
Alexander Kornienko6e0cbc82014-09-12 08:53:36 +0000385 EffectiveOptions.CheckOptions = getCheckOptions(EffectiveOptions);
Alexander Kornienko65eccb42015-08-17 10:03:27 +0000386 llvm::outs() << configurationAsText(
387 ClangTidyOptions::getDefaults().mergeWith(
388 EffectiveOptions))
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000389 << "\n";
390 return 0;
391 }
392
Alexander Kornienkofbf92582014-06-02 20:32:06 +0000393 if (EnabledChecks.empty()) {
394 llvm::errs() << "Error: no checks enabled.\n";
395 llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true);
Rafael Espindolad63b2f32017-09-08 00:33:39 +0000396 return 0;
Alexander Kornienkofbf92582014-06-02 20:32:06 +0000397 }
398
Alexander Kornienko65eccb42015-08-17 10:03:27 +0000399 if (PathList.empty()) {
400 llvm::errs() << "Error: no input files specified.\n";
401 llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true);
Rafael Espindolad63b2f32017-09-08 00:33:39 +0000402 return 0;
Alexander Kornienko65eccb42015-08-17 10:03:27 +0000403 }
404
Samuel Benzaquenaedd9942014-10-23 17:23:20 +0000405 ProfileData Profile;
406
Zachary Turnerfbdca1d2017-10-20 23:00:51 +0000407 llvm::InitializeAllTargetInfos();
408 llvm::InitializeAllTargetMCs();
409 llvm::InitializeAllAsmParsers();
410
Alexander Kornienko25613202017-04-06 13:41:29 +0000411 ClangTidyContext Context(std::move(OwningOptionsProvider));
412 runClangTidy(Context, OptionsParser.getCompilations(), PathList,
413 EnableCheckProfile ? &Profile : nullptr);
414 ArrayRef<ClangTidyError> Errors = Context.getErrors();
Alexander Kornienko5eac3c62014-11-03 14:06:31 +0000415 bool FoundErrors =
416 std::find_if(Errors.begin(), Errors.end(), [](const ClangTidyError &E) {
417 return E.DiagLevel == ClangTidyError::Error;
418 }) != Errors.end();
419
420 const bool DisableFixes = Fix && FoundErrors && !FixErrors;
421
Jonathan Roelofsd60388a2016-01-13 17:36:41 +0000422 unsigned WErrorCount = 0;
423
Alexander Kornienko5eac3c62014-11-03 14:06:31 +0000424 // -fix-errors implies -fix.
Alexander Kornienko25613202017-04-06 13:41:29 +0000425 handleErrors(Context, (FixErrors || Fix) && !DisableFixes, WErrorCount);
Daniel Jasperd07c8402013-07-29 08:19:24 +0000426
Alexander Kornienko4153da22014-09-04 15:19:49 +0000427 if (!ExportFixes.empty() && !Errors.empty()) {
Benjamin Kramerfb98b742014-09-04 10:31:23 +0000428 std::error_code EC;
429 llvm::raw_fd_ostream OS(ExportFixes, EC, llvm::sys::fs::F_None);
430 if (EC) {
431 llvm::errs() << "Error opening output file: " << EC.message() << '\n';
432 return 1;
433 }
Alexander Kornienko563de792017-01-03 14:36:13 +0000434 exportReplacements(FilePath.str(), Errors, OS);
Benjamin Kramerfb98b742014-09-04 10:31:23 +0000435 }
436
Ehsan Akhgarib7418d32017-02-09 18:32:02 +0000437 if (!Quiet) {
Alexander Kornienko25613202017-04-06 13:41:29 +0000438 printStats(Context.getStats());
Ehsan Akhgarib7418d32017-02-09 18:32:02 +0000439 if (DisableFixes)
440 llvm::errs()
441 << "Found compiler errors, but -fix-errors was not specified.\n"
442 "Fixes have NOT been applied.\n\n";
443 }
Alexander Kornienko5eac3c62014-11-03 14:06:31 +0000444
Samuel Benzaquenaedd9942014-10-23 17:23:20 +0000445 if (EnableCheckProfile)
446 printProfileData(Profile, llvm::errs());
447
Jonathan Roelofsd60388a2016-01-13 17:36:41 +0000448 if (WErrorCount) {
Ehsan Akhgarib7418d32017-02-09 18:32:02 +0000449 if (!Quiet) {
450 StringRef Plural = WErrorCount == 1 ? "" : "s";
451 llvm::errs() << WErrorCount << " warning" << Plural << " treated as error"
452 << Plural << "\n";
453 }
Jonathan Roelofsd60388a2016-01-13 17:36:41 +0000454 return WErrorCount;
455 }
456
Daniel Jasperd07c8402013-07-29 08:19:24 +0000457 return 0;
458}
Daniel Jasper89bbab02013-08-04 15:56:30 +0000459
Aaron Ballmanea2f90c2015-10-02 13:27:19 +0000460// This anchor is used to force the linker to link the CERTModule.
461extern volatile int CERTModuleAnchorSource;
462static int LLVM_ATTRIBUTE_UNUSED CERTModuleAnchorDestination =
463 CERTModuleAnchorSource;
464
Piotr Padlewski5625f652016-04-29 17:58:29 +0000465// This anchor is used to force the linker to link the BoostModule.
466extern volatile int BoostModuleAnchorSource;
467static int LLVM_ATTRIBUTE_UNUSED BoostModuleAnchorDestination =
468 BoostModuleAnchorSource;
469
Gabor Horvath829e75a2017-07-14 12:15:55 +0000470// This anchor is used to force the linker to link the BugproneModule.
471extern volatile int BugproneModuleAnchorSource;
472static int LLVM_ATTRIBUTE_UNUSED BugproneModuleAnchorDestination =
473 BugproneModuleAnchorSource;
474
Daniel Jasper89bbab02013-08-04 15:56:30 +0000475// This anchor is used to force the linker to link the LLVMModule.
476extern volatile int LLVMModuleAnchorSource;
Alexander Kornienkoe1292f82015-08-19 16:54:51 +0000477static int LLVM_ATTRIBUTE_UNUSED LLVMModuleAnchorDestination =
478 LLVMModuleAnchorSource;
Daniel Jasper89bbab02013-08-04 15:56:30 +0000479
Aaron Ballmanaaa40802015-10-06 13:31:00 +0000480// This anchor is used to force the linker to link the CppCoreGuidelinesModule.
481extern volatile int CppCoreGuidelinesModuleAnchorSource;
482static int LLVM_ATTRIBUTE_UNUSED CppCoreGuidelinesModuleAnchorDestination =
483 CppCoreGuidelinesModuleAnchorSource;
484
Daniel Jasper89bbab02013-08-04 15:56:30 +0000485// This anchor is used to force the linker to link the GoogleModule.
486extern volatile int GoogleModuleAnchorSource;
Alexander Kornienkoe1292f82015-08-19 16:54:51 +0000487static int LLVM_ATTRIBUTE_UNUSED GoogleModuleAnchorDestination =
488 GoogleModuleAnchorSource;
Daniel Jasper89bbab02013-08-04 15:56:30 +0000489
Yan Wang36206202017-06-23 21:37:29 +0000490// This anchor is used to force the linker to link the AndroidModule.
491extern volatile int AndroidModuleAnchorSource;
492static int LLVM_ATTRIBUTE_UNUSED AndroidModuleAnchorDestination =
493 AndroidModuleAnchorSource;
494
Alexander Kornienko16ac6ce2014-03-05 13:14:32 +0000495// This anchor is used to force the linker to link the MiscModule.
496extern volatile int MiscModuleAnchorSource;
Alexander Kornienkoe1292f82015-08-19 16:54:51 +0000497static int LLVM_ATTRIBUTE_UNUSED MiscModuleAnchorDestination =
498 MiscModuleAnchorSource;
Alexander Kornienko16ac6ce2014-03-05 13:14:32 +0000499
Alexander Kornienkofc650862015-08-14 13:17:11 +0000500// This anchor is used to force the linker to link the ModernizeModule.
501extern volatile int ModernizeModuleAnchorSource;
Alexander Kornienkoe1292f82015-08-19 16:54:51 +0000502static int LLVM_ATTRIBUTE_UNUSED ModernizeModuleAnchorDestination =
503 ModernizeModuleAnchorSource;
Alexander Kornienkofc650862015-08-14 13:17:11 +0000504
Alexander Kornienko5e0a50c2016-08-02 20:29:35 +0000505// This anchor is used to force the linker to link the MPIModule.
506extern volatile int MPIModuleAnchorSource;
507static int LLVM_ATTRIBUTE_UNUSED MPIModuleAnchorDestination =
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000508 MPIModuleAnchorSource;
Alexander Kornienko5e0a50c2016-08-02 20:29:35 +0000509
Alexander Kornienkob959f4c2015-12-30 10:24:40 +0000510// This anchor is used to force the linker to link the PerformanceModule.
511extern volatile int PerformanceModuleAnchorSource;
512static int LLVM_ATTRIBUTE_UNUSED PerformanceModuleAnchorDestination =
513 PerformanceModuleAnchorSource;
514
Alexander Kornienko2192a8e2014-10-26 01:41:14 +0000515// This anchor is used to force the linker to link the ReadabilityModule.
516extern volatile int ReadabilityModuleAnchorSource;
Alexander Kornienkoe1292f82015-08-19 16:54:51 +0000517static int LLVM_ATTRIBUTE_UNUSED ReadabilityModuleAnchorDestination =
518 ReadabilityModuleAnchorSource;
Alexander Kornienko2192a8e2014-10-26 01:41:14 +0000519
Aaron Ballmandbdbabf2017-03-19 17:23:23 +0000520// This anchor is used to force the linker to link the HICPPModule.
521extern volatile int HICPPModuleAnchorSource;
522static int LLVM_ATTRIBUTE_UNUSED HICPPModuleAnchorDestination =
523 HICPPModuleAnchorSource;
Jonathan Coe3032d3c2017-02-06 22:57:14 +0000524
Daniel Jasper89bbab02013-08-04 15:56:30 +0000525} // namespace tidy
526} // namespace clang
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000527
528int main(int argc, const char **argv) {
529 return clang::tidy::clangTidyMain(argc, argv);
530}