blob: 7d2f662a9a020d5635f32e08d5d3b4e4d7acf0b8 [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
Ilya Biryukova67b3662018-01-23 12:31:06 +0000212static cl::opt<std::string> VfsOverlay("vfsoverlay", cl::desc(R"(
213Overlay the virtual filesystem described by file
214over the real file system.
215)"),
216 cl::value_desc("filename"),
217 cl::cat(ClangTidyCategory));
218
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000219namespace clang {
220namespace tidy {
221
222static void printStats(const ClangTidyStats &Stats) {
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000223 if (Stats.errorsIgnored()) {
224 llvm::errs() << "Suppressed " << Stats.errorsIgnored() << " warnings (";
Alexander Kornienko5d174542014-05-07 09:06:53 +0000225 StringRef Separator = "";
226 if (Stats.ErrorsIgnoredNonUserCode) {
227 llvm::errs() << Stats.ErrorsIgnoredNonUserCode << " in non-user code";
228 Separator = ", ";
229 }
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000230 if (Stats.ErrorsIgnoredLineFilter) {
231 llvm::errs() << Separator << Stats.ErrorsIgnoredLineFilter
232 << " due to line filter";
233 Separator = ", ";
234 }
Alexander Kornienko5d174542014-05-07 09:06:53 +0000235 if (Stats.ErrorsIgnoredNOLINT) {
236 llvm::errs() << Separator << Stats.ErrorsIgnoredNOLINT << " NOLINT";
237 Separator = ", ";
238 }
239 if (Stats.ErrorsIgnoredCheckFilter)
240 llvm::errs() << Separator << Stats.ErrorsIgnoredCheckFilter
241 << " with check filters";
242 llvm::errs() << ").\n";
243 if (Stats.ErrorsIgnoredNonUserCode)
Aaron Ballman5a4892b2015-07-27 13:41:30 +0000244 llvm::errs() << "Use -header-filter=.* to display errors from all "
Alexander Kornienkof1d54eb2016-02-08 00:19:29 +0000245 "non-system headers. Use -system-headers to display "
246 "errors from system headers as well.\n";
Alexander Kornienko5d174542014-05-07 09:06:53 +0000247 }
248}
249
Samuel Benzaquenaedd9942014-10-23 17:23:20 +0000250static void printProfileData(const ProfileData &Profile,
251 llvm::raw_ostream &OS) {
252 // Time is first to allow for sorting by it.
253 std::vector<std::pair<llvm::TimeRecord, StringRef>> Timers;
254 TimeRecord Total;
255
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000256 for (const auto &P : Profile.Records) {
Samuel Benzaquenaedd9942014-10-23 17:23:20 +0000257 Timers.emplace_back(P.getValue(), P.getKey());
258 Total += P.getValue();
259 }
260
261 std::sort(Timers.begin(), Timers.end());
262
263 std::string Line = "===" + std::string(73, '-') + "===\n";
264 OS << Line;
265
266 if (Total.getUserTime())
267 OS << " ---User Time---";
268 if (Total.getSystemTime())
269 OS << " --System Time--";
270 if (Total.getProcessTime())
271 OS << " --User+System--";
272 OS << " ---Wall Time---";
273 if (Total.getMemUsed())
274 OS << " ---Mem---";
275 OS << " --- Name ---\n";
276
277 // Loop through all of the timing data, printing it out.
278 for (auto I = Timers.rbegin(), E = Timers.rend(); I != E; ++I) {
279 I->first.print(Total, OS);
280 OS << I->second << '\n';
281 }
282
283 Total.print(Total, OS);
284 OS << "Total\n";
285 OS << Line << "\n";
286 OS.flush();
287}
288
Benjamin Kramere7103712015-03-23 12:49:15 +0000289static std::unique_ptr<ClangTidyOptionsProvider> createOptionsProvider() {
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000290 ClangTidyGlobalOptions GlobalOptions;
291 if (std::error_code Err = parseLineFilter(LineFilter, GlobalOptions)) {
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000292 llvm::errs() << "Invalid LineFilter: " << Err.message() << "\n\nUsage:\n";
293 llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true);
Alexander Kornienkoeff4e922014-09-26 11:42:29 +0000294 return nullptr;
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000295 }
Alexander Kornienko33a9bcc2014-04-29 15:20:10 +0000296
Alexander Kornienkoe9951542014-09-24 18:36:03 +0000297 ClangTidyOptions DefaultOptions;
298 DefaultOptions.Checks = DefaultChecks;
Jonathan Roelofsd60388a2016-01-13 17:36:41 +0000299 DefaultOptions.WarningsAsErrors = "";
Alexander Kornienkoe9951542014-09-24 18:36:03 +0000300 DefaultOptions.HeaderFilterRegex = HeaderFilter;
Alexander Kornienko37f7abe2014-10-28 22:16:13 +0000301 DefaultOptions.SystemHeaders = SystemHeaders;
Alexander Kornienkoe9951542014-09-24 18:36:03 +0000302 DefaultOptions.AnalyzeTemporaryDtors = AnalyzeTemporaryDtors;
Alexander Kornienko25613202017-04-06 13:41:29 +0000303 DefaultOptions.FormatStyle = FormatStyle;
Alexander Kornienkoe9951542014-09-24 18:36:03 +0000304 DefaultOptions.User = llvm::sys::Process::GetEnv("USER");
305 // USERNAME is used on Windows.
306 if (!DefaultOptions.User)
307 DefaultOptions.User = llvm::sys::Process::GetEnv("USERNAME");
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000308
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000309 ClangTidyOptions OverrideOptions;
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000310 if (Checks.getNumOccurrences() > 0)
311 OverrideOptions.Checks = Checks;
Jonathan Roelofsd60388a2016-01-13 17:36:41 +0000312 if (WarningsAsErrors.getNumOccurrences() > 0)
313 OverrideOptions.WarningsAsErrors = WarningsAsErrors;
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000314 if (HeaderFilter.getNumOccurrences() > 0)
315 OverrideOptions.HeaderFilterRegex = HeaderFilter;
Alexander Kornienko37f7abe2014-10-28 22:16:13 +0000316 if (SystemHeaders.getNumOccurrences() > 0)
317 OverrideOptions.SystemHeaders = SystemHeaders;
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000318 if (AnalyzeTemporaryDtors.getNumOccurrences() > 0)
319 OverrideOptions.AnalyzeTemporaryDtors = AnalyzeTemporaryDtors;
Alexander Kornienko25613202017-04-06 13:41:29 +0000320 if (FormatStyle.getNumOccurrences() > 0)
321 OverrideOptions.FormatStyle = FormatStyle;
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000322
Alexander Kornienkoeff4e922014-09-26 11:42:29 +0000323 if (!Config.empty()) {
324 if (llvm::ErrorOr<ClangTidyOptions> ParsedConfig =
325 parseConfiguration(Config)) {
Haojian Wu12e6b8f2016-04-27 09:15:01 +0000326 return llvm::make_unique<ConfigOptionsProvider>(
327 GlobalOptions,
328 ClangTidyOptions::getDefaults().mergeWith(DefaultOptions),
329 *ParsedConfig, OverrideOptions);
Alexander Kornienkoeff4e922014-09-26 11:42:29 +0000330 } else {
331 llvm::errs() << "Error: invalid configuration specified.\n"
332 << ParsedConfig.getError().message() << "\n";
333 return nullptr;
334 }
335 }
336 return llvm::make_unique<FileOptionsProvider>(GlobalOptions, DefaultOptions,
337 OverrideOptions);
338}
339
Ilya Biryukova67b3662018-01-23 12:31:06 +0000340llvm::IntrusiveRefCntPtr<vfs::FileSystem>
341getVfsOverlayFromFile(const std::string &OverlayFile) {
342 llvm::IntrusiveRefCntPtr<vfs::OverlayFileSystem> OverlayFS(
343 new vfs::OverlayFileSystem(vfs::getRealFileSystem()));
344 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buffer =
345 OverlayFS->getBufferForFile(OverlayFile);
346 if (!Buffer) {
347 llvm::errs() << "Can't load virtual filesystem overlay file '"
348 << OverlayFile << "': " << Buffer.getError().message()
349 << ".\n";
350 return nullptr;
351 }
352
353 IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getVFSFromYAML(
354 std::move(Buffer.get()), /*DiagHandler*/ nullptr, OverlayFile);
355 if (!FS) {
356 llvm::errs() << "Error: invalid virtual filesystem overlay file '"
357 << OverlayFile << "'.\n";
358 return nullptr;
359 }
360 OverlayFS->pushOverlay(FS);
361 return OverlayFS;
362}
363
Benjamin Kramere7103712015-03-23 12:49:15 +0000364static int clangTidyMain(int argc, const char **argv) {
Alexander Kornienko65eccb42015-08-17 10:03:27 +0000365 CommonOptionsParser OptionsParser(argc, argv, ClangTidyCategory,
366 cl::ZeroOrMore);
Alexander Kornienkoeff4e922014-09-26 11:42:29 +0000367
Alexander Kornienko25613202017-04-06 13:41:29 +0000368 auto OwningOptionsProvider = createOptionsProvider();
369 auto *OptionsProvider = OwningOptionsProvider.get();
Alexander Kornienkoeff4e922014-09-26 11:42:29 +0000370 if (!OptionsProvider)
371 return 1;
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000372
Alexander Kornienko65eccb42015-08-17 10:03:27 +0000373 StringRef FileName("dummy");
374 auto PathList = OptionsParser.getSourcePathList();
375 if (!PathList.empty()) {
Alexander Kornienkoe0c900e2015-08-17 11:27:11 +0000376 FileName = PathList.front();
Alexander Kornienko65eccb42015-08-17 10:03:27 +0000377 }
Haojian Wud1218752016-07-11 07:47:04 +0000378
379 SmallString<256> FilePath(FileName);
380 if (std::error_code EC = llvm::sys::fs::make_absolute(FilePath)) {
381 llvm::errs() << "Can't make absolute path from " << FileName << ": "
382 << EC.message() << "\n";
383 }
384 ClangTidyOptions EffectiveOptions = OptionsProvider->getOptions(FilePath);
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000385 std::vector<std::string> EnabledChecks = getCheckNames(EffectiveOptions);
Alexander Kornienkofbf92582014-06-02 20:32:06 +0000386
Haojian Wu12e6b8f2016-04-27 09:15:01 +0000387 if (ExplainConfig) {
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000388 // FIXME: Show other ClangTidyOptions' fields, like ExtraArg.
Haojian Wu12e6b8f2016-04-27 09:15:01 +0000389 std::vector<clang::tidy::ClangTidyOptionsProvider::OptionsSource>
Haojian Wud1218752016-07-11 07:47:04 +0000390 RawOptions = OptionsProvider->getRawOptions(FilePath);
Haojian Wu12e6b8f2016-04-27 09:15:01 +0000391 for (const std::string &Check : EnabledChecks) {
392 for (auto It = RawOptions.rbegin(); It != RawOptions.rend(); ++It) {
393 if (It->first.Checks && GlobList(*It->first.Checks).contains(Check)) {
394 llvm::outs() << "'" << Check << "' is enabled in the " << It->second
395 << ".\n";
396 break;
397 }
398 }
399 }
400 return 0;
401 }
402
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +0000403 if (ListChecks) {
Alexander Kornienko493db092016-04-27 11:45:14 +0000404 if (EnabledChecks.empty()) {
405 llvm::errs() << "No checks enabled.\n";
406 return 1;
407 }
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +0000408 llvm::outs() << "Enabled checks:";
Benjamin Kramer51a9cc92016-06-15 15:46:10 +0000409 for (const auto &CheckName : EnabledChecks)
Alexander Kornienko16ac6ce2014-03-05 13:14:32 +0000410 llvm::outs() << "\n " << CheckName;
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +0000411 llvm::outs() << "\n\n";
412 return 0;
413 }
414
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000415 if (DumpConfig) {
Alexander Kornienko6e0cbc82014-09-12 08:53:36 +0000416 EffectiveOptions.CheckOptions = getCheckOptions(EffectiveOptions);
Alexander Kornienko65eccb42015-08-17 10:03:27 +0000417 llvm::outs() << configurationAsText(
418 ClangTidyOptions::getDefaults().mergeWith(
419 EffectiveOptions))
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000420 << "\n";
421 return 0;
422 }
423
Alexander Kornienkofbf92582014-06-02 20:32:06 +0000424 if (EnabledChecks.empty()) {
425 llvm::errs() << "Error: no checks enabled.\n";
426 llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true);
Rafael Espindolad63b2f32017-09-08 00:33:39 +0000427 return 0;
Alexander Kornienkofbf92582014-06-02 20:32:06 +0000428 }
429
Alexander Kornienko65eccb42015-08-17 10:03:27 +0000430 if (PathList.empty()) {
431 llvm::errs() << "Error: no input files specified.\n";
432 llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true);
Rafael Espindolad63b2f32017-09-08 00:33:39 +0000433 return 0;
Alexander Kornienko65eccb42015-08-17 10:03:27 +0000434 }
Ilya Biryukova67b3662018-01-23 12:31:06 +0000435 llvm::IntrusiveRefCntPtr<vfs::FileSystem> BaseFS(
436 VfsOverlay.empty() ? vfs::getRealFileSystem()
437 : getVfsOverlayFromFile(VfsOverlay));
438 if (!BaseFS)
439 return 1;
Alexander Kornienko65eccb42015-08-17 10:03:27 +0000440
Samuel Benzaquenaedd9942014-10-23 17:23:20 +0000441 ProfileData Profile;
442
Zachary Turnerfbdca1d2017-10-20 23:00:51 +0000443 llvm::InitializeAllTargetInfos();
444 llvm::InitializeAllTargetMCs();
445 llvm::InitializeAllAsmParsers();
446
Alexander Kornienko25613202017-04-06 13:41:29 +0000447 ClangTidyContext Context(std::move(OwningOptionsProvider));
Ilya Biryukova67b3662018-01-23 12:31:06 +0000448 runClangTidy(Context, OptionsParser.getCompilations(), PathList, BaseFS,
Alexander Kornienko25613202017-04-06 13:41:29 +0000449 EnableCheckProfile ? &Profile : nullptr);
450 ArrayRef<ClangTidyError> Errors = Context.getErrors();
Alexander Kornienko5eac3c62014-11-03 14:06:31 +0000451 bool FoundErrors =
452 std::find_if(Errors.begin(), Errors.end(), [](const ClangTidyError &E) {
453 return E.DiagLevel == ClangTidyError::Error;
454 }) != Errors.end();
455
456 const bool DisableFixes = Fix && FoundErrors && !FixErrors;
457
Jonathan Roelofsd60388a2016-01-13 17:36:41 +0000458 unsigned WErrorCount = 0;
459
Alexander Kornienko5eac3c62014-11-03 14:06:31 +0000460 // -fix-errors implies -fix.
Ilya Biryukova67b3662018-01-23 12:31:06 +0000461 handleErrors(Context, (FixErrors || Fix) && !DisableFixes, WErrorCount,
462 BaseFS);
Daniel Jasperd07c8402013-07-29 08:19:24 +0000463
Alexander Kornienko4153da22014-09-04 15:19:49 +0000464 if (!ExportFixes.empty() && !Errors.empty()) {
Benjamin Kramerfb98b742014-09-04 10:31:23 +0000465 std::error_code EC;
466 llvm::raw_fd_ostream OS(ExportFixes, EC, llvm::sys::fs::F_None);
467 if (EC) {
468 llvm::errs() << "Error opening output file: " << EC.message() << '\n';
469 return 1;
470 }
Alexander Kornienko563de792017-01-03 14:36:13 +0000471 exportReplacements(FilePath.str(), Errors, OS);
Benjamin Kramerfb98b742014-09-04 10:31:23 +0000472 }
473
Ehsan Akhgarib7418d32017-02-09 18:32:02 +0000474 if (!Quiet) {
Alexander Kornienko25613202017-04-06 13:41:29 +0000475 printStats(Context.getStats());
Ehsan Akhgarib7418d32017-02-09 18:32:02 +0000476 if (DisableFixes)
477 llvm::errs()
478 << "Found compiler errors, but -fix-errors was not specified.\n"
479 "Fixes have NOT been applied.\n\n";
480 }
Alexander Kornienko5eac3c62014-11-03 14:06:31 +0000481
Samuel Benzaquenaedd9942014-10-23 17:23:20 +0000482 if (EnableCheckProfile)
483 printProfileData(Profile, llvm::errs());
484
Jonathan Roelofsd60388a2016-01-13 17:36:41 +0000485 if (WErrorCount) {
Ehsan Akhgarib7418d32017-02-09 18:32:02 +0000486 if (!Quiet) {
487 StringRef Plural = WErrorCount == 1 ? "" : "s";
488 llvm::errs() << WErrorCount << " warning" << Plural << " treated as error"
489 << Plural << "\n";
490 }
Jonathan Roelofsd60388a2016-01-13 17:36:41 +0000491 return WErrorCount;
492 }
493
Daniel Jasperd07c8402013-07-29 08:19:24 +0000494 return 0;
495}
Daniel Jasper89bbab02013-08-04 15:56:30 +0000496
Aaron Ballmanea2f90c2015-10-02 13:27:19 +0000497// This anchor is used to force the linker to link the CERTModule.
498extern volatile int CERTModuleAnchorSource;
499static int LLVM_ATTRIBUTE_UNUSED CERTModuleAnchorDestination =
500 CERTModuleAnchorSource;
501
Haojian Wu40571b7c2018-03-09 10:47:14 +0000502// This anchor is used to force the linker to link the AbseilModule.
503extern volatile int AbseilModuleAnchorSource;
504static int LLVM_ATTRIBUTE_UNUSED AbseilModuleAnchorDestination =
505 AbseilModuleAnchorSource;
506
Piotr Padlewski5625f652016-04-29 17:58:29 +0000507// This anchor is used to force the linker to link the BoostModule.
508extern volatile int BoostModuleAnchorSource;
509static int LLVM_ATTRIBUTE_UNUSED BoostModuleAnchorDestination =
510 BoostModuleAnchorSource;
511
Gabor Horvath829e75a2017-07-14 12:15:55 +0000512// This anchor is used to force the linker to link the BugproneModule.
513extern volatile int BugproneModuleAnchorSource;
514static int LLVM_ATTRIBUTE_UNUSED BugproneModuleAnchorDestination =
515 BugproneModuleAnchorSource;
516
Daniel Jasper89bbab02013-08-04 15:56:30 +0000517// This anchor is used to force the linker to link the LLVMModule.
518extern volatile int LLVMModuleAnchorSource;
Alexander Kornienkoe1292f82015-08-19 16:54:51 +0000519static int LLVM_ATTRIBUTE_UNUSED LLVMModuleAnchorDestination =
520 LLVMModuleAnchorSource;
Daniel Jasper89bbab02013-08-04 15:56:30 +0000521
Aaron Ballmanaaa40802015-10-06 13:31:00 +0000522// This anchor is used to force the linker to link the CppCoreGuidelinesModule.
523extern volatile int CppCoreGuidelinesModuleAnchorSource;
524static int LLVM_ATTRIBUTE_UNUSED CppCoreGuidelinesModuleAnchorDestination =
525 CppCoreGuidelinesModuleAnchorSource;
526
Julie Hockettc12d7532018-03-13 21:24:08 +0000527// This anchor is used to force the linker to link the FuchsiaModule.
Aaron Ballmand3d78b92017-11-28 21:09:25 +0000528extern volatile int FuchsiaModuleAnchorSource;
529static int LLVM_ATTRIBUTE_UNUSED FuchsiaModuleAnchorDestination =
530 FuchsiaModuleAnchorSource;
531
532// This anchor is used to force the linker to link the GoogleModule.
Daniel Jasper89bbab02013-08-04 15:56:30 +0000533extern volatile int GoogleModuleAnchorSource;
Alexander Kornienkoe1292f82015-08-19 16:54:51 +0000534static int LLVM_ATTRIBUTE_UNUSED GoogleModuleAnchorDestination =
535 GoogleModuleAnchorSource;
Daniel Jasper89bbab02013-08-04 15:56:30 +0000536
Yan Wang36206202017-06-23 21:37:29 +0000537// This anchor is used to force the linker to link the AndroidModule.
538extern volatile int AndroidModuleAnchorSource;
539static int LLVM_ATTRIBUTE_UNUSED AndroidModuleAnchorDestination =
540 AndroidModuleAnchorSource;
541
Alexander Kornienko16ac6ce2014-03-05 13:14:32 +0000542// This anchor is used to force the linker to link the MiscModule.
543extern volatile int MiscModuleAnchorSource;
Alexander Kornienkoe1292f82015-08-19 16:54:51 +0000544static int LLVM_ATTRIBUTE_UNUSED MiscModuleAnchorDestination =
545 MiscModuleAnchorSource;
Alexander Kornienko16ac6ce2014-03-05 13:14:32 +0000546
Alexander Kornienkofc650862015-08-14 13:17:11 +0000547// This anchor is used to force the linker to link the ModernizeModule.
548extern volatile int ModernizeModuleAnchorSource;
Alexander Kornienkoe1292f82015-08-19 16:54:51 +0000549static int LLVM_ATTRIBUTE_UNUSED ModernizeModuleAnchorDestination =
550 ModernizeModuleAnchorSource;
Alexander Kornienkofc650862015-08-14 13:17:11 +0000551
Alexander Kornienko5e0a50c2016-08-02 20:29:35 +0000552// This anchor is used to force the linker to link the MPIModule.
553extern volatile int MPIModuleAnchorSource;
554static int LLVM_ATTRIBUTE_UNUSED MPIModuleAnchorDestination =
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000555 MPIModuleAnchorSource;
Alexander Kornienko5e0a50c2016-08-02 20:29:35 +0000556
Alexander Kornienkob959f4c2015-12-30 10:24:40 +0000557// This anchor is used to force the linker to link the PerformanceModule.
558extern volatile int PerformanceModuleAnchorSource;
559static int LLVM_ATTRIBUTE_UNUSED PerformanceModuleAnchorDestination =
560 PerformanceModuleAnchorSource;
561
Fangrui Songc0e768d2018-03-07 16:57:42 +0000562// This anchor is used to force the linker to link the PortabilityModule.
563extern volatile int PortabilityModuleAnchorSource;
564static int LLVM_ATTRIBUTE_UNUSED PortabilityModuleAnchorDestination =
565 PortabilityModuleAnchorSource;
566
Alexander Kornienko2192a8e2014-10-26 01:41:14 +0000567// This anchor is used to force the linker to link the ReadabilityModule.
568extern volatile int ReadabilityModuleAnchorSource;
Alexander Kornienkoe1292f82015-08-19 16:54:51 +0000569static int LLVM_ATTRIBUTE_UNUSED ReadabilityModuleAnchorDestination =
570 ReadabilityModuleAnchorSource;
Alexander Kornienko2192a8e2014-10-26 01:41:14 +0000571
Haojian Wuabcd64c2017-10-26 08:23:20 +0000572// This anchor is used to force the linker to link the ObjCModule.
573extern volatile int ObjCModuleAnchorSource;
574static int LLVM_ATTRIBUTE_UNUSED ObjCModuleAnchorDestination =
575 ObjCModuleAnchorSource;
576
Aaron Ballmandbdbabf2017-03-19 17:23:23 +0000577// This anchor is used to force the linker to link the HICPPModule.
578extern volatile int HICPPModuleAnchorSource;
579static int LLVM_ATTRIBUTE_UNUSED HICPPModuleAnchorDestination =
580 HICPPModuleAnchorSource;
Jonathan Coe3032d3c2017-02-06 22:57:14 +0000581
Daniel Jasper89bbab02013-08-04 15:56:30 +0000582} // namespace tidy
583} // namespace clang
Alexander Kornienkoc28c32d2014-09-10 11:43:09 +0000584
585int main(int argc, const char **argv) {
586 return clang::tidy::clangTidyMain(argc, argv);
587}