Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Support/GraphWriter.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- GraphWriter.cpp - Implements GraphWriter support routines ----------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file implements misc. GraphWriter support routines.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "llvm/Support/GraphWriter.h"
14
#include "llvm/ADT/SmallString.h"
15
#include "llvm/ADT/SmallVector.h"
16
#include "llvm/ADT/StringRef.h"
17
#include "llvm/Config/config.h"
18
#include "llvm/Support/CommandLine.h"
19
#include "llvm/Support/Compiler.h"
20
#include "llvm/Support/ErrorHandling.h"
21
#include "llvm/Support/ErrorOr.h"
22
#include "llvm/Support/FileSystem.h"
23
#include "llvm/Support/Program.h"
24
#include "llvm/Support/raw_ostream.h"
25
#include <cassert>
26
#include <system_error>
27
#include <string>
28
#include <vector>
29
30
using namespace llvm;
31
32
static cl::opt<bool> ViewBackground("view-background", cl::Hidden,
33
  cl::desc("Execute graph viewer in the background. Creates tmp file litter."));
34
35
26
std::string llvm::DOT::EscapeString(const std::string &Label) {
36
26
  std::string Str(Label);
37
1.61k
  for (unsigned i = 0; i != Str.length(); 
++i1.58k
)
38
1.58k
  switch (Str[i]) {
39
1.58k
    case '\n':
40
0
      Str.insert(Str.begin()+i, '\\');  // Escape character...
41
0
      ++i;
42
0
      Str[i] = 'n';
43
0
      break;
44
1.58k
    case '\t':
45
0
      Str.insert(Str.begin()+i, ' ');  // Convert to two spaces
46
0
      ++i;
47
0
      Str[i] = ' ';
48
0
      break;
49
1.58k
    case '\\':
50
36
      if (i+1 != Str.length())
51
36
        switch (Str[i+1]) {
52
36
          case 'l': continue; // don't disturb \l
53
36
          
case '|': 0
case '{': 0
case '}':
54
0
            Str.erase(Str.begin()+i); continue;
55
0
          default: break;
56
0
        }
57
0
        LLVM_FALLTHROUGH;
58
0
    case '{': case '}':
59
0
    case '<': case '>':
60
0
    case '|': case '"':
61
0
      Str.insert(Str.begin()+i, '\\');  // Escape character...
62
0
      ++i;  // don't infinite loop
63
0
      break;
64
1.58k
  }
65
26
  return Str;
66
26
}
67
68
/// Get a color string for this node number. Simply round-robin selects
69
/// from a reasonable number of colors.
70
0
StringRef llvm::DOT::getColorString(unsigned ColorNumber) {
71
0
  static const int NumColors = 20;
72
0
  static const char* Colors[NumColors] = {
73
0
    "aaaaaa", "aa0000", "00aa00", "aa5500", "0055ff", "aa00aa", "00aaaa",
74
0
    "555555", "ff5555", "55ff55", "ffff55", "5555ff", "ff55ff", "55ffff",
75
0
    "ffaaaa", "aaffaa", "ffffaa", "aaaaff", "ffaaff", "aaffff"};
76
0
  return Colors[ColorNumber % NumColors];
77
0
}
78
79
0
std::string llvm::createGraphFilename(const Twine &Name, int &FD) {
80
0
  FD = -1;
81
0
  SmallString<128> Filename;
82
0
  std::error_code EC = sys::fs::createTemporaryFile(Name, "dot", FD, Filename);
83
0
  if (EC) {
84
0
    errs() << "Error: " << EC.message() << "\n";
85
0
    return "";
86
0
  }
87
0
88
0
  errs() << "Writing '" << Filename << "'... ";
89
0
  return Filename.str();
90
0
}
91
92
// Execute the graph viewer. Return true if there were errors.
93
static bool ExecGraphViewer(StringRef ExecPath, std::vector<StringRef> &args,
94
                            StringRef Filename, bool wait,
95
0
                            std::string &ErrMsg) {
96
0
  if (wait) {
97
0
    if (sys::ExecuteAndWait(ExecPath, args, None, {}, 0, 0, &ErrMsg)) {
98
0
      errs() << "Error: " << ErrMsg << "\n";
99
0
      return true;
100
0
    }
101
0
    sys::fs::remove(Filename);
102
0
    errs() << " done. \n";
103
0
  } else {
104
0
    sys::ExecuteNoWait(ExecPath, args, None, {}, 0, &ErrMsg);
105
0
    errs() << "Remember to erase graph file: " << Filename << "\n";
106
0
  }
107
0
  return false;
108
0
}
109
110
namespace {
111
112
struct GraphSession {
113
  std::string LogBuffer;
114
115
0
  bool TryFindProgram(StringRef Names, std::string &ProgramPath) {
116
0
    raw_string_ostream Log(LogBuffer);
117
0
    SmallVector<StringRef, 8> parts;
118
0
    Names.split(parts, '|');
119
0
    for (auto Name : parts) {
120
0
      if (ErrorOr<std::string> P = sys::findProgramByName(Name)) {
121
0
        ProgramPath = *P;
122
0
        return true;
123
0
      }
124
0
      Log << "  Tried '" << Name << "'\n";
125
0
    }
126
0
    return false;
127
0
  }
128
};
129
130
} // end anonymous namespace
131
132
0
static const char *getProgramName(GraphProgram::Name program) {
133
0
  switch (program) {
134
0
  case GraphProgram::DOT:
135
0
    return "dot";
136
0
  case GraphProgram::FDP:
137
0
    return "fdp";
138
0
  case GraphProgram::NEATO:
139
0
    return "neato";
140
0
  case GraphProgram::TWOPI:
141
0
    return "twopi";
142
0
  case GraphProgram::CIRCO:
143
0
    return "circo";
144
0
  }
145
0
  llvm_unreachable("bad kind");
146
0
}
147
148
bool llvm::DisplayGraph(StringRef FilenameRef, bool wait,
149
0
                        GraphProgram::Name program) {
150
0
  std::string Filename = FilenameRef;
151
0
  std::string ErrMsg;
152
0
  std::string ViewerPath;
153
0
  GraphSession S;
154
0
155
0
#ifdef __APPLE__
156
0
  wait &= !ViewBackground;
157
0
  if (S.TryFindProgram("open", ViewerPath)) {
158
0
    std::vector<StringRef> args;
159
0
    args.push_back(ViewerPath);
160
0
    if (wait)
161
0
      args.push_back("-W");
162
0
    args.push_back(Filename);
163
0
    errs() << "Trying 'open' program... ";
164
0
    if (!ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg))
165
0
      return false;
166
0
  }
