[Lldb-commits] [lldb] r113866 - in /lldb/trunk: include/lldb/Expression/ClangASTSource.h include/lldb/Expression/ClangExpressionDeclMap.h source/Expression/ClangASTSource.cpp source/Expression/ClangExpressionDeclMap.cpp source/Expression/ClangUserExpression.cpp

Sean Callanan scallanan at apple.com
Tue Sep 14 14:59:35 PDT 2010


Author: spyffe
Date: Tue Sep 14 16:59:34 2010
New Revision: 113866

URL: http://llvm.org/viewvc/llvm-project?rev=113866&view=rev
Log:
Added code to support use of "this" and "self" in
expressions.  This involved three main changes:

 - In ClangUserExpression::ClangUserExpression(),
   we now insert the following lines into the
   expression:
     #define this ___clang_this
     #define self ___clang_self

 - In ClangExpressionDeclMap::GetDecls(), we
   special-case ___clang_(this|self) and instead
   look up "this" or "self"

 - In ClangASTSource, we introduce the capability
   to generate Decls with a different, overridden,
   name from the one that was requested, e.g.
   this for ___clang_this.

Modified:
    lldb/trunk/include/lldb/Expression/ClangASTSource.h
    lldb/trunk/include/lldb/Expression/ClangExpressionDeclMap.h
    lldb/trunk/source/Expression/ClangASTSource.cpp
    lldb/trunk/source/Expression/ClangExpressionDeclMap.cpp
    lldb/trunk/source/Expression/ClangUserExpression.cpp

Modified: lldb/trunk/include/lldb/Expression/ClangASTSource.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Expression/ClangASTSource.h?rev=113866&r1=113865&r2=113866&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Expression/ClangASTSource.h (original)
+++ lldb/trunk/include/lldb/Expression/ClangASTSource.h Tue Sep 14 16:59:34 2010
@@ -161,8 +161,15 @@
     ///
     /// @param[in] type
     ///     The opaque QualType for the VarDecl being registered.
+    ///
+    /// @param[in] override_name
+    ///     In some cases, the name needs to be overridden (such as when
+    ///     searching for ___clang_this, which should resolve to this).
+    ///     This is the name to be used instead of what is being searched
+    ///     for.
     //------------------------------------------------------------------
-    clang::NamedDecl *AddVarDecl(void *type);
+    clang::NamedDecl *AddVarDecl(void *type,
+                                 const char *override_name = NULL);
     
     //------------------------------------------------------------------
     /// Create a FunDecl with the name being searched for and the provided

Modified: lldb/trunk/include/lldb/Expression/ClangExpressionDeclMap.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Expression/ClangExpressionDeclMap.h?rev=113866&r1=113865&r2=113866&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Expression/ClangExpressionDeclMap.h (original)
+++ lldb/trunk/include/lldb/Expression/ClangExpressionDeclMap.h Tue Sep 14 16:59:34 2010
@@ -342,14 +342,13 @@
     std::string                 m_result_name;              ///< The name of the result variable ($1, for example)
     
     //------------------------------------------------------------------
-    /// Given a symbol context, find a variable that matches the given
-    /// name and type.  We need this for expression re-use; we may not
-    /// always get the same lldb::Variable back, and we want the expression
-    /// to work wherever it can.  Returns the variable defined in the
-    /// tightest scope.
+    /// Given a stack frame, find a variable that matches the given name and 
+    /// type.  We need this for expression re-use; we may not always get the
+    /// same lldb::Variable back, and we want the expression to work wherever 
+    /// it can.  Returns the variable defined in the tightest scope.
     ///
-    /// @param[in] sym_ctx
-    ///     The SymbolContext to search for the variable.
+    /// @param[in] frame
+    ///     The stack frame to use as a basis for finding the variable.
     ///
     /// @param[in] name
     ///     The name as a plain C string.
@@ -362,9 +361,15 @@
     /// @return
     ///     The LLDB Variable found, or NULL if none was found.
     //------------------------------------------------------------------
+#ifdef OLD_CODE
     Variable *FindVariableInScope(const SymbolContext &sym_ctx,
                                   const char *name,
                                   TypeFromUser *type = NULL);
+#endif
+    
+    Variable *FindVariableInScope(StackFrame &frame,
+                                  const char *name,
+                                  TypeFromUser *type = NULL);
     
     //------------------------------------------------------------------
     /// Get the value of a variable in a given execution context and return
@@ -410,8 +415,14 @@
     ///
     /// @param[in] var
     ///     The LLDB Variable that needs a Decl.
-    //------------------------------------------------------------------
-    void AddOneVariable(NameSearchContext &context, Variable *var);
+    ///
+    /// @param[in] override_name
+    ///     A new name to give the Decl, if the one being looked for needs
+    ///     to be overriden.  Example: this for ___clang_this.
+    //------------------------------------------------------------------
+    void AddOneVariable(NameSearchContext &context, 
+                        Variable *var, 
+                        const char *override_name);
     
     //------------------------------------------------------------------
     /// Use the NameSearchContext to generate a Decl for the given

Modified: lldb/trunk/source/Expression/ClangASTSource.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Expression/ClangASTSource.cpp?rev=113866&r1=113865&r2=113866&view=diff
==============================================================================
--- lldb/trunk/source/Expression/ClangASTSource.cpp (original)
+++ lldb/trunk/source/Expression/ClangASTSource.cpp Tue Sep 14 16:59:34 2010
@@ -85,11 +85,20 @@
     return &ASTSource.Context;
 }
 
