Added the ability to see global variables with a variable expression path so
you can do things like:

(lldb) target variable g_global.a
(lldb) target variable *g_global.ptr
(lldb) target variable g_global.ptr[1]



git-svn-id: https://llvm.org/svn/llvm-project/llvdb/trunk@134745 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/source/Commands/CommandObjectTarget.cpp b/source/Commands/CommandObjectTarget.cpp
index 3ee7026..9b2147e 100644
--- a/source/Commands/CommandObjectTarget.cpp
+++ b/source/Commands/CommandObjectTarget.cpp
@@ -433,7 +433,82 @@
     ~CommandObjectTargetVariable ()
     {
     }
+
+    void
+    DumpValueObject (Stream &s, VariableSP &var_sp, ValueObjectSP &valobj_sp, const char *root_name)
+    {
+        if (m_option_variable.format != eFormatDefault)
+            valobj_sp->SetFormat (m_option_variable.format);
+        
+        switch (var_sp->GetScope())
+        {
+            case eValueTypeVariableGlobal:
+                if (m_option_variable.show_scope)
+                    s.PutCString("GLOBAL: ");
+                break;
+                
+            case eValueTypeVariableStatic:
+                if (m_option_variable.show_scope)
+                    s.PutCString("STATIC: ");
+                break;
+                
+            case eValueTypeVariableArgument:
+                if (m_option_variable.show_scope)
+                    s.PutCString("   ARG: ");
+                break;
+                
+            case eValueTypeVariableLocal:
+                if (m_option_variable.show_scope)
+                    s.PutCString(" LOCAL: ");
+                break;
+                
+            default:
+                break;
+        }
+        
+        if (m_option_variable.show_decl && var_sp->GetDeclaration ().GetFile())
+        {
+            var_sp->GetDeclaration ().DumpStopContext (&s, false);
+            s.PutCString (": ");
+        }
+        
+        const Format format = m_option_variable.format;
+        if (format != eFormatDefault)
+            valobj_sp->SetFormat (format);
+        
+        ValueObject::DumpValueObject (s, 
+                                      valobj_sp.get(), 
+                                      root_name,
+                                      m_varobj_options.ptr_depth, 
+                                      0, 
+                                      m_varobj_options.max_depth, 
+                                      m_varobj_options.show_types,
+                                      m_varobj_options.show_location,
+                                      m_varobj_options.use_objc,
+                                      m_varobj_options.use_dynamic,
+                                      false,
+                                      m_varobj_options.flat_output);                                        
+
+    }
     
+    
+    static uint32_t GetVariableCallback (void *baton, 
+                                         const char *name,
+                                         VariableList &variable_list)
+    {
+        Target *target = static_cast<Target *>(baton);
+        if (target)
+        {
+            return target->GetImages().FindGlobalVariables (ConstString(name),
+                                                            true, 
+                                                            UINT32_MAX, 
+                                                            variable_list);
+        }
+        return 0;
+    }
+    
+
+
     virtual bool
     Execute (Args& args, CommandReturnObject &result)
     {
@@ -444,9 +519,13 @@
             const size_t argc = args.GetArgumentCount();
             if (argc > 0)
             {
+                Stream &s = result.GetOutputStream();
+
                 for (size_t idx = 0; idx < argc; ++idx)
                 {
-                    VariableList global_var_list;
+                    VariableList variable_list;
+                    ValueObjectList valobj_list;
+
                     const char *arg = args.GetArgumentAtIndex(idx);
                     uint32_t matches = 0;
                     if (m_option_variable.use_regex)
@@ -461,14 +540,22 @@
                         matches = exe_ctx.target->GetImages().FindGlobalVariables (regex,
                                                                                    true, 
                                                                                    UINT32_MAX, 
-                                                                                   global_var_list);
+                                                                                   variable_list);
                     }
                     else
                     {
-                        matches = exe_ctx.target->GetImages().FindGlobalVariables (ConstString(arg),
-                                                                                   true, 
-                                                                                   UINT32_MAX, 
-                                                                                   global_var_list);
+                        Error error (Variable::GetValuesForVariableExpressionPath (arg,
+                                                                                   exe_ctx.target,
+                                                                                   GetVariableCallback,
+                                                                                   exe_ctx.target,
+                                                                                   variable_list,
+                                                                                   valobj_list));
+                        
+//                        matches = exe_ctx.target->GetImages().FindGlobalVariables (ConstString(arg),
+//                                                                                   true, 
+//                                                                                   UINT32_MAX, 
+//                                                                                   variable_list);
+                        matches = variable_list.GetSize();
                     }
                     
                     if (matches == 0)
@@ -479,68 +566,17 @@
                     }
                     else
                     {
-                        Stream &s = result.GetOutputStream();
                         for (uint32_t global_idx=0; global_idx<matches; ++global_idx)
                         {
-                            VariableSP var_sp (global_var_list.GetVariableAtIndex(global_idx));
+                            VariableSP var_sp (variable_list.GetVariableAtIndex(global_idx));
                             if (var_sp)
                             {
-                                ValueObjectSP valobj_sp(ValueObjectVariable::Create (exe_ctx.target, var_sp));
+                                ValueObjectSP valobj_sp (valobj_list.GetValueObjectAtIndex(global_idx));
+                                if (!valobj_sp)
+                                    valobj_sp = ValueObjectVariable::Create (exe_ctx.target, var_sp);
                                 
                                 if (valobj_sp)
-                                {
-                                    if (m_option_variable.format != eFormatDefault)
-                                        valobj_sp->SetFormat (m_option_variable.format);
-
-                                    switch (var_sp->GetScope())
-                                    {
-                                        case eValueTypeVariableGlobal:
-                                            if (m_option_variable.show_scope)
-                                                s.PutCString("GLOBAL: ");
-                                            break;
-                                            
-                                        case eValueTypeVariableStatic:
-                                            if (m_option_variable.show_scope)
-                                                s.PutCString("STATIC: ");
-                                            break;
-                                            
-                                        case eValueTypeVariableArgument:
-                                            if (m_option_variable.show_scope)
-                                                s.PutCString("   ARG: ");
-                                            break;
-                                            
-                                        case eValueTypeVariableLocal:
-                                            if (m_option_variable.show_scope)
-                                                s.PutCString(" LOCAL: ");
-                                            break;
-                                            
-                                        default:
-                                            break;
-                                    }
-
-                                    if (m_option_variable.show_decl && var_sp->GetDeclaration ().GetFile())
-                                    {
-                                        var_sp->GetDeclaration ().DumpStopContext (&s, false);
-                                        s.PutCString (": ");
-                                    }
-
-                                    const Format format = m_option_variable.format;
-                                    if (format != eFormatDefault)
-                                        valobj_sp->SetFormat (format);
-                                    
-                                    ValueObject::DumpValueObject (s, 
-                                                                  valobj_sp.get(), 
-                                                                  var_sp->GetName().GetCString(), 
-                                                                  m_varobj_options.ptr_depth, 
-                                                                  0, 
-                                                                  m_varobj_options.max_depth, 
-                                                                  m_varobj_options.show_types,
-                                                                  m_varobj_options.show_location,
-                                                                  m_varobj_options.use_objc,
-                                                                  m_varobj_options.use_dynamic,
-                                                                  false,
-                                                                  m_varobj_options.flat_output);                                        
-                                }
+                                    DumpValueObject (s, var_sp, valobj_sp, arg);
                             }
                         }
                     }
