blob: df1f2395654440a28e9c65954f2f406f132e872b [file] [log] [blame]
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +00001//===--- CompilationGraph.cpp - The LLVM Compiler Driver --------*- C++ -*-===//
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open
6// Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000010// Compilation graph - implementation.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000011//
12//===----------------------------------------------------------------------===//
13
Mikhail Glushenkova6730372008-05-12 16:31:42 +000014#include "Error.h"
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000015#include "CompilationGraph.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000016
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000017#include "llvm/ADT/STLExtras.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000018#include "llvm/Support/CommandLine.h"
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000019#include "llvm/Support/DOTGraphTraits.h"
20#include "llvm/Support/GraphWriter.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000021
Mikhail Glushenkov02606582008-05-06 18:07:14 +000022#include <algorithm>
Mikhail Glushenkov02606582008-05-06 18:07:14 +000023#include <iterator>
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000024#include <limits>
Mikhail Glushenkov02606582008-05-06 18:07:14 +000025#include <queue>
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000026#include <stdexcept>
27
28using namespace llvm;
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000029using namespace llvmc;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000030
31extern cl::list<std::string> InputFilenames;
32extern cl::opt<std::string> OutputFilename;
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000033extern cl::list<std::string> Languages;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000034
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +000035namespace llvmc {
36 /// ExtsToLangs - Map from file extensions to language names.
37 LanguageMap GlobalLanguageMap;
38
39 /// GetLanguage - Find the language name corresponding to the given file.
40 const std::string& GetLanguage(const sys::Path& File) {
41 LanguageMap::const_iterator Lang = GlobalLanguageMap.find(File.getSuffix());
42 if (Lang == GlobalLanguageMap.end())
43 throw std::runtime_error("Unknown suffix: " + File.getSuffix());
44 return Lang->second;
45 }
46}
47
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000048namespace {
49
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000050 /// ChooseEdge - Return the edge with the maximum weight.
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000051 template <class C>
52 const Edge* ChooseEdge(const C& EdgesContainer,
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +000053 const InputLanguagesSet& InLangs,
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000054 const std::string& NodeName = "root") {
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000055 const Edge* MaxEdge = 0;
56 unsigned MaxWeight = 0;
57 bool SingleMax = true;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000058
59 for (typename C::const_iterator B = EdgesContainer.begin(),
60 E = EdgesContainer.end(); B != E; ++B) {
61 const Edge* E = B->getPtr();
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +000062 unsigned EW = E->Weight(InLangs);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000063 if (EW > MaxWeight) {
64 MaxEdge = E;
65 MaxWeight = EW;
66 SingleMax = true;
Mikhail Glushenkove0ff9ae2008-05-06 18:18:58 +000067 } else if (EW == MaxWeight) {
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000068 SingleMax = false;
69 }
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000070 }
71
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000072 if (!SingleMax)
73 throw std::runtime_error("Node " + NodeName +
74 ": multiple maximal outward edges found!"
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000075 " Most probably a specification error.");
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000076 if (!MaxEdge)
77 throw std::runtime_error("Node " + NodeName +
78 ": no maximal outward edge found!"
79 " Most probably a specification error.");
80 return MaxEdge;
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000081 }
82
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000083}
84
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000085CompilationGraph::CompilationGraph() {
86 NodesMap["root"] = Node(this);
87}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000088
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000089Node& CompilationGraph::getNode(const std::string& ToolName) {
90 nodes_map_type::iterator I = NodesMap.find(ToolName);
91 if (I == NodesMap.end())
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000092 throw std::runtime_error("Node " + ToolName + " is not in the graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000093 return I->second;
94}
95
96const Node& CompilationGraph::getNode(const std::string& ToolName) const {
97 nodes_map_type::const_iterator I = NodesMap.find(ToolName);
98 if (I == NodesMap.end())
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000099 throw std::runtime_error("Node " + ToolName + " is not in the graph!");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000100 return I->second;
101}
102
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000103// Find the tools list corresponding to the given language name.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000104const CompilationGraph::tools_vector_type&
105CompilationGraph::getToolsVector(const std::string& LangName) const
106{
107 tools_map_type::const_iterator I = ToolsMap.find(LangName);
108 if (I == ToolsMap.end())
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000109 throw std::runtime_error("No tool corresponding to the language "
Mikhail Glushenkov35bca412008-05-30 06:16:59 +0000110 + LangName + " found");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000111 return I->second;
112}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000113
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000114void CompilationGraph::insertNode(Tool* V) {
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000115 if (NodesMap.count(V->Name()) == 0) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000116 Node N;
117 N.OwningGraph = this;
118 N.ToolPtr = V;
119 NodesMap[V->Name()] = N;
120 }
121}
122
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000123void CompilationGraph::insertEdge(const std::string& A, Edge* Edg) {
124 Node& B = getNode(Edg->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000125 if (A == "root") {
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +0000126 const char** InLangs = B.ToolPtr->InputLanguages();
127 for (;*InLangs; ++InLangs)
128 ToolsMap[*InLangs].push_back(IntrusiveRefCntPtr<Edge>(Edg));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000129 NodesMap["root"].AddEdge(Edg);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000130 }
131 else {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000132 Node& N = getNode(A);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000133 N.AddEdge(Edg);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000134 }
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000135 // Increase the inward edge counter.
136 B.IncrInEdges();
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000137}
138
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000139namespace {
140 sys::Path MakeTempFile(const sys::Path& TempDir, const std::string& BaseName,
141 const std::string& Suffix) {
142 sys::Path Out = TempDir;
143 Out.appendComponent(BaseName);
144 Out.appendSuffix(Suffix);
145 Out.makeUnique(true, NULL);
146 return Out;
147 }
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000148}
149
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000150// Pass input file through the chain until we bump into a Join node or
151// a node that says that it is the last.
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000152void CompilationGraph::PassThroughGraph (const sys::Path& InFile,
153 const Node* StartNode,
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000154 const InputLanguagesSet& InLangs,
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000155 const sys::Path& TempDir) const {
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000156 bool Last = false;
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000157 sys::Path In = InFile;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000158 const Node* CurNode = StartNode;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000159
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000160 while(!Last) {
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000161 sys::Path Out;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000162 Tool* CurTool = CurNode->ToolPtr.getPtr();
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000163
164 if (CurTool->IsJoin()) {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000165 JoinTool& JT = dynamic_cast<JoinTool&>(*CurTool);
166 JT.AddToJoinList(In);
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000167 break;
168 }
169
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000170 // Since toolchains do not have to end with a Join node, we should
171 // check if this Node is the last.
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000172 if (!CurNode->HasChildren() || CurTool->IsLast()) {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000173 if (!OutputFilename.empty()) {
174 Out.set(OutputFilename);
175 }
176 else {
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000177 Out.set(In.getBasename());
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000178 Out.appendSuffix(CurTool->OutputSuffix());
179 }
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000180 Last = true;
181 }
182 else {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000183 Out = MakeTempFile(TempDir, In.getBasename(), CurTool->OutputSuffix());
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000184 }
185
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +0000186 if (int ret = CurTool->GenerateAction(In, Out, InLangs).Execute())
Mikhail Glushenkova6730372008-05-12 16:31:42 +0000187 throw error_code(ret);
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000188
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000189 if (Last)
190 return;
191
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000192 CurNode = &getNode(ChooseEdge(CurNode->OutEdges,
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000193 InLangs,
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000194 CurNode->Name())->ToolName());
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000195 In = Out; Out.clear();
196 }
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000197}
198
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000199// Find the head of the toolchain corresponding to the given file.
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000200// Also, insert an input language into InLangs.
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000201const Node* CompilationGraph::
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000202FindToolChain(const sys::Path& In, const std::string* forceLanguage,
203 InputLanguagesSet& InLangs) const {
204
205 // Determine the input language.
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000206 const std::string& InLanguage =
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +0000207 forceLanguage ? *forceLanguage : GetLanguage(In);
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000208
209 // Add the current input language to the input language set.
210 InLangs.insert(InLanguage);
211
212 // Find the toolchain for the input language.
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000213 const tools_vector_type& TV = getToolsVector(InLanguage);
214 if (TV.empty())
Mikhail Glushenkova6730372008-05-12 16:31:42 +0000215 throw std::runtime_error("No toolchain corresponding to language "
216 + InLanguage + " found");
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000217 return &getNode(ChooseEdge(TV, InLangs)->ToolName());
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000218}
219
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000220// Helper function used by Build().
221// Traverses initial portions of the toolchains (up to the first Join node).
222// This function is also responsible for handling the -x option.
223void CompilationGraph::BuildInitial (InputLanguagesSet& InLangs,
224 const sys::Path& TempDir) {
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000225 // This is related to -x option handling.
226 cl::list<std::string>::const_iterator xIter = Languages.begin(),
227 xBegin = xIter, xEnd = Languages.end();
228 bool xEmpty = true;
229 const std::string* xLanguage = 0;
230 unsigned xPos = 0, xPosNext = 0, filePos = 0;
231
232 if (xIter != xEnd) {
233 xEmpty = false;
234 xPos = Languages.getPosition(xIter - xBegin);
235 cl::list<std::string>::const_iterator xNext = llvm::next(xIter);
236 xPosNext = (xNext == xEnd) ? std::numeric_limits<unsigned>::max()
237 : Languages.getPosition(xNext - xBegin);
238 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
239 }
240
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000241 // For each input file:
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000242 for (cl::list<std::string>::const_iterator B = InputFilenames.begin(),
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000243 CB = B, E = InputFilenames.end(); B != E; ++B) {
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000244 sys::Path In = sys::Path(*B);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000245
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000246 // Code for handling the -x option.
247 // Output: std::string* xLanguage (can be NULL).
248 if (!xEmpty) {
249 filePos = InputFilenames.getPosition(B - CB);
250
251 if (xPos < filePos) {
252 if (filePos < xPosNext) {
253 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
254 }
255 else { // filePos >= xPosNext
256 // Skip xIters while filePos > xPosNext
257 while (filePos > xPosNext) {
258 ++xIter;
259 xPos = xPosNext;
260
261 cl::list<std::string>::const_iterator xNext = llvm::next(xIter);
262 if (xNext == xEnd)
263 xPosNext = std::numeric_limits<unsigned>::max();
264 else
265 xPosNext = Languages.getPosition(xNext - xBegin);
266 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
267 }
268 }
269 }
270 }
271
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000272 // Find the toolchain corresponding to this file.
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000273 const Node* N = FindToolChain(In, xLanguage, InLangs);
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000274 // Pass file through the chain starting at head.
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000275 PassThroughGraph(In, N, InLangs, TempDir);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000276 }
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000277}
278
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000279// Sort the nodes in topological order.
280void CompilationGraph::TopologicalSort(std::vector<const Node*>& Out) {
281 std::queue<const Node*> Q;
282 Q.push(&getNode("root"));
283
284 while (!Q.empty()) {
285 const Node* A = Q.front();
286 Q.pop();
287 Out.push_back(A);
288 for (Node::const_iterator EB = A->EdgesBegin(), EE = A->EdgesEnd();
289 EB != EE; ++EB) {
290 Node* B = &getNode((*EB)->ToolName());
291 B->DecrInEdges();
292 if (B->HasNoInEdges())
293 Q.push(B);
294 }
295 }
296}
297
298namespace {
299 bool NotJoinNode(const Node* N) {
300 return N->ToolPtr ? !N->ToolPtr->IsJoin() : true;
301 }
302}
303
304// Call TopologicalSort and filter the resulting list to include
305// only Join nodes.
306void CompilationGraph::
307TopologicalSortFilterJoinNodes(std::vector<const Node*>& Out) {
308 std::vector<const Node*> TopSorted;
309 TopologicalSort(TopSorted);
310 std::remove_copy_if(TopSorted.begin(), TopSorted.end(),
311 std::back_inserter(Out), NotJoinNode);
312}
313
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000314int CompilationGraph::Build (const sys::Path& TempDir) {
315
316 InputLanguagesSet InLangs;
317
318 // Traverse initial parts of the toolchains and fill in InLangs.
319 BuildInitial(InLangs, TempDir);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000320
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000321 std::vector<const Node*> JTV;
322 TopologicalSortFilterJoinNodes(JTV);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000323
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000324 // For all join nodes in topological order:
325 for (std::vector<const Node*>::iterator B = JTV.begin(), E = JTV.end();
326 B != E; ++B) {
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000327
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000328 sys::Path Out;
329 const Node* CurNode = *B;
330 JoinTool* JT = &dynamic_cast<JoinTool&>(*CurNode->ToolPtr.getPtr());
331 bool IsLast = false;
332
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000333 // Are there any files in the join list?
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000334 if (JT->JoinListEmpty())
335 continue;
336
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000337 // Is this the last tool in the toolchain?
338 // NOTE: we can process several toolchains in parallel.
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000339 if (!CurNode->HasChildren() || JT->IsLast()) {
340 if (OutputFilename.empty()) {
341 Out.set("a");
342 Out.appendSuffix(JT->OutputSuffix());
343 }
344 else
345 Out.set(OutputFilename);
346 IsLast = true;
347 }
348 else {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000349 Out = MakeTempFile(TempDir, "tmp", JT->OutputSuffix());
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000350 }
351
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +0000352 if (int ret = JT->GenerateAction(Out, InLangs).Execute())
Mikhail Glushenkova6730372008-05-12 16:31:42 +0000353 throw error_code(ret);
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000354
355 if (!IsLast) {
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000356 const Node* NextNode =
357 &getNode(ChooseEdge(CurNode->OutEdges, InLangs,
358 CurNode->Name())->ToolName());
359 PassThroughGraph(Out, NextNode, InLangs, TempDir);
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000360 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000361 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000362
363 return 0;
364}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000365
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000366// Code related to graph visualization.
367
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000368namespace llvm {
369 template <>
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000370 struct DOTGraphTraits<llvmc::CompilationGraph*>
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000371 : public DefaultDOTGraphTraits
372 {
373
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000374 template<typename GraphType>
375 static std::string getNodeLabel(const Node* N, const GraphType&)
376 {
377 if (N->ToolPtr)
378 if (N->ToolPtr->IsJoin())
379 return N->Name() + "\n (join" +
380 (N->HasChildren() ? ")"
381 : std::string(": ") + N->ToolPtr->OutputLanguage() + ')');
382 else
383 return N->Name();
384 else
385 return "root";
386 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000387
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000388 template<typename EdgeIter>
389 static std::string getEdgeSourceLabel(const Node* N, EdgeIter I) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000390 if (N->ToolPtr) {
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000391 return N->ToolPtr->OutputLanguage();
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000392 }
393 else {
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +0000394 const char** InLangs = I->ToolPtr->InputLanguages();
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000395 std::string ret;
396
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +0000397 for (; *InLangs; ++InLangs) {
398 if (*(InLangs + 1)) {
399 ret += *InLangs;
400 ret += ", ";
401 }
402 else {
403 ret += *InLangs;
404 }
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000405 }
406
407 return ret;
408 }
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000409 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000410 };
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000411
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000412}
413
414void CompilationGraph::writeGraph() {
Mikhail Glushenkov35bca412008-05-30 06:16:59 +0000415 std::ofstream O("compilation-graph.dot");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000416
Mikhail Glushenkova4db8c02008-05-06 16:37:33 +0000417 if (O.good()) {
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000418 llvm::WriteGraph(this, "compilation-graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000419 O.close();
420 }
421 else {
Mikhail Glushenkov35bca412008-05-30 06:16:59 +0000422 throw std::runtime_error("Error opening file 'compilation-graph.dot'"
423 " for writing!");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000424 }
425}
426
427void CompilationGraph::viewGraph() {
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000428 llvm::ViewGraph(this, "compilation-graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000429}