[llvm] r360913 - [CommandLine] Don't allow duplicate categories.

Don Hinton via llvm-commits llvm-commits at lists.llvm.org
Thu May 16 09:25:14 PDT 2019


Author: dhinton
Date: Thu May 16 09:25:13 2019
New Revision: 360913

URL: http://llvm.org/viewvc/llvm-project?rev=360913&view=rev
Log:
[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

Modified:
    llvm/trunk/lib/Support/CommandLine.cpp
    llvm/trunk/unittests/Support/CommandLineTest.cpp

Modified: llvm/trunk/lib/Support/CommandLine.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/CommandLine.cpp?rev=360913&r1=360912&r2=360913&view=diff
==============================================================================
--- llvm/trunk/lib/Support/CommandLine.cpp (original)
+++ llvm/trunk/lib/Support/CommandLine.cpp Thu May 16 09:25:13 2019
@@ -450,7 +450,7 @@ void Option::addCategory(OptionCategory
   // 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);
 }
 

Modified: llvm/trunk/unittests/Support/CommandLineTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/CommandLineTest.cpp?rev=360913&r1=360912&r2=360913&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/CommandLineTest.cpp (original)
+++ llvm/trunk/unittests/Support/CommandLineTest.cpp Thu May 16 09:25:13 2019
@@ -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) {




More information about the llvm-commits mailing list