clang-tools 22.0.0git
ClangTidyMain.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file This file implements a clang-tidy tool.
10///
11/// This tool uses the Clang Tooling infrastructure, see
12/// https://clang.llvm.org/docs/HowToSetupToolingForLLVM.html
13/// for details on setting it up with LLVM source tree.
14///
15//===----------------------------------------------------------------------===//
16
17#include "ClangTidyMain.h"
18#include "../ClangTidy.h"
20#include "../GlobList.h"
21#include "clang/Tooling/CommonOptionsParser.h"
22#include "llvm/ADT/StringSet.h"
23#include "llvm/Support/CommandLine.h"
24#include "llvm/Support/InitLLVM.h"
25#include "llvm/Support/PluginLoader.h"
26#include "llvm/Support/Process.h"
27#include "llvm/Support/Signals.h"
28#include "llvm/Support/TargetSelect.h"
29#include "llvm/Support/WithColor.h"
30#include "llvm/TargetParser/Host.h"
31#include <optional>
32
33using namespace clang::tooling;
34using namespace llvm;
35
36static cl::desc desc(StringRef Description) { return {Description.ltrim()}; }
37
38static cl::OptionCategory ClangTidyCategory("clang-tidy options");
39
40static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
41static cl::extrahelp ClangTidyParameterFileHelp(R"(
42Parameters files:
43 A large number of options or source files can be passed as parameter files
44 by use '@parameter-file' in the command line.
45)");
46static cl::extrahelp ClangTidyHelp(R"(
47Configuration files:
48 clang-tidy attempts to read configuration for each source file from a
49 .clang-tidy file located in the closest parent directory of the source
50 file. The .clang-tidy file is specified in YAML format. If any configuration
51 options have a corresponding command-line option, command-line option takes
52 precedence.
53
54 The following configuration options may be used in a .clang-tidy file:
56 CheckOptions - List of key-value pairs defining check-specific
57 options. Example:
58 CheckOptions:
59 some-check.SomeOption: 'some value'
60 Checks - Same as '--checks'. Additionally, the list of
61 globs can be specified as a list instead of a
62 string.
63 CustomChecks - Array of user defined checks based on
64 Clang-Query syntax.
65 ExcludeHeaderFilterRegex - Same as '--exclude-header-filter'.
66 ExtraArgs - Same as '--extra-arg'.
67 ExtraArgsBefore - Same as '--extra-arg-before'.
68 FormatStyle - Same as '--format-style'.
69 HeaderFileExtensions - File extensions to consider to determine if a
70 given diagnostic is located in a header file.
71 HeaderFilterRegex - Same as '--header-filter'.
72 ImplementationFileExtensions - File extensions to consider to determine if a
73 given diagnostic is located in an
74 implementation file.
75 InheritParentConfig - If this option is true in a config file, the
76 configuration file in the parent directory
77 (if any exists) will be taken and the current
78 config file will be applied on top of the
79 parent one.
80 SystemHeaders - Same as '--system-headers'.
81 UseColor - Same as '--use-color'.
82 User - Specifies the name or e-mail of the user
83 running clang-tidy. This option is used, for
84 example, to place the correct user name in
85 TODO() comments in the relevant check.
86 WarningsAsErrors - Same as '--warnings-as-errors'.
88 The effective configuration can be inspected using --dump-config:
89
90 $ clang-tidy --dump-config
91 ---
92 Checks: '-*,some-check'
93 WarningsAsErrors: ''
94 HeaderFileExtensions: ['', 'h','hh','hpp','hxx']
95 ImplementationFileExtensions: ['c','cc','cpp','cxx']
96 HeaderFilterRegex: '.*'
97 FormatStyle: none
98 InheritParentConfig: true
99 User: user
100 CheckOptions:
101 some-check.SomeOption: 'some value'
102 ...
103
104)");
105
106const char DefaultChecks[] = // Enable these checks by default:
107 "clang-diagnostic-*," // * compiler diagnostics
108 "clang-analyzer-*"; // * Static Analyzer checks
109
110static cl::opt<std::string> Checks("checks", desc(R"(
111Comma-separated list of globs with optional '-'
112prefix. Globs are processed in order of
113appearance in the list. Globs without '-'
114prefix add checks with matching names to the
115set, globs with the '-' prefix remove checks
116with matching names from the set of enabled
117checks. This option's value is appended to the
118value of the 'Checks' option in .clang-tidy
119file, if any.
120)"),
121 cl::init(""), cl::cat(ClangTidyCategory));
122
123static cl::opt<std::string> WarningsAsErrors("warnings-as-errors", desc(R"(
124Upgrades warnings to errors. Same format as
125'-checks'.
126This option's value is appended to the value of
127the 'WarningsAsErrors' option in .clang-tidy
128file, if any.
129)"),
130 cl::init(""),
131 cl::cat(ClangTidyCategory));
133static cl::opt<std::string> HeaderFilter("header-filter", desc(R"(
134Regular expression matching the names of the
135headers to output diagnostics from. The default
136value is '.*', i.e. diagnostics from all non-system
137headers are displayed by default. Diagnostics
138from the main file of each translation unit are
139always displayed.
140Can be used together with -line-filter.
141This option overrides the 'HeaderFilterRegex'
142option in .clang-tidy file, if any.
143)"),
144 cl::init(".*"),
145 cl::cat(ClangTidyCategory));
146
147static cl::opt<std::string> ExcludeHeaderFilter("exclude-header-filter",
149Regular expression matching the names of the
150headers to exclude diagnostics from. Diagnostics
151from the main file of each translation unit are
152always displayed.
153Must be used together with --header-filter.
154Can be used together with -line-filter.
155This option overrides the 'ExcludeHeaderFilterRegex'
156option in .clang-tidy file, if any.
157)"),
158 cl::init(""),
159 cl::cat(ClangTidyCategory));
160
161static cl::opt<bool> SystemHeaders("system-headers", desc(R"(
162Display the errors from system headers.
163This option overrides the 'SystemHeaders' option
164in .clang-tidy file, if any.
165)"),
166 cl::init(false), cl::cat(ClangTidyCategory));
167
168static cl::opt<std::string> LineFilter("line-filter", desc(R"(
169List of files and line ranges to output diagnostics from.
170The range is inclusive on both ends. Can be used together
171with -header-filter. The format of the list is a JSON
172array of objects. For example:
173
174 [
175 {"name":"file1.cpp","lines":[[1,3],[5,7]]},
176 {"name":"file2.h"}
178
179This will output diagnostics from 'file1.cpp' only for
180the line ranges [1,3] and [5,7], as well as all from the
181entire 'file2.h'.
182)"),
183 cl::init(""),
184 cl::cat(ClangTidyCategory));
185
186static cl::opt<bool> Fix("fix", desc(R"(
187Apply suggested fixes. Without -fix-errors
188clang-tidy will bail out if any compilation
189errors were found.
190)"),
191 cl::init(false), cl::cat(ClangTidyCategory));
192
193static cl::opt<bool> FixErrors("fix-errors", desc(R"(
194Apply suggested fixes even if compilation
195errors were found. If compiler errors have
196attached fix-its, clang-tidy will apply them as
197well.
198)"),
199 cl::init(false), cl::cat(ClangTidyCategory));
200
201static cl::opt<bool> FixNotes("fix-notes", desc(R"(
202If a warning has no fix, but a single fix can
203be found through an associated diagnostic note,
204apply the fix.
205Specifying this flag will implicitly enable the
206'--fix' flag.
207)"),
208 cl::init(false), cl::cat(ClangTidyCategory));
209
210static cl::opt<std::string> FormatStyle("format-style", desc(R"(
211Style for formatting code around applied fixes:
212 - 'none' (default) turns off formatting
213 - 'file' (literally 'file', not a placeholder)
214 uses .clang-format file in the closest parent
215 directory
216 - '{ <json> }' specifies options inline, e.g.
217 -format-style='{BasedOnStyle: llvm, IndentWidth: 8}'
218 - 'llvm', 'google', 'webkit', 'mozilla'
219See clang-format documentation for the up-to-date
220information about formatting styles and options.
221This option overrides the 'FormatStyle` option in
222.clang-tidy file, if any.
223)"),
224 cl::init("none"),
225 cl::cat(ClangTidyCategory));
226
227static cl::opt<bool> ListChecks("list-checks", desc(R"(
228List all enabled checks and exit. Use with
229-checks=* to list all available checks.
230)"),
231 cl::init(false), cl::cat(ClangTidyCategory));
232
233static cl::opt<bool> ExplainConfig("explain-config", desc(R"(
234For each enabled check explains, where it is
235enabled, i.e. in clang-tidy binary, command
236line or a specific configuration file.
237)"),
238 cl::init(false), cl::cat(ClangTidyCategory));
239
240static cl::opt<std::string> Config("config", desc(R"(
241Specifies a configuration in YAML/JSON format:
242 -config="{Checks: '*',
243 CheckOptions: {x: y}}"
244When the value is empty, clang-tidy will
245attempt to find a file named .clang-tidy for
246each source file in its parent directories.
247)"),
248 cl::init(""), cl::cat(ClangTidyCategory));
249
250static cl::opt<std::string> ConfigFile("config-file", desc(R"(
251Specify the path of .clang-tidy or custom config file:
252 e.g. --config-file=/some/path/myTidyConfigFile
253This option internally works exactly the same way as
254 --config option after reading specified config file.
255Use either --config-file or --config, not both.
256)"),
257 cl::init(""),
258 cl::cat(ClangTidyCategory));
259
260static cl::opt<bool> DumpConfig("dump-config", desc(R"(
261Dumps configuration in the YAML format to
262stdout. This option can be used along with a
263file name (and '--' if the file is outside of a
264project with configured compilation database).
265The configuration used for this file will be
266printed.
267Use along with -checks=* to include
268configuration of all checks.
269)"),
270 cl::init(false), cl::cat(ClangTidyCategory));
271
272static cl::opt<bool> EnableCheckProfile("enable-check-profile", desc(R"(
273Enable per-check timing profiles, and print a
274report to stderr.
275)"),
276 cl::init(false),
277 cl::cat(ClangTidyCategory));
278
279static cl::opt<std::string> StoreCheckProfile("store-check-profile", desc(R"(
280By default reports are printed in tabulated
281format to stderr. When this option is passed,
282these per-TU profiles are instead stored as JSON.
283)"),
284 cl::value_desc("prefix"),
285 cl::cat(ClangTidyCategory));
286
287/// This option allows enabling the experimental alpha checkers from the static
288/// analyzer. This option is set to false and not visible in help, because it is
289/// highly not recommended for users.
290static cl::opt<bool>
291 AllowEnablingAnalyzerAlphaCheckers("allow-enabling-analyzer-alpha-checkers",
292 cl::init(false), cl::Hidden,
293 cl::cat(ClangTidyCategory));
294
295static cl::opt<bool> EnableModuleHeadersParsing("enable-module-headers-parsing",
296 desc(R"(
297Enables preprocessor-level module header parsing
298for C++20 and above, empowering specific checks
299to detect macro definitions within modules. This
300feature may cause performance and parsing issues
301and is therefore considered experimental.
302)"),
303 cl::init(false),
304 cl::cat(ClangTidyCategory));
305
306static cl::opt<std::string> ExportFixes("export-fixes", desc(R"(
307YAML file to store suggested fixes in. The
308stored fixes can be applied to the input source
309code with clang-apply-replacements.
310)"),
311 cl::value_desc("filename"),
312 cl::cat(ClangTidyCategory));
313
314static cl::opt<bool> Quiet("quiet", desc(R"(
315Run clang-tidy in quiet mode. This suppresses
316printing statistics about ignored warnings and
317warnings treated as errors if the respective
318options are specified.
319)"),
320 cl::init(false), cl::cat(ClangTidyCategory));
321
322static cl::opt<std::string> VfsOverlay("vfsoverlay", desc(R"(
323Overlay the virtual filesystem described by file
324over the real file system.
325)"),
326 cl::value_desc("filename"),
327 cl::cat(ClangTidyCategory));
328
329static cl::opt<bool> UseColor("use-color", desc(R"(
330Use colors in diagnostics. If not set, colors
331will be used if the terminal connected to
332standard output supports colors.
333This option overrides the 'UseColor' option in
334.clang-tidy file, if any.
335)"),
336 cl::init(false), cl::cat(ClangTidyCategory));
337
338static cl::opt<bool> VerifyConfig("verify-config", desc(R"(
339Check the config files to ensure each check and
340option is recognized without running any checks.
341)"),
342 cl::init(false), cl::cat(ClangTidyCategory));
343
344static cl::opt<bool> AllowNoChecks("allow-no-checks", desc(R"(
345Allow empty enabled checks. This suppresses
346the "no checks enabled" error when disabling
347all of the checks.
348)"),
349 cl::init(false), cl::cat(ClangTidyCategory));
350
351static cl::opt<bool> ExperimentalCustomChecks("experimental-custom-checks",
352 desc(R"(
353Enable experimental clang-query based
354custom checks.
355see https://clang.llvm.org/extra/clang-tidy/QueryBasedCustomChecks.html.
356)"),
357 cl::init(false),
358 cl::cat(ClangTidyCategory));
359
360namespace clang::tidy {
361
362static void printStats(const ClangTidyStats &Stats) {
363 if (Stats.errorsIgnored()) {
364 llvm::errs() << "Suppressed " << Stats.errorsIgnored() << " warnings (";
365 StringRef Separator = "";
366 if (Stats.ErrorsIgnoredNonUserCode) {
367 llvm::errs() << Stats.ErrorsIgnoredNonUserCode << " in non-user code";
368 Separator = ", ";
369 }
370 if (Stats.ErrorsIgnoredLineFilter) {
371 llvm::errs() << Separator << Stats.ErrorsIgnoredLineFilter
372 << " due to line filter";
373 Separator = ", ";
374 }
375 if (Stats.ErrorsIgnoredNOLINT) {
376 llvm::errs() << Separator << Stats.ErrorsIgnoredNOLINT << " NOLINT";
377 Separator = ", ";
378 }
379 if (Stats.ErrorsIgnoredCheckFilter)
380 llvm::errs() << Separator << Stats.ErrorsIgnoredCheckFilter
381 << " with check filters";
382 llvm::errs() << ").\n";
383 if (Stats.ErrorsIgnoredNonUserCode)
384 llvm::errs() << "Use -header-filter=.* or leave it as default to display "
385 "errors from all non-system headers. Use -system-headers "
386 "to display errors from system headers as well.\n";
387 }
388}
389
390static std::unique_ptr<ClangTidyOptionsProvider>
391createOptionsProvider(llvm::IntrusiveRefCntPtr<vfs::FileSystem> FS) {
392 ClangTidyGlobalOptions GlobalOptions;
393 if (const std::error_code Err = parseLineFilter(LineFilter, GlobalOptions)) {
394 llvm::errs() << "Invalid LineFilter: " << Err.message() << "\n\nUsage:\n";
395 llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true);
396 return nullptr;
397 }
398
399 ClangTidyOptions DefaultOptions;
400 DefaultOptions.Checks = DefaultChecks;
401 DefaultOptions.WarningsAsErrors = "";
402 DefaultOptions.HeaderFilterRegex = HeaderFilter;
404 DefaultOptions.SystemHeaders = SystemHeaders;
405 DefaultOptions.FormatStyle = FormatStyle;
406 DefaultOptions.User = llvm::sys::Process::GetEnv("USER");
407 // USERNAME is used on Windows.
408 if (!DefaultOptions.User)
409 DefaultOptions.User = llvm::sys::Process::GetEnv("USERNAME");
410
411 ClangTidyOptions OverrideOptions;
412 if (Checks.getNumOccurrences() > 0)
413 OverrideOptions.Checks = Checks;
414 if (WarningsAsErrors.getNumOccurrences() > 0)
415 OverrideOptions.WarningsAsErrors = WarningsAsErrors;
416 if (HeaderFilter.getNumOccurrences() > 0)
417 OverrideOptions.HeaderFilterRegex = HeaderFilter;
418 if (ExcludeHeaderFilter.getNumOccurrences() > 0)
420 if (SystemHeaders.getNumOccurrences() > 0)
421 OverrideOptions.SystemHeaders = SystemHeaders;
422 if (FormatStyle.getNumOccurrences() > 0)
423 OverrideOptions.FormatStyle = FormatStyle;
424 if (UseColor.getNumOccurrences() > 0)
425 OverrideOptions.UseColor = UseColor;
426
427 auto LoadConfig =
428 [&](StringRef Configuration,
429 StringRef Source) -> std::unique_ptr<ClangTidyOptionsProvider> {
430 llvm::ErrorOr<ClangTidyOptions> ParsedConfig =
431 parseConfiguration(MemoryBufferRef(Configuration, Source));
432 if (ParsedConfig)
433 return std::make_unique<ConfigOptionsProvider>(
434 std::move(GlobalOptions),
435 ClangTidyOptions::getDefaults().merge(DefaultOptions, 0),
436 std::move(*ParsedConfig), std::move(OverrideOptions), std::move(FS));
437 llvm::errs() << "Error: invalid configuration specified.\n"
438 << ParsedConfig.getError().message() << "\n";
439 return nullptr;
440 };
441
442 if (ConfigFile.getNumOccurrences() > 0) {
443 if (Config.getNumOccurrences() > 0) {
444 llvm::errs() << "Error: --config-file and --config are "
445 "mutually exclusive. Specify only one.\n";
446 return nullptr;
447 }
448
449 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
450 llvm::MemoryBuffer::getFile(ConfigFile);
451 if (const std::error_code EC = Text.getError()) {
452 llvm::errs() << "Error: can't read config-file '" << ConfigFile
453 << "': " << EC.message() << "\n";
454 return nullptr;
455 }
456
457 return LoadConfig((*Text)->getBuffer(), ConfigFile);
458 }
459
460 if (Config.getNumOccurrences() > 0)
461 return LoadConfig(Config, "<command-line-config>");
462
463 return std::make_unique<FileOptionsProvider>(
464 std::move(GlobalOptions), std::move(DefaultOptions),
465 std::move(OverrideOptions), std::move(FS));
466}
467
468static llvm::IntrusiveRefCntPtr<vfs::FileSystem>
469getVfsFromFile(const std::string &OverlayFile,
470 llvm::IntrusiveRefCntPtr<vfs::FileSystem> BaseFS) {
471 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buffer =
472 BaseFS->getBufferForFile(OverlayFile);
473 if (!Buffer) {
474 llvm::errs() << "Can't load virtual filesystem overlay file '"
475 << OverlayFile << "': " << Buffer.getError().message()
476 << ".\n";
477 return nullptr;
478 }
479
480 IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getVFSFromYAML(
481 std::move(Buffer.get()), /*DiagHandler*/ nullptr, OverlayFile);
482 if (!FS) {
483 llvm::errs() << "Error: invalid virtual filesystem overlay file '"
484 << OverlayFile << "'.\n";
485 return nullptr;
486 }
487 return FS;
488}
489
490static StringRef closest(StringRef Value, const StringSet<> &Allowed) {
491 unsigned MaxEdit = 5U;
492 StringRef Closest;
493 for (auto Item : Allowed.keys()) {
494 const unsigned Cur = Value.edit_distance_insensitive(Item, true, MaxEdit);
495 if (Cur < MaxEdit) {
496 Closest = Item;
497 MaxEdit = Cur;
498 }
499 }
500 return Closest;
501}
502
503static constexpr StringLiteral VerifyConfigWarningEnd = " [-verify-config]\n";
504
505static bool verifyChecks(const StringSet<> &AllChecks, StringRef CheckGlob,
506 StringRef Source) {
507 const GlobList Globs(CheckGlob);
508 bool AnyInvalid = false;
509 for (const auto &Item : Globs.getItems()) {
510 if (Item.Text.starts_with("clang-diagnostic"))
511 continue;
512 if (llvm::none_of(AllChecks.keys(),
513 [&Item](StringRef S) { return Item.Regex.match(S); })) {
514 AnyInvalid = true;
515 if (Item.Text.contains('*'))
516 llvm::WithColor::warning(llvm::errs(), Source)
517 << "check glob '" << Item.Text << "' doesn't match any known check"
519 else {
520 llvm::raw_ostream &Output =
521 llvm::WithColor::warning(llvm::errs(), Source)
522 << "unknown check '" << Item.Text << '\'';
523 const llvm::StringRef Closest = closest(Item.Text, AllChecks);
524 if (!Closest.empty())
525 Output << "; did you mean '" << Closest << '\'';
526 Output << VerifyConfigWarningEnd;
527 }
528 }
529 }
530 return AnyInvalid;
531}
532
533static bool verifyFileExtensions(
534 const std::vector<std::string> &HeaderFileExtensions,
535 const std::vector<std::string> &ImplementationFileExtensions,
536 StringRef Source) {
537 bool AnyInvalid = false;
538 for (const auto &HeaderExtension : HeaderFileExtensions) {
539 for (const auto &ImplementationExtension : ImplementationFileExtensions) {
540 if (HeaderExtension == ImplementationExtension) {
541 AnyInvalid = true;
542 auto &Output = llvm::WithColor::warning(llvm::errs(), Source)
543 << "HeaderFileExtension '" << HeaderExtension << '\''
544 << " is the same as ImplementationFileExtension '"
545 << ImplementationExtension << '\'';
546 Output << VerifyConfigWarningEnd;
547 }
548 }
549 }
550 return AnyInvalid;
551}
552
553static bool verifyOptions(const llvm::StringSet<> &ValidOptions,
554 const ClangTidyOptions::OptionMap &OptionMap,
555 StringRef Source) {
556 bool AnyInvalid = false;
557 for (auto Key : OptionMap.keys()) {
558 if (ValidOptions.contains(Key))
559 continue;
560 AnyInvalid = true;
561 auto &Output = llvm::WithColor::warning(llvm::errs(), Source)
562 << "unknown check option '" << Key << '\'';
563 const llvm::StringRef Closest = closest(Key, ValidOptions);
564 if (!Closest.empty())
565 Output << "; did you mean '" << Closest << '\'';
566 Output << VerifyConfigWarningEnd;
567 }
568 return AnyInvalid;
569}
570
571static SmallString<256> makeAbsolute(llvm::StringRef Input) {
572 if (Input.empty())
573 return {};
574 SmallString<256> AbsolutePath(Input);
575 if (const std::error_code EC = llvm::sys::fs::make_absolute(AbsolutePath)) {
576 llvm::errs() << "Can't make absolute path from " << Input << ": "
577 << EC.message() << "\n";
578 }
579 return AbsolutePath;
580}
581
582static llvm::IntrusiveRefCntPtr<vfs::OverlayFileSystem> createBaseFS() {
583 llvm::IntrusiveRefCntPtr<vfs::OverlayFileSystem> BaseFS(
584 new vfs::OverlayFileSystem(vfs::getRealFileSystem()));
585
586 if (!VfsOverlay.empty()) {
587 IntrusiveRefCntPtr<vfs::FileSystem> VfsFromFile =
588 getVfsFromFile(VfsOverlay, BaseFS);
589 if (!VfsFromFile)
590 return nullptr;
591 BaseFS->pushOverlay(std::move(VfsFromFile));
592 }
593 return BaseFS;
594}
595
596int clangTidyMain(int argc, const char **argv) {
597 const llvm::InitLLVM X(argc, argv);
598 SmallVector<const char *> Args{argv, argv + argc};
599
600 // expand parameters file to argc and argv.
601 llvm::BumpPtrAllocator Alloc;
602 llvm::cl::TokenizerCallback Tokenizer =
603 llvm::Triple(llvm::sys::getProcessTriple()).isOSWindows()
604 ? llvm::cl::TokenizeWindowsCommandLine
605 : llvm::cl::TokenizeGNUCommandLine;
606 llvm::cl::ExpansionContext ECtx(Alloc, Tokenizer);
607 if (llvm::Error Err = ECtx.expandResponseFiles(Args)) {
608 llvm::WithColor::error() << llvm::toString(std::move(Err)) << "\n";
609 return 1;
610 }
611 argc = static_cast<int>(Args.size());
612 argv = Args.data();
613
614 // Enable help for -load option, if plugins are enabled.
615 if (cl::Option *LoadOpt = cl::getRegisteredOptions().lookup("load"))
616 LoadOpt->addCategory(ClangTidyCategory);
617
618 llvm::Expected<CommonOptionsParser> OptionsParser =
619 CommonOptionsParser::create(argc, argv, ClangTidyCategory,
620 cl::ZeroOrMore);
621 if (!OptionsParser) {
622 llvm::WithColor::error() << llvm::toString(OptionsParser.takeError());
623 return 1;
624 }
625
626 const llvm::IntrusiveRefCntPtr<vfs::OverlayFileSystem> BaseFS =
627 createBaseFS();
628 if (!BaseFS)
629 return 1;
630
631 auto OwningOptionsProvider = createOptionsProvider(BaseFS);
632 auto *OptionsProvider = OwningOptionsProvider.get();
633 if (!OptionsProvider)
634 return 1;
635
636 const SmallString<256> ProfilePrefix = makeAbsolute(StoreCheckProfile);
637
638 StringRef FileName("dummy");
639 auto PathList = OptionsParser->getSourcePathList();
640 if (!PathList.empty()) {
641 FileName = PathList.front();
642 }
643
644 const SmallString<256> FilePath = makeAbsolute(FileName);
645 ClangTidyOptions EffectiveOptions = OptionsProvider->getOptions(FilePath);
646
647 const std::vector<std::string> EnabledChecks =
650
651 if (ExplainConfig) {
652 // FIXME: Show other ClangTidyOptions' fields, like ExtraArg.
653 std::vector<clang::tidy::ClangTidyOptionsProvider::OptionsSource>
654 RawOptions = OptionsProvider->getRawOptions(FilePath);
655 for (const std::string &Check : EnabledChecks) {
656 for (const auto &[Opts, Source] : llvm::reverse(RawOptions)) {
657 if (Opts.Checks && GlobList(*Opts.Checks).contains(Check)) {
658 llvm::outs() << "'" << Check << "' is enabled in the " << Source
659 << ".\n";
660 break;
661 }
662 }
663 }
664 return 0;
665 }
666
667 if (ListChecks) {
668 if (EnabledChecks.empty() && !AllowNoChecks) {
669 llvm::errs() << "No checks enabled.\n";
670 return 1;
671 }
672 llvm::outs() << "Enabled checks:";
673 for (const auto &CheckName : EnabledChecks)
674 llvm::outs() << "\n " << CheckName;
675 llvm::outs() << "\n\n";
676 return 0;
677 }
678
679 if (DumpConfig) {
680 EffectiveOptions.CheckOptions =
683 ClangTidyOptions OptionsToDump =
684 ClangTidyOptions::getDefaults().merge(EffectiveOptions, 0);
685 filterCheckOptions(OptionsToDump, EnabledChecks);
686 llvm::outs() << configurationAsText(OptionsToDump) << "\n";
687 return 0;
688 }
689
690 if (VerifyConfig) {
691 const std::vector<ClangTidyOptionsProvider::OptionsSource> RawOptions =
692 OptionsProvider->getRawOptions(FileName);
695 bool AnyInvalid = false;
696 for (const auto &[Opts, Source] : RawOptions) {
697 if (Opts.Checks)
698 AnyInvalid |= verifyChecks(Valid.Checks, *Opts.Checks, Source);
699 if (Opts.HeaderFileExtensions && Opts.ImplementationFileExtensions)
700 AnyInvalid |=
701 verifyFileExtensions(*Opts.HeaderFileExtensions,
702 *Opts.ImplementationFileExtensions, Source);
703 AnyInvalid |= verifyOptions(Valid.Options, Opts.CheckOptions, Source);
704 }
705 if (AnyInvalid)
706 return 1;
707 llvm::outs() << "No config errors detected.\n";
708 return 0;
709 }
710
711 if (EnabledChecks.empty()) {
712 if (AllowNoChecks) {
713 llvm::outs() << "No checks enabled.\n";
714 return 0;
715 }
716 llvm::errs() << "Error: no checks enabled.\n";
717 llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true);
718 return 1;
719 }
720
721 if (PathList.empty()) {
722 llvm::errs() << "Error: no input files specified.\n";
723 llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true);
724 return 1;
725 }
726
727 llvm::InitializeAllTargetInfos();
728 llvm::InitializeAllTargetMCs();
729 llvm::InitializeAllAsmParsers();
730
731 ClangTidyContext Context(
732 std::move(OwningOptionsProvider), AllowEnablingAnalyzerAlphaCheckers,
734 std::vector<ClangTidyError> Errors =
735 runClangTidy(Context, OptionsParser->getCompilations(), PathList, BaseFS,
736 FixNotes, EnableCheckProfile, ProfilePrefix, Quiet);
737 const bool FoundErrors = llvm::any_of(Errors, [](const ClangTidyError &E) {
738 return E.DiagLevel == ClangTidyError::Error;
739 });
740
741 // --fix-errors and --fix-notes imply --fix.
742 const FixBehaviour Behaviour = FixNotes ? FB_FixNotes
743 : (Fix || FixErrors) ? FB_Fix
744 : FB_NoFix;
745
746 const bool DisableFixes = FoundErrors && !FixErrors;
747
748 unsigned WErrorCount = 0;
749
750 handleErrors(Errors, Context, DisableFixes ? FB_NoFix : Behaviour,
751 WErrorCount, BaseFS);
752
753 if (!ExportFixes.empty() && !Errors.empty()) {
754 std::error_code EC;
755 llvm::raw_fd_ostream OS(ExportFixes, EC, llvm::sys::fs::OF_None);
756 if (EC) {
757 llvm::errs() << "Error opening output file: " << EC.message() << '\n';
758 return 1;
759 }
760 exportReplacements(FilePath.str(), Errors, OS);
761 }
762
763 if (!Quiet) {
764 printStats(Context.getStats());
765 if (DisableFixes && Behaviour != FB_NoFix)
766 llvm::errs()
767 << "Found compiler errors, but -fix-errors was not specified.\n"
768 "Fixes have NOT been applied.\n\n";
769 }
770
771 if (WErrorCount) {
772 if (!Quiet) {
773 const StringRef Plural = WErrorCount == 1 ? "" : "s";
774 llvm::errs() << WErrorCount << " warning" << Plural << " treated as error"
775 << Plural << "\n";
776 }
777 return 1;
778 }
779
780 if (FoundErrors) {
781 // TODO: Figure out when zero exit code should be used with -fix-errors:
782 // a. when a fix has been applied for an error
783 // b. when a fix has been applied for all errors
784 // c. some other condition.
785 // For now always returning zero when -fix-errors is used.
786 if (FixErrors)
787 return 0;
788 if (!Quiet)
789 llvm::errs() << "Found compiler error(s).\n";
790 return 1;
791 }
792
793 return 0;
794}
795
796} // namespace clang::tidy
static cl::opt< bool > UseColor("use-color", cl::desc(R"(Use colors in detailed AST output. If not set, colors will be used if the terminal connected to standard output supports colors.)"), cl::init(false), cl::cat(ClangQueryCategory))
static cl::opt< bool > EnableCheckProfile("enable-check-profile", desc(R"( Enable per-check timing profiles, and print a report to stderr. )"), cl::init(false), cl::cat(ClangTidyCategory))
static cl::opt< std::string > VfsOverlay("vfsoverlay", desc(R"( Overlay the virtual filesystem described by file over the real file system. )"), cl::value_desc("filename"), cl::cat(ClangTidyCategory))
static cl::opt< bool > FixNotes("fix-notes", desc(R"( If a warning has no fix, but a single fix can be found through an associated diagnostic note, apply the fix. Specifying this flag will implicitly enable the '--fix' flag. )"), cl::init(false), cl::cat(ClangTidyCategory))
static cl::opt< bool > Fix("fix", desc(R"( Apply suggested fixes. Without -fix-errors clang-tidy will bail out if any compilation errors were found. )"), cl::init(false), cl::cat(ClangTidyCategory))
static cl::opt< bool > ExplainConfig("explain-config", desc(R"( For each enabled check explains, where it is enabled, i.e. in clang-tidy binary, command line or a specific configuration file. )"), cl::init(false), cl::cat(ClangTidyCategory))
static cl::opt< bool > UseColor("use-color", desc(R"( Use colors in diagnostics. If not set, colors will be used if the terminal connected to standard output supports colors. This option overrides the 'UseColor' option in .clang-tidy file, if any. )"), cl::init(false), cl::cat(ClangTidyCategory))
static cl::opt< std::string > WarningsAsErrors("warnings-as-errors", desc(R"( Upgrades warnings to errors. Same format as '-checks'. This option's value is appended to the value of the 'WarningsAsErrors' option in .clang-tidy file, if any. )"), cl::init(""), cl::cat(ClangTidyCategory))
static cl::opt< bool > ExperimentalCustomChecks("experimental-custom-checks", desc(R"( Enable experimental clang-query based custom checks. see https://clang.llvm.org/extra/clang-tidy/QueryBasedCustomChecks.html. )"), cl::init(false), cl::cat(ClangTidyCategory))
static cl::extrahelp ClangTidyHelp(R"( Configuration files: clang-tidy attempts to read configuration for each source file from a .clang-tidy file located in the closest parent directory of the source file. The .clang-tidy file is specified in YAML format. If any configuration options have a corresponding command-line option, command-line option takes precedence. The following configuration options may be used in a .clang-tidy file: CheckOptions - List of key-value pairs defining check-specific options. Example: CheckOptions: some-check.SomeOption: 'some value' Checks - Same as '--checks'. Additionally, the list of globs can be specified as a list instead of a string. CustomChecks - Array of user defined checks based on Clang-Query syntax. ExcludeHeaderFilterRegex - Same as '--exclude-header-filter'. ExtraArgs - Same as '--extra-arg'. ExtraArgsBefore - Same as '--extra-arg-before'. FormatStyle - Same as '--format-style'. HeaderFileExtensions - File extensions to consider to determine if a given diagnostic is located in a header file. HeaderFilterRegex - Same as '--header-filter'. ImplementationFileExtensions - File extensions to consider to determine if a given diagnostic is located in an implementation file. InheritParentConfig - If this option is true in a config file, the configuration file in the parent directory (if any exists) will be taken and the current config file will be applied on top of the parent one. SystemHeaders - Same as '--system-headers'. UseColor - Same as '--use-color'. User - Specifies the name or e-mail of the user running clang-tidy. This option is used, for example, to place the correct user name in TODO() comments in the relevant check. WarningsAsErrors - Same as '--warnings-as-errors'. The effective configuration can be inspected using --dump-config: $ clang-tidy --dump-config --- Checks: '-*,some-check' WarningsAsErrors: '' HeaderFileExtensions: ['', 'h','hh','hpp','hxx'] ImplementationFileExtensions: ['c','cc','cpp','cxx'] HeaderFilterRegex: '.*' FormatStyle: none InheritParentConfig: true User: user CheckOptions: some-check.SomeOption: 'some value' ... )")
static cl::opt< std::string > ExcludeHeaderFilter("exclude-header-filter", desc(R"( Regular expression matching the names of the headers to exclude diagnostics from. Diagnostics from the main file of each translation unit are always displayed. Must be used together with --header-filter. Can be used together with -line-filter. This option overrides the 'ExcludeHeaderFilterRegex' option in .clang-tidy file, if any. )"), cl::init(""), cl::cat(ClangTidyCategory))
static cl::opt< bool > AllowEnablingAnalyzerAlphaCheckers("allow-enabling-analyzer-alpha-checkers", cl::init(false), cl::Hidden, cl::cat(ClangTidyCategory))
This option allows enabling the experimental alpha checkers from the static analyzer.
static cl::opt< std::string > ConfigFile("config-file", desc(R"( Specify the path of .clang-tidy or custom config file: e.g. --config-file=/some/path/myTidyConfigFile This option internally works exactly the same way as --config option after reading specified config file. Use either --config-file or --config, not both. )"), cl::init(""), cl::cat(ClangTidyCategory))
static cl::opt< bool > EnableModuleHeadersParsing("enable-module-headers-parsing", desc(R"( Enables preprocessor-level module header parsing for C++20 and above, empowering specific checks to detect macro definitions within modules. This feature may cause performance and parsing issues and is therefore considered experimental. )"), cl::init(false), cl::cat(ClangTidyCategory))
static cl::opt< std::string > Config("config", desc(R"( Specifies a configuration in YAML/JSON format: -config="{Checks:' *', CheckOptions:{x:y}}" When the value is empty, clang-tidy will attempt to find a file named .clang-tidy for each source file in its parent directories. )"), cl::init(""), cl::cat(ClangTidyCategory))
static cl::OptionCategory ClangTidyCategory("clang-tidy options")
static cl::opt< bool > FixErrors("fix-errors", desc(R"( Apply suggested fixes even if compilation errors were found. If compiler errors have attached fix-its, clang-tidy will apply them as well. )"), cl::init(false), cl::cat(ClangTidyCategory))
static cl::extrahelp ClangTidyParameterFileHelp(R"( Parameters files: A large number of options or source files can be passed as parameter files by use '@parameter-file' in the command line. )")
static cl::opt< bool > ListChecks("list-checks", desc(R"( List all enabled checks and exit. Use with -checks=* to list all available checks. )"), cl::init(false), cl::cat(ClangTidyCategory))
static cl::opt< std::string > HeaderFilter("header-filter", desc(R"( Regular expression matching the names of the headers to output diagnostics from. The default value is '.*', i.e. diagnostics from all non-system headers are displayed by default. Diagnostics from the main file of each translation unit are always displayed. Can be used together with -line-filter. This option overrides the 'HeaderFilterRegex' option in .clang-tidy file, if any. )"), cl::init(".*"), cl::cat(ClangTidyCategory))
static cl::opt< bool > DumpConfig("dump-config", desc(R"( Dumps configuration in the YAML format to stdout. This option can be used along with a file name (and '--' if the file is outside of a project with configured compilation database). The configuration used for this file will be printed. Use along with -checks=* to include configuration of all checks. )"), cl::init(false), cl::cat(ClangTidyCategory))
static cl::opt< bool > SystemHeaders("system-headers", desc(R"( Display the errors from system headers. This option overrides the 'SystemHeaders' option in .clang-tidy file, if any. )"), cl::init(false), cl::cat(ClangTidyCategory))
static cl::opt< std::string > LineFilter("line-filter", desc(R"( List of files and line ranges to output diagnostics from. The range is inclusive on both ends. Can be used together with -header-filter. The format of the list is a JSON array of objects. For example: [ {"name":"file1.cpp","lines":[[1,3],[5,7]]}, {"name":"file2.h"} ] This will output diagnostics from 'file1.cpp' only for the line ranges [1,3] and [5,7], as well as all from the entire 'file2.h'. )"), cl::init(""), cl::cat(ClangTidyCategory))
static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage)
static cl::opt< bool > VerifyConfig("verify-config", desc(R"( Check the config files to ensure each check and option is recognized without running any checks. )"), cl::init(false), cl::cat(ClangTidyCategory))
static cl::opt< std::string > Checks("checks", desc(R"( Comma-separated list of globs with optional '-' prefix. Globs are processed in order of appearance in the list. Globs without '-' prefix add checks with matching names to the set, globs with the '-' prefix remove checks with matching names from the set of enabled checks. This option's value is appended to the value of the 'Checks' option in .clang-tidy file, if any. )"), cl::init(""), cl::cat(ClangTidyCategory))
static cl::opt< bool > AllowNoChecks("allow-no-checks", desc(R"( Allow empty enabled checks. This suppresses the "no checks enabled" error when disabling all of the checks. )"), cl::init(false), cl::cat(ClangTidyCategory))
const char DefaultChecks[]
static cl::opt< bool > Quiet("quiet", desc(R"( Run clang-tidy in quiet mode. This suppresses printing statistics about ignored warnings and warnings treated as errors if the respective options are specified. )"), cl::init(false), cl::cat(ClangTidyCategory))
static cl::opt< std::string > ExportFixes("export-fixes", desc(R"( YAML file to store suggested fixes in. The stored fixes can be applied to the input source code with clang-apply-replacements. )"), cl::value_desc("filename"), cl::cat(ClangTidyCategory))
static cl::desc desc(StringRef Description)
static cl::opt< std::string > FormatStyle("format-style", desc(R"( Style for formatting code around applied fixes: - 'none' (default) turns off formatting - 'file' (literally 'file', not a placeholder) uses .clang-format file in the closest parent directory - '{ <json> }' specifies options inline, e.g. -format-style='{BasedOnStyle: llvm, IndentWidth: 8}' - 'llvm', 'google', 'webkit', 'mozilla' See clang-format documentation for the up-to-date information about formatting styles and options. This option overrides the 'FormatStyle` option in .clang-tidy file, if any. )"), cl::init("none"), cl::cat(ClangTidyCategory))
static cl::opt< std::string > StoreCheckProfile("store-check-profile", desc(R"( By default reports are printed in tabulated format to stderr. When this option is passed, these per-TU profiles are instead stored as JSON. )"), cl::value_desc("prefix"), cl::cat(ClangTidyCategory))
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Read-only set of strings represented as a list of positive and negative globs.
Definition GlobList.h:25
virtual bool contains(StringRef S) const
Returns true if the pattern matches S.
Definition GlobList.cpp:59
std::vector< std::string > lookup(const SymbolIndex &I, llvm::ArrayRef< SymbolID > IDs)
ChecksAndOptions getAllChecksAndOptions(bool AllowEnablingAnalyzerAlphaCheckers, bool ExperimentalCustomChecks)
std::error_code parseLineFilter(StringRef LineFilter, clang::tidy::ClangTidyGlobalOptions &Options)
Parses -line-filter option and stores it to the Options.
FixBehaviour
Controls what kind of fixes clang-tidy is allowed to apply.
Definition ClangTidy.h:103
@ FB_NoFix
Don't try to apply any fix.
Definition ClangTidy.h:105
@ FB_FixNotes
Apply fixes found in notes.
Definition ClangTidy.h:109
@ FB_Fix
Only apply fixes added to warnings.
Definition ClangTidy.h:107
std::vector< std::string > getCheckNames(const ClangTidyOptions &Options, bool AllowEnablingAnalyzerAlphaCheckers, bool ExperimentalCustomChecks)
Fills the list of check names that are enabled when the provided filters are applied.
int clangTidyMain(int argc, const char **argv)
static ClangTidyModuleRegistry::Add< altera::AlteraModule > X("altera-module", "Adds Altera FPGA OpenCL lint checks.")
static llvm::IntrusiveRefCntPtr< vfs::FileSystem > getVfsFromFile(const std::string &OverlayFile, llvm::IntrusiveRefCntPtr< vfs::FileSystem > BaseFS)
static void printStats(const ClangTidyStats &Stats)
static llvm::IntrusiveRefCntPtr< vfs::OverlayFileSystem > createBaseFS()
ClangTidyOptions::OptionMap getCheckOptions(const ClangTidyOptions &Options, bool AllowEnablingAnalyzerAlphaCheckers, bool ExperimentalCustomChecks)
Returns the effective check-specific options.
void handleErrors(llvm::ArrayRef< ClangTidyError > Errors, ClangTidyContext &Context, FixBehaviour Fix, unsigned &WarningsAsErrorsCount, llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > BaseFS)
Displays the found Errors to the users.
void filterCheckOptions(ClangTidyOptions &Options, const std::vector< std::string > &EnabledChecks)
Filters CheckOptions in Options to only include options specified in the EnabledChecks which is a sor...
static constexpr StringLiteral VerifyConfigWarningEnd
static SmallString< 256 > makeAbsolute(llvm::StringRef Input)
static bool verifyOptions(const llvm::StringSet<> &ValidOptions, const ClangTidyOptions::OptionMap &OptionMap, StringRef Source)
static std::unique_ptr< ClangTidyOptionsProvider > createOptionsProvider(llvm::IntrusiveRefCntPtr< vfs::FileSystem > FS)
static bool verifyFileExtensions(const std::vector< std::string > &HeaderFileExtensions, const std::vector< std::string > &ImplementationFileExtensions, StringRef Source)
std::string configurationAsText(const ClangTidyOptions &Options)
Serializes configuration to a YAML-encoded string.
llvm::ErrorOr< ClangTidyOptions > parseConfiguration(llvm::MemoryBufferRef Config)
Parses configuration from JSON and returns ClangTidyOptions or an error.
void exportReplacements(const llvm::StringRef MainFilePath, const std::vector< ClangTidyError > &Errors, raw_ostream &OS)
static bool verifyChecks(const StringSet<> &AllChecks, StringRef CheckGlob, StringRef Source)
std::vector< ClangTidyError > runClangTidy(clang::tidy::ClangTidyContext &Context, const CompilationDatabase &Compilations, ArrayRef< std::string > InputFiles, llvm::IntrusiveRefCntPtr< llvm::vfs::OverlayFileSystem > BaseFS, bool ApplyAnyFix, bool EnableCheckProfile, llvm::StringRef StoreCheckProfile, bool Quiet)
static StringRef closest(StringRef Value, const StringSet<> &Allowed)
Some operations such as code completion produce a set of candidates.
Definition Generators.h:66
OptionMap CheckOptions
Key-value mapping used to store check-specific options.
std::optional< std::string > HeaderFilterRegex
Output warnings from headers matching this filter.
std::optional< std::string > Checks
Checks filter.
std::optional< std::string > WarningsAsErrors
WarningsAsErrors filter.
std::optional< std::string > User
Specifies the name or e-mail of the user running clang-tidy.
std::optional< bool > UseColor
Use colors in diagnostics. If missing, it will be auto detected.
std::optional< bool > SystemHeaders
Output warnings from system headers matching HeaderFilterRegex.
std::optional< std::string > ExcludeHeaderFilterRegex
Exclude warnings from headers matching this filter, even if they match HeaderFilterRegex.
std::optional< std::string > FormatStyle
Format code around applied fixes with clang-format using this style.
A detected error complete with information to display diagnostic and automatic fix.
Contains options for clang-tidy.
ClangTidyOptions merge(const ClangTidyOptions &Other, unsigned Order) const
Creates a new ClangTidyOptions instance combined from all fields of this instance overridden by the f...
llvm::StringMap< ClangTidyValue > OptionMap
static ClangTidyOptions getDefaults()
These options are used for all settings that haven't been overridden by the OptionsProvider.
Contains displayed and ignored diagnostic counters for a ClangTidy run.