Index: llvm/include/llvm/Support/FileSystem.h =================================================================== --- llvm/include/llvm/Support/FileSystem.h +++ llvm/include/llvm/Support/FileSystem.h @@ -716,7 +716,11 @@ OF_Append = 2, F_Append = 2, // For compatibility - /// Delete the file on close. Only makes a difference on windows. + /// Delete the file on close. This is guaranteed to be supported on all + /// platforms, and it is guaranteed that the file descriptor / file object + /// that is returned from the open call is valid and usable in the way the + /// user has requested. However, it is **not** guaranteed that + /// exists(Path) will return true after the call to open returns. OF_Delete = 4 }; Index: llvm/lib/Support/Unix/Path.inc =================================================================== --- llvm/lib/Support/Unix/Path.inc +++ llvm/lib/Support/Unix/Path.inc @@ -774,6 +774,9 @@ (void)r; assert(r == 0 && "fcntl(F_SETFD, FD_CLOEXEC) failed"); #endif + if (Flags & OF_Delete) + ::unlink(P.data()); + return std::error_code(); } Index: llvm/unittests/Support/Path.cpp =================================================================== --- llvm/unittests/Support/Path.cpp +++ llvm/unittests/Support/Path.cpp @@ -1458,6 +1458,26 @@ verifyWrite(FD, "Buzz", true); } +TEST_F(FileSystemTest, DeleteOnClose) { + createFileWithData(NonExistantFile, false, fs::CD_CreateNew, "Fizz"); + + // Even though we expect the file to be deleted automatically on close, we put + // this here as a failsafe so it gets removed even if the test fails, so we + // don't clutter up a file system. + FileRemover Cleanup(NonExistantFile); + + int FD; + Optional Closer; + + ASSERT_NO_ERROR(fs::openFileForReadWrite(NonExistantFile, FD, + fs::CD_OpenExisting, fs::OF_Delete)); + Closer.emplace(FD); + verifyRead(FD, "Fizz", true); + verifyWrite(FD, "Buzz", true); + Closer.reset(); + ASSERT_FALSE(fs::exists(NonExistantFile)); +} + TEST_F(FileSystemTest, set_current_path) { SmallString<128> path;