LLVM API Documentation

GraphWriter.cpp
Go to the documentation of this file.
00001 //===-- GraphWriter.cpp - Implements GraphWriter support routines ---------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements misc. GraphWriter support routines.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Support/GraphWriter.h"
00015 #include "llvm/Config/config.h"
00016 #include "llvm/Support/CommandLine.h"
00017 #include "llvm/Support/FileSystem.h"
00018 #include "llvm/Support/Path.h"
00019 #include "llvm/Support/Program.h"
00020 using namespace llvm;
00021 
00022 static cl::opt<bool> ViewBackground("view-background", cl::Hidden,
00023   cl::desc("Execute graph viewer in the background. Creates tmp file litter."));
00024 
00025 std::string llvm::DOT::EscapeString(const std::string &Label) {
00026   std::string Str(Label);
00027   for (unsigned i = 0; i != Str.length(); ++i)
00028   switch (Str[i]) {
00029     case '\n':
00030       Str.insert(Str.begin()+i, '\\');  // Escape character...
00031       ++i;
00032       Str[i] = 'n';
00033       break;
00034     case '\t':
00035       Str.insert(Str.begin()+i, ' ');  // Convert to two spaces
00036       ++i;
00037       Str[i] = ' ';
00038       break;
00039     case '\\':
00040       if (i+1 != Str.length())
00041         switch (Str[i+1]) {
00042           case 'l': continue; // don't disturb \l
00043           case '|': case '{': case '}':
00044             Str.erase(Str.begin()+i); continue;
00045           default: break;
00046         }
00047     case '{': case '}':
00048     case '<': case '>':
00049     case '|': case '"':
00050       Str.insert(Str.begin()+i, '\\');  // Escape character...
00051       ++i;  // don't infinite loop
00052       break;
00053   }
00054   return Str;
00055 }
00056 
00057 /// \brief Get a color string for this node number. Simply round-robin selects
00058 /// from a reasonable number of colors.
00059 StringRef llvm::DOT::getColorString(unsigned ColorNumber) {
00060   static const int NumColors = 20;
00061   static const char* Colors[NumColors] = {
00062     "aaaaaa", "aa0000", "00aa00", "aa5500", "0055ff", "aa00aa", "00aaaa",
00063     "555555", "ff5555", "55ff55", "ffff55", "5555ff", "ff55ff", "55ffff",
00064     "ffaaaa", "aaffaa", "ffffaa", "aaaaff", "ffaaff", "aaffff"};
00065   return Colors[ColorNumber % NumColors];
00066 }
00067 
00068 std::string llvm::createGraphFilename(const Twine &Name, int &FD) {
00069   FD = -1;
00070   SmallString<128> Filename;
00071   std::error_code EC = sys::fs::createTemporaryFile(Name, "dot", FD, Filename);
00072   if (EC) {
00073     errs() << "Error: " << EC.message() << "\n";
00074     return "";
00075   }
00076 
00077   errs() << "Writing '" << Filename << "'... ";
00078   return Filename.str();
00079 }
00080 
00081 // Execute the graph viewer. Return true if there were errors.
00082 static bool ExecGraphViewer(StringRef ExecPath, std::vector<const char *> &args,
00083                             StringRef Filename, bool wait,
00084                             std::string &ErrMsg) {
00085   assert(args.back() == nullptr);
00086   if (wait) {
00087     if (sys::ExecuteAndWait(ExecPath, args.data(), nullptr, nullptr, 0, 0,
00088                             &ErrMsg)) {
00089       errs() << "Error: " << ErrMsg << "\n";
00090       return true;
00091     }
00092     sys::fs::remove(Filename);
00093     errs() << " done. \n";
00094   } else {
00095     sys::ExecuteNoWait(ExecPath, args.data(), nullptr, nullptr, 0, &ErrMsg);
00096     errs() << "Remember to erase graph file: " << Filename.str() << "\n";
00097   }
00098   return false;
00099 }
00100 
00101 struct GraphSession {
00102   std::string LogBuffer;
00103   bool TryFindProgram(StringRef Names, std::string &ProgramPath) {
00104     raw_string_ostream Log(LogBuffer);
00105     SmallVector<StringRef, 8> parts;
00106     Names.split(parts, "|");
00107     for (auto Name : parts) {
00108       ProgramPath = sys::FindProgramByName(Name);
00109       if (!ProgramPath.empty())
00110         return true;
00111       Log << "  Tried '" << Name << "'\n";
00112     }
00113     return false;
00114   }
00115 };
00116 
00117 static const char *getProgramName(GraphProgram::Name program) {
00118   switch (program) {
00119   case GraphProgram::DOT:
00120     return "dot";
00121   case GraphProgram::FDP:
00122     return "fdp";
00123   case GraphProgram::NEATO:
00124     return "neato";
00125   case GraphProgram::TWOPI:
00126     return "twopi";
00127   case GraphProgram::CIRCO:
00128     return "circo";
00129   }
00130   llvm_unreachable("bad kind");
00131 }
00132 
00133 bool llvm::DisplayGraph(StringRef FilenameRef, bool wait,
00134                         GraphProgram::Name program) {
00135   std::string Filename = FilenameRef;
00136   wait &= !ViewBackground;
00137   std::string ErrMsg;
00138   std::string ViewerPath;
00139   GraphSession S;
00140 
00141   // Graphviz
00142   if (S.TryFindProgram("Graphviz", ViewerPath)) {
00143     std::vector<const char *> args;
00144     args.push_back(ViewerPath.c_str());
00145     args.push_back(Filename.c_str());
00146     args.push_back(nullptr);
00147 
00148     errs() << "Running 'Graphviz' program... ";
00149     return ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg);
00150   }
00151 
00152   // xdot
00153   if (S.TryFindProgram("xdot|xdot.py", ViewerPath)) {
00154     std::vector<const char *> args;
00155     args.push_back(ViewerPath.c_str());
00156     args.push_back(Filename.c_str());
00157 
00158     args.push_back("-f");
00159     args.push_back(getProgramName(program));
00160 
00161     args.push_back(nullptr);
00162 
00163     errs() << "Running 'xdot.py' program... ";
00164     return ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg);
00165   }
00166 
00167   enum PSViewerKind { PSV_None, PSV_OSXOpen, PSV_XDGOpen, PSV_Ghostview };
00168   PSViewerKind PSViewer = PSV_None;
00169 #ifdef __APPLE__
00170   if (!PSViewer && S.TryFindProgram("open", ViewerPath))
00171     PSViewer = PSV_OSXOpen;
00172 #endif
00173   if (!PSViewer && S.TryFindProgram("gv", ViewerPath))
00174     PSViewer = PSV_Ghostview;
00175   if (!PSViewer && S.TryFindProgram("xdg-open", ViewerPath))
00176     PSViewer = PSV_XDGOpen;
00177 
00178   // PostScript graph generator + PostScript viewer
00179   std::string GeneratorPath;
00180   if (PSViewer &&
00181       (S.TryFindProgram(getProgramName(program), GeneratorPath) ||
00182        S.TryFindProgram("dot|fdp|neato|twopi|circo", GeneratorPath))) {
00183     std::string PSFilename = Filename + ".ps";
00184 
00185     std::vector<const char *> args;
00186     args.push_back(GeneratorPath.c_str());
00187     args.push_back("-Tps");
00188     args.push_back("-Nfontname=Courier");
00189     args.push_back("-Gsize=7.5,10");
00190     args.push_back(Filename.c_str());
00191     args.push_back("-o");
00192     args.push_back(PSFilename.c_str());
00193     args.push_back(nullptr);
00194 
00195     errs() << "Running '" << GeneratorPath << "' program... ";
00196 
00197     if (ExecGraphViewer(GeneratorPath, args, Filename, wait, ErrMsg))
00198       return true;
00199 
00200     args.clear();
00201     args.push_back(ViewerPath.c_str());
00202     switch (PSViewer) {
00203     case PSV_OSXOpen:
00204       args.push_back("-W");
00205       args.push_back(PSFilename.c_str());
00206       break;
00207     case PSV_XDGOpen:
00208       wait = false;
00209       args.push_back(PSFilename.c_str());
00210       break;
00211     case PSV_Ghostview:
00212       args.push_back("--spartan");
00213       args.push_back(PSFilename.c_str());
00214       break;
00215     case PSV_None:
00216       llvm_unreachable("Invalid viewer");
00217     }
00218     args.push_back(nullptr);
00219 
00220     ErrMsg.clear();
00221     return ExecGraphViewer(ViewerPath, args, PSFilename, wait, ErrMsg);
00222   }
00223 
00224   // dotty
00225   if (S.TryFindProgram("dotty", ViewerPath)) {
00226     std::vector<const char *> args;
00227     args.push_back(ViewerPath.c_str());
00228     args.push_back(Filename.c_str());
00229     args.push_back(nullptr);
00230 
00231 // Dotty spawns another app and doesn't wait until it returns
00232 #ifdef LLVM_ON_WIN32
00233     wait = false;
00234 #endif
00235     errs() << "Running 'dotty' program... ";
00236     return ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg);
00237   }
00238 
00239   errs() << "Error: Couldn't find a usable graph viewer program:\n";
00240   errs() << S.LogBuffer << "\n";
00241   return true;
00242 }