diff options
author | Hans Wennborg <hans@hanshq.net> | 2018-06-13 14:29:26 +0000 |
---|---|---|
committer | Hans Wennborg <hans@hanshq.net> | 2018-06-13 14:29:26 +0000 |
commit | 12ba9ec929c31977d6295cb89e0dbaf3d326a1af (patch) | |
tree | fdfe4ac13c8fca98139e163d012e53823caa1525 /llvm/lib/Support | |
parent | b391f2430378cd4d1bba41d04e6f60d890748f20 (diff) | |
download | bcm5719-llvm-12ba9ec929c31977d6295cb89e0dbaf3d326a1af.tar.gz bcm5719-llvm-12ba9ec929c31977d6295cb89e0dbaf3d326a1af.zip |
Do not enforce absolute path argv0 in windows
Even if we support no-canonical-prefix on
clang-cl(https://reviews.llvm.org/D47480), argv0 becomes absolute path
in clang-cl and that embeds absolute path in /showIncludes.
This patch removes such full path normalization from InitLLVM on
windows, and that removes absolute path from clang-cl output
(obj/stdout/stderr) when debug flag is disabled.
Patch by Takuto Ikuta!
Differential Revision https://reviews.llvm.org/D47578
llvm-svn: 334602
Diffstat (limited to 'llvm/lib/Support')
-rw-r--r-- | llvm/lib/Support/Windows/Process.inc | 68 |
1 files changed, 39 insertions, 29 deletions
diff --git a/llvm/lib/Support/Windows/Process.inc b/llvm/lib/Support/Windows/Process.inc index b1dbc5e3b0e..30126568769 100644 --- a/llvm/lib/Support/Windows/Process.inc +++ b/llvm/lib/Support/Windows/Process.inc @@ -209,55 +209,65 @@ static std::error_code WildcardExpand(const wchar_t *Arg, return ec; } -static std::error_code ExpandShortFileName(const wchar_t *Arg, - SmallVectorImpl<const char *> &Args, - BumpPtrAllocator &Alloc) { - SmallVector<wchar_t, MAX_PATH> LongPath; - DWORD Length = GetLongPathNameW(Arg, LongPath.data(), LongPath.capacity()); +static std::error_code GetExecutableName(SmallVectorImpl<char> &Filename) { + // 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]; + size_t Length = ::GetModuleFileNameW(NULL, ModuleName, MAX_PATH); + if (Length == 0 || Length == MAX_PATH) { + return mapWindowsError(GetLastError()); + } + + // 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. + Length = GetLongPathNameW(ModuleName, ModuleName, MAX_PATH); if (Length == 0) return mapWindowsError(GetLastError()); - if (Length > LongPath.capacity()) { + if (Length > MAX_PATH) { // We're not going to try to deal with paths longer than MAX_PATH, so we'll // treat this as an error. GetLastError() returns ERROR_SUCCESS, which // isn't useful, so we'll hardcode an appropriate error value. return mapWindowsError(ERROR_INSUFFICIENT_BUFFER); } - LongPath.set_size(Length); - return ConvertAndPushArg(LongPath.data(), Args, Alloc); + + std::error_code EC = windows::UTF16ToUTF8(ModuleName, Length, Filename); + if (EC) + return EC; + + StringRef Base = sys::path::filename(Filename.data()); + Filename.assign(Base.begin(), Base.end()); + return std::error_code(); } std::error_code windows::GetCommandLineArguments(SmallVectorImpl<const char *> &Args, BumpPtrAllocator &Alloc) { int ArgCount; - wchar_t **UnicodeCommandLine = - CommandLineToArgvW(GetCommandLineW(), &ArgCount); + std::unique_ptr<wchar_t *[], decltype(&LocalFree)> UnicodeCommandLine{ + CommandLineToArgvW(GetCommandLineW(), &ArgCount), &LocalFree}; if (!UnicodeCommandLine) 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; + std::error_code EC; - // 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); + Args.reserve(ArgCount); - for (int i = 1; i < ArgCount && !ec; ++i) { - ec = WildcardExpand(UnicodeCommandLine[i], Args, Alloc); - if (ec) - break; + for (int I = 0; I < ArgCount; ++I) { + EC = WildcardExpand(UnicodeCommandLine[I], Args, Alloc); + if (EC) + return EC; } - LocalFree(UnicodeCommandLine); - return ec; + SmallVector<char, MAX_PATH> Arg0(Args[0], Args[0] + strlen(Args[0])); + SmallVector<char, MAX_PATH> Filename; + sys::path::remove_filename(Arg0); + EC = GetExecutableName(Filename); + if (EC) + return EC; + sys::path::append(Arg0, Filename); + Args[0] = AllocateString(Arg0, Alloc); + return std::error_code(); } std::error_code Process::FixupStandardFileDescriptors() { |