diff --git a/source/Core/ValueObjectList.cpp b/source/Core/ValueObjectList.cpp
index f732a69..9f7185a 100644
--- a/source/Core/ValueObjectList.cpp
+++ b/source/Core/ValueObjectList.cpp
@@ -82,6 +82,18 @@
     return valobj_sp;
 }
 
+lldb::ValueObjectSP
+ValueObjectList::RemoveValueObjectAtIndex (uint32_t idx)
+{
+    lldb::ValueObjectSP valobj_sp;
+    if (idx < m_value_objects.size())
+    {
+        valobj_sp = m_value_objects[idx];
+        m_value_objects.erase (m_value_objects.begin() + idx);
+    }
+    return valobj_sp;
+}
+
 void
 ValueObjectList::SetValueObjectAtIndex (uint32_t idx, const ValueObjectSP &valobj_sp)
 {
diff --git a/source/Symbol/Variable.cpp b/source/Symbol/Variable.cpp
index 7f841b4..895fb5c 100644
--- a/source/Symbol/Variable.cpp
+++ b/source/Symbol/Variable.cpp
@@ -11,10 +11,13 @@
 
 #include "lldb/Core/Stream.h"
 #include "lldb/Core/RegularExpression.h"
+#include "lldb/Core/ValueObject.h"
+#include "lldb/Core/ValueObjectVariable.h"
 #include "lldb/Symbol/Block.h"
 #include "lldb/Symbol/Function.h"
 #include "lldb/Symbol/SymbolContext.h"
 #include "lldb/Symbol/Type.h"
+#include "lldb/Symbol/VariableList.h"
 #include "lldb/Target/Process.h"
 #include "lldb/Target/RegisterContext.h"
 #include "lldb/Target/StackFrame.h"
@@ -229,3 +232,163 @@
     return false;
 }
 
