+template<typename GraphType>
+sys::Path WriteGraph(const GraphType &G, const std::string &Name,
+ bool ShortNames = false, const std::string &Title = "") {
+ std::string ErrMsg;
+ sys::Path Filename = sys::Path::GetTemporaryDirectory(&ErrMsg);
+ if (Filename.isEmpty()) {
+ errs() << "Error: " << ErrMsg << "\n";
+ return Filename;
+ }
+ Filename.appendComponent(Name + ".dot");
+ if (Filename.makeUnique(true,&ErrMsg)) {
+ errs() << "Error: " << ErrMsg << "\n";
+ return sys::Path();
+ }
+
+ errs() << "Writing '" << Filename.str() << "'... ";
+
+ std::string ErrorInfo;
+ raw_fd_ostream O(Filename.c_str(), ErrorInfo);
+
+ if (ErrorInfo.empty()) {
+ WriteGraph(O, G, ShortNames, Name, Title);
+ errs() << " done. \n";
+ } else {
+ errs() << "error opening file '" << Filename.str() << "' 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,
+ bool ShortNames = false, const std::string &Title = "",
+ GraphProgram::Name Program = GraphProgram::DOT) {
+ sys::Path Filename = WriteGraph(G, Name, ShortNames, Title);
+
+ if (Filename.isEmpty())
+ return;
+
+ DisplayGraph(Filename, true, Program);
+}
+
+} // End llvm namespace
+