[PATCH] D52636: GraphWriter: Provide an API for writing a graph into a specified file

George Karpenkov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 28 11:52:44 PDT 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rL343351: GraphWriter: Provide an API for writing a graph into a specified file (authored by george.karpenkov, committed by ).
Herald added a subscriber: kristina.

Changed prior to commit:
  https://reviews.llvm.org/D52636?vs=167407&id=167525#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D52636

Files:
  llvm/trunk/include/llvm/Support/GraphWriter.h


Index: llvm/trunk/include/llvm/Support/GraphWriter.h
===================================================================
--- llvm/trunk/include/llvm/Support/GraphWriter.h
+++ llvm/trunk/include/llvm/Support/GraphWriter.h
@@ -27,6 +27,7 @@
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/Twine.h"
 #include "llvm/Support/DOTGraphTraits.h"
+#include "llvm/Support/FileSystem.h"
 #include "llvm/Support/raw_ostream.h"
 #include <algorithm>
 #include <cstddef>
@@ -320,14 +321,32 @@
 
 std::string createGraphFilename(const Twine &Name, int &FD);
 
+/// Writes graph into a provided {@code Filename}.
+/// If {@code Filename} is empty, generates a random one.
+/// \return The resulting filename, or an empty string if writing
+/// failed.
 template <typename GraphType>
 std::string WriteGraph(const GraphType &G, const Twine &Name,
-                       bool ShortNames = false, const Twine &Title = "") {
+                       bool ShortNames = false,
+                       const Twine &Title = "",
+                       std::string Filename = "") {
   int FD;
   // Windows can't always handle long paths, so limit the length of the name.
   std::string N = Name.str();
   N = N.substr(0, std::min<std::size_t>(N.size(), 140));
-  std::string Filename = createGraphFilename(N, FD);
+  if (Filename.empty()) {
+    Filename = createGraphFilename(N, FD);
+  } else {
+    std::error_code EC = sys::fs::openFileForWrite(Filename, FD);
+
+    // Writing over an existing file is not considered an error.
+    if (EC == std::errc::file_exists) {
+      errs() << "file exists, overwriting" << "\n";
+    } else if (EC) {
+      errs() << "error writing into file" << "\n";
+      return "";
+    }
+  }
   raw_fd_ostream O(FD, /*shouldClose=*/ true);
 
   if (FD == -1) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D52636.167525.patch
Type: text/x-patch
Size: 1780 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180928/d19d3882/attachment-0001.bin>


More information about the llvm-commits mailing list