Skip to content

Commit 5cdc27e

Browse files
committed
[clang-tidy] New option to remove arguments from the command line
When using clang-tidy from a compilation database, some options might not be recognized by clang if the compilation database was generated for another compiler. This forces the user to add conditional code in their CMakeLists.txt to remove those arguments when using clang-tidy. A new option was added to the .clang-tidy config to remove those unknown flags without the need to generate a second compilation_commands.json Fixes #108455
1 parent 971b579 commit 5cdc27e

File tree

6 files changed

+48
-0
lines changed

6 files changed

+48
-0
lines changed

clang-tools-extra/clang-tidy/ClangTidy.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,30 @@ runClangTidy(clang::tidy::ClangTidyContext &Context,
552552
return AdjustedArgs;
553553
};
554554

555+
// Remove unwanted arguments passed to the compiler
556+
ArgumentsAdjuster CompilationArgsToIgnore =
557+
[&Context](const CommandLineArguments &Args, StringRef Filename) {
558+
ClangTidyOptions Opts = Context.getOptionsForFile(Filename);
559+
CommandLineArguments AdjustedArgs = Args;
560+
561+
if (Opts.CompilationArgsToRemoveRegex) {
562+
for (StringRef ArgToIgnore : *Opts.CompilationArgsToRemoveRegex) {
563+
llvm::Regex ArgToIgnoreRegex(ArgToIgnore);
564+
AdjustedArgs.erase(
565+
std::remove_if(AdjustedArgs.begin(), AdjustedArgs.end(),
566+
[&](llvm::StringRef Arg) {
567+
return Arg.starts_with("-") &&
568+
Arg != "-fsyntax-only" &&
569+
ArgToIgnoreRegex.match(Arg);
570+
}),
571+
AdjustedArgs.end());
572+
}
573+
}
574+
575+
return AdjustedArgs;
576+
};
577+
578+
Tool.appendArgumentsAdjuster(CompilationArgsToIgnore);
555579
Tool.appendArgumentsAdjuster(PerFileExtraArgumentsInserter);
556580
Tool.appendArgumentsAdjuster(getStripPluginsAdjuster());
557581
Context.setEnableProfiling(EnableCheckProfile);

clang-tools-extra/clang-tidy/ClangTidyOptions.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ template <> struct MappingTraits<ClangTidyOptions> {
174174
Options.ExcludeHeaderFilterRegex);
175175
IO.mapOptional("FormatStyle", Options.FormatStyle);
176176
IO.mapOptional("User", Options.User);
177+
IO.mapOptional("CompilationArgsToRemoveRegex",
178+
Options.CompilationArgsToRemoveRegex);
177179
IO.mapOptional("CheckOptions", Options.CheckOptions);
178180
IO.mapOptional("ExtraArgs", Options.ExtraArgs);
179181
IO.mapOptional("ExtraArgsBefore", Options.ExtraArgsBefore);
@@ -198,6 +200,7 @@ ClangTidyOptions ClangTidyOptions::getDefaults() {
198200
Options.SystemHeaders = false;
199201
Options.FormatStyle = "none";
200202
Options.User = std::nullopt;
203+
Options.CompilationArgsToRemoveRegex = std::nullopt;
201204
for (const ClangTidyModuleRegistry::entry &Module :
202205
ClangTidyModuleRegistry::entries())
203206
Options.mergeWith(Module.instantiate()->getModuleOptions(), 0);
@@ -238,6 +241,8 @@ ClangTidyOptions &ClangTidyOptions::mergeWith(const ClangTidyOptions &Other,
238241
overrideValue(SystemHeaders, Other.SystemHeaders);
239242
overrideValue(FormatStyle, Other.FormatStyle);
240243
overrideValue(User, Other.User);
244+
overrideValue(CompilationArgsToRemoveRegex,
245+
Other.CompilationArgsToRemoveRegex);
241246
overrideValue(UseColor, Other.UseColor);
242247
mergeVectors(ExtraArgs, Other.ExtraArgs);
243248
mergeVectors(ExtraArgsBefore, Other.ExtraArgsBefore);

clang-tools-extra/clang-tidy/ClangTidyOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ struct ClangTidyOptions {
110110
/// comments in the relevant check.
111111
std::optional<std::string> User;
112112

113+
/// \brief Remove command line arguments sent to the compiler matching this
114+
/// regex.
115+
std::optional<std::vector<std::string>> CompilationArgsToRemoveRegex;
116+
113117
/// Helper structure for storing option value with priority of the value.
114118
struct ClangTidyValue {
115119
ClangTidyValue() = default;

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ Improvements to clang-tidy
106106
- Improved :program:`run-clang-tidy.py` script. Fixed minor shutdown noise
107107
happening on certain platforms when interrupting the script.
108108

109+
- Improved :program:`clang-tidy` by adding the option `CompilationArgsToRemoveRegex`
110+
to remove arguments sent to the compiler when invoking clang-tidy.
111+
109112
New checks
110113
^^^^^^^^^^
111114

clang-tools-extra/docs/clang-tidy/index.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ An overview of all the command-line options:
303303
example, to place the correct user name in
304304
TODO() comments in the relevant check.
305305
WarningsAsErrors - Same as '--warnings-as-errors'.
306+
CompilationArgsToRemoveRegex - List of arguments to remove from the command
307+
line sent to the compiler.
306308
307309
The effective configuration can be inspected using --dump-config:
308310
@@ -312,6 +314,7 @@ An overview of all the command-line options:
312314
WarningsAsErrors: ''
313315
HeaderFileExtensions: ['', 'h','hh','hpp','hxx']
314316
ImplementationFileExtensions: ['c','cc','cpp','cxx']
317+
CompilationArgsToRemoveRegex: ['-Werror', '-f.*']
315318
HeaderFilterRegex: ''
316319
FormatStyle: none
317320
InheritParentConfig: true
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: not clang-tidy %s -- -fnot-an-option | FileCheck %s -check-prefix=INVALID-A
2+
// RUN: clang-tidy %s --config="{CompilationArgsToRemoveRegex: ['-fnot-an-option']}" -- -fnot-an-option
3+
// RUN: not clang-tidy %s --config="{CompilationArgsToRemoveRegex: ['-f.*']}" -- -fnot-an-option -invalid-option | FileCheck %s -check-prefix=INVALID-B
4+
// RUN: clang-tidy %s --config="{CompilationArgsToRemoveRegex: ['-f.*', '-invalid-option']}" -- -fnot-an-option -fnot-another-option -finvalid-option -invalid-option
5+
// RUN: not clang-tidy %s --config="{CompilationArgsToRemoveRegex: ['\$invalid-option']}" -- -finvalid-option | FileCheck %s -check-prefix=INVALID-C
6+
7+
// INVALID-A: error: unknown argument: '-fnot-an-option' [clang-diagnostic-error]
8+
// INVALID-B: error: unknown argument: '-invalid-option' [clang-diagnostic-error]
9+
// INVALID-C: error: unknown argument: '-finvalid-option' [clang-diagnostic-error]

0 commit comments

Comments
 (0)