[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 03:12:10 PST 2015


chfast created this revision.
chfast added a subscriber: llvm-commits.
chfast set the repository for this revision to rL LLVM.

On Windows we have to take UTF16 encoded env vars and convert them to UTF8. This patch fixes CopyEnvironment helper function used by process unit tests.

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"
@@ -58,16 +59,33 @@
 ProgramTestStringArg2("program-test-string-arg2");
 
 static void CopyEnvironment(std::vector<const char *> &out) {
+#ifdef LLVM_ON_WIN32
+  // On Windows we have to take UTF16 encoded env vars and convert them to UTF8.
+  static std::vector<std::string> storage; // Helper storage for converted vars.
+  storage.clear();
+  _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
 #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
 }
 
 #ifdef LLVM_ON_WIN32


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


More information about the llvm-commits mailing list