[Lldb-commits] [lldb] r220059 - Make a general helper function on the AST context to retrieve a type by identifier in the fashion needed by data formatters

Enrico Granata egranata at apple.com
Fri Oct 17 10:56:59 PDT 2014


Author: enrico
Date: Fri Oct 17 12:56:59 2014
New Revision: 220059

URL: http://llvm.org/viewvc/llvm-project?rev=220059&view=rev
Log:
Make a general helper function on the AST context to retrieve a type by identifier in the fashion needed by data formatters

Modified:
    lldb/trunk/include/lldb/Symbol/ClangASTContext.h
    lldb/trunk/source/DataFormatters/NSDictionary.cpp

Modified: lldb/trunk/include/lldb/Symbol/ClangASTContext.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Symbol/ClangASTContext.h?rev=220059&r1=220058&r2=220059&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Symbol/ClangASTContext.h (original)
+++ lldb/trunk/include/lldb/Symbol/ClangASTContext.h Fri Oct 17 12:56:59 2014
@@ -19,12 +19,14 @@
 
 // Other libraries and framework includes
 #include "llvm/ADT/SmallVector.h"
+#include "clang/AST/ASTContext.h"
 #include "clang/AST/TemplateBase.h"
 
 
 // Project includes
 #include "lldb/lldb-enumerations.h"
 #include "lldb/Core/ClangForward.h"
+#include "lldb/Core/ConstString.h"
 #include "lldb/Symbol/ClangASTType.h"
 
 namespace lldb_private {
@@ -213,6 +215,34 @@ public:
     
     ClangASTType
     GetTypeForDecl (clang::ObjCInterfaceDecl *objc_decl);
+    
+    template <typename RecordDeclType>
+    ClangASTType
+    GetTypeForIdentifier (const ConstString &type_name)
+    {
+        ClangASTType clang_type;
+        
+        if (type_name.GetLength())
+        {
+            clang::ASTContext *ast = getASTContext();
+            if (ast)
+            {
+                clang::IdentifierInfo &myIdent = ast->Idents.get(type_name.GetCString());
+                clang::DeclarationName myName = ast->DeclarationNames.getIdentifier(&myIdent);
+                
+                clang::DeclContext::lookup_const_result result = ast->getTranslationUnitDecl()->lookup(myName);
+                
+                if (!result.empty())
+                {
+                    clang::NamedDecl *named_decl = result[0];
+                    if (const RecordDeclType *record_decl = llvm::dyn_cast<RecordDeclType>(named_decl))
+                        clang_type.SetClangType(ast, clang::QualType(record_decl->getTypeForDecl(), 0));
+                }
+            }
+        }
+        
+        return clang_type;
+    }
 
     //------------------------------------------------------------------
     // Structure, Unions, Classes

Modified: lldb/trunk/source/DataFormatters/NSDictionary.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/DataFormatters/NSDictionary.cpp?rev=220059&r1=220058&r2=220059&view=diff
==============================================================================
--- lldb/trunk/source/DataFormatters/NSDictionary.cpp (original)
+++ lldb/trunk/source/DataFormatters/NSDictionary.cpp Fri Oct 17 12:56:59 2014
@@ -36,37 +36,21 @@ GetLLDBNSPairType (TargetSP target_sp)
 
     if (target_ast_context)
     {
-        clang::ASTContext *ast = target_ast_context->getASTContext();
+        ConstString g___lldb_autogen_nspair("__lldb_autogen_nspair");
 
-        if (ast)
+        clang_type = target_ast_context->GetTypeForIdentifier<clang::CXXRecordDecl>(g___lldb_autogen_nspair);
+        
+        if (!clang_type)
         {
-            const char* type_name = "__lldb_autogen_nspair";
+            clang_type = target_ast_context->CreateRecordType(NULL, lldb::eAccessPublic, g___lldb_autogen_nspair.GetCString(), clang::TTK_Struct, lldb::eLanguageTypeC);
             
-            clang::IdentifierInfo &myIdent = ast->Idents.get(type_name);
-            clang::DeclarationName myName = ast->DeclarationNames.getIdentifier(&myIdent);
-
-            clang::DeclContext::lookup_const_result result = ast->getTranslationUnitDecl()->lookup(myName);
-
-            if (!result.empty())
-            {
-                clang::NamedDecl *named_decl = result[0];
-                if (const clang::CXXRecordDecl *record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(named_decl))
-                    clang_type.SetClangType(ast, clang::QualType(record_decl->getTypeForDecl(), 0));
-                return clang_type;
-            }
-
-            if (!clang_type)
+            if (clang_type)
             {
-                clang_type = target_ast_context->CreateRecordType(NULL, lldb::eAccessPublic, type_name, clang::TTK_Struct, lldb::eLanguageTypeC);
-                
-                if (clang_type)
-                {
-                    clang_type.StartTagDeclarationDefinition();
-                    ClangASTType id_clang_type = target_ast_context->GetBasicType (eBasicTypeObjCID);
-                    clang_type.AddFieldToRecordType("key", id_clang_type, lldb::eAccessPublic, 0);
-                    clang_type.AddFieldToRecordType("value", id_clang_type, lldb::eAccessPublic, 0);
-                    clang_type.CompleteTagDeclarationDefinition();
-                }
+                clang_type.StartTagDeclarationDefinition();
+                ClangASTType id_clang_type = target_ast_context->GetBasicType (eBasicTypeObjCID);
+                clang_type.AddFieldToRecordType("key", id_clang_type, lldb::eAccessPublic, 0);
+                clang_type.AddFieldToRecordType("value", id_clang_type, lldb::eAccessPublic, 0);
+                clang_type.CompleteTagDeclarationDefinition();
             }
         }
     }





More information about the lldb-commits mailing list