Support/FileSystem: Add remove implementation.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@120817 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Support/Unix/PathV2.inc b/lib/Support/Unix/PathV2.inc
index 985dde7..e6972f8 100644
--- a/lib/Support/Unix/PathV2.inc
+++ b/lib/Support/Unix/PathV2.inc
@@ -186,6 +186,20 @@
   return make_error_code(errc::success);
 }
 
+error_code remove(const Twine &path, bool &existed) {
+  SmallString<128> path_storage;
+  StringRef p = path.toNullTerminatedStringRef(path_storage);
+
+  if (::remove(p.begin()) == -1) {
+    if (errno != ENOENT)
+      return error_code(errno, system_category());
+    existed = false;
+  } else
+    existed = true;
+
+  return make_error_code(errc::success);
+}
+
 error_code exists(const Twine &path, bool &result) {
   SmallString<128> path_storage;
   StringRef p = path.toNullTerminatedStringRef(path_storage);
diff --git a/lib/Support/Windows/PathV2.inc b/lib/Support/Windows/PathV2.inc
index 98db5fd..c86c24e 100644
--- a/lib/Support/Windows/PathV2.inc
+++ b/lib/Support/Windows/PathV2.inc
@@ -252,6 +252,25 @@
   return make_error_code(errc::success);
 }
 
+error_code remove(const Twine &path, bool &existed) {
+  SmallString<128> path_storage;
+  SmallVector<wchar_t, 128> path_utf16;
+
+  if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
+                                  path_utf16))
+    return ec;
+
+  if (!::DeleteFileW(path_utf16.begin())) {
+    error_code ec = make_error_code(windows_error(::GetLastError()));
+    if (ec != make_error_code(windows_error::file_not_found))
+      return ec;
+    existed = false;
+  } else
+    existed = true;
+
+  return make_error_code(errc::success);
+}
+
 error_code exists(const Twine &path, bool &result) {
   SmallString<128> path_storage;
   SmallVector<wchar_t, 128> path_utf16;
diff --git a/unittests/Support/Path.cpp b/unittests/Support/Path.cpp
index d19bf4a..40e6281 100644
--- a/unittests/Support/Path.cpp
+++ b/unittests/Support/Path.cpp
@@ -128,7 +128,8 @@
   EXPECT_TRUE(TempFileExists);
 
   ::close(FileDescriptor);
-  ::remove(TempPath.c_str());
+  ASSERT_FALSE(fs::remove(Twine(TempPath), TempFileExists));
+  EXPECT_TRUE(TempFileExists);
 
   ASSERT_FALSE(fs::exists(Twine(TempPath), TempFileExists));
   EXPECT_FALSE(TempFileExists);