Hi danalbert,

With newlib as the libc, the following program would print out the same 
filename twice:

```
#include "support/platform_support.h"
#include <iostream>
int main() {
std::string temp1 = get_temp_file_name();
std::string temp2 = get_temp_file_name();
std::cout << temp1 << " " << temp2 << std::endl;
}
```

This breaks a few of the tests similar to 
input.output/file.streams/fstreams/filebuf.assign/member_swap.pass.cpp which 
expect the returned filenames to be different.

This patch fixes that bug by creating a TempFileName class which, on 
construction, is guaranteed to have a unique name.

Please double check the Windows side of the TempFileName class with extra 
scrutiny. I have neither tested, nor built this on Windows (and I don't have a 
machine to try it out on either).

http://reviews.llvm.org/D4962

Files:
  test/input.output/file.streams/fstreams/filebuf.assign/member_swap.pass.cpp
  test/input.output/file.streams/fstreams/filebuf.assign/move_assign.pass.cpp
  test/input.output/file.streams/fstreams/filebuf.assign/nonmember_swap.pass.cpp
  test/input.output/file.streams/fstreams/filebuf.cons/move.pass.cpp
  test/input.output/file.streams/fstreams/filebuf.members/open_pointer.pass.cpp
  test/input.output/file.streams/fstreams/fstream.assign/member_swap.pass.cpp
  test/input.output/file.streams/fstreams/fstream.assign/move_assign.pass.cpp
  test/input.output/file.streams/fstreams/fstream.assign/nonmember_swap.pass.cpp
  test/input.output/file.streams/fstreams/fstream.cons/move.pass.cpp
  test/input.output/file.streams/fstreams/fstream.cons/pointer.pass.cpp
  test/input.output/file.streams/fstreams/fstream.cons/string.pass.cpp
  test/input.output/file.streams/fstreams/fstream.members/close.pass.cpp
  test/input.output/file.streams/fstreams/fstream.members/open_pointer.pass.cpp
  test/input.output/file.streams/fstreams/fstream.members/open_string.pass.cpp
  test/input.output/file.streams/fstreams/ofstream.assign/member_swap.pass.cpp
  test/input.output/file.streams/fstreams/ofstream.assign/move_assign.pass.cpp
  
test/input.output/file.streams/fstreams/ofstream.assign/nonmember_swap.pass.cpp
  test/input.output/file.streams/fstreams/ofstream.cons/move.pass.cpp
  test/input.output/file.streams/fstreams/ofstream.cons/pointer.pass.cpp
  test/input.output/file.streams/fstreams/ofstream.cons/string.pass.cpp
  test/input.output/file.streams/fstreams/ofstream.members/close.pass.cpp
  test/input.output/file.streams/fstreams/ofstream.members/open_pointer.pass.cpp
  test/input.output/file.streams/fstreams/ofstream.members/open_string.pass.cpp
  test/input.output/file.streams/fstreams/ofstream.members/rdbuf.pass.cpp
  test/support/platform_support.h
Index: test/input.output/file.streams/fstreams/filebuf.assign/member_swap.pass.cpp
===================================================================
--- test/input.output/file.streams/fstreams/filebuf.assign/member_swap.pass.cpp
+++ test/input.output/file.streams/fstreams/filebuf.assign/member_swap.pass.cpp
@@ -20,35 +20,38 @@
 
 int main()
 {
-    std::string temp = get_temp_file_name();
     {
-        std::filebuf f;
-        assert(f.open(temp.c_str(), std::ios_base::out | std::ios_base::in
-                                               | std::ios_base::trunc) != 0);
-        assert(f.is_open());
-        assert(f.sputn("123", 3) == 3);
-        f.pubseekoff(1, std::ios_base::beg);
-        assert(f.sgetc() == '2');
-        std::filebuf f2;
-        f2.swap(f);
-        assert(!f.is_open());
-        assert(f2.is_open());
-        assert(f2.sgetc() == '2');
+        libcxx_support::TempFileName temp;
+        {
+            std::filebuf f;
+            assert(f.open(temp.c_str(), std::ios_base::out | std::ios_base::in
+                                                   | std::ios_base::trunc) != 0);
+            assert(f.is_open());
+            assert(f.sputn("123", 3) == 3);
+            f.pubseekoff(1, std::ios_base::beg);
+            assert(f.sgetc() == '2');
+            std::filebuf f2;
+            f2.swap(f);
+            assert(!f.is_open());
+            assert(f2.is_open());
+            assert(f2.sgetc() == '2');
+        }
     }
-    std::remove(temp.c_str());
     {
-        std::wfilebuf f;
-        assert(f.open(temp.c_str(), std::ios_base::out | std::ios_base::in
-                                               | std::ios_base::trunc) != 0);
-        assert(f.is_open());
-        assert(f.sputn(L"123", 3) == 3);
-        f.pubseekoff(1, std::ios_base::beg);
-        assert(f.sgetc() == L'2');
-        std::wfilebuf f2;
-        f2.swap(f);
-        assert(!f.is_open());
-        assert(f2.is_open());
-        assert(f2.sgetc() == L'2');
+        libcxx_support::TempFileName temp;
+        {
+            std::wfilebuf f;
+            assert(f.open(temp.c_str(), std::ios_base::out | std::ios_base::in
+                                                   | std::ios_base::trunc) != 0);
+            assert(f.is_open());
+            assert(f.sputn(L"123", 3) == 3);
+            f.pubseekoff(1, std::ios_base::beg);
+            assert(f.sgetc() == L'2');
+            std::wfilebuf f2;
+            f2.swap(f);
+            assert(!f.is_open());
+            assert(f2.is_open());
+            assert(f2.sgetc() == L'2');
+        }
     }
-    std::remove(temp.c_str());
 }
Index: test/input.output/file.streams/fstreams/filebuf.assign/move_assign.pass.cpp
===================================================================
--- test/input.output/file.streams/fstreams/filebuf.assign/move_assign.pass.cpp
+++ test/input.output/file.streams/fstreams/filebuf.assign/move_assign.pass.cpp
@@ -21,36 +21,39 @@
 int main()
 {
 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
-    std::string temp = get_temp_file_name();
     {
-        std::filebuf f;
-        assert(f.open(temp.c_str(), std::ios_base::out | std::ios_base::in
-                                               | std::ios_base::trunc) != 0);
-        assert(f.is_open());
-        assert(f.sputn("123", 3) == 3);
-        f.pubseekoff(1, std::ios_base::beg);
-        assert(f.sgetc() == '2');
-        std::filebuf f2;
-        f2 = move(f);
-        assert(!f.is_open());
-        assert(f2.is_open());
-        assert(f2.sgetc() == '2');
+        libcxx_support::TempFileName temp;
+        {
+            std::filebuf f;
+            assert(f.open(temp.c_str(), std::ios_base::out | std::ios_base::in
+                                                   | std::ios_base::trunc) != 0);
+            assert(f.is_open());
+            assert(f.sputn("123", 3) == 3);
+            f.pubseekoff(1, std::ios_base::beg);
+            assert(f.sgetc() == '2');
+            std::filebuf f2;
+            f2 = move(f);
+            assert(!f.is_open());
+            assert(f2.is_open());
+            assert(f2.sgetc() == '2');
+        }
     }
-    std::remove(temp.c_str());
     {
-        std::wfilebuf f;
-        assert(f.open(temp.c_str(), std::ios_base::out | std::ios_base::in
-                                               | std::ios_base::trunc) != 0);
-        assert(f.is_open());
-        assert(f.sputn(L"123", 3) == 3);
-        f.pubseekoff(1, std::ios_base::beg);
-        assert(f.sgetc() == L'2');
-        std::wfilebuf f2;
-        f2 = move(f);
-        assert(!f.is_open());
-        assert(f2.is_open());
-        assert(f2.sgetc() == L'2');
+        libcxx_support::TempFileName temp;
+        {
+            std::wfilebuf f;
+            assert(f.open(temp.c_str(), std::ios_base::out | std::ios_base::in
+                                                   | std::ios_base::trunc) != 0);
+            assert(f.is_open());
+            assert(f.sputn(L"123", 3) == 3);
+            f.pubseekoff(1, std::ios_base::beg);
+            assert(f.sgetc() == L'2');
+            std::wfilebuf f2;
+            f2 = move(f);
+            assert(!f.is_open());
+            assert(f2.is_open());
+            assert(f2.sgetc() == L'2');
+        }
     }
-    std::remove(temp.c_str());
 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
 }
