From: Douglas Gregor Date: Mon, 24 Jan 2011 18:44:28 +0000 (+0000) Subject: Eliminate the use of getTypeForDecl from clang_getCursorType() and X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=fbcfeeabed6aa60ba664be302f2b03bed3d1b24d;p=clang Eliminate the use of getTypeForDecl from clang_getCursorType() and clang_getDeclObjCTypeEncoding(); use ASTContext's methods instead, which will (lazily) create the type as needed. Otherwise, we can end up with null QualTypes. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@124133 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/test/Index/print-typekind.c b/test/Index/print-typekind.c index d521f06ed0..fad3a2dc25 100644 --- a/test/Index/print-typekind.c +++ b/test/Index/print-typekind.c @@ -4,6 +4,7 @@ int *f(int *p, char *x, FooType z) { FooType w = z; return p + z; } +typedef double OtherType; // RUN: c-index-test -test-print-typekind %s | FileCheck %s // CHECK: TypedefDecl=FooType:1:13 (Definition) typekind=Typedef [canonical=Int] [isPOD=1] @@ -22,4 +23,5 @@ int *f(int *p, char *x, FooType z) { // CHECK: UnexposedExpr= typekind=Pointer [isPOD=1] // CHECK: DeclRefExpr=p:3:13 typekind=Pointer [isPOD=1] // CHECK: DeclRefExpr=z:3:33 typekind=Typedef [canonical=Int] [isPOD=1] +// CHECK: TypedefDecl=OtherType:7:16 (Definition) typekind=Typedef [canonical=Double] [isPOD=1] diff --git a/tools/libclang/CXType.cpp b/tools/libclang/CXType.cpp index 7cb4492804..7b603c0f93 100644 --- a/tools/libclang/CXType.cpp +++ b/tools/libclang/CXType.cpp @@ -113,6 +113,7 @@ CXType clang_getCursorType(CXCursor C) { using namespace cxcursor; CXTranslationUnit TU = cxcursor::getCursorTU(C); + ASTContext &Context = static_cast(TU->TUData)->getASTContext(); if (clang_isExpression(C.kind)) { QualType T = cxcursor::getCursorExpr(C)->getType(); return MakeCXType(T, TU); @@ -122,9 +123,9 @@ CXType clang_getCursorType(CXCursor C) { Decl *D = cxcursor::getCursorDecl(C); if (TypeDecl *TD = dyn_cast(D)) - return MakeCXType(QualType(TD->getTypeForDecl(), 0), TU); + return MakeCXType(Context.getTypeDeclType(TD), TU); if (ObjCInterfaceDecl *ID = dyn_cast(D)) - return MakeCXType(QualType(ID->getTypeForDecl(), 0), TU); + return MakeCXType(Context.getObjCInterfaceType(ID), TU); if (ValueDecl *VD = dyn_cast(D)) return MakeCXType(VD->getType(), TU); if (ObjCPropertyDecl *PD = dyn_cast(D)) @@ -136,22 +137,22 @@ CXType clang_getCursorType(CXCursor C) { if (clang_isReference(C.kind)) { switch (C.kind) { - case CXCursor_ObjCSuperClassRef: - return MakeCXType( - QualType(getCursorObjCSuperClassRef(C).first->getTypeForDecl(), - 0), - TU); - - case CXCursor_ObjCClassRef: - return MakeCXType( - QualType(getCursorObjCClassRef(C).first->getTypeForDecl(), - 0), - TU); - - case CXCursor_TypeRef: - return MakeCXType(QualType(getCursorTypeRef(C).first->getTypeForDecl(), - 0), - TU); + case CXCursor_ObjCSuperClassRef: { + QualType T + = Context.getObjCInterfaceType(getCursorObjCSuperClassRef(C).first); + return MakeCXType(T, TU); + } + + case CXCursor_ObjCClassRef: { + QualType T = Context.getObjCInterfaceType(getCursorObjCClassRef(C).first); + return MakeCXType(T, TU); + } + + case CXCursor_TypeRef: { + QualType T = Context.getTypeDeclType(getCursorTypeRef(C).first); + return MakeCXType(T, TU); + + } case CXCursor_CXXBaseSpecifier: return cxtype::MakeCXType(getCursorCXXBaseSpecifier(C)->getType(), TU); @@ -372,7 +373,7 @@ CXString clang_getDeclObjCTypeEncoding(CXCursor C) { else { QualType Ty; if (TypeDecl *TD = dyn_cast(D)) - Ty = QualType(TD->getTypeForDecl(), 0); + Ty = Ctx.getTypeDeclType(TD); if (ValueDecl *VD = dyn_cast(D)) Ty = VD->getType(); else return cxstring::createCXString("?");