diff --git a/llvm/lib/Support/CommandLine.cpp b/llvm/lib/Support/CommandLine.cpp --- a/llvm/lib/Support/CommandLine.cpp +++ b/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); } diff --git a/llvm/unittests/Support/CommandLineTest.cpp b/llvm/unittests/Support/CommandLineTest.cpp --- a/llvm/unittests/Support/CommandLineTest.cpp +++ b/llvm/unittests/Support/CommandLineTest.cpp @@ -170,8 +170,12 @@ 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) {