[lld] r275523 - ELF: Simplify path constructions for -save-temps. NFC.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 14 19:17:13 PDT 2016


Author: ruiu
Date: Thu Jul 14 21:17:13 2016
New Revision: 275523

URL: http://llvm.org/viewvc/llvm-project?rev=275523&view=rev
Log:
ELF: Simplify path constructions for -save-temps. NFC.

Modified:
    lld/trunk/ELF/LTO.cpp

Modified: lld/trunk/ELF/LTO.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LTO.cpp?rev=275523&r1=275522&r2=275523&view=diff
==============================================================================
--- lld/trunk/ELF/LTO.cpp (original)
+++ lld/trunk/ELF/LTO.cpp Thu Jul 14 21:17:13 2016
@@ -43,23 +43,18 @@ using namespace lld;
 using namespace lld::elf;
 
 // This is for use when debugging LTO.
-static void saveLtoObjectFile(StringRef Buffer, unsigned I, bool Many) {
-  SmallString<128> Path = Config->OutputFile;
-  if (Many)
-    Path += utostr(I);
-  Path += ".lto.o";
+static void saveBuffer(StringRef Buffer, const Twine &Path) {
   std::error_code EC;
-  raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None);
+  raw_fd_ostream OS(Path.str(), EC, sys::fs::OpenFlags::F_None);
   if (EC)
     error(EC, "cannot create " + Path);
   OS << Buffer;
 }
 
 // This is for use when debugging LTO.
-static void saveBCFile(Module &M, StringRef Suffix) {
-  std::string Path = (Config->OutputFile + Suffix).str();
+static void saveBCFile(Module &M, const Twine &Path) {
   std::error_code EC;
-  raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None);
+  raw_fd_ostream OS(Path.str(), EC, sys::fs::OpenFlags::F_None);
   if (EC)
     error(EC, "cannot create " + Path);
   WriteBitcodeToFile(&M, OS, /* ShouldPreserveUseListOrder */ true);
@@ -138,7 +133,7 @@ static void runLTOPasses(Module &M, Targ
   }
 
   if (Config->SaveTemps)
-    saveBCFile(M, ".lto.opt.bc");
+    saveBCFile(M, Config->OutputFile + ".lto.opt.bc");
 }
 
 static bool shouldInternalize(const SmallPtrSet<GlobalValue *, 8> &Used,
@@ -274,9 +269,16 @@ std::vector<std::unique_ptr<InputFile>>
     ObjFiles.push_back(createObjectFile(
         MemoryBufferRef(Obj, "LLD-INTERNAL-combined-lto-object")));
 
-  if (Config->SaveTemps)
-    for (unsigned I = 0; I < NumThreads; ++I)
-      saveLtoObjectFile(OwningData[I], I, NumThreads > 1);
+  // If -save-temps is given, we need to save temporary objects to files.
+  // This is for debugging.
+  if (Config->SaveTemps) {
+    if (NumThreads == 1) {
+      saveBuffer(OwningData[0], Config->OutputFile + ".lto.o");
+    } else {
+      for (unsigned I = 0; I < NumThreads; ++I)
+        saveBuffer(OwningData[I], Config->OutputFile + Twine(I) + ".lto.o");
+    }
+  }
 
   return ObjFiles;
 }
@@ -314,7 +316,7 @@ std::vector<std::unique_ptr<InputFile>>
   updateCompilerUsed(*Combined, *TM, AsmUndefinedRefs);
 
   if (Config->SaveTemps)
-    saveBCFile(*Combined, ".lto.bc");
+    saveBCFile(*Combined, Config->OutputFile + ".lto.bc");
 
   runLTOPasses(*Combined, *TM);
   if (HasError)




More information about the llvm-commits mailing list