From: Fangrui Song Date: Fri, 21 Jun 2019 11:05:26 +0000 (+0000) Subject: [Symbolize] Avoid lifetime extension and simplify std::map find/insert. NFC X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0a8ee95b82f3b9b2933408ff9cff88cf958aa7f9;p=llvm [Symbolize] Avoid lifetime extension and simplify std::map find/insert. NFC git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@364025 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/DebugInfo/Symbolize/Symbolize.cpp b/lib/DebugInfo/Symbolize/Symbolize.cpp index edb440a03c9..1bfd4e8904a 100644 --- a/lib/DebugInfo/Symbolize/Symbolize.cpp +++ b/lib/DebugInfo/Symbolize/Symbolize.cpp @@ -304,15 +304,14 @@ ObjectFile *LLVMSymbolizer::lookUpDebuglinkObject(const std::string &Path, Expected LLVMSymbolizer::getOrCreateObjectPair(const std::string &Path, const std::string &ArchName) { - const auto &I = ObjectPairForPathArch.find(std::make_pair(Path, ArchName)); - if (I != ObjectPairForPathArch.end()) { + auto I = ObjectPairForPathArch.find(std::make_pair(Path, ArchName)); + if (I != ObjectPairForPathArch.end()) return I->second; - } auto ObjOrErr = getOrCreateObject(Path, ArchName); if (!ObjOrErr) { - ObjectPairForPathArch.insert(std::make_pair(std::make_pair(Path, ArchName), - ObjectPair(nullptr, nullptr))); + ObjectPairForPathArch.emplace(std::make_pair(Path, ArchName), + ObjectPair(nullptr, nullptr)); return ObjOrErr.takeError(); } @@ -327,46 +326,43 @@ LLVMSymbolizer::getOrCreateObjectPair(const std::string &Path, if (!DbgObj) DbgObj = Obj; ObjectPair Res = std::make_pair(Obj, DbgObj); - ObjectPairForPathArch.insert( - std::make_pair(std::make_pair(Path, ArchName), Res)); + ObjectPairForPathArch.emplace(std::make_pair(Path, ArchName), Res); return Res; } Expected LLVMSymbolizer::getOrCreateObject(const std::string &Path, const std::string &ArchName) { - const auto &I = BinaryForPath.find(Path); - Binary *Bin = nullptr; - if (I == BinaryForPath.end()) { + Binary *Bin; + auto Pair = BinaryForPath.emplace(Path, OwningBinary()); + if (!Pair.second) { + Bin = Pair.first->second.getBinary(); + } else { Expected> BinOrErr = createBinary(Path); - if (!BinOrErr) { - BinaryForPath.insert(std::make_pair(Path, OwningBinary())); + if (!BinOrErr) return BinOrErr.takeError(); - } - Bin = BinOrErr->getBinary(); - BinaryForPath.insert(std::make_pair(Path, std::move(BinOrErr.get()))); - } else { - Bin = I->second.getBinary(); + Pair.first->second = std::move(BinOrErr.get()); + Bin = Pair.first->second.getBinary(); } if (!Bin) return static_cast(nullptr); if (MachOUniversalBinary *UB = dyn_cast_or_null(Bin)) { - const auto &I = ObjectForUBPathAndArch.find(std::make_pair(Path, ArchName)); - if (I != ObjectForUBPathAndArch.end()) { + auto I = ObjectForUBPathAndArch.find(std::make_pair(Path, ArchName)); + if (I != ObjectForUBPathAndArch.end()) return I->second.get(); - } + Expected> ObjOrErr = UB->getObjectForArch(ArchName); if (!ObjOrErr) { - ObjectForUBPathAndArch.insert(std::make_pair( - std::make_pair(Path, ArchName), std::unique_ptr())); + ObjectForUBPathAndArch.emplace(std::make_pair(Path, ArchName), + std::unique_ptr()); return ObjOrErr.takeError(); } ObjectFile *Res = ObjOrErr->get(); - ObjectForUBPathAndArch.insert(std::make_pair(std::make_pair(Path, ArchName), - std::move(ObjOrErr.get()))); + ObjectForUBPathAndArch.emplace(std::make_pair(Path, ArchName), + std::move(ObjOrErr.get())); return Res; } if (Bin->isObject()) { @@ -377,10 +373,10 @@ LLVMSymbolizer::getOrCreateObject(const std::string &Path, Expected LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) { - const auto &I = Modules.find(ModuleName); - if (I != Modules.end()) { + auto I = Modules.find(ModuleName); + if (I != Modules.end()) return I->second.get(); - } + std::string BinaryName = ModuleName; std::string ArchName = Opts.DefaultArch; size_t ColonPos = ModuleName.find_last_of(':'); @@ -395,8 +391,7 @@ LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) { auto ObjectsOrErr = getOrCreateObjectPair(BinaryName, ArchName); if (!ObjectsOrErr) { // Failed to find valid object file. - Modules.insert( - std::make_pair(ModuleName, std::unique_ptr())); + Modules.emplace(ModuleName, std::unique_ptr()); return ObjectsOrErr.takeError(); } ObjectPair Objects = ObjectsOrErr.get(); @@ -413,8 +408,7 @@ LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) { std::unique_ptr Session; if (auto Err = loadDataForEXE(PDB_ReaderType::DIA, Objects.first->getFileName(), Session)) { - Modules.insert( - std::make_pair(ModuleName, std::unique_ptr())); + Modules.emplace(ModuleName, std::unique_ptr()); // Return along the PDB filename to provide more context return createFileError(PDBFileName, std::move(Err)); } @@ -430,8 +424,7 @@ LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) { std::unique_ptr SymMod; if (InfoOrErr) SymMod = std::move(InfoOrErr.get()); - auto InsertResult = - Modules.insert(std::make_pair(ModuleName, std::move(SymMod))); + auto InsertResult = Modules.emplace(ModuleName, std::move(SymMod)); assert(InsertResult.second); if (auto EC = InfoOrErr.getError()) return errorCodeToError(EC);