22#include "clang/Tooling/CommonOptionsParser.h"
23#include "llvm/ADT/StringSet.h"
24#include "llvm/Support/CommandLine.h"
25#include "llvm/Support/InitLLVM.h"
26#include "llvm/Support/PluginLoader.h"
27#include "llvm/Support/Process.h"
28#include "llvm/Support/Signals.h"
29#include "llvm/Support/TargetSelect.h"
30#include "llvm/Support/WithColor.h"
31#include "llvm/TargetParser/Host.h"
37static cl::desc
desc(StringRef Description) {
return {Description.ltrim()}; }
41static cl::extrahelp
CommonHelp(CommonOptionsParser::HelpMessage);
44 A large number of options or source files can be passed as parameter files
45 by use '@parameter-file' in the command line.
49 clang-tidy attempts to read configuration for each source file from a
50 .clang-tidy file located in the closest parent directory of the source
51 file. The .clang-tidy file is specified in YAML format. If any configuration
52 options have a corresponding command-line option, command-line option takes
55 The following configuration options may be used in a .clang-tidy file:
57 CheckOptions - List of key-value pairs defining check-specific
60 some-check.SomeOption: 'some value'
61 Checks - Same as '--checks'. Additionally, the list of
62 globs can be specified as a list instead of a
64 CustomChecks - Array of user defined checks based on
66 ExcludeHeaderFilterRegex - Same as '--exclude-header-filter'.
67 ExtraArgs - Same as '--extra-arg'.
68 ExtraArgsBefore - Same as '--extra-arg-before'.
69 FormatStyle - Same as '--format-style'.
70 HeaderFileExtensions - File extensions to consider to determine if a
71 given diagnostic is located in a header file.
72 HeaderFilterRegex - Same as '--header-filter'.
73 ImplementationFileExtensions - File extensions to consider to determine if a
74 given diagnostic is located in an
76 InheritParentConfig - If this option is true in a config file, the
77 configuration file in the parent directory
78 (if any exists) will be taken and the current
79 config file will be applied on top of the
81 RemovedArgs - Same as '--removed-arg'.
82 SystemHeaders - Same as '--system-headers'.
83 UseColor - Same as '--use-color'.
84 User - Specifies the name or e-mail of the user
85 running clang-tidy. This option is used, for
86 example, to place the correct user name in
87 TODO() comments in the relevant check.
88 WarningsAsErrors - Same as '--warnings-as-errors'.
90 The effective configuration can be inspected using --dump-config:
92 $ clang-tidy --dump-config
94 Checks: '-*,some-check'
96 HeaderFileExtensions: ['', 'h','hh','hpp','hxx']
97 ImplementationFileExtensions: ['c','cc','cpp','cxx']
98 HeaderFilterRegex: '.*'
100 InheritParentConfig: true
103 some-check.SomeOption: 'some value'
109 "clang-diagnostic-*";
111static cl::opt<std::string>
Checks(
"checks",
desc(R
"(
112Comma-separated list of globs with optional '-'
113prefix. Globs are processed in order of
114appearance in the list. Globs without '-'
115prefix add checks with matching names to the
116set, globs with the '-' prefix remove checks
117with matching names from the set of enabled
118checks. This option's value is appended to the
119value of the 'Checks' option in .clang-tidy
125Upgrades warnings to errors. Same format as
127This option's value is appended to the value of
128the 'WarningsAsErrors' option in .clang-tidy
135Regular expression matching the names of the
136headers to output diagnostics from. The default
137value is '.*', i.e. diagnostics from all non-system
138headers are displayed by default. Diagnostics
139from the main file of each translation unit are
141Can be used together with -line-filter.
142This option overrides the 'HeaderFilterRegex'
143option in .clang-tidy file, if any.
150Regular expression matching the names of the
151headers to exclude diagnostics from. Diagnostics
152from the main file of each translation unit are
154Must be used together with --header-filter.
155Can be used together with -line-filter.
156This option overrides the 'ExcludeHeaderFilterRegex'
157option in .clang-tidy file, if any.
163Display the errors from system headers.
164This option overrides the 'SystemHeaders' option
165in .clang-tidy file, if any.
170List of files and line ranges to output diagnostics from.
171The range is inclusive on both ends. Can be used together
172with -header-filter. The format of the list is a JSON
173array of objects. For example:
176 {"name":"file1.cpp","lines":[[1,3],[5,7]]},
180This will output diagnostics from 'file1.cpp' only for
181the line ranges [1,3] and [5,7], as well as all from the
187static cl::opt<bool>
Fix(
"fix",
desc(R
"(
188Apply suggested fixes. Without -fix-errors
189clang-tidy will bail out if any compilation
195Apply suggested fixes even if compilation
196errors were found. If compiler errors have
197attached fix-its, clang-tidy will apply them as
203If a warning has no fix, but a single fix can
204be found through an associated diagnostic note,
206Specifying this flag will implicitly enable the
212Style for formatting code around applied fixes:
213 - 'none' (default) turns off formatting
214 - 'file' (literally 'file', not a placeholder)
215 uses .clang-format file in the closest parent
217 - '{ <json> }' specifies options inline, e.g.
218 -format-style='{BasedOnStyle: llvm, IndentWidth: 8}'
219 - 'llvm', 'google', 'webkit', 'mozilla'
220See clang-format documentation for the up-to-date
221information about formatting styles and options.
222This option overrides the 'FormatStyle` option in
223.clang-tidy file, if any.
229List all enabled checks and exit. Use with
230-checks=* to list all available checks.
235For each enabled check explains, where it is
236enabled, i.e. in clang-tidy binary, command
237line or a specific configuration file.
241static cl::opt<std::string>
Config(
"config",
desc(R
"(
242Specifies a configuration in YAML/JSON format:
243 -config="{Checks: '*',
244 CheckOptions: {x: y}}"
245When the value is empty, clang-tidy will
246attempt to find a file named .clang-tidy for
247each source file in its parent directories.
252Specify the path of .clang-tidy or custom config file:
253 e.g. --config-file=/some/path/myTidyConfigFile
254This option internally works exactly the same way as
255 --config option after reading specified config file.
256Use either --config-file or --config, not both.
262Dumps configuration in the YAML format to
263stdout. This option can be used along with a
264file name (and '--' if the file is outside of a
265project with configured compilation database).
266The configuration used for this file will be
268Use along with -checks=* to include
269configuration of all checks.
274Enable per-check timing profiles, and print a
281By default reports are printed in tabulated
282format to stderr. When this option is passed,
283these per-TU profiles are instead stored as JSON.
285 cl::value_desc("prefix"),
293 cl::init(
false), cl::Hidden,
298Enables preprocessor-level module header parsing
299for C++20 and above, empowering specific checks
300to detect macro definitions within modules. This
301feature may cause performance and parsing issues
302and is therefore considered experimental.
308YAML file to store suggested fixes in. The
309stored fixes can be applied to the input source
310code with clang-apply-replacements.
312 cl::value_desc("filename"),
315static cl::opt<bool>
Quiet(
"quiet",
desc(R
"(
316Run clang-tidy in quiet mode. This suppresses
317printing statistics about ignored warnings and
318warnings treated as errors if the respective
319options are specified.
324Overlay the virtual filesystem described by file
325over the real file system.
327 cl::value_desc("filename"),
331Use colors in diagnostics. If not set, colors
332will be used if the terminal connected to
333standard output supports colors.
334This option overrides the 'UseColor' option in
335.clang-tidy file, if any.
340Check the config files to ensure each check and
341option is recognized without running any checks.
346Allow empty enabled checks. This suppresses
347the "no checks enabled" error when disabling
354Enable experimental clang-query based
356see https://clang.llvm.org/extra/clang-tidy/QueryBasedCustomChecks.html.
362List of arguments to remove from the command
363line sent to the compiler. Please note that
364removing arguments might change the semantic
365of the analyzed code, possibly leading to
366compiler errors, false positives or
367false negatives. This option is applied
368before --extra-arg and --extra-arg-before)"),
375 llvm::errs() <<
"Suppressed " << Stats.
errorsIgnored() <<
" warnings (";
376 StringRef Separator =
"";
383 <<
" due to line filter";
392 <<
" with check filters";
393 llvm::errs() <<
").\n";
395 llvm::errs() <<
"Use -header-filter=.* or leave it as default to display "
396 "errors from all non-system headers. Use -system-headers "
397 "to display errors from system headers as well.\n";
401static std::unique_ptr<ClangTidyOptionsProvider>
405 llvm::errs() <<
"Invalid LineFilter: " << Err.message() <<
"\n\nUsage:\n";
406 llvm::cl::PrintHelpMessage(
false,
true);
417 DefaultOptions.
User = llvm::sys::Process::GetEnv(
"USER");
419 if (!DefaultOptions.
User)
420 DefaultOptions.
User = llvm::sys::Process::GetEnv(
"USERNAME");
423 if (
Checks.getNumOccurrences() > 0)
435 if (
UseColor.getNumOccurrences() > 0)
441 [&](StringRef Configuration,
442 StringRef Source) -> std::unique_ptr<ClangTidyOptionsProvider> {
443 llvm::ErrorOr<ClangTidyOptions> ParsedConfig =
446 return std::make_unique<ConfigOptionsProvider>(
447 std::move(GlobalOptions),
449 std::move(*ParsedConfig), std::move(OverrideOptions), std::move(FS));
450 llvm::errs() <<
"Error: invalid configuration specified.\n"
451 << ParsedConfig.getError().message() <<
"\n";
456 if (
Config.getNumOccurrences() > 0) {
457 llvm::errs() <<
"Error: --config-file and --config are "
458 "mutually exclusive. Specify only one.\n";
462 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
Text =
464 if (
const std::error_code EC =
Text.getError()) {
465 llvm::errs() <<
"Error: can't read config-file '" <<
ConfigFile
466 <<
"': " << EC.message() <<
"\n";
470 return LoadConfig((*Text)->getBuffer(),
ConfigFile);
473 if (
Config.getNumOccurrences() > 0)
474 return LoadConfig(
Config,
"<command-line-config>");
476 return std::make_unique<FileOptionsProvider>(
477 std::move(GlobalOptions), std::move(DefaultOptions),
478 std::move(OverrideOptions), std::move(FS));
481static llvm::IntrusiveRefCntPtr<vfs::FileSystem>
482getVfsFromFile(
const std::string &OverlayFile, vfs::FileSystem &BaseFS) {
483 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buffer =
484 BaseFS.getBufferForFile(OverlayFile);
486 llvm::errs() <<
"Can't load virtual filesystem overlay file '"
487 << OverlayFile <<
"': " << Buffer.getError().message()
492 IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getVFSFromYAML(
493 std::move(Buffer.get()),
nullptr, OverlayFile);
495 llvm::errs() <<
"Error: invalid virtual filesystem overlay file '"
496 << OverlayFile <<
"'.\n";
502static StringRef
closest(StringRef Value,
const StringSet<> &Allowed) {
503 unsigned MaxEdit = 5U;
505 for (
auto Item : Allowed.keys()) {
506 const unsigned Cur =
Value.edit_distance_insensitive(Item,
true, MaxEdit);
517static bool verifyChecks(
const StringSet<> &AllChecks, StringRef CheckGlob,
520 bool AnyInvalid =
false;
521 for (
const auto &Item : Globs.getItems()) {
522 if (Item.Text.starts_with(
"clang-diagnostic"))
524 if (llvm::none_of(AllChecks.keys(),
525 [&Item](StringRef S) { return Item.Regex.match(S); })) {
527 if (Item.Text.contains(
'*'))
528 llvm::WithColor::warning(llvm::errs(), Source)
529 <<
"check glob '" << Item.Text <<
"' doesn't match any known check"
532 llvm::raw_ostream &Output =
533 llvm::WithColor::warning(llvm::errs(), Source)
534 <<
"unknown check '" << Item.Text <<
'\'';
535 const llvm::StringRef Closest =
closest(Item.Text, AllChecks);
536 if (!Closest.empty())
537 Output <<
"; did you mean '" << Closest <<
'\'';
546 const std::vector<std::string> &HeaderFileExtensions,
547 const std::vector<std::string> &ImplementationFileExtensions,
549 bool AnyInvalid =
false;
550 for (
const auto &HeaderExtension : HeaderFileExtensions) {
551 for (
const auto &ImplementationExtension : ImplementationFileExtensions) {
552 if (HeaderExtension == ImplementationExtension) {
554 auto &Output = llvm::WithColor::warning(llvm::errs(), Source)
555 <<
"HeaderFileExtension '" << HeaderExtension <<
'\''
556 <<
" is the same as ImplementationFileExtension '"
557 << ImplementationExtension <<
'\'';
565static bool verifyOptions(
const llvm::StringSet<> &ValidOptions,
568 bool AnyInvalid =
false;
569 for (
auto Key : OptionMap.keys()) {
570 if (ValidOptions.contains(Key))
573 auto &Output = llvm::WithColor::warning(llvm::errs(), Source)
574 <<
"unknown check option '" <<
Key <<
'\'';
575 const llvm::StringRef Closest =
closest(Key, ValidOptions);
576 if (!Closest.empty())
577 Output <<
"; did you mean '" << Closest <<
'\'';
583static SmallString<256>
makeAbsolute(llvm::StringRef Input) {
586 SmallString<256> AbsolutePath(Input);
587 if (
const std::error_code EC = llvm::sys::fs::make_absolute(AbsolutePath)) {
588 llvm::errs() <<
"Can't make absolute path from " << Input <<
": "
589 << EC.message() <<
"\n";
594static llvm::IntrusiveRefCntPtr<vfs::OverlayFileSystem>
createBaseFS() {
595 llvm::IntrusiveRefCntPtr<vfs::OverlayFileSystem> BaseFS(
596 new vfs::OverlayFileSystem(vfs::getRealFileSystem()));
599 IntrusiveRefCntPtr<vfs::FileSystem> VfsFromFile =
603 BaseFS->pushOverlay(std::move(VfsFromFile));
609 const llvm::InitLLVM
X(argc, argv);
610 SmallVector<const char *> Args{argv, argv + argc};
613 llvm::BumpPtrAllocator Alloc;
614 llvm::cl::TokenizerCallback Tokenizer =
615 llvm::Triple(llvm::sys::getProcessTriple()).isOSWindows()
616 ? llvm::cl::TokenizeWindowsCommandLine
617 : llvm::cl::TokenizeGNUCommandLine;
618 llvm::cl::ExpansionContext ECtx(Alloc, Tokenizer);
619 if (llvm::Error Err = ECtx.expandResponseFiles(Args)) {
620 llvm::WithColor::error() << llvm::toString(std::move(Err)) <<
"\n";
623 argc =
static_cast<int>(Args.size());
627 if (cl::Option *LoadOpt = cl::getRegisteredOptions().
lookup(
"load"))
630 llvm::Expected<CommonOptionsParser> OptionsParser =
633 if (!OptionsParser) {
634 llvm::WithColor::error() << llvm::toString(OptionsParser.takeError());
638 const llvm::IntrusiveRefCntPtr<vfs::OverlayFileSystem> BaseFS =
644 auto *OptionsProvider = OwningOptionsProvider.get();
645 if (!OptionsProvider)
650 StringRef FileName(
"dummy");
651 auto PathList = OptionsParser->getSourcePathList();
652 if (!PathList.empty()) {
653 FileName = PathList.front();
656 const SmallString<256> FilePath =
makeAbsolute(FileName);
659 const std::vector<std::string> EnabledChecks =
665 std::vector<clang::tidy::ClangTidyOptionsProvider::OptionsSource>
666 RawOptions = OptionsProvider->getRawOptions(FilePath);
667 for (
const std::string &Check : EnabledChecks) {
668 for (
const auto &[Opts, Source] : llvm::reverse(RawOptions)) {
670 llvm::outs() <<
"'" << Check <<
"' is enabled in the " << Source
681 llvm::errs() <<
"No checks enabled.\n";
684 llvm::outs() <<
"Enabled checks:";
685 for (
const auto &CheckName : EnabledChecks)
686 llvm::outs() <<
"\n " << CheckName;
687 llvm::outs() <<
"\n\n";
703 const std::vector<ClangTidyOptionsProvider::OptionsSource> RawOptions =
704 OptionsProvider->getRawOptions(FileName);
707 bool AnyInvalid =
false;
708 for (
const auto &[Opts, Source] : RawOptions) {
710 AnyInvalid |=
verifyChecks(Valid.Checks, *Opts.Checks, Source);
711 if (Opts.HeaderFileExtensions && Opts.ImplementationFileExtensions)
714 *Opts.ImplementationFileExtensions, Source);
715 AnyInvalid |=
verifyOptions(Valid.Options, Opts.CheckOptions, Source);
719 llvm::outs() <<
"No config errors detected.\n";
723 if (EnabledChecks.empty()) {
725 llvm::outs() <<
"No checks enabled.\n";
728 llvm::errs() <<
"Error: no checks enabled.\n";
729 llvm::cl::PrintHelpMessage(
false,
true);
733 if (PathList.empty()) {
734 llvm::errs() <<
"Error: no input files specified.\n";
735 llvm::cl::PrintHelpMessage(
false,
true);
739 llvm::InitializeAllTargetInfos();
740 llvm::InitializeAllTargetMCs();
741 llvm::InitializeAllAsmParsers();
746 std::vector<ClangTidyError> Errors =
747 runClangTidy(Context, OptionsParser->getCompilations(), PathList, BaseFS,
749 const bool FoundErrors = llvm::any_of(Errors, [](
const ClangTidyError &E) {
750 return E.DiagLevel == ClangTidyError::Error;
758 const bool DisableFixes = FoundErrors && !
FixErrors;
760 unsigned WErrorCount = 0;
763 WErrorCount, BaseFS);
767 llvm::raw_fd_ostream OS(
ExportFixes, EC, llvm::sys::fs::OF_None);
769 llvm::errs() <<
"Error opening output file: " << EC.message() <<
'\n';
777 if (DisableFixes && Behaviour !=
FB_NoFix)
779 <<
"Found compiler errors, but -fix-errors was not specified.\n"
780 "Fixes have NOT been applied.\n\n";
785 const StringRef Plural = WErrorCount == 1 ?
"" :
"s";
786 llvm::errs() << WErrorCount <<
" warning" << Plural <<
" treated as error"
801 llvm::errs() <<
"Found compiler error(s).\n";
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::list< std::string > RemovedArgs("removed-arg", desc(R"(
List of arguments to remove from the command
line sent to the compiler. Please note that
removing arguments might change the semantic
of the analyzed code, possibly leading to
compiler errors, false positives or
false negatives. This option is applied
before --extra-arg and --extra-arg-before)"), 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::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))
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.
RemovedArgs - Same as '--removed-arg'.
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'
...
)")
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.
virtual bool contains(StringRef S) const
Returns true if the pattern matches S.
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.
@ FB_NoFix
Don't try to apply any fix.
@ FB_FixNotes
Apply fixes found in notes.
@ FB_Fix
Only apply fixes added to warnings.
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.
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< ArgList > RemovedArgs
Remove command line arguments sent to the compiler matching this.
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.
unsigned ErrorsIgnoredCheckFilter
unsigned ErrorsIgnoredNonUserCode
unsigned ErrorsIgnoredLineFilter
unsigned ErrorsIgnoredNOLINT
unsigned errorsIgnored() const