[llvm] fad7559 - [llvm] [unittests] Remove temporary files after they're not needed

Sergej Jaskiewicz via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 1 14:34:58 PDT 2020


Author: Sergej Jaskiewicz
Date: 2020-09-02T00:34:44+03:00
New Revision: fad75598d272b9a5591fb7d9b591cf00cdf5022c

URL: https://github.com/llvm/llvm-project/commit/fad75598d272b9a5591fb7d9b591cf00cdf5022c
DIFF: https://github.com/llvm/llvm-project/commit/fad75598d272b9a5591fb7d9b591cf00cdf5022c.diff

LOG: [llvm] [unittests] Remove temporary files after they're not needed

Some LLVM unit tests forget to clean up temporary files and
directories. Introduce RAII classes for cleaning them up.

Refactor the tests to use those classes.

Differential Revision: https://reviews.llvm.org/D83228

Added: 
    

Modified: 
    llvm/include/llvm/Testing/Support/SupportHelpers.h
    llvm/unittests/ProfileData/SampleProfTest.cpp
    llvm/unittests/Support/CommandLineTest.cpp
    llvm/unittests/Support/FileCollectorTest.cpp
    llvm/unittests/Support/FileUtilitiesTest.cpp
    llvm/unittests/Support/LockFileManagerTest.cpp
    llvm/unittests/Support/TarWriterTest.cpp
    llvm/unittests/Support/VirtualFileSystemTest.cpp
    llvm/unittests/tools/llvm-exegesis/Mips/BenchmarkResultTest.cpp
    llvm/unittests/tools/llvm-exegesis/X86/SnippetFileTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Testing/Support/SupportHelpers.h b/llvm/include/llvm/Testing/Support/SupportHelpers.h
index 38726b1cfaf7..3517361041b9 100644
--- a/llvm/include/llvm/Testing/Support/SupportHelpers.h
+++ b/llvm/include/llvm/Testing/Support/SupportHelpers.h
@@ -12,6 +12,8 @@
 #include "llvm/ADT/Optional.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/Support/Error.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Path.h"
 #include "llvm/Support/raw_os_ostream.h"
 #include "gmock/gmock-matchers.h"
 #include "gtest/gtest-printers.h"
@@ -103,7 +105,140 @@ detail::ValueIsMatcher<InnerMatcher> ValueIs(const InnerMatcher &ValueMatcher) {
   return detail::ValueIsMatcher<InnerMatcher>(ValueMatcher);
 }
 namespace unittest {
+
 SmallString<128> getInputFileDirectory(const char *Argv0);
+
+/// A RAII object that creates a temporary directory upon initialization and
+/// removes it upon destruction.
+class TempDir {
+  SmallString<128> Path;
+
+public:
+  /// Creates a managed temporary directory.
+  ///
+  /// @param Name The name of the directory to create.
+  /// @param Unique If true, the directory will be created using
+  ///               llvm::sys::fs::createUniqueDirectory.
+  explicit TempDir(StringRef Name, bool Unique = false) {
+    std::error_code EC;
+    if (Unique) {
+      EC = llvm::sys::fs::createUniqueDirectory(Name, Path);
+      if (!EC) {
+        // Resolve any symlinks in the new directory.
+        std::string UnresolvedPath(Path.str());
+        EC = llvm::sys::fs::real_path(UnresolvedPath, Path);
+      }
+    } else {
+      Path = Name;
+      EC = llvm::sys::fs::create_directory(Path);
+    }
+    if (EC)
+      Path.clear();
+    EXPECT_FALSE(EC) << EC.message();
+  }
+
+  ~TempDir() {
+    if (!Path.empty()) {
+      EXPECT_FALSE(llvm::sys::fs::remove_directories(Path.str()));
+    }
+  }
+
+  TempDir(const TempDir &) = delete;
+  TempDir &operator=(const TempDir &) = delete;
+
+  TempDir(TempDir &&) = default;
+  TempDir &operator=(TempDir &&) = default;
+
+  /// The path to the temporary directory.
+  StringRef path() const { return Path; }
+
+  /// Creates a new path by appending the argument to the path of the managed
+  /// directory using the native path separator.
+  SmallString<128> path(StringRef component) const {
+    SmallString<128> Result(Path);
+    SmallString<128> ComponentToAppend(component);
+    llvm::sys::path::native(ComponentToAppend);
+    llvm::sys::path::append(Result, Twine(ComponentToAppend));
+    return Result;
+  }
+};
+
+/// A RAII object that creates a link upon initialization and
+/// removes it upon destruction.
+///
+/// The link may be a soft or a hard link, depending on the platform.
+class TempLink {
+  SmallString<128> Path;
+
+public:
+  /// Creates a managed link at path Link pointing to Target.
+  TempLink(StringRef Target, StringRef Link) {
+    Path = Link;
+    std::error_code EC = sys::fs::create_link(Target, Link);
+    if (EC)
+      Path.clear();
+    EXPECT_FALSE(EC);
+  }
+  ~TempLink() {
+    if (!Path.empty()) {
+      EXPECT_FALSE(llvm::sys::fs::remove(Path.str()));
+    }
+  }
+
+  TempLink(const TempLink &) = delete;
+  TempLink &operator=(const TempLink &) = delete;
+
+  TempLink(TempLink &&) = default;
+  TempLink &operator=(TempLink &&) = default;
+
+  /// The path to the link.
+  StringRef path() const { return Path; }
+};
+
+/// A RAII object that creates a file upon initialization and
+/// removes it upon destruction.
+class TempFile {
+  SmallString<128> Path;
+
+public:
+  /// Creates a managed file.
+  ///
+  /// @param Name The name of the file to create.
+  /// @param Contents The string to write to the file.
+  /// @param Unique If true, the file will be created using
+  ///               llvm::sys::fs::createTemporaryFile.
+  TempFile(StringRef Name, StringRef Suffix = "", StringRef Contents = "",
+           bool Unique = false) {
+    std::error_code EC;
+    int fd;
+    if (Unique) {
+      EC = llvm::sys::fs::createTemporaryFile(Name, Suffix, fd, Path);
+    } else {
+      Path = Name;
+      if (!Suffix.empty()) {
+        Path.append(".");
+        Path.append(Suffix);
+      }
+      EC = llvm::sys::fs::openFileForWrite(Path, fd);
+    }
+    EXPECT_FALSE(EC);
+    raw_fd_ostream OS(fd, /*shouldClose*/ true);
+    OS << Contents;
+    OS.flush();
+    EXPECT_FALSE(OS.error());
+    if (EC || OS.error())
+      Path.clear();
+  }
+  ~TempFile() {
+    if (!Path.empty()) {
+      EXPECT_FALSE(llvm::sys::fs::remove(Path.str()));
+    }
+  }
+
+  /// The path to the file.
+  StringRef path() const { return Path; }
+};
+
 } // namespace unittest
 } // namespace llvm
 

diff  --git a/llvm/unittests/ProfileData/SampleProfTest.cpp b/llvm/unittests/ProfileData/SampleProfTest.cpp
index cef5f042dd37..8b81418a8506 100644
--- a/llvm/unittests/ProfileData/SampleProfTest.cpp
+++ b/llvm/unittests/ProfileData/SampleProfTest.cpp
@@ -19,6 +19,7 @@
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/raw_ostream.h"
+#include "llvm/Testing/Support/SupportHelpers.h"
 #include "gtest/gtest.h"
 #include <string>
 #include <vector>
@@ -26,6 +27,8 @@
 using namespace llvm;
 using namespace sampleprof;
 
+using llvm::unittest::TempFile;
+
 static ::testing::AssertionResult NoError(std::error_code EC) {
   if (!EC)
     return ::testing::AssertionSuccess();
@@ -60,21 +63,14 @@ struct SampleProfTest : ::testing::Test {
     Reader->collectFuncsFrom(M);
   }
 
-  void createRemapFile(SmallVectorImpl<char> &RemapPath, StringRef &RemapFile) {
-    std::error_code EC =
-        llvm::sys::fs::createTemporaryFile("remapfile", "", RemapPath);
-    ASSERT_TRUE(NoError(EC));
-    RemapFile = StringRef(RemapPath.data(), RemapPath.size());
-
-    std::unique_ptr<raw_fd_ostream> OS(
-        new raw_fd_ostream(RemapFile, EC, sys::fs::OF_None));
-    *OS << R"(
+  TempFile createRemapFile() {
+    return TempFile("remapfile", "", R"(
       # Types 'int' and 'long' are equivalent
       type i l
       # Function names 'foo' and 'faux' are equivalent
       name 3foo 4faux
-    )";
-    OS->close();
+    )",
+                    /*Unique*/ true);
   }
 
   // Verify profile summary is consistent in the roundtrip to and from
