[Lldb-commits] [lldb] r263468 - More of the alias refactoring work! CommandAlias is now a CommandObject

Enrico Granata via lldb-commits lldb-commits at lists.llvm.org
Mon Mar 14 12:00:21 PDT 2016


Author: enrico
Date: Mon Mar 14 14:00:21 2016
New Revision: 263468

URL: http://llvm.org/viewvc/llvm-project?rev=263468&view=rev
Log:
More of the alias refactoring work! CommandAlias is now a CommandObject


Modified:
    lldb/trunk/include/lldb/Interpreter/CommandAlias.h
    lldb/trunk/include/lldb/Interpreter/CommandInterpreter.h
    lldb/trunk/include/lldb/Interpreter/CommandObject.h
    lldb/trunk/source/Interpreter/CommandAlias.cpp
    lldb/trunk/source/Interpreter/CommandInterpreter.cpp

Modified: lldb/trunk/include/lldb/Interpreter/CommandAlias.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Interpreter/CommandAlias.h?rev=263468&r1=263467&r2=263468&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Interpreter/CommandAlias.h (original)
+++ lldb/trunk/include/lldb/Interpreter/CommandAlias.h Mon Mar 14 14:00:21 2016
@@ -18,16 +18,21 @@
 // Project includes
 #include "lldb/lldb-forward.h"
 #include "lldb/Interpreter/Args.h"
