summaryrefslogtreecommitdiffstats
path: root/llvm/unittests/Support/ProcessTest.cpp
diff options
context:
space:
mode:
authorRui Ueyama <ruiu@google.com>2013-09-10 19:45:51 +0000
committerRui Ueyama <ruiu@google.com>2013-09-10 19:45:51 +0000
commit471d0c57e789b57302329761f678e48dffc8d41d (patch)
tree0045e4ae4f9bdb12e6c3844a8123761836955974 /llvm/unittests/Support/ProcessTest.cpp
parent934f6f39f49febece76d5532346ba5bb297ad7ea (diff)
downloadbcm5719-llvm-471d0c57e789b57302329761f678e48dffc8d41d.tar.gz
bcm5719-llvm-471d0c57e789b57302329761f678e48dffc8d41d.zip
Add getenv() wrapper that works on multibyte environment variable.
On Windows, character encoding of multibyte environment variable varies depending on settings. The only reliable way to handle it I think is to use GetEnvironmentVariableW(). GetEnvironmentVariableW() works on wchar_t string, which is on Windows UTF16 string. That's not ideal because we use UTF-8 as the internal encoding in LLVM. This patch defines a wrapper function which takes and returns UTF-8 string for GetEnvironmentVariableW(). The wrapper function does not do any conversion and just forwards the argument to getenv() on Unix. Differential Revision: http://llvm-reviews.chandlerc.com/D1612 llvm-svn: 190423
Diffstat (limited to 'llvm/unittests/Support/ProcessTest.cpp')
-rw-r--r--llvm/unittests/Support/ProcessTest.cpp28
1 files changed, 28 insertions, 0 deletions
diff --git a/llvm/unittests/Support/ProcessTest.cpp b/llvm/unittests/Support/ProcessTest.cpp
index eff9c711a1a..ac1b01e88bf 100644
--- a/llvm/unittests/Support/ProcessTest.cpp
+++ b/llvm/unittests/Support/ProcessTest.cpp
@@ -39,4 +39,32 @@ TEST(ProcessTest, SelfProcess) {
EXPECT_GT(TimeValue::MaxTime, process::get_self()->get_wall_time());
}
+#ifdef LLVM_ON_WIN32
+#define setenv(name, var, ignore) _putenv_s(name, var)
+#endif
+
+#if HAVE_SETENV || defined(LLVM_ON_WIN32)
+TEST(ProcessTest, Basic) {
+ setenv("__LLVM_TEST_ENVIRON_VAR__", "abc", true);
+ Optional<std::string> val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__"));
+ EXPECT_TRUE(val.hasValue());
+ EXPECT_STREQ("abc", val->c_str());
+}
+
+TEST(ProcessTest, None) {
+ Optional<std::string> val(
+ Process::GetEnv("__LLVM_TEST_ENVIRON_NO_SUCH_VAR__"));
+ EXPECT_FALSE(val.hasValue());
+}
+#endif
+
+#ifdef LLVM_ON_WIN32
+TEST(ProcessTest, Wchar) {
+ SetEnvironmentVariableW(L"__LLVM_TEST_ENVIRON_VAR__", L"abcdefghijklmnopqrs");
+ Optional<std::string> val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__"));
+ EXPECT_TRUE(val.hasValue());
+ EXPECT_STREQ("abcdefghijklmnopqrs", val->c_str());
+}
+#endif
+
} // end anonymous namespace
OpenPOWER on IntegriCloud