Skip to content

[clang-tidy] filter check options by enabled checks in '--dump-config' #147142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
15 changes: 15 additions & 0 deletions clang-tools-extra/clang-tidy/ClangTidy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,21 @@ getCheckNames(const ClangTidyOptions &Options,
return Factory.getCheckNames();
}

void filterCheckOptions(ClangTidyOptions &Options,
const std::vector<std::string> &EnabledChecks) {
StringSet<> EnabledChecksSet(llvm::from_range, EnabledChecks);
ClangTidyOptions::OptionMap FilteredOptions;
for (const auto &[OptionName, Value] : Options.CheckOptions) {
const size_t CheckNameEndPos = OptionName.find('.');
if (CheckNameEndPos == StringRef::npos)
continue;
const StringRef CheckName = OptionName.substr(0, CheckNameEndPos);
if (EnabledChecksSet.contains(CheckName))
FilteredOptions[OptionName] = Value;
}
Options.CheckOptions = std::move(FilteredOptions);
}

ClangTidyOptions::OptionMap
getCheckOptions(const ClangTidyOptions &Options,
bool AllowEnablingAnalyzerAlphaCheckers) {
Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/clang-tidy/ClangTidy.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ ClangTidyOptions::OptionMap
getCheckOptions(const ClangTidyOptions &Options,
bool AllowEnablingAnalyzerAlphaCheckers);

/// Filters CheckOptions in \p Options to only include options specified in
/// the \p EnabledChecks.
void filterCheckOptions(ClangTidyOptions &Options,
const std::vector<std::string> &EnabledChecks);

/// Run a set of clang-tidy checks on a set of files.
///
/// \param EnableCheckProfile If provided, it enables check profile collection
Expand Down
7 changes: 4 additions & 3 deletions clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -659,9 +659,10 @@ int clangTidyMain(int argc, const char **argv) {
if (DumpConfig) {
EffectiveOptions.CheckOptions =
getCheckOptions(EffectiveOptions, AllowEnablingAnalyzerAlphaCheckers);
llvm::outs() << configurationAsText(ClangTidyOptions::getDefaults().merge(
EffectiveOptions, 0))
<< "\n";
ClangTidyOptions OptionsToDump =
ClangTidyOptions::getDefaults().merge(EffectiveOptions, 0);
filterCheckOptions(OptionsToDump, EnabledChecks);
llvm::outs() << configurationAsText(OptionsToDump) << "\n";
return 0;
}

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 @@ -108,6 +108,9 @@ Improvements to clang-tidy
- Improved :program:`clang-tidy-diff.py` script. Add the `-warnings-as-errors`
argument to treat warnings as errors.

- Improved :program:`clang-tidy` to show `CheckOptions` only for checks enabled
in `Checks` when running ``--dump-config``.

- Fixed bug in :program:`clang-tidy` by which `HeaderFilterRegex` did not take
effect when passed via the `.clang-tidy` file.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// RUN: clang-tidy -checks='-*,misc-unused-parameters' -dump-config %s -- 2>/dev/null | FileCheck %s --check-prefix=CHECK
// RUN: clang-tidy -checks='-*' -dump-config %s -- 2>/dev/null | FileCheck %s --check-prefix=CHECK-DISABLED

// CHECK: CheckOptions:
// CHECK-NEXT: misc-unused-parameters.IgnoreVirtual: 'false'
// CHECK-NEXT: misc-unused-parameters.StrictMode: 'false'
// CHECK-NEXT: SystemHeaders: false

// CHECK-DISABLED: CheckOptions: {}
// CHECK-DISABLED-NEXT: SystemHeaders: false

int main() { return 0; }
Loading