Fix for ambiguous lookup in expressions between local variable and namespace

Summary:
In an Objective-C context a local variable and namespace can cause an ambiguous name lookup when used in an expression. The solution involves mimicking the existing C++ solution which is to add local using declarations for local variables. This causes a different type of lookup to be used which eliminates the namespace during acceptable results filtering.

Differential Revision: https://reviews.llvm.org/D59960

llvm-svn: 359921
diff --git a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionSourceCode.cpp b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionSourceCode.cpp
index 0bc664c..ce604aa 100644
--- a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionSourceCode.cpp
+++ b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionSourceCode.cpp
@@ -200,17 +200,31 @@
 
 static void AddLocalVariableDecls(const lldb::VariableListSP &var_list_sp,
                                   StreamString &stream,
-                                  const std::string &expr) {
+                                  const std::string &expr,
+                                  lldb::LanguageType wrapping_language) {
   for (size_t i = 0; i < var_list_sp->GetSize(); i++) {
     lldb::VariableSP var_sp = var_list_sp->GetVariableAtIndex(i);
 
     ConstString var_name = var_sp->GetName();
-    if (!var_name || var_name == "this" || var_name == ".block_descriptor")
+
+
+    // We can check for .block_descriptor w/o checking for langauge since this
+    // is not a valid identifier in either C or C++.
+    if (!var_name || var_name == ConstString(".block_descriptor"))
       continue;
 
     if (!expr.empty() && !ExprBodyContainsVar(expr, var_name.GetStringRef()))
       continue;
 
+    if ((var_name == ConstString("self") || var_name == ConstString("_cmd")) &&
+        (wrapping_language == lldb::eLanguageTypeObjC ||
+         wrapping_language == lldb::eLanguageTypeObjC_plus_plus))
+      continue;
+
+    if (var_name == ConstString("this") &&
+        wrapping_language == lldb::eLanguageTypeC_plus_plus)
+      continue;
+
     stream.Printf("using $__lldb_local_vars::%s;\n", var_name.AsCString());
   }
 }
@@ -291,16 +305,14 @@
       }
     }
 
-    if (add_locals) {
-      if (Language::LanguageIsCPlusPlus(frame->GetLanguage())) {
-        if (target->GetInjectLocalVariables(&exe_ctx)) {
-          lldb::VariableListSP var_list_sp =
-              frame->GetInScopeVariableList(false, true);
-          AddLocalVariableDecls(var_list_sp, lldb_local_var_decls,
-                                force_add_all_locals ? "" : m_body);
-        }
+    if (add_locals)
+      if (target->GetInjectLocalVariables(&exe_ctx)) {
+        lldb::VariableListSP var_list_sp =
+            frame->GetInScopeVariableList(false, true);
+        AddLocalVariableDecls(var_list_sp, lldb_local_var_decls,
+                              force_add_all_locals ? "" : m_body,
+                              wrapping_language);
       }
-    }
   }
 
   if (m_wrap) {
@@ -378,11 +390,12 @@
             "@implementation $__lldb_objc_class ($__lldb_category)   \n"
             "+(void)%s:(void *)$__lldb_arg                           \n"
             "{                                                       \n"
+            "    %s;                                                 \n"
             "%s"
             "}                                                       \n"
             "@end                                                    \n",
             module_imports.c_str(), m_name.c_str(), m_name.c_str(),
-            tagged_body.c_str());
+            lldb_local_var_decls.GetData(), tagged_body.c_str());
       } else {
         wrap_stream.Printf(
             "%s"
@@ -392,11 +405,12 @@
             "@implementation $__lldb_objc_class ($__lldb_category)  \n"
             "-(void)%s:(void *)$__lldb_arg                          \n"
             "{                                                      \n"
+            "    %s;                                                 \n"
             "%s"
             "}                                                      \n"
             "@end                                                   \n",
             module_imports.c_str(), m_name.c_str(), m_name.c_str(),
-            tagged_body.c_str());
+            lldb_local_var_decls.GetData(), tagged_body.c_str());
       }
       break;
     }