From 006bce77640690b2b4b539ec47eec2deaf190d1e Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Sun, 21 Jun 2015 06:31:53 +0000 Subject: [PATCH] ArrayRef-ify ParseArgs git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@240233 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Option/OptTable.h | 7 +++--- lib/LibDriver/LibDriver.cpp | 4 ++-- lib/Option/OptTable.cpp | 7 +++--- unittests/Option/OptionParsingTest.cpp | 30 ++++++++++---------------- 4 files changed, 19 insertions(+), 29 deletions(-) diff --git a/include/llvm/Option/OptTable.h b/include/llvm/Option/OptTable.h index a7ff46919cd..e23d6cdf60c 100644 --- a/include/llvm/Option/OptTable.h +++ b/include/llvm/Option/OptTable.h @@ -10,6 +10,7 @@ #ifndef LLVM_OPTION_OPTTABLE_H #define LLVM_OPTION_OPTTABLE_H +#include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/StringSet.h" #include "llvm/Option/OptSpecifier.h" @@ -152,10 +153,8 @@ public: /// is the default and means exclude nothing. /// \return An InputArgList; on error this will contain all the options /// which could be parsed. - InputArgList *ParseArgs(const char* const *ArgBegin, - const char* const *ArgEnd, - unsigned &MissingArgIndex, - unsigned &MissingArgCount, + InputArgList *ParseArgs(ArrayRef Args, + unsigned &MissingArgIndex, unsigned &MissingArgCount, unsigned FlagsToInclude = 0, unsigned FlagsToExclude = 0) const; diff --git a/lib/LibDriver/LibDriver.cpp b/lib/LibDriver/LibDriver.cpp index c9857b0493d..0b7c4756d0a 100644 --- a/lib/LibDriver/LibDriver.cpp +++ b/lib/LibDriver/LibDriver.cpp @@ -114,8 +114,8 @@ int llvm::libDriverMain(int Argc, const char **Argv) { LibOptTable Table; unsigned MissingIndex; unsigned MissingCount; - std::unique_ptr Args( - Table.ParseArgs(&Argv[1], &Argv[Argc], MissingIndex, MissingCount)); + std::unique_ptr Args(Table.ParseArgs( + makeArrayRef(Argv, Argc).slice(1), MissingIndex, MissingCount)); if (MissingCount) { llvm::errs() << "missing arg value for \"" << Args->getArgString(MissingIndex) diff --git a/lib/Option/OptTable.cpp b/lib/Option/OptTable.cpp index c37f193fa64..cec1717454f 100644 --- a/lib/Option/OptTable.cpp +++ b/lib/Option/OptTable.cpp @@ -247,18 +247,17 @@ Arg *OptTable::ParseOneArg(const ArgList &Args, unsigned &Index, return new Arg(getOption(TheUnknownOptionID), Str, Index++, Str); } -InputArgList *OptTable::ParseArgs(const char *const *ArgBegin, - const char *const *ArgEnd, +InputArgList *OptTable::ParseArgs(ArrayRef ArgArr, unsigned &MissingArgIndex, unsigned &MissingArgCount, unsigned FlagsToInclude, unsigned FlagsToExclude) const { - InputArgList *Args = new InputArgList(ArgBegin, ArgEnd); + InputArgList *Args = new InputArgList(ArgArr.begin(), ArgArr.end()); // FIXME: Handle '@' args (or at least error on them). MissingArgIndex = MissingArgCount = 0; - unsigned Index = 0, End = ArgEnd - ArgBegin; + unsigned Index = 0, End = ArgArr.size(); while (Index < End) { // Ingore nullptrs, they are response file's EOL markers if (Args->getArgString(Index) == nullptr) { diff --git a/unittests/Option/OptionParsingTest.cpp b/unittests/Option/OptionParsingTest.cpp index 521009a9e66..671cf6de0cb 100644 --- a/unittests/Option/OptionParsingTest.cpp +++ b/unittests/Option/OptionParsingTest.cpp @@ -67,8 +67,7 @@ const char *Args[] = { TEST(Option, OptionParsing) { TestOptTable T; unsigned MAI, MAC; - std::unique_ptr AL( - T.ParseArgs(std::begin(Args), std::end(Args), MAI, MAC)); + std::unique_ptr AL(T.ParseArgs(Args, MAI, MAC)); // Check they all exist. EXPECT_TRUE(AL->hasArg(OPT_A)); @@ -114,7 +113,7 @@ TEST(Option, ParseWithFlagExclusions) { std::unique_ptr AL; // Exclude flag3 to avoid parsing as OPT_SLASH_C. - AL.reset(T.ParseArgs(std::begin(Args), std::end(Args), MAI, MAC, + AL.reset(T.ParseArgs(Args, MAI, MAC, /*FlagsToInclude=*/0, /*FlagsToExclude=*/OptFlag3)); EXPECT_TRUE(AL->hasArg(OPT_A)); @@ -122,7 +121,7 @@ TEST(Option, ParseWithFlagExclusions) { EXPECT_FALSE(AL->hasArg(OPT_SLASH_C)); // Exclude flag1 to avoid parsing as OPT_C. - AL.reset(T.ParseArgs(std::begin(Args), std::end(Args), MAI, MAC, + AL.reset(T.ParseArgs(Args, MAI, MAC, /*FlagsToInclude=*/0, /*FlagsToExclude=*/OptFlag1)); EXPECT_TRUE(AL->hasArg(OPT_B)); @@ -130,7 +129,7 @@ TEST(Option, ParseWithFlagExclusions) { EXPECT_TRUE(AL->hasArg(OPT_SLASH_C)); const char *NewArgs[] = { "/C", "foo", "--C=bar" }; - AL.reset(T.ParseArgs(std::begin(NewArgs), std::end(NewArgs), MAI, MAC)); + AL.reset(T.ParseArgs(NewArgs, MAI, MAC)); EXPECT_TRUE(AL->hasArg(OPT_SLASH_C)); EXPECT_TRUE(AL->hasArg(OPT_C)); EXPECT_EQ(AL->getLastArgValue(OPT_SLASH_C), "foo"); @@ -142,8 +141,7 @@ TEST(Option, ParseAliasInGroup) { unsigned MAI, MAC; const char *MyArgs[] = { "-I" }; - std::unique_ptr AL( - T.ParseArgs(std::begin(MyArgs), std::end(MyArgs), MAI, MAC)); + std::unique_ptr AL(T.ParseArgs(MyArgs, MAI, MAC)); EXPECT_TRUE(AL->hasArg(OPT_H)); } @@ -152,8 +150,7 @@ TEST(Option, AliasArgs) { unsigned MAI, MAC; const char *MyArgs[] = { "-J", "-Joo" }; - std::unique_ptr AL( - T.ParseArgs(std::begin(MyArgs), std::end(MyArgs), MAI, MAC)); + std::unique_ptr AL(T.ParseArgs(MyArgs, MAI, MAC)); EXPECT_TRUE(AL->hasArg(OPT_B)); EXPECT_EQ(AL->getAllArgValues(OPT_B)[0], "foo"); EXPECT_EQ(AL->getAllArgValues(OPT_B)[1], "bar"); @@ -164,8 +161,7 @@ TEST(Option, IgnoreCase) { unsigned MAI, MAC; const char *MyArgs[] = { "-a", "-joo" }; - std::unique_ptr AL( - T.ParseArgs(std::begin(MyArgs), std::end(MyArgs), MAI, MAC)); + std::unique_ptr AL(T.ParseArgs(MyArgs, MAI, MAC)); EXPECT_TRUE(AL->hasArg(OPT_A)); EXPECT_TRUE(AL->hasArg(OPT_B)); } @@ -175,8 +171,7 @@ TEST(Option, DoNotIgnoreCase) { unsigned MAI, MAC; const char *MyArgs[] = { "-a", "-joo" }; - std::unique_ptr AL( - T.ParseArgs(std::begin(MyArgs), std::end(MyArgs), MAI, MAC)); + std::unique_ptr AL(T.ParseArgs(MyArgs, MAI, MAC)); EXPECT_FALSE(AL->hasArg(OPT_A)); EXPECT_FALSE(AL->hasArg(OPT_B)); } @@ -186,8 +181,7 @@ TEST(Option, SlurpEmpty) { unsigned MAI, MAC; const char *MyArgs[] = { "-A", "-slurp" }; - std::unique_ptr AL( - T.ParseArgs(std::begin(MyArgs), std::end(MyArgs), MAI, MAC)); + std::unique_ptr AL(T.ParseArgs(MyArgs, MAI, MAC)); EXPECT_TRUE(AL->hasArg(OPT_A)); EXPECT_TRUE(AL->hasArg(OPT_Slurp)); EXPECT_EQ(AL->getAllArgValues(OPT_Slurp).size(), 0U); @@ -198,8 +192,7 @@ TEST(Option, Slurp) { unsigned MAI, MAC; const char *MyArgs[] = { "-A", "-slurp", "-B", "--", "foo" }; - std::unique_ptr AL( - T.ParseArgs(std::begin(MyArgs), std::end(MyArgs), MAI, MAC)); + std::unique_ptr AL(T.ParseArgs(MyArgs, MAI, MAC)); EXPECT_EQ(AL->size(), 2U); EXPECT_TRUE(AL->hasArg(OPT_A)); EXPECT_FALSE(AL->hasArg(OPT_B)); @@ -216,8 +209,7 @@ TEST(Option, FlagAliasToJoined) { // Check that a flag alias provides an empty argument to a joined option. const char *MyArgs[] = { "-K" }; - std::unique_ptr AL( - T.ParseArgs(std::begin(MyArgs), std::end(MyArgs), MAI, MAC)); + std::unique_ptr AL(T.ParseArgs(MyArgs, MAI, MAC)); EXPECT_EQ(AL->size(), 1U); EXPECT_TRUE(AL->hasArg(OPT_B)); EXPECT_EQ(AL->getAllArgValues(OPT_B).size(), 1U);