blob: 04baa7b26be554178f0673c4578f7bad7284254b [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 Kornienkof1d54eb2016-02-08 00:19:29 +000030static cl::extrahelp ClangTidyHelp(R"(
31Configuration files:
32 clang-tidy attempts to read configuration for each source file from a
33 .clang-tidy file located in the closest parent directory of the source
34 file. If any configuration options have a corresponding command-line
35 option, command-line option takes precedence. The effective
36 configuration can be inspected using -dump-config:
37
Alexander Kornienko215604c2017-04-06 14:27:00 +000038 $ clang-tidy -dump-config
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +000039 ---
40 Checks: '-*,some-check'
41 WarningsAsErrors: ''
42 HeaderFilterRegex: ''
43 AnalyzeTemporaryDtors: false
Alexander Kornienko215604c2017-04-06 14:27:00 +000044 FormatStyle: none
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +000045 User: user
46 CheckOptions:
47 - key: some-check.SomeOption
48 value: 'some value'
49 ...
50
51)");
Daniel Jasperd07c8402013-07-29 08:19:24 +000052
Jonas Devlieghere73191842016-11-30 18:06:42 +000053const char DefaultChecks[] = // Enable these checks by default:
54 "clang-diagnostic-*," // * compiler diagnostics
55 "clang-analyzer-*"; // * Static Analyzer checks
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000056
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +000057static cl::opt<std::string> Checks("checks", cl::desc(R"(
58Comma-separated list of globs with optional '-'
59prefix. Globs are processed in order of
60appearance in the list. Globs without '-'
61prefix add checks with matching names to the
62set, globs with the '-' prefix remove checks
63with matching names from the set of enabled
Alexander Kornienko17a4b232017-03-03 11:16:34 +000064checks. This option's value is appended to the
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +000065value of the 'Checks' option in .clang-tidy
66file, if any.
67)"),
68 cl::init(""), cl::cat(ClangTidyCategory));
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000069
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +000070static cl::opt<std::string> WarningsAsErrors("warnings-as-errors", cl::desc(R"(
71Upgrades warnings to errors. Same format as
72'-checks'.
73This option's value is appended to the value of
74the 'WarningsAsErrors' option in .clang-tidy
75file, if any.
76)"),
77 cl::init(""),
78 cl::cat(ClangTidyCategory));
Jonathan Roelofsd60388a2016-01-13 17:36:41 +000079
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +000080static cl::opt<std::string> HeaderFilter("header-filter", cl::desc(R"(
81Regular expression matching the names of the
82headers to output diagnostics from. Diagnostics
83from the main file of each translation unit are
84always displayed.
85Can be used together with -line-filter.
86This option overrides the 'HeaderFilter' option
87in .clang-tidy file, if any.
88)"),
89 cl::init(""),
90 cl::cat(ClangTidyCategory));
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000091
Alexander Kornienko37f7abe2014-10-28 22:16:13 +000092static cl::opt<bool>
93 SystemHeaders("system-headers",
Alexander Kornienko5eac3c62014-11-03 14:06:31 +000094 cl::desc("Display the errors from system headers."),
Alexander Kornienko37f7abe2014-10-28 22:16:13 +000095 cl::init(false), cl::cat(ClangTidyCategory));
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +000096static cl::opt<std::string> LineFilter("line-filter", cl::desc(R"(
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +000097List of files with line ranges to filter the
98warnings. Can be used together with
99-header-filter. The format of the list is a
100JSON array of objects:
101 [
102 {"name":"file1.cpp","lines":[[1,3],[5,7]]},
103 {"name":"file2.h"}
104 ]
105)"),
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000106 cl::init(""),
107 cl::cat(ClangTidyCategory));
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000108
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +0000109static cl::opt<bool> Fix("fix", cl::desc(R"(
110Apply suggested fixes. Without -fix-errors
111clang-tidy will bail out if any compilation
112errors were found.
113)"),
114 cl::init(false), cl::cat(ClangTidyCategory));
Alexander Kornienko5eac3c62014-11-03 14:06:31 +0000115
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +0000116static cl::opt<bool> FixErrors("fix-errors", cl::desc(R"(
117Apply suggested fixes even if compilation
118errors were found. If compiler errors have
119attached fix-its, clang-tidy will apply them as
120well.
121)"),
122 cl::init(false), cl::cat(ClangTidyCategory));
Daniel Jasperd07c8402013-07-29 08:19:24 +0000123
Alexander Kornienko17a4b232017-03-03 11:16:34 +0000124static cl::opt<std::string> FormatStyle("format-style", cl::desc(R"(
125Style for formatting code around applied fixes:
126 - 'none' (default) turns off formatting
127 - 'file' (literally 'file', not a placeholder)
128 uses .clang-format file in the closest parent
129 directory
130 - '{ <json> }' specifies options inline, e.g.
131 -format-style='{BasedOnStyle: llvm, IndentWidth: 8}'
132 - 'llvm', 'google', 'webkit', 'mozilla'
133See clang-format documentation for the up-to-date
134information about formatting styles and options.
Alexander Kornienko215604c2017-04-06 14:27:00 +0000135This option overrides the 'FormatStyle` option in
136.clang-tidy file, if any.
Jonas Devlieghere73191842016-11-30 18:06:42 +0000137)"),
Alexander Kornienko17a4b232017-03-03 11:16:34 +0000138 cl::init("none"),
139 cl::cat(ClangTidyCategory));
Jonas Devlieghere73191842016-11-30 18:06:42 +0000140
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +0000141static cl::opt<bool> ListChecks("list-checks", cl::desc(R"(
142List all enabled checks and exit. Use with
143-checks=* to list all available checks.
144)"),
145 cl::init(false), cl::cat(ClangTidyCategory));
Daniel Jasperd07c8402013-07-29 08:19:24 +0000146
Haojian Wu12e6b8f2016-04-27 09:15:01 +0000147static cl::opt<bool> ExplainConfig("explain-config", cl::desc(R"(
Haojian Wue5555e22016-09-22 14:36:43 +0000148For each enabled check explains, where it is
149enabled, i.e. in clang-tidy binary, command
150line or a specific configuration file.
Haojian Wu12e6b8f2016-04-27 09:15:01 +0000151)"),
152 cl::init(false), cl::cat(ClangTidyCategory));
153
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +0000154static cl::opt<std::string> Config("config", cl::desc(R"(
155Specifies a configuration in YAML/JSON format:
156 -config="{Checks: '*',
157 CheckOptions: [{key: x,
158 value: y}]}"
159When the value is empty, clang-tidy will
160attempt to find a file named .clang-tidy for
161each source file in its parent directories.
162)"),
163 cl::init(""), cl::cat(ClangTidyCategory));
Alexander Kornienkoeff4e922014-09-26 11:42:29 +0000164
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +0000165static cl::opt<bool> DumpConfig("dump-config", cl::desc(R"(
166Dumps configuration in the YAML format to
167stdout. This option can be used along with a
168file name (and '--' if the file is outside of a
169project with configured compilation database).
170The configuration used for this file will be
171printed.
172Use along with -checks=* to include
173configuration of all checks.
174)"),
175 cl::init(false), cl::cat(ClangTidyCategory));
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000176
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +0000177static cl::opt<bool> EnableCheckProfile("enable-check-profile", cl::desc(R"(
178Enable per-check timing profiles, and print a
179report to stderr.
180)"),
181 cl::init(false),
182 cl::cat(ClangTidyCategory));
Samuel Benzaquenaedd9942014-10-23 17:23:20 +0000183
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +0000184static cl::opt<bool> AnalyzeTemporaryDtors("analyze-temporary-dtors",
185 cl::desc(R"(
186Enable temporary destructor-aware analysis in
187clang-analyzer- checks.
188This option overrides the value read from a
189.clang-tidy file.
190)"),
191 cl::init(false),
192 cl::cat(ClangTidyCategory));
Alex McCarthyfec08c72014-04-30 14:09:24 +0000193
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +0000194static cl::opt<std::string> ExportFixes("export-fixes", cl::desc(R"(
195YAML file to store suggested fixes in. The
Kirill Bobyrev405699e2016-08-19 09:36:14 +0000196stored fixes can be applied to the input source
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +0000197code with clang-apply-replacements.
198)"),
199 cl::value_desc("filename"),
200 cl::cat(ClangTidyCategory));
Benjamin Kramerfb98b742014-09-04 10:31:23 +0000201
Ehsan Akhgarib7418d32017-02-09 18:32:02 +0000202static cl::opt<bool> Quiet("quiet", cl::desc(R"(
Alexander Kornienko17a4b232017-03-03 11:16:34 +0000203Run clang-tidy in quiet mode. This suppresses
Ehsan Akhgarib7418d32017-02-09 18:32:02 +0000204printing statistics about ignored warnings and
205warnings treated as errors if the respective
206options are specified.
207)"),
208 cl::init(false),
209 cl::cat(ClangTidyCategory));
210
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000211namespace clang {
212namespace tidy {
213
214static void printStats(const ClangTidyStats &Stats) {
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000215 if (Stats.errorsIgnored()) {
216 llvm::errs() << "Suppressed " << Stats.errorsIgnored() << " warnings (";
Alexander Kornienko5d174542014-05-07 09:06:53 +0000217 StringRef Separator = "";
218 if (Stats.ErrorsIgnoredNonUserCode) {
219 llvm::errs() << Stats.ErrorsIgnoredNonUserCode << " in non-user code";
220 Separator = ", ";
221 }
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000222 if (Stats.ErrorsIgnoredLineFilter) {
223 llvm::errs() << Separator << Stats.ErrorsIgnoredLineFilter
224 << " due to line filter";
225 Separator = ", ";
226 }
Alexander Kornienko5d174542014-05-07 09:06:53 +0000227 if (Stats.ErrorsIgnoredNOLINT) {
228 llvm::errs() << Separator << Stats.ErrorsIgnoredNOLINT << " NOLINT";
229 Separator = ", ";
230 }
231 if (Stats.ErrorsIgnoredCheckFilter)
232 llvm::errs() << Separator << Stats.ErrorsIgnoredCheckFilter
233 << " with check filters";
234 llvm::errs() << ").\n";
235 if (Stats.ErrorsIgnoredNonUserCode)
Aaron Ballman5a4892b2015-07-27 13:41:30 +0000236 llvm::errs() << "Use -header-filter=.* to display errors from all "
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +0000237 "non-system headers. Use -system-headers to display "
238 "errors from system headers as well.\n";
Alexander Kornienko5d174542014-05-07 09:06:53 +0000239 }
240}
241
Samuel Benzaquenaedd9942014-10-23 17:23:20 +0000242static void printProfileData(const ProfileData &Profile,
243 llvm::raw_ostream &OS) {
244 // Time is first to allow for sorting by it.
245 std::vector<std::pair<llvm::TimeRecord, StringRef>> Timers;
246 TimeRecord Total;
247
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000248 for (const auto &P : Profile.Records) {
Samuel Benzaquenaedd9942014-10-23 17:23:20 +0000249 Timers.emplace_back(P.getValue(), P.getKey());
250 Total += P.getValue();
251 }
252
253 std::sort(Timers.begin(), Timers.end());
254
255 std::string Line = "===" + std::string(73, '-') + "===\n";
256 OS << Line;
257
258 if (Total.getUserTime())
259 OS << " ---User Time---";
260 if (Total.getSystemTime())
261 OS << " --System Time--";
262 if (Total.getProcessTime())
263 OS << " --User+System--";
264 OS << " ---Wall Time---";
265 if (Total.getMemUsed())
266 OS << " ---Mem---";
267 OS << " --- Name ---\n";
268
269 // Loop through all of the timing data, printing it out.
270 for (auto I = Timers.rbegin(), E = Timers.rend(); I != E; ++I) {
271 I->first.print(Total, OS);
272 OS << I->second << '\n';
273 }
274
275 Total.print(Total, OS);
276 OS << "Total\n";
277 OS << Line << "\n";
278 OS.flush();
279}
280
Benjamin Kramere7103712015-03-23 12:49:15 +0000281static std::unique_ptr<ClangTidyOptionsProvider> createOptionsProvider() {
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000282 ClangTidyGlobalOptions GlobalOptions;
283 if (std::error_code Err = parseLineFilter(LineFilter, GlobalOptions)) {
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000284 llvm::errs() << "Invalid LineFilter: " << Err.message() << "\n\nUsage:\n";
285 llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true);
Alexander Kornienkoeff4e922014-09-26 11:42:29 +0000286 return nullptr;
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000287 }
Alexander Kornienko33a9bcc2014-04-29 15:20:10 +0000288
Alexander Kornienkoe9951542014-09-24 18:36:03 +0000289 ClangTidyOptions DefaultOptions;
290 DefaultOptions.Checks = DefaultChecks;
Jonathan Roelofsd60388a2016-01-13 17:36:41 +0000291 DefaultOptions.WarningsAsErrors = "";
Alexander Kornienkoe9951542014-09-24 18:36:03 +0000292 DefaultOptions.HeaderFilterRegex = HeaderFilter;
Alexander Kornienko37f7abe2014-10-28 22:16:13 +0000293 DefaultOptions.SystemHeaders = SystemHeaders;
Alexander Kornienkoe9951542014-09-24 18:36:03 +0000294 DefaultOptions.AnalyzeTemporaryDtors = AnalyzeTemporaryDtors;
Alexander Kornienko25613202017-04-06 13:41:29 +0000295 DefaultOptions.FormatStyle = FormatStyle;
Alexander Kornienkoe9951542014-09-24 18:36:03 +0000296 DefaultOptions.User = llvm::sys::Process::GetEnv("USER");
297 // USERNAME is used on Windows.
298 if (!DefaultOptions.User)
299 DefaultOptions.User = llvm::sys::Process::GetEnv("USERNAME");
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000300
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000301 ClangTidyOptions OverrideOptions;
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000302 if (Checks.getNumOccurrences() > 0)
303 OverrideOptions.Checks = Checks;
Jonathan Roelofsd60388a2016-01-13 17:36:41 +0000304 if (WarningsAsErrors.getNumOccurrences() > 0)
305 OverrideOptions.WarningsAsErrors = WarningsAsErrors;
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000306 if (HeaderFilter.getNumOccurrences() > 0)
307 OverrideOptions.HeaderFilterRegex = HeaderFilter;
Alexander Kornienko37f7abe2014-10-28 22:16:13 +0000308 if (SystemHeaders.getNumOccurrences() > 0)
309 OverrideOptions.SystemHeaders = SystemHeaders;
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000310 if (AnalyzeTemporaryDtors.getNumOccurrences() > 0)
311 OverrideOptions.AnalyzeTemporaryDtors = AnalyzeTemporaryDtors;
Alexander Kornienko25613202017-04-06 13:41:29 +0000312 if (FormatStyle.getNumOccurrences() > 0)
313 OverrideOptions.FormatStyle = FormatStyle;
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000314
Alexander Kornienkoeff4e922014-09-26 11:42:29 +0000315 if (!Config.empty()) {
316 if (llvm::ErrorOr<ClangTidyOptions> ParsedConfig =
317 parseConfiguration(Config)) {
Haojian Wu12e6b8f2016-04-27 09:15:01 +0000318 return llvm::make_unique<ConfigOptionsProvider>(
319 GlobalOptions,
320 ClangTidyOptions::getDefaults().mergeWith(DefaultOptions),
321 *ParsedConfig, OverrideOptions);
Alexander Kornienkoeff4e922014-09-26 11:42:29 +0000322 } else {
323 llvm::errs() << "Error: invalid configuration specified.\n"
324 << ParsedConfig.getError().message() << "\n";
325 return nullptr;
326 }
327 }
328 return llvm::make_unique<FileOptionsProvider>(GlobalOptions, DefaultOptions,
329 OverrideOptions);
330}
331
Benjamin Kramere7103712015-03-23 12:49:15 +0000332static int clangTidyMain(int argc, const char **argv) {
Alexander Kornienko65eccb42015-08-17 10:03:27 +0000333 CommonOptionsParser OptionsParser(argc, argv, ClangTidyCategory,
334 cl::ZeroOrMore);
Alexander Kornienkoeff4e922014-09-26 11:42:29 +0000335
Alexander Kornienko25613202017-04-06 13:41:29 +0000336 auto OwningOptionsProvider = createOptionsProvider();
337 auto *OptionsProvider = OwningOptionsProvider.get();
Alexander Kornienkoeff4e922014-09-26 11:42:29 +0000338 if (!OptionsProvider)
339 return 1;
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000340
Alexander Kornienko65eccb42015-08-17 10:03:27 +0000341 StringRef FileName("dummy");
342 auto PathList = OptionsParser.getSourcePathList();
343 if (!PathList.empty()) {
Alexander Kornienkoe0c900e2015-08-17 11:27:11 +0000344 FileName = PathList.front();
Alexander Kornienko65eccb42015-08-17 10:03:27 +0000345 }
Haojian Wud1218752016-07-11 07:47:04 +0000346
347 SmallString<256> FilePath(FileName);
348 if (std::error_code EC = llvm::sys::fs::make_absolute(FilePath)) {
349 llvm::errs() << "Can't make absolute path from " << FileName << ": "
350 << EC.message() << "\n";
351 }
352 ClangTidyOptions EffectiveOptions = OptionsProvider->getOptions(FilePath);
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000353 std::vector<std::string> EnabledChecks = getCheckNames(EffectiveOptions);
Alexander Kornienkofbf92582014-06-02 20:32:06 +0000354
Haojian Wu12e6b8f2016-04-27 09:15:01 +0000355 if (ExplainConfig) {
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000356 // FIXME: Show other ClangTidyOptions' fields, like ExtraArg.
Haojian Wu12e6b8f2016-04-27 09:15:01 +0000357 std::vector<clang::tidy::ClangTidyOptionsProvider::OptionsSource>
Haojian Wud1218752016-07-11 07:47:04 +0000358 RawOptions = OptionsProvider->getRawOptions(FilePath);
Haojian Wu12e6b8f2016-04-27 09:15:01 +0000359 for (const std::string &Check : EnabledChecks) {
360 for (auto It = RawOptions.rbegin(); It != RawOptions.rend(); ++It) {
361 if (It->first.Checks && GlobList(*It->first.Checks).contains(Check)) {
362 llvm::outs() << "'" << Check << "' is enabled in the " << It->second
363 << ".\n";
364 break;
365 }
366 }
367 }
368 return 0;
369 }
370
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +0000371 if (ListChecks) {
Alexander Kornienko493db092016-04-27 11:45:14 +0000372 if (EnabledChecks.empty()) {
373 llvm::errs() << "No checks enabled.\n";
374 return 1;
375 }
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +0000376 llvm::outs() << "Enabled checks:";
Benjamin Kramer51a9cc92016-06-15 15:46:10 +0000377 for (const auto &CheckName : EnabledChecks)
Alexander Kornienko16ac6ce2014-03-05 13:14:32 +0000378 llvm::outs() << "\n " << CheckName;
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +0000379 llvm::outs() << "\n\n";
380 return 0;
381 }
382
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000383 if (DumpConfig) {
Alexander Kornienko6e0cbc82014-09-12 08:53:36 +0000384 EffectiveOptions.CheckOptions = getCheckOptions(EffectiveOptions);
Alexander Kornienko65eccb42015-08-17 10:03:27 +0000385 llvm::outs() << configurationAsText(
386 ClangTidyOptions::getDefaults().mergeWith(
387 EffectiveOptions))
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000388 << "\n";
389 return 0;
390 }
391
Alexander Kornienkofbf92582014-06-02 20:32:06 +0000392 if (EnabledChecks.empty()) {
393 llvm::errs() << "Error: no checks enabled.\n";
394 llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true);
395 return 1;
396 }
397
Alexander Kornienko65eccb42015-08-17 10:03:27 +0000398 if (PathList.empty()) {
399 llvm::errs() << "Error: no input files specified.\n";
400 llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true);
401 return 1;
402 }
403
Samuel Benzaquenaedd9942014-10-23 17:23:20 +0000404 ProfileData Profile;
405
Alexander Kornienko25613202017-04-06 13:41:29 +0000406 ClangTidyContext Context(std::move(OwningOptionsProvider));
407 runClangTidy(Context, OptionsParser.getCompilations(), PathList,
408 EnableCheckProfile ? &Profile : nullptr);
409 ArrayRef<ClangTidyError> Errors = Context.getErrors();
Alexander Kornienko5eac3c62014-11-03 14:06:31 +0000410 bool FoundErrors =
411 std::find_if(Errors.begin(), Errors.end(), [](const ClangTidyError &E) {
412 return E.DiagLevel == ClangTidyError::Error;
413 }) != Errors.end();
414
415 const bool DisableFixes = Fix && FoundErrors && !FixErrors;
416
Jonathan Roelofsd60388a2016-01-13 17:36:41 +0000417 unsigned WErrorCount = 0;
418
Alexander Kornienko5eac3c62014-11-03 14:06:31 +0000419 // -fix-errors implies -fix.
Alexander Kornienko25613202017-04-06 13:41:29 +0000420 handleErrors(Context, (FixErrors || Fix) && !DisableFixes, WErrorCount);
Daniel Jasperd07c8402013-07-29 08:19:24 +0000421
Alexander Kornienko4153da22014-09-04 15:19:49 +0000422 if (!ExportFixes.empty() && !Errors.empty()) {
Benjamin Kramerfb98b742014-09-04 10:31:23 +0000423 std::error_code EC;
424 llvm::raw_fd_ostream OS(ExportFixes, EC, llvm::sys::fs::F_None);
425 if (EC) {
426 llvm::errs() << "Error opening output file: " << EC.message() << '\n';
427 return 1;
428 }
Alexander Kornienko563de792017-01-03 14:36:13 +0000429 exportReplacements(FilePath.str(), Errors, OS);
Benjamin Kramerfb98b742014-09-04 10:31:23 +0000430 }
431
Ehsan Akhgarib7418d32017-02-09 18:32:02 +0000432 if (!Quiet) {
Alexander Kornienko25613202017-04-06 13:41:29 +0000433 printStats(Context.getStats());
Ehsan Akhgarib7418d32017-02-09 18:32:02 +0000434 if (DisableFixes)
435 llvm::errs()
436 << "Found compiler errors, but -fix-errors was not specified.\n"
437 "Fixes have NOT been applied.\n\n";
438 }
Alexander Kornienko5eac3c62014-11-03 14:06:31 +0000439
Samuel Benzaquenaedd9942014-10-23 17:23:20 +0000440 if (EnableCheckProfile)
441 printProfileData(Profile, llvm::errs());
442
Jonathan Roelofsd60388a2016-01-13 17:36:41 +0000443 if (WErrorCount) {
Ehsan Akhgarib7418d32017-02-09 18:32:02 +0000444 if (!Quiet) {
445 StringRef Plural = WErrorCount == 1 ? "" : "s";
446 llvm::errs() << WErrorCount << " warning" << Plural << " treated as error"
447 << Plural << "\n";
448 }
Jonathan Roelofsd60388a2016-01-13 17:36:41 +0000449 return WErrorCount;
450 }
451
Daniel Jasperd07c8402013-07-29 08:19:24 +0000452 return 0;
453}
Daniel Jasper89bbab02013-08-04 15:56:30 +0000454
Aaron Ballmanea2f90c2015-10-02 13:27:19 +0000455// This anchor is used to force the linker to link the CERTModule.
456extern volatile int CERTModuleAnchorSource;
457static int LLVM_ATTRIBUTE_UNUSED CERTModuleAnchorDestination =
458 CERTModuleAnchorSource;
459
Piotr Padlewski5625f652016-04-29 17:58:29 +0000460// This anchor is used to force the linker to link the BoostModule.
461extern volatile int BoostModuleAnchorSource;
462static int LLVM_ATTRIBUTE_UNUSED BoostModuleAnchorDestination =
463 BoostModuleAnchorSource;
464
Daniel Jasper89bbab02013-08-04 15:56:30 +0000465// This anchor is used to force the linker to link the LLVMModule.
466extern volatile int LLVMModuleAnchorSource;
Alexander Kornienkoe1292f82015-08-19 16:54:51 +0000467static int LLVM_ATTRIBUTE_UNUSED LLVMModuleAnchorDestination =
468 LLVMModuleAnchorSource;
Daniel Jasper89bbab02013-08-04 15:56:30 +0000469
Aaron Ballmanaaa40802015-10-06 13:31:00 +0000470// This anchor is used to force the linker to link the CppCoreGuidelinesModule.
471extern volatile int CppCoreGuidelinesModuleAnchorSource;
472static int LLVM_ATTRIBUTE_UNUSED CppCoreGuidelinesModuleAnchorDestination =
473 CppCoreGuidelinesModuleAnchorSource;
474
Daniel Jasper89bbab02013-08-04 15:56:30 +0000475// This anchor is used to force the linker to link the GoogleModule.
476extern volatile int GoogleModuleAnchorSource;
Alexander Kornienkoe1292f82015-08-19 16:54:51 +0000477static int LLVM_ATTRIBUTE_UNUSED GoogleModuleAnchorDestination =
478 GoogleModuleAnchorSource;
Daniel Jasper89bbab02013-08-04 15:56:30 +0000479
Alexander Kornienko16ac6ce2014-03-05 13:14:32 +0000480// This anchor is used to force the linker to link the MiscModule.
481extern volatile int MiscModuleAnchorSource;
Alexander Kornienkoe1292f82015-08-19 16:54:51 +0000482static int LLVM_ATTRIBUTE_UNUSED MiscModuleAnchorDestination =
483 MiscModuleAnchorSource;
Alexander Kornienko16ac6ce2014-03-05 13:14:32 +0000484
Alexander Kornienkofc650862015-08-14 13:17:11 +0000485// This anchor is used to force the linker to link the ModernizeModule.
486extern volatile int ModernizeModuleAnchorSource;
Alexander Kornienkoe1292f82015-08-19 16:54:51 +0000487static int LLVM_ATTRIBUTE_UNUSED ModernizeModuleAnchorDestination =
488 ModernizeModuleAnchorSource;
Alexander Kornienkofc650862015-08-14 13:17:11 +0000489
Alexander Kornienko5e0a50c2016-08-02 20:29:35 +0000490// This anchor is used to force the linker to link the MPIModule.
491extern volatile int MPIModuleAnchorSource;
492static int LLVM_ATTRIBUTE_UNUSED MPIModuleAnchorDestination =
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000493 MPIModuleAnchorSource;
Alexander Kornienko5e0a50c2016-08-02 20:29:35 +0000494
Alexander Kornienkob959f4c2015-12-30 10:24:40 +0000495// This anchor is used to force the linker to link the PerformanceModule.
496extern volatile int PerformanceModuleAnchorSource;
497static int LLVM_ATTRIBUTE_UNUSED PerformanceModuleAnchorDestination =
498 PerformanceModuleAnchorSource;
499
Alexander Kornienko2192a8e2014-10-26 01:41:14 +0000500// This anchor is used to force the linker to link the ReadabilityModule.
501extern volatile int ReadabilityModuleAnchorSource;
Alexander Kornienkoe1292f82015-08-19 16:54:51 +0000502static int LLVM_ATTRIBUTE_UNUSED ReadabilityModuleAnchorDestination =
503 ReadabilityModuleAnchorSource;
Alexander Kornienko2192a8e2014-10-26 01:41:14 +0000504
Aaron Ballmandbdbabf2017-03-19 17:23:23 +0000505// This anchor is used to force the linker to link the HICPPModule.
506extern volatile int HICPPModuleAnchorSource;
507static int LLVM_ATTRIBUTE_UNUSED HICPPModuleAnchorDestination =
508 HICPPModuleAnchorSource;
Jonathan Coe3032d3c2017-02-06 22:57:14 +0000509
Daniel Jasper89bbab02013-08-04 15:56:30 +0000510} // namespace tidy
511} // namespace clang
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000512
513int main(int argc, const char **argv) {
514 return clang::tidy::clangTidyMain(argc, argv);
515}