-
Notifications
You must be signed in to change notification settings - Fork 14.5k
[clang-tidy] Add filtering of 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
Conversation
@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; }
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Added function to filter out
CheckOptions
that come fromClangTidyOptions::getDefaults()
, but does not have a corresponding check enabled in theChecks
configuration.Fixes #146693.