comparison include/llvm/Support/GraphWriter.h @ 148:63bd29f05246

merged
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 14 Aug 2019 19:46:37 +0900
parents c2174574ed3a
children
comparison
equal deleted inserted replaced
146:3fc4d5c3e21e 148:63bd29f05246
1 //===- llvm/Support/GraphWriter.h - Write graph to a .dot file --*- C++ -*-===// 1 //===- llvm/Support/GraphWriter.h - Write graph to a .dot file --*- C++ -*-===//
2 // 2 //
3 // The LLVM Compiler Infrastructure 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // 4 // See https://llvm.org/LICENSE.txt for license information.
5 // This file is distributed under the University of Illinois Open Source 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 // License. See LICENSE.TXT for details.
7 // 6 //
8 //===----------------------------------------------------------------------===// 7 //===----------------------------------------------------------------------===//
9 // 8 //
10 // This file defines a simple interface that can be used to print out generic 9 // This file defines a simple interface that can be used to print out generic
11 // LLVM graphs to ".dot" files. "dot" is a tool that is part of the AT&T 10 // LLVM graphs to ".dot" files. "dot" is a tool that is part of the AT&T
25 24
26 #include "llvm/ADT/GraphTraits.h" 25 #include "llvm/ADT/GraphTraits.h"
27 #include "llvm/ADT/StringRef.h" 26 #include "llvm/ADT/StringRef.h"
28 #include "llvm/ADT/Twine.h" 27 #include "llvm/ADT/Twine.h"
29 #include "llvm/Support/DOTGraphTraits.h" 28 #include "llvm/Support/DOTGraphTraits.h"
29 #include "llvm/Support/FileSystem.h"
30 #include "llvm/Support/raw_ostream.h" 30 #include "llvm/Support/raw_ostream.h"
31 #include <algorithm> 31 #include <algorithm>
32 #include <cstddef> 32 #include <cstddef>
33 #include <iterator> 33 #include <iterator>
34 #include <string> 34 #include <string>
39 39
40 namespace DOT { // Private functions... 40 namespace DOT { // Private functions...
41 41
42 std::string EscapeString(const std::string &Label); 42 std::string EscapeString(const std::string &Label);
43 43
44 /// \brief Get a color string for this node number. Simply round-robin selects 44 /// Get a color string for this node number. Simply round-robin selects
45 /// from a reasonable number of colors. 45 /// from a reasonable number of colors.
46 StringRef getColorString(unsigned NodeNumber); 46 StringRef getColorString(unsigned NodeNumber);
47 47
48 } // end namespace DOT 48 } // end namespace DOT
49 49
318 return O; 318 return O;
319 } 319 }
320 320
321 std::string createGraphFilename(const Twine &Name, int &FD); 321 std::string createGraphFilename(const Twine &Name, int &FD);
322 322
323 /// Writes graph into a provided {@code Filename}.
324 /// If {@code Filename} is empty, generates a random one.
325 /// \return The resulting filename, or an empty string if writing
326 /// failed.
323 template <typename GraphType> 327 template <typename GraphType>
324 std::string WriteGraph(const GraphType &G, const Twine &Name, 328 std::string WriteGraph(const GraphType &G, const Twine &Name,
325 bool ShortNames = false, const Twine &Title = "") { 329 bool ShortNames = false,
330 const Twine &Title = "",
331 std::string Filename = "") {
326 int FD; 332 int FD;
327 // Windows can't always handle long paths, so limit the length of the name. 333 // Windows can't always handle long paths, so limit the length of the name.
328 std::string N = Name.str(); 334 std::string N = Name.str();
329 N = N.substr(0, std::min<std::size_t>(N.size(), 140)); 335 N = N.substr(0, std::min<std::size_t>(N.size(), 140));
330 std::string Filename = createGraphFilename(N, FD); 336 if (Filename.empty()) {
337 Filename = createGraphFilename(N, FD);
338 } else {
339 std::error_code EC = sys::fs::openFileForWrite(Filename, FD);
340
341 // Writing over an existing file is not considered an error.
342 if (EC == std::errc::file_exists) {
343 errs() << "file exists, overwriting" << "\n";
344 } else if (EC) {
345 errs() << "error writing into file" << "\n";
346 return "";
347 }
348 }
331 raw_fd_ostream O(FD, /*shouldClose=*/ true); 349 raw_fd_ostream O(FD, /*shouldClose=*/ true);
332 350
333 if (FD == -1) { 351 if (FD == -1) {
334 errs() << "error opening file '" << Filename << "' for writing!\n"; 352 errs() << "error opening file '" << Filename << "' for writing!\n";
335 return ""; 353 return "";