[PATCH] D14278: Fix unit tests on Windows: handle env vars with non-ASCII chars.

Paweł Bylica via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 3 06:13:46 PST 2015


chfast updated this revision to Diff 39053.
chfast added a comment.

Use local temporary storage for processing env vars on Windows.


Repository:
  rL LLVM

http://reviews.llvm.org/D14278

Files:
  unittests/Support/ProgramTest.cpp

Index: unittests/Support/ProgramTest.cpp
===================================================================
--- unittests/Support/ProgramTest.cpp
+++ unittests/Support/ProgramTest.cpp
@@ -7,6 +7,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/Support/ConvertUTF.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/Path.h"
@@ -57,17 +58,36 @@
 static cl::opt<std::string>
 ProgramTestStringArg2("program-test-string-arg2");
 
-static void CopyEnvironment(std::vector<const char *> &out) {
+static std::vector<const char *>
+CopyEnvironment(std::vector<std::string> &Storage) {
+  std::vector<const char *> out;
+#ifdef LLVM_ON_WIN32
+  // On Windows we have to take UTF16 encoded env vars and convert them to UTF8.
+  _wgetenv(L"TMP"); // Populate _wenviron, initially is null
+  wchar_t **envp = _wenviron;
+  assert(envp);
+  while (*envp != nullptr) {
+    auto pathLen = wcslen(*envp);
+    ArrayRef<char> ref{reinterpret_cast<char const *>(*envp),
+      pathLen * sizeof(wchar_t)};
+    Storage.emplace_back();
+    convertUTF16ToUTF8String(ref, Storage.back());
+    out.emplace_back(Storage.back().c_str());
+    ++envp;
+  }
+#else
+  (void)Storage;
 #ifdef __APPLE__
   char **envp = *_NSGetEnviron();
 #else
-  // environ seems to work for Windows and most other Unices.
   char **envp = environ;
 #endif
   while (*envp != nullptr) {
     out.push_back(*envp);
     ++envp;
   }
+#endif
+  return out;
 }
 
 #ifdef LLVM_ON_WIN32
@@ -90,8 +110,8 @@
   };
 
   // Add LLVM_PROGRAM_TEST_LONG_PATH to the environment of the child.
-  std::vector<const char *> EnvP;
-  CopyEnvironment(EnvP);
+  std::vector<std::string> EnvStorage;
+  auto EnvP = CopyEnvironment(EnvStorage);
   EnvP.push_back("LLVM_PROGRAM_TEST_LONG_PATH=1");
   EnvP.push_back(nullptr);
 
@@ -140,8 +160,8 @@
   };
 
   // Add LLVM_PROGRAM_TEST_CHILD to the environment of the child.
-  std::vector<const char *> envp;
-  CopyEnvironment(envp);
+  std::vector<std::string> EnvStorage;
+  auto envp = CopyEnvironment(EnvStorage);
   envp.push_back("LLVM_PROGRAM_TEST_CHILD=1");
   envp.push_back(nullptr);
 
@@ -178,8 +198,8 @@
   };
 
   // Add LLVM_PROGRAM_TEST_EXECUTE_NO_WAIT to the environment of the child.
-  std::vector<const char *> envp;
-  CopyEnvironment(envp);
+  std::vector<std::string> EnvStorage;
+  auto envp = CopyEnvironment(EnvStorage);
   envp.push_back("LLVM_PROGRAM_TEST_EXECUTE_NO_WAIT=1");
   envp.push_back(nullptr);
 
@@ -239,8 +259,8 @@
   };
 
   // Add LLVM_PROGRAM_TEST_TIMEOUT to the environment of the child.
-  std::vector<const char *> envp;
-  CopyEnvironment(envp);
+  std::vector<std::string> EnvStorage;
+  auto envp = CopyEnvironment(EnvStorage);
   envp.push_back("LLVM_PROGRAM_TEST_TIMEOUT=1");
   envp.push_back(nullptr);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D14278.39053.patch
Type: text/x-patch
Size: 2857 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151103/4ecbbc27/attachment.bin>


More information about the llvm-commits mailing list