blob: 92f3104c018a894605d63fa5289227d6d42487f9 [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 Glushenkov4f6e3a42008-05-06 17:28:03 +000035namespace {
36
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000037 /// ChooseEdge - Return the edge with the maximum weight.
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000038 template <class C>
39 const Edge* ChooseEdge(const C& EdgesContainer,
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +000040 const InputLanguagesSet& InLangs,
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000041 const std::string& NodeName = "root") {
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000042 const Edge* MaxEdge = 0;
43 unsigned MaxWeight = 0;
44 bool SingleMax = true;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000045
46 for (typename C::const_iterator B = EdgesContainer.begin(),
47 E = EdgesContainer.end(); B != E; ++B) {
48 const Edge* E = B->getPtr();
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +000049 unsigned EW = E->Weight(InLangs);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000050 if (EW > MaxWeight) {
51 MaxEdge = E;
52 MaxWeight = EW;
53 SingleMax = true;
Mikhail Glushenkove0ff9ae2008-05-06 18:18:58 +000054 } else if (EW == MaxWeight) {
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000055 SingleMax = false;
56 }
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000057 }
58
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000059 if (!SingleMax)
60 throw std::runtime_error("Node " + NodeName +
61 ": multiple maximal outward edges found!"
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000062 " Most probably a specification error.");
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000063 if (!MaxEdge)
64 throw std::runtime_error("Node " + NodeName +
65 ": no maximal outward edge found!"
66 " Most probably a specification error.");
67 return MaxEdge;
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000068 }
69
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000070}
71
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000072CompilationGraph::CompilationGraph() {
73 NodesMap["root"] = Node(this);
74}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000075
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000076Node& CompilationGraph::getNode(const std::string& ToolName) {
77 nodes_map_type::iterator I = NodesMap.find(ToolName);
78 if (I == NodesMap.end())
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000079 throw std::runtime_error("Node " + ToolName + " is not in the graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000080 return I->second;
81}
82
83const Node& CompilationGraph::getNode(const std::string& ToolName) const {
84 nodes_map_type::const_iterator I = NodesMap.find(ToolName);
85 if (I == NodesMap.end())
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000086 throw std::runtime_error("Node " + ToolName + " is not in the graph!");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000087 return I->second;
88}
89
Mikhail Glushenkovbe867122008-05-06 18:16:52 +000090// Find the language name corresponding to the given file.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000091const std::string& CompilationGraph::getLanguage(const sys::Path& File) const {
92 LanguageMap::const_iterator Lang = ExtsToLangs.find(File.getSuffix());
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000093 if (Lang == ExtsToLangs.end())
Mikhail Glushenkova6730372008-05-12 16:31:42 +000094 throw std::runtime_error("Unknown suffix: " + File.getSuffix());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000095 return Lang->second;
96}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000097
Mikhail Glushenkovbe867122008-05-06 18:16:52 +000098// Find the tools list corresponding to the given language name.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000099const CompilationGraph::tools_vector_type&
100CompilationGraph::getToolsVector(const std::string& LangName) const
101{
102 tools_map_type::const_iterator I = ToolsMap.find(LangName);
103 if (I == ToolsMap.end())
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000104 throw std::runtime_error("No tool corresponding to the language "
Mikhail Glushenkov35bca412008-05-30 06:16:59 +0000105 + LangName + " found");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000106 return I->second;
107}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000108
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000109void CompilationGraph::insertNode(Tool* V) {
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000110 if (NodesMap.count(V->Name()) == 0) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000111 Node N;
112 N.OwningGraph = this;
113 N.ToolPtr = V;
114 NodesMap[V->Name()] = N;
115 }
116}
117
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000118void CompilationGraph::insertEdge(const std::string& A, Edge* Edg) {
119 Node& B = getNode(Edg->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000120 if (A == "root") {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000121 const StrVector& InputLanguages = B.ToolPtr->InputLanguages();
122 for (StrVector::const_iterator B = InputLanguages.begin(),
123 E = InputLanguages.end(); B != E; ++B)
124 ToolsMap[*B].push_back(IntrusiveRefCntPtr<Edge>(Edg));
125 NodesMap["root"].AddEdge(Edg);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000126 }
127 else {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000128 Node& N = getNode(A);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000129 N.AddEdge(Edg);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000130 }
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000131 // Increase the inward edge counter.
132 B.IncrInEdges();
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000133}
134
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000135namespace {
136 sys::Path MakeTempFile(const sys::Path& TempDir, const std::string& BaseName,
137 const std::string& Suffix) {
138 sys::Path Out = TempDir;
139 Out.appendComponent(BaseName);
140 Out.appendSuffix(Suffix);
141 Out.makeUnique(true, NULL);
142 return Out;
143 }
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000144}
145
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000146// Pass input file through the chain until we bump into a Join node or
147// a node that says that it is the last.
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000148void CompilationGraph::PassThroughGraph (const sys::Path& InFile,
149 const Node* StartNode,
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000150 const InputLanguagesSet& InLangs,
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000151 const sys::Path& TempDir) const {
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000152 bool Last = false;
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000153 sys::Path In = InFile;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000154 const Node* CurNode = StartNode;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000155
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000156 while(!Last) {
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000157 sys::Path Out;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000158 Tool* CurTool = CurNode->ToolPtr.getPtr();
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000159
160 if (CurTool->IsJoin()) {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000161 JoinTool& JT = dynamic_cast<JoinTool&>(*CurTool);
162 JT.AddToJoinList(In);
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000163 break;
164 }
165
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000166 // Since toolchains do not have to end with a Join node, we should
167 // check if this Node is the last.
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000168 if (!CurNode->HasChildren() || CurTool->IsLast()) {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000169 if (!OutputFilename.empty()) {
170 Out.set(OutputFilename);
171 }
172 else {
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000173 Out.set(In.getBasename());
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000174 Out.appendSuffix(CurTool->OutputSuffix());
175 }
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000176 Last = true;
177 }
178 else {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000179 Out = MakeTempFile(TempDir, In.getBasename(), CurTool->OutputSuffix());
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000180 }
181
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +0000182 if (int ret = CurTool->GenerateAction(In, Out, InLangs).Execute())
Mikhail Glushenkova6730372008-05-12 16:31:42 +0000183 throw error_code(ret);
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000184
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000185 if (Last)
186 return;
187
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000188 CurNode = &getNode(ChooseEdge(CurNode->OutEdges,
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000189 InLangs,
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000190 CurNode->Name())->ToolName());
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000191 In = Out; Out.clear();
192 }
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000193}
194
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000195// Find the head of the toolchain corresponding to the given file.
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000196// Also, insert an input language into InLangs.
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000197const Node* CompilationGraph::
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000198FindToolChain(const sys::Path& In, const std::string* forceLanguage,
199 InputLanguagesSet& InLangs) const {
200
201 // Determine the input language.
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000202 const std::string& InLanguage =
203 forceLanguage ? *forceLanguage : getLanguage(In);
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000204
205 // Add the current input language to the input language set.
206 InLangs.insert(InLanguage);
207
208 // Find the toolchain for the input language.
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000209 const tools_vector_type& TV = getToolsVector(InLanguage);
210 if (TV.empty())
Mikhail Glushenkova6730372008-05-12 16:31:42 +0000211 throw std::runtime_error("No toolchain corresponding to language "
212 + InLanguage + " found");
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000213 return &getNode(ChooseEdge(TV, InLangs)->ToolName());
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000214}
215
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000216// Helper function used by Build().
217// Traverses initial portions of the toolchains (up to the first Join node).
218// This function is also responsible for handling the -x option.
219void CompilationGraph::BuildInitial (InputLanguagesSet& InLangs,
220 const sys::Path& TempDir) {
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000221 // This is related to -x option handling.
222 cl::list<std::string>::const_iterator xIter = Languages.begin(),
223 xBegin = xIter, xEnd = Languages.end();
224 bool xEmpty = true;
225 const std::string* xLanguage = 0;
226 unsigned xPos = 0, xPosNext = 0, filePos = 0;
227
228 if (xIter != xEnd) {
229 xEmpty = false;
230 xPos = Languages.getPosition(xIter - xBegin);
231 cl::list<std::string>::const_iterator xNext = llvm::next(xIter);
232 xPosNext = (xNext == xEnd) ? std::numeric_limits<unsigned>::max()
233 : Languages.getPosition(xNext - xBegin);
234 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
235 }
236
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000237 // For each input file:
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000238 for (cl::list<std::string>::const_iterator B = InputFilenames.begin(),
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000239 CB = B, E = InputFilenames.end(); B != E; ++B) {
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000240 sys::Path In = sys::Path(*B);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000241
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000242 // Code for handling the -x option.
243 // Output: std::string* xLanguage (can be NULL).
244 if (!xEmpty) {
245 filePos = InputFilenames.getPosition(B - CB);
246
247 if (xPos < filePos) {
248 if (filePos < xPosNext) {
249 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
250 }
251 else { // filePos >= xPosNext
252 // Skip xIters while filePos > xPosNext
253 while (filePos > xPosNext) {
254 ++xIter;
255 xPos = xPosNext;
256
257 cl::list<std::string>::const_iterator xNext = llvm::next(xIter);
258 if (xNext == xEnd)
259 xPosNext = std::numeric_limits<unsigned>::max();
260 else
261 xPosNext = Languages.getPosition(xNext - xBegin);
262 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
263 }
264 }
265 }
266 }
267
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000268 // Find the toolchain corresponding to this file.
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000269 const Node* N = FindToolChain(In, xLanguage, InLangs);
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000270 // Pass file through the chain starting at head.
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000271 PassThroughGraph(In, N, InLangs, TempDir);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000272 }
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000273}
274
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000275// Sort the nodes in topological order.
276void CompilationGraph::TopologicalSort(std::vector<const Node*>& Out) {
277 std::queue<const Node*> Q;
278 Q.push(&getNode("root"));
279
280 while (!Q.empty()) {
281 const Node* A = Q.front();
282 Q.pop();
283 Out.push_back(A);
284 for (Node::const_iterator EB = A->EdgesBegin(), EE = A->EdgesEnd();
285 EB != EE; ++EB) {
286 Node* B = &getNode((*EB)->ToolName());
287 B->DecrInEdges();
288 if (B->HasNoInEdges())
289 Q.push(B);
290 }
291 }
292}
293
294namespace {
295 bool NotJoinNode(const Node* N) {
296 return N->ToolPtr ? !N->ToolPtr->IsJoin() : true;
297 }
298}
299
300// Call TopologicalSort and filter the resulting list to include
301// only Join nodes.
302void CompilationGraph::
303TopologicalSortFilterJoinNodes(std::vector<const Node*>& Out) {
304 std::vector<const Node*> TopSorted;
305 TopologicalSort(TopSorted);
306 std::remove_copy_if(TopSorted.begin(), TopSorted.end(),
307 std::back_inserter(Out), NotJoinNode);
308}
309
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000310int CompilationGraph::Build (const sys::Path& TempDir) {
311
312 InputLanguagesSet InLangs;
313
314 // Traverse initial parts of the toolchains and fill in InLangs.
315 BuildInitial(InLangs, TempDir);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000316
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000317 std::vector<const Node*> JTV;
318 TopologicalSortFilterJoinNodes(JTV);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000319
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000320 // For all join nodes in topological order:
321 for (std::vector<const Node*>::iterator B = JTV.begin(), E = JTV.end();
322 B != E; ++B) {
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000323
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000324 sys::Path Out;
325 const Node* CurNode = *B;
326 JoinTool* JT = &dynamic_cast<JoinTool&>(*CurNode->ToolPtr.getPtr());
327 bool IsLast = false;
328
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000329 // Are there any files in the join list?
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000330 if (JT->JoinListEmpty())
331 continue;
332
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000333 // Is this the last tool in the toolchain?
334 // NOTE: we can process several toolchains in parallel.
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000335 if (!CurNode->HasChildren() || JT->IsLast()) {
336 if (OutputFilename.empty()) {
337 Out.set("a");
338 Out.appendSuffix(JT->OutputSuffix());
339 }
340 else
341 Out.set(OutputFilename);
342 IsLast = true;
343 }
344 else {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000345 Out = MakeTempFile(TempDir, "tmp", JT->OutputSuffix());
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000346 }
347
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +0000348 if (int ret = JT->GenerateAction(Out, InLangs).Execute())
Mikhail Glushenkova6730372008-05-12 16:31:42 +0000349 throw error_code(ret);
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000350
351 if (!IsLast) {
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000352 const Node* NextNode =
353 &getNode(ChooseEdge(CurNode->OutEdges, InLangs,
354 CurNode->Name())->ToolName());
355 PassThroughGraph(Out, NextNode, InLangs, TempDir);
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000356 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000357 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000358
359 return 0;
360}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000361
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000362// Code related to graph visualization.
363
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000364namespace llvm {
365 template <>
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000366 struct DOTGraphTraits<llvmc::CompilationGraph*>
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000367 : public DefaultDOTGraphTraits
368 {
369
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000370 template<typename GraphType>
371 static std::string getNodeLabel(const Node* N, const GraphType&)
372 {
373 if (N->ToolPtr)
374 if (N->ToolPtr->IsJoin())
375 return N->Name() + "\n (join" +
376 (N->HasChildren() ? ")"
377 : std::string(": ") + N->ToolPtr->OutputLanguage() + ')');
378 else
379 return N->Name();
380 else
381 return "root";
382 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000383
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000384 template<typename EdgeIter>
385 static std::string getEdgeSourceLabel(const Node* N, EdgeIter I) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000386 if (N->ToolPtr) {
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000387 return N->ToolPtr->OutputLanguage();
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000388 }
389 else {
390 const StrVector& InputLanguages = I->ToolPtr->InputLanguages();
391 std::string ret;
392
393 for (StrVector::const_iterator B = InputLanguages.begin(),
394 E = InputLanguages.end(); B != E; ++B) {
395 if (llvm::next(B) != E)
396 ret += *B + ", ";
397 else
398 ret += *B;
399 }
400
401 return ret;
402 }
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000403 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000404 };
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000405
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000406}
407
408void CompilationGraph::writeGraph() {
Mikhail Glushenkov35bca412008-05-30 06:16:59 +0000409 std::ofstream O("compilation-graph.dot");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000410
Mikhail Glushenkova4db8c02008-05-06 16:37:33 +0000411 if (O.good()) {
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000412 llvm::WriteGraph(this, "compilation-graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000413 O.close();
414 }
415 else {
Mikhail Glushenkov35bca412008-05-30 06:16:59 +0000416 throw std::runtime_error("Error opening file 'compilation-graph.dot'"
417 " for writing!");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000418 }
419}
420
421void CompilationGraph::viewGraph() {
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000422 llvm::ViewGraph(this, "compilation-graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000423}