diff unittests/Support/ProgramTest.cpp @ 148:63bd29f05246

merged
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 14 Aug 2019 19:46:37 +0900
parents c2174574ed3a
children
line wrap: on
line diff
--- a/unittests/Support/ProgramTest.cpp	Sun Dec 23 19:23:36 2018 +0900
+++ b/unittests/Support/ProgramTest.cpp	Wed Aug 14 19:46:37 2019 +0900
@@ -1,13 +1,13 @@
 //===- unittest/Support/ProgramTest.cpp -----------------------------------===//
 //
-//                     The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Support/Program.h"
+#include "llvm/Config/llvm-config.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/ConvertUTF.h"
 #include "llvm/Support/FileSystem.h"
@@ -26,7 +26,7 @@
 void sleep_for(unsigned int seconds) {
   sleep(seconds);
 }
-#elif defined(LLVM_ON_WIN32)
+#elif defined(_WIN32)
 #include <windows.h>
 void sleep_for(unsigned int seconds) {
   Sleep(seconds * 1000);
@@ -59,13 +59,13 @@
 ProgramTestStringArg2("program-test-string-arg2");
 
 class ProgramEnvTest : public testing::Test {
-  std::vector<const char *> EnvTable;
+  std::vector<StringRef> EnvTable;
   std::vector<std::string> EnvStorage;
 
 protected:
   void SetUp() override {
     auto EnvP = [] {
-#if defined(LLVM_ON_WIN32)
+#if defined(_WIN32)
       _wgetenv(L"TMP"); // Populate _wenviron, initially is null
       return _wenviron;
 #elif defined(__APPLE__)
@@ -76,8 +76,8 @@
     }();
     ASSERT_TRUE(EnvP);
 
-    auto prepareEnvVar = [this](decltype(*EnvP) Var) {
-#if defined(LLVM_ON_WIN32)
+    auto prepareEnvVar = [this](decltype(*EnvP) Var) -> StringRef {
+#if defined(_WIN32)
       // On Windows convert UTF16 encoded variable to UTF8
       auto Len = wcslen(Var);
       ArrayRef<char> Ref{reinterpret_cast<char const *>(Var),
@@ -85,10 +85,10 @@
       EnvStorage.emplace_back();
       auto convStatus = convertUTF16ToUTF8String(Ref, EnvStorage.back());
       EXPECT_TRUE(convStatus);
-      return EnvStorage.back().c_str();
+      return EnvStorage.back();
 #else
       (void)this;
-      return Var;
+      return StringRef(Var);
 #endif
     };
 
@@ -103,19 +103,12 @@
     EnvStorage.clear();
   }
 
-  void addEnvVar(const char *Var) {
-    ASSERT_TRUE(EnvTable.empty() || EnvTable.back()) << "Env table sealed";
-    EnvTable.emplace_back(Var);
-  }
+  void addEnvVar(StringRef Var) { EnvTable.emplace_back(Var); }
 
-  const char **getEnviron() {
-    if (EnvTable.back() != nullptr)
-      EnvTable.emplace_back(nullptr); // Seal table.
-    return &EnvTable[0];
-  }
+  ArrayRef<StringRef> getEnviron() const { return EnvTable; }
 };
 
-#ifdef LLVM_ON_WIN32
+#ifdef _WIN32
 TEST_F(ProgramEnvTest, CreateProcessLongPath) {
   if (getenv("LLVM_PROGRAM_TEST_LONG_PATH"))
     exit(0);
@@ -128,11 +121,8 @@
     MyExe.append("\\\\?\\");
   MyExe.append(MyAbsExe);
 
-  const char *ArgV[] = {
-    MyExe.c_str(),
-    "--gtest_filter=ProgramEnvTest.CreateProcessLongPath",
-    nullptr
-  };
+  StringRef ArgV[] = {MyExe,
+                      "--gtest_filter=ProgramEnvTest.CreateProcessLongPath"};
 
   // Add LLVM_PROGRAM_TEST_LONG_PATH to the environment of the child.
   addEnvVar("LLVM_PROGRAM_TEST_LONG_PATH=1");
@@ -172,13 +162,13 @@
 
   std::string my_exe =
       sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
-  const char *argv[] = {
-    my_exe.c_str(),
-    "--gtest_filter=ProgramEnvTest.CreateProcessTrailingSlash",
-    "-program-test-string-arg1", "has\\\\ trailing\\",
-    "-program-test-string-arg2", "has\\\\ trailing\\",
-    nullptr
-  };
+  StringRef argv[] = {
+      my_exe,
+      "--gtest_filter=ProgramEnvTest.CreateProcessTrailingSlash",
+      "-program-test-string-arg1",
+      "has\\\\ trailing\\",
+      "-program-test-string-arg2",
+      "has\\\\ trailing\\"};
 
   // Add LLVM_PROGRAM_TEST_CHILD to the environment of the child.
   addEnvVar("LLVM_PROGRAM_TEST_CHILD=1");
@@ -186,7 +176,7 @@
   std::string error;
   bool ExecutionFailed;
   // Redirect stdout and stdin to NUL, but let stderr through.
-#ifdef LLVM_ON_WIN32
+#ifdef _WIN32
   StringRef nul("NUL");
 #else
   StringRef nul("/dev/null");
@@ -209,11 +199,8 @@
 
   std::string Executable =
       sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
-  const char *argv[] = {
-    Executable.c_str(),
-    "--gtest_filter=ProgramEnvTest.TestExecuteNoWait",
-    nullptr
-  };
+  StringRef argv[] = {Executable,
+                      "--gtest_filter=ProgramEnvTest.TestExecuteNoWait"};
 
   // Add LLVM_PROGRAM_TEST_EXECUTE_NO_WAIT to the environment of the child.
   addEnvVar("LLVM_PROGRAM_TEST_EXECUTE_NO_WAIT=1");
@@ -267,11 +254,8 @@
 
   std::string Executable =
       sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
-  const char *argv[] = {
-    Executable.c_str(),
-    "--gtest_filter=ProgramEnvTest.TestExecuteAndWaitTimeout",
-    nullptr
-  };
+  StringRef argv[] = {
+      Executable, "--gtest_filter=ProgramEnvTest.TestExecuteAndWaitTimeout"};
 
   // Add LLVM_PROGRAM_TEST_TIMEOUT to the environment of the child.
  addEnvVar("LLVM_PROGRAM_TEST_TIMEOUT=1");
@@ -286,12 +270,12 @@
 
 TEST(ProgramTest, TestExecuteNegative) {
   std::string Executable = "i_dont_exist";
-  const char *argv[] = { Executable.c_str(), nullptr };
+  StringRef argv[] = {Executable};
 
   {
     std::string Error;
     bool ExecutionFailed;
-    int RetCode = ExecuteAndWait(Executable, argv, nullptr, {}, 0, 0, &Error,
+    int RetCode = ExecuteAndWait(Executable, argv, llvm::None, {}, 0, 0, &Error,
                                  &ExecutionFailed);
     ASSERT_TRUE(RetCode < 0) << "On error ExecuteAndWait should return 0 or "
                                 "positive value indicating the result code";
@@ -302,7 +286,7 @@
   {
     std::string Error;
     bool ExecutionFailed;
-    ProcessInfo PI = ExecuteNoWait(Executable, argv, nullptr, {}, 0, &Error,
+    ProcessInfo PI = ExecuteNoWait(Executable, argv, llvm::None, {}, 0, &Error,
                                    &ExecutionFailed);
     ASSERT_EQ(PI.Pid, ProcessInfo::InvalidPid)
         << "On error ExecuteNoWait should return an invalid ProcessInfo";
@@ -312,7 +296,7 @@
 
 }
 
-#ifdef LLVM_ON_WIN32
+#ifdef _WIN32
 const char utf16le_text[] =
     "\x6c\x00\x69\x00\x6e\x00\x67\x00\xfc\x00\x69\x00\xe7\x00\x61\x00";
 const char utf16be_text[] =
@@ -332,7 +316,7 @@
                                              sys::WEM_UTF16));
   int fd = 0;
   ASSERT_NO_ERROR(fs::openFileForRead(file_pathname.c_str(), fd));
-#if defined(LLVM_ON_WIN32)
+#if defined(_WIN32)
   char buf[18];
   ASSERT_EQ(::read(fd, buf, 18), 18);
   if (strncmp(buf, "\xfe\xff", 2) == 0) { // UTF16-BE