takuto.ikuta updated this revision to Diff 149394.
takuto.ikuta added a comment.

Add test and split function


https://reviews.llvm.org/D47578

Files:
  llvm/lib/Support/Windows/Process.inc
  llvm/unittests/Support/CommandLineTest.cpp

Index: llvm/unittests/Support/CommandLineTest.cpp
===================================================================
--- llvm/unittests/Support/CommandLineTest.cpp
+++ llvm/unittests/Support/CommandLineTest.cpp
@@ -13,6 +13,7 @@
 #include "llvm/ADT/Triple.h"
 #include "llvm/Config/config.h"
 #include "llvm/Support/FileSystem.h"
+#include "llvm/Support/InitLLVM.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/Program.h"
 #include "llvm/Support/StringSaver.h"
@@ -821,4 +822,22 @@
   EXPECT_TRUE(Errs.empty());
 }
 
+#ifdef _WIN32
+TEST(CommandLineTest, GetCommandLineArguments) {
+  int argc = __argc;
+  char **argv = __argv;
+
+  // GetCommandLineArguments is called in InitLLVM.
+  llvm::InitLLVM X(argc, argv);
+
+  EXPECT_EQ(llvm::sys::path::is_absolute(argv[0]),
+            llvm::sys::path::is_absolute(__argv[0]));
+
+  EXPECT_TRUE(llvm::sys::path::filename(argv[0])
+              .equals_lower("supporttests.exe"))
+      << "Filename of test executable is "
+      << llvm::sys::path::filename(argv[0]);
+}
+#endif
+
 }  // anonymous namespace
Index: llvm/lib/Support/Windows/Process.inc
===================================================================
--- llvm/lib/Support/Windows/Process.inc
+++ llvm/lib/Support/Windows/Process.inc
@@ -209,9 +209,7 @@
 }
 
 static std::error_code ExpandShortFileName(const wchar_t *Arg,
-                                           SmallVectorImpl<const char *> &Args,
-                                           BumpPtrAllocator &Alloc) {
-  SmallVector<wchar_t, MAX_PATH> LongPath;
+                                           SmallVectorImpl<wchar_t> &LongPath) {
   DWORD Length = GetLongPathNameW(Arg, LongPath.data(), LongPath.capacity());
   if (Length == 0)
     return mapWindowsError(GetLastError());
@@ -222,7 +220,53 @@
     return mapWindowsError(ERROR_INSUFFICIENT_BUFFER);
   }
   LongPath.set_size(Length);
-  return ConvertAndPushArg(LongPath.data(), Args, Alloc);
+  return std::error_code();
+}
+
+static std::error_code GetLongArgv0FullPath(const wchar_t *Argv0,
+                                            SmallVectorImpl<wchar_t> &LongArgv0) {
+  // The first argument may contain just the name of the executable (e.g.,
+  // "clang") rather than the full path, so swap it with the full path.
+  wchar_t ModuleName[MAX_PATH];
+  int Length = ::GetModuleFileNameW(NULL, ModuleName, MAX_PATH);
+  if (0 < Length && Length < MAX_PATH)
+    Argv0 = ModuleName;
+
+  // If the first argument is a shortened (8.3) name (which is possible even
+  // if we got the module name), the driver will have trouble distinguishing it
+  // (e.g., clang.exe v. clang++.exe), so expand it now.
+  return ExpandShortFileName(Argv0, LongArgv0);
+}
+
+static std::error_code GetLongArgv0Path(const wchar_t *Argv0,
+                                        SmallVectorImpl<wchar_t> &LongArgv0) {
+  SmallVector<wchar_t, MAX_PATH> UTF16LongArgv0Full;
+
+  std::error_code ec = GetLongArgv0FullPath(Argv0, UTF16LongArgv0Full);
+  if (ec)
+    return ec;
+
+  // Replace filename in original argv0 with expanded filename.
+  // This may change argv0 like below,
+  // * clang -> clang.exe (just add extension)
+  // * CLANG_~1.EXE -> clang++.exe (extend shorname)
+  // This is for keeping relativeness of original argv0 path.
+  SmallVector<char, MAX_PATH> UTF8LongArgv0Full;
+  ec = windows::UTF16ToUTF8(UTF16LongArgv0Full.data(), UTF16LongArgv0Full.size(),
+                            UTF8LongArgv0Full);
+  if (ec)
+    return ec;
+
+  SmallVector<char, MAX_PATH> UTF8Argv0;
+  ec = windows::UTF16ToUTF8(Argv0, wcslen(Argv0), UTF8Argv0);
+  if (ec)
+    return ec;
+
+  sys::path::remove_filename(UTF8Argv0);
+  sys::path::append(UTF8Argv0, sys::path::filename(UTF8LongArgv0Full.data()));
+
+  return windows::UTF8ToUTF16(StringRef(UTF8Argv0.data(), UTF8Argv0.size()),
+                              LongArgv0);
 }
 
 std::error_code
@@ -235,19 +279,11 @@
     return mapWindowsError(::GetLastError());
 
   Args.reserve(ArgCount);
-  std::error_code ec;
-
-  // The first argument may contain just the name of the executable (e.g.,
-  // "clang") rather than the full path, so swap it with the full path.
-  wchar_t ModuleName[MAX_PATH];
-  int Length = ::GetModuleFileNameW(NULL, ModuleName, MAX_PATH);
-  if (0 < Length && Length < MAX_PATH)
-    UnicodeCommandLine[0] = ModuleName;
+  SmallVector<wchar_t, MAX_PATH> LongArgv0;
+  std::error_code ec = GetLongArgv0Path(UnicodeCommandLine[0], LongArgv0);
 
-  // If the first argument is a shortened (8.3) name (which is possible even
-  // if we got the module name), the driver will have trouble distinguishing it
-  // (e.g., clang.exe v. clang++.exe), so expand it now.
-  ec = ExpandShortFileName(UnicodeCommandLine[0], Args, Alloc);
+  if (!ec)
+    ec = ConvertAndPushArg(LongArgv0.data(), Args, Alloc);
 
   for (int i = 1; i < ArgCount && !ec; ++i) {
     ec = WildcardExpand(UnicodeCommandLine[i], Args, Alloc);
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to