Last round of preliminary cleanup in my refactoring of aliases.
authorEnrico Granata <egranata@apple.com>
Wed, 9 Mar 2016 02:27:57 +0000 (02:27 +0000)
committerEnrico Granata <egranata@apple.com>
Wed, 9 Mar 2016 02:27:57 +0000 (02:27 +0000)
The next step is to actually turn CommandAlias into a full-blown CommandObject citizen.

This is tricky given the current architecture of the CommandInterpreter but I think I have found a reasonable path forward.
The current plan is to make class CommandAlias : public CommandObject, and have all the several GetCommand calls not actually traverse through the alias to the underlying command object
The only times that an alias will be traversed are:
a) execution; when time comes to run an alias, I will just grab the underlying command and options, and make the interpreter execute that according to its current algorithm
b) subcommand traversal; if one has an alias to a multiword command, grabbing a subcommand will see through to the subcommand

Other operations, e.g. command listing, command names, command helps, ..., will all use the alias directly. This will, in turn, lead to the removal of the separate alias dictionary, and just mix user commands and aliases in one map

llvm-svn: 262986

lldb/include/lldb/Interpreter/CommandAlias.h
lldb/source/Commands/CommandObjectHelp.cpp
lldb/source/Interpreter/CommandAlias.cpp
lldb/source/Interpreter/CommandInterpreter.cpp

index fcc3786..cfe693d 100644 (file)
@@ -25,16 +25,12 @@ class CommandAlias
 public:
     typedef std::unique_ptr<CommandAlias> UniquePointer;
 
-    static bool
-    ProcessAliasOptionsArgs (lldb::CommandObjectSP &cmd_obj_sp,
-                             const char *options_args,
-                             OptionArgVectorSP &option_arg_vector_sp);
-
-    CommandAlias (lldb::CommandObjectSP cmd_sp = nullptr,
-                  OptionArgVectorSP args_sp = nullptr);
+    static UniquePointer
+    GetCommandAlias (lldb::CommandObjectSP cmd_sp,
+                     const char *options_args);
     
     void
-    GetAliasHelp (StreamString &help_string);
+    GetAliasExpansion (StreamString &help_string);
     
     bool
     IsValid ()
@@ -50,6 +46,10 @@ public:
     lldb::CommandObjectSP GetUnderlyingCommand() { return m_underlying_command_sp; }
     OptionArgVectorSP GetOptionArguments() { return m_option_args_sp; }
     
+protected:
+    CommandAlias (lldb::CommandObjectSP cmd_sp = nullptr,
+                  OptionArgVectorSP args_sp = nullptr);
+    
 private:
     lldb::CommandObjectSP m_underlying_command_sp;
     OptionArgVectorSP m_option_args_sp ;
index 9f8af30..d84de54 100644 (file)
@@ -189,7 +189,7 @@ CommandObjectHelp::DoExecute (Args& command, CommandReturnObject &result)
             if (is_alias_command)
             {
                 StreamString sstr;
-                m_interpreter.GetAlias(alias_name.c_str())->GetAliasHelp(sstr);
+                m_interpreter.GetAlias(alias_name.c_str())->GetAliasExpansion(sstr);
                 result.GetOutputStream().Printf ("\n'%s' is an abbreviation for %s\n", alias_name.c_str(), sstr.GetData());
             }
         }
index d77eeee..10dc84a 100644 (file)
@@ -9,3 +9,115 @@
 
 #include "lldb/Interpreter/CommandAlias.h"
 
