Skip to content

Commit 69b7c43

Browse files
committed
fixup! fixup! [clang-tidy] New option to remove arguments from the command line
Code review
1 parent 452eecd commit 69b7c43

File tree

8 files changed

+31
-15
lines changed

8 files changed

+31
-15
lines changed

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

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

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

590590
if (Opts.RemovedArgs) {
591-
for (StringRef ArgToIgnore : *Opts.RemovedArgs) {
591+
for (StringRef ArgToRemove : *Opts.RemovedArgs) {
592592
AdjustedArgs.erase(std::remove(AdjustedArgs.begin(),
593-
AdjustedArgs.end(), ArgToIgnore),
593+
AdjustedArgs.end(), ArgToRemove),
594594
AdjustedArgs.end());
595595
}
596596
}
597597

598598
return AdjustedArgs;
599599
};
600600

601-
Tool.appendArgumentsAdjuster(CompilationArgsToRemove);
601+
Tool.appendArgumentsAdjuster(PerFileArgumentRemover);
602602
Tool.appendArgumentsAdjuster(PerFileExtraArgumentsInserter);
603603
Tool.appendArgumentsAdjuster(getStripPluginsAdjuster());
604604
Context.setEnableProfiling(EnableCheckProfile);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,10 @@ template <> struct MappingTraits<ClangTidyOptions> {
227227
Options.ExcludeHeaderFilterRegex);
228228
IO.mapOptional("FormatStyle", Options.FormatStyle);
229229
IO.mapOptional("User", Options.User);
230-
IO.mapOptional("RemovedArgs", Options.RemovedArgs);
231230
IO.mapOptional("CheckOptions", Options.CheckOptions);
232231
IO.mapOptional("ExtraArgs", Options.ExtraArgs);
233232
IO.mapOptional("ExtraArgsBefore", Options.ExtraArgsBefore);
233+
IO.mapOptional("RemovedArgs", Options.RemovedArgs);
234234
IO.mapOptional("InheritParentConfig", Options.InheritParentConfig);
235235
IO.mapOptional("UseColor", Options.UseColor);
236236
IO.mapOptional("SystemHeaders", Options.SystemHeaders);
@@ -294,10 +294,10 @@ ClangTidyOptions &ClangTidyOptions::mergeWith(const ClangTidyOptions &Other,
294294
overrideValue(SystemHeaders, Other.SystemHeaders);
295295
overrideValue(FormatStyle, Other.FormatStyle);
296296
overrideValue(User, Other.User);
297-
mergeVectors(RemovedArgs, Other.RemovedArgs);
298297
overrideValue(UseColor, Other.UseColor);
299298
mergeVectors(ExtraArgs, Other.ExtraArgs);
300299
mergeVectors(ExtraArgsBefore, Other.ExtraArgsBefore);
300+
mergeVectors(RemovedArgs, Other.RemovedArgs);
301301
// FIXME: how to handle duplicate names check?
302302
mergeVectors(CustomChecks, Other.CustomChecks);
303303
for (const auto &KeyValue : Other.CheckOptions) {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ struct ClangTidyOptions {
112112
/// comments in the relevant check.
113113
std::optional<std::string> User;
114114

115-
/// \brief Remove command line arguments sent to the compiler matching this
116-
/// regex.
115+
/// \brief Remove command line arguments sent to the compiler matching this.
117116
std::optional<std::vector<std::string>> RemovedArgs;
118117

119118
/// Helper structure for storing option value with priority of the value.

clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "../ClangTidy.h"
1919
#include "../ClangTidyForceLinker.h"
2020
#include "../GlobList.h"
21+
#include "../utils/OptionsUtils.h"
2122
#include "clang/Tooling/CommonOptionsParser.h"
2223
#include "llvm/ADT/StringSet.h"
2324
#include "llvm/Support/CommandLine.h"
@@ -355,6 +356,16 @@ see https://clang.llvm.org/extra/clang-tidy/QueryBasedCustomChecks.html.
355356
cl::init(false),
356357
cl::cat(ClangTidyCategory));
357358

359+
static cl::list<std::string> RemovedArgs("removed-arg", desc(R"(
360+
List of arguments to remove from the command
361+
line sent to the compiler. Please note that
362+
removing arguments might change the semantic
363+
of the analzed code, possibly leading to
364+
compiler errors, false positives or
365+
false negatives. This option is applied
366+
before --extra-arg and --extra-arg-before)"),
367+
cl::cat(ClangTidyCategory));
368+
358369
namespace clang::tidy {
359370

360371
static void printStats(const ClangTidyStats &Stats) {
@@ -421,6 +432,8 @@ createOptionsProvider(llvm::IntrusiveRefCntPtr<vfs::FileSystem> FS) {
421432
OverrideOptions.FormatStyle = FormatStyle;
422433
if (UseColor.getNumOccurrences() > 0)
423434
OverrideOptions.UseColor = UseColor;
435+
if (RemovedArgs.getNumOccurrences() > 0)
436+
OverrideOptions.RemovedArgs = RemovedArgs;
424437

425438
auto LoadConfig =
426439
[&](StringRef Configuration,

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ Improvements to clang-tidy
168168
informational messages.
169169

170170
- Improved :program:`clang-tidy` by adding the option `RemovedArgs` to remove
171-
arguments sent to the compiler when invoking clang-tidy.
171+
arguments sent to the compiler when invoking Clang-Tidy.
172172

173173
- Deprecated the :program:`clang-tidy` ``zircon`` module. All checks have been
174174
moved to the ``fuchsia`` module instead. The ``zircon`` module will be removed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,12 @@ An overview of all the command-line options:
330330
TODO() comments in the relevant check.
331331
WarningsAsErrors - Same as '--warnings-as-errors'.
332332
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.
333+
line sent to the compiler. Please note that
334+
removing arguments might change the semantic
335+
of the analzed code, possibly leading to
336+
compiler errors, false positives or
337+
false negatives. This option is applied
338+
before --extra-arg and --extra-arg-before
336339
337340
The effective configuration can be inspected using --dump-config:
338341
@@ -342,7 +345,6 @@ An overview of all the command-line options:
342345
WarningsAsErrors: ''
343346
HeaderFileExtensions: ['', 'h','hh','hpp','hxx']
344347
ImplementationFileExtensions: ['c','cc','cpp','cxx']
345-
RemovedArgs: ['-Werror']
346348
HeaderFilterRegex: ''
347349
FormatStyle: none
348350
InheritParentConfig: true
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
// RUN: not clang-tidy --invalid-arg 2>&1 | FileCheck %s
22

33
// CHECK: error: clang-tidy{{(\.exe)?}}: Unknown command line argument '--invalid-arg'. Try: '{{.*}}clang-tidy{{(\.exe)?}} --help'
4-
// CHECK-NEXT: clang-tidy{{(\.exe)?}}: Did you mean '--extra-arg'?
4+
// CHECK-NEXT: clang-tidy{{(\.exe)?}}: Did you mean '--removed-arg'?
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// RUN: not clang-tidy %s -- -fnot-an-option | FileCheck %s -check-prefix=INVALID-A
22
// RUN: clang-tidy %s --config="{RemovedArgs: ['-fnot-an-option']}" -- -fnot-an-option
33
// RUN: clang-tidy %s --config="{RemovedArgs: ['-fnot-another-option', '-fnot-an-option']}" -- -fnot-an-option -fnot-another-option
4+
// RUN clang-tidy %s --removed-arg="-fnot-an-option" -- -fnot-an-option -fnot-another-option | FileCheck %s -check-prefix=INVALID-B
45

5-
// INVALID-A: error: unknown argument: '-fnot-an-option' [clang-diagnostic-error]
6+
// INVALID-A: error: unknown argument: '-fnot-an-option' [clang-diagnostic-error]
7+
// INVALID-B: error: unknown argument: '-fnot-another-option' [clang-diagnostic-error]

0 commit comments

Comments
 (0)