Index: test/input.output/file.streams/fstreams/filebuf.assign/nonmember_swap.pass.cpp
===================================================================
--- test/input.output/file.streams/fstreams/filebuf.assign/nonmember_swap.pass.cpp
+++ test/input.output/file.streams/fstreams/filebuf.assign/nonmember_swap.pass.cpp
@@ -22,35 +22,38 @@
 
 int main()
 {
-    std::string temp = get_temp_file_name();
     {
-        std::filebuf f;
-        assert(f.open(temp.c_str(), std::ios_base::out | std::ios_base::in
-                                               | std::ios_base::trunc) != 0);
-        assert(f.is_open());
-        assert(f.sputn("123", 3) == 3);
-        f.pubseekoff(1, std::ios_base::beg);
-        assert(f.sgetc() == '2');
-        std::filebuf f2;
-        swap(f2, f);
-        assert(!f.is_open());
-        assert(f2.is_open());
-        assert(f2.sgetc() == '2');
+        libcxx_support::TempFileName temp;
+        {
+            std::filebuf f;
+            assert(f.open(temp.c_str(), std::ios_base::out | std::ios_base::in
+                                                   | std::ios_base::trunc) != 0);
+            assert(f.is_open());
+            assert(f.sputn("123", 3) == 3);
+            f.pubseekoff(1, std::ios_base::beg);
+            assert(f.sgetc() == '2');
+            std::filebuf f2;
+            swap(f2, f);
+            assert(!f.is_open());
+            assert(f2.is_open());
+            assert(f2.sgetc() == '2');
+        }
     }
-    std::remove(temp.c_str());
     {
-        std::wfilebuf f;
-        assert(f.open(temp.c_str(), std::ios_base::out | std::ios_base::in
-                                               | std::ios_base::trunc) != 0);
-        assert(f.is_open());
-        assert(f.sputn(L"123", 3) == 3);
-        f.pubseekoff(1, std::ios_base::beg);
-        assert(f.sgetc() == L'2');
-        std::wfilebuf f2;
-        swap(f2, f);
-        assert(!f.is_open());
-        assert(f2.is_open());
-        assert(f2.sgetc() == L'2');
+        libcxx_support::TempFileName temp;
+        {
+            std::wfilebuf f;
+            assert(f.open(temp.c_str(), std::ios_base::out | std::ios_base::in
+                                                   | std::ios_base::trunc) != 0);
+            assert(f.is_open());
+            assert(f.sputn(L"123", 3) == 3);
+            f.pubseekoff(1, std::ios_base::beg);
+            assert(f.sgetc() == L'2');
+            std::wfilebuf f2;
+            swap(f2, f);
+            assert(!f.is_open());
+            assert(f2.is_open());
+            assert(f2.sgetc() == L'2');
+        }
     }
-    std::remove(temp.c_str());
 }
Index: test/input.output/file.streams/fstreams/filebuf.cons/move.pass.cpp
===================================================================
--- test/input.output/file.streams/fstreams/filebuf.cons/move.pass.cpp
+++ test/input.output/file.streams/fstreams/filebuf.cons/move.pass.cpp
@@ -21,34 +21,37 @@
 int main()
 {
 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
-    std::string temp = get_temp_file_name();
     {
-        std::filebuf f;
-        assert(f.open(temp.c_str(), std::ios_base::out | std::ios_base::in
-                                               | std::ios_base::trunc) != 0);
-        assert(f.is_open());
-        assert(f.sputn("123", 3) == 3);
-        f.pubseekoff(1, std::ios_base::beg);
-        assert(f.sgetc() == '2');
-        std::filebuf f2(move(f));
-        assert(!f.is_open());
-        assert(f2.is_open());
-        assert(f2.sgetc() == '2');
+        libcxx_support::TempFileName temp;
+        {
+            std::filebuf f;
+            assert(f.open(temp.c_str(), std::ios_base::out | std::ios_base::in
+                                                   | std::ios_base::trunc) != 0);
+            assert(f.is_open());
+            assert(f.sputn("123", 3) == 3);
+            f.pubseekoff(1, std::ios_base::beg);
+            assert(f.sgetc() == '2');
+            std::filebuf f2(move(f));
+            assert(!f.is_open());
+            assert(f2.is_open());
+            assert(f2.sgetc() == '2');
+        }
     }
-    std::remove(temp.c_str());
     {
-        std::wfilebuf f;
-        assert(f.open(temp.c_str(), std::ios_base::out | std::ios_base::in
-                                               | std::ios_base::trunc) != 0);
-        assert(f.is_open());
-        assert(f.sputn(L"123", 3) == 3);
-        f.pubseekoff(1, std::ios_base::beg);
-        assert(f.sgetc() == L'2');
-        std::wfilebuf f2(move(f));
-        assert(!f.is_open());
-        assert(f2.is_open());
-        assert(f2.sgetc() == L'2');
+        libcxx_support::TempFileName temp;
+        {
+            std::wfilebuf f;
+            assert(f.open(temp.c_str(), std::ios_base::out | std::ios_base::in
+                                                   | std::ios_base::trunc) != 0);
+            assert(f.is_open());
+            assert(f.sputn(L"123", 3) == 3);
+            f.pubseekoff(1, std::ios_base::beg);
+            assert(f.sgetc() == L'2');
+            std::wfilebuf f2(move(f));
+            assert(!f.is_open());
+            assert(f2.is_open());
+            assert(f2.sgetc() == L'2');
+        }
     }
-    std::remove(temp.c_str());
 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
 }
Index: test/input.output/file.streams/fstreams/filebuf.members/open_pointer.pass.cpp
===================================================================
--- test/input.output/file.streams/fstreams/filebuf.members/open_pointer.pass.cpp
+++ test/input.output/file.streams/fstreams/filebuf.members/open_pointer.pass.cpp
@@ -17,35 +17,38 @@
 
 int main()
 {
-    std::string temp = get_temp_file_name();
     {
-        std::filebuf f;
-        assert(f.open(temp.c_str(), std::ios_base::out) != 0);
-        assert(f.is_open());
-        assert(f.sputn("123", 3) == 3);
+        libcxx_support::TempFileName temp;
+        {
+            std::filebuf f;
+            assert(f.open(temp.c_str(), std::ios_base::out) != 0);
+            assert(f.is_open());
+            assert(f.sputn("123", 3) == 3);
+        }
+        {
+            std::filebuf f;
+            assert(f.open(temp.c_str(), std::ios_base::in) != 0);
+            assert(f.is_open());
+            assert(f.sbumpc() == '1');
+            assert(f.sbumpc() == '2');
+            assert(f.sbumpc() == '3');
+        }
     }
     {
-        std::filebuf f;
-        assert(f.open(temp.c_str(), std::ios_base::in) != 0);
-        assert(f.is_open());
-        assert(f.sbumpc() == '1');
-        assert(f.sbumpc() == '2');
-        assert(f.sbumpc() == '3');
+        libcxx_support::TempFileName temp;
+        {
+            std::wfilebuf f;
+            assert(f.open(temp.c_str(), std::ios_base::out) != 0);
+            assert(f.is_open());
+            assert(f.sputn(L"123", 3) == 3);
+        }
+        {
+            std::wfilebuf f;
+            assert(f.open(temp.c_str(), std::ios_base::in) != 0);
+            assert(f.is_open());
+            assert(f.sbumpc() == L'1');
+            assert(f.sbumpc() == L'2');
+            assert(f.sbumpc() == L'3');
+        }
     }
-    std::remove(temp.c_str());
-    {
-        std::wfilebuf f;
-        assert(f.open(temp.c_str(), std::ios_base::out) != 0);
-        assert(f.is_open());
-        assert(f.sputn(L"123", 3) == 3);
-    }
-    {
-        std::wfilebuf f;
-        assert(f.open(temp.c_str(), std::ios_base::in) != 0);
-        assert(f.is_open());
-        assert(f.sbumpc() == L'1');
-        assert(f.sbumpc() == L'2');
-        assert(f.sbumpc() == L'3');
-    }
-    remove(temp.c_str());
 }
Index: test/input.output/file.streams/fstreams/fstream.assign/member_swap.pass.cpp
===================================================================
--- test/input.output/file.streams/fstreams/fstream.assign/member_swap.pass.cpp
+++ test/input.output/file.streams/fstreams/fstream.assign/member_swap.pass.cpp
@@ -20,52 +20,54 @@
 
 int main()
 {
-    std::string temp1 = get_temp_file_name();
-    std::string temp2 = get_temp_file_name();
     {
-        std::fstream fs1(temp1.c_str(), std::ios_base::in | std::ios_base::out
-                                                  | std::ios_base::trunc);
-        std::fstream fs2(temp2.c_str(), std::ios_base::in | std::ios_base::out
-                                                  | std::ios_base::trunc);
-        fs1 << 1 << ' ' << 2;
-        fs2 << 2 << ' ' << 1;
-        fs1.seekg(0);
-        fs1.swap(fs2);
-        fs1.seekg(0);
-        int i;
-        fs1 >> i;
-        assert(i == 2);
-        fs1 >> i;
-        assert(i == 1);
-        i = 0;
-        fs2 >> i;
-        assert(i == 1);
-        fs2 >> i;
-        assert(i == 2);
+        libcxx_support::TempFileName temp1;
+        libcxx_support::TempFileName temp2;
+        {
+            std::fstream fs1(temp1.c_str(), std::ios_base::in | std::ios_base::out
+                                                      | std::ios_base::trunc);
+            std::fstream fs2(temp2.c_str(), std::ios_base::in | std::ios_base::out
+                                                      | std::ios_base::trunc);
+            fs1 << 1 << ' ' << 2;
+            fs2 << 2 << ' ' << 1;
+            fs1.seekg(0);
+            fs1.swap(fs2);
+            fs1.seekg(0);
+            int i;
+            fs1 >> i;
+            assert(i == 2);
+            fs1 >> i;
+            assert(i == 1);
+            i = 0;
+            fs2 >> i;
+            assert(i == 1);
+            fs2 >> i;
+            assert(i == 2);
+        }
     }
-    std::remove(temp1.c_str());
-    std::remove(temp2.c_str());
     {
-        std::wfstream fs1(temp1.c_str(), std::ios_base::in | std::ios_base::out
-                                                   | std::ios_base::trunc);
-        std::wfstream fs2(temp2.c_str(), std::ios_base::in | std::ios_base::out
-                                                   | std::ios_base::trunc);
-        fs1 << 1 << ' ' << 2;
-        fs2 << 2 << ' ' << 1;
-        fs1.seekg(0);
-        fs1.swap(fs2);
-        fs1.seekg(0);
-        int i;
-        fs1 >> i;
-        assert(i == 2);
-        fs1 >> i;
-        assert(i == 1);
-        i = 0;
-        fs2 >> i;
-        assert(i == 1);
-        fs2 >> i;
-        assert(i == 2);
+        libcxx_support::TempFileName temp1;
+        libcxx_support::TempFileName temp2;
+        {
+            std::wfstream fs1(temp1.c_str(), std::ios_base::in | std::ios_base::out
+                                                       | std::ios_base::trunc);
+            std::wfstream fs2(temp2.c_str(), std::ios_base::in | std::ios_base::out
+                                                       | std::ios_base::trunc);
+            fs1 << 1 << ' ' << 2;
+            fs2 << 2 << ' ' << 1;
+            fs1.seekg(0);
+            fs1.swap(fs2);
+            fs1.seekg(0);
+            int i;
+            fs1 >> i;
+            assert(i == 2);
+            fs1 >> i;
+            assert(i == 1);
+            i = 0;
+            fs2 >> i;
+            assert(i == 1);
+            fs2 >> i;
+            assert(i == 2);
+        }
     }
-    std::remove(temp1.c_str());
-    std::remove(temp2.c_str());
 }
Index: test/input.output/file.streams/fstreams/fstream.assign/move_assign.pass.cpp
===================================================================
--- test/input.output/file.streams/fstreams/fstream.assign/move_assign.pass.cpp
+++ test/input.output/file.streams/fstreams/fstream.assign/move_assign.pass.cpp
@@ -21,30 +21,33 @@
 int main()
 {
 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
-    std::string temp = get_temp_file_name();
     {
-        std::fstream fso(temp.c_str(), std::ios_base::in | std::ios_base::out
-                                                 | std::ios_base::trunc);
-        std::fstream fs;
-        fs = move(fso);
-        double x = 0;
-        fs << 3.25;
-        fs.seekg(0);
-        fs >> x;
-        assert(x == 3.25);
+        libcxx_support::TempFileName temp;
+        {
+            std::fstream fso(temp.c_str(), std::ios_base::in | std::ios_base::out
+                                                     | std::ios_base::trunc);
+            std::fstream fs;
+            fs = move(fso);
+            double x = 0;
+            fs << 3.25;
+            fs.seekg(0);
+            fs >> x;
+            assert(x == 3.25);
+        }
     }
-    std::remove(temp.c_str());
     {
-        std::wfstream fso(temp.c_str(), std::ios_base::in | std::ios_base::out
-                                                  | std::ios_base::trunc);
-        std::wfstream fs;
-        fs = move(fso);
-        double x = 0;
-        fs << 3.25;
-        fs.seekg(0);
-        fs >> x;
-        assert(x == 3.25);
+        libcxx_support::TempFileName temp;
+        {
+            std::wfstream fso(temp.c_str(), std::ios_base::in | std::ios_base::out
+                                                      | std::ios_base::trunc);
+            std::wfstream fs;
+            fs = move(fso);
+            double x = 0;
+            fs << 3.25;
+            fs.seekg(0);
+            fs >> x;
+            assert(x == 3.25);
+        }
     }
-    std::remove(temp.c_str());
 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
 }
Index: test/input.output/file.streams/fstreams/fstream.assign/nonmember_swap.pass.cpp
===================================================================
--- test/input.output/file.streams/fstreams/fstream.assign/nonmember_swap.pass.cpp
+++ test/input.output/file.streams/fstreams/fstream.assign/nonmember_swap.pass.cpp
@@ -21,52 +21,54 @@
 
 int main()
 {
-    std::string temp1 = get_temp_file_name();
-    std::string temp2 = get_temp_file_name();
     {
-        std::fstream fs1(temp1.c_str(), std::ios_base::in | std::ios_base::out
-                                                  | std::ios_base::trunc);
-        std::fstream fs2(temp2.c_str(), std::ios_base::in | std::ios_base::out
-                                                  | std::ios_base::trunc);
-        fs1 << 1 << ' ' << 2;
-        fs2 << 2 << ' ' << 1;
-        fs1.seekg(0);
-        swap(fs1, fs2);
-        fs1.seekg(0);
-        int i;
-        fs1 >> i;
-        assert(i == 2);
-        fs1 >> i;
-        assert(i == 1);
-        i = 0;
-        fs2 >> i;
-        assert(i == 1);
-        fs2 >> i;
-        assert(i == 2);
+        libcxx_support::TempFileName temp1;
+        libcxx_support::TempFileName temp2;
+        {
+            std::fstream fs1(temp1.c_str(), std::ios_base::in | std::ios_base::out
+                                                      | std::ios_base::trunc);
+            std::fstream fs2(temp2.c_str(), std::ios_base::in | std::ios_base::out
+                                                      | std::ios_base::trunc);
+            fs1 << 1 << ' ' << 2;
+            fs2 << 2 << ' ' << 1;
+            fs1.seekg(0);
+            swap(fs1, fs2);
+            fs1.seekg(0);
+            int i;
+            fs1 >> i;
+            assert(i == 2);
+            fs1 >> i;
+            assert(i == 1);
+            i = 0;
+            fs2 >> i;
+            assert(i == 1);
+            fs2 >> i;
+            assert(i == 2);
+        }
     }
-    std::remove(temp1.c_str());
-    std::remove(temp2.c_str());
     {
-        std::wfstream fs1(temp1.c_str(), std::ios_base::in | std::ios_base::out
-                                                   | std::ios_base::trunc);
-        std::wfstream fs2(temp2.c_str(), std::ios_base::in | std::ios_base::out
-                                                   | std::ios_base::trunc);
-        fs1 << 1 << ' ' << 2;
-        fs2 << 2 << ' ' << 1;
-        fs1.seekg(0);
-        swap(fs1, fs2);
-        fs1.seekg(0);
-        int i;
-        fs1 >> i;
-        assert(i == 2);
-        fs1 >> i;
-        assert(i == 1);
-        i = 0;
-        fs2 >> i;
-        assert(i == 1);
-        fs2 >> i;
-        assert(i == 2);
+        libcxx_support::TempFileName temp1;
+        libcxx_support::TempFileName temp2;
+        {
+            std::wfstream fs1(temp1.c_str(), std::ios_base::in | std::ios_base::out
+                                                       | std::ios_base::trunc);
+            std::wfstream fs2(temp2.c_str(), std::ios_base::in | std::ios_base::out
+                                                       | std::ios_base::trunc);
+            fs1 << 1 << ' ' << 2;
+            fs2 << 2 << ' ' << 1;
+            fs1.seekg(0);
+            swap(fs1, fs2);
+            fs1.seekg(0);
+            int i;
+            fs1 >> i;
+            assert(i == 2);
+            fs1 >> i;
+            assert(i == 1);
+            i = 0;
+            fs2 >> i;
+            assert(i == 1);
+            fs2 >> i;
+            assert(i == 2);
+        }
     }
-    std::remove(temp1.c_str());
-    std::remove(temp2.c_str());
 }
Index: test/input.output/file.streams/fstreams/fstream.cons/move.pass.cpp
===================================================================
--- test/input.output/file.streams/fstreams/fstream.cons/move.pass.cpp
+++ test/input.output/file.streams/fstreams/fstream.cons/move.pass.cpp
@@ -21,28 +21,31 @@
 int main()
 {
 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
-    std::string temp = get_temp_file_name();
     {
-        std::fstream fso(temp, std::ios_base::in | std::ios_base::out
-                                                 | std::ios_base::trunc);
-        std::fstream fs = move(fso);
-        double x = 0;
-        fs << 3.25;
-        fs.seekg(0);
-        fs >> x;
-        assert(x == 3.25);
+        libcxx_support::TempFileName temp;
+        {
+            std::fstream fso(temp, std::ios_base::in | std::ios_base::out
+                                                     | std::ios_base::trunc);
+            std::fstream fs = move(fso);
+            double x = 0;
+            fs << 3.25;
+            fs.seekg(0);
+            fs >> x;
+            assert(x == 3.25);
+        }
     }
-    std::remove(temp.c_str());
     {
-        std::wfstream fso(temp, std::ios_base::in | std::ios_base::out
-                                                  | std::ios_base::trunc);
-        std::wfstream fs = move(fso);
-        double x = 0;
-        fs << 3.25;
-        fs.seekg(0);
-        fs >> x;
-        assert(x == 3.25);
+        libcxx_support::TempFileName temp;
+        {
+            std::wfstream fso(temp, std::ios_base::in | std::ios_base::out
+                                                      | std::ios_base::trunc);
+            std::wfstream fs = move(fso);
+            double x = 0;
+            fs << 3.25;
+            fs.seekg(0);
+            fs >> x;
+            assert(x == 3.25);
+        }
     }
-    std::remove(temp.c_str());
 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
 }
Index: test/input.output/file.streams/fstreams/fstream.cons/pointer.pass.cpp
===================================================================
--- test/input.output/file.streams/fstreams/fstream.cons/pointer.pass.cpp
+++ test/input.output/file.streams/fstreams/fstream.cons/pointer.pass.cpp
@@ -20,25 +20,28 @@
 
 int main()
 {
-    std::string temp = get_temp_file_name();
     {
-        std::fstream fs(temp.c_str(), std::ios_base::in | std::ios_base::out
-                                                | std::ios_base::trunc);
-        double x = 0;
-        fs << 3.25;
-        fs.seekg(0);
-        fs >> x;
-        assert(x == 3.25);
+        libcxx_support::TempFileName temp;
+        {
+            std::fstream fs(temp.c_str(), std::ios_base::in | std::ios_base::out
+                                                    | std::ios_base::trunc);
+            double x = 0;
+            fs << 3.25;
+            fs.seekg(0);
+            fs >> x;
+            assert(x == 3.25);
+        }
     }
-    std::remove(temp.c_str());
     {
-        std::wfstream fs(temp.c_str(), std::ios_base::in | std::ios_base::out
-                                                 | std::ios_base::trunc);
-        double x = 0;
-        fs << 3.25;
-        fs.seekg(0);
-        fs >> x;
-        assert(x == 3.25);
+        libcxx_support::TempFileName temp;
+        {
+            std::wfstream fs(temp.c_str(), std::ios_base::in | std::ios_base::out
+                                                     | std::ios_base::trunc);
+            double x = 0;
+            fs << 3.25;
+            fs.seekg(0);
+            fs >> x;
+            assert(x == 3.25);
+        }
     }
-    std::remove(temp.c_str());
 }
Index: test/input.output/file.streams/fstreams/fstream.cons/string.pass.cpp
===================================================================
--- test/input.output/file.streams/fstreams/fstream.cons/string.pass.cpp
+++ test/input.output/file.streams/fstreams/fstream.cons/string.pass.cpp
@@ -20,27 +20,30 @@
 
 int main()
 {
-    std::string temp = get_temp_file_name();
     {
-        std::fstream fs(temp,
-                        std::ios_base::in | std::ios_base::out
-                                          | std::ios_base::trunc);
-        double x = 0;
-        fs << 3.25;
-        fs.seekg(0);
-        fs >> x;
-        assert(x == 3.25);
+        libcxx_support::TempFileName temp;
+        {
+            std::fstream fs(temp,
+                            std::ios_base::in | std::ios_base::out
+                                              | std::ios_base::trunc);
+            double x = 0;
+            fs << 3.25;
+            fs.seekg(0);
+            fs >> x;
+            assert(x == 3.25);
+        }
     }
-    std::remove(temp.c_str());
     {
-        std::wfstream fs(temp,
-                         std::ios_base::in | std::ios_base::out
-                                           | std::ios_base::trunc);
-        double x = 0;
-        fs << 3.25;
-        fs.seekg(0);
-        fs >> x;
-        assert(x == 3.25);
+        libcxx_support::TempFileName temp;
+        {
+            std::wfstream fs(temp,
+                             std::ios_base::in | std::ios_base::out
+                                               | std::ios_base::trunc);
+            double x = 0;
+            fs << 3.25;
+            fs.seekg(0);
+            fs >> x;
+            assert(x == 3.25);
+        }
     }
-    std::remove(temp.c_str());
 }
Index: test/input.output/file.streams/fstreams/fstream.members/close.pass.cpp
===================================================================
--- test/input.output/file.streams/fstreams/fstream.members/close.pass.cpp
+++ test/input.output/file.streams/fstreams/fstream.members/close.pass.cpp
@@ -20,23 +20,26 @@
 
 int main()
 {
-    std::string temp = get_temp_file_name();
     {
-        std::fstream fs;
-        assert(!fs.is_open());
-        fs.open(temp.c_str(), std::ios_base::out);
-        assert(fs.is_open());
-        fs.close();
-        assert(!fs.is_open());
+        libcxx_support::TempFileName temp;
+        {
+            std::fstream fs;
+            assert(!fs.is_open());
+            fs.open(temp.c_str(), std::ios_base::out);
+            assert(fs.is_open());
+            fs.close();
+            assert(!fs.is_open());
+        }
     }
-    std::remove(temp.c_str());
     {
-        std::wfstream fs;
-        assert(!fs.is_open());
-        fs.open(temp.c_str(), std::ios_base::out);
-        assert(fs.is_open());
-        fs.close();
-        assert(!fs.is_open());
+        libcxx_support::TempFileName temp;
+        {
+            std::wfstream fs;
+            assert(!fs.is_open());
+            fs.open(temp.c_str(), std::ios_base::out);
+            assert(fs.is_open());
+            fs.close();
+            assert(!fs.is_open());
+        }
     }
-    std::remove(temp.c_str());
 }
Index: test/input.output/file.streams/fstreams/fstream.members/open_pointer.pass.cpp
===================================================================
--- test/input.output/file.streams/fstreams/fstream.members/open_pointer.pass.cpp
+++ test/input.output/file.streams/fstreams/fstream.members/open_pointer.pass.cpp
@@ -20,31 +20,34 @@
 
 int main()
 {
-    std::string temp = get_temp_file_name();
     {
-        std::fstream fs;
-        assert(!fs.is_open());
-        fs.open(temp.c_str(), std::ios_base::in | std::ios_base::out
-                                        | std::ios_base::trunc);
-        assert(fs.is_open());
-        double x = 0;
-        fs << 3.25;
-        fs.seekg(0);
-        fs >> x;
-        assert(x == 3.25);
+        libcxx_support::TempFileName temp;
+        {
+            std::fstream fs;
+            assert(!fs.is_open());
+            fs.open(temp.c_str(), std::ios_base::in | std::ios_base::out
+                                            | std::ios_base::trunc);
+            assert(fs.is_open());
+            double x = 0;
+            fs << 3.25;
+            fs.seekg(0);
+            fs >> x;
+            assert(x == 3.25);
+        }
     }
-    std::remove(temp.c_str());
     {
-        std::wfstream fs;
-        assert(!fs.is_open());
-        fs.open(temp.c_str(), std::ios_base::in | std::ios_base::out
-                                        | std::ios_base::trunc);
-        assert(fs.is_open());
-        double x = 0;
-        fs << 3.25;
-        fs.seekg(0);
-        fs >> x;
-        assert(x == 3.25);
+        libcxx_support::TempFileName temp;
+        {
+            std::wfstream fs;
+            assert(!fs.is_open());
+            fs.open(temp.c_str(), std::ios_base::in | std::ios_base::out
+                                            | std::ios_base::trunc);
+            assert(fs.is_open());
+            double x = 0;
+            fs << 3.25;
+            fs.seekg(0);
+            fs >> x;
+            assert(x == 3.25);
+        }
     }
-    std::remove(temp.c_str());
 }
Index: test/input.output/file.streams/fstreams/fstream.members/open_string.pass.cpp
===================================================================
--- test/input.output/file.streams/fstreams/fstream.members/open_string.pass.cpp
+++ test/input.output/file.streams/fstreams/fstream.members/open_string.pass.cpp
@@ -20,31 +20,34 @@
 
 int main()
 {
-    std::string temp = get_temp_file_name();
     {
-        std::fstream fs;
-        assert(!fs.is_open());
-        fs.open(temp, std::ios_base::in | std::ios_base::out
-                                                     | std::ios_base::trunc);
-        assert(fs.is_open());
-        double x = 0;
-        fs << 3.25;
-        fs.seekg(0);
-        fs >> x;
-        assert(x == 3.25);
+        libcxx_support::TempFileName temp;
+        {
+            std::fstream fs;
+            assert(!fs.is_open());
+            fs.open(temp, std::ios_base::in | std::ios_base::out
+                                                         | std::ios_base::trunc);
+            assert(fs.is_open());
+            double x = 0;
+            fs << 3.25;
+            fs.seekg(0);
+            fs >> x;
+            assert(x == 3.25);
+        }
     }
-    std::remove(temp.c_str());
     {
-        std::wfstream fs;
-        assert(!fs.is_open());
-        fs.open(temp, std::ios_base::in | std::ios_base::out
-                                                     | std::ios_base::trunc);
-        assert(fs.is_open());
-        double x = 0;
-        fs << 3.25;
-        fs.seekg(0);
-        fs >> x;
-        assert(x == 3.25);
+        libcxx_support::TempFileName temp;
+        {
+            std::wfstream fs;
+            assert(!fs.is_open());
+            fs.open(temp, std::ios_base::in | std::ios_base::out
+                                                         | std::ios_base::trunc);
+            assert(fs.is_open());
+            double x = 0;
+            fs << 3.25;
+            fs.seekg(0);
+            fs >> x;
+            assert(x == 3.25);
+        }
     }
-    std::remove(temp.c_str());
 }
Index: test/input.output/file.streams/fstreams/ofstream.assign/member_swap.pass.cpp
===================================================================
--- test/input.output/file.streams/fstreams/ofstream.assign/member_swap.pass.cpp
+++ test/input.output/file.streams/fstreams/ofstream.assign/member_swap.pass.cpp
@@ -20,60 +20,66 @@
 
 int main()
 {
-    std::string temp1 = get_temp_file_name();
-    std::string temp2 = get_temp_file_name();
     {
-        std::ofstream fs1(temp1.c_str());
-        std::ofstream fs2(temp2.c_str());
-        fs1 << 3.25;
-        fs2 << 4.5;
-        fs1.swap(fs2);
-        fs1 << ' ' << 3.25;
-        fs2 << ' ' << 4.5;
+        libcxx_support::TempFileName temp2;
+        {
+            libcxx_support::TempFileName temp1;
+            {
+                std::ofstream fs1(temp1.c_str());
+                std::ofstream fs2(temp2.c_str());
+                fs1 << 3.25;
+                fs2 << 4.5;
+                fs1.swap(fs2);
+                fs1 << ' ' << 3.25;
+                fs2 << ' ' << 4.5;
+            }
+            {
+                std::ifstream fs(temp1.c_str());
+                double x = 0;
+                fs >> x;
+                assert(x == 3.25);
+                fs >> x;
+                assert(x == 4.5);
+            }
+        }
+        {
+            std::ifstream fs(temp2.c_str());
+            double x = 0;
+            fs >> x;
+            assert(x == 4.5);
+            fs >> x;
+            assert(x == 3.25);
+        }
+    }
+    {
+        libcxx_support::TempFileName temp2;
+        {
+            libcxx_support::TempFileName temp1;
+            {
+                std::wofstream fs1(temp1.c_str());
+                std::wofstream fs2(temp2.c_str());
+                fs1 << 3.25;
+                fs2 << 4.5;
+                fs1.swap(fs2);
+                fs1 << ' ' << 3.25;
+                fs2 << ' ' << 4.5;
+            }
+            {
+                std::wifstream fs(temp1.c_str());
+                double x = 0;
+                fs >> x;
+                assert(x == 3.25);
+                fs >> x;
+                assert(x == 4.5);
+            }
+        }
+        {
+            std::wifstream fs(temp2.c_str());
+            double x = 0;
+            fs >> x;
+            assert(x == 4.5);
+            fs >> x;
+            assert(x == 3.25);
+        }
     }
-    {
-        std::ifstream fs(temp1.c_str());
-        double x = 0;
-        fs >> x;
-        assert(x == 3.25);
-        fs >> x;
-        assert(x == 4.5);
-    }
-    std::remove(temp1.c_str());
-    {
-        std::ifstream fs(temp2.c_str());
-        double x = 0;
-        fs >> x;
-        assert(x == 4.5);
-        fs >> x;
-        assert(x == 3.25);
-    }
-    std::remove(temp2.c_str());
-    {
-        std::wofstream fs1(temp1.c_str());
-        std::wofstream fs2(temp2.c_str());
-        fs1 << 3.25;
-        fs2 << 4.5;
-        fs1.swap(fs2);
-        fs1 << ' ' << 3.25;
-        fs2 << ' ' << 4.5;
-    }
-    {
-        std::wifstream fs(temp1.c_str());
-        double x = 0;
-        fs >> x;
-        assert(x == 3.25);
-        fs >> x;
-        assert(x == 4.5);
-    }
-    std::remove(temp1.c_str());
-    {
-        std::wifstream fs(temp2.c_str());
-        double x = 0;
-        fs >> x;
-        assert(x == 4.5);
-        fs >> x;
-        assert(x == 3.25);
-    }
-    std::remove(temp2.c_str());
 }
Index: test/input.output/file.streams/fstreams/ofstream.assign/move_assign.pass.cpp
===================================================================
--- test/input.output/file.streams/fstreams/ofstream.assign/move_assign.pass.cpp
+++ test/input.output/file.streams/fstreams/ofstream.assign/move_assign.pass.cpp
@@ -21,32 +21,35 @@
 int main()
 {
 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
-    std::string temp = get_temp_file_name();
     {
-        std::ofstream fso(temp.c_str());
-        std::ofstream fs;
-        fs = move(fso);
-        fs << 3.25;
+        libcxx_support::TempFileName temp;
+        {
+            std::ofstream fso(temp.c_str());
+            std::ofstream fs;
+            fs = move(fso);
+            fs << 3.25;
+        }
+        {
+            std::ifstream fs(temp.c_str());
+            double x = 0;
+            fs >> x;
+            assert(x == 3.25);
+        }
     }
     {
-        std::ifstream fs(temp.c_str());
-        double x = 0;
-        fs >> x;
-        assert(x == 3.25);
+        libcxx_support::TempFileName temp;
+        {
+            std::wofstream fso(temp.c_str());
+            std::wofstream fs;
+            fs = move(fso);
+            fs << 3.25;
+        }
+        {
+            std::wifstream fs(temp.c_str());
+            double x = 0;
+            fs >> x;
+            assert(x == 3.25);
+        }
     }
-    std::remove(temp.c_str());
-    {
-        std::wofstream fso(temp.c_str());
-        std::wofstream fs;
-        fs = move(fso);
-        fs << 3.25;
-    }
-    {
-        std::wifstream fs(temp.c_str());
-        double x = 0;
-        fs >> x;
-        assert(x == 3.25);
-    }
-    std::remove(temp.c_str());
 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
 }
Index: test/input.output/file.streams/fstreams/ofstream.assign/nonmember_swap.pass.cpp
===================================================================
--- test/input.output/file.streams/fstreams/ofstream.assign/nonmember_swap.pass.cpp
+++ test/input.output/file.streams/fstreams/ofstream.assign/nonmember_swap.pass.cpp
@@ -21,60 +21,66 @@
 
 int main()
 {
-    std::string temp1 = get_temp_file_name();
-    std::string temp2 = get_temp_file_name();
     {
-        std::ofstream fs1(temp1.c_str());
-        std::ofstream fs2(temp2.c_str());
-        fs1 << 3.25;
-        fs2 << 4.5;
-        swap(fs1, fs2);
-        fs1 << ' ' << 3.25;
-        fs2 << ' ' << 4.5;
+        libcxx_support::TempFileName temp2;
+        {
+            libcxx_support::TempFileName temp1;
+            {
+                std::ofstream fs1(temp1.c_str());
+                std::ofstream fs2(temp2.c_str());
+                fs1 << 3.25;
+                fs2 << 4.5;
+                swap(fs1, fs2);
+                fs1 << ' ' << 3.25;
+                fs2 << ' ' << 4.5;
+            }
+            {
+                std::ifstream fs(temp1.c_str());
+                double x = 0;
+                fs >> x;
+                assert(x == 3.25);
+                fs >> x;
+                assert(x == 4.5);
+            }
+        }
+        {
+            std::ifstream fs(temp2.c_str());
+            double x = 0;
+            fs >> x;
+            assert(x == 4.5);
+            fs >> x;
+            assert(x == 3.25);
+        }
+    }
+    {
+        libcxx_support::TempFileName temp2;
+        {
+            libcxx_support::TempFileName temp1;
+            {
+                std::wofstream fs1(temp1.c_str());
+                std::wofstream fs2(temp2.c_str());
+                fs1 << 3.25;
+                fs2 << 4.5;
+                swap(fs1, fs2);
+                fs1 << ' ' << 3.25;
+                fs2 << ' ' << 4.5;
+            }
+            {
+                std::wifstream fs(temp1.c_str());
+                double x = 0;
+                fs >> x;
+                assert(x == 3.25);
+                fs >> x;
+                assert(x == 4.5);
+            }
+        }
+        {
+            std::wifstream fs(temp2.c_str());
+            double x = 0;
+            fs >> x;
+            assert(x == 4.5);
+            fs >> x;
+            assert(x == 3.25);
+        }
     }
-    {
-        std::ifstream fs(temp1.c_str());
-        double x = 0;
-        fs >> x;
-        assert(x == 3.25);
-        fs >> x;
-        assert(x == 4.5);
-    }
-    std::remove(temp1.c_str());
-    {
-        std::ifstream fs(temp2.c_str());
-        double x = 0;
-        fs >> x;
-        assert(x == 4.5);
-        fs >> x;
-        assert(x == 3.25);
-    }
-    std::remove(temp2.c_str());
-    {
-        std::wofstream fs1(temp1.c_str());
-        std::wofstream fs2(temp2.c_str());
-        fs1 << 3.25;
-        fs2 << 4.5;
-        swap(fs1, fs2);
-        fs1 << ' ' << 3.25;
-        fs2 << ' ' << 4.5;
-    }
-    {
-        std::wifstream fs(temp1.c_str());
-        double x = 0;
-        fs >> x;
-        assert(x == 3.25);
-        fs >> x;
-        assert(x == 4.5);
-    }
-    std::remove(temp1.c_str());
-    {
-        std::wifstream fs(temp2.c_str());
-        double x = 0;
-        fs >> x;
-        assert(x == 4.5);
-        fs >> x;
-        assert(x == 3.25);
-    }
-    std::remove(temp2.c_str());
 }
