Make a general helper function on the AST context to retrieve a type by identifier in the fashion needed by data formatters

llvm-svn: 220059
This commit is contained in:
Enrico Granata 2014-10-17 17:56:59 +00:00
parent fde87a0cdf
commit 0e478f5aa3
2 changed files with 41 additions and 27 deletions

View File

@ -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

View File

@ -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())
if (clang_type)
{
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)
{
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();
}
}
}