[clang-tools-extra] [include-cleaner] Use heterogeneous lookups (NFC) (PR #139685)
Kazu Hirata via cfe-commits
cfe-commits at lists.llvm.org
Tue May 13 00:49:23 PDT 2025
https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/139685
Heterogenous lookups allow us to call find with StringRef, avoiding a
temporary heap allocation of std::string.
>From 9615232d9257d6d263d40656f2947d281848046e Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Tue, 13 May 2025 00:44:06 -0700
Subject: [PATCH] [include-cleaner] Use heterogeneous lookups (NFC)
Heterogenous lookups allow us to call find with StringRef, avoiding a
temporary heap allocation of std::string.
---
clang-tools-extra/include-cleaner/tool/IncludeCleaner.cpp | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/clang-tools-extra/include-cleaner/tool/IncludeCleaner.cpp b/clang-tools-extra/include-cleaner/tool/IncludeCleaner.cpp
index 9431211b03e71..372ab5fa2706e 100644
--- a/clang-tools-extra/include-cleaner/tool/IncludeCleaner.cpp
+++ b/clang-tools-extra/include-cleaner/tool/IncludeCleaner.cpp
@@ -312,11 +312,11 @@ std::function<bool(llvm::StringRef)> headerFilter() {
// Maps absolute path of each files of each compilation commands to the
// absolute path of the input file.
-llvm::Expected<std::map<std::string, std::string>>
+llvm::Expected<std::map<std::string, std::string, std::less<>>>
mapInputsToAbsPaths(clang::tooling::CompilationDatabase &CDB,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
const std::vector<std::string> &Inputs) {
- std::map<std::string, std::string> CDBToAbsPaths;
+ std::map<std::string, std::string, std::less<>> CDBToAbsPaths;
// Factory.editedFiles()` will contain the final code, along with the
// path given in the compilation database. That path can be
// absolute or relative, and if it is relative, it is relative to the
@@ -395,8 +395,7 @@ int main(int argc, const char **argv) {
if (Edit) {
for (const auto &NameAndContent : Factory.editedFiles()) {
llvm::StringRef FileName = NameAndContent.first();
- if (auto It = CDBToAbsPaths->find(FileName.str());
- It != CDBToAbsPaths->end())
+ if (auto It = CDBToAbsPaths->find(FileName); It != CDBToAbsPaths->end())
FileName = It->second;
const std::string &FinalCode = NameAndContent.second;
More information about the cfe-commits
mailing list