-clang::NamedDecl *NameSearchContext::AddVarDecl(void *type) {
+clang::NamedDecl *NameSearchContext::AddVarDecl(void *type,
+                                                const char *override_name) {
+    IdentifierInfo *ii = NULL;
+    
+    if (override_name)
+        ii = &ASTSource.Context.Idents.get(override_name);
+    else
+        ii = Name.getAsIdentifierInfo();
+    
+        
     clang::NamedDecl *Decl = VarDecl::Create(ASTSource.Context, 
                                              const_cast<DeclContext*>(DC), 
                                              SourceLocation(), 
-                                             Name.getAsIdentifierInfo(), 
+                                             ii, 
                                              QualType::getFromOpaquePtr(type), 
                                              0, 
                                              VarDecl::Static, 

Modified: lldb/trunk/source/Expression/ClangExpressionDeclMap.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Expression/ClangExpressionDeclMap.cpp?rev=113866&r1=113865&r2=113866&view=diff
==============================================================================
--- lldb/trunk/source/Expression/ClangExpressionDeclMap.cpp (original)
+++ lldb/trunk/source/Expression/ClangExpressionDeclMap.cpp Tue Sep 14 16:59:34 2010
@@ -539,7 +539,10 @@
 {
     Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
     
-    Variable *var = FindVariableInScope(sym_ctx, name, &type);
+    if (!exe_ctx.frame)
+        return false;
+    
+    Variable *var = FindVariableInScope(*exe_ctx.frame, name, &type);
     
     if (!var)
     {
@@ -612,6 +615,7 @@
     return true;
 }
 
+#ifdef OLD_CODE
 Variable*
 ClangExpressionDeclMap::FindVariableInScope(const SymbolContext &sym_ctx,
                                             const char *name,
@@ -715,6 +719,43 @@
     
     return NULL;
 }
+#endif
+
+Variable *
+ClangExpressionDeclMap::FindVariableInScope(StackFrame &frame,
+                                            const char *name,
+                                            TypeFromUser *type)
+{
+    Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
+    
+    ConstString name_cs(name);
+    
+    VariableList *var_list = frame.GetVariableList(true);
+    
+    lldb::VariableSP var = var_list->FindVariable(name_cs);
+    
+    if (!var)
+        return NULL;
+    
+    if (!type)
+        return var.get();
+    
+    if (type->GetASTContext() == var->GetType()->GetClangAST())
+    {
+        if (!ClangASTContext::AreTypesSame(type->GetASTContext(), type->GetOpaqueQualType(), var->GetType()->GetOpaqueClangQualType()))
+            return NULL;
+    }
+    else
+    {
+        if (log)
+            log->PutCString("Skipping a candidate variable because of different AST contexts");
+        return NULL;
+    }
+    
+    return var.get();
+    
+    return NULL;
+}
 
 // Interface for ClangASTSource
 void 
@@ -723,14 +764,23 @@
 {
     Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
     
+    const char* override_name = NULL;
+    
+    if (!strcmp(name, "___clang_this"))
+        override_name = "this";
+    if (!strcmp(name, "___clang_self"))
+        override_name = "self";
+    
+    const char *search_name = (override_name ? override_name : name);
+    
     if (log)
-        log->Printf("Hunting for a definition for %s", name);
+        log->Printf("Hunting for a definition for %s", search_name);
     
     // Back out in all cases where we're not fully initialized
     if (!m_exe_ctx || !m_exe_ctx->frame || !m_sym_ctx)
         return;
-        
-    ConstString name_cs(name);
+    
+    ConstString name_cs(search_name);
     SymbolContextList sym_ctxs;
     
     m_sym_ctx->FindFunctionsByName(name_cs, false, sym_ctxs);
@@ -763,10 +813,10 @@
         }
     }
     
-    Variable *var = FindVariableInScope(*m_sym_ctx, name);
+    Variable *var = FindVariableInScope(*m_exe_ctx->frame, search_name);
     
     if (var)
-        AddOneVariable(context, var);
+        AddOneVariable(context, var, override_name);
     
     ClangExpressionVariable *pvar(m_persistent_vars->GetVariable(name));
     
@@ -893,7 +943,8 @@
 
 void
 ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context,
-                                       Variable* var)
+                                       Variable* var,
+                                       const char *override_name)
 {
     Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
     
@@ -906,10 +957,10 @@
                                            &ut,
                                            &pt);
     
-    NamedDecl *var_decl = context.AddVarDecl(pt.GetOpaqueQualType());
+    NamedDecl *var_decl = context.AddVarDecl(pt.GetOpaqueQualType(), override_name);
     
     ClangExpressionVariable &entity(m_found_entities.VariableAtIndex(m_found_entities.CreateVariable()));
-    entity.m_name       = context.Name.getAsString();
+    entity.m_name       = (override_name ? override_name : context.Name.getAsString());
     entity.m_user_type  = ut;
     
     entity.EnableParserVars();

Modified: lldb/trunk/source/Expression/ClangUserExpression.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Expression/ClangUserExpression.cpp?rev=113866&r1=113865&r2=113866&view=diff
==============================================================================
--- lldb/trunk/source/Expression/ClangUserExpression.cpp (original)
+++ lldb/trunk/source/Expression/ClangUserExpression.cpp Tue Sep 14 16:59:34 2010
@@ -38,7 +38,13 @@
 {
     StreamString m_transformed_stream;
     
-    m_transformed_stream.Printf("extern \"C\" void %s(void *___clang_arg) { %s; }\n",
+    m_transformed_stream.Printf("#define this ___clang_this     \n"
+                                "#define self ___clang_self     \n"
+                                "extern \"C\" void              \n"
+                                "%s(void *___clang_arg)         \n"
+                                "{                              \n"
+                                    "%s;                        \n" 
+                                "}                              \n",
                                 FunctionName(),
                                 m_expr_text.c_str());
     





More information about the lldb-commits mailing list