From ece7bca75d9288767abe7d9e78cc6eff70a90f6b Mon Sep 17 00:00:00 2001 From: Don Hinton Date: Thu, 16 May 2019 16:25:13 +0000 Subject: [PATCH] [CommandLine] Don't allow duplicate categories. 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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@360913 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Support/CommandLine.cpp | 2 +- unittests/Support/CommandLineTest.cpp | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/Support/CommandLine.cpp b/lib/Support/CommandLine.cpp index 4e63a07673a..3d82f159701 100644 --- a/lib/Support/CommandLine.cpp +++ b/lib/Support/CommandLine.cpp @@ -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); } diff --git a/unittests/Support/CommandLineTest.cpp b/unittests/Support/CommandLineTest.cpp index 4fb20b18d9b..43c9452bf70 100644 --- a/unittests/Support/CommandLineTest.cpp +++ b/unittests/Support/CommandLineTest.cpp @@ -170,8 +170,12 @@ TEST(CommandLineTest, UseOptionCategory) { TEST(CommandLineTest, UseMultipleCategories) { StackOption 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) { -- 2.40.0