Skip to content

Commit 3252816

Browse files
[clang] Use *Map::try_emplace (NFC) (#140477)
We can simplify the code with *Map::try_emplace where we need default-constructed values while avoding calling constructors when keys are already present.
1 parent 2a277f9 commit 3252816

File tree

8 files changed

+9
-11
lines changed

8 files changed

+9
-11
lines changed

clang/lib/AST/DeclBase.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1924,8 +1924,7 @@ DeclContext::lookupImpl(DeclarationName Name,
19241924
Map = CreateStoredDeclsMap(getParentASTContext());
19251925

19261926
// If we have a lookup result with no external decls, we are done.
1927-
std::pair<StoredDeclsMap::iterator, bool> R =
1928-
Map->insert(std::make_pair(Name, StoredDeclsList()));
1927+
std::pair<StoredDeclsMap::iterator, bool> R = Map->try_emplace(Name);
19291928
if (!R.second && !R.first->second.hasExternalDecls())
19301929
return R.first->second.getLookupResult();
19311930

clang/lib/Basic/Diagnostic.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,7 @@ void DiagnosticsEngine::Reset(bool soft /*=false*/) {
150150

151151
DiagnosticMapping &
152152
DiagnosticsEngine::DiagState::getOrAddMapping(diag::kind Diag) {
153-
std::pair<iterator, bool> Result =
154-
DiagMap.insert(std::make_pair(Diag, DiagnosticMapping()));
153+
std::pair<iterator, bool> Result = DiagMap.try_emplace(Diag);
155154

156155
// Initialize the entry if we added it.
157156
if (Result.second) {

clang/lib/Basic/SourceManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1726,7 +1726,7 @@ void SourceManager::computeMacroArgsCache(MacroArgsMap &MacroArgsCache,
17261726
assert(FID.isValid());
17271727

17281728
// Initially no macro argument chunk is present.
1729-
MacroArgsCache.insert(std::make_pair(0, SourceLocation()));
1729+
MacroArgsCache.try_emplace(0);
17301730

17311731
int ID = FID.ID;
17321732
while (true) {

clang/lib/CodeGen/CGOpenMPRuntime.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11350,7 +11350,7 @@ CGOpenMPRuntime::LastprivateConditionalRAII::LastprivateConditionalRAII(
1135011350
LastprivateConditionalData &Data =
1135111351
CGM.getOpenMPRuntime().LastprivateConditionalStack.emplace_back();
1135211352
for (const Decl *VD : NeedToAddForLPCsAsDisabled)
11353-
Data.DeclToUniqueName.insert(std::make_pair(VD, SmallString<16>()));
11353+
Data.DeclToUniqueName.try_emplace(VD);
1135411354
Data.Fn = CGF.CurFn;
1135511355
Data.Disabled = true;
1135611356
}

clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,7 @@ llvm::Function *CGOpenMPRuntimeGPU::emitTeamsOutlinedFunction(
10201020
for (const auto &Pair : MappedDeclsFields) {
10211021
assert(Pair.getFirst()->isCanonicalDecl() &&
10221022
"Expected canonical declaration");
1023-
Data.insert(std::make_pair(Pair.getFirst(), MappedVarData()));
1023+
Data.try_emplace(Pair.getFirst());
10241024
}
10251025
}
10261026
Rt.emitGenericVarsProlog(CGF, Loc);
@@ -2025,7 +2025,7 @@ void CGOpenMPRuntimeGPU::emitFunctionProlog(CodeGenFunction &CGF,
20252025
DeclToAddrMapTy &Data = I->getSecond().LocalVarData;
20262026
for (const ValueDecl *VD : VarChecker.getEscapedDecls()) {
20272027
assert(VD->isCanonicalDecl() && "Expected canonical declaration");
2028-
Data.insert(std::make_pair(VD, MappedVarData()));
2028+
Data.try_emplace(VD);
20292029
}
20302030
if (!NeedToDelayGlobalization) {
20312031
emitGenericVarsProlog(CGF, D->getBeginLoc());

clang/lib/Lex/PPLexerChange.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ void Preprocessor::EnterSubmodule(Module *M, SourceLocation ImportLoc,
711711
ModMap.resolveConflicts(M, /*Complain=*/false);
712712

713713
// If this is the first time we've entered this module, set up its state.
714-
auto R = Submodules.insert(std::make_pair(M, SubmoduleState()));
714+
auto R = Submodules.try_emplace(M);
715715
auto &State = R.first->second;
716716
bool FirstTime = R.second;
717717
if (FirstTime) {

clang/lib/Lex/Preprocessor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ Preprocessor::macro_begin(bool IncludeExternalMacros) const {
319319

320320
// Make sure we cover all macros in visible modules.
321321
for (const ModuleMacro &Macro : ModuleMacros)
322-
CurSubmoduleState->Macros.insert(std::make_pair(Macro.II, MacroState()));
322+
CurSubmoduleState->Macros.try_emplace(Macro.II);
323323

324324
return CurSubmoduleState->Macros.begin();
325325
}

clang/lib/Sema/SemaDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20342,7 +20342,7 @@ bool Sema::IsValueInFlagEnum(const EnumDecl *ED, const llvm::APInt &Val,
2034220342
assert(ED->isClosedFlag() && "looking for value in non-flag or open enum");
2034320343
assert(ED->isCompleteDefinition() && "expected enum definition");
2034420344

20345-
auto R = FlagBitsCache.insert(std::make_pair(ED, llvm::APInt()));
20345+
auto R = FlagBitsCache.try_emplace(ED);
2034620346
llvm::APInt &FlagBits = R.first->second;
2034720347

2034820348
if (R.second) {

0 commit comments

Comments
 (0)