167
0
#endif
168
0
  if (S.TryFindProgram("xdg-open", ViewerPath)) {
169
0
    std::vector<StringRef> args;
170
0
    args.push_back(ViewerPath);
171
0
    args.push_back(Filename);
172
0
    errs() << "Trying 'xdg-open' program... ";
173
0
    if (!ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg))
174
0
      return false;
175
0
  }
176
0
177
0
  // Graphviz
178
0
  if (S.TryFindProgram("Graphviz", ViewerPath)) {
179
0
    std::vector<StringRef> args;
180
0
    args.push_back(ViewerPath);
181
0
    args.push_back(Filename);
182
0
183
0
    errs() << "Running 'Graphviz' program... ";
184
0
    return ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg);
185
0
  }
186
0
187
0
  // xdot
188
0
  if (S.TryFindProgram("xdot|xdot.py", ViewerPath)) {
189
0
    std::vector<StringRef> args;
190
0
    args.push_back(ViewerPath);
191
0
    args.push_back(Filename);
192
0
193
0
    args.push_back("-f");
194
0
    args.push_back(getProgramName(program));
195
0
196
0
    errs() << "Running 'xdot.py' program... ";
197
0
    return ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg);
198
0
  }
199
0
200
0
  enum ViewerKind {
201
0
    VK_None,
202
0
    VK_OSXOpen,
203
0
    VK_XDGOpen,
204
0
    VK_Ghostview,
205
0
    VK_CmdStart
206
0
  };
