aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/Support/CommandLineTest.cpp
diff options
context:
space:
mode:
authorDon Hinton <hintonda@gmail.com>2019-06-22 17:22:50 +0000
committerDon Hinton <hintonda@gmail.com>2019-06-22 17:22:50 +0000
commita5b83bc9e3b8e8945b55068c762bd6c73621a4b0 (patch)
treeabbffae8fa14b46047ea093bf0b17244181b01a3 /llvm/unittests/Support/CommandLineTest.cpp
parent6f3222ed94f17bb47c8661758f659ffe6555b279 (diff)
downloadllvm-a5b83bc9e3b8e8945b55068c762bd6c73621a4b0.zip
llvm-a5b83bc9e3b8e8945b55068c762bd6c73621a4b0.tar.gz
llvm-a5b83bc9e3b8e8945b55068c762bd6c73621a4b0.tar.bz2
[CommandLine] Remove OptionCategory and SubCommand caches from the Option class.
Summary: This change processes `OptionCategory`s and `SubCommand`s as they are seen instead of caching them in the Option class and processing them later. Doing so simplifies the work needed to be done by the Global parser and significantly reduces the size of the Option class to a mere 64 bytes. Removing the `OptionCategory` cache saved 24 bytes, and removing the `SubCommand` cache saved an additional 48 bytes, for a total of a 72 byte reduction. Reviewers: beanz, zturner, MaskRay, serge-sans-paille Reviewed By: serge-sans-paille Subscribers: serge-sans-paille, tstellar, zturner, hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D62105 llvm-svn: 364134
Diffstat (limited to 'llvm/unittests/Support/CommandLineTest.cpp')
-rw-r--r--llvm/unittests/Support/CommandLineTest.cpp60
1 files changed, 43 insertions, 17 deletions
diff --git a/llvm/unittests/Support/CommandLineTest.cpp b/llvm/unittests/Support/CommandLineTest.cpp
index 1a1c8a4..42c578a 100644
--- a/llvm/unittests/Support/CommandLineTest.cpp
+++ b/llvm/unittests/Support/CommandLineTest.cpp
@@ -95,16 +95,16 @@ TEST(CommandLineTest, ModifyExisitingOption) {
cl::Option *Retrieved = Map["test-option"];
ASSERT_EQ(&TestOption, Retrieved) << "Retrieved wrong option.";
- ASSERT_NE(Retrieved->Categories.end(),
- find_if(Retrieved->Categories,
+ ASSERT_NE(Retrieved->getCategories().end(),
+ find_if(Retrieved->getCategories(),
[&](const llvm::cl::OptionCategory *Cat) {
return Cat == &cl::GeneralCategory;
}))
<< "Incorrect default option category.";
Retrieved->addCategory(TestCategory);
- ASSERT_NE(Retrieved->Categories.end(),
- find_if(Retrieved->Categories,
+ ASSERT_NE(Retrieved->getCategories().end(),
+ find_if(Retrieved->getCategories(),
[&](const llvm::cl::OptionCategory *Cat) {
return Cat == &TestCategory;
}))
@@ -160,8 +160,8 @@ TEST(CommandLineTest, ParseEnvironmentToLocalVar) {
TEST(CommandLineTest, UseOptionCategory) {
StackOption<int> TestOption2("test-option", cl::cat(TestCategory));
- ASSERT_NE(TestOption2.Categories.end(),
- find_if(TestOption2.Categories,
+ ASSERT_NE(TestOption2.getCategories().end(),
+ find_if(TestOption2.getCategories(),
[&](const llvm::cl::OptionCategory *Cat) {
return Cat == &TestCategory;
}))
@@ -172,18 +172,19 @@ TEST(CommandLineTest, UseMultipleCategories) {
StackOption<int> TestOption2("test-option2", cl::cat(TestCategory),
cl::cat(cl::GeneralCategory),
cl::cat(cl::GeneralCategory));
+ auto TestOption2Categories = TestOption2.getCategories();
// Make sure cl::GeneralCategory wasn't added twice.
- ASSERT_EQ(TestOption2.Categories.size(), 2U);
+ ASSERT_EQ(TestOption2Categories.size(), 2U);
- ASSERT_NE(TestOption2.Categories.end(),
- find_if(TestOption2.Categories,
+ ASSERT_NE(TestOption2Categories.end(),
+ find_if(TestOption2Categories,
[&](const llvm::cl::OptionCategory *Cat) {
return Cat == &TestCategory;
}))
<< "Failed to assign Option Category.";
- ASSERT_NE(TestOption2.Categories.end(),
- find_if(TestOption2.Categories,
+ ASSERT_NE(TestOption2Categories.end(),
+ find_if(TestOption2Categories,
[&](const llvm::cl::OptionCategory *Cat) {
return Cat == &cl::GeneralCategory;
}))
@@ -192,20 +193,21 @@ TEST(CommandLineTest, UseMultipleCategories) {
cl::OptionCategory AnotherCategory("Additional test Options", "Description");
StackOption<int> TestOption("test-option", cl::cat(TestCategory),
cl::cat(AnotherCategory));
- ASSERT_EQ(TestOption.Categories.end(),
- find_if(TestOption.Categories,
+ auto TestOptionCategories = TestOption.getCategories();
+ ASSERT_EQ(TestOptionCategories.end(),
+ find_if(TestOptionCategories,
[&](const llvm::cl::OptionCategory *Cat) {
return Cat == &cl::GeneralCategory;
}))
<< "Failed to remove General Category.";
- ASSERT_NE(TestOption.Categories.end(),
- find_if(TestOption.Categories,
+ ASSERT_NE(TestOptionCategories.end(),
+ find_if(TestOptionCategories,
[&](const llvm::cl::OptionCategory *Cat) {
return Cat == &TestCategory;
}))
<< "Failed to assign Option Category.";
- ASSERT_NE(TestOption.Categories.end(),
- find_if(TestOption.Categories,
+ ASSERT_NE(TestOptionCategories.end(),
+ find_if(TestOptionCategories,
[&](const llvm::cl::OptionCategory *Cat) {
return Cat == &AnotherCategory;
}))
@@ -378,6 +380,30 @@ TEST(CommandLineTest, AliasRequired) {
testAliasRequired(array_lengthof(opts2), opts2);
}
+TEST(CommandLineTest, AliasWithSubCommand) {
+ StackSubCommand SC1("sc1", "Subcommand 1");
+ StackOption<std::string> Option1("option", cl::value_desc("output file"),
+ cl::init("-"), cl::desc("Option"),
+ cl::sub(SC1));
+ StackOption<std::string, cl::alias> Alias1("o", llvm::cl::aliasopt(Option1),
+ cl::desc("Alias for --option"),
+ cl::sub(SC1));
+}
+
+TEST(CommandLineTest, AliasWithMultipleSubCommandsWithSameOption) {
+ StackSubCommand SC1("sc1", "Subcommand 1");
+ StackOption<std::string> Option1("option", cl::value_desc("output file"),
+ cl::init("-"), cl::desc("Option"),
+ cl::sub(SC1));
+ StackSubCommand SC2("sc2", "Subcommand 2");
+ StackOption<std::string> Option2("option", cl::value_desc("output file"),
+ cl::init("-"), cl::desc("Option"),
+ cl::sub(SC2));
+
+ StackOption<std::string, cl::alias> Alias1("o", llvm::cl::aliasopt(Option1),
+ cl::desc("Alias for --option"));
+}
+
TEST(CommandLineTest, HideUnrelatedOptions) {
StackOption<int> TestOption1("hide-option-1");
StackOption<int> TestOption2("hide-option-2", cl::cat(TestCategory));