+Error
+Variable::GetValuesForVariableExpressionPath (const char *variable_expr_path,
+                                              ExecutionContextScope *scope,
+                                              GetVariableCallback callback,
+                                              void *baton,
+                                              VariableList &variable_list,
+                                              ValueObjectList &valobj_list)
+{
+    Error error;
+    if (variable_expr_path && callback)
+    {
+        switch (variable_expr_path[0])
+        {
+        case '*':
+            {
+                error = Variable::GetValuesForVariableExpressionPath (variable_expr_path + 1,
+                                                                      scope,
+                                                                      callback,
+                                                                      baton,
+                                                                      variable_list,
+                                                                      valobj_list);
+                if (error.Success())
+                {
+                    for (uint32_t i=0; i<valobj_list.GetSize(); )
+                    {
+                        Error tmp_error;
+                        ValueObjectSP valobj_sp (valobj_list.GetValueObjectAtIndex(i)->Dereference(tmp_error));
+                        if (tmp_error.Fail())
+                        {
+                            variable_list.RemoveVariableAtIndex (i);
+                            valobj_list.RemoveValueObjectAtIndex (i);
+                        }
+                        else
+                        {
+                            valobj_list.SetValueObjectAtIndex (i, valobj_sp);
+                            ++i;
+                        }
+                    }
+                }
+                else
+                {
+                    error.SetErrorString ("unknown error");
+                }
+                return error;
+            }
+            break;
+        
+        case '&':
+            {
+                error = Variable::GetValuesForVariableExpressionPath (variable_expr_path + 1,
+                                                                      scope,
+                                                                      callback,
+                                                                      baton,
+                                                                      variable_list,
+                                                                      valobj_list);
+                if (error.Success())
+                {
+                    for (uint32_t i=0; i<valobj_list.GetSize(); )
+                    {
+                        Error tmp_error;
+                        ValueObjectSP valobj_sp (valobj_list.GetValueObjectAtIndex(i)->AddressOf(tmp_error));
+                        if (tmp_error.Fail())
+                        {
+                            variable_list.RemoveVariableAtIndex (i);
+                            valobj_list.RemoveValueObjectAtIndex (i);
+                        }
+                        else
+                        {
+                            valobj_list.SetValueObjectAtIndex (i, valobj_sp);
+                            ++i;
+                        }
+                    }
+                }
+                else
+                {
+                    error.SetErrorString ("unknown error");
+                }
+                return error;
+            }
+            break;
+            
+        default:
+            {
+                RegularExpression regex ("^([A-Za-z_:][A-Za-z_0-9:]*)(.*)");
+                if (regex.Execute(variable_expr_path, 1))
+                {
+                    std::string variable_name;
+                    if (regex.GetMatchAtIndex(variable_expr_path, 1, variable_name))
+                    {
+                        variable_list.Clear();
+                        if (callback (baton, variable_name.c_str(), variable_list))
+                        {
+                            uint32_t i=0;
+                            while (i < variable_list.GetSize())
+                            {
+                                VariableSP var_sp (variable_list.GetVariableAtIndex (i));
+                                ValueObjectSP valobj_sp;
+                                if (var_sp)
+                                {
+                                    ValueObjectSP variable_valobj_sp(ValueObjectVariable::Create (scope, var_sp));
+                                    if (variable_valobj_sp)
+                                    {
+                                        variable_expr_path += variable_name.size();
+                                        if (*variable_expr_path)
+                                        {
+                                            const char* first_unparsed = NULL;
+                                            ValueObject::ExpressionPathScanEndReason reason_to_stop;
+                                            ValueObject::ExpressionPathEndResultType final_value_type;
+                                            ValueObject::GetValueForExpressionPathOptions options;
+                                            ValueObject::ExpressionPathAftermath final_task_on_target;
+
+                                            valobj_sp = variable_valobj_sp->GetValueForExpressionPath (variable_expr_path,
+                                                                                                       &first_unparsed,
+                                                                                                       &reason_to_stop,
+                                                                                                       &final_value_type,
+                                                                                                       options,
+                                                                                                       &final_task_on_target);
+                                            if (!valobj_sp)
+                                            {
+                                                error.SetErrorStringWithFormat ("invalid expression path '%s' for variable '%s'",
+                                                                                variable_expr_path,
+                                                                                var_sp->GetName().GetCString());
+                                            }
+                                        }
+                                        else
+                                        {
+                                            // Just the name of a variable with no extras
+                                            valobj_sp = variable_valobj_sp;
+                                        }
+                                    }
+                                }
+
+                                if (!var_sp || !valobj_sp)
+                                {
+                                    variable_list.RemoveVariableAtIndex (i);
+                                }
+                                else
+                                {
+                                    valobj_list.Append(valobj_sp);
+                                    ++i;
+                                }
+                            }
+                            
+                            if (variable_list.GetSize() > 0)
+                            {
+                                error.Clear();
+                                return error;
+                            }
+                        }
+                    }
+                }
+                error.SetErrorStringWithFormat ("unable to extracta variable name from '%s'", variable_expr_path);
+            }
+            break;
+        }
+    }
+    error.SetErrorString ("unknown error");
+    return error;
+}
+
diff --git a/source/Symbol/VariableList.cpp b/source/Symbol/VariableList.cpp
index 7dd80a4..1c2bb0d 100644
--- a/source/Symbol/VariableList.cpp
+++ b/source/Symbol/VariableList.cpp
@@ -72,6 +72,18 @@
     return var_sp;
 }
 
+VariableSP
+VariableList::RemoveVariableAtIndex(uint32_t idx)
+{
+    VariableSP var_sp;
+    if (idx < m_variables.size())
+    {
+        var_sp = m_variables[idx];
+        m_variables.erase (m_variables.begin() + idx);
+    }
+    return var_sp;
+}
+
 uint32_t
 VariableList::FindVariableIndex (const VariableSP &var_sp)
 {