[PATCH] D62105: [CommandLine] Remove OptionCategory and SubCommand caches from the Option class.

2019-07-10 Thread Phabricator via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL365675: Recommit [CommandLine] Remove OptionCategory 
and SubCommand caches from theā€¦ (authored by dhinton, committed by ).
Herald added a subscriber: ilya-biryukov.

Changed prior to commit:
  https://reviews.llvm.org/D62105?vs=208736=209021#toc

Repository:
  rL LLVM

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D62105/new/

https://reviews.llvm.org/D62105

Files:
  cfe/trunk/tools/clang-refactor/ClangRefactor.cpp
  clang-tools-extra/trunk/clangd/index/dex/dexp/Dexp.cpp
  clang-tools-extra/trunk/clangd/indexer/IndexerMain.cpp
  llvm/trunk/include/llvm/Support/CommandLine.h
  llvm/trunk/lib/Support/CommandLine.cpp
  llvm/trunk/unittests/Support/CommandLineTest.cpp

Index: llvm/trunk/lib/Support/CommandLine.cpp
===
--- llvm/trunk/lib/Support/CommandLine.cpp
+++ llvm/trunk/lib/Support/CommandLine.cpp
@@ -142,7 +142,7 @@
   // This collects Options added with the cl::DefaultOption flag. Since they can
   // be overridden, they are not added to the appropriate SubCommands until
   // ParseCommandLineOptions actually runs.
-  SmallVector DefaultOptions;
+  SmallVector, 4> DefaultOptions;
 
   // This collects the different option categories that have been registered.
   SmallPtrSet RegisteredOptionCategories;
@@ -151,6 +151,7 @@
   SmallPtrSet RegisteredSubCommands;
 
   CommandLineParser() : ActiveSubCommand(nullptr) {
+RegisteredOptionCategories.insert(&*GeneralCategory);
 registerSubCommand(&*TopLevelSubCommand);
 registerSubCommand(&*AllSubCommands);
   }
@@ -182,15 +183,16 @@
   }
 
   void addLiteralOption(Option , StringRef Name) {
-if (Opt.Subs.empty())
-  addLiteralOption(Opt, &*TopLevelSubCommand, Name);
-else {
-  for (auto SC : Opt.Subs)
-addLiteralOption(Opt, SC, Name);
-}
+for(SubCommand *SC: Opt.getSubCommands())
+  addLiteralOption(Opt, SC, Name);
   }
 
