Skip to content

Commit f2c15c6

Browse files
committed
fixup! [clang-tidy] New option to remove arguments from the command line
Removed Regex Support
1 parent d9a64e6 commit f2c15c6

File tree

6 files changed

+19
-34
lines changed

6 files changed

+19
-34
lines changed

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -582,29 +582,23 @@ runClangTidy(clang::tidy::ClangTidyContext &Context,
582582
};
583583

584584
// Remove unwanted arguments passed to the compiler
585-
ArgumentsAdjuster CompilationArgsToIgnore =
585+
ArgumentsAdjuster CompilationArgsToRemove =
586586
[&Context](const CommandLineArguments &Args, StringRef Filename) {
587587
ClangTidyOptions Opts = Context.getOptionsForFile(Filename);
588588
CommandLineArguments AdjustedArgs = Args;
589589

590-
if (Opts.CompilationArgsToRemoveRegex) {
591-
for (StringRef ArgToIgnore : *Opts.CompilationArgsToRemoveRegex) {
592-
llvm::Regex ArgToIgnoreRegex(ArgToIgnore);
593-
AdjustedArgs.erase(
594-
std::remove_if(AdjustedArgs.begin(), AdjustedArgs.end(),
595-
[&](llvm::StringRef Arg) {
596-
return Arg.starts_with("-") &&
597-
Arg != "-fsyntax-only" &&
598-
ArgToIgnoreRegex.match(Arg);
599-
}),
600-
AdjustedArgs.end());
590+
if (Opts.RemovedArgs) {
591+
for (StringRef ArgToIgnore : *Opts.RemovedArgs) {
592+
AdjustedArgs.erase(std::remove(AdjustedArgs.begin(),
593+
AdjustedArgs.end(), ArgToIgnore),
594+
AdjustedArgs.end());
601595
}
602596
}
603597

604598
return AdjustedArgs;
605599
};
606600

607-
Tool.appendArgumentsAdjuster(CompilationArgsToIgnore);
601+
Tool.appendArgumentsAdjuster(CompilationArgsToRemove);
608602
Tool.appendArgumentsAdjuster(PerFileExtraArgumentsInserter);
609603
Tool.appendArgumentsAdjuster(getStripPluginsAdjuster());
610604
Context.setEnableProfiling(EnableCheckProfile);

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,7 @@ template <> struct MappingTraits<ClangTidyOptions> {
227227
Options.ExcludeHeaderFilterRegex);
228228
IO.mapOptional("FormatStyle", Options.FormatStyle);
229229
IO.mapOptional("User", Options.User);
230-
IO.mapOptional("CompilationArgsToRemoveRegex",
231-
Options.CompilationArgsToRemoveRegex);
230+
IO.mapOptional("RemovedArgs", Options.RemovedArgs);
232231
IO.mapOptional("CheckOptions", Options.CheckOptions);
233232
IO.mapOptional("ExtraArgs", Options.ExtraArgs);
234233
IO.mapOptional("ExtraArgsBefore", Options.ExtraArgsBefore);
@@ -254,7 +253,7 @@ ClangTidyOptions ClangTidyOptions::getDefaults() {
254253
Options.SystemHeaders = false;
255254
Options.FormatStyle = "none";
256255
Options.User = std::nullopt;
257-
Options.CompilationArgsToRemoveRegex = std::nullopt;
256+
Options.RemovedArgs = std::nullopt;
258257
for (const ClangTidyModuleRegistry::entry &Module :
259258
ClangTidyModuleRegistry::entries())
260259
Options.mergeWith(Module.instantiate()->getModuleOptions(), 0);
@@ -295,8 +294,7 @@ ClangTidyOptions &ClangTidyOptions::mergeWith(const ClangTidyOptions &Other,
295294
overrideValue(SystemHeaders, Other.SystemHeaders);
296295
overrideValue(FormatStyle, Other.FormatStyle);
297296
overrideValue(User, Other.User);
298-
overrideValue(CompilationArgsToRemoveRegex,
299-
Other.CompilationArgsToRemoveRegex);
297+
mergeVectors(RemovedArgs, Other.RemovedArgs);
300298
overrideValue(UseColor, Other.UseColor);
301299
mergeVectors(ExtraArgs, Other.ExtraArgs);
302300
mergeVectors(ExtraArgsBefore, Other.ExtraArgsBefore);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ struct ClangTidyOptions {
114114

115115
/// \brief Remove command line arguments sent to the compiler matching this
116116
/// regex.
117-
std::optional<std::vector<std::string>> CompilationArgsToRemoveRegex;
117+
std::optional<std::vector<std::string>> RemovedArgs;
118118

119119
/// Helper structure for storing option value with priority of the value.
120120
struct ClangTidyValue {

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,7 @@ Improvements to clang-tidy
132132
when run over C files. If ``-std`` is not specified, it defaults to
133133
``c99-or-later``.
134134

135-
<<<<<<< HEAD
136135
- :program:`clang-tidy` no longer attempts to analyze code from system headers
137-
=======
138-
<<<<<<< HEAD
139-
- :program:`clang-tidy` no longer attemps to analyze code from system headers
140-
>>>>>>> 64f91e7543e8 (fixup! [clang-tidy] New option to remove arguments from the command line)
141136
by default, greatly improving performance. This behavior is disabled if the
142137
`SystemHeaders` option is enabled.
143138

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,10 @@ An overview of all the command-line options:
329329
example, to place the correct user name in
330330
TODO() comments in the relevant check.
331331
WarningsAsErrors - Same as '--warnings-as-errors'.
332-
CompilationArgsToRemoveRegex - List of arguments to remove from the command
333-
line sent to the compiler.
332+
RemovedArgs - List of arguments to remove from the command
333+
line sent to the compiler. Please note that
334+
removing arguments from the command line
335+
might lead to false positive or negatives.
334336
335337
The effective configuration can be inspected using --dump-config:
336338
@@ -340,7 +342,7 @@ An overview of all the command-line options:
340342
WarningsAsErrors: ''
341343
HeaderFileExtensions: ['', 'h','hh','hpp','hxx']
342344
ImplementationFileExtensions: ['c','cc','cpp','cxx']
343-
CompilationArgsToRemoveRegex: ['-Werror', '-f.*']
345+
RemovedArgs: ['-Werror']
344346
HeaderFilterRegex: ''
345347
FormatStyle: none
346348
InheritParentConfig: true
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
// 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
2+
// RUN: clang-tidy %s --config="{RemovedArgs: ['-fnot-an-option']}" -- -fnot-an-option
3+
// RUN: clang-tidy %s --config="{RemovedArgs: ['-fnot-another-option', '-fnot-an-option']}" -- -fnot-an-option -fnot-another-option
64

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]
5+
// INVALID-A: error: unknown argument: '-fnot-an-option' [clang-diagnostic-error]

0 commit comments

Comments
 (0)