Skip to content

[include-cleaner] Use heterogeneous lookups (NFC) #139685

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

kazutakahirata
Copy link
Contributor

Heterogenous lookups allow us to call find with StringRef, avoiding a
temporary heap allocation of std::string.

Heterogenous lookups allow us to call find with StringRef, avoiding a
temporary heap allocation of std::string.
@llvmbot
Copy link
Member

llvmbot commented May 13, 2025

@llvm/pr-subscribers-clang-tools-extra

Author: Kazu Hirata (kazutakahirata)

Changes

Heterogenous lookups allow us to call find with StringRef, avoiding a
temporary heap allocation of std::string.


Full diff: https://github.com/llvm/llvm-project/pull/139685.diff

1 Files Affected:

  • (modified) clang-tools-extra/include-cleaner/tool/IncludeCleaner.cpp (+3-4)
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;

@kazutakahirata kazutakahirata requested a review from hokein May 13, 2025 07:49
@kazutakahirata kazutakahirata merged commit ac583df into llvm:main May 13, 2025
13 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_001_heterogenous_include_cleaner branch May 13, 2025 14:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants