[libcxx-commits] [libcxx] [libcxx] Add fallback to standard C when `unistd` is unavailable (PR #102005)

via libcxx-commits libcxx-commits at lists.llvm.org
Mon Aug 5 08:36:25 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Joseph Huber (jhuber6)

<details>
<summary>Changes</summary>

Summary:
This utility function gets a temp file to use for tests. It either uses
WIN32 or POSIX to create it. Some targets only follow the C standard,
and this test case will fail. This patch simply adds a fallback that
uses the `tmpnam` function from standard C. This function isn't ideal,
but it is good enough for our use-case.


---
Full diff: https://github.com/llvm/llvm-project/pull/102005.diff


1 Files Affected:

- (modified) libcxx/test/support/platform_support.h (+14-1) 


``````````diff
diff --git a/libcxx/test/support/platform_support.h b/libcxx/test/support/platform_support.h
index ba14b32e3e94d..b19e9a73c6597 100644
--- a/libcxx/test/support/platform_support.h
+++ b/libcxx/test/support/platform_support.h
@@ -15,6 +15,7 @@
 #define PLATFORM_SUPPORT_H
 
 #include "test_macros.h"
+#include <cerrno>
 
 // locale names
 #define LOCALE_en_US_UTF_8     "en_US.UTF-8"
@@ -40,8 +41,10 @@
 #   include <io.h> // _mktemp_s
 #   include <fcntl.h> // _O_EXCL, ...
 #   include <sys/stat.h> // _S_IREAD, ...
-#else
+#elif __has_include(<unistd.h>)
 #   include <unistd.h> // close
+#else
+#   define TEST_TEMP_FALLBACK
 #endif
 
 #if defined(_CS_GNU_LIBC_VERSION)
@@ -71,6 +74,16 @@ std::string get_temp_file_name()
             continue;
         abort();
     }
+#elif defined(TEST_TEMP_FALLBACK)
+    char *filename = tmpnam(nullptr);
+    if (!filename)
+      abort();
+    FILE *file = fopen(filename, "w");
+    if (!file)
+      abort();
+    if (fclose(file) == EOF)
+      abort();
+    return std::string(filename);
 #else
     std::string Name = "libcxx.XXXXXX";
     int FD = mkstemp(&Name[0]);

``````````

</details>


https://github.com/llvm/llvm-project/pull/102005


More information about the libcxx-commits mailing list