-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[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
vbvictor
wants to merge
1
commit into
llvm:main
Choose a base branch
from
vbvictor:clang-tidy-fix-dump
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+39
−3
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-clang-tidy @llvm/pr-subscribers-clang-tools-extra Author: Baranov Victor (vbvictor) ChangesAdded function to filter out options that come from Fixes #146693. Full diff: https://github.com/llvm/llvm-project/pull/147142.diff 5 Files Affected:
diff --git a/clang-tools-extra/clang-tidy/ClangTidy.cpp b/clang-tools-extra/clang-tidy/ClangTidy.cpp
index f4ab93b51f4a7..30bc96d62de7c 100644
--- a/clang-tools-extra/clang-tidy/ClangTidy.cpp
+++ b/clang-tools-extra/clang-tidy/ClangTidy.cpp
@@ -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) {
diff --git a/clang-tools-extra/clang-tidy/ClangTidy.h b/clang-tools-extra/clang-tidy/ClangTidy.h
index 4ffd49f6ebf50..02ab3a0c1d213 100644
--- a/clang-tools-extra/clang-tidy/ClangTidy.h
+++ b/clang-tools-extra/clang-tidy/ClangTidy.h
@@ -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
diff --git a/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp b/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
index 4336c723bd7cd..4de1caec5ec8c 100644
--- a/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
+++ b/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
@@ -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;
}
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 198efee7754de..c8f9c127c115c 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -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.
diff --git a/clang-tools-extra/test/clang-tidy/infrastructure/dump-config-filtering.cpp b/clang-tools-extra/test/clang-tidy/infrastructure/dump-config-filtering.cpp
new file mode 100644
index 0000000000000..f22ec7edb1775
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/infrastructure/dump-config-filtering.cpp
@@ -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; }
|
localspook
reviewed
Jul 6, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Added function to filter out
CheckOptions
that come fromClangTidyOptions::getDefaults()
, but does not have a corresponding check enabled in theChecks
configuration.Fixes #146693.