207
0
  ViewerKind Viewer = VK_None;
208
0
#ifdef __APPLE__
209
0
  if (!Viewer && S.TryFindProgram("open", ViewerPath))
210
0
    Viewer = VK_OSXOpen;
211
0
#endif
212
0
  if (!Viewer && S.TryFindProgram("gv", ViewerPath))
213
0
    Viewer = VK_Ghostview;
214
0
  if (!Viewer && S.TryFindProgram("xdg-open", ViewerPath))
215
0
    Viewer = VK_XDGOpen;
216
#ifdef _WIN32
217
  if (!Viewer && S.TryFindProgram("cmd", ViewerPath)) {
218
    Viewer = VK_CmdStart;
219
  }
220
#endif
221
222
0
  // PostScript or PDF graph generator + PostScript/PDF viewer
223
0
  std::string GeneratorPath;
224
0
  if (Viewer &&
225
0
      (S.TryFindProgram(getProgramName(program), GeneratorPath) ||
226
0
       S.TryFindProgram("dot|fdp|neato|twopi|circo", GeneratorPath))) {
227
0
    std::string OutputFilename =
228
0
        Filename + (Viewer == VK_CmdStart ? ".pdf" : ".ps");
229
0
230
0
    std::vector<StringRef> args;
231
0
    args.push_back(GeneratorPath);
232
0
    if (Viewer == VK_CmdStart)
233
0
      args.push_back("-Tpdf");
234
0
    else
235
0
      args.push_back("-Tps");
236
0
    args.push_back("-Nfontname=Courier");
237
0
    args.push_back("-Gsize=7.5,10");
238
0
    args.push_back(Filename);
239
0
    args.push_back("-o");
240
0
    args.push_back(OutputFilename);
241
0
242
0
    errs() << "Running '" << GeneratorPath << "' program... ";
243
0
244
0
    if (ExecGraphViewer(GeneratorPath, args, Filename, true, ErrMsg))
245
0
      return true;
246
0
247
0
    // The lifetime of StartArg must include the call of ExecGraphViewer
248
0
    // because the args are passed as vector of char*.
249
0
    std::string StartArg;
250
0
251
0
    args.clear();
252
0
    args.push_back(ViewerPath);
253
0
    switch (Viewer) {
254
0
    case VK_OSXOpen:
255
0
      args.push_back("-W");
256
0
      args.push_back(OutputFilename);
257
0
      break;
258
0
    case VK_XDGOpen:
259
0
      wait = false;
260
0
      args.push_back(OutputFilename);
261
0
      break;
262
0
    case VK_Ghostview:
263
0
      args.push_back("--spartan");
264
0
      args.push_back(OutputFilename);
265
0
      break;
266
0
    case VK_CmdStart:
267
0
      args.push_back("/S");
268
0
      args.push_back("/C");
269
0
      StartArg =
270
0
          (StringRef("start ") + (wait ? "/WAIT " : "") + OutputFilename).str();
271
0
      args.push_back(StartArg);
272
0
      break;
273
0
    case VK_None:
274
0
      llvm_unreachable("Invalid viewer");
275
0
    }
276
0
277
0
    ErrMsg.clear();
278
0
    return ExecGraphViewer(ViewerPath, args, OutputFilename, wait, ErrMsg);
279
0
  }
280
0
281
0
  // dotty
282
0
  if (S.TryFindProgram("dotty", ViewerPath)) {
283
0
    std::vector<StringRef> args;
284
0
    args.push_back(ViewerPath);
285
0
    args.push_back(Filename);
286
0
287
0
// Dotty spawns another app and doesn't wait until it returns
288
#ifdef _WIN32
289
    wait = false;
290
#endif
291
    errs() << "Running 'dotty' program... ";
292
0
    return ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg);
293
0
  }
294
0
295
0
  errs() << "Error: Couldn't find a usable graph viewer program:\n";
296
0
  errs() << S.LogBuffer << "\n";
297
0
  return true;
298
0
}