[clang] [clang] Use *Map::try_emplace (NFC) (PR #143563)
Kazu Hirata via cfe-commits
cfe-commits at lists.llvm.org
Tue Jun 10 08:56:34 PDT 2025
https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/143563
- try_emplace(Key) is shorter than insert({Key, nullptr}).
- try_emplace performs value initialization without value parameters.
- We overwrite values on successful insertion anyway.
>From 3a578a03d2318dcb999f63c5dab96b2b36d58efa Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Mon, 9 Jun 2025 12:56:28 -0700
Subject: [PATCH] [clang] Use *Map::try_emplace (NFC)
- try_emplace(Key) is shorter than insert({Key, nullptr}).
- try_emplace performs value initialization without value parameters.
- We overwrite values on successful insertion anyway.
---
clang/include/clang/Basic/IdentifierTable.h | 2 +-
clang/include/clang/ExtractAPI/API.h | 2 +-
clang/lib/CodeGen/CodeGenTypes.cpp | 2 +-
.../Tooling/DependencyScanning/DependencyScanningFilesystem.cpp | 2 +-
4 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/clang/include/clang/Basic/IdentifierTable.h b/clang/include/clang/Basic/IdentifierTable.h
index 54540193cfcc0..e4044bcdfcc60 100644
--- a/clang/include/clang/Basic/IdentifierTable.h
+++ b/clang/include/clang/Basic/IdentifierTable.h
@@ -731,7 +731,7 @@ class IdentifierTable {
/// introduce or modify an identifier. If they called get(), they would
/// likely end up in a recursion.
IdentifierInfo &getOwn(StringRef Name) {
- auto &Entry = *HashTable.insert(std::make_pair(Name, nullptr)).first;
+ auto &Entry = *HashTable.try_emplace(Name).first;
IdentifierInfo *&II = Entry.second;
if (II)
diff --git a/clang/include/clang/ExtractAPI/API.h b/clang/include/clang/ExtractAPI/API.h
index c30e6fac66d6b..1ace535686a3e 100644
--- a/clang/include/clang/ExtractAPI/API.h
+++ b/clang/include/clang/ExtractAPI/API.h
@@ -1499,7 +1499,7 @@ APISet::createRecord(StringRef USR, StringRef Name,
CtorArgsContTy &&...CtorArgs) {
// Ensure USR refers to a String stored in the allocator.
auto USRString = copyString(USR);
- auto Result = USRBasedLookupTable.insert({USRString, nullptr});
+ auto Result = USRBasedLookupTable.try_emplace(USRString);
RecordTy *Record;
// Create the record if it does not already exist
diff --git a/clang/lib/CodeGen/CodeGenTypes.cpp b/clang/lib/CodeGen/CodeGenTypes.cpp
index 09f6b20f53ec2..c98503e4bbd26 100644
--- a/clang/lib/CodeGen/CodeGenTypes.cpp
+++ b/clang/lib/CodeGen/CodeGenTypes.cpp
@@ -726,7 +726,7 @@ llvm::Type *CodeGenTypes::ConvertType(QualType T) {
auto *MPTy = cast<MemberPointerType>(Ty);
if (!getCXXABI().isMemberPointerConvertible(MPTy)) {
auto *C = MPTy->getMostRecentCXXRecordDecl()->getTypeForDecl();
- auto Insertion = RecordsWithOpaqueMemberPointers.insert({C, nullptr});
+ auto Insertion = RecordsWithOpaqueMemberPointers.try_emplace(C);
if (Insertion.second)
Insertion.first->second = llvm::StructType::create(getLLVMContext());
ResultType = Insertion.first->second;
diff --git a/clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp b/clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp
index 4316b584a2f32..140833050f4e9 100644
--- a/clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp
+++ b/clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp
@@ -173,7 +173,7 @@ DependencyScanningFilesystemSharedCache::CacheShard::getOrEmplaceEntryForUID(
llvm::sys::fs::UniqueID UID, llvm::vfs::Status Stat,
std::unique_ptr<llvm::MemoryBuffer> Contents) {
std::lock_guard<std::mutex> LockGuard(CacheLock);
- auto [It, Inserted] = EntriesByUID.insert({UID, nullptr});
+ auto [It, Inserted] = EntriesByUID.try_emplace(UID);
auto &CachedEntry = It->getSecond();
if (Inserted) {
CachedFileContents *StoredContents = nullptr;
More information about the cfe-commits
mailing list