[llvm] r364025 - [Symbolize] Avoid lifetime extension and simplify std::map find/insert. NFC

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 21 04:05:27 PDT 2019


Author: maskray
Date: Fri Jun 21 04:05:26 2019
New Revision: 364025

URL: http://llvm.org/viewvc/llvm-project?rev=364025&view=rev
Log:
[Symbolize] Avoid lifetime extension and simplify std::map find/insert. NFC

Modified:
    llvm/trunk/lib/DebugInfo/Symbolize/Symbolize.cpp

Modified: llvm/trunk/lib/DebugInfo/Symbolize/Symbolize.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/Symbolize/Symbolize.cpp?rev=364025&r1=364024&r2=364025&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/Symbolize/Symbolize.cpp (original)
+++ llvm/trunk/lib/DebugInfo/Symbolize/Symbolize.cpp Fri Jun 21 04:05:26 2019
@@ -304,15 +304,14 @@ ObjectFile *LLVMSymbolizer::lookUpDebugl
 Expected<LLVMSymbolizer::ObjectPair>
 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(co
   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<ObjectFile *>
 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<Binary>());
+  if (!Pair.second) {
+    Bin = Pair.first->second.getBinary();
+  } else {
     Expected<OwningBinary<Binary>> BinOrErr = createBinary(Path);
-    if (!BinOrErr) {
-      BinaryForPath.insert(std::make_pair(Path, OwningBinary<Binary>()));
+    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<ObjectFile *>(nullptr);
 
   if (MachOUniversalBinary *UB = dyn_cast_or_null<MachOUniversalBinary>(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<std::unique_ptr<ObjectFile>> ObjOrErr =
         UB->getObjectForArch(ArchName);
     if (!ObjOrErr) {
-      ObjectForUBPathAndArch.insert(std::make_pair(
-          std::make_pair(Path, ArchName), std::unique_ptr<ObjectFile>()));
+      ObjectForUBPathAndArch.emplace(std::make_pair(Path, ArchName),
+                                     std::unique_ptr<ObjectFile>());
       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
 
 Expected<SymbolizableModule *>
 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(co
   auto ObjectsOrErr = getOrCreateObjectPair(BinaryName, ArchName);
   if (!ObjectsOrErr) {
     // Failed to find valid object file.
-    Modules.insert(
-        std::make_pair(ModuleName, std::unique_ptr<SymbolizableModule>()));
+    Modules.emplace(ModuleName, std::unique_ptr<SymbolizableModule>());
     return ObjectsOrErr.takeError();
   }
   ObjectPair Objects = ObjectsOrErr.get();
@@ -413,8 +408,7 @@ LLVMSymbolizer::getOrCreateModuleInfo(co
       std::unique_ptr<IPDBSession> Session;
       if (auto Err = loadDataForEXE(PDB_ReaderType::DIA,
                                     Objects.first->getFileName(), Session)) {
-        Modules.insert(
-            std::make_pair(ModuleName, std::unique_ptr<SymbolizableModule>()));
+        Modules.emplace(ModuleName, std::unique_ptr<SymbolizableModule>());
         // Return along the PDB filename to provide more context
         return createFileError(PDBFileName, std::move(Err));
       }
@@ -430,8 +424,7 @@ LLVMSymbolizer::getOrCreateModuleInfo(co
   std::unique_ptr<SymbolizableModule> 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);




More information about the llvm-commits mailing list