Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 24 additions & 0 deletions clang-tools-extra/clang-tidy/ClangTidy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,30 @@ runClangTidy(clang::tidy::ClangTidyContext &Context,
return AdjustedArgs;
};

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

if (Opts.CompilationArgsToRemoveRegex) {
for (StringRef ArgToIgnore : *Opts.CompilationArgsToRemoveRegex) {
llvm::Regex ArgToIgnoreRegex(ArgToIgnore);
AdjustedArgs.erase(
std::remove_if(AdjustedArgs.begin(), AdjustedArgs.end(),
[&](llvm::StringRef Arg) {
return Arg.starts_with("-") &&
Arg != "-fsyntax-only" &&
ArgToIgnoreRegex.match(Arg);
}),
AdjustedArgs.end());
}
}

return AdjustedArgs;
};

Tool.appendArgumentsAdjuster(CompilationArgsToIgnore);
Tool.appendArgumentsAdjuster(PerFileExtraArgumentsInserter);
Tool.appendArgumentsAdjuster(getStripPluginsAdjuster());
Context.setEnableProfiling(EnableCheckProfile);
Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/clang-tidy/ClangTidyOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ template <> struct MappingTraits<ClangTidyOptions> {
Options.ExcludeHeaderFilterRegex);
IO.mapOptional("FormatStyle", Options.FormatStyle);
IO.mapOptional("User", Options.User);
IO.mapOptional("CompilationArgsToRemoveRegex",
Options.CompilationArgsToRemoveRegex);
IO.mapOptional("CheckOptions", Options.CheckOptions);
IO.mapOptional("ExtraArgs", Options.ExtraArgs);
IO.mapOptional("ExtraArgsBefore", Options.ExtraArgsBefore);
Expand All @@ -198,6 +200,7 @@ ClangTidyOptions ClangTidyOptions::getDefaults() {
Options.SystemHeaders = false;
Options.FormatStyle = "none";
Options.User = std::nullopt;
Options.CompilationArgsToRemoveRegex = std::nullopt;
for (const ClangTidyModuleRegistry::entry &Module :
ClangTidyModuleRegistry::entries())
Options.mergeWith(Module.instantiate()->getModuleOptions(), 0);
Expand Down Expand Up @@ -238,6 +241,8 @@ ClangTidyOptions &ClangTidyOptions::mergeWith(const ClangTidyOptions &Other,
overrideValue(SystemHeaders, Other.SystemHeaders);
overrideValue(FormatStyle, Other.FormatStyle);
overrideValue(User, Other.User);
overrideValue(CompilationArgsToRemoveRegex,
Other.CompilationArgsToRemoveRegex);
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 @@ -110,6 +110,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.
std::optional<std::vector<std::string>> CompilationArgsToRemoveRegex;

/// 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 @@ -106,6 +106,9 @@ Improvements to clang-tidy
- Improved :program:`run-clang-tidy.py` script. Fixed minor shutdown noise
happening on certain platforms when interrupting the script.

- Improved :program:`clang-tidy` by adding the option `CompilationArgsToRemoveRegex`
Copy link
Contributor

Choose a reason for hiding this comment

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

Please move it after previous clang-tidy entry. Also ":option:CompilationArgsToRemoveRegex"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@EugeneZelenko, I might be wrong, but since the option is only mentioned in index.rst, I don't think I can use :option: in the Release notes.

to remove arguments sent to the compiler when invoking clang-tidy.

New checks
^^^^^^^^^^

Expand Down
3 changes: 3 additions & 0 deletions clang-tools-extra/docs/clang-tidy/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ 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'.
CompilationArgsToRemoveRegex - List of arguments to remove from the command
line sent to the compiler.

The effective configuration can be inspected using --dump-config:

Expand All @@ -312,6 +314,7 @@ An overview of all the command-line options:
WarningsAsErrors: ''
HeaderFileExtensions: ['', 'h','hh','hpp','hxx']
ImplementationFileExtensions: ['c','cc','cpp','cxx']
CompilationArgsToRemoveRegex: ['-Werror', '-f.*']
HeaderFilterRegex: ''
FormatStyle: none
InheritParentConfig: true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: not clang-tidy %s -- -fnot-an-option | FileCheck %s -check-prefix=INVALID-A
// RUN: clang-tidy %s --config="{CompilationArgsToRemoveRegex: ['-fnot-an-option']}" -- -fnot-an-option
// RUN: not clang-tidy %s --config="{CompilationArgsToRemoveRegex: ['-f.*']}" -- -fnot-an-option -invalid-option | FileCheck %s -check-prefix=INVALID-B
// RUN: clang-tidy %s --config="{CompilationArgsToRemoveRegex: ['-f.*', '-invalid-option']}" -- -fnot-an-option -fnot-another-option -finvalid-option -invalid-option
// RUN: not clang-tidy %s --config="{CompilationArgsToRemoveRegex: ['\$invalid-option']}" -- -finvalid-option | FileCheck %s -check-prefix=INVALID-C

// INVALID-A: error: unknown argument: '-fnot-an-option' [clang-diagnostic-error]
// INVALID-B: error: unknown argument: '-invalid-option' [clang-diagnostic-error]
// INVALID-C: error: unknown argument: '-finvalid-option' [clang-diagnostic-error]
Loading