From 431b0a7646105c53c607cbf0015c615269bc5f11 Mon Sep 17 00:00:00 2001 From: Reid Kleckner Date: Thu, 18 Jul 2013 16:52:05 +0000 Subject: [Support] Beef up and expose the response file parsing in llvm::cl The plan is to use it for clang and lld. Major behavior changes: - We can now parse UTF-16 files that have a byte order mark. - PR16209: Don't drop backslashes on the floor if they don't escape anything. The actual parsing loop was based on code from Clang's driver.cpp, although it's been rewritten to track its state with control flow rather than state variables. Reviewers: hans Differential Revision: http://llvm-reviews.chandlerc.com/D1170 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@186587 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/Support/CommandLineTest.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'unittests/Support/CommandLineTest.cpp') diff --git a/unittests/Support/CommandLineTest.cpp b/unittests/Support/CommandLineTest.cpp index cd235d2..7a1c382 100644 --- a/unittests/Support/CommandLineTest.cpp +++ b/unittests/Support/CommandLineTest.cpp @@ -7,6 +7,7 @@ // //===----------------------------------------------------------------------===// +#include "llvm/ADT/STLExtras.h" #include "llvm/Support/CommandLine.h" #include "llvm/Config/config.h" #include "gtest/gtest.h" @@ -118,4 +119,27 @@ TEST(CommandLineTest, UseOptionCategory) { "Category."; } +class StrDupSaver : public cl::StringSaver { + const char *SaveString(const char *Str) LLVM_OVERRIDE { + return strdup(Str); + } +}; + +TEST(CommandLineTest, TokenizeGNUCommandLine) { + const char *Input = "foo\\ bar \"foo bar\" \'foo bar\' 'foo\\\\bar' " + "foo\"bar\"baz C:\\src\\foo.cpp \"C:\\src\\foo.cpp\""; + const char *const Output[] = { "foo bar", "foo bar", "foo bar", "foo\\bar", + "foobarbaz", "C:\\src\\foo.cpp", + "C:\\src\\foo.cpp" }; + SmallVector Actual; + StrDupSaver Saver; + cl::TokenizeGNUCommandLine(Input, Saver, Actual); + EXPECT_EQ(array_lengthof(Output), Actual.size()); + for (unsigned I = 0, E = Actual.size(); I != E; ++I) { + if (I < array_lengthof(Output)) + EXPECT_STREQ(Output[I], Actual[I]); + free(const_cast(Actual[I])); + } +} + } // anonymous namespace -- cgit v1.1 From 264e92d6db46083f9f46484ec39e99f18d35d370 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Tue, 30 Jul 2013 19:03:20 +0000 Subject: Implement TokenizeWindowsCommandLine. This is a follow up patch for r187390 to implement the parser for the Windows-style command line. This should follow the rule as described at http://msdn.microsoft.com/en-us/library/windows/desktop/17w5ykft(v=vs.85).aspx Differential Revision: http://llvm-reviews.chandlerc.com/D1235 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@187430 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/Support/CommandLineTest.cpp | 37 ++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) (limited to 'unittests/Support/CommandLineTest.cpp') diff --git a/unittests/Support/CommandLineTest.cpp b/unittests/Support/CommandLineTest.cpp index 7a1c382..c54e1b9 100644 --- a/unittests/Support/CommandLineTest.cpp +++ b/unittests/Support/CommandLineTest.cpp @@ -125,21 +125,40 @@ class StrDupSaver : public cl::StringSaver { } }; -TEST(CommandLineTest, TokenizeGNUCommandLine) { - const char *Input = "foo\\ bar \"foo bar\" \'foo bar\' 'foo\\\\bar' " - "foo\"bar\"baz C:\\src\\foo.cpp \"C:\\src\\foo.cpp\""; - const char *const Output[] = { "foo bar", "foo bar", "foo bar", "foo\\bar", - "foobarbaz", "C:\\src\\foo.cpp", - "C:\\src\\foo.cpp" }; +typedef void ParserFunction(StringRef Source, llvm::cl::StringSaver &Saver, + SmallVectorImpl &NewArgv); + + +void testCommandLineTokenizer(ParserFunction *parse, const char *Input, + const char *const Output[], size_t OutputSize) { SmallVector Actual; StrDupSaver Saver; - cl::TokenizeGNUCommandLine(Input, Saver, Actual); - EXPECT_EQ(array_lengthof(Output), Actual.size()); + parse(Input, Saver, Actual); + EXPECT_EQ(OutputSize, Actual.size()); for (unsigned I = 0, E = Actual.size(); I != E; ++I) { - if (I < array_lengthof(Output)) + if (I < OutputSize) EXPECT_STREQ(Output[I], Actual[I]); free(const_cast(Actual[I])); } } +TEST(CommandLineTest, TokenizeGNUCommandLine) { + const char *Input = "foo\\ bar \"foo bar\" \'foo bar\' 'foo\\\\bar' " + "foo\"bar\"baz C:\\src\\foo.cpp \"C:\\src\\foo.cpp\""; + const char *const Output[] = { "foo bar", "foo bar", "foo bar", "foo\\bar", + "foobarbaz", "C:\\src\\foo.cpp", + "C:\\src\\foo.cpp" }; + testCommandLineTokenizer(cl::TokenizeGNUCommandLine, Input, Output, + array_lengthof(Output)); +} + +TEST(CommandLineTest, TokenizeWindowsCommandLine) { + 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", + "lmn", "o", "pqr", "st \"u", "\\v" }; + testCommandLineTokenizer(cl::TokenizeWindowsCommandLine, Input, Output, + array_lengthof(Output)); +} + } // anonymous namespace -- cgit v1.1