Take object file as input and handle files with the same name correctly.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@50749 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/tools/llvmc2/CompilationGraph.cpp b/tools/llvmc2/CompilationGraph.cpp
index 817e0fb..ed03f58 100644
--- a/tools/llvmc2/CompilationGraph.cpp
+++ b/tools/llvmc2/CompilationGraph.cpp
@@ -121,42 +121,50 @@
   B.IncrInEdges();
 }
 
+// Make a temporary file named like BaseName-RandomDigits.Suffix
+sys::Path MakeTempFile(const sys::Path& TempDir, const std::string& BaseName,
+                       const std::string& Suffix) {
+  sys::Path Out = TempDir;
+  Out.appendComponent(BaseName);
+  Out.appendSuffix(Suffix);
+  Out.makeUnique(true, NULL);
+  Out.eraseFromDisk();
+  return Out;
+}
+
 // Pass input file through the chain until we bump into a Join node or
 // a node that says that it is the last.
-const JoinTool*
-CompilationGraph::PassThroughGraph (sys::Path& In,
-                                    const Node* StartNode,
-                                    const sys::Path& TempDir) const {
+void CompilationGraph::PassThroughGraph (const sys::Path& InFile,
+                                         const Node* StartNode,
+                                         const sys::Path& TempDir) const {
   bool Last = false;
+  sys::Path In = InFile;
   const Node* CurNode = StartNode;
-  JoinTool* ret = 0;
 
   while(!Last) {
     sys::Path Out;
     Tool* CurTool = CurNode->ToolPtr.getPtr();
 
     if (CurTool->IsJoin()) {
-      ret = &dynamic_cast<JoinTool&>(*CurTool);
-      ret->AddToJoinList(In);
+      JoinTool& JT = dynamic_cast<JoinTool&>(*CurTool);
+      JT.AddToJoinList(In);
       break;
     }
 
-    // Is this the last tool?
+    // Since toolchains do not have to end with a Join node, we should
+    // check if this Node is the last.
     if (!CurNode->HasChildren() || CurTool->IsLast()) {
-      // Check if the first tool is also the last
-      if (Out.empty())
+      if (!OutputFilename.empty()) {
+        Out.set(OutputFilename);
+      }
+      else {
         Out.set(In.getBasename());
-      else
-        Out.appendComponent(In.getBasename());
-      Out.appendSuffix(CurTool->OutputSuffix());
+        Out.appendSuffix(CurTool->OutputSuffix());
+      }
       Last = true;
     }
     else {
-      Out = TempDir;
-      Out.appendComponent(In.getBasename());
-      Out.appendSuffix(CurTool->OutputSuffix());
-      Out.makeUnique(true, NULL);
-      Out.eraseFromDisk();
+      Out = MakeTempFile(TempDir, In.getBasename(), CurTool->OutputSuffix());
     }
 
     if (CurTool->GenerateAction(In, Out).Execute() != 0)
@@ -166,8 +174,6 @@
                                   CurNode->Name())->ToolName());
     In = Out; Out.clear();
   }
-
-  return ret;
 }
 
 // Sort the nodes in topological order.
@@ -215,7 +221,6 @@
   return &getNode(ChooseEdge(TV)->ToolName());
 }
 
-// TOFIX: merge some parts with PassThroughGraph.
 // Build the targets. Command-line options are passed through
 // temporary variables.
 int CompilationGraph::Build (const sys::Path& TempDir) {
@@ -243,10 +248,12 @@
     JoinTool* JT = &dynamic_cast<JoinTool&>(*CurNode->ToolPtr.getPtr());
     bool IsLast = false;
 
-    // Has files pending?
+    // Are there any files to be joined?
     if (JT->JoinListEmpty())
       continue;
 
+    // Is this the last tool in the chain?
+    // NOTE: we can process several chains in parallel.
     if (!CurNode->HasChildren() || JT->IsLast()) {
       if (OutputFilename.empty()) {
         Out.set("a");
@@ -257,11 +264,7 @@
       IsLast = true;
     }
     else {
-      Out = TempDir;
-      Out.appendComponent("tmp");
-      Out.appendSuffix(JT->OutputSuffix());
-      Out.makeUnique(true, NULL);
-      Out.eraseFromDisk();
+      Out = MakeTempFile(TempDir, "tmp", JT->OutputSuffix());
     }
 
     if (JT->GenerateAction(Out).Execute() != 0)