llvm.org GIT mirror llvm / 7c4ee81
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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@334602 91177308-0d34-0410-b5e6-96231b3b80d8 Hans Wennborg 1 year, 4 months ago
2 changed file(s) with 61 addition(s) and 32 deletion(s). Raw diff Collapse all Expand all
208208 return ec;
209209 }
210210
211 static std::error_code ExpandShortFileName(const wchar_t *Arg,
212 SmallVectorImpl &Args,
213 BumpPtrAllocator &Alloc) {
214 SmallVector LongPath;
215 DWORD Length = GetLongPathNameW(Arg, LongPath.data(), LongPath.capacity());
211 static std::error_code GetExecutableName(SmallVectorImpl &Filename) {
212 // The first argument may contain just the name of the executable (e.g.,
213 // "clang") rather than the full path, so swap it with the full path.
214 wchar_t ModuleName[MAX_PATH];
215 size_t Length = ::GetModuleFileNameW(NULL, ModuleName, MAX_PATH);
216 if (Length == 0 || Length == MAX_PATH) {
217 return mapWindowsError(GetLastError());
218 }
219
220 // If the first argument is a shortened (8.3) name (which is possible even
221 // if we got the module name), the driver will have trouble distinguishing it
222 // (e.g., clang.exe v. clang++.exe), so expand it now.
223 Length = GetLongPathNameW(ModuleName, ModuleName, MAX_PATH);
216224 if (Length == 0)
217225 return mapWindowsError(GetLastError());
218 if (Length > LongPath.capacity()) {
226 if (Length > MAX_PATH) {
219227 // We're not going to try to deal with paths longer than MAX_PATH, so we'll
220228 // treat this as an error. GetLastError() returns ERROR_SUCCESS, which
221229 // isn't useful, so we'll hardcode an appropriate error value.
222230 return mapWindowsError(ERROR_INSUFFICIENT_BUFFER);
223231 }
224 LongPath.set_size(Length);
225 return ConvertAndPushArg(LongPath.data(), Args, Alloc);
232
233 std::error_code EC = windows::UTF16ToUTF8(ModuleName, Length, Filename);
234 if (EC)
235 return EC;
236
237 StringRef Base = sys::path::filename(Filename.data());
238 Filename.assign(Base.begin(), Base.end());
239 return std::error_code();
226240 }
227241
228242 std::error_code
229243 windows::GetCommandLineArguments(SmallVectorImpl &Args,
230244 BumpPtrAllocator &Alloc) {
231245 int ArgCount;
232 wchar_t **UnicodeCommandLine =
233 CommandLineToArgvW(GetCommandLineW(), &ArgCount);
246 std::unique_ptr UnicodeCommandLine{
247 CommandLineToArgvW(GetCommandLineW(), &ArgCount), &LocalFree};
234248 if (!UnicodeCommandLine)
235249 return mapWindowsError(::GetLastError());
236250
251 std::error_code EC;
252
237253 Args.reserve(ArgCount);
238 std::error_code ec;
239
240 // The first argument may contain just the name of the executable (e.g.,
241 // "clang") rather than the full path, so swap it with the full path.
242 wchar_t ModuleName[MAX_PATH];
243 int Length = ::GetModuleFileNameW(NULL, ModuleName, MAX_PATH);
244 if (0 < Length && Length < MAX_PATH)
245 UnicodeCommandLine[0] = ModuleName;
246
247 // If the first argument is a shortened (8.3) name (which is possible even
248 // if we got the module name), the driver will have trouble distinguishing it
249 // (e.g., clang.exe v. clang++.exe), so expand it now.
250 ec = ExpandShortFileName(UnicodeCommandLine[0], Args, Alloc);
251
252 for (int i = 1; i < ArgCount && !ec; ++i) {
253 ec = WildcardExpand(UnicodeCommandLine[i], Args, Alloc);
254 if (ec)
255 break;
256 }
257
258 LocalFree(UnicodeCommandLine);
259 return ec;
254
255 for (int I = 0; I < ArgCount; ++I) {
256 EC = WildcardExpand(UnicodeCommandLine[I], Args, Alloc);
257 if (EC)
258 return EC;
259 }
260
261 SmallVector Arg0(Args[0], Args[0] + strlen(Args[0]));
262 SmallVector Filename;
263 sys::path::remove_filename(Arg0);
264 EC = GetExecutableName(Filename);
265 if (EC)
266 return EC;
267 sys::path::append(Arg0, Filename);
268 Args[0] = AllocateString(Arg0, Alloc);
269 return std::error_code();
260270 }
261271
262272 std::error_code Process::FixupStandardFileDescriptors() {
1212 #include "llvm/ADT/Triple.h"
1313 #include "llvm/Config/config.h"
1414 #include "llvm/Support/FileSystem.h"
15 #include "llvm/Support/InitLLVM.h"
1516 #include "llvm/Support/Path.h"
1617 #include "llvm/Support/Program.h"
1718 #include "llvm/Support/StringSaver.h"
820821 EXPECT_TRUE(Errs.empty());
821822 }
822823
824 #ifdef _WIN32
825 TEST(CommandLineTest, GetCommandLineArguments) {
826 int argc = __argc;
827 char **argv = __argv;
828
829 // GetCommandLineArguments is called in InitLLVM.
830 llvm::InitLLVM X(argc, argv);
831
832 EXPECT_EQ(llvm::sys::path::is_absolute(argv[0]),
833 llvm::sys::path::is_absolute(__argv[0]));
834
835 EXPECT_TRUE(llvm::sys::path::filename(argv[0])
836 .equals_lower("supporttests.exe"))
837 << "Filename of test executable is "
838 << llvm::sys::path::filename(argv[0]);
839 }
840 #endif
841
823842 } // anonymous namespace