diff --git a/clang-tools-extra/clang-tidy/ClangTidy.cpp b/clang-tools-extra/clang-tidy/ClangTidy.cpp index 7e18f3806a143..6e91e0a254c0f 100644 --- a/clang-tools-extra/clang-tidy/ClangTidy.cpp +++ b/clang-tools-extra/clang-tidy/ClangTidy.cpp @@ -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) { + AdjustedArgs.erase(std::remove(AdjustedArgs.begin(), + AdjustedArgs.end(), ArgToIgnore), + AdjustedArgs.end()); + } + } + + return AdjustedArgs; + }; + + Tool.appendArgumentsAdjuster(CompilationArgsToRemove); Tool.appendArgumentsAdjuster(PerFileExtraArgumentsInserter); Tool.appendArgumentsAdjuster(getStripPluginsAdjuster()); Context.setEnableProfiling(EnableCheckProfile); diff --git a/clang-tools-extra/clang-tidy/ClangTidyOptions.cpp b/clang-tools-extra/clang-tidy/ClangTidyOptions.cpp index 21455db7c7e7b..cca32f013b4a1 100644 --- a/clang-tools-extra/clang-tidy/ClangTidyOptions.cpp +++ b/clang-tools-extra/clang-tidy/ClangTidyOptions.cpp @@ -227,6 +227,7 @@ template <> struct MappingTraits { Options.ExcludeHeaderFilterRegex); IO.mapOptional("FormatStyle", Options.FormatStyle); IO.mapOptional("User", Options.User); + IO.mapOptional("RemovedArgs", Options.RemovedArgs); IO.mapOptional("CheckOptions", Options.CheckOptions); IO.mapOptional("ExtraArgs", Options.ExtraArgs); IO.mapOptional("ExtraArgsBefore", Options.ExtraArgsBefore); @@ -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); @@ -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); overrideValue(UseColor, Other.UseColor); mergeVectors(ExtraArgs, Other.ExtraArgs); mergeVectors(ExtraArgsBefore, Other.ExtraArgsBefore); diff --git a/clang-tools-extra/clang-tidy/ClangTidyOptions.h b/clang-tools-extra/clang-tidy/ClangTidyOptions.h index 2aae92f1d9eb3..989b1c651c830 100644 --- a/clang-tools-extra/clang-tidy/ClangTidyOptions.h +++ b/clang-tools-extra/clang-tidy/ClangTidyOptions.h @@ -112,6 +112,10 @@ struct ClangTidyOptions { /// comments in the relevant check. std::optional User; + /// \brief Remove command line arguments sent to the compiler matching this + /// regex. + std::optional> RemovedArgs; + /// Helper structure for storing option value with priority of the value. struct ClangTidyValue { ClangTidyValue() = default; diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 8a0151f567c24..59046b6f4c28e 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -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. + - 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. diff --git a/clang-tools-extra/docs/clang-tidy/index.rst b/clang-tools-extra/docs/clang-tidy/index.rst index bd2c40e948f34..43142c9121716 100644 --- a/clang-tools-extra/docs/clang-tidy/index.rst +++ b/clang-tools-extra/docs/clang-tidy/index.rst @@ -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 + line sent to the compiler. Please note that + removing arguments from the command line + might lead to false positive or negatives. The effective configuration can be inspected using --dump-config: @@ -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'] HeaderFilterRegex: '' FormatStyle: none InheritParentConfig: true diff --git a/clang-tools-extra/test/clang-tidy/infrastructure/compilation-args-to-ignore.cpp b/clang-tools-extra/test/clang-tidy/infrastructure/compilation-args-to-ignore.cpp new file mode 100644 index 0000000000000..9a042dedce3a4 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/infrastructure/compilation-args-to-ignore.cpp @@ -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] \ No newline at end of file