[llvm] [ADT] Use brace initialization in Set/Map (NFC) (PR #155182)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Aug 24 10:13:24 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-adt
Author: Kazu Hirata (kazutakahirata)
<details>
<summary>Changes</summary>
With brace initialization, we get to avoid type decay and enjoy
guaranteed copy elision as part of C++17.
---
Full diff: https://github.com/llvm/llvm-project/pull/155182.diff
5 Files Affected:
- (modified) llvm/include/llvm/ADT/DenseMap.h (+6-9)
- (modified) llvm/include/llvm/ADT/MapVector.h (+4-4)
- (modified) llvm/include/llvm/ADT/SmallPtrSet.h (+3-3)
- (modified) llvm/include/llvm/ADT/SparseSet.h (+2-2)
- (modified) llvm/include/llvm/ADT/StringMap.h (+3-3)
``````````diff
diff --git a/llvm/include/llvm/ADT/DenseMap.h b/llvm/include/llvm/ADT/DenseMap.h
index 643dee98f0e28..ab1bc6356dcb9 100644
--- a/llvm/include/llvm/ADT/DenseMap.h
+++ b/llvm/include/llvm/ADT/DenseMap.h
@@ -257,13 +257,12 @@ class DenseMapBase : public DebugEpochBase {
std::pair<iterator, bool> try_emplace(KeyT &&Key, Ts &&...Args) {
BucketT *TheBucket;
if (LookupBucketFor(Key, TheBucket))
- return std::make_pair(makeInsertIterator(TheBucket),
- false); // Already in map.
+ return {makeInsertIterator(TheBucket), false}; // Already in map.
// Otherwise, insert the new element.
TheBucket =
InsertIntoBucket(TheBucket, std::move(Key), std::forward<Ts>(Args)...);
- return std::make_pair(makeInsertIterator(TheBucket), true);
+ return {makeInsertIterator(TheBucket), true};
}
// Inserts key,value pair into the map if the key isn't already in the map.
@@ -273,12 +272,11 @@ class DenseMapBase : public DebugEpochBase {
std::pair<iterator, bool> try_emplace(const KeyT &Key, Ts &&...Args) {
BucketT *TheBucket;
if (LookupBucketFor(Key, TheBucket))
- return std::make_pair(makeInsertIterator(TheBucket),
- false); // Already in map.
+ return {makeInsertIterator(TheBucket), false}; // Already in map.
// Otherwise, insert the new element.
TheBucket = InsertIntoBucket(TheBucket, Key, std::forward<Ts>(Args)...);
- return std::make_pair(makeInsertIterator(TheBucket), true);
+ return {makeInsertIterator(TheBucket), true};
}
/// Alternate version of insert() which allows a different, and possibly
@@ -291,13 +289,12 @@ class DenseMapBase : public DebugEpochBase {
const LookupKeyT &Val) {
BucketT *TheBucket;
if (LookupBucketFor(Val, TheBucket))
- return std::make_pair(makeInsertIterator(TheBucket),
- false); // Already in map.
+ return {makeInsertIterator(TheBucket), false}; // Already in map.
// Otherwise, insert the new element.
TheBucket = InsertIntoBucketWithLookup(TheBucket, std::move(KV.first),
std::move(KV.second), Val);
- return std::make_pair(makeInsertIterator(TheBucket), true);
+ return {makeInsertIterator(TheBucket), true};
}
/// insert - Range insertion of pairs.
diff --git a/llvm/include/llvm/ADT/MapVector.h b/llvm/include/llvm/ADT/MapVector.h
index 015605fd98dff..24976535716d1 100644
--- a/llvm/include/llvm/ADT/MapVector.h
+++ b/llvm/include/llvm/ADT/MapVector.h
@@ -123,9 +123,9 @@ class MapVector {
It->second = Vector.size();
Vector.emplace_back(std::piecewise_construct, std::forward_as_tuple(Key),
std::forward_as_tuple(std::forward<Ts>(Args)...));
- return std::make_pair(std::prev(end()), true);
+ return {std::prev(end()), true};
}
- return std::make_pair(begin() + It->second, false);
+ return {begin() + It->second, false};
}
template <typename... Ts>
std::pair<iterator, bool> try_emplace(KeyT &&Key, Ts &&...Args) {
@@ -135,9 +135,9 @@ class MapVector {
Vector.emplace_back(std::piecewise_construct,
std::forward_as_tuple(std::move(Key)),
std::forward_as_tuple(std::forward<Ts>(Args)...));
- return std::make_pair(std::prev(end()), true);
+ return {std::prev(end()), true};
}
- return std::make_pair(begin() + It->second, false);
+ return {begin() + It->second, false};
}
std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &KV) {
diff --git a/llvm/include/llvm/ADT/SmallPtrSet.h b/llvm/include/llvm/ADT/SmallPtrSet.h
index f55627c6866f7..16ad3973e054d 100644
--- a/llvm/include/llvm/ADT/SmallPtrSet.h
+++ b/llvm/include/llvm/ADT/SmallPtrSet.h
@@ -172,14 +172,14 @@ class SmallPtrSetImplBase : public DebugEpochBase {
// Check to see if it is already in the set.
for (const void *&Bucket : small_buckets()) {
if (Bucket == Ptr)
- return std::make_pair(&Bucket, false);
+ return {&Bucket, false};
}
// Nope, there isn't. If we stay small, just 'pushback' now.
if (NumEntries < CurArraySize) {
CurArray[NumEntries++] = Ptr;
incrementEpoch();
- return std::make_pair(CurArray + (NumEntries - 1), true);
+ return {CurArray + (NumEntries - 1), true};
}
// Otherwise, hit the big set case, which will call grow.
}
@@ -400,7 +400,7 @@ class SmallPtrSetImpl : public SmallPtrSetImplBase {
/// the element equal to Ptr.
std::pair<iterator, bool> insert(PtrType Ptr) {
auto p = insert_imp(PtrTraits::getAsVoidPointer(Ptr));
- return std::make_pair(makeIterator(p.first), p.second);
+ return {makeIterator(p.first), p.second};
}
/// Insert the given pointer with an iterator hint that is ignored. This is
diff --git a/llvm/include/llvm/ADT/SparseSet.h b/llvm/include/llvm/ADT/SparseSet.h
index d9ded9875d377..a8ebc9f786486 100644
--- a/llvm/include/llvm/ADT/SparseSet.h
+++ b/llvm/include/llvm/ADT/SparseSet.h
@@ -258,10 +258,10 @@ class SparseSet {
unsigned Idx = ValIndexOf(Val);
iterator I = findIndex(Idx);
if (I != end())
- return std::make_pair(I, false);
+ return {I, false};
Sparse[Idx] = size();
Dense.push_back(Val);
- return std::make_pair(end() - 1, true);
+ return {end() - 1, true};
}
/// array subscript - If an element already exists with this key, return it.
diff --git a/llvm/include/llvm/ADT/StringMap.h b/llvm/include/llvm/ADT/StringMap.h
index 0bf062f988f3a..dbc0485967ac6 100644
--- a/llvm/include/llvm/ADT/StringMap.h
+++ b/llvm/include/llvm/ADT/StringMap.h
@@ -380,8 +380,8 @@ class LLVM_ALLOCATORHOLDER_EMPTYBASE StringMap
unsigned BucketNo = LookupBucketFor(Key, FullHashValue);
StringMapEntryBase *&Bucket = TheTable[BucketNo];
if (Bucket && Bucket != getTombstoneVal())
- return std::make_pair(iterator(TheTable + BucketNo, false),
- false); // Already exists in map.
+ return {iterator(TheTable + BucketNo, false),
+ false}; // Already exists in map.
if (Bucket == getTombstoneVal())
--NumTombstones;
@@ -391,7 +391,7 @@ class LLVM_ALLOCATORHOLDER_EMPTYBASE StringMap
assert(NumItems + NumTombstones <= NumBuckets);
BucketNo = RehashTable(BucketNo);
- return std::make_pair(iterator(TheTable + BucketNo, false), true);
+ return {iterator(TheTable + BucketNo, false), true};
}
// clear - Empties out the StringMap
``````````
</details>
https://github.com/llvm/llvm-project/pull/155182
More information about the llvm-commits
mailing list