-  void addOption(Option *O, SubCommand *SC) {
+  void addOption(Option *O, SubCommand *SC, bool ProcessDefaultOptions = false) {
+if (!ProcessDefaultOptions && O->isDefaultOption()) {
+  DefaultOptions.push_back(std::make_pair(O, SC));
+  return;
+}
+
 bool HadErrors = false;
 if (O->hasArgStr()) {
   // If it's a DefaultOption, check to make sure it isn't already there.
@@ -232,22 +234,14 @@
   for (const auto  : RegisteredSubCommands) {
 if (SC == Sub)
   continue;
-addOption(O, Sub);
+addOption(O, Sub, ProcessDefaultOptions);
   }
 }
   }
 
-  void addOption(Option *O, bool ProcessDefaultOption = false) {
-if (!ProcessDefaultOption && O->isDefaultOption()) {
-  DefaultOptions.push_back(O);
-  return;
-}
-
-if (O->Subs.empty()) {
-  addOption(O, &*TopLevelSubCommand);
-} else {
-  for (auto SC : O->Subs)
-addOption(O, SC);
+  void addDefaultOptions() {
+for (std::pair  : DefaultOptions) {
+  addOption(DO.first, DO.second, true);
 }
   }
 
@@ -285,17 +279,8 @@
   }
 
   void removeOption(Option *O) {
-if (O->Subs.empty())
-  removeOption(O, &*TopLevelSubCommand);
-else {
-  if (O->isInAllSubCommands()) {
-for (auto SC : RegisteredSubCommands)
-  removeOption(O, SC);
-  } else {
-for (auto SC : O->Subs)
-  removeOption(O, SC);
-  }
-}
+for (auto SC : RegisteredSubCommands)
+  removeOption(O, SC);
   }
 
   bool hasOptions(const SubCommand ) const {
@@ -324,17 +309,8 @@
   }
 
   void updateArgStr(Option *O, StringRef NewName) {
-if (O->Subs.empty())
-  updateArgStr(O, NewName, &*TopLevelSubCommand);
-else {
-  if (O->isInAllSubCommands()) {
-for (auto SC : RegisteredSubCommands)
-  updateArgStr(O, NewName, SC);
-  } else {
-for (auto SC : O->Subs)
-  updateArgStr(O, NewName, SC);
-  }
-}
+for (auto SC : RegisteredSubCommands)
+  updateArgStr(O, NewName, SC);
   }
 
   void printOptionValues();
@@ -389,6 +365,7 @@
 
 MoreHelp.clear();
 RegisteredOptionCategories.clear();
+RegisteredOptionCategories.insert(&*GeneralCategory);
 
 ResetAllOptionOccurrences();
 RegisteredSubCommands.clear();
@@ -427,13 +404,38 @@
   GlobalParser->MoreHelp.push_back(Help);
 }
 
-void Option::addArgument() {
-  GlobalParser->addOption(this);
+void Option::addArgument(SubCommand ) {
+  GlobalParser->addOption(this, );
   FullyInitialized = true;
 }
 
 void Option::removeArgument() { GlobalParser->removeOption(this); }
 
+SmallPtrSet Option::getCategories() const {
+  SmallPtrSet Cats;
+  for (OptionCategory *C: GlobalParser->RegisteredOptionCategories) {
+if (C->MemberOptions.find(this) != C->MemberOptions.end())
+  Cats.insert(C);
+  }
+  if (Cats.empty())
+Cats.insert(&*GeneralCategory);
+  return Cats;
+}
+
+SmallPtrSet Option::getSubCommands() const {
+  // This can 

[PATCH] D62105: [CommandLine] Remove OptionCategory and SubCommand caches from the Option class.

2019-07-09 Thread Don Hinton via Phabricator via cfe-commits
hintonda updated this revision to Diff 208736.
hintonda added a comment.
Herald added subscribers: cfe-commits, kadircet, arphaman, jkorous.
Herald added a project: clang.

Make GeneralCategory a ManagedStatic.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D62105/new/

https://reviews.llvm.org/D62105

Files:
  clang-tools-extra/clangd/index/dex/dexp/Dexp.cpp
  clang-tools-extra/clangd/indexer/IndexerMain.cpp
  clang/tools/clang-refactor/ClangRefactor.cpp
  llvm/include/llvm/Support/CommandLine.h
  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
@@ -95,16 +95,16 @@
   cl::Option *Retrieved = Map["test-option"];
   ASSERT_EQ(, Retrieved) << "Retrieved wrong option.";
 
-  ASSERT_NE(Retrieved->Categories.end(),
-find_if(Retrieved->Categories,
+  ASSERT_NE(Retrieved->getCategories().end(),
+find_if(Retrieved->getCategories(),
 [&](const llvm::cl::OptionCategory *Cat) {
-  return Cat == ::GeneralCategory;
+  return Cat == &*cl::GeneralCategory;
 }))
   << "Incorrect default option category.";
 
   Retrieved->addCategory(TestCategory);
-  ASSERT_NE(Retrieved->Categories.end(),
-find_if(Retrieved->Categories,
+  ASSERT_NE(Retrieved->getCategories().end(),
+find_if(Retrieved->getCategories(),
 [&](const llvm::cl::OptionCategory *Cat) {
   return Cat == 
 }))
@@ -160,8 +160,8 @@
 TEST(CommandLineTest, UseOptionCategory) {
   StackOption TestOption2("test-option", cl::cat(TestCategory));
 
-  ASSERT_NE(TestOption2.Categories.end(),
-find_if(TestOption2.Categories,
+  ASSERT_NE(TestOption2.getCategories().end(),
+find_if(TestOption2.getCategories(),
  [&](const llvm::cl::OptionCategory *Cat) {
return Cat == 
  }))
@@ -170,42 +170,44 @@
 
 TEST(CommandLineTest, UseMultipleCategories) {
   StackOption TestOption2("test-option2", cl::cat(TestCategory),
-   cl::cat(cl::GeneralCategory),
-   cl::cat(cl::GeneralCategory));
+   cl::cat(*cl::GeneralCategory),
+   cl::cat(*cl::GeneralCategory));
+  auto TestOption2Categories = TestOption2.getCategories();
 
   // Make sure cl::GeneralCategory wasn't added twice.
-  ASSERT_EQ(TestOption2.Categories.size(), 2U);
+  ASSERT_EQ(TestOption2Categories.size(), 2U);
 
-  ASSERT_NE(TestOption2.Categories.end(),
-find_if(TestOption2.Categories,
+  ASSERT_NE(TestOption2Categories.end(),
+find_if(TestOption2Categories,
  [&](const llvm::cl::OptionCategory *Cat) {
return Cat == 
  }))
   << "Failed to assign Option Category.";
-  ASSERT_NE(TestOption2.Categories.end(),
-find_if(TestOption2.Categories,
+  ASSERT_NE(TestOption2Categories.end(),
+find_if(TestOption2Categories,
  [&](const llvm::cl::OptionCategory *Cat) {
-   return Cat == ::GeneralCategory;
+   return Cat == &*cl::GeneralCategory;
  }))
   << "Failed to assign General Category.";
 
   cl::OptionCategory AnotherCategory("Additional test Options", "Description");
   StackOption TestOption("test-option", cl::cat(TestCategory),
   cl::cat(AnotherCategory));
-  ASSERT_EQ(TestOption.Categories.end(),
-find_if(TestOption.Categories,
+  auto TestOptionCategories = TestOption.getCategories();
+  ASSERT_EQ(TestOptionCategories.end(),
+find_if(TestOptionCategories,
  [&](const llvm::cl::OptionCategory *Cat) {
-   return Cat == ::GeneralCategory;
+   return Cat == &*cl::GeneralCategory;
  }))
   << "Failed to remove General Category.";
-  ASSERT_NE(TestOption.Categories.end(),
-find_if(TestOption.Categories,
+  ASSERT_NE(TestOptionCategories.end(),
+find_if(TestOptionCategories,
  [&](const llvm::cl::OptionCategory *Cat) {
return Cat == 
  }))
   << "Failed to assign Option Category.";
-  ASSERT_NE(TestOption.Categories.end(),
-find_if(TestOption.Categories,
+  ASSERT_NE(TestOptionCategories.end(),
+find_if(TestOptionCategories,
  [&](const llvm::cl::OptionCategory *Cat) {
return Cat ==