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