From 2012d744f49715ff6a3eea5c63e722e11e8674e9 Mon Sep 17 00:00:00 2001 From: Zachary Turner Date: Tue, 28 Jun 2016 20:09:47 +0000 Subject: Update llvm command line parser to support subcommands. This allows command line tools to use syntaxes like the following: llvm-foo.exe command1 -o1 -o2 llvm-foo.exe command2 -p1 -p2 Where command1 and command2 contain completely different sets of valid options. This is backwards compatible with previous uses of llvm cl which did not support subcommands, as any option which specifies no optional subcommand (e.g. all existing code) goes into a special "top level" subcommand that expects dashed options to appear immediately after the program name. For example, code which is subcommand unaware would generate a command line such as the following, where no subcommand is specified: llvm-foo.exe -q1 -q2 The top level subcommand can co-exist with actual subcommands, as it is implemented as an actual subcommand which is searched if no explicit subcommand is specified. So llvm-foo.exe as specified above could be written so as to support all three aforementioned command lines simultaneously. There is one additional "special" subcommand called AllSubCommands, which can be used to inject an option into every subcommand. This is useful to support things like help, so that commands such as: llvm-foo.exe --help llvm-foo.exe command1 --help llvm-foo.exe command2 --help All work and display the help for the selected subcommand without having to explicitly go and write code to handle each one separately. This patch is submitted without an example of anything actually using subcommands, but a followup patch will convert the llvm-pdbdump tool to use subcommands. Reviewed By: beanz Differential Revision: http://reviews.llvm.org/D21485 llvm-svn: 274054 --- llvm/unittests/Support/CommandLineTest.cpp | 204 ++++++++++++++++++++++++++++- llvm/unittests/Support/ProgramTest.cpp | 4 +- 2 files changed, 203 insertions(+), 5 deletions(-) (limited to 'llvm/unittests/Support') diff --git a/llvm/unittests/Support/CommandLineTest.cpp b/llvm/unittests/Support/CommandLineTest.cpp index 760df3c0182..79ab1e2bc70 100644 --- a/llvm/unittests/Support/CommandLineTest.cpp +++ b/llvm/unittests/Support/CommandLineTest.cpp @@ -67,6 +67,22 @@ public: : Base(M0, M1, M2, M3) {} ~StackOption() override { this->removeArgument(); } + + template StackOption &operator=(const DT &V) { + this->setValue(V); + return *this; + } +}; + +class StackSubCommand : public cl::SubCommand { +public: + StackSubCommand(const char *const Name, + const char *const Description = nullptr) + : SubCommand(Name, Description) {} + + StackSubCommand() : SubCommand() {} + + ~StackSubCommand() { unregisterSubCommand(); } }; @@ -78,7 +94,8 @@ TEST(CommandLineTest, ModifyExisitingOption) { const char ArgString[] = "new-test-option"; const char ValueString[] = "Integer"; - StringMap &Map = cl::getRegisteredOptions(); + StringMap &Map = + cl::getRegisteredOptions(*cl::TopLevelSubCommand); ASSERT_TRUE(Map.count("test-option") == 1) << "Could not find option in map."; @@ -237,7 +254,8 @@ TEST(CommandLineTest, HideUnrelatedOptions) { ASSERT_EQ(cl::NotHidden, TestOption2.getOptionHiddenFlag()) << "Hid extra option that should be visable."; - StringMap &Map = cl::getRegisteredOptions(); + StringMap &Map = + cl::getRegisteredOptions(*cl::TopLevelSubCommand); ASSERT_EQ(cl::NotHidden, Map["help"]->getOptionHiddenFlag()) << "Hid default option that should be visable."; } @@ -261,9 +279,189 @@ TEST(CommandLineTest, HideUnrelatedOptionsMulti) { ASSERT_EQ(cl::NotHidden, TestOption3.getOptionHiddenFlag()) << "Hid extra option that should be visable."; - StringMap &Map = cl::getRegisteredOptions(); + StringMap &Map = + cl::getRegisteredOptions(*cl::TopLevelSubCommand); ASSERT_EQ(cl::NotHidden, Map["help"]->getOptionHiddenFlag()) << "Hid default option that should be visable."; } +TEST(CommandLineTest, SetValueInSubcategories) { + cl::ResetCommandLineOptions(); + + StackSubCommand SC1("sc1", "First subcommand"); + StackSubCommand SC2("sc2", "Second subcommand"); + + StackOption TopLevelOpt("top-level", cl::init(false)); + StackOption SC1Opt("sc1", cl::sub(SC1), cl::init(false)); + StackOption SC2Opt("sc2", cl::sub(SC2), cl::init(false)); + + EXPECT_FALSE(TopLevelOpt); + EXPECT_FALSE(SC1Opt); + EXPECT_FALSE(SC2Opt); + const char *args[] = {"prog", "-top-level"}; + EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, nullptr, true)); + EXPECT_TRUE(TopLevelOpt); + EXPECT_FALSE(SC1Opt); + EXPECT_FALSE(SC2Opt); + + TopLevelOpt = false; + + EXPECT_FALSE(TopLevelOpt); + EXPECT_FALSE(SC1Opt); + EXPECT_FALSE(SC2Opt); + const char *args2[] = {"prog", "sc1", "-sc1"}; + EXPECT_TRUE(cl::ParseCommandLineOptions(3, args2, nullptr, true)); + EXPECT_FALSE(TopLevelOpt); + EXPECT_TRUE(SC1Opt); + EXPECT_FALSE(SC2Opt); + + SC1Opt = false; + + EXPECT_FALSE(TopLevelOpt); + EXPECT_FALSE(SC1Opt); + EXPECT_FALSE(SC2Opt); + const char *args3[] = {"prog", "sc2", "-sc2"}; + EXPECT_TRUE(cl::ParseCommandLineOptions(3, args3, nullptr, true)); + EXPECT_FALSE(TopLevelOpt); + EXPECT_FALSE(SC1Opt); + EXPECT_TRUE(SC2Opt); +} + +TEST(CommandLineTest, LookupFailsInWrongSubCommand) { + cl::ResetCommandLineOptions(); + + StackSubCommand SC1("sc1", "First subcommand"); + StackSubCommand SC2("sc2", "Second subcommand"); + + StackOption SC1Opt("sc1", cl::sub(SC1), cl::init(false)); + StackOption SC2Opt("sc2", cl::sub(SC2), cl::init(false)); + + const char *args[] = {"prog", "sc1", "-sc2"}; + EXPECT_FALSE(cl::ParseCommandLineOptions(3, args, nullptr, true)); +} + +TEST(CommandLineTest, AddToAllSubCommands) { + cl::ResetCommandLineOptions(); + + StackSubCommand SC1("sc1", "First subcommand"); + StackOption AllOpt("everywhere", cl::sub(*cl::AllSubCommands), + cl::init(false)); + StackSubCommand SC2("sc2", "Second subcommand"); + + const char *args[] = {"prog", "-everywhere"}; + const char *args2[] = {"prog", "sc1", "-everywhere"}; + const char *args3[] = {"prog", "sc2", "-everywhere"}; + + EXPECT_FALSE(AllOpt); + EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, nullptr, true)); + EXPECT_TRUE(AllOpt); + + AllOpt = false; + + EXPECT_FALSE(AllOpt); + EXPECT_TRUE(cl::ParseCommandLineOptions(3, args2, nullptr, true)); + EXPECT_TRUE(AllOpt); + + AllOpt = false; + + EXPECT_FALSE(AllOpt); + EXPECT_TRUE(cl::ParseCommandLineOptions(3, args3, nullptr, true)); + EXPECT_TRUE(AllOpt); +} + +TEST(CommandLineTest, ReparseCommandLineOptions) { + cl::ResetCommandLineOptions(); + + StackOption TopLevelOpt("top-level", cl::sub(*cl::TopLevelSubCommand), + cl::init(false)); + + const char *args[] = {"prog", "-top-level"}; + + EXPECT_FALSE(TopLevelOpt); + EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, nullptr, true)); + EXPECT_TRUE(TopLevelOpt); + + TopLevelOpt = false; + + EXPECT_FALSE(TopLevelOpt); + EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, nullptr, true)); + EXPECT_TRUE(TopLevelOpt); +} + +TEST(CommandLineTest, RemoveFromRegularSubCommand) { + cl::ResetCommandLineOptions(); + + StackSubCommand SC("sc", "Subcommand"); + StackOption RemoveOption("remove-option", cl::sub(SC), cl::init(false)); + StackOption KeepOption("keep-option", cl::sub(SC), cl::init(false)); + + const char *args[] = {"prog", "sc", "-remove-option"}; + + EXPECT_FALSE(RemoveOption); + EXPECT_TRUE(cl::ParseCommandLineOptions(3, args, nullptr, true)); + EXPECT_TRUE(RemoveOption); + + RemoveOption.removeArgument(); + + EXPECT_FALSE(cl::ParseCommandLineOptions(3, args, nullptr, true)); +} + +TEST(CommandLineTest, RemoveFromTopLevelSubCommand) { + cl::ResetCommandLineOptions(); + + StackOption TopLevelRemove( + "top-level-remove", cl::sub(*cl::TopLevelSubCommand), cl::init(false)); + StackOption TopLevelKeep( + "top-level-keep", cl::sub(*cl::TopLevelSubCommand), cl::init(false)); + + const char *args[] = {"prog", "-top-level-remove"}; + + EXPECT_FALSE(TopLevelRemove); + EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, nullptr, true)); + EXPECT_TRUE(TopLevelRemove); + + TopLevelRemove.removeArgument(); + + EXPECT_FALSE(cl::ParseCommandLineOptions(2, args, nullptr, true)); +} + +TEST(CommandLineTest, RemoveFromAllSubCommands) { + cl::ResetCommandLineOptions(); + + StackSubCommand SC1("sc1", "First Subcommand"); + StackSubCommand SC2("sc2", "Second Subcommand"); + StackOption RemoveOption("remove-option", cl::sub(*cl::AllSubCommands), + cl::init(false)); + StackOption KeepOption("keep-option", cl::sub(*cl::AllSubCommands), + cl::init(false)); + + const char *args0[] = {"prog", "-remove-option"}; + const char *args1[] = {"prog", "sc1", "-remove-option"}; + const char *args2[] = {"prog", "sc2", "-remove-option"}; + + // It should work for all subcommands including the top-level. + EXPECT_FALSE(RemoveOption); + EXPECT_TRUE(cl::ParseCommandLineOptions(2, args0, nullptr, true)); + EXPECT_TRUE(RemoveOption); + + RemoveOption = false; + + EXPECT_FALSE(RemoveOption); + EXPECT_TRUE(cl::ParseCommandLineOptions(3, args1, nullptr, true)); + EXPECT_TRUE(RemoveOption); + + RemoveOption = false; + + EXPECT_FALSE(RemoveOption); + EXPECT_TRUE(cl::ParseCommandLineOptions(3, args2, nullptr, true)); + EXPECT_TRUE(RemoveOption); + + RemoveOption.removeArgument(); + + // It should not work for any subcommands including the top-level. + EXPECT_FALSE(cl::ParseCommandLineOptions(2, args0, nullptr, true)); + EXPECT_FALSE(cl::ParseCommandLineOptions(3, args1, nullptr, true)); + EXPECT_FALSE(cl::ParseCommandLineOptions(3, args2, nullptr, true)); +} + } // anonymous namespace diff --git a/llvm/unittests/Support/ProgramTest.cpp b/llvm/unittests/Support/ProgramTest.cpp index deadaadec1d..886ead8305b 100644 --- a/llvm/unittests/Support/ProgramTest.cpp +++ b/llvm/unittests/Support/ProgramTest.cpp @@ -231,7 +231,7 @@ TEST_F(ProgramEnvTest, TestExecuteNoWait) { // LoopCount should only be incremented once. while (true) { ++LoopCount; - ProcessInfo WaitResult = Wait(PI1, 0, true, &Error); + ProcessInfo WaitResult = llvm::sys::Wait(PI1, 0, true, &Error); ASSERT_TRUE(Error.empty()); if (WaitResult.Pid == PI1.Pid) break; @@ -248,7 +248,7 @@ TEST_F(ProgramEnvTest, TestExecuteNoWait) { // cse, LoopCount should be greater than 1 (more than one increment occurs). while (true) { ++LoopCount; - ProcessInfo WaitResult = Wait(PI2, 0, false, &Error); + ProcessInfo WaitResult = llvm::sys::Wait(PI2, 0, false, &Error); ASSERT_TRUE(Error.empty()); if (WaitResult.Pid == PI2.Pid) break; -- cgit v1.2.3