[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
Thu Sep 27 15:26:14 PDT 2018


george.karpenkov created this revision.
george.karpenkov added reviewers: NoQ, Bigcheese, bkramer.

Always generating a temporary file is not always suitable, especially for tests


https://reviews.llvm.org/D52636

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


Index: llvm/include/llvm/Support/GraphWriter.h
===================================================================
--- llvm/include/llvm/Support/GraphWriter.h
+++ llvm/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>
@@ -322,12 +323,22 @@
 
 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);
+    if (EC && EC != std::errc::file_exists) {
+      llvm::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.167407.patch
Type: text/x-patch
Size: 1376 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180927/999997a4/attachment.bin>


More information about the llvm-commits mailing list