Skip to content

Commit

Permalink
[CommandLine] Don't allow duplicate categories.
Browse files Browse the repository at this point in the history
Summary:
This is a fix to D61574, r360179, that allowed duplicate
OptionCategory's.  This change adds a check to make sure a category can
only be added once even if the user passes it twice.

Reviewed By: MaskRay

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D61972

llvm-svn: 360913
  • Loading branch information
donhinton committed May 16, 2019
1 parent 600ec01 commit 8249a88
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion llvm/lib/Support/CommandLine.cpp
Expand Up @@ -450,7 +450,7 @@ void Option::addCategory(OptionCategory &C) {
// must be explicitly added if you want multiple categories that include it.
if (&C != &GeneralCategory && Categories[0] == &GeneralCategory)
Categories[0] = &C;
else
else if (find(Categories, &C) == Categories.end())
Categories.push_back(&C);
}

Expand Down
4 changes: 4 additions & 0 deletions llvm/unittests/Support/CommandLineTest.cpp
Expand Up @@ -170,8 +170,12 @@ TEST(CommandLineTest, UseOptionCategory) {

TEST(CommandLineTest, UseMultipleCategories) {
StackOption<int> TestOption2("test-option2", cl::cat(TestCategory),
cl::cat(cl::GeneralCategory),
cl::cat(cl::GeneralCategory));

// Make sure cl::GeneralCategory wasn't added twice.
ASSERT_EQ(TestOption2.Categories.size(), 2U);

ASSERT_NE(TestOption2.Categories.end(),
find_if(TestOption2.Categories,
[&](const llvm::cl::OptionCategory *Cat) {
Expand Down

0 comments on commit 8249a88

Please sign in to comment.