#ifndef LLVM_SUPPORT_GRAPHWRITER_H
#define LLVM_SUPPORT_GRAPHWRITER_H
+#include "llvm/Support/Debug.h"
#include "llvm/Support/DOTGraphTraits.h"
#include "llvm/ADT/GraphTraits.h"
+#include "llvm/System/Path.h"
+#include <fstream>
#include <vector>
-#include <iostream>
namespace llvm {
}
}
+void DisplayGraph(const sys::Path& Filename);
+
template<typename GraphType>
class GraphWriter {
std::ostream &O;
}
void writeNode(NodeType *Node) {
- std::string NodeAttributes = DOTTraits::getNodeAttributes(Node);
+ std::string NodeAttributes = DOTTraits::getNodeAttributes(Node, G);
O << "\tNode" << reinterpret_cast<const void*>(Node) << " [shape=record,";
if (!NodeAttributes.empty()) O << NodeAttributes << ",";
return O;
}
+template<typename GraphType>
+sys::Path WriteGraph(const GraphType &G,
+ const std::string& Name,
+ const std::string& Title = "") {
+ std::string ErrMsg;
+ sys::Path Filename = sys::Path::GetTemporaryDirectory(&ErrMsg);
+ if (Filename.isEmpty()) {
+ cerr << "Error: " << ErrMsg << "\n";
+ return Filename;
+ }
+ Filename.appendComponent(Name + ".dot");
+ if (Filename.makeUnique(true,&ErrMsg)) {
+ cerr << "Error: " << ErrMsg << "\n";
+ return sys::Path();
+ }
+
+ cerr << "Writing '" << Filename << "'... ";
+
+ std::ofstream O(Filename.c_str());
+
+ if (O.good()) {
+ // Start the graph emission process...
+ GraphWriter<GraphType> W(O, G);
+
+ // Output the header for the graph...
+ W.writeHeader(Title);
+
+ // Emit all of the nodes in the graph...
+ W.writeNodes();
+
+ // Output any customizations on the graph
+ DOTGraphTraits<GraphType>::addCustomGraphFeatures(G, W);
+
+ // Output the end of the graph
+ W.writeFooter();
+ cerr << " done. \n";
+
+ O.close();
+
+ } else {
+ cerr << "error opening file for writing!\n";
+ Filename.clear();
+ }
+
+ return Filename;
+}
+
+/// ViewGraph - Emit a dot graph, run 'dot', run gv on the postscript file,
+/// then cleanup. For use from the debugger.
+///
+template<typename GraphType>
+void ViewGraph(const GraphType& G,
+ const std::string& Name,
+ const std::string& Title = "") {
+ sys::Path Filename = WriteGraph(G, Name, Title);
+
+ if (Filename.isEmpty()) {
+ return;
+ }
+
+ DisplayGraph(Filename);
+}
+
} // End llvm namespace
#endif