From: Bob Wilson Date: Tue, 4 Nov 2014 22:28:48 +0000 (+0000) Subject: Use backslashes to escape spaces and other backslashes in -dwarf-debug-flags. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=257215d560bcfc01f26fce5283d77fc0f9586318;p=clang Use backslashes to escape spaces and other backslashes in -dwarf-debug-flags. The command line options are specified in a space-separated list that is an argument to -dwarf-debug-flags, so that breaks if there are spaces in the options. This feature came from Apple's internal version of GCC, so I went back to check how llvm-gcc handled this and matched that behavior. rdar://problem/18775420 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@221309 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp index d11fa4ea79..174df12c19 100644 --- a/lib/Driver/Tools.cpp +++ b/lib/Driver/Tools.cpp @@ -83,6 +83,23 @@ static void CheckCodeGenerationOptions(const Driver &D, const ArgList &Args) { << A->getAsString(Args) << "-static"; } +// Add backslashes to escape spaces and other backslashes. +// This is used for the space-separated argument list specified with +// the -dwarf-debug-flags option. +static void EscapeSpacesAndBackslashes(const char *Arg, + SmallVectorImpl &Res) { + for ( ; *Arg; ++Arg) { + switch (*Arg) { + default: break; + case ' ': + case '\\': + Res.push_back('\\'); + break; + } + Res.push_back(*Arg); + } +} + // Quote target names for inclusion in GNU Make dependency files. // Only the characters '$', '#', ' ', '\t' are quoted. static void QuoteTarget(StringRef Target, @@ -4465,8 +4482,10 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, SmallString<256> Flags; Flags += Exec; for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) { + SmallString<128> EscapedArg; + EscapeSpacesAndBackslashes(OriginalArgs[i], EscapedArg); Flags += " "; - Flags += OriginalArgs[i]; + Flags += EscapedArg; } CmdArgs.push_back("-dwarf-debug-flags"); CmdArgs.push_back(Args.MakeArgString(Flags.str())); @@ -4896,8 +4915,10 @@ void ClangAs::ConstructJob(Compilation &C, const JobAction &JA, const char *Exec = getToolChain().getDriver().getClangProgramPath(); Flags += Exec; for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) { + SmallString<128> EscapedArg; + EscapeSpacesAndBackslashes(OriginalArgs[i], EscapedArg); Flags += " "; - Flags += OriginalArgs[i]; + Flags += EscapedArg; } CmdArgs.push_back("-dwarf-debug-flags"); CmdArgs.push_back(Args.MakeArgString(Flags.str())); diff --git a/test/Driver/darwin-debug-flags.c b/test/Driver/darwin-debug-flags.c index f98e9ce7bd..20f1bbcfd6 100644 --- a/test/Driver/darwin-debug-flags.c +++ b/test/Driver/darwin-debug-flags.c @@ -1,4 +1,4 @@ -// RUN: env RC_DEBUG_OPTIONS=1 %clang -target i386-apple-darwin9 -g -Os %s -emit-llvm -S -o - | FileCheck %s +// RUN: env RC_DEBUG_OPTIONS=1 %clang -target i386-apple-darwin9 -I "path with \spaces" -g -Os %s -emit-llvm -S -o - | FileCheck %s // // RUN: touch %t.s // RUN: env RC_DEBUG_OPTIONS=1 %clang -### -target i386-apple-darwin9 -c -g %t.s 2>&1 | FileCheck -check-prefix=S %s @@ -6,6 +6,7 @@ // RUN: %clang -### -target i386-apple-darwin9 -c -g %t.s 2>&1 | FileCheck -check-prefix=P %s // CHECK: !0 = metadata !{ +// CHECK: -I path\5C with\5C \5C\5Cspaces // CHECK: -g -Os // CHECK: -mmacosx-version-min=10.5.0 // CHECK: [ DW_TAG_compile_unit ]