aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/Support/CommandLineTest.cpp
diff options
context:
space:
mode:
authorDon Hinton <hintonda@gmail.com>2019-07-10 17:57:05 +0000
committerDon Hinton <hintonda@gmail.com>2019-07-10 17:57:05 +0000
commit43d75f977853c3ec891a440c362b2df183a211b5 (patch)
tree8efff444c18d00719e671d5a6bb99719787fce42 /llvm/unittests/Support/CommandLineTest.cpp
parent5dd2af5248789933a865c4e2b3cb058c519912d9 (diff)
downloadllvm-43d75f977853c3ec891a440c362b2df183a211b5.zip
llvm-43d75f977853c3ec891a440c362b2df183a211b5.tar.gz
llvm-43d75f977853c3ec891a440c362b2df183a211b5.tar.bz2
Recommit "[CommandLine] Remove OptionCategory and SubCommand caches from the Option class."
Previously reverted in 364141 due to buildbot breakage, and fixed here by making GeneralCategory global a ManagedStatic. 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. Reviewed By: serge-sans-paille Tags: #llvm, #clang Differential Revision: https://reviews.llvm.org/D62105 llvm-svn: 365675
Diffstat (limited to 'llvm/unittests/Support/CommandLineTest.cpp')
-rw-r--r--llvm/unittests/Support/CommandLineTest.cpp70
1 files changed, 48 insertions, 22 deletions
diff --git a/llvm/unittests/Support/CommandLineTest.cpp b/llvm/unittests/Support/CommandLineTest.cpp
index 1a1c8a4..f180e05 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;
+ 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;
}))
@@ -170,42 +170,44 @@ TEST(CommandLineTest, UseOptionCategory) {
TEST(CommandLineTest, UseMultipleCategories) {
StackOption<int> TestOption2("test-option2", cl::cat(TestCategory),
- cl::cat(cl::GeneralCategory),
- cl::cat(cl::GeneralCategory));
+ 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;
+ return Cat == &*cl::GeneralCategory;
}))
<< "Failed to assign General Category.";
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;
+ 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));