[clang-tools-extra] [clang-tidy] filter check options by enabled checks in '--dump-config' (PR #147142)
Baranov Victor via cfe-commits
cfe-commits at lists.llvm.org
Sun Jul 6 02:32:42 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);
+}
----------------
vbvictor wrote:
The question is can we both iterate over `llvm::StringMap` and erase elements from it. For `std::unordered_map::erase` we [can](https://en.cppreference.com/w/cpp/container/unordered_map/erase), but I don't know for sure about `llvm::StringMap::erase`. Even if it could like `std::unordered_map::erase`, it still takes some manual management of iterators that I don't like:
```cpp
std::unordered_map<int, std::string> c =
{
{1, "one"}, {2, "two"}, {3, "three"},
{4, "four"}, {5, "five"}, {6, "six"}
};
// erase all odd numbers from c
for (auto it = c.begin(); it != c.end();)
{
if (it->first % 2 != 0)
it = c.erase(it);
else
++it;
}
```
My code is not in a hot path by any means and the number of check options is relatively small, so I'd keep readability over little performance gain.
https://github.com/llvm/llvm-project/pull/147142
More information about the cfe-commits
mailing list