+#include "lldb/Core/StreamString.h"
+#include "lldb/Interpreter/CommandObject.h"
+#include "lldb/Interpreter/CommandReturnObject.h"
+#include "lldb/Interpreter/Options.h"
+
+using namespace lldb;
+using namespace lldb_private;
+
+static bool
+ProcessAliasOptionsArgs (lldb::CommandObjectSP &cmd_obj_sp,
+                         const char *options_args,
+                         OptionArgVectorSP &option_arg_vector_sp)
+{
+    bool success = true;
+    OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
+    
+    if (!options_args || (strlen (options_args) < 1))
+        return true;
+    
+    std::string options_string (options_args);
+    Args args (options_args);
+    CommandReturnObject result;
+    // Check to see if the command being aliased can take any command options.
+    Options *options = cmd_obj_sp->GetOptions ();
+    if (options)
+    {
+        // See if any options were specified as part of the alias;  if so, handle them appropriately.
+        options->NotifyOptionParsingStarting ();
+        args.Unshift ("dummy_arg");
+        args.ParseAliasOptions (*options, result, option_arg_vector, options_string);
+        args.Shift ();
+        if (result.Succeeded())
+            options->VerifyPartialOptions (result);
+        if (!result.Succeeded() && result.GetStatus() != lldb::eReturnStatusStarted)
+        {
+            result.AppendError ("Unable to create requested alias.\n");
+            return false;
+        }
+    }
+    
+    if (!options_string.empty())
+    {
+        if (cmd_obj_sp->WantsRawCommandString ())
+            option_arg_vector->push_back (OptionArgPair ("<argument>",
+                                                         OptionArgValue (-1,
+                                                                         options_string)));
+        else
+        {
+            const size_t argc = args.GetArgumentCount();
+            for (size_t i = 0; i < argc; ++i)
+                if (strcmp (args.GetArgumentAtIndex (i), "") != 0)
+                    option_arg_vector->push_back
+                    (OptionArgPair ("<argument>",
+                                    OptionArgValue (-1,
+                                                    std::string (args.GetArgumentAtIndex (i)))));
+        }
+    }
+    
+    return success;
+}
+
+CommandAlias::UniquePointer
+CommandAlias::GetCommandAlias (lldb::CommandObjectSP cmd_sp,
+                               const char *options_args)
+{
+    CommandAlias::UniquePointer ret_val(nullptr);
+    OptionArgVectorSP opt_args_sp(new OptionArgVector);
+    if (ProcessAliasOptionsArgs(cmd_sp, options_args, opt_args_sp))
+        ret_val.reset(new CommandAlias(cmd_sp, opt_args_sp));
+    return ret_val;
+}
+
+CommandAlias::CommandAlias (lldb::CommandObjectSP cmd_sp,
+                            OptionArgVectorSP args_sp) :
+m_underlying_command_sp(cmd_sp),
+m_option_args_sp(args_sp)
+{
+}
+
+void
+CommandAlias::GetAliasExpansion (StreamString &help_string)
+{
+    const char* command_name = m_underlying_command_sp->GetCommandName();
+    help_string.Printf ("'%s", command_name);
+    
+    if (m_option_args_sp)
+    {
+        OptionArgVector *options = m_option_args_sp.get();
+        for (size_t i = 0; i < options->size(); ++i)
+        {
+            OptionArgPair cur_option = (*options)[i];
+            std::string opt = cur_option.first;
+            OptionArgValue value_pair = cur_option.second;
+            std::string value = value_pair.second;
+            if (opt.compare("<argument>") == 0)
+            {
+                help_string.Printf (" %s", value.c_str());
+            }
+            else
+            {
+                help_string.Printf (" %s", opt.c_str());
+                if ((value.compare ("<no-argument>") != 0)
+                    && (value.compare ("<need-argument") != 0))
+                {
+                    help_string.Printf (" %s", value.c_str());
+                }
+            }
+        }
+    }
+    
+    help_string.Printf ("'");
+}
index 6102170..ec43fd6 100644 (file)
@@ -97,100 +97,6 @@ enum
     eSpaceReplPrompts = 3
 };
 
