Skip to content

Commit 7108dec

Browse files
[MC] Use *Map::try_emplace (NFC) (#140397)
try_emplace with is much shorter and simpler if we are default-constructing the value. While I'm at it, this patch uses structured bindings to receive the return value from try_emplace.
1 parent 55531e3 commit 7108dec

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

llvm/lib/MC/MCContext.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -619,16 +619,16 @@ MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
619619
Buffer.append(LinkedToSym->getName());
620620
support::endian::write(Buffer, UniqueID, endianness::native);
621621
StringRef UniqueMapKey = StringRef(Buffer);
622-
EntryNewPair = ELFUniquingMap.insert(std::make_pair(UniqueMapKey, nullptr));
622+
EntryNewPair = ELFUniquingMap.try_emplace(UniqueMapKey);
623623
} else if (!Section.isSingleStringRef()) {
624624
SmallString<128> Buffer;
625625
StringRef UniqueMapKey = Section.toStringRef(Buffer);
626626
SectionLen = UniqueMapKey.size();
627-
EntryNewPair = ELFUniquingMap.insert(std::make_pair(UniqueMapKey, nullptr));
627+
EntryNewPair = ELFUniquingMap.try_emplace(UniqueMapKey);
628628
} else {
629629
StringRef UniqueMapKey = Section.getSingleStringRef();
630630
SectionLen = UniqueMapKey.size();
631-
EntryNewPair = ELFUniquingMap.insert(std::make_pair(UniqueMapKey, nullptr));
631+
EntryNewPair = ELFUniquingMap.try_emplace(UniqueMapKey);
632632
}
633633

634634
if (!EntryNewPair.second)
@@ -696,10 +696,8 @@ MCSectionGOFF *MCContext::getGOFFSection(StringRef Section, SectionKind Kind,
696696
MCSection *Parent,
697697
uint32_t Subsection) {
698698
// Do the lookup. If we don't have a hit, return a new section.
699-
auto IterBool =
700-
GOFFUniquingMap.insert(std::make_pair(Section.str(), nullptr));
701-
auto Iter = IterBool.first;
702-
if (!IterBool.second)
699+
auto [Iter, Inserted] = GOFFUniquingMap.try_emplace(Section.str());
700+
if (!Inserted)
703701
return Iter->second;
704702

705703
StringRef CachedName = Iter->first;
@@ -731,9 +729,8 @@ MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
731729

732730
// Do the lookup, if we have a hit, return it.
733731
COFFSectionKey T{Section, COMDATSymName, Selection, UniqueID};
734-
auto IterBool = COFFUniquingMap.insert(std::make_pair(T, nullptr));
735-
auto Iter = IterBool.first;
736-
if (!IterBool.second)
732+
auto [Iter, Inserted] = COFFUniquingMap.try_emplace(T);
733+
if (!Inserted)
737734
return Iter->second;
738735

739736
StringRef CachedName = Iter->first.SectionName;

0 commit comments

Comments
 (0)