Index: test/input.output/file.streams/fstreams/ofstream.cons/move.pass.cpp
===================================================================
--- test/input.output/file.streams/fstreams/ofstream.cons/move.pass.cpp
+++ test/input.output/file.streams/fstreams/ofstream.cons/move.pass.cpp
@@ -21,30 +21,33 @@
 int main()
 {
 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
-    std::string temp = get_temp_file_name();
     {
-        std::ofstream fso(temp.c_str());
-        std::ofstream fs = move(fso);
-        fs << 3.25;
+        libcxx_support::TempFileName temp;
+        {
+            std::ofstream fso(temp.c_str());
+            std::ofstream fs = move(fso);
+            fs << 3.25;
+        }
+        {
+            std::ifstream fs(temp.c_str());
+            double x = 0;
+            fs >> x;
+            assert(x == 3.25);
+        }
     }
     {
-        std::ifstream fs(temp.c_str());
-        double x = 0;
-        fs >> x;
-        assert(x == 3.25);
+        libcxx_support::TempFileName temp;
+        {
+            std::wofstream fso(temp.c_str());
+            std::wofstream fs = move(fso);
+            fs << 3.25;
+        }
+        {
+            std::wifstream fs(temp.c_str());
+            double x = 0;
+            fs >> x;
+            assert(x == 3.25);
+        }
     }
-    std::remove(temp.c_str());
-    {
-        std::wofstream fso(temp.c_str());
-        std::wofstream fs = move(fso);
-        fs << 3.25;
-    }
-    {
-        std::wifstream fs(temp.c_str());
-        double x = 0;
-        fs >> x;
-        assert(x == 3.25);
-    }
-    std::remove(temp.c_str());
 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
 }
Index: test/input.output/file.streams/fstreams/ofstream.cons/pointer.pass.cpp
===================================================================
--- test/input.output/file.streams/fstreams/ofstream.cons/pointer.pass.cpp
+++ test/input.output/file.streams/fstreams/ofstream.cons/pointer.pass.cpp
@@ -20,27 +20,30 @@
 
 int main()
 {
-    std::string temp = get_temp_file_name();
     {
-        std::ofstream fs(temp.c_str());
-        fs << 3.25;
+        libcxx_support::TempFileName temp;
+        {
+            std::ofstream fs(temp.c_str());
+            fs << 3.25;
+        }
+        {
+            std::ifstream fs(temp.c_str());
+            double x = 0;
+            fs >> x;
+            assert(x == 3.25);
+        }
     }
     {
-        std::ifstream fs(temp.c_str());
-        double x = 0;
-        fs >> x;
-        assert(x == 3.25);
+        libcxx_support::TempFileName temp;
+        {
+            std::wofstream fs(temp.c_str());
+            fs << 3.25;
+        }
+        {
+            std::wifstream fs(temp.c_str());
+            double x = 0;
+            fs >> x;
+            assert(x == 3.25);
+        }
     }
-    std::remove(temp.c_str());
-    {
-        std::wofstream fs(temp.c_str());
-        fs << 3.25;
-    }
-    {
-        std::wifstream fs(temp.c_str());
-        double x = 0;
-        fs >> x;
-        assert(x == 3.25);
-    }
-    std::remove(temp.c_str());
 }
Index: test/input.output/file.streams/fstreams/ofstream.cons/string.pass.cpp
===================================================================
--- test/input.output/file.streams/fstreams/ofstream.cons/string.pass.cpp
+++ test/input.output/file.streams/fstreams/ofstream.cons/string.pass.cpp
@@ -20,27 +20,30 @@
 
 int main()
 {
-    std::string temp = get_temp_file_name();
     {
-        std::ofstream fs(temp);
-        fs << 3.25;
+        libcxx_support::TempFileName temp;
+        {
+            std::ofstream fs(temp);
+            fs << 3.25;
+        }
+        {
+            std::ifstream fs(temp);
+            double x = 0;
+            fs >> x;
+            assert(x == 3.25);
+        }
     }
     {
-        std::ifstream fs(temp);
-        double x = 0;
-        fs >> x;
-        assert(x == 3.25);
+        libcxx_support::TempFileName temp;
+        {
+            std::wofstream fs(temp);
+            fs << 3.25;
+        }
+        {
+            std::wifstream fs(temp);
+            double x = 0;
+            fs >> x;
+            assert(x == 3.25);
+        }
     }
-    std::remove(temp.c_str());
-    {
-        std::wofstream fs(temp);
-        fs << 3.25;
-    }
-    {
-        std::wifstream fs(temp);
-        double x = 0;
-        fs >> x;
-        assert(x == 3.25);
-    }
-    std::remove(temp.c_str());
 }
Index: test/input.output/file.streams/fstreams/ofstream.members/close.pass.cpp
===================================================================
--- test/input.output/file.streams/fstreams/ofstream.members/close.pass.cpp
+++ test/input.output/file.streams/fstreams/ofstream.members/close.pass.cpp
@@ -20,23 +20,26 @@
 
 int main()
 {
-    std::string temp = get_temp_file_name();
     {
-        std::ofstream fs;
-        assert(!fs.is_open());
-        fs.open(temp.c_str());
-        assert(fs.is_open());
-        fs.close();
-        assert(!fs.is_open());
+        libcxx_support::TempFileName temp;
+        {
+            std::ofstream fs;
+            assert(!fs.is_open());
+            fs.open(temp.c_str());
+            assert(fs.is_open());
+            fs.close();
+            assert(!fs.is_open());
+        }
     }
-    std::remove(temp.c_str());
     {
-        std::wofstream fs;
-        assert(!fs.is_open());
-        fs.open(temp.c_str());
-        assert(fs.is_open());
-        fs.close();
-        assert(!fs.is_open());
+        libcxx_support::TempFileName temp;
+        {
+            std::wofstream fs;
+            assert(!fs.is_open());
+            fs.open(temp.c_str());
+            assert(fs.is_open());
+            fs.close();
+            assert(!fs.is_open());
+        }
     }
-    std::remove(temp.c_str());
 }
