[PATCH] D61972: [CommandLine] Don't allow duplicate categories.

Don Hinton via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed May 15 18:37:18 PDT 2019


hintonda created this revision.
hintonda added a reviewer: MaskRay.
Herald added a subscriber: hiraditya.
Herald added a project: LLVM.

This is a fix to D61574 <https://reviews.llvm.org/D61574>, that allowed duplicate
OptionCategory's.  The change adds a check to make sure a category can
only be added once even if the user passes it twice.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D61972

Files:
  llvm/lib/Support/CommandLine.cpp
  llvm/unittests/Support/CommandLineTest.cpp


Index: llvm/unittests/Support/CommandLineTest.cpp
===================================================================
--- llvm/unittests/Support/CommandLineTest.cpp
+++ llvm/unittests/Support/CommandLineTest.cpp
@@ -170,8 +170,12 @@
 
 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) {
Index: llvm/lib/Support/CommandLine.cpp
===================================================================
--- llvm/lib/Support/CommandLine.cpp
+++ llvm/lib/Support/CommandLine.cpp
@@ -450,7 +450,9 @@
   // 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_if(Categories, [&](OptionCategory *Cat) {
+             return (&C == Cat);
+           }) == Categories.end())
     Categories.push_back(&C);
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D61972.199711.patch
Type: text/x-patch
Size: 1293 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190516/993a375f/attachment.bin>


More information about the llvm-commits mailing list