[clang-tools-extra] [clang-tidy] filter check options by enabled checks in '--dump-config' (PR #147142)
Victor Chernyakin via cfe-commits
cfe-commits at lists.llvm.org
Sun Jul 6 01:37:56 PDT 2025
================
@@ -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);
+}
----------------
localspook wrote:
Instead of building up a new `OptionMap` and then overwriting `Options.CheckOptions`, could we avoid allocations by directly erasing the elements we don't want to keep from `Options.CheckOptions`? Something like this (not tested):
```diff
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) {
+ for (auto I = Options.CheckOptions.begin(), E = Options.CheckOptions.end(); I != E; ++I) {
+ const auto &[OptionName, Value] = *I;
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;
+ if (!EnabledChecksSet.contains(CheckName))
+ Options.CheckOptions.erase(I);
}
- Options.CheckOptions = std::move(FilteredOptions);
}
```
https://github.com/llvm/llvm-project/pull/147142
More information about the cfe-commits
mailing list