@@ -137,10 +133,8 @@ struct SampleProfTest : ::testing::Test {
   }
 
   void testRoundTrip(SampleProfileFormat Format, bool Remap, bool UseMD5) {
-    SmallVector<char, 128> ProfilePath;
-    ASSERT_TRUE(NoError(llvm::sys::fs::createTemporaryFile("profile", "", ProfilePath)));
-    StringRef Profile(ProfilePath.data(), ProfilePath.size());
-    createWriter(Format, Profile);
+    TempFile ProfileFile("profile", "", "", /*Unique*/ true);
+    createWriter(Format, ProfileFile.path());
     if (Format == SampleProfileFormat::SPF_Ext_Binary && UseMD5)
       static_cast<SampleProfileWriterExtBinary *>(Writer.get())->setUseMD5();
 
@@ -207,10 +201,8 @@ struct SampleProfTest : ::testing::Test {
     FunctionType *fn_type =
         FunctionType::get(Type::getVoidTy(Context), {}, false);
 
-    SmallVector<char, 128> RemapPath;
-    StringRef RemapFile;
+    TempFile RemapFile(createRemapFile());
     if (Remap) {
-      createRemapFile(RemapPath, RemapFile);
       FooName = "_Z4fauxi";
       BarName = "_Z3barl";
       GooName = "_Z3gool";
@@ -234,7 +226,7 @@ struct SampleProfTest : ::testing::Test {
 
     Writer->getOutputStream().flush();
 
-    readProfile(M, Profile, RemapFile);
+    readProfile(M, ProfileFile.path(), RemapFile.path());
     EC = Reader->read();
     ASSERT_TRUE(NoError(EC));
 
@@ -375,24 +367,21 @@ struct SampleProfTest : ::testing::Test {
 
   void testSuffixElisionPolicy(SampleProfileFormat Format, StringRef Policy,
                                const StringMap<uint64_t> &Expected) {
-    SmallVector<char, 128> ProfilePath;
-    std::error_code EC;
-    EC = llvm::sys::fs::createTemporaryFile("profile", "", ProfilePath);
-    ASSERT_TRUE(NoError(EC));
-    StringRef ProfileFile(ProfilePath.data(), ProfilePath.size());
+    TempFile ProfileFile("profile", "", "", /*Unique*/ true);
 
     Module M("my_module", Context);
     setupModuleForElisionTest(&M, Policy);
     StringMap<FunctionSamples> ProfMap = setupFcnSamplesForElisionTest(Policy);
 
     // write profile
-    createWriter(Format, ProfileFile);
+    createWriter(Format, ProfileFile.path());
+    std::error_code EC;
     EC = Writer->write(ProfMap);
     ASSERT_TRUE(NoError(EC));
     Writer->getOutputStream().flush();
 
     // read profile
-    readProfile(M, ProfileFile);
+    readProfile(M, ProfileFile.path());
     EC = Reader->read();
     ASSERT_TRUE(NoError(EC));
 

diff  --git a/llvm/unittests/Support/CommandLineTest.cpp b/llvm/unittests/Support/CommandLineTest.cpp
index be8217b10962..c02e9e59a5e0 100644
--- a/llvm/unittests/Support/CommandLineTest.cpp
+++ b/llvm/unittests/Support/CommandLineTest.cpp
@@ -22,6 +22,7 @@
 #include "llvm/Support/StringSaver.h"
 #include "llvm/Support/VirtualFileSystem.h"
 #include "llvm/Support/raw_ostream.h"
+#include "llvm/Testing/Support/SupportHelpers.h"
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
 #include <fstream>
@@ -30,6 +31,8 @@
 #include <tuple>
 
 using namespace llvm;
+using llvm::unittest::TempDir;
+using llvm::unittest::TempFile;
 
 namespace {
 
@@ -754,20 +757,13 @@ TEST(CommandLineTest, ResponseFileWindows) {
   StackOption<bool> TopLevelOpt("top-level", cl::init(false));
 
   // Create response file.
-  int FileDescriptor;
-  SmallString<64> TempPath;
-  std::error_code EC =
-      llvm::sys::fs::createTemporaryFile("resp-", ".txt", FileDescriptor, TempPath);
-  EXPECT_TRUE(!EC);
-
-  std::ofstream RspFile(TempPath.c_str());
-  EXPECT_TRUE(RspFile.is_open());
-  RspFile << "-top-level\npath\\dir\\file1\npath/dir/file2";
-  RspFile.close();
+  TempFile ResponseFile("resp-", ".txt",
+                        "-top-level\npath\\dir\\file1\npath/dir/file2",
+                        /*Unique*/ true);
 
   llvm::SmallString<128> RspOpt;
   RspOpt.append(1, '@');
-  RspOpt.append(TempPath.c_str());
+  RspOpt.append(ResponseFile.path());
   const char *args[] = {"prog", RspOpt.c_str()};
   EXPECT_FALSE(TopLevelOpt);
   EXPECT_TRUE(
@@ -775,8 +771,6 @@ TEST(CommandLineTest, ResponseFileWindows) {
   EXPECT_TRUE(TopLevelOpt);
   EXPECT_TRUE(InputFilenames[0] == "path\\dir\\file1");
   EXPECT_TRUE(InputFilenames[1] == "path/dir/file2");
-
-  llvm::sys::fs::remove(TempPath.c_str());
 }
 
 TEST(CommandLineTest, ResponseFiles) {
@@ -1007,44 +1001,38 @@ TEST(CommandLineTest, SetDefautValue) {
 TEST(CommandLineTest, ReadConfigFile) {
   llvm::SmallVector<const char *, 1> Argv;
 
-  llvm::SmallString<128> TestDir;
-  std::error_code EC =
-      llvm::sys::fs::createUniqueDirectory("unittest", TestDir);
-  EXPECT_TRUE(!EC);
+  TempDir TestDir("unittest", /*Unique*/ true);
 
   llvm::SmallString<128> TestCfg;
-  llvm::sys::path::append(TestCfg, TestDir, "foo");
-  std::ofstream ConfigFile(TestCfg.c_str());
-  EXPECT_TRUE(ConfigFile.is_open());
-  ConfigFile << "# Comment\n"
-                "-option_1\n"
-                "@subconfig\n"
-                "-option_3=abcd\n"
-                "-option_4=\\\n"
-                "cdef\n";
-  ConfigFile.close();
+  llvm::sys::path::append(TestCfg, TestDir.path(), "foo");
+
+  TempFile ConfigFile(TestCfg, "",
+                      "# Comment\n"
+                      "-option_1\n"
+                      "@subconfig\n"
+                      "-option_3=abcd\n"
+                      "-option_4=\\\n"
+                      "cdef\n");
 
   llvm::SmallString<128> TestCfg2;
-  llvm::sys::path::append(TestCfg2, TestDir, "subconfig");
-  std::ofstream ConfigFile2(TestCfg2.c_str());
-  EXPECT_TRUE(ConfigFile2.is_open());
-  ConfigFile2 << "-option_2\n"
-                 "\n"
-                 "   # comment\n";
-  ConfigFile2.close();
+  llvm::sys::path::append(TestCfg2, TestDir.path(), "subconfig");
+  TempFile ConfigFile2(TestCfg2, "",
+                       "-option_2\n"
+                       "\n"
+                       "   # comment\n");
 
   // Make sure the current directory is not the directory where config files
   // resides. In this case the code that expands response files will not find
   // 'subconfig' unless it resolves nested inclusions relative to the including
   // file.
   llvm::SmallString<128> CurrDir;
-  EC = llvm::sys::fs::current_path(CurrDir);
+  std::error_code EC = llvm::sys::fs::current_path(CurrDir);
   EXPECT_TRUE(!EC);
-  EXPECT_TRUE(StringRef(CurrDir) != StringRef(TestDir));
+  EXPECT_TRUE(StringRef(CurrDir) != TestDir.path());
 
   llvm::BumpPtrAllocator A;
   llvm::StringSaver Saver(A);
-  bool Result = llvm::cl::readConfigFile(TestCfg, Saver, Argv);
+  bool Result = llvm::cl::readConfigFile(ConfigFile.path(), Saver, Argv);
 
   EXPECT_TRUE(Result);
   EXPECT_EQ(Argv.size(), 4U);
@@ -1052,10 +1040,6 @@ TEST(CommandLineTest, ReadConfigFile) {
   EXPECT_STREQ(Argv[1], "-option_2");
   EXPECT_STREQ(Argv[2], "-option_3=abcd");
   EXPECT_STREQ(Argv[3], "-option_4=cdef");
-
-  llvm::sys::fs::remove(TestCfg2);
-  llvm::sys::fs::remove(TestCfg);
-  llvm::sys::fs::remove(TestDir);
 }
 
 TEST(CommandLineTest, PositionalEatArgsError) {

diff  --git a/llvm/unittests/Support/FileCollectorTest.cpp b/llvm/unittests/Support/FileCollectorTest.cpp
index a81ce7130f2f..20d7068d2071 100644
--- a/llvm/unittests/Support/FileCollectorTest.cpp
+++ b/llvm/unittests/Support/FileCollectorTest.cpp
@@ -11,8 +11,12 @@
 
 #include "llvm/Support/FileCollector.h"
 #include "llvm/Support/FileSystem.h"
+#include "llvm/Testing/Support/SupportHelpers.h"
 
 using namespace llvm;
+using llvm::unittest::TempDir;
+using llvm::unittest::TempFile;
+using llvm::unittest::TempLink;
 
 namespace llvm {
 namespace vfs {
@@ -37,71 +41,11 @@ class TestingFileCollector : public FileCollector {
   }
 };
 
-struct ScopedDir {
-  SmallString<128> Path;
-  ScopedDir(const Twine &Name, bool Unique = false) {
-    std::error_code EC;
-    if (Unique) {
-      EC = llvm::sys::fs::createUniqueDirectory(Name, Path);
-    } else {
-      Path = Name.str();
-      EC = llvm::sys::fs::create_directory(Twine(Path));
-    }
-    if (EC)
-      Path = "";
-    EXPECT_FALSE(EC);
-    // Ensure the path is the real path so tests can use it to compare against
-    // realpath output.
-    SmallString<128> RealPath;
-    if (!llvm::sys::fs::real_path(Path, RealPath))
-      Path.swap(RealPath);
-  }
-  ~ScopedDir() {
-    if (Path != "") {
-      EXPECT_FALSE(llvm::sys::fs::remove_directories(Path.str()));
-    }
-  }
-  operator StringRef() { return Path.str(); }
-};
-
-struct ScopedLink {
-  SmallString<128> Path;
-  ScopedLink(const Twine &To, const Twine &From) {
-    Path = From.str();
-    std::error_code EC = sys::fs::create_link(To, From);
-    if (EC)
-      Path = "";
-    EXPECT_FALSE(EC);
-  }
-  ~ScopedLink() {
-    if (Path != "") {
-      EXPECT_FALSE(llvm::sys::fs::remove(Path.str()));
-    }
-  }
-  operator StringRef() { return Path.str(); }
-};
-
-struct ScopedFile {
-  SmallString<128> Path;
-  ScopedFile(const Twine &Name) {
-    std::error_code EC;
-    EC = llvm::sys::fs::createUniqueFile(Name, Path);
-    if (EC)
-      Path = "";
-    EXPECT_FALSE(EC);
-  }
-  ~ScopedFile() {
-    if (Path != "") {
-      EXPECT_FALSE(llvm::sys::fs::remove(Path.str()));
-    }
-  }
-  operator StringRef() { return Path.str(); }
-};
 } // end anonymous namespace
 
 TEST(FileCollectorTest, addFile) {
-  ScopedDir root("add_file_root", true);
-  std::string root_fs = std::string(root.Path.str());
+  TempDir root("add_file_root", /*Unique*/ true);
+  std::string root_fs(root.path());
   TestingFileCollector FileCollector(root_fs, root_fs);
 
   FileCollector.addFile("/path/to/a");
@@ -121,53 +65,53 @@ TEST(FileCollectorTest, addFile) {
 }
 
 TEST(FileCollectorTest, addDirectory) {
-  ScopedDir file_root("file_root", true);
+  TempDir file_root("file_root", /*Unique*/ true);
 
-  llvm::SmallString<128> aaa = file_root.Path;
+  llvm::SmallString<128> aaa(file_root.path());
   llvm::sys::path::append(aaa, "aaa");
-  ScopedFile a(aaa.str());
+  TempFile a(aaa.str());
 
-  llvm::SmallString<128> bbb = file_root.Path;
+  llvm::SmallString<128> bbb(file_root.path());
   llvm::sys::path::append(bbb, "bbb");
-  ScopedFile b(bbb.str());
+  TempFile b(bbb.str());
 
-  llvm::SmallString<128> ccc = file_root.Path;
+  llvm::SmallString<128> ccc(file_root.path());
   llvm::sys::path::append(ccc, "ccc");
-  ScopedFile c(ccc.str());
+  TempFile c(ccc.str());
 
-  std::string root_fs = std::string(file_root.Path.str());
+  std::string root_fs(file_root.path());
   TestingFileCollector FileCollector(root_fs, root_fs);
 
-  FileCollector.addDirectory(file_root.Path);
+  FileCollector.addDirectory(file_root.path());
 
   // Make sure the root is correct.
   EXPECT_EQ(FileCollector.Root, root_fs);
 
   // Make sure we've seen all the added files.
-  EXPECT_TRUE(FileCollector.hasSeen(a.Path));
-  EXPECT_TRUE(FileCollector.hasSeen(b.Path));
-  EXPECT_TRUE(FileCollector.hasSeen(c.Path));
+  EXPECT_TRUE(FileCollector.hasSeen(a.path()));
+  EXPECT_TRUE(FileCollector.hasSeen(b.path()));
+  EXPECT_TRUE(FileCollector.hasSeen(c.path()));
 
   // Make sure we've only seen the added files.
-  llvm::SmallString<128> ddd = file_root.Path;
+  llvm::SmallString<128> ddd(file_root.path());
   llvm::sys::path::append(ddd, "ddd");
-  ScopedFile d(ddd.str());
-  EXPECT_FALSE(FileCollector.hasSeen(d.Path));
+  TempFile d(ddd.str(), "", "", /*Unique*/ true);
+  EXPECT_FALSE(FileCollector.hasSeen(d.path()));
 }
 
 TEST(FileCollectorTest, copyFiles) {
-  ScopedDir file_root("file_root", true);
-  ScopedFile a(file_root + "/aaa");
-  ScopedFile b(file_root + "/bbb");
-  ScopedFile c(file_root + "/ccc");
+  TempDir file_root("file_root", /*Unique*/ true);
+  TempFile a(file_root.path("aaa"));
+  TempFile b(file_root.path("bbb"));
+  TempFile c(file_root.path("ccc"));
 
   // Create file collector and add files.
-  ScopedDir root("copy_files_root", true);
-  std::string root_fs = std::string(root.Path.str());
+  TempDir root("copy_files_root", /*Unique*/ true);
+  std::string root_fs(root.path());
   TestingFileCollector FileCollector(root_fs, root_fs);
-  FileCollector.addFile(a.Path);
-  FileCollector.addFile(b.Path);
-  FileCollector.addFile(c.Path);
+  FileCollector.addFile(a.path());
+  FileCollector.addFile(b.path());
+  FileCollector.addFile(c.path());
 
   // Make sure we can copy the files.
   std::error_code ec = FileCollector.copyFiles(true);
@@ -184,72 +128,70 @@ TEST(FileCollectorTest, copyFiles) {
 }
 
 TEST(FileCollectorTest, recordAndConstructDirectory) {
-  ScopedDir file_root("dir_root", true);
-  ScopedDir subdir(file_root + "/subdir");
-  ScopedDir subdir2(file_root + "/subdir2");
-  ScopedFile a(subdir2 + "/a");
+  TempDir file_root("dir_root", /*Unique*/ true);
+  TempDir subdir(file_root.path("subdir"));
+  TempDir subdir2(file_root.path("subdir2"));
+  TempFile a(subdir2.path("a"));
 
   // Create file collector and add files.
-  ScopedDir root("copy_files_root", true);
-  std::string root_fs = std::string(root.Path.str());
+  TempDir root("copy_files_root", /*Unique*/ true);
+  std::string root_fs(root.path());
   TestingFileCollector FileCollector(root_fs, root_fs);
-  FileCollector.addFile(a.Path);
+  FileCollector.addFile(a.path());
 
   // The empty directory isn't seen until we add it.
-  EXPECT_TRUE(FileCollector.hasSeen(a.Path));
-  EXPECT_FALSE(FileCollector.hasSeen(subdir.Path));
+  EXPECT_TRUE(FileCollector.hasSeen(a.path()));
+  EXPECT_FALSE(FileCollector.hasSeen(subdir.path()));
 
-  FileCollector.addFile(subdir.Path);
-  EXPECT_TRUE(FileCollector.hasSeen(subdir.Path));
+  FileCollector.addFile(subdir.path());
+  EXPECT_TRUE(FileCollector.hasSeen(subdir.path()));
 
   // Make sure we can construct the directory.
   std::error_code ec = FileCollector.copyFiles(true);
   EXPECT_FALSE(ec);
   bool IsDirectory = false;
-  llvm::SmallString<128> SubdirInRoot = root.Path;
+  llvm::SmallString<128> SubdirInRoot = root.path();
   llvm::sys::path::append(SubdirInRoot,
-                          llvm::sys::path::relative_path(subdir.Path));
+                          llvm::sys::path::relative_path(subdir.path()));
   ec = sys::fs::is_directory(SubdirInRoot, IsDirectory);
   EXPECT_FALSE(ec);
   ASSERT_TRUE(IsDirectory);
 }
 
 TEST(FileCollectorTest, recordVFSAccesses) {
-  ScopedDir file_root("dir_root", true);
-  ScopedDir subdir(file_root + "/subdir");
-  ScopedDir subdir2(file_root + "/subdir2");
-  ScopedFile a(subdir2 + "/a");
-  ScopedFile b(file_root + "/b");
-  ScopedDir subdir3(file_root + "/subdir3");
-  ScopedFile subdir3a(subdir3 + "/aa");
-  ScopedDir subdir3b(subdir3 + "/subdirb");
-  {
-    ScopedFile subdir3fileremoved(subdir3 + "/removed");
-  }
+  TempDir file_root("dir_root", /*Unique*/ true);
+  TempDir subdir(file_root.path("subdir"));
+  TempDir subdir2(file_root.path("subdir2"));
+  TempFile a(subdir2.path("a"));
+  TempFile b(file_root.path("b"));
+  TempDir subdir3(file_root.path("subdir3"));
+  TempFile subdir3a(subdir3.path("aa"));
+  TempDir subdir3b(subdir3.path("subdirb"));
+  { TempFile subdir3fileremoved(subdir3.path("removed")); }
 
   // Create file collector and add files.
-  ScopedDir root("copy_files_root", true);
-  std::string root_fs = std::string(root.Path.str());
+  TempDir root("copy_files_root", /*Unique*/ true);
+  std::string root_fs(root.path());
   auto Collector = std::make_shared<TestingFileCollector>(root_fs, root_fs);
   auto VFS =
       FileCollector::createCollectorVFS(vfs::getRealFileSystem(), Collector);
-  VFS->status(a.Path);
-  EXPECT_TRUE(Collector->hasSeen(a.Path));
+  VFS->status(a.path());
+  EXPECT_TRUE(Collector->hasSeen(a.path()));
 
-  VFS->openFileForRead(b.Path);
-  EXPECT_TRUE(Collector->hasSeen(b.Path));
+  VFS->openFileForRead(b.path());
+  EXPECT_TRUE(Collector->hasSeen(b.path()));
 
-  VFS->status(subdir.Path);
-  EXPECT_TRUE(Collector->hasSeen(subdir.Path));
+  VFS->status(subdir.path());
+  EXPECT_TRUE(Collector->hasSeen(subdir.path()));
 
 #ifndef _WIN32
   std::error_code EC;
-  auto It = VFS->dir_begin(subdir3.Path, EC);
+  auto It = VFS->dir_begin(subdir3.path(), EC);
   EXPECT_FALSE(EC);
-  EXPECT_TRUE(Collector->hasSeen(subdir3.Path));
-  EXPECT_TRUE(Collector->hasSeen(subdir3a.Path));
-  EXPECT_TRUE(Collector->hasSeen(subdir3b.Path));
-  std::string RemovedFileName = (Twine(subdir3.Path) + "/removed").str();
+  EXPECT_TRUE(Collector->hasSeen(subdir3.path()));
+  EXPECT_TRUE(Collector->hasSeen(subdir3a.path()));
+  EXPECT_TRUE(Collector->hasSeen(subdir3b.path()));
+  std::string RemovedFileName((Twine(subdir3.path("removed"))).str());
   EXPECT_FALSE(Collector->hasSeen(RemovedFileName));
 #endif
 }
@@ -257,78 +199,80 @@ TEST(FileCollectorTest, recordVFSAccesses) {
 #ifndef _WIN32
 TEST(FileCollectorTest, Symlinks) {
   // Root where the original files live.
-  ScopedDir file_root("file_root", true);
+  TempDir file_root("file_root", /*Unique*/ true);
 
   // Create some files in the file root.
-  ScopedFile a(file_root + "/aaa");
-  ScopedFile b(file_root + "/bbb");
-  ScopedFile c(file_root + "/ccc");
+  TempFile a(file_root.path("aaa"));
+  TempFile b(file_root.path("bbb"));
+  TempFile c(file_root.path("ccc"));
 
   // Create a directory foo with file ddd.
-  ScopedDir foo(file_root + "/foo");
-  ScopedFile d(foo + "/ddd");
+  TempDir foo(file_root.path("foo"));
+  TempFile d(foo.path("ddd"));
 
   // Create a file eee in the foo's parent directory.
-  ScopedFile e(foo + "/../eee");
+  TempFile e(foo.path("../eee"));
 
   // Create a symlink bar pointing to foo.
-  ScopedLink symlink(file_root + "/foo", file_root + "/bar");
+  TempLink symlink(file_root.path("foo"), file_root.path("bar"));
 
   // Root where files are copied to.
-  ScopedDir reproducer_root("reproducer_root", true);
-  std::string root_fs = std::string(reproducer_root.Path.str());
+  TempDir reproducer_root("reproducer_root", /*Unique*/ true);
+  std::string root_fs(reproducer_root.path());
   TestingFileCollector FileCollector(root_fs, root_fs);
 
   // Add all the files to the collector.
-  FileCollector.addFile(a.Path);
-  FileCollector.addFile(b.Path);
-  FileCollector.addFile(c.Path);
-  FileCollector.addFile(d.Path);
-  FileCollector.addFile(e.Path);
-  FileCollector.addFile(file_root + "/bar/ddd");
+  FileCollector.addFile(a.path());
+  FileCollector.addFile(b.path());
+  FileCollector.addFile(c.path());
+  FileCollector.addFile(d.path());
+  FileCollector.addFile(e.path());
+  FileCollector.addFile(file_root.path() + "/bar/ddd");
 
   auto mapping = FileCollector.VFSWriter.getMappings();
 
   {
     // Make sure the common case works.
-    std::string vpath = (file_root + "/aaa").str();
-    std::string rpath = (reproducer_root.Path + file_root.Path + "/aaa").str();
+    std::string vpath = (file_root.path() + "/aaa").str();
+    std::string rpath =
+        (reproducer_root.path() + file_root.path() + "/aaa").str();
     printf("%s -> %s\n", vpath.c_str(), rpath.c_str());
     EXPECT_THAT(mapping, testing::Contains(vfs::YAMLVFSEntry(vpath, rpath)));
   }
 
   {
     // Make sure the virtual path points to the real source path.
-    std::string vpath = (file_root + "/bar/ddd").str();
+    std::string vpath = (file_root.path() + "/bar/ddd").str();
     std::string rpath =
-        (reproducer_root.Path + file_root.Path + "/foo/ddd").str();
+        (reproducer_root.path() + file_root.path() + "/foo/ddd").str();
     printf("%s -> %s\n", vpath.c_str(), rpath.c_str());
     EXPECT_THAT(mapping, testing::Contains(vfs::YAMLVFSEntry(vpath, rpath)));
   }
 
   {
     // Make sure that .. is removed from the source path.
-    std::string vpath = (file_root + "/eee").str();
-    std::string rpath = (reproducer_root.Path + file_root.Path + "/eee").str();
+    std::string vpath = (file_root.path() + "/eee").str();
+    std::string rpath =
+        (reproducer_root.path() + file_root.path() + "/eee").str();
     printf("%s -> %s\n", vpath.c_str(), rpath.c_str());
     EXPECT_THAT(mapping, testing::Contains(vfs::YAMLVFSEntry(vpath, rpath)));
   }
 }
 
 TEST(FileCollectorTest, recordVFSSymlinkAccesses) {
-  ScopedDir file_root("dir_root", true);
-  ScopedFile a(file_root + "/a");
-  ScopedLink symlink(file_root + "/a", file_root + "/b");
+  TempDir file_root("dir_root", /*Unique*/ true);
+  TempFile a(file_root.path("a"));
+  TempLink symlink(file_root.path("a"), file_root.path("b"));
 
   // Create file collector and add files.
-  ScopedDir root("copy_files_root", true);
-  std::string root_fs = std::string(root.Path.str());
+  TempDir root("copy_files_root", true);
+  std::string root_fs(root.path());
   auto Collector = std::make_shared<TestingFileCollector>(root_fs, root_fs);
   auto VFS =
       FileCollector::createCollectorVFS(vfs::getRealFileSystem(), Collector);
   SmallString<256> Output;
-  VFS->getRealPath(symlink.Path, Output);
-  EXPECT_TRUE(Collector->hasSeen(a.Path));
-  EXPECT_TRUE(Collector->hasSeen(symlink.Path));
+  VFS->getRealPath(symlink.path(), Output);
+  EXPECT_TRUE(Collector->hasSeen(a.path()));
+  EXPECT_TRUE(Collector->hasSeen(symlink.path()));
 }
 #endif

diff  --git a/llvm/unittests/Support/FileUtilitiesTest.cpp b/llvm/unittests/Support/FileUtilitiesTest.cpp
index cf1453b33d80..ff973e235965 100644
--- a/llvm/unittests/Support/FileUtilitiesTest.cpp
+++ b/llvm/unittests/Support/FileUtilitiesTest.cpp
@@ -12,12 +12,15 @@
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/Path.h"
+#include "llvm/Testing/Support/SupportHelpers.h"
 #include "gtest/gtest.h"
 #include <fstream>
 
 using namespace llvm;
 using namespace llvm::sys;
 
+using llvm::unittest::TempDir;
+
 #define ASSERT_NO_ERROR(x)                                                     \
   if (std::error_code ASSERT_NO_ERROR_ec = x) {                                \
     SmallString<128> MessageStorage;                                           \
@@ -32,11 +35,9 @@ using namespace llvm::sys;
 namespace {
 TEST(writeFileAtomicallyTest, Test) {
   // Create unique temporary directory for these tests
-  SmallString<128> RootTestDirectory;
-  ASSERT_NO_ERROR(
-    fs::createUniqueDirectory("writeFileAtomicallyTest", RootTestDirectory));
+  TempDir RootTestDirectory("writeFileAtomicallyTest", /*Unique*/ true);
 
-  SmallString<128> FinalTestfilePath(RootTestDirectory);
+  SmallString<128> FinalTestfilePath(RootTestDirectory.path());
   sys::path::append(FinalTestfilePath, "foo.txt");
   const std::string TempUniqTestFileModel =
       std::string(FinalTestfilePath) + "-%%%%%%%%";

diff  --git a/llvm/unittests/Support/LockFileManagerTest.cpp b/llvm/unittests/Support/LockFileManagerTest.cpp
index 5f1fe0fd3aea..587e442be196 100644
--- a/llvm/unittests/Support/LockFileManagerTest.cpp
+++ b/llvm/unittests/Support/LockFileManagerTest.cpp
@@ -9,20 +9,19 @@
 #include "llvm/Support/LockFileManager.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/Path.h"
+#include "llvm/Testing/Support/SupportHelpers.h"
 #include "gtest/gtest.h"
 #include <memory>
 
 using namespace llvm;
+using llvm::unittest::TempDir;
 
 namespace {
 
 TEST(LockFileManagerTest, Basic) {
-  SmallString<64> TmpDir;
-  std::error_code EC;
-  EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir);
-  ASSERT_FALSE(EC);
+  TempDir TmpDir("LockFileManagerTestDir", /*Unique*/ true);
 
-  SmallString<64> LockedFile(TmpDir);
+  SmallString<64> LockedFile(TmpDir.path());
   sys::path::append(LockedFile, "file.lock");
 
   {
@@ -38,28 +37,22 @@ TEST(LockFileManagerTest, Basic) {
 
   // Now that the lock is out of scope, the file should be gone.
   EXPECT_FALSE(sys::fs::exists(StringRef(LockedFile)));
-
-  EC = sys::fs::remove(StringRef(TmpDir));
-  ASSERT_FALSE(EC);
 }
 
 TEST(LockFileManagerTest, LinkLockExists) {
-  SmallString<64> TmpDir;
-  std::error_code EC;
-  EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir);
-  ASSERT_FALSE(EC);
+  TempDir LockFileManagerTestDir("LockFileManagerTestDir", /*Unique*/ true);
 
-  SmallString<64> LockedFile(TmpDir);
+  SmallString<64> LockedFile(LockFileManagerTestDir.path());
   sys::path::append(LockedFile, "file");
 
-  SmallString<64> FileLocK(TmpDir);
+  SmallString<64> FileLocK(LockFileManagerTestDir.path());
   sys::path::append(FileLocK, "file.lock");
 
-  SmallString<64> TmpFileLock(TmpDir);
+  SmallString<64> TmpFileLock(LockFileManagerTestDir.path());
   sys::path::append(TmpFileLock, "file.lock-000");
 
   int FD;
-  EC = sys::fs::openFileForWrite(StringRef(TmpFileLock), FD);
+  std::error_code EC = sys::fs::openFileForWrite(StringRef(TmpFileLock), FD);
   ASSERT_FALSE(EC);
 
   int Ret = close(FD);
@@ -80,24 +73,18 @@ TEST(LockFileManagerTest, LinkLockExists) {
 
   // Now that the lock is out of scope, the file should be gone.
   EXPECT_FALSE(sys::fs::exists(StringRef(LockedFile)));
-
-  EC = sys::fs::remove(StringRef(TmpDir));
-  ASSERT_FALSE(EC);
 }
 
 
 TEST(LockFileManagerTest, RelativePath) {
-  SmallString<64> TmpDir;
-  std::error_code EC;
-  EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir);
-  ASSERT_FALSE(EC);
+  TempDir LockFileManagerTestDir("LockFileManagerTestDir", /*Unique*/ true);
 
   char PathBuf[1024];
   const char *OrigPath = getcwd(PathBuf, 1024);
-  ASSERT_FALSE(chdir(TmpDir.c_str()));
+  ASSERT_FALSE(chdir(LockFileManagerTestDir.path().data()));
 
-  sys::fs::create_directory("inner");
-  SmallString<64> LockedFile("inner");
+  TempDir inner("inner");
+  SmallString<64> LockedFile(inner.path());
   sys::path::append(LockedFile, "file");
 
   SmallString<64> FileLock(LockedFile);
@@ -114,13 +101,7 @@ TEST(LockFileManagerTest, RelativePath) {
   EXPECT_FALSE(sys::fs::exists(LockedFile.str()));
   EXPECT_FALSE(sys::fs::exists(FileLock.str()));
 
-  EC = sys::fs::remove("inner");
-  ASSERT_FALSE(EC);
-
   ASSERT_FALSE(chdir(OrigPath));
-
-  EC = sys::fs::remove(StringRef(TmpDir));
-  ASSERT_FALSE(EC);
 }
 
 } // end anonymous namespace

diff  --git a/llvm/unittests/Support/TarWriterTest.cpp b/llvm/unittests/Support/TarWriterTest.cpp
index 579c06d75472..5a7d901a2e9d 100644
--- a/llvm/unittests/Support/TarWriterTest.cpp
+++ b/llvm/unittests/Support/TarWriterTest.cpp
@@ -9,10 +9,13 @@
 #include "llvm/Support/TarWriter.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Testing/Support/SupportHelpers.h"
 #include "gtest/gtest.h"
 #include <vector>
 
 using namespace llvm;
+using llvm::unittest::TempFile;
+
 namespace {
 
 struct UstarHeader {
@@ -38,21 +41,19 @@ struct UstarHeader {
 class TarWriterTest : public ::testing::Test {};
 
 static std::vector<uint8_t> createTar(StringRef Base, StringRef Filename) {
-  // Create a temporary file.
-  SmallString<128> Path;
-  std::error_code EC =
-      sys::fs::createTemporaryFile("TarWriterTest", "tar", Path);
-  EXPECT_FALSE((bool)EC);
+  TempFile TarWriterTest("TarWriterTest", "tar", "", /*Unique*/ true);
 
   // Create a tar file.
-  Expected<std::unique_ptr<TarWriter>> TarOrErr = TarWriter::create(Path, Base);
+  Expected<std::unique_ptr<TarWriter>> TarOrErr =
+      TarWriter::create(TarWriterTest.path(), Base);
   EXPECT_TRUE((bool)TarOrErr);
   std::unique_ptr<TarWriter> Tar = std::move(*TarOrErr);
   Tar->append(Filename, "contents");
   Tar.reset();
 
   // Read the tar file.
-  ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
+  ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
+      MemoryBuffer::getFile(TarWriterTest.path());
   EXPECT_TRUE((bool)MBOrErr);
   std::unique_ptr<MemoryBuffer> MB = std::move(*MBOrErr);
   std::vector<uint8_t> Buf((const uint8_t *)MB->getBufferStart(),
@@ -61,7 +62,6 @@ static std::vector<uint8_t> createTar(StringRef Base, StringRef Filename) {
   // Windows does not allow us to remove a mmap'ed files, so
   // unmap first and then remove the temporary file.
   MB = nullptr;
-  sys::fs::remove(Path);
 
   return Buf;
 }
@@ -123,30 +123,26 @@ TEST_F(TarWriterTest, Pax) {
 }
 
 TEST_F(TarWriterTest, SingleFile) {
-  SmallString<128> Path;
-  std::error_code EC =
-      sys::fs::createTemporaryFile("TarWriterTest", "tar", Path);
-  EXPECT_FALSE((bool)EC);
+  TempFile TarWriterTest("TarWriterTest", "tar", "", /*Unique*/ true);
 
-  Expected<std::unique_ptr<TarWriter>> TarOrErr = TarWriter::create(Path, "");
+  Expected<std::unique_ptr<TarWriter>> TarOrErr =
+      TarWriter::create(TarWriterTest.path(), "");
   EXPECT_TRUE((bool)TarOrErr);
   std::unique_ptr<TarWriter> Tar = std::move(*TarOrErr);
   Tar->append("FooPath", "foo");
   Tar.reset();
 
   uint64_t TarSize;
-  EC = sys::fs::file_size(Path, TarSize);
+  std::error_code EC = sys::fs::file_size(TarWriterTest.path(), TarSize);
   EXPECT_FALSE((bool)EC);
   EXPECT_EQ(TarSize, 2048ULL);
 }
 
 TEST_F(TarWriterTest, NoDuplicate) {
-  SmallString<128> Path;
-  std::error_code EC =
-      sys::fs::createTemporaryFile("TarWriterTest", "tar", Path);
-  EXPECT_FALSE((bool)EC);
+  TempFile TarWriterTest("TarWriterTest", "tar", "", /*Unique*/ true);
 
-  Expected<std::unique_ptr<TarWriter>> TarOrErr = TarWriter::create(Path, "");
+  Expected<std::unique_ptr<TarWriter>> TarOrErr =
+      TarWriter::create(TarWriterTest.path(), "");
   EXPECT_TRUE((bool)TarOrErr);
   std::unique_ptr<TarWriter> Tar = std::move(*TarOrErr);
   Tar->append("FooPath", "foo");
@@ -154,18 +150,16 @@ TEST_F(TarWriterTest, NoDuplicate) {
   Tar.reset();
 
   uint64_t TarSize;
-  EC = sys::fs::file_size(Path, TarSize);
+  std::error_code EC = sys::fs::file_size(TarWriterTest.path(), TarSize);
   EXPECT_FALSE((bool)EC);
   EXPECT_EQ(TarSize, 3072ULL);
 }
 
 TEST_F(TarWriterTest, Duplicate) {
-  SmallString<128> Path;
-  std::error_code EC =
-      sys::fs::createTemporaryFile("TarWriterTest", "tar", Path);
-  EXPECT_FALSE((bool)EC);
+  TempFile TarWriterTest("TarWriterTest", "tar", "", /*Unique*/ true);
 
-  Expected<std::unique_ptr<TarWriter>> TarOrErr = TarWriter::create(Path, "");
+  Expected<std::unique_ptr<TarWriter>> TarOrErr =
+      TarWriter::create(TarWriterTest.path(), "");
   EXPECT_TRUE((bool)TarOrErr);
   std::unique_ptr<TarWriter> Tar = std::move(*TarOrErr);
   Tar->append("FooPath", "foo");
@@ -173,7 +167,7 @@ TEST_F(TarWriterTest, Duplicate) {
   Tar.reset();
 
   uint64_t TarSize;
-  EC = sys::fs::file_size(Path, TarSize);
+  std::error_code EC = sys::fs::file_size(TarWriterTest.path(), TarSize);
   EXPECT_FALSE((bool)EC);
   EXPECT_EQ(TarSize, 2048ULL);
 }

diff  --git a/llvm/unittests/Support/VirtualFileSystemTest.cpp b/llvm/unittests/Support/VirtualFileSystemTest.cpp
index 85e7093514b3..64982b9e2160 100644
--- a/llvm/unittests/Support/VirtualFileSystemTest.cpp
+++ b/llvm/unittests/Support/VirtualFileSystemTest.cpp
@@ -14,6 +14,7 @@
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/SourceMgr.h"
+#include "llvm/Testing/Support/SupportHelpers.h"
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
 #include <map>
@@ -21,6 +22,9 @@
 
 using namespace llvm;
 using llvm::sys::fs::UniqueID;
+using llvm::unittest::TempDir;
+using llvm::unittest::TempFile;
+using llvm::unittest::TempLink;
 using testing::ElementsAre;
 using testing::Pair;
 using testing::UnorderedElementsAre;
@@ -410,87 +414,21 @@ TEST(VirtualFileSystemTest, OverlayIterator) {
   }
 }
 
-namespace {
-struct ScopedDir {
-  SmallString<128> Path;
-  ScopedDir(const Twine &Name, bool Unique = false) {
-    std::error_code EC;
-    if (Unique) {
-      EC = llvm::sys::fs::createUniqueDirectory(Name, Path);
-      if (!EC) {
-        // Resolve any symlinks in the new directory.
-        std::string UnresolvedPath = std::string(Path.str());
-        EC = llvm::sys::fs::real_path(UnresolvedPath, Path);
-      }
-    } else {
-      Path = Name.str();
-      EC = llvm::sys::fs::create_directory(Twine(Path));
-    }
-    if (EC)
-      Path = "";
-    EXPECT_FALSE(EC) << EC.message();
-  }
-  ~ScopedDir() {
-    if (Path != "") {
-      EXPECT_FALSE(llvm::sys::fs::remove(Path.str()));
-    }
-  }
-  operator StringRef() { return Path.str(); }
-};
-
-struct ScopedLink {
-  SmallString<128> Path;
-  ScopedLink(const Twine &To, const Twine &From) {
-    Path = From.str();
-    std::error_code EC = sys::fs::create_link(To, From);
-    if (EC)
-      Path = "";
-    EXPECT_FALSE(EC);
-  }
-  ~ScopedLink() {
-    if (Path != "") {
-      EXPECT_FALSE(llvm::sys::fs::remove(Path.str()));
-    }
-  }
-  operator StringRef() { return Path.str(); }
-};
-
-struct ScopedFile {
-  SmallString<128> Path;
-  ScopedFile(const Twine &Path, StringRef Contents) {
-    Path.toVector(this->Path);
-    std::error_code EC;
-    raw_fd_ostream OS(this->Path, EC);
-    EXPECT_FALSE(EC);
-    OS << Contents;
-    OS.flush();
-    EXPECT_FALSE(OS.error());
-    if (EC || OS.error())
-      this->Path = "";
-  }
-  ~ScopedFile() {
-    if (Path != "") {
-      EXPECT_FALSE(llvm::sys::fs::remove(Path.str()));
-    }
-  }
-};
-} // end anonymous namespace
-
 TEST(VirtualFileSystemTest, BasicRealFSIteration) {
-  ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/ true);
+  TempDir TestDirectory("virtual-file-system-test", /*Unique*/ true);
   IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getRealFileSystem();
 
   std::error_code EC;
-  vfs::directory_iterator I = FS->dir_begin(Twine(TestDirectory), EC);
+  vfs::directory_iterator I = FS->dir_begin(Twine(TestDirectory.path()), EC);
   ASSERT_FALSE(EC);
   EXPECT_EQ(vfs::directory_iterator(), I); // empty directory is empty
 
-  ScopedDir _a(TestDirectory + "/a");
-  ScopedDir _ab(TestDirectory + "/a/b");
-  ScopedDir _c(TestDirectory + "/c");
-  ScopedDir _cd(TestDirectory + "/c/d");
+  TempDir _a(TestDirectory.path("a"));
+  TempDir _ab(TestDirectory.path("a/b"));
+  TempDir _c(TestDirectory.path("c"));
+  TempDir _cd(TestDirectory.path("c/d"));
 
-  I = FS->dir_begin(Twine(TestDirectory), EC);
+  I = FS->dir_begin(Twine(TestDirectory.path()), EC);
   ASSERT_FALSE(EC);
   ASSERT_NE(vfs::directory_iterator(), I);
   // Check either a or c, since we can't rely on the iteration order.
@@ -508,16 +446,19 @@ TEST(VirtualFileSystemTest, MultipleWorkingDirs) {
   // Our root contains a/aa, b/bb, c, where c is a link to a/.
   // Run tests both in root/b/ and root/c/ (to test "normal" and symlink dirs).
   // Interleave operations to show the working directories are independent.
-  ScopedDir Root("r", true), ADir(Root.Path + "/a"), BDir(Root.Path + "/b");
-  ScopedLink C(ADir.Path, Root.Path + "/c");
-  ScopedFile AA(ADir.Path + "/aa", "aaaa"), BB(BDir.Path + "/bb", "bbbb");
+  TempDir Root("r", /*Unique*/ true);
+  TempDir ADir(Root.path("a"));
+  TempDir BDir(Root.path("b"));
+  TempLink C(ADir.path(), Root.path("c"));
+  TempFile AA(ADir.path("aa"), "", "aaaa");
+  TempFile BB(BDir.path("bb"), "", "bbbb");
   std::unique_ptr<vfs::FileSystem> BFS = vfs::createPhysicalFileSystem(),
                                    CFS = vfs::createPhysicalFileSystem();
 
-  ASSERT_FALSE(BFS->setCurrentWorkingDirectory(BDir.Path));
-  ASSERT_FALSE(CFS->setCurrentWorkingDirectory(C.Path));
-  EXPECT_EQ(BDir.Path, *BFS->getCurrentWorkingDirectory());
-  EXPECT_EQ(C.Path, *CFS->getCurrentWorkingDirectory());
+  ASSERT_FALSE(BFS->setCurrentWorkingDirectory(BDir.path()));
+  ASSERT_FALSE(CFS->setCurrentWorkingDirectory(C.path()));
+  EXPECT_EQ(BDir.path(), *BFS->getCurrentWorkingDirectory());
+  EXPECT_EQ(C.path(), *CFS->getCurrentWorkingDirectory());
 
   // openFileForRead(), indirectly.
   auto BBuf = BFS->getBufferForFile("bb");
@@ -540,18 +481,18 @@ TEST(VirtualFileSystemTest, MultipleWorkingDirs) {
   // getRealPath()
   SmallString<128> BPath;
   ASSERT_FALSE(BFS->getRealPath("bb", BPath));
-  EXPECT_EQ(BB.Path, BPath);
+  EXPECT_EQ(BB.path(), BPath);
 
   SmallString<128> APath;
   ASSERT_FALSE(CFS->getRealPath("aa", APath));
-  EXPECT_EQ(AA.Path, APath); // Reports resolved name.
+  EXPECT_EQ(AA.path(), APath); // Reports resolved name.
 
   // dir_begin
   std::error_code EC;
   auto BIt = BFS->dir_begin(".", EC);
   ASSERT_FALSE(EC);
   ASSERT_NE(BIt, vfs::directory_iterator());
-  EXPECT_EQ((BDir.Path + "/./bb").str(), BIt->path());
+  EXPECT_EQ((BDir.path() + "/./bb").str(), BIt->path());
   BIt.increment(EC);
   ASSERT_FALSE(EC);
   ASSERT_EQ(BIt, vfs::directory_iterator());
@@ -559,24 +500,27 @@ TEST(VirtualFileSystemTest, MultipleWorkingDirs) {
   auto CIt = CFS->dir_begin(".", EC);
   ASSERT_FALSE(EC);
   ASSERT_NE(CIt, vfs::directory_iterator());
-  EXPECT_EQ((ADir.Path + "/./aa").str(), CIt->path()); // Partly resolved name!
+  EXPECT_EQ((ADir.path() + "/./aa").str(),
+            CIt->path()); // Partly resolved name!
   CIt.increment(EC); // Because likely to read through this path.
   ASSERT_FALSE(EC);
   ASSERT_EQ(CIt, vfs::directory_iterator());
 }
 
 TEST(VirtualFileSystemTest, BrokenSymlinkRealFSIteration) {
-  ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/ true);
+  TempDir TestDirectory("virtual-file-system-test", /*Unique*/ true);
   IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getRealFileSystem();
 
-  ScopedLink _a("no_such_file", TestDirectory + "/a");
-  ScopedDir _b(TestDirectory + "/b");
-  ScopedLink _c("no_such_file", TestDirectory + "/c");
+  TempLink _a("no_such_file", TestDirectory.path("a"));
+  TempDir _b(TestDirectory.path("b"));
+  TempLink _c("no_such_file", TestDirectory.path("c"));
 
   // Should get no iteration error, but a stat error for the broken symlinks.
   std::map<std::string, std::error_code> StatResults;
   std::error_code EC;
-  for (vfs::directory_iterator I = FS->dir_begin(Twine(TestDirectory), EC), E;
+  for (vfs::directory_iterator
+           I = FS->dir_begin(Twine(TestDirectory.path()), EC),
+           E;
        I != E; I.increment(EC)) {
     EXPECT_FALSE(EC);
     StatResults[std::string(sys::path::filename(I->path()))] =
@@ -593,20 +537,21 @@ TEST(VirtualFileSystemTest, BrokenSymlinkRealFSIteration) {
 #endif
 
 TEST(VirtualFileSystemTest, BasicRealFSRecursiveIteration) {
-  ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/ true);
+  TempDir TestDirectory("virtual-file-system-test", /*Unique*/ true);
   IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getRealFileSystem();
 
   std::error_code EC;
-  auto I = vfs::recursive_directory_iterator(*FS, Twine(TestDirectory), EC);
+  auto I =
+      vfs::recursive_directory_iterator(*FS, Twine(TestDirectory.path()), EC);
   ASSERT_FALSE(EC);
   EXPECT_EQ(vfs::recursive_directory_iterator(), I); // empty directory is empty
 
-  ScopedDir _a(TestDirectory + "/a");
-  ScopedDir _ab(TestDirectory + "/a/b");
-  ScopedDir _c(TestDirectory + "/c");
-  ScopedDir _cd(TestDirectory + "/c/d");
+  TempDir _a(TestDirectory.path("a"));
+  TempDir _ab(TestDirectory.path("a/b"));
+  TempDir _c(TestDirectory.path("c"));
+  TempDir _cd(TestDirectory.path("c/d"));
 
-  I = vfs::recursive_directory_iterator(*FS, Twine(TestDirectory), EC);
+  I = vfs::recursive_directory_iterator(*FS, Twine(TestDirectory.path()), EC);
   ASSERT_FALSE(EC);
   ASSERT_NE(vfs::recursive_directory_iterator(), I);
 
@@ -632,22 +577,23 @@ TEST(VirtualFileSystemTest, BasicRealFSRecursiveIteration) {
 }
 
 TEST(VirtualFileSystemTest, BasicRealFSRecursiveIterationNoPush) {
-  ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/ true);
+  TempDir TestDirectory("virtual-file-system-test", /*Unique*/ true);
 
-  ScopedDir _a(TestDirectory + "/a");
-  ScopedDir _ab(TestDirectory + "/a/b");
-  ScopedDir _c(TestDirectory + "/c");
-  ScopedDir _cd(TestDirectory + "/c/d");
-  ScopedDir _e(TestDirectory + "/e");
-  ScopedDir _ef(TestDirectory + "/e/f");
-  ScopedDir _g(TestDirectory + "/g");
+  TempDir _a(TestDirectory.path("a"));
+  TempDir _ab(TestDirectory.path("a/b"));
+  TempDir _c(TestDirectory.path("c"));
+  TempDir _cd(TestDirectory.path("c/d"));
+  TempDir _e(TestDirectory.path("e"));
+  TempDir _ef(TestDirectory.path("e/f"));
+  TempDir _g(TestDirectory.path("g"));
 
   IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getRealFileSystem();
 
   // Test that calling no_push on entries without subdirectories has no effect.
   {
     std::error_code EC;
-    auto I = vfs::recursive_directory_iterator(*FS, Twine(TestDirectory), EC);
+    auto I =
+        vfs::recursive_directory_iterator(*FS, Twine(TestDirectory.path()), EC);
     ASSERT_FALSE(EC);
 
     std::vector<std::string> Contents;
@@ -672,7 +618,8 @@ TEST(VirtualFileSystemTest, BasicRealFSRecursiveIterationNoPush) {
   // Test that calling no_push skips subdirectories.
   {
     std::error_code EC;
-    auto I = vfs::recursive_directory_iterator(*FS, Twine(TestDirectory), EC);
+    auto I =
+        vfs::recursive_directory_iterator(*FS, Twine(TestDirectory.path()), EC);
     ASSERT_FALSE(EC);
 
     std::vector<std::string> Contents;
@@ -712,24 +659,26 @@ TEST(VirtualFileSystemTest, BasicRealFSRecursiveIterationNoPush) {
 
 #ifdef LLVM_ON_UNIX
 TEST(VirtualFileSystemTest, BrokenSymlinkRealFSRecursiveIteration) {
-  ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/ true);
+  TempDir TestDirectory("virtual-file-system-test", /*Unique*/ true);
   IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getRealFileSystem();
 
-  ScopedLink _a("no_such_file", TestDirectory + "/a");
-  ScopedDir _b(TestDirectory + "/b");
-  ScopedLink _ba("no_such_file", TestDirectory + "/b/a");
-  ScopedDir _bb(TestDirectory + "/b/b");
-  ScopedLink _bc("no_such_file", TestDirectory + "/b/c");
-  ScopedLink _c("no_such_file", TestDirectory + "/c");
-  ScopedDir _d(TestDirectory + "/d");
-  ScopedDir _dd(TestDirectory + "/d/d");
-  ScopedDir _ddd(TestDirectory + "/d/d/d");
-  ScopedLink _e("no_such_file", TestDirectory + "/e");
+  TempLink _a("no_such_file", TestDirectory.path("a"));
+  TempDir _b(TestDirectory.path("b"));
+  TempLink _ba("no_such_file", TestDirectory.path("b/a"));
+  TempDir _bb(TestDirectory.path("b/b"));
+  TempLink _bc("no_such_file", TestDirectory.path("b/c"));
+  TempLink _c("no_such_file", TestDirectory.path("c"));
+  TempDir _d(TestDirectory.path("d"));
+  TempDir _dd(TestDirectory.path("d/d"));
+  TempDir _ddd(TestDirectory.path("d/d/d"));
+  TempLink _e("no_such_file", TestDirectory.path("e"));
 
   std::vector<std::string> VisitedBrokenSymlinks;
   std::vector<std::string> VisitedNonBrokenSymlinks;
   std::error_code EC;
-  for (vfs::recursive_directory_iterator I(*FS, Twine(TestDirectory), EC), E;
+  for (vfs::recursive_directory_iterator
+           I(*FS, Twine(TestDirectory.path()), EC),
+       E;
        I != E; I.increment(EC)) {
     EXPECT_FALSE(EC);
     (FS->status(I->path()) ? VisitedNonBrokenSymlinks : VisitedBrokenSymlinks)
@@ -738,13 +687,13 @@ TEST(VirtualFileSystemTest, BrokenSymlinkRealFSRecursiveIteration) {
 
   // Check visited file names.
   EXPECT_THAT(VisitedBrokenSymlinks,
-              UnorderedElementsAre(StringRef(_a).str(), StringRef(_ba).str(),
-                                   StringRef(_bc).str(), StringRef(_c).str(),
-                                   StringRef(_e).str()));
+              UnorderedElementsAre(_a.path().str(), _ba.path().str(),
+                                   _bc.path().str(), _c.path().str(),
+                                   _e.path().str()));
   EXPECT_THAT(VisitedNonBrokenSymlinks,
-              UnorderedElementsAre(StringRef(_b).str(), StringRef(_bb).str(),
-                                   StringRef(_d).str(), StringRef(_dd).str(),
-                                   StringRef(_ddd).str()));
+              UnorderedElementsAre(_b.path().str(), _bb.path().str(),
+                                   _d.path().str(), _dd.path().str(),
+                                   _ddd.path().str()));
 }
 #endif
 
@@ -2190,24 +2139,24 @@ TEST_F(VFSFromYAMLTest, WorkingDirectoryFallthroughInvalid) {
 }
 
 TEST_F(VFSFromYAMLTest, YAMLVFSWriterTest) {
-  ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/ true);
-  ScopedDir _a(TestDirectory + "/a");
-  ScopedFile _ab(TestDirectory + "/a/b", "");
-  ScopedDir _c(TestDirectory + "/c");
-  ScopedFile _cd(TestDirectory + "/c/d", "");
-  ScopedDir _e(TestDirectory + "/e");
-  ScopedDir _ef(TestDirectory + "/e/f");
-  ScopedFile _g(TestDirectory + "/g", "");
-  ScopedDir _h(TestDirectory + "/h");
+  TempDir TestDirectory("virtual-file-system-test", /*Unique*/ true);
+  TempDir _a(TestDirectory.path("a"));
+  TempFile _ab(TestDirectory.path("a, b"));
+  TempDir _c(TestDirectory.path("c"));
+  TempFile _cd(TestDirectory.path("c/d"));
+  TempDir _e(TestDirectory.path("e"));
+  TempDir _ef(TestDirectory.path("e/f"));
+  TempFile _g(TestDirectory.path("g"));
+  TempDir _h(TestDirectory.path("h"));
 
   vfs::YAMLVFSWriter VFSWriter;
-  VFSWriter.addDirectoryMapping(_a.Path, "//root/a");
-  VFSWriter.addFileMapping(_ab.Path, "//root/a/b");
-  VFSWriter.addFileMapping(_cd.Path, "//root/c/d");
-  VFSWriter.addDirectoryMapping(_e.Path, "//root/e");
-  VFSWriter.addDirectoryMapping(_ef.Path, "//root/e/f");
-  VFSWriter.addFileMapping(_g.Path, "//root/g");
-  VFSWriter.addDirectoryMapping(_h.Path, "//root/h");
+  VFSWriter.addDirectoryMapping(_a.path(), "//root/a");
+  VFSWriter.addFileMapping(_ab.path(), "//root/a/b");
+  VFSWriter.addFileMapping(_cd.path(), "//root/c/d");
+  VFSWriter.addDirectoryMapping(_e.path(), "//root/e");
+  VFSWriter.addDirectoryMapping(_ef.path(), "//root/e/f");
+  VFSWriter.addFileMapping(_g.path(), "//root/g");
+  VFSWriter.addDirectoryMapping(_h.path(), "//root/h");
 
   std::string Buffer;
   raw_string_ostream OS(Buffer);
@@ -2229,36 +2178,36 @@ TEST_F(VFSFromYAMLTest, YAMLVFSWriterTest) {
   IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLRawString(Buffer, Lower);
   ASSERT_TRUE(FS.get() != nullptr);
 
-  EXPECT_TRUE(FS->exists(_a.Path));
-  EXPECT_TRUE(FS->exists(_ab.Path));
-  EXPECT_TRUE(FS->exists(_c.Path));
-  EXPECT_TRUE(FS->exists(_cd.Path));
-  EXPECT_TRUE(FS->exists(_e.Path));
-  EXPECT_TRUE(FS->exists(_ef.Path));
-  EXPECT_TRUE(FS->exists(_g.Path));
-  EXPECT_TRUE(FS->exists(_h.Path));
+  EXPECT_TRUE(FS->exists(_a.path()));
+  EXPECT_TRUE(FS->exists(_ab.path()));
+  EXPECT_TRUE(FS->exists(_c.path()));
+  EXPECT_TRUE(FS->exists(_cd.path()));
+  EXPECT_TRUE(FS->exists(_e.path()));
+  EXPECT_TRUE(FS->exists(_ef.path()));
+  EXPECT_TRUE(FS->exists(_g.path()));
+  EXPECT_TRUE(FS->exists(_h.path()));
 }
 
 TEST_F(VFSFromYAMLTest, YAMLVFSWriterTest2) {
-  ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/ true);
-  ScopedDir _a(TestDirectory + "/a");
-  ScopedFile _ab(TestDirectory + "/a/b", "");
-  ScopedDir _ac(TestDirectory + "/a/c");
-  ScopedFile _acd(TestDirectory + "/a/c/d", "");
-  ScopedFile _ace(TestDirectory + "/a/c/e", "");
-  ScopedFile _acf(TestDirectory + "/a/c/f", "");
-  ScopedDir _ag(TestDirectory + "/a/g");
-  ScopedFile _agh(TestDirectory + "/a/g/h", "");
+  TempDir TestDirectory("virtual-file-system-test", /*Unique*/ true);
+  TempDir _a(TestDirectory.path("a"));
+  TempFile _ab(TestDirectory.path("a/b"));
+  TempDir _ac(TestDirectory.path("a/c"));
+  TempFile _acd(TestDirectory.path("a/c/d"));
+  TempFile _ace(TestDirectory.path("a/c/e"));
+  TempFile _acf(TestDirectory.path("a/c/f"));
+  TempDir _ag(TestDirectory.path("a/g"));
+  TempFile _agh(TestDirectory.path("a/g/h"));
 
   vfs::YAMLVFSWriter VFSWriter;
-  VFSWriter.addDirectoryMapping(_a.Path, "//root/a");
-  VFSWriter.addFileMapping(_ab.Path, "//root/a/b");
-  VFSWriter.addDirectoryMapping(_ac.Path, "//root/a/c");
-  VFSWriter.addFileMapping(_acd.Path, "//root/a/c/d");
-  VFSWriter.addFileMapping(_ace.Path, "//root/a/c/e");
-  VFSWriter.addFileMapping(_acf.Path, "//root/a/c/f");
-  VFSWriter.addDirectoryMapping(_ag.Path, "//root/a/g");
-  VFSWriter.addFileMapping(_agh.Path, "//root/a/g/h");
+  VFSWriter.addDirectoryMapping(_a.path(), "//root/a");
+  VFSWriter.addFileMapping(_ab.path(), "//root/a/b");
+  VFSWriter.addDirectoryMapping(_ac.path(), "//root/a/c");
+  VFSWriter.addFileMapping(_acd.path(), "//root/a/c/d");
+  VFSWriter.addFileMapping(_ace.path(), "//root/a/c/e");
+  VFSWriter.addFileMapping(_acf.path(), "//root/a/c/f");
+  VFSWriter.addDirectoryMapping(_ag.path(), "//root/a/g");
+  VFSWriter.addFileMapping(_agh.path(), "//root/a/g/h");
 
   std::string Buffer;
   raw_string_ostream OS(Buffer);
@@ -2271,27 +2220,27 @@ TEST_F(VFSFromYAMLTest, YAMLVFSWriterTest2) {
 }
 
 TEST_F(VFSFromYAMLTest, YAMLVFSWriterTest3) {
-  ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/ true);
-  ScopedDir _a(TestDirectory + "/a");
-  ScopedFile _ab(TestDirectory + "/a/b", "");
-  ScopedDir _ac(TestDirectory + "/a/c");
-  ScopedDir _acd(TestDirectory + "/a/c/d");
-  ScopedDir _acde(TestDirectory + "/a/c/d/e");
-  ScopedFile _acdef(TestDirectory + "/a/c/d/e/f", "");
-  ScopedFile _acdeg(TestDirectory + "/a/c/d/e/g", "");
-  ScopedDir _ah(TestDirectory + "/a/h");
-  ScopedFile _ahi(TestDirectory + "/a/h/i", "");
+  TempDir TestDirectory("virtual-file-system-test", /*Unique*/ true);
+  TempDir _a(TestDirectory.path("a"));
+  TempFile _ab(TestDirectory.path("a/b"));
+  TempDir _ac(TestDirectory.path("a/c"));
+  TempDir _acd(TestDirectory.path("a/c/d"));
+  TempDir _acde(TestDirectory.path("a/c/d/e"));
+  TempFile _acdef(TestDirectory.path("a/c/d/e/f"));
+  TempFile _acdeg(TestDirectory.path("a/c/d/e/g"));
+  TempDir _ah(TestDirectory.path("a/h"));
+  TempFile _ahi(TestDirectory.path("a/h/i"));
 
   vfs::YAMLVFSWriter VFSWriter;
-  VFSWriter.addDirectoryMapping(_a.Path, "//root/a");
-  VFSWriter.addFileMapping(_ab.Path, "//root/a/b");
-  VFSWriter.addDirectoryMapping(_ac.Path, "//root/a/c");
-  VFSWriter.addDirectoryMapping(_acd.Path, "//root/a/c/d");
-  VFSWriter.addDirectoryMapping(_acde.Path, "//root/a/c/d/e");
-  VFSWriter.addFileMapping(_acdef.Path, "//root/a/c/d/e/f");
-  VFSWriter.addFileMapping(_acdeg.Path, "//root/a/c/d/e/g");
-  VFSWriter.addDirectoryMapping(_ahi.Path, "//root/a/h");
-  VFSWriter.addFileMapping(_ahi.Path, "//root/a/h/i");
+  VFSWriter.addDirectoryMapping(_a.path(), "//root/a");
+  VFSWriter.addFileMapping(_ab.path(), "//root/a/b");
+  VFSWriter.addDirectoryMapping(_ac.path(), "//root/a/c");
+  VFSWriter.addDirectoryMapping(_acd.path(), "//root/a/c/d");
+  VFSWriter.addDirectoryMapping(_acde.path(), "//root/a/c/d/e");
+  VFSWriter.addFileMapping(_acdef.path(), "//root/a/c/d/e/f");
+  VFSWriter.addFileMapping(_acdeg.path(), "//root/a/c/d/e/g");
+  VFSWriter.addDirectoryMapping(_ahi.path(), "//root/a/h");
+  VFSWriter.addFileMapping(_ahi.path(), "//root/a/h/i");
 
   std::string Buffer;
   raw_string_ostream OS(Buffer);
@@ -2304,15 +2253,15 @@ TEST_F(VFSFromYAMLTest, YAMLVFSWriterTest3) {
 }
 
 TEST_F(VFSFromYAMLTest, YAMLVFSWriterTestHandleDirs) {
-  ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/ true);
-  ScopedDir _a(TestDirectory + "/a");
-  ScopedDir _b(TestDirectory + "/b");
-  ScopedDir _c(TestDirectory + "/c");
+  TempDir TestDirectory("virtual-file-system-test", /*Unique*/ true);
+  TempDir _a(TestDirectory.path("a"));
+  TempDir _b(TestDirectory.path("b"));
+  TempDir _c(TestDirectory.path("c"));
 
   vfs::YAMLVFSWriter VFSWriter;
-  VFSWriter.addDirectoryMapping(_a.Path, "//root/a");
-  VFSWriter.addDirectoryMapping(_b.Path, "//root/b");
-  VFSWriter.addDirectoryMapping(_c.Path, "//root/c");
+  VFSWriter.addDirectoryMapping(_a.path(), "//root/a");
+  VFSWriter.addDirectoryMapping(_b.path(), "//root/b");
+  VFSWriter.addDirectoryMapping(_c.path(), "//root/c");
 
   std::string Buffer;
   raw_string_ostream OS(Buffer);
@@ -2334,7 +2283,7 @@ TEST_F(VFSFromYAMLTest, YAMLVFSWriterTestHandleDirs) {
   IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLRawString(Buffer, Lower);
   ASSERT_TRUE(FS.get() != nullptr);
 
-  EXPECT_FALSE(FS->exists(_a.Path + "/a"));
-  EXPECT_FALSE(FS->exists(_b.Path + "/b"));
-  EXPECT_FALSE(FS->exists(_c.Path + "/c"));
+  EXPECT_FALSE(FS->exists(_a.path("a")));
+  EXPECT_FALSE(FS->exists(_b.path("b")));
+  EXPECT_FALSE(FS->exists(_c.path("c")));
 }

diff  --git a/llvm/unittests/tools/llvm-exegesis/Mips/BenchmarkResultTest.cpp b/llvm/unittests/tools/llvm-exegesis/Mips/BenchmarkResultTest.cpp
index fcbb77063f01..3353de00b44f 100644
--- a/llvm/unittests/tools/llvm-exegesis/Mips/BenchmarkResultTest.cpp
+++ b/llvm/unittests/tools/llvm-exegesis/Mips/BenchmarkResultTest.cpp
@@ -16,6 +16,7 @@
 #include "llvm/Support/TargetSelect.h"
 #include "llvm/Support/YAMLTraits.h"
 #include "llvm/Support/raw_ostream.h"
+#include "llvm/Testing/Support/SupportHelpers.h"
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
 
@@ -25,6 +26,8 @@ using ::testing::get;
 using ::testing::Pointwise;
 using ::testing::Property;
 
+using llvm::unittest::TempDir;
+
 namespace llvm {
 namespace exegesis {
 
@@ -76,10 +79,8 @@ TEST_F(BenchmarkResultTest, WriteToAndReadFromDisk) {
   ToDisk.Error = "error";
   ToDisk.Info = "info";
 
-  SmallString<64> Filename;
-  std::error_code EC;
-  EC = sys::fs::createUniqueDirectory("BenchmarkResultTestDir", Filename);
-  ASSERT_FALSE(EC);
+  TempDir TestDirectory("BenchmarkResultTestDir", /*Unique*/ true);
+  SmallString<64> Filename(TestDirectory.path());
   sys::path::append(Filename, "data.yaml");
   errs() << Filename << "-------\n";
   ExitOnErr(ToDisk.writeYaml(State, Filename));

diff  --git a/llvm/unittests/tools/llvm-exegesis/X86/SnippetFileTest.cpp b/llvm/unittests/tools/llvm-exegesis/X86/SnippetFileTest.cpp
index da3974775be6..4bcd3f27f872 100644
--- a/llvm/unittests/tools/llvm-exegesis/X86/SnippetFileTest.cpp
+++ b/llvm/unittests/tools/llvm-exegesis/X86/SnippetFileTest.cpp
@@ -17,6 +17,7 @@
 #include "llvm/Support/TargetRegistry.h"
 #include "llvm/Support/TargetSelect.h"
 #include "llvm/Support/raw_ostream.h"
+#include "llvm/Testing/Support/SupportHelpers.h"
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
 
@@ -34,16 +35,17 @@ using testing::Field;
 using testing::Property;
 using testing::SizeIs;
 
+using llvm::unittest::TempDir;
+
 class X86SnippetFileTest : public X86TestBase {
 protected:
   Expected<std::vector<BenchmarkCode>> TestCommon(StringRef Contents) {
-    SmallString<64> Filename;
-    std::error_code EC;
-    EC = sys::fs::createUniqueDirectory("SnippetFileTestDir", Filename);
-    EXPECT_FALSE(EC);
+    TempDir TestDirectory("SnippetFileTestDir", /*Unique*/ true);
+    SmallString<64> Filename(TestDirectory.path());
     sys::path::append(Filename, "snippet.s");
     errs() << Filename << "-------\n";
     {
+      std::error_code EC;
       raw_fd_ostream FOS(Filename, EC);
       FOS << Contents;
       EXPECT_FALSE(EC);


        


More information about the llvm-commits mailing list