blob: 1114ff3d14292a3469f56e2a8b9c3c0fb865a026 [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
38 $ clang-tidy -dump-config - --
39 ---
40 Checks: '-*,some-check'
41 WarningsAsErrors: ''
42 HeaderFilterRegex: ''
43 AnalyzeTemporaryDtors: false
44 User: user
45 CheckOptions:
46 - key: some-check.SomeOption
47 value: 'some value'
48 ...
49
50)");
Daniel Jasperd07c8402013-07-29 08:19:24 +000051
Jonas Devlieghere73191842016-11-30 18:06:42 +000052const char DefaultChecks[] = // Enable these checks by default:
53 "clang-diagnostic-*," // * compiler diagnostics
54 "clang-analyzer-*"; // * Static Analyzer checks
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000055
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +000056static cl::opt<std::string> Checks("checks", cl::desc(R"(
57Comma-separated list of globs with optional '-'
58prefix. Globs are processed in order of
59appearance in the list. Globs without '-'
60prefix add checks with matching names to the
61set, globs with the '-' prefix remove checks
62with matching names from the set of enabled
Alexander Kornienko17a4b232017-03-03 11:16:34 +000063checks. This option's value is appended to the
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +000064value of the 'Checks' option in .clang-tidy
65file, if any.
66)"),
67 cl::init(""), cl::cat(ClangTidyCategory));
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000068
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +000069static cl::opt<std::string> WarningsAsErrors("warnings-as-errors", cl::desc(R"(
70Upgrades warnings to errors. Same format as
71'-checks'.
72This option's value is appended to the value of
73the 'WarningsAsErrors' option in .clang-tidy
74file, if any.
75)"),
76 cl::init(""),
77 cl::cat(ClangTidyCategory));
Jonathan Roelofsd60388a2016-01-13 17:36:41 +000078
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +000079static cl::opt<std::string> HeaderFilter("header-filter", cl::desc(R"(
80Regular expression matching the names of the
81headers to output diagnostics from. Diagnostics
82from the main file of each translation unit are
83always displayed.
84Can be used together with -line-filter.
85This option overrides the 'HeaderFilter' option
86in .clang-tidy file, if any.
87)"),
88 cl::init(""),
89 cl::cat(ClangTidyCategory));
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000090
Alexander Kornienko37f7abe2014-10-28 22:16:13 +000091static cl::opt<bool>
92 SystemHeaders("system-headers",
Alexander Kornienko5eac3c62014-11-03 14:06:31 +000093 cl::desc("Display the errors from system headers."),
Alexander Kornienko37f7abe2014-10-28 22:16:13 +000094 cl::init(false), cl::cat(ClangTidyCategory));
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +000095static cl::opt<std::string> LineFilter("line-filter", cl::desc(R"(
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +000096List of files with line ranges to filter the
97warnings. Can be used together with
98-header-filter. The format of the list is a
99JSON array of objects:
100 [
101 {"name":"file1.cpp","lines":[[1,3],[5,7]]},
102 {"name":"file2.h"}
103 ]
104)"),
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000105 cl::init(""),
106 cl::cat(ClangTidyCategory));
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000107
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +0000108static cl::opt<bool> Fix("fix", cl::desc(R"(
109Apply suggested fixes. Without -fix-errors
110clang-tidy will bail out if any compilation
111errors were found.
112)"),
113 cl::init(false), cl::cat(ClangTidyCategory));
Alexander Kornienko5eac3c62014-11-03 14:06:31 +0000114
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +0000115static cl::opt<bool> FixErrors("fix-errors", cl::desc(R"(
116Apply suggested fixes even if compilation
117errors were found. If compiler errors have
118attached fix-its, clang-tidy will apply them as
119well.
120)"),
121 cl::init(false), cl::cat(ClangTidyCategory));
Daniel Jasperd07c8402013-07-29 08:19:24 +0000122
Alexander Kornienko17a4b232017-03-03 11:16:34 +0000123static cl::opt<std::string> FormatStyle("format-style", cl::desc(R"(
124Style for formatting code around applied fixes:
125 - 'none' (default) turns off formatting
126 - 'file' (literally 'file', not a placeholder)
127 uses .clang-format file in the closest parent
128 directory
129 - '{ <json> }' specifies options inline, e.g.
130 -format-style='{BasedOnStyle: llvm, IndentWidth: 8}'
131 - 'llvm', 'google', 'webkit', 'mozilla'
132See clang-format documentation for the up-to-date
133information about formatting styles and options.
Jonas Devlieghere73191842016-11-30 18:06:42 +0000134)"),
Alexander Kornienko17a4b232017-03-03 11:16:34 +0000135 cl::init("none"),
136 cl::cat(ClangTidyCategory));
Jonas Devlieghere73191842016-11-30 18:06:42 +0000137
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +0000138static cl::opt<bool> ListChecks("list-checks", cl::desc(R"(
139List all enabled checks and exit. Use with
140-checks=* to list all available checks.
141)"),
142 cl::init(false), cl::cat(ClangTidyCategory));
Daniel Jasperd07c8402013-07-29 08:19:24 +0000143
Haojian Wu12e6b8f2016-04-27 09:15:01 +0000144static cl::opt<bool> ExplainConfig("explain-config", cl::desc(R"(
Haojian Wue5555e22016-09-22 14:36:43 +0000145For each enabled check explains, where it is
146enabled, i.e. in clang-tidy binary, command
147line or a specific configuration file.
Haojian Wu12e6b8f2016-04-27 09:15:01 +0000148)"),
149 cl::init(false), cl::cat(ClangTidyCategory));
150
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +0000151static cl::opt<std::string> Config("config", cl::desc(R"(
152Specifies a configuration in YAML/JSON format:
153 -config="{Checks: '*',
154 CheckOptions: [{key: x,
155 value: y}]}"
156When the value is empty, clang-tidy will
157attempt to find a file named .clang-tidy for
158each source file in its parent directories.
159)"),
160 cl::init(""), cl::cat(ClangTidyCategory));
Alexander Kornienkoeff4e922014-09-26 11:42:29 +0000161
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +0000162static cl::opt<bool> DumpConfig("dump-config", cl::desc(R"(
163Dumps configuration in the YAML format to
164stdout. This option can be used along with a
165file name (and '--' if the file is outside of a
166project with configured compilation database).
167The configuration used for this file will be
168printed.
169Use along with -checks=* to include
170configuration of all checks.
171)"),
172 cl::init(false), cl::cat(ClangTidyCategory));
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000173
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +0000174static cl::opt<bool> EnableCheckProfile("enable-check-profile", cl::desc(R"(
175Enable per-check timing profiles, and print a
176report to stderr.
177)"),
178 cl::init(false),
179 cl::cat(ClangTidyCategory));
Samuel Benzaquenaedd9942014-10-23 17:23:20 +0000180
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +0000181static cl::opt<bool> AnalyzeTemporaryDtors("analyze-temporary-dtors",
182 cl::desc(R"(
183Enable temporary destructor-aware analysis in
184clang-analyzer- checks.
185This option overrides the value read from a
186.clang-tidy file.
187)"),
188 cl::init(false),
189 cl::cat(ClangTidyCategory));
Alex McCarthyfec08c72014-04-30 14:09:24 +0000190
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +0000191static cl::opt<std::string> ExportFixes("export-fixes", cl::desc(R"(
192YAML file to store suggested fixes in. The
Kirill Bobyrev405699e2016-08-19 09:36:14 +0000193stored fixes can be applied to the input source
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +0000194code with clang-apply-replacements.
195)"),
196 cl::value_desc("filename"),
197 cl::cat(ClangTidyCategory));
Benjamin Kramerfb98b742014-09-04 10:31:23 +0000198
Ehsan Akhgarib7418d32017-02-09 18:32:02 +0000199static cl::opt<bool> Quiet("quiet", cl::desc(R"(
Alexander Kornienko17a4b232017-03-03 11:16:34 +0000200Run clang-tidy in quiet mode. This suppresses
Ehsan Akhgarib7418d32017-02-09 18:32:02 +0000201printing statistics about ignored warnings and
202warnings treated as errors if the respective
203options are specified.
204)"),
205 cl::init(false),
206 cl::cat(ClangTidyCategory));
207
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000208namespace clang {
209namespace tidy {
210
211static void printStats(const ClangTidyStats &Stats) {
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000212 if (Stats.errorsIgnored()) {
213 llvm::errs() << "Suppressed " << Stats.errorsIgnored() << " warnings (";
Alexander Kornienko5d174542014-05-07 09:06:53 +0000214 StringRef Separator = "";
215 if (Stats.ErrorsIgnoredNonUserCode) {
216 llvm::errs() << Stats.ErrorsIgnoredNonUserCode << " in non-user code";
217 Separator = ", ";
218 }
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000219 if (Stats.ErrorsIgnoredLineFilter) {
220 llvm::errs() << Separator << Stats.ErrorsIgnoredLineFilter
221 << " due to line filter";
222 Separator = ", ";
223 }
Alexander Kornienko5d174542014-05-07 09:06:53 +0000224 if (Stats.ErrorsIgnoredNOLINT) {
225 llvm::errs() << Separator << Stats.ErrorsIgnoredNOLINT << " NOLINT";
226 Separator = ", ";
227 }
228 if (Stats.ErrorsIgnoredCheckFilter)
229 llvm::errs() << Separator << Stats.ErrorsIgnoredCheckFilter
230 << " with check filters";
231 llvm::errs() << ").\n";
232 if (Stats.ErrorsIgnoredNonUserCode)
Aaron Ballman5a4892b2015-07-27 13:41:30 +0000233 llvm::errs() << "Use -header-filter=.* to display errors from all "
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +0000234 "non-system headers. Use -system-headers to display "
235 "errors from system headers as well.\n";
Alexander Kornienko5d174542014-05-07 09:06:53 +0000236 }
237}
238
Samuel Benzaquenaedd9942014-10-23 17:23:20 +0000239static void printProfileData(const ProfileData &Profile,
240 llvm::raw_ostream &OS) {
241 // Time is first to allow for sorting by it.
242 std::vector<std::pair<llvm::TimeRecord, StringRef>> Timers;
243 TimeRecord Total;
244
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000245 for (const auto &P : Profile.Records) {
Samuel Benzaquenaedd9942014-10-23 17:23:20 +0000246 Timers.emplace_back(P.getValue(), P.getKey());
247 Total += P.getValue();
248 }
249
250 std::sort(Timers.begin(), Timers.end());
251
252 std::string Line = "===" + std::string(73, '-') + "===\n";
253 OS << Line;
254
255 if (Total.getUserTime())
256 OS << " ---User Time---";
257 if (Total.getSystemTime())
258 OS << " --System Time--";
259 if (Total.getProcessTime())
260 OS << " --User+System--";
261 OS << " ---Wall Time---";
262 if (Total.getMemUsed())
263 OS << " ---Mem---";
264 OS << " --- Name ---\n";
265
266 // Loop through all of the timing data, printing it out.
267 for (auto I = Timers.rbegin(), E = Timers.rend(); I != E; ++I) {
268 I->first.print(Total, OS);
269 OS << I->second << '\n';
270 }
271
272 Total.print(Total, OS);
273 OS << "Total\n";
274 OS << Line << "\n";
275 OS.flush();
276}
277
Benjamin Kramere7103712015-03-23 12:49:15 +0000278static std::unique_ptr<ClangTidyOptionsProvider> createOptionsProvider() {
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000279 ClangTidyGlobalOptions GlobalOptions;
280 if (std::error_code Err = parseLineFilter(LineFilter, GlobalOptions)) {
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000281 llvm::errs() << "Invalid LineFilter: " << Err.message() << "\n\nUsage:\n";
282 llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true);
Alexander Kornienkoeff4e922014-09-26 11:42:29 +0000283 return nullptr;
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000284 }
Alexander Kornienko33a9bcc2014-04-29 15:20:10 +0000285
Alexander Kornienkoe9951542014-09-24 18:36:03 +0000286 ClangTidyOptions DefaultOptions;
287 DefaultOptions.Checks = DefaultChecks;
Jonathan Roelofsd60388a2016-01-13 17:36:41 +0000288 DefaultOptions.WarningsAsErrors = "";
Alexander Kornienkoe9951542014-09-24 18:36:03 +0000289 DefaultOptions.HeaderFilterRegex = HeaderFilter;
Alexander Kornienko37f7abe2014-10-28 22:16:13 +0000290 DefaultOptions.SystemHeaders = SystemHeaders;
Alexander Kornienkoe9951542014-09-24 18:36:03 +0000291 DefaultOptions.AnalyzeTemporaryDtors = AnalyzeTemporaryDtors;
Alexander Kornienko25613202017-04-06 13:41:29 +0000292 DefaultOptions.FormatStyle = FormatStyle;
Alexander Kornienkoe9951542014-09-24 18:36:03 +0000293 DefaultOptions.User = llvm::sys::Process::GetEnv("USER");
294 // USERNAME is used on Windows.
295 if (!DefaultOptions.User)
296 DefaultOptions.User = llvm::sys::Process::GetEnv("USERNAME");
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000297
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000298 ClangTidyOptions OverrideOptions;
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000299 if (Checks.getNumOccurrences() > 0)
300 OverrideOptions.Checks = Checks;
Jonathan Roelofsd60388a2016-01-13 17:36:41 +0000301 if (WarningsAsErrors.getNumOccurrences() > 0)
302 OverrideOptions.WarningsAsErrors = WarningsAsErrors;
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000303 if (HeaderFilter.getNumOccurrences() > 0)
304 OverrideOptions.HeaderFilterRegex = HeaderFilter;
Alexander Kornienko37f7abe2014-10-28 22:16:13 +0000305 if (SystemHeaders.getNumOccurrences() > 0)
306 OverrideOptions.SystemHeaders = SystemHeaders;
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000307 if (AnalyzeTemporaryDtors.getNumOccurrences() > 0)
308 OverrideOptions.AnalyzeTemporaryDtors = AnalyzeTemporaryDtors;
Alexander Kornienko25613202017-04-06 13:41:29 +0000309 if (FormatStyle.getNumOccurrences() > 0)
310 OverrideOptions.FormatStyle = FormatStyle;
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000311
Alexander Kornienkoeff4e922014-09-26 11:42:29 +0000312 if (!Config.empty()) {
313 if (llvm::ErrorOr<ClangTidyOptions> ParsedConfig =
314 parseConfiguration(Config)) {
Haojian Wu12e6b8f2016-04-27 09:15:01 +0000315 return llvm::make_unique<ConfigOptionsProvider>(
316 GlobalOptions,
317 ClangTidyOptions::getDefaults().mergeWith(DefaultOptions),
318 *ParsedConfig, OverrideOptions);
Alexander Kornienkoeff4e922014-09-26 11:42:29 +0000319 } else {
320 llvm::errs() << "Error: invalid configuration specified.\n"
321 << ParsedConfig.getError().message() << "\n";
322 return nullptr;
323 }
324 }
325 return llvm::make_unique<FileOptionsProvider>(GlobalOptions, DefaultOptions,
326 OverrideOptions);
327}
328
Benjamin Kramere7103712015-03-23 12:49:15 +0000329static int clangTidyMain(int argc, const char **argv) {
Alexander Kornienko65eccb42015-08-17 10:03:27 +0000330 CommonOptionsParser OptionsParser(argc, argv, ClangTidyCategory,
331 cl::ZeroOrMore);
Alexander Kornienkoeff4e922014-09-26 11:42:29 +0000332
Alexander Kornienko25613202017-04-06 13:41:29 +0000333 auto OwningOptionsProvider = createOptionsProvider();
334 auto *OptionsProvider = OwningOptionsProvider.get();
Alexander Kornienkoeff4e922014-09-26 11:42:29 +0000335 if (!OptionsProvider)
336 return 1;
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000337
Alexander Kornienko65eccb42015-08-17 10:03:27 +0000338 StringRef FileName("dummy");
339 auto PathList = OptionsParser.getSourcePathList();
340 if (!PathList.empty()) {
Alexander Kornienkoe0c900e2015-08-17 11:27:11 +0000341 FileName = PathList.front();
Alexander Kornienko65eccb42015-08-17 10:03:27 +0000342 }
Haojian Wud1218752016-07-11 07:47:04 +0000343
344 SmallString<256> FilePath(FileName);
345 if (std::error_code EC = llvm::sys::fs::make_absolute(FilePath)) {
346 llvm::errs() << "Can't make absolute path from " << FileName << ": "
347 << EC.message() << "\n";
348 }
349 ClangTidyOptions EffectiveOptions = OptionsProvider->getOptions(FilePath);
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000350 std::vector<std::string> EnabledChecks = getCheckNames(EffectiveOptions);
Alexander Kornienkofbf92582014-06-02 20:32:06 +0000351
Haojian Wu12e6b8f2016-04-27 09:15:01 +0000352 if (ExplainConfig) {
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000353 // FIXME: Show other ClangTidyOptions' fields, like ExtraArg.
Haojian Wu12e6b8f2016-04-27 09:15:01 +0000354 std::vector<clang::tidy::ClangTidyOptionsProvider::OptionsSource>
Haojian Wud1218752016-07-11 07:47:04 +0000355 RawOptions = OptionsProvider->getRawOptions(FilePath);
Haojian Wu12e6b8f2016-04-27 09:15:01 +0000356 for (const std::string &Check : EnabledChecks) {
357 for (auto It = RawOptions.rbegin(); It != RawOptions.rend(); ++It) {
358 if (It->first.Checks && GlobList(*It->first.Checks).contains(Check)) {
359 llvm::outs() << "'" << Check << "' is enabled in the " << It->second
360 << ".\n";
361 break;
362 }
363 }
364 }
365 return 0;
366 }
367
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +0000368 if (ListChecks) {
Alexander Kornienko493db092016-04-27 11:45:14 +0000369 if (EnabledChecks.empty()) {
370 llvm::errs() << "No checks enabled.\n";
371 return 1;
372 }
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +0000373 llvm::outs() << "Enabled checks:";
Benjamin Kramer51a9cc92016-06-15 15:46:10 +0000374 for (const auto &CheckName : EnabledChecks)
Alexander Kornienko16ac6ce2014-03-05 13:14:32 +0000375 llvm::outs() << "\n " << CheckName;
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +0000376 llvm::outs() << "\n\n";
377 return 0;
378 }
379
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000380 if (DumpConfig) {
Alexander Kornienko6e0cbc82014-09-12 08:53:36 +0000381 EffectiveOptions.CheckOptions = getCheckOptions(EffectiveOptions);
Alexander Kornienko65eccb42015-08-17 10:03:27 +0000382 llvm::outs() << configurationAsText(
383 ClangTidyOptions::getDefaults().mergeWith(
384 EffectiveOptions))
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000385 << "\n";
386 return 0;
387 }
388
Alexander Kornienkofbf92582014-06-02 20:32:06 +0000389 if (EnabledChecks.empty()) {
390 llvm::errs() << "Error: no checks enabled.\n";
391 llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true);
392 return 1;
393 }
394
Alexander Kornienko65eccb42015-08-17 10:03:27 +0000395 if (PathList.empty()) {
396 llvm::errs() << "Error: no input files specified.\n";
397 llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true);
398 return 1;
399 }
400
Samuel Benzaquenaedd9942014-10-23 17:23:20 +0000401 ProfileData Profile;
402
Alexander Kornienko25613202017-04-06 13:41:29 +0000403 ClangTidyContext Context(std::move(OwningOptionsProvider));
404 runClangTidy(Context, OptionsParser.getCompilations(), PathList,
405 EnableCheckProfile ? &Profile : nullptr);
406 ArrayRef<ClangTidyError> Errors = Context.getErrors();
Alexander Kornienko5eac3c62014-11-03 14:06:31 +0000407 bool FoundErrors =
408 std::find_if(Errors.begin(), Errors.end(), [](const ClangTidyError &E) {
409 return E.DiagLevel == ClangTidyError::Error;
410 }) != Errors.end();
411
412 const bool DisableFixes = Fix && FoundErrors && !FixErrors;
413
Jonathan Roelofsd60388a2016-01-13 17:36:41 +0000414 unsigned WErrorCount = 0;
415
Alexander Kornienko5eac3c62014-11-03 14:06:31 +0000416 // -fix-errors implies -fix.
Alexander Kornienko25613202017-04-06 13:41:29 +0000417 handleErrors(Context, (FixErrors || Fix) && !DisableFixes, WErrorCount);
Daniel Jasperd07c8402013-07-29 08:19:24 +0000418
Alexander Kornienko4153da22014-09-04 15:19:49 +0000419 if (!ExportFixes.empty() && !Errors.empty()) {
Benjamin Kramerfb98b742014-09-04 10:31:23 +0000420 std::error_code EC;
421 llvm::raw_fd_ostream OS(ExportFixes, EC, llvm::sys::fs::F_None);
422 if (EC) {
423 llvm::errs() << "Error opening output file: " << EC.message() << '\n';
424 return 1;
425 }
Alexander Kornienko563de792017-01-03 14:36:13 +0000426 exportReplacements(FilePath.str(), Errors, OS);
Benjamin Kramerfb98b742014-09-04 10:31:23 +0000427 }
428
Ehsan Akhgarib7418d32017-02-09 18:32:02 +0000429 if (!Quiet) {
Alexander Kornienko25613202017-04-06 13:41:29 +0000430 printStats(Context.getStats());
Ehsan Akhgarib7418d32017-02-09 18:32:02 +0000431 if (DisableFixes)
432 llvm::errs()
433 << "Found compiler errors, but -fix-errors was not specified.\n"
434 "Fixes have NOT been applied.\n\n";
435 }
Alexander Kornienko5eac3c62014-11-03 14:06:31 +0000436
Samuel Benzaquenaedd9942014-10-23 17:23:20 +0000437 if (EnableCheckProfile)
438 printProfileData(Profile, llvm::errs());
439
Jonathan Roelofsd60388a2016-01-13 17:36:41 +0000440 if (WErrorCount) {
Ehsan Akhgarib7418d32017-02-09 18:32:02 +0000441 if (!Quiet) {
442 StringRef Plural = WErrorCount == 1 ? "" : "s";
443 llvm::errs() << WErrorCount << " warning" << Plural << " treated as error"
444 << Plural << "\n";
445 }
Jonathan Roelofsd60388a2016-01-13 17:36:41 +0000446 return WErrorCount;
447 }
448
Daniel Jasperd07c8402013-07-29 08:19:24 +0000449 return 0;
450}
Daniel Jasper89bbab02013-08-04 15:56:30 +0000451
Aaron Ballmanea2f90c2015-10-02 13:27:19 +0000452// This anchor is used to force the linker to link the CERTModule.
453extern volatile int CERTModuleAnchorSource;
454static int LLVM_ATTRIBUTE_UNUSED CERTModuleAnchorDestination =
455 CERTModuleAnchorSource;
456
Piotr Padlewski5625f652016-04-29 17:58:29 +0000457// This anchor is used to force the linker to link the BoostModule.
458extern volatile int BoostModuleAnchorSource;
459static int LLVM_ATTRIBUTE_UNUSED BoostModuleAnchorDestination =
460 BoostModuleAnchorSource;
461
Daniel Jasper89bbab02013-08-04 15:56:30 +0000462// This anchor is used to force the linker to link the LLVMModule.
463extern volatile int LLVMModuleAnchorSource;
Alexander Kornienkoe1292f82015-08-19 16:54:51 +0000464static int LLVM_ATTRIBUTE_UNUSED LLVMModuleAnchorDestination =
465 LLVMModuleAnchorSource;
Daniel Jasper89bbab02013-08-04 15:56:30 +0000466
Aaron Ballmanaaa40802015-10-06 13:31:00 +0000467// This anchor is used to force the linker to link the CppCoreGuidelinesModule.
468extern volatile int CppCoreGuidelinesModuleAnchorSource;
469static int LLVM_ATTRIBUTE_UNUSED CppCoreGuidelinesModuleAnchorDestination =
470 CppCoreGuidelinesModuleAnchorSource;
471
Daniel Jasper89bbab02013-08-04 15:56:30 +0000472// This anchor is used to force the linker to link the GoogleModule.
473extern volatile int GoogleModuleAnchorSource;
Alexander Kornienkoe1292f82015-08-19 16:54:51 +0000474static int LLVM_ATTRIBUTE_UNUSED GoogleModuleAnchorDestination =
475 GoogleModuleAnchorSource;
Daniel Jasper89bbab02013-08-04 15:56:30 +0000476
Alexander Kornienko16ac6ce2014-03-05 13:14:32 +0000477// This anchor is used to force the linker to link the MiscModule.
478extern volatile int MiscModuleAnchorSource;
Alexander Kornienkoe1292f82015-08-19 16:54:51 +0000479static int LLVM_ATTRIBUTE_UNUSED MiscModuleAnchorDestination =
480 MiscModuleAnchorSource;
Alexander Kornienko16ac6ce2014-03-05 13:14:32 +0000481
Alexander Kornienkofc650862015-08-14 13:17:11 +0000482// This anchor is used to force the linker to link the ModernizeModule.
483extern volatile int ModernizeModuleAnchorSource;
Alexander Kornienkoe1292f82015-08-19 16:54:51 +0000484static int LLVM_ATTRIBUTE_UNUSED ModernizeModuleAnchorDestination =
485 ModernizeModuleAnchorSource;
Alexander Kornienkofc650862015-08-14 13:17:11 +0000486
Alexander Kornienko5e0a50c2016-08-02 20:29:35 +0000487// This anchor is used to force the linker to link the MPIModule.
488extern volatile int MPIModuleAnchorSource;
489static int LLVM_ATTRIBUTE_UNUSED MPIModuleAnchorDestination =
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000490 MPIModuleAnchorSource;
Alexander Kornienko5e0a50c2016-08-02 20:29:35 +0000491
Alexander Kornienkob959f4c2015-12-30 10:24:40 +0000492// This anchor is used to force the linker to link the PerformanceModule.
493extern volatile int PerformanceModuleAnchorSource;
494static int LLVM_ATTRIBUTE_UNUSED PerformanceModuleAnchorDestination =
495 PerformanceModuleAnchorSource;
496
Alexander Kornienko2192a8e2014-10-26 01:41:14 +0000497// This anchor is used to force the linker to link the ReadabilityModule.
498extern volatile int ReadabilityModuleAnchorSource;
Alexander Kornienkoe1292f82015-08-19 16:54:51 +0000499static int LLVM_ATTRIBUTE_UNUSED ReadabilityModuleAnchorDestination =
500 ReadabilityModuleAnchorSource;
Alexander Kornienko2192a8e2014-10-26 01:41:14 +0000501
Aaron Ballmandbdbabf2017-03-19 17:23:23 +0000502// This anchor is used to force the linker to link the HICPPModule.
503extern volatile int HICPPModuleAnchorSource;
504static int LLVM_ATTRIBUTE_UNUSED HICPPModuleAnchorDestination =
505 HICPPModuleAnchorSource;
Jonathan Coe3032d3c2017-02-06 22:57:14 +0000506
Daniel Jasper89bbab02013-08-04 15:56:30 +0000507} // namespace tidy
508} // namespace clang
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000509
510int main(int argc, const char **argv) {
511 return clang::tidy::clangTidyMain(argc, argv);
512}