From 53ebef63afa57b2c46b86c184aafc2c04aff7f40 Mon Sep 17 00:00:00 2001 From: Sunil Srivastava Date: Thu, 14 Mar 2019 19:26:04 +0000 Subject: [PATCH] Handle consecutive-double-quotes in Windows argument parsing Windows command line argument processing treats consecutive double quotes as a single double-quote. This patch implements this functionality. Differential Revision: https://reviews.llvm.org/D58662 llvm-svn: 356193 --- lib/Support/CommandLine.cpp | 7 +++++++ unittests/Support/CommandLineTest.cpp | 9 ++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/Support/CommandLine.cpp b/lib/Support/CommandLine.cpp index 39991a8905d..c1193f90e8f 100644 --- a/lib/Support/CommandLine.cpp +++ b/lib/Support/CommandLine.cpp @@ -874,6 +874,13 @@ void cl::TokenizeWindowsCommandLine(StringRef Src, StringSaver &Saver, // QUOTED state means that it's reading a token quoted by double quotes. if (State == QUOTED) { if (C == '"') { + if (I < (E - 1) && Src[I + 1] == '"') { + // Consecutive double-quotes inside a quoted string implies one + // double-quote. + Token.push_back('"'); + I = I + 1; + continue; + } State = UNQUOTED; continue; } diff --git a/unittests/Support/CommandLineTest.cpp b/unittests/Support/CommandLineTest.cpp index 4bfa8a35a16..a7aee41244f 100644 --- a/unittests/Support/CommandLineTest.cpp +++ b/unittests/Support/CommandLineTest.cpp @@ -185,7 +185,7 @@ TEST(CommandLineTest, TokenizeGNUCommandLine) { array_lengthof(Output)); } -TEST(CommandLineTest, TokenizeWindowsCommandLine) { +TEST(CommandLineTest, TokenizeWindowsCommandLine1) { const char Input[] = "a\\b c\\\\d e\\\\\"f g\" h\\\"i j\\\\\\\"k \"lmn\" o pqr " "\"st \\\"u\" \\v"; const char *const Output[] = { "a\\b", "c\\\\d", "e\\f g", "h\"i", "j\\\"k", @@ -194,6 +194,13 @@ TEST(CommandLineTest, TokenizeWindowsCommandLine) { array_lengthof(Output)); } +TEST(CommandLineTest, TokenizeWindowsCommandLine2) { + const char Input[] = "clang -c -DFOO=\"\"\"ABC\"\"\" x.cpp"; + const char *const Output[] = { "clang", "-c", "-DFOO=\"ABC\"", "x.cpp"}; + testCommandLineTokenizer(cl::TokenizeWindowsCommandLine, Input, Output, + array_lengthof(Output)); +} + TEST(CommandLineTest, TokenizeConfigFile1) { const char *Input = "\\"; const char *const Output[] = { "\\" };