Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions clang-tools-extra/clang-tidy/ClangTidy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,24 @@ runClangTidy(clang::tidy::ClangTidyContext &Context,
return AdjustedArgs;
};

// Remove unwanted arguments passed to the compiler
ArgumentsAdjuster CompilationArgsToRemove =
[&Context](const CommandLineArguments &Args, StringRef Filename) {
ClangTidyOptions Opts = Context.getOptionsForFile(Filename);
CommandLineArguments AdjustedArgs = Args;

if (Opts.RemovedArgs) {
for (StringRef ArgToIgnore : *Opts.RemovedArgs) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ArgToRemove

AdjustedArgs.erase(std::remove(AdjustedArgs.begin(),
AdjustedArgs.end(), ArgToIgnore),
AdjustedArgs.end());
}
}

return AdjustedArgs;
};

Tool.appendArgumentsAdjuster(CompilationArgsToRemove);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: for consistency with the one for ExtraArgs, maybe we can call this PerFileArgumentRemover?

Tool.appendArgumentsAdjuster(PerFileExtraArgumentsInserter);
Tool.appendArgumentsAdjuster(getStripPluginsAdjuster());
Context.setEnableProfiling(EnableCheckProfile);
Expand Down
3 changes: 3 additions & 0 deletions clang-tools-extra/clang-tidy/ClangTidyOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ template <> struct MappingTraits<ClangTidyOptions> {
Options.ExcludeHeaderFilterRegex);
IO.mapOptional("FormatStyle", Options.FormatStyle);
IO.mapOptional("User", Options.User);
IO.mapOptional("RemovedArgs", Options.RemovedArgs);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't alphabetically formatted, so could we place RemoveArgs next to ExtraArgs for consistency?

IO.mapOptional("CheckOptions", Options.CheckOptions);
IO.mapOptional("ExtraArgs", Options.ExtraArgs);
IO.mapOptional("ExtraArgsBefore", Options.ExtraArgsBefore);
Expand All @@ -252,6 +253,7 @@ ClangTidyOptions ClangTidyOptions::getDefaults() {
Options.SystemHeaders = false;
Options.FormatStyle = "none";
Options.User = std::nullopt;
Options.RemovedArgs = std::nullopt;
for (const ClangTidyModuleRegistry::entry &Module :
ClangTidyModuleRegistry::entries())
Options.mergeWith(Module.instantiate()->getModuleOptions(), 0);
Expand Down Expand Up @@ -292,6 +294,7 @@ ClangTidyOptions &ClangTidyOptions::mergeWith(const ClangTidyOptions &Other,
overrideValue(SystemHeaders, Other.SystemHeaders);
overrideValue(FormatStyle, Other.FormatStyle);
overrideValue(User, Other.User);
mergeVectors(RemovedArgs, Other.RemovedArgs);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto consistency

overrideValue(UseColor, Other.UseColor);
mergeVectors(ExtraArgs, Other.ExtraArgs);
mergeVectors(ExtraArgsBefore, Other.ExtraArgsBefore);
Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/clang-tidy/ClangTidyOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ struct ClangTidyOptions {
/// comments in the relevant check.
std::optional<std::string> User;

/// \brief Remove command line arguments sent to the compiler matching this
/// regex.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's no longer a regex

std::optional<std::vector<std::string>> RemovedArgs;

/// Helper structure for storing option value with priority of the value.
struct ClangTidyValue {
ClangTidyValue() = default;
Expand Down
3 changes: 3 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ Improvements to clang-tidy
scripts by adding the `-hide-progress` option to suppress progress and
informational messages.

- Improved :program:`clang-tidy` by adding the option `RemovedArgs` to remove
arguments sent to the compiler when invoking clang-tidy.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
arguments sent to the compiler when invoking clang-tidy.
arguments sent to the compiler when invoking Clang-Tidy.


- Deprecated the :program:`clang-tidy` ``zircon`` module. All checks have been
moved to the ``fuchsia`` module instead. The ``zircon`` module will be removed
in the 24th release.
Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/docs/clang-tidy/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,10 @@ An overview of all the command-line options:
example, to place the correct user name in
TODO() comments in the relevant check.
WarningsAsErrors - Same as '--warnings-as-errors'.
RemovedArgs - List of arguments to remove from the command
Copy link
Contributor

@vbvictor vbvictor Oct 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would It be hard to also add cli-option --remove-arg to be consistent with:

    ExtraArgs                    - Same as '--extra-arg'.
    ExtraArgsBefore              - Same as '--extra-arg-before'.

line sent to the compiler. Please note that
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps good to document that this removes the args from the "original" command line, before considering ExtraArgs

removing arguments from the command line
might lead to false positive or negatives.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about: might change the semantics of the analyzed code, possibly leading to crashes, false positives or false negatives.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning people about false positives and negatives makes sense, but I don't think we should be telling people to expect crashes; a crash is always a bug, it's not something users should ever expect, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm yes, crash is a strong word. I meant more "hard compiler errors" which cannot simply be suppressed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may be missing something, but how can removing compiler options lead to FP/FN? I agree with "hard compiler errors" thought.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an example, removing the m flag from my STM32 project made clang/clang-tidy think pointers are 64 bits instead of 32 bits, and I started getting warnings about casting uint32s to pointers.

The effective configuration can be inspected using --dump-config:
Expand All @@ -338,6 +342,7 @@ An overview of all the command-line options:
WarningsAsErrors: ''
HeaderFileExtensions: ['', 'h','hh','hpp','hxx']
ImplementationFileExtensions: ['c','cc','cpp','cxx']
RemovedArgs: ['-Werror']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 I think RemoveArgs sounds better

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant to remove this line from the code.

HeaderFilterRegex: ''
FormatStyle: none
InheritParentConfig: true
Expand Down
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to removed-args.cpp for consistency with the option name

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// RUN: not clang-tidy %s -- -fnot-an-option | FileCheck %s -check-prefix=INVALID-A
// RUN: clang-tidy %s --config="{RemovedArgs: ['-fnot-an-option']}" -- -fnot-an-option
// RUN: clang-tidy %s --config="{RemovedArgs: ['-fnot-another-option', '-fnot-an-option']}" -- -fnot-an-option -fnot-another-option

// INVALID-A: error: unknown argument: '-fnot-an-option' [clang-diagnostic-error]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing newline at the end of the file