+#include "lldb/Interpreter/CommandObject.h"
 
 namespace lldb_private {
-class CommandAlias
+class CommandAlias : public CommandObject
 {
 public:
     typedef std::unique_ptr<CommandAlias> UniquePointer;
 
-    static UniquePointer
-    GetCommandAlias (lldb::CommandObjectSP cmd_sp,
-                     const char *options_args);
+    CommandAlias (CommandInterpreter &interpreter,
+                  lldb::CommandObjectSP cmd_sp,
+                  const char *options_args,
+                  const char *name,
+                  const char *help = nullptr,
+                  const char *syntax = nullptr,
+                  uint32_t flags = 0);
     
     void
     GetAliasExpansion (StreamString &help_string);
@@ -43,13 +48,18 @@ public:
         return IsValid();
     }
     
+    bool
+    WantsRawCommandString() override;
+    
+    bool
+    IsAlias () override { return true; }
+    
+    bool
+    Execute(const char *args_string, CommandReturnObject &result) override;
+    
     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 ;

Modified: lldb/trunk/include/lldb/Interpreter/CommandInterpreter.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Interpreter/CommandInterpreter.h?rev=263468&r1=263467&r2=263468&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Interpreter/CommandInterpreter.h (original)
+++ lldb/trunk/include/lldb/Interpreter/CommandInterpreter.h Mon Mar 14 14:00:21 2016
@@ -201,7 +201,7 @@ class CommandInterpreter :
     public IOHandlerDelegate
 {
 public:
-    typedef std::map<std::string, CommandAlias::UniquePointer> CommandAliasMap;
+    typedef std::map<std::string, lldb::CommandObjectSP> CommandAliasMap;
     
     enum
     {

Modified: lldb/trunk/include/lldb/Interpreter/CommandObject.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Interpreter/CommandObject.h?rev=263468&r1=263467&r2=263468&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Interpreter/CommandObject.h (original)
+++ lldb/trunk/include/lldb/Interpreter/CommandObject.h Mon Mar 14 14:00:21 2016
@@ -177,6 +177,9 @@ public:
     
     virtual bool
     IsMultiwordObject () { return false; }
+    
+    virtual bool
+    IsAlias () { return false; }
 
     virtual lldb::CommandObjectSP
     GetSubcommandSP(const char *sub_cmd, StringList *matches = nullptr)

Modified: lldb/trunk/source/Interpreter/CommandAlias.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/CommandAlias.cpp?rev=263468&r1=263467&r2=263468&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/CommandAlias.cpp (original)
+++ lldb/trunk/source/Interpreter/CommandAlias.cpp Mon Mar 14 14:00:21 2016
@@ -9,6 +9,8 @@
 
 #include "lldb/Interpreter/CommandAlias.h"
 
+#include "llvm/Support/ErrorHandling.h"
+
 #include "lldb/Core/StreamString.h"
 #include "lldb/Interpreter/CommandObject.h"
 #include "lldb/Interpreter/CommandReturnObject.h"
@@ -70,22 +72,48 @@ ProcessAliasOptionsArgs (lldb::CommandOb
     return success;
 }
 
-CommandAlias::UniquePointer
-CommandAlias::GetCommandAlias (lldb::CommandObjectSP cmd_sp,
-                               const char *options_args)
+CommandAlias::CommandAlias (CommandInterpreter &interpreter,
+                            lldb::CommandObjectSP cmd_sp,
+                            const char *options_args,
+                            const char *name,
+                            const char *help,
+                            const char *syntax,
+                            uint32_t flags) :
+    CommandObject(interpreter,
+                  name,
+                  help,
+                  syntax,
+                  flags),
+m_underlying_command_sp(),
+m_option_args_sp(new OptionArgVector)
+{
+    if (ProcessAliasOptionsArgs(cmd_sp, options_args, m_option_args_sp))
+    {
+        m_underlying_command_sp = cmd_sp;
+        if (!help || !help[0])
+        {
+            StreamString sstr;
+            StreamString translation_and_help;
+            GetAliasExpansion(sstr);
+            
+            translation_and_help.Printf ("(%s)  %s", sstr.GetData(), GetUnderlyingCommand()->GetHelp());
+            SetHelp(translation_and_help.GetData());
+        }
+    }
+}
+
+bool
+CommandAlias::WantsRawCommandString()
 {
-    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;
+    if (IsValid())
+        return m_underlying_command_sp->WantsRawCommandString();
+    return false;
 }
 
-CommandAlias::CommandAlias (lldb::CommandObjectSP cmd_sp,
-                            OptionArgVectorSP args_sp) :
-m_underlying_command_sp(cmd_sp),
-m_option_args_sp(args_sp)
+bool
+CommandAlias::Execute(const char *args_string, CommandReturnObject &result)
 {
+    llvm_unreachable("CommandAlias::Execute is not to be called");
 }
 
 void

Modified: lldb/trunk/source/Interpreter/CommandInterpreter.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/CommandInterpreter.cpp?rev=263468&r1=263467&r2=263468&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/CommandInterpreter.cpp (original)
+++ lldb/trunk/source/Interpreter/CommandInterpreter.cpp Mon Mar 14 14:00:21 2016
@@ -772,7 +772,7 @@ CommandInterpreter::GetCommandSP (const
     {
         CommandAliasMap::iterator alias_pos = m_alias_dict.find(cmd);
         if (alias_pos != m_alias_dict.end())
-            command_sp = alias_pos->second->GetUnderlyingCommand();
+            command_sp = ((CommandAlias*)alias_pos->second.get())->GetUnderlyingCommand();
     }
 
     if (HasUserCommands())
@@ -823,7 +823,7 @@ CommandInterpreter::GetCommandSP (const
             cmd.assign(matches->GetStringAtIndex (num_cmd_matches));
             CommandAliasMap::iterator alias_pos = m_alias_dict.find(cmd);
             if (alias_pos != m_alias_dict.end())
-                alias_match_sp = alias_pos->second->GetUnderlyingCommand();
+                alias_match_sp = ((CommandAlias*)alias_pos->second.get())->GetUnderlyingCommand();
         }
 
         if (HasUserCommands())
@@ -1057,11 +1057,17 @@ CommandInterpreter::AddAlias (const char
     if (command_obj_sp.get())
         assert((this == &command_obj_sp->GetCommandInterpreter()) && "tried to add a CommandObject from a different interpreter");
     
-    if (auto cmd_alias = CommandAlias::GetCommandAlias(command_obj_sp, args_string))
+    std::unique_ptr<CommandAlias> command_alias_up(new CommandAlias(*this,
+                                                                    command_obj_sp,
+                                                                    args_string,
+                                                                    alias_name));
+    
+    if (command_alias_up && command_alias_up->IsValid())
     {
-        m_alias_dict[alias_name] = std::move(cmd_alias);
+        m_alias_dict[alias_name] = CommandObjectSP(command_alias_up.release());
         return true;
     }
+    
     return false;
 }
 
@@ -1144,15 +1150,8 @@ CommandInterpreter::GetHelp (CommandRetu
 
         for (auto alias_pos = m_alias_dict.begin(); alias_pos != m_alias_dict.end(); ++alias_pos)
         {
-            StreamString sstr;
-            StreamString translation_and_help;
-            std::string entry_name = alias_pos->first;
-            std::string second_entry = alias_pos->second->GetUnderlyingCommand()->GetCommandName();
-            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(), "--",
-                                     translation_and_help.GetData(), max_len);
+            OutputFormattedHelpText (result.GetOutputStream(), alias_pos->first.c_str(), "--", alias_pos->second->GetHelp(),
+                                     max_len);
         }
         result.AppendMessage("");
     }
@@ -1996,7 +1995,7 @@ CommandInterpreter::GetAlias (const char
 
     auto pos = m_alias_dict.find(alias);
     if (pos != m_alias_dict.end())
-        return pos->second.get();
+        return (CommandAlias*)pos->second.get();
     
     return nullptr;
 }




More information about the lldb-commits mailing list