[clang] [clang][Lex] Replace push_back with emplace_back for vector insertions (PR #232260)
Hardik Kumar via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 22 08:55:56 PDT 2026
https://github.com/hardikxk updated https://github.com/llvm/llvm-project/pull/232260
>From 506c722a83db69a9579fb1f1889f4b007c077c29 Mon Sep 17 00:00:00 2001
From: Hardik Kumar <hardikxk at gmail.com>
Date: Thu, 15 Oct 2026 15:11:58 +0530
Subject: [PATCH 1/3] [clang][Lex] Replace push_back with emplace_back for
vector insertions
Replace the push_back() calls with emplace_back() to allow in place
construction of objects instead of the object creation and move calls
done by push_back().
A constructor for `UnresolvedConflict` is also created to support
the emplace_back() method.
Signed-off-by: Hardik Kumar <hardikxk at gmail.com>
---
clang/include/clang/Basic/Module.h | 5 +++++
clang/lib/Lex/ModuleMap.cpp | 13 ++++---------
2 files changed, 9 insertions(+), 9 deletions(-)
diff --git a/clang/include/clang/Basic/Module.h b/clang/include/clang/Basic/Module.h
index 4d304b8f006b60..28e635efc4f904 100644
--- a/clang/include/clang/Basic/Module.h
+++ b/clang/include/clang/Basic/Module.h
@@ -737,6 +737,11 @@ class alignas(8) Module {
/// The message provided to the user when there is a conflict.
std::string Message;
+
+ /// Construct an unresolved conflict.
+ UnresolvedConflict() = default;
+ UnresolvedConflict(ModuleId Id, std::string Message)
+ : Id(Id), Message(Message) {}
};
/// The list of conflicts for which the module-id has not yet been
diff --git a/clang/lib/Lex/ModuleMap.cpp b/clang/lib/Lex/ModuleMap.cpp
index b75f5c98ba72bb..4d40d2d5e03c93 100644
--- a/clang/lib/Lex/ModuleMap.cpp
+++ b/clang/lib/Lex/ModuleMap.cpp
@@ -2301,8 +2301,8 @@ void ModuleMapLoader::handleUseDecl(const modulemap::UseDecl &UD) {
}
void ModuleMapLoader::handleLinkDecl(const modulemap::LinkDecl &LD) {
- ActiveModule->LinkLibraries.push_back(
- Module::LinkLibrary(std::string{LD.Library}, LD.Framework));
+ ActiveModule->LinkLibraries.emplace_back(std::string{LD.Library},
+ LD.Framework);
}
void ModuleMapLoader::handleConfigMacros(
@@ -2322,13 +2322,8 @@ void ModuleMapLoader::handleConfigMacros(
}
void ModuleMapLoader::handleConflict(const modulemap::ConflictDecl &CD) {
- Module::UnresolvedConflict Conflict;
-
- Conflict.Id = CD.Id;
- Conflict.Message = CD.Message;
-
- // FIXME: when we move to C++20 we should consider using emplace_back
- ActiveModule->UnresolvedConflicts.push_back(std::move(Conflict));
+ ActiveModule->UnresolvedConflicts.emplace_back(CD.Id,
+ std::string{CD.Message});
}
void ModuleMapLoader::handleInferredModuleDecl(
>From c2dc685324112c7e4cae3730bc0e4ed7b982da06 Mon Sep 17 00:00:00 2001
From: Hardik Kumar <hardikxk at gmail.com>
Date: Thu, 22 Oct 2026 20:36:33 +0530
Subject: [PATCH 2/3] revert back changes made for UnresolvedConflict
Signed-off-by: Hardik Kumar <hardikxk at gmail.com>
---
clang/include/clang/Basic/Module.h | 5 -----
clang/lib/Lex/ModuleMap.cpp | 9 +++++++--
2 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/clang/include/clang/Basic/Module.h b/clang/include/clang/Basic/Module.h
index 28e635efc4f904..4d304b8f006b60 100644
--- a/clang/include/clang/Basic/Module.h
+++ b/clang/include/clang/Basic/Module.h
@@ -737,11 +737,6 @@ class alignas(8) Module {
/// The message provided to the user when there is a conflict.
std::string Message;
-
- /// Construct an unresolved conflict.
- UnresolvedConflict() = default;
- UnresolvedConflict(ModuleId Id, std::string Message)
- : Id(Id), Message(Message) {}
};
/// The list of conflicts for which the module-id has not yet been
diff --git a/clang/lib/Lex/ModuleMap.cpp b/clang/lib/Lex/ModuleMap.cpp
index 4d40d2d5e03c93..70c16c7b494444 100644
--- a/clang/lib/Lex/ModuleMap.cpp
+++ b/clang/lib/Lex/ModuleMap.cpp
@@ -2322,8 +2322,13 @@ void ModuleMapLoader::handleConfigMacros(
}
void ModuleMapLoader::handleConflict(const modulemap::ConflictDecl &CD) {
- ActiveModule->UnresolvedConflicts.emplace_back(CD.Id,
- std::string{CD.Message});
+ Module::UnresolvedConflict Conflict;
+
+ Conflict.Id = CD.Id;
+ Conflict.Message = CD.Message;
+
+ // FIXME: when we move to C++20 we should consider using emplace_back
+ ActiveModule->UnresolvedConflicts.push_back(std::move(Conflict));
}
void ModuleMapLoader::handleInferredModuleDecl(
>From 09d597da445fe1a7af5d31baaaa92ad84eac0573 Mon Sep 17 00:00:00 2001
From: Hardik Kumar <hardikxk at gmail.com>
Date: Thu, 22 Oct 2026 21:20:18 +0530
Subject: [PATCH 3/3] replace another instance of push_back
Signed-off-by: Hardik Kumar <hardikxk at gmail.com>
---
clang/lib/Lex/ModuleMap.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Lex/ModuleMap.cpp b/clang/lib/Lex/ModuleMap.cpp
index 70c16c7b494444..662a7790cba64e 100644
--- a/clang/lib/Lex/ModuleMap.cpp
+++ b/clang/lib/Lex/ModuleMap.cpp
@@ -1126,8 +1126,8 @@ static void inferFrameworkLink(Module *Mod) {
StringRef FrameworkName(Mod->Name);
FrameworkName.consume_back("_Private");
- Mod->LinkLibraries.push_back(Module::LinkLibrary(FrameworkName.str(),
- /*IsFramework=*/true));
+ Mod->LinkLibraries.emplace_back(FrameworkName.str(),
+ /*IsFramework=*/true);
}
Module *ModuleMap::inferFrameworkModule(DirectoryEntryRef FrameworkDir,
More information about the cfe-commits
mailing list