Skip to content

Commit b56aeba

Browse files
authored
[clang-tidy] Add filtering of check options by enabled checks in '--dump-config' (#147142)
Added function to filter out `CheckOptions` that come from `ClangTidyOptions::getDefaults()`, but does not have a corresponding check enabled in the `Checks` configuration. Fixes #146693.
1 parent cac806b commit b56aeba

File tree

5 files changed

+38
-3
lines changed

5 files changed

+38
-3
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,20 @@ getCheckNames(const ClangTidyOptions &Options,
505505
return Factory.getCheckNames();
506506
}
507507

508+
void filterCheckOptions(ClangTidyOptions &Options,
509+
const std::vector<std::string> &EnabledChecks) {
510+
ClangTidyOptions::OptionMap FilteredOptions;
511+
for (const auto &[OptionName, Value] : Options.CheckOptions) {
512+
const size_t CheckNameEndPos = OptionName.find('.');
513+
if (CheckNameEndPos == StringRef::npos)
514+
continue;
515+
const StringRef CheckName = OptionName.substr(0, CheckNameEndPos);
516+
if (llvm::binary_search(EnabledChecks, CheckName))
517+
FilteredOptions[OptionName] = Value;
518+
}
519+
Options.CheckOptions = std::move(FilteredOptions);
520+
}
521+
508522
ClangTidyOptions::OptionMap
509523
getCheckOptions(const ClangTidyOptions &Options,
510524
bool AllowEnablingAnalyzerAlphaCheckers) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ ClangTidyOptions::OptionMap
7676
getCheckOptions(const ClangTidyOptions &Options,
7777
bool AllowEnablingAnalyzerAlphaCheckers);
7878

79+
/// Filters CheckOptions in \p Options to only include options specified in
80+
/// the \p EnabledChecks which is a sorted vector.
81+
void filterCheckOptions(ClangTidyOptions &Options,
82+
const std::vector<std::string> &EnabledChecks);
83+
7984
/// Run a set of clang-tidy checks on a set of files.
8085
///
8186
/// \param EnableCheckProfile If provided, it enables check profile collection

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -659,9 +659,10 @@ int clangTidyMain(int argc, const char **argv) {
659659
if (DumpConfig) {
660660
EffectiveOptions.CheckOptions =
661661
getCheckOptions(EffectiveOptions, AllowEnablingAnalyzerAlphaCheckers);
662-
llvm::outs() << configurationAsText(ClangTidyOptions::getDefaults().merge(
663-
EffectiveOptions, 0))
664-
<< "\n";
662+
ClangTidyOptions OptionsToDump =
663+
ClangTidyOptions::getDefaults().merge(EffectiveOptions, 0);
664+
filterCheckOptions(OptionsToDump, EnabledChecks);
665+
llvm::outs() << configurationAsText(OptionsToDump) << "\n";
665666
return 0;
666667
}
667668

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ Improvements to clang-tidy
108108
- Improved :program:`clang-tidy-diff.py` script. Add the `-warnings-as-errors`
109109
argument to treat warnings as errors.
110110

111+
- Improved :program:`clang-tidy` to show `CheckOptions` only for checks enabled
112+
in `Checks` when running ``--dump-config``.
113+
111114
- Fixed bug in :program:`clang-tidy` by which `HeaderFilterRegex` did not take
112115
effect when passed via the `.clang-tidy` file.
113116

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: clang-tidy -checks='-*,misc-unused-parameters' -dump-config %s -- 2>/dev/null | FileCheck %s --check-prefix=CHECK
2+
// RUN: clang-tidy -checks='-*' -dump-config %s -- 2>/dev/null | FileCheck %s --check-prefix=CHECK-DISABLED
3+
4+
// CHECK: CheckOptions:
5+
// CHECK-NEXT: misc-unused-parameters.IgnoreVirtual: 'false'
6+
// CHECK-NEXT: misc-unused-parameters.StrictMode: 'false'
7+
// CHECK-NEXT: SystemHeaders: false
8+
9+
// CHECK-DISABLED: CheckOptions: {}
10+
// CHECK-DISABLED-NEXT: SystemHeaders: false
11+
12+
int main() { return 0; }

0 commit comments

Comments
 (0)