-CommandAlias::CommandAlias (lldb::CommandObjectSP cmd_sp,
-                            OptionArgVectorSP args_sp) :
-    m_underlying_command_sp(cmd_sp),
-    m_option_args_sp(args_sp)
-{
-}
-
-void
-CommandAlias::GetAliasHelp (StreamString &help_string)
-{
-    const char* command_name = m_underlying_command_sp->GetCommandName();
-    help_string.Printf ("'%s", command_name);
-    
-    if (m_option_args_sp)
-    {
-        OptionArgVector *options = m_option_args_sp.get();
-        for (size_t i = 0; i < options->size(); ++i)
-        {
-            OptionArgPair cur_option = (*options)[i];
-            std::string opt = cur_option.first;
-            OptionArgValue value_pair = cur_option.second;
-            std::string value = value_pair.second;
-            if (opt.compare("<argument>") == 0)
-            {
-                help_string.Printf (" %s", value.c_str());
-            }
-            else
-            {
-                help_string.Printf (" %s", opt.c_str());
-                if ((value.compare ("<no-argument>") != 0)
-                    && (value.compare ("<need-argument") != 0))
-                {
-                    help_string.Printf (" %s", value.c_str());
-                }
-            }
-        }
-    }
-    
-    help_string.Printf ("'");
-}
-
-bool
-CommandAlias::ProcessAliasOptionsArgs (lldb::CommandObjectSP &cmd_obj_sp,
-                                       const char *options_args,
-                                       OptionArgVectorSP &option_arg_vector_sp)
-{
-    bool success = true;
-    OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
-    
-    if (!options_args || (strlen (options_args) < 1))
-        return true;
-    
-    std::string options_string (options_args);
-    Args args (options_args);
-    CommandReturnObject result;
-    // Check to see if the command being aliased can take any command options.
-    Options *options = cmd_obj_sp->GetOptions ();
-    if (options)
-    {
-        // See if any options were specified as part of the alias;  if so, handle them appropriately.
-        options->NotifyOptionParsingStarting ();
-        args.Unshift ("dummy_arg");
-        args.ParseAliasOptions (*options, result, option_arg_vector, options_string);
-        args.Shift ();
-        if (result.Succeeded())
-            options->VerifyPartialOptions (result);
-        if (!result.Succeeded() && result.GetStatus() != lldb::eReturnStatusStarted)
-        {
-            result.AppendError ("Unable to create requested alias.\n");
-            return false;
-        }
-    }
-    
-    if (!options_string.empty())
-    {
-        if (cmd_obj_sp->WantsRawCommandString ())
-            option_arg_vector->push_back (OptionArgPair ("<argument>",
-                                                         OptionArgValue (-1,
-                                                                         options_string)));
-        else
-        {
-            const size_t argc = args.GetArgumentCount();
-            for (size_t i = 0; i < argc; ++i)
-                if (strcmp (args.GetArgumentAtIndex (i), "") != 0)
-                    option_arg_vector->push_back
-                    (OptionArgPair ("<argument>",
-                                    OptionArgValue (-1,
-                                                    std::string (args.GetArgumentAtIndex (i)))));
-        }
-    }
-    
-    return success;
-}
-
 ConstString &
 CommandInterpreter::GetStaticBroadcasterClass ()
 {
@@ -1151,10 +1057,9 @@ CommandInterpreter::AddAlias (const char *alias_name,
     if (command_obj_sp.get())
         assert((this == &command_obj_sp->GetCommandInterpreter()) && "tried to add a CommandObject from a different interpreter");
     
-    OptionArgVectorSP args_sp(new OptionArgVector);
-    if (CommandAlias::ProcessAliasOptionsArgs(command_obj_sp, args_string, args_sp))
+    if (auto cmd_alias = CommandAlias::GetCommandAlias(command_obj_sp, args_string))
     {
-        m_alias_dict[alias_name] = CommandAlias::UniquePointer(new CommandAlias(command_obj_sp,args_sp));
+        m_alias_dict[alias_name] = std::move(cmd_alias);
         return true;
     }
     return false;
@@ -1243,7 +1148,7 @@ CommandInterpreter::GetHelp (CommandReturnObject &result,
             StreamString translation_and_help;
             std::string entry_name = alias_pos->first;
             std::string second_entry = alias_pos->second->GetUnderlyingCommand()->GetCommandName();
-            alias_pos->second->GetAliasHelp(sstr);
+            alias_pos->second->GetAliasExpansion(sstr);
             
             translation_and_help.Printf ("(%s)  %s", sstr.GetData(), alias_pos->second->GetUnderlyingCommand()->GetHelp());
             OutputFormattedHelpText (result.GetOutputStream(), alias_pos->first.c_str(), "--",