[libcxx-commits] [libcxx] [libc++][test] Make filesystem_test_helper.h more portable to Windows (PR #74182)

via libcxx-commits libcxx-commits at lists.llvm.org
Fri Dec 1 22:43:12 PST 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Stephan T. Lavavej (StephanTLavavej)

<details>
<summary>Changes</summary>

Found while running libc++'s test suite with MSVC's STL.

* `filesystem_test_helper.h` directly contains `#include <windows.h>`, so it should defend against `windows.h` macroizing `max()`. The easiest defense is to add extra parentheses, which has no effect on non-cursed platforms. This fixes:

```
filesystem_test_helper.h(234,58): error: too few arguments provided to function-like macro invocation
```

* Use `_getcwd`, `_stat`, `_fileno`, `_chdir` on Windows.

Our MSVC configuration doesn't provide the `meow` POSIX names by default (as they are Microsoft-deprecated); instead the `_meow` names should be used. For readability, I'm extracting `const int fd`. This fixes:

```
filesystem_test_helper.h(139,23): error: no member named 'getcwd' in the global namespace; did you mean '_getcwd'?
filesystem_test_helper.h(145,18): error: no struct named 'stat' in the global namespace
filesystem_test_helper.h(253,17): error: use of undeclared identifier 'fileno'; did you mean '_fileno'?
filesystem_test_helper.h(471,17): error: no member named 'chdir' in the global namespace; did you mean '_chdir'?
```


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


1 Files Affected:

- (modified) libcxx/test/support/filesystem_test_helper.h (+21-3) 


``````````diff
diff --git a/libcxx/test/support/filesystem_test_helper.h b/libcxx/test/support/filesystem_test_helper.h
index a049efe03d844e8..1dedbf5f22fdf5f 100644
--- a/libcxx/test/support/filesystem_test_helper.h
+++ b/libcxx/test/support/filesystem_test_helper.h
@@ -136,14 +136,23 @@ namespace utils {
         // Assume that path lengths are not greater than this.
         // This should be fine for testing purposes.
         char buf[4096];
+#ifdef _WIN32
+        char* ret = ::_getcwd(buf, sizeof(buf));
+#else
         char* ret = ::getcwd(buf, sizeof(buf));
+#endif
         assert(ret && "getcwd failed");
         return std::string(ret);
     }
 
     inline bool exists(std::string const& path) {
+#ifdef _WIN32
+        struct ::_stat tmp;
+        return ::_stat(path.c_str(), &tmp) == 0;
+#else
         struct ::stat tmp;
         return ::stat(path.c_str(), &tmp) == 0;
+#endif
     }
 } // end namespace utils
 
@@ -231,7 +240,7 @@ struct scoped_test_env
 
         if (size >
             static_cast<typename std::make_unsigned<utils::off64_t>::type>(
-                std::numeric_limits<utils::off64_t>::max())) {
+                (std::numeric_limits<utils::off64_t>::max)())) {
             fprintf(stderr, "create_file(%s, %ju) too large\n",
                     filename.c_str(), size);
             abort();
@@ -249,8 +258,13 @@ struct scoped_test_env
             abort();
         }
 
-        if (utils::ftruncate64(
-                fileno(file), static_cast<utils::off64_t>(size)) == -1) {
+#ifdef _WIN32
+        const int fd = _fileno(file);
+#else
+        const int fd = fileno(file);
+#endif
+
+        if (utils::ftruncate64(fd, static_cast<utils::off64_t>(size)) == -1) {
             fprintf(stderr, "ftruncate %s %ju failed: %s\n", filename.c_str(),
                     size, strerror(errno));
             fclose(file);
@@ -468,7 +482,11 @@ struct CWDGuard {
   std::string oldCwd_;
   CWDGuard() : oldCwd_(utils::getcwd()) { }
   ~CWDGuard() {
+#ifdef _WIN32
+    int ret = ::_chdir(oldCwd_.c_str());
+#else
     int ret = ::chdir(oldCwd_.c_str());
+#endif
     assert(ret == 0 && "chdir failed");
   }
 

``````````

</details>


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


More information about the libcxx-commits mailing list