-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang] Use *Map::try_emplace (NFC) #140477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[clang] Use *Map::try_emplace (NFC) #140477
Conversation
We can simplify the code with *Map::try_emplace where we need default-constructed values while avoding calling constructors when keys are already present.
@llvm/pr-subscribers-clang Author: Kazu Hirata (kazutakahirata) ChangesWe can simplify the code with *Map::try_emplace where we need Full diff: https://github.com/llvm/llvm-project/pull/140477.diff 8 Files Affected:
diff --git a/clang/lib/AST/DeclBase.cpp b/clang/lib/AST/DeclBase.cpp
index e30057e32d312..a1bb62bcb68fa 100644
--- a/clang/lib/AST/DeclBase.cpp
+++ b/clang/lib/AST/DeclBase.cpp
@@ -1924,8 +1924,7 @@ DeclContext::lookupImpl(DeclarationName Name,
Map = CreateStoredDeclsMap(getParentASTContext());
// If we have a lookup result with no external decls, we are done.
- std::pair<StoredDeclsMap::iterator, bool> R =
- Map->insert(std::make_pair(Name, StoredDeclsList()));
+ std::pair<StoredDeclsMap::iterator, bool> R = Map->try_emplace(Name);
if (!R.second && !R.first->second.hasExternalDecls())
return R.first->second.getLookupResult();
diff --git a/clang/lib/Basic/Diagnostic.cpp b/clang/lib/Basic/Diagnostic.cpp
index c45607f218594..b48eed8650672 100644
--- a/clang/lib/Basic/Diagnostic.cpp
+++ b/clang/lib/Basic/Diagnostic.cpp
@@ -150,8 +150,7 @@ void DiagnosticsEngine::Reset(bool soft /*=false*/) {
DiagnosticMapping &
DiagnosticsEngine::DiagState::getOrAddMapping(diag::kind Diag) {
- std::pair<iterator, bool> Result =
- DiagMap.insert(std::make_pair(Diag, DiagnosticMapping()));
+ std::pair<iterator, bool> Result = DiagMap.try_emplace(Diag);
// Initialize the entry if we added it.
if (Result.second) {
diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp
index 6d6e54b1bec69..4028bbf060364 100644
--- a/clang/lib/Basic/SourceManager.cpp
+++ b/clang/lib/Basic/SourceManager.cpp
@@ -1726,7 +1726,7 @@ void SourceManager::computeMacroArgsCache(MacroArgsMap &MacroArgsCache,
assert(FID.isValid());
// Initially no macro argument chunk is present.
- MacroArgsCache.insert(std::make_pair(0, SourceLocation()));
+ MacroArgsCache.try_emplace(0);
int ID = FID.ID;
while (true) {
diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index df6edee93f3bb..e458d437d085a 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -11350,7 +11350,7 @@ CGOpenMPRuntime::LastprivateConditionalRAII::LastprivateConditionalRAII(
LastprivateConditionalData &Data =
CGM.getOpenMPRuntime().LastprivateConditionalStack.emplace_back();
for (const Decl *VD : NeedToAddForLPCsAsDisabled)
- Data.DeclToUniqueName.insert(std::make_pair(VD, SmallString<16>()));
+ Data.DeclToUniqueName.try_emplace(VD);
Data.Fn = CGF.CurFn;
Data.Disabled = true;
}
diff --git a/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp b/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
index 6f819640a9521..f746b483b2b91 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
@@ -1020,7 +1020,7 @@ llvm::Function *CGOpenMPRuntimeGPU::emitTeamsOutlinedFunction(
for (const auto &Pair : MappedDeclsFields) {
assert(Pair.getFirst()->isCanonicalDecl() &&
"Expected canonical declaration");
- Data.insert(std::make_pair(Pair.getFirst(), MappedVarData()));
+ Data.try_emplace(Pair.getFirst());
}
}
Rt.emitGenericVarsProlog(CGF, Loc);
@@ -2025,7 +2025,7 @@ void CGOpenMPRuntimeGPU::emitFunctionProlog(CodeGenFunction &CGF,
DeclToAddrMapTy &Data = I->getSecond().LocalVarData;
for (const ValueDecl *VD : VarChecker.getEscapedDecls()) {
assert(VD->isCanonicalDecl() && "Expected canonical declaration");
- Data.insert(std::make_pair(VD, MappedVarData()));
+ Data.try_emplace(VD);
}
if (!NeedToDelayGlobalization) {
emitGenericVarsProlog(CGF, D->getBeginLoc());
diff --git a/clang/lib/Lex/PPLexerChange.cpp b/clang/lib/Lex/PPLexerChange.cpp
index 44b5fa8be9f1b..d8f61c02a9837 100644
--- a/clang/lib/Lex/PPLexerChange.cpp
+++ b/clang/lib/Lex/PPLexerChange.cpp
@@ -711,7 +711,7 @@ void Preprocessor::EnterSubmodule(Module *M, SourceLocation ImportLoc,
ModMap.resolveConflicts(M, /*Complain=*/false);
// If this is the first time we've entered this module, set up its state.
- auto R = Submodules.insert(std::make_pair(M, SubmoduleState()));
+ auto R = Submodules.try_emplace(M);
auto &State = R.first->second;
bool FirstTime = R.second;
if (FirstTime) {
diff --git a/clang/lib/Lex/Preprocessor.cpp b/clang/lib/Lex/Preprocessor.cpp
index 4c2dbbe881b48..21fc7a2b6fae2 100644
--- a/clang/lib/Lex/Preprocessor.cpp
+++ b/clang/lib/Lex/Preprocessor.cpp
@@ -319,7 +319,7 @@ Preprocessor::macro_begin(bool IncludeExternalMacros) const {
// Make sure we cover all macros in visible modules.
for (const ModuleMacro &Macro : ModuleMacros)
- CurSubmoduleState->Macros.insert(std::make_pair(Macro.II, MacroState()));
+ CurSubmoduleState->Macros.try_emplace(Macro.II);
return CurSubmoduleState->Macros.begin();
}
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 6dae243b520f0..d47c1d39adf92 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -20342,7 +20342,7 @@ bool Sema::IsValueInFlagEnum(const EnumDecl *ED, const llvm::APInt &Val,
assert(ED->isClosedFlag() && "looking for value in non-flag or open enum");
assert(ED->isCompleteDefinition() && "expected enum definition");
- auto R = FlagBitsCache.insert(std::make_pair(ED, llvm::APInt()));
+ auto R = FlagBitsCache.try_emplace(ED);
llvm::APInt &FlagBits = R.first->second;
if (R.second) {
|
@llvm/pr-subscribers-clang-codegen Author: Kazu Hirata (kazutakahirata) ChangesWe can simplify the code with *Map::try_emplace where we need Full diff: https://github.com/llvm/llvm-project/pull/140477.diff 8 Files Affected:
diff --git a/clang/lib/AST/DeclBase.cpp b/clang/lib/AST/DeclBase.cpp
index e30057e32d312..a1bb62bcb68fa 100644
--- a/clang/lib/AST/DeclBase.cpp
+++ b/clang/lib/AST/DeclBase.cpp
@@ -1924,8 +1924,7 @@ DeclContext::lookupImpl(DeclarationName Name,
Map = CreateStoredDeclsMap(getParentASTContext());
// If we have a lookup result with no external decls, we are done.
- std::pair<StoredDeclsMap::iterator, bool> R =
- Map->insert(std::make_pair(Name, StoredDeclsList()));
+ std::pair<StoredDeclsMap::iterator, bool> R = Map->try_emplace(Name);
if (!R.second && !R.first->second.hasExternalDecls())
return R.first->second.getLookupResult();
diff --git a/clang/lib/Basic/Diagnostic.cpp b/clang/lib/Basic/Diagnostic.cpp
index c45607f218594..b48eed8650672 100644
--- a/clang/lib/Basic/Diagnostic.cpp
+++ b/clang/lib/Basic/Diagnostic.cpp
@@ -150,8 +150,7 @@ void DiagnosticsEngine::Reset(bool soft /*=false*/) {
DiagnosticMapping &
DiagnosticsEngine::DiagState::getOrAddMapping(diag::kind Diag) {
- std::pair<iterator, bool> Result =
- DiagMap.insert(std::make_pair(Diag, DiagnosticMapping()));
+ std::pair<iterator, bool> Result = DiagMap.try_emplace(Diag);
// Initialize the entry if we added it.
if (Result.second) {
diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp
index 6d6e54b1bec69..4028bbf060364 100644
--- a/clang/lib/Basic/SourceManager.cpp
+++ b/clang/lib/Basic/SourceManager.cpp
@@ -1726,7 +1726,7 @@ void SourceManager::computeMacroArgsCache(MacroArgsMap &MacroArgsCache,
assert(FID.isValid());
// Initially no macro argument chunk is present.
- MacroArgsCache.insert(std::make_pair(0, SourceLocation()));
+ MacroArgsCache.try_emplace(0);
int ID = FID.ID;
while (true) {
diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index df6edee93f3bb..e458d437d085a 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -11350,7 +11350,7 @@ CGOpenMPRuntime::LastprivateConditionalRAII::LastprivateConditionalRAII(
LastprivateConditionalData &Data =
CGM.getOpenMPRuntime().LastprivateConditionalStack.emplace_back();
for (const Decl *VD : NeedToAddForLPCsAsDisabled)
- Data.DeclToUniqueName.insert(std::make_pair(VD, SmallString<16>()));
+ Data.DeclToUniqueName.try_emplace(VD);
Data.Fn = CGF.CurFn;
Data.Disabled = true;
}
diff --git a/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp b/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
index 6f819640a9521..f746b483b2b91 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
@@ -1020,7 +1020,7 @@ llvm::Function *CGOpenMPRuntimeGPU::emitTeamsOutlinedFunction(
for (const auto &Pair : MappedDeclsFields) {
assert(Pair.getFirst()->isCanonicalDecl() &&
"Expected canonical declaration");
- Data.insert(std::make_pair(Pair.getFirst(), MappedVarData()));
+ Data.try_emplace(Pair.getFirst());
}
}
Rt.emitGenericVarsProlog(CGF, Loc);
@@ -2025,7 +2025,7 @@ void CGOpenMPRuntimeGPU::emitFunctionProlog(CodeGenFunction &CGF,
DeclToAddrMapTy &Data = I->getSecond().LocalVarData;
for (const ValueDecl *VD : VarChecker.getEscapedDecls()) {
assert(VD->isCanonicalDecl() && "Expected canonical declaration");
- Data.insert(std::make_pair(VD, MappedVarData()));
+ Data.try_emplace(VD);
}
if (!NeedToDelayGlobalization) {
emitGenericVarsProlog(CGF, D->getBeginLoc());
diff --git a/clang/lib/Lex/PPLexerChange.cpp b/clang/lib/Lex/PPLexerChange.cpp
index 44b5fa8be9f1b..d8f61c02a9837 100644
--- a/clang/lib/Lex/PPLexerChange.cpp
+++ b/clang/lib/Lex/PPLexerChange.cpp
@@ -711,7 +711,7 @@ void Preprocessor::EnterSubmodule(Module *M, SourceLocation ImportLoc,
ModMap.resolveConflicts(M, /*Complain=*/false);
// If this is the first time we've entered this module, set up its state.
- auto R = Submodules.insert(std::make_pair(M, SubmoduleState()));
+ auto R = Submodules.try_emplace(M);
auto &State = R.first->second;
bool FirstTime = R.second;
if (FirstTime) {
diff --git a/clang/lib/Lex/Preprocessor.cpp b/clang/lib/Lex/Preprocessor.cpp
index 4c2dbbe881b48..21fc7a2b6fae2 100644
--- a/clang/lib/Lex/Preprocessor.cpp
+++ b/clang/lib/Lex/Preprocessor.cpp
@@ -319,7 +319,7 @@ Preprocessor::macro_begin(bool IncludeExternalMacros) const {
// Make sure we cover all macros in visible modules.
for (const ModuleMacro &Macro : ModuleMacros)
- CurSubmoduleState->Macros.insert(std::make_pair(Macro.II, MacroState()));
+ CurSubmoduleState->Macros.try_emplace(Macro.II);
return CurSubmoduleState->Macros.begin();
}
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 6dae243b520f0..d47c1d39adf92 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -20342,7 +20342,7 @@ bool Sema::IsValueInFlagEnum(const EnumDecl *ED, const llvm::APInt &Val,
assert(ED->isClosedFlag() && "looking for value in non-flag or open enum");
assert(ED->isCompleteDefinition() && "expected enum definition");
- auto R = FlagBitsCache.insert(std::make_pair(ED, llvm::APInt()));
+ auto R = FlagBitsCache.try_emplace(ED);
llvm::APInt &FlagBits = R.first->second;
if (R.second) {
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/11/builds/15430 Here is the relevant piece of the build log for the reference
|
We can simplify the code with *Map::try_emplace where we need default-constructed values while avoding calling constructors when keys are already present.
We can simplify the code with *Map::try_emplace where we need
default-constructed values while avoding calling constructors when
keys are already present.