[libcxx-commits] [libcxx] [libc++][test] Make filesystem_test_helper.h more portable to Windows (PR #74182)
Stephan T. Lavavej via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Dec 1 23:26:10 PST 2023
https://github.com/StephanTLavavej updated https://github.com/llvm/llvm-project/pull/74182
>From 49409dd213fbf859916137c7ce095a5992b7f0a1 Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at nuwen.net>
Date: Wed, 29 Nov 2023 21:25:57 -0800
Subject: [PATCH 1/3] Defend against windows.h macroizing max().
This header directly contains `#include <windows.h>`.
filesystem_test_helper.h(234,58): error: too few arguments provided to function-like macro invocation
---
libcxx/test/support/filesystem_test_helper.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libcxx/test/support/filesystem_test_helper.h b/libcxx/test/support/filesystem_test_helper.h
index a049efe03d844e8..6d83a196125122e 100644
--- a/libcxx/test/support/filesystem_test_helper.h
+++ b/libcxx/test/support/filesystem_test_helper.h
@@ -231,7 +231,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();
>From 2d2bb7c56178e7b454261aea92edfbaa9385baf0 Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at nuwen.net>
Date: Wed, 29 Nov 2023 21:34:53 -0800
Subject: [PATCH 2/3] Use _getcwd, _stat, _fileno, _chdir on Windows.
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'?
---
libcxx/test/support/filesystem_test_helper.h | 22 ++++++++++++++++++--
1 file changed, 20 insertions(+), 2 deletions(-)
diff --git a/libcxx/test/support/filesystem_test_helper.h b/libcxx/test/support/filesystem_test_helper.h
index 6d83a196125122e..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
@@ -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");
}
>From 801535e25a0e9b6bc0d095532ad4f05e07b2a88c Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at nuwen.net>
Date: Fri, 1 Dec 2023 23:25:24 -0800
Subject: [PATCH 3/3] Apply clang-format from CI.
---
libcxx/test/support/filesystem_test_helper.h | 25 +++++++++-----------
1 file changed, 11 insertions(+), 14 deletions(-)
diff --git a/libcxx/test/support/filesystem_test_helper.h b/libcxx/test/support/filesystem_test_helper.h
index 1dedbf5f22fdf5f..5f5641d4f2e72bd 100644
--- a/libcxx/test/support/filesystem_test_helper.h
+++ b/libcxx/test/support/filesystem_test_helper.h
@@ -147,11 +147,11 @@ namespace utils {
inline bool exists(std::string const& path) {
#ifdef _WIN32
- struct ::_stat tmp;
- return ::_stat(path.c_str(), &tmp) == 0;
+ struct ::_stat tmp;
+ return ::_stat(path.c_str(), &tmp) == 0;
#else
- struct ::stat tmp;
- return ::stat(path.c_str(), &tmp) == 0;
+ struct ::stat tmp;
+ return ::stat(path.c_str(), &tmp) == 0;
#endif
}
} // end namespace utils
@@ -238,12 +238,10 @@ struct scoped_test_env
std::string create_file(fs::path filename_path, std::uintmax_t size = 0) {
std::string filename = sanitize_path(filename_path.string());
- if (size >
- static_cast<typename std::make_unsigned<utils::off64_t>::type>(
- (std::numeric_limits<utils::off64_t>::max)())) {
- fprintf(stderr, "create_file(%s, %ju) too large\n",
- filename.c_str(), size);
- abort();
+ if (size > static_cast<typename std::make_unsigned<utils::off64_t>::type>(
+ (std::numeric_limits<utils::off64_t>::max)())) {
+ fprintf(stderr, "create_file(%s, %ju) too large\n", filename.c_str(), size);
+ abort();
}
#if defined(_WIN32) || defined(__MVS__)
@@ -265,10 +263,9 @@ struct scoped_test_env
#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);
- abort();
+ fprintf(stderr, "ftruncate %s %ju failed: %s\n", filename.c_str(), size, strerror(errno));
+ fclose(file);
+ abort();
}
fclose(file);
More information about the libcxx-commits
mailing list