[llvm] d85a13b - Revert "[CommandLine][NFCI] Do not add 'All' to 'RegisteredSubCommands' (#77041)"

Igor Kudrin via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 10 18:01:14 PST 2024


Author: Igor Kudrin
Date: 2024-01-10T17:58:59-08:00
New Revision: d85a13b867b17fa93965bc7e439a58c954045217

URL: https://github.com/llvm/llvm-project/commit/d85a13b867b17fa93965bc7e439a58c954045217
DIFF: https://github.com/llvm/llvm-project/commit/d85a13b867b17fa93965bc7e439a58c954045217.diff

LOG: Revert "[CommandLine][NFCI] Do not add 'All' to 'RegisteredSubCommands' (#77041)"

This reverts commit fb7fe49960ae053c92985f3376d85a15bbd10d1a.

The commit introduced a bug where an option with the `All' subcommand
would not be added to a category initialized after that option.

Added: 
    

Modified: 
    llvm/lib/Support/CommandLine.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Support/CommandLine.cpp b/llvm/lib/Support/CommandLine.cpp
index 9a57936be2db70..7360d733d96e7b 100644
--- a/llvm/lib/Support/CommandLine.cpp
+++ b/llvm/lib/Support/CommandLine.cpp
@@ -164,7 +164,10 @@ class CommandLineParser {
   // This collects the 
diff erent subcommands that have been registered.
   SmallPtrSet<SubCommand *, 4> RegisteredSubCommands;
 
-  CommandLineParser() { registerSubCommand(&SubCommand::getTopLevel()); }
+  CommandLineParser() {
+    registerSubCommand(&SubCommand::getTopLevel());
+    registerSubCommand(&SubCommand::getAll());
+  }
 
   void ResetAllOptionOccurrences();
 
@@ -345,15 +348,15 @@ class CommandLineParser {
 
     // For all options that have been registered for all subcommands, add the
     // option to this subcommand now.
-    assert(sub != &SubCommand::getAll() &&
-           "SubCommand::getAll() should not be registered");
-    for (auto &E : SubCommand::getAll().OptionsMap) {
-      Option *O = E.second;
-      if ((O->isPositional() || O->isSink() || O->isConsumeAfter()) ||
-          O->hasArgStr())
-        addOption(O, sub);
-      else
-        addLiteralOption(*O, sub, E.first());
+    if (sub != &SubCommand::getAll()) {
+      for (auto &E : SubCommand::getAll().OptionsMap) {
+        Option *O = E.second;
+        if ((O->isPositional() || O->isSink() || O->isConsumeAfter()) ||
+            O->hasArgStr())
+          addOption(O, sub);
+        else
+          addLiteralOption(*O, sub, E.first());
+      }
     }
   }
 
@@ -381,6 +384,7 @@ class CommandLineParser {
     SubCommand::getTopLevel().reset();
     SubCommand::getAll().reset();
     registerSubCommand(&SubCommand::getTopLevel());
+    registerSubCommand(&SubCommand::getAll());
 
     DefaultOptions.clear();
   }
@@ -528,8 +532,8 @@ SubCommand *CommandLineParser::LookupSubCommand(StringRef Name,
   // Find a subcommand with the edit distance == 1.
   SubCommand *NearestMatch = nullptr;
   for (auto *S : RegisteredSubCommands) {
-    assert(S != &SubCommand::getAll() &&
-           "SubCommand::getAll() is not expected in RegisteredSubCommands");
+    if (S == &SubCommand::getAll())
+      continue;
     if (S->getName().empty())
       continue;
 


        


More information about the llvm-commits mailing list