Index: test/input.output/file.streams/fstreams/ofstream.members/open_pointer.pass.cpp
===================================================================
--- test/input.output/file.streams/fstreams/ofstream.members/open_pointer.pass.cpp
+++ test/input.output/file.streams/fstreams/ofstream.members/open_pointer.pass.cpp
@@ -20,39 +20,42 @@
 
 int main()
 {
-    std::string temp = get_temp_file_name();
     {
-        std::ofstream fs;
-        assert(!fs.is_open());
-        char c = 'a';
-        fs << c;
-        assert(fs.fail());
-        fs.open(temp.c_str());
-        assert(fs.is_open());
-        fs << c;
+        libcxx_support::TempFileName temp;
+        {
+            std::ofstream fs;
+            assert(!fs.is_open());
+            char c = 'a';
+            fs << c;
+            assert(fs.fail());
+            fs.open(temp.c_str());
+            assert(fs.is_open());
+            fs << c;
+        }
+        {
+            std::ifstream fs(temp.c_str());
+            char c = 0;
+            fs >> c;
+            assert(c == 'a');
+        }
     }
     {
-        std::ifstream fs(temp.c_str());
-        char c = 0;
-        fs >> c;
-        assert(c == 'a');
+        libcxx_support::TempFileName temp;
+        {
+            std::wofstream fs;
+            assert(!fs.is_open());
+            wchar_t c = L'a';
+            fs << c;
+            assert(fs.fail());
+            fs.open(temp.c_str());
+            assert(fs.is_open());
+            fs << c;
+        }
+        {
+            std::wifstream fs(temp.c_str());
+            wchar_t c = 0;
+            fs >> c;
+            assert(c == L'a');
+        }
     }
-    std::remove(temp.c_str());
-    {
-        std::wofstream fs;
-        assert(!fs.is_open());
-        wchar_t c = L'a';
-        fs << c;
-        assert(fs.fail());
-        fs.open(temp.c_str());
-        assert(fs.is_open());
-        fs << c;
-    }
-    {
-        std::wifstream fs(temp.c_str());
-        wchar_t c = 0;
-        fs >> c;
-        assert(c == L'a');
-    }
-    std::remove(temp.c_str());
 }
Index: test/input.output/file.streams/fstreams/ofstream.members/open_string.pass.cpp
===================================================================
--- test/input.output/file.streams/fstreams/ofstream.members/open_string.pass.cpp
+++ test/input.output/file.streams/fstreams/ofstream.members/open_string.pass.cpp
@@ -20,39 +20,42 @@
 
 int main()
 {
-    std::string temp = get_temp_file_name();
     {
-        std::ofstream fs;
-        assert(!fs.is_open());
-        char c = 'a';
-        fs << c;
-        assert(fs.fail());
-        fs.open(temp);
-        assert(fs.is_open());
-        fs << c;
+        libcxx_support::TempFileName temp;
+        {
+            std::ofstream fs;
+            assert(!fs.is_open());
+            char c = 'a';
+            fs << c;
+            assert(fs.fail());
+            fs.open(temp);
+            assert(fs.is_open());
+            fs << c;
+        }
+        {
+            std::ifstream fs(temp.c_str());
+            char c = 0;
+            fs >> c;
+            assert(c == 'a');
+        }
     }
     {
-        std::ifstream fs(temp.c_str());
-        char c = 0;
-        fs >> c;
-        assert(c == 'a');
+        libcxx_support::TempFileName temp;
+        {
+            std::wofstream fs;
+            assert(!fs.is_open());
+            wchar_t c = L'a';
+            fs << c;
+            assert(fs.fail());
+            fs.open(temp);
+            assert(fs.is_open());
+            fs << c;
+        }
+        {
+            std::wifstream fs(temp.c_str());
+            wchar_t c = 0;
+            fs >> c;
+            assert(c == L'a');
+        }
     }
-    std::remove(temp.c_str());
-    {
-        std::wofstream fs;
-        assert(!fs.is_open());
-        wchar_t c = L'a';
-        fs << c;
-        assert(fs.fail());
-        fs.open(temp);
-        assert(fs.is_open());
-        fs << c;
-    }
-    {
-        std::wifstream fs(temp.c_str());
-        wchar_t c = 0;
-        fs >> c;
-        assert(c == L'a');
-    }
-    std::remove(temp.c_str());
 }
Index: test/input.output/file.streams/fstreams/ofstream.members/rdbuf.pass.cpp
===================================================================
--- test/input.output/file.streams/fstreams/ofstream.members/rdbuf.pass.cpp
+++ test/input.output/file.streams/fstreams/ofstream.members/rdbuf.pass.cpp
@@ -20,17 +20,20 @@
 
 int main()
 {
-    std::string temp = get_temp_file_name();
     {
-        std::ofstream fs(temp.c_str());
-        std::filebuf* fb = fs.rdbuf();
-        assert(fb->sputc('r') == 'r');
+        libcxx_support::TempFileName temp;
+        {
+            std::ofstream fs(temp.c_str());
+            std::filebuf* fb = fs.rdbuf();
+            assert(fb->sputc('r') == 'r');
+        }
     }
-    std::remove(temp.c_str());
     {
-        std::wofstream fs(temp.c_str());
-        std::wfilebuf* fb = fs.rdbuf();
-        assert(fb->sputc(L'r') == L'r');
+        libcxx_support::TempFileName temp;
+        {
+            std::wofstream fs(temp.c_str());
+            std::wfilebuf* fb = fs.rdbuf();
+            assert(fb->sputc(L'r') == L'r');
+        }
     }
-    std::remove(temp.c_str());
 }
Index: test/support/platform_support.h
===================================================================
--- test/support/platform_support.h
+++ test/support/platform_support.h
@@ -41,22 +41,49 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <algorithm> // std::remove
 #include <string>
 #if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
 #include <io.h> // _mktemp
+#else
+#include <stdlib.h> // mkstemp
+#include <errno.h>
 #endif
 
-inline
-std::string
-get_temp_file_name()
-{
-   std::string s("temp.XXXXXX");
+namespace libcxx_support {
+
+class TempFileName {
+public:
+  TempFileName() {
 #if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
-   _mktemp(&s[0]);
+    char Path[MAX_PATH+1];
+    char FN[MAX_PATH+1];
+    do { } while (0 == GetTempPath(MAX_PATH+1, Path));
+    do { } while (0 == GetTempFileName(Path, "libcxx", 0, FN));
+    Name = FN;
 #else
-   mktemp(&s[0]);
+    do {
+      Name = "libcxx.XXXXXX";
+      FD = mkstemp(&Name[0]);
+      assert(errno != EINVAL && "Something is wrong with the mkstemp's argument");
+    } while (errno == EEXIST);
+#endif
+  }
+  ~TempFileName() {
+#if !defined(_LIBCPP_MSVCRT) && !defined(__MINGW32__)
+   close(FD);
 #endif
-   return s;
+   std::remove(Name.c_str());
+  }
+  operator const std::string &() { return Name; }
+  const char *c_str() { return Name.c_str(); }
+private:
+  std::string Name;
+#if !defined(_LIBCPP_MSVCRT) && !defined(__MINGW32__)
+  int FD;
+#endif
+};
+
 }
 
 #endif // PLATFORM_SUPPORT_H
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to