[flang] Use a file descriptor in Temp struct (flang-compiler/f18#1036)
authorSteve Scalpone <sscalpone@nvidia.com>
Thu, 5 Mar 2020 15:09:29 +0000 (07:09 -0800)
committerGitHub <noreply@github.com>
Thu, 5 Mar 2020 15:09:29 +0000 (07:09 -0800)
The struct Temp is used in the function call createUniqueFile
which only takes in a file descriptor instead of a file handler.
In Unix these are the same thing, but in Windows they are different.
Therefore, the type of the member of struct Temp is changed
from file handler to file descriptor and when closing the file
the file descriptor is converted to a file handler.

Original-commit: flang-compiler/f18@a8edb328f717305143ac827132c6d6f45e6e11b9
Reviewed-on: https://github.com/flang-compiler/f18/pull/1036

flang/lib/Semantics/mod-file.cpp

index 2810c35..7251e3f 100644 (file)
@@ -617,15 +617,16 @@ std::ostream &PutLower(std::ostream &os, const std::string &str) {
 }
 
 struct Temp {
-  Temp(llvm::sys::fs::file_t fd, std::string path) : fd{fd}, path{path} {}
+  Temp(int fd, std::string path) : fd{fd}, path{path} {}
   Temp(Temp &&t) : fd{std::exchange(t.fd, -1)}, path{std::move(t.path)} {}
   ~Temp() {
     if (fd >= 0) {
-      llvm::sys::fs::closeFile(fd);
+      llvm::sys::fs::file_t native{llvm::sys::fs::convertFDToNativeFile(fd)};
+      llvm::sys::fs::closeFile(native);
       llvm::sys::fs::remove(path.c_str());
     }
   }
-  llvm::sys::fs::file_t fd;
+  int fd;
   std::string path;
 };
 
@@ -639,7 +640,7 @@ static llvm::ErrorOr<Temp> MkTemp(const std::string &path) {
   CHECK(length > suffix.length() &&
       path.substr(length - suffix.length()) == suffix);
   auto prefix{path.substr(0, length - suffix.length())};
-  llvm::sys::fs::file_t fd;
+  int fd;
   llvm::SmallString<16> tempPath;
   if (std::error_code err{llvm::sys::fs::createUniqueFile(
           prefix + "%%%%%%" + suffix, fd, tempPath)}) {