Index: test/Index/print-type.cpp =================================================================== --- test/Index/print-type.cpp +++ test/Index/print-type.cpp @@ -61,6 +61,13 @@ struct TypeAliasUser { TypeAlias foo; }; +template +struct Foo { + T* ptr; +}; + +typedef Foo IntFoo; + // RUN: c-index-test -test-print-type %s -std=c++14 | FileCheck %s // CHECK: Namespace=outer:1:11 (Definition) [type=] [typekind=Invalid] [isPOD=0] // CHECK: ClassTemplate=Foo:4:8 (Definition) [type=] [typekind=Invalid] [isPOD=0] @@ -157,3 +164,7 @@ // CHECK: TemplateTypeParameter=T:59:20 (Definition) [type=T] [typekind=Unexposed] [canonicaltype=type-parameter-0-0] [canonicaltypekind=Unexposed] [isPOD=0] // CHECK: FieldDecl=foo:62:39 (Definition) [type=TypeAlias] [typekind=Unexposed] [canonicaltype=outer::Qux] [canonicaltypekind=Record] [templateargs/1= [type=int] [typekind=Int]] [isPOD=1] // CHECK: TemplateRef=TypeAlias:60:1 [type=] [typekind=Invalid] [isPOD=0] +// CHECK: ClassTemplate=Foo:65:8 (Definition) [type=] [typekind=Invalid] [isPOD=0] +// CHECK: TemplateTypeParameter=T:64:19 (Definition) [type=T] [typekind=Unexposed] [canonicaltype=type-parameter-0-0] [canonicaltypekind=Unexposed] [isPOD=0] +// CHECK: TypedefDecl=IntFoo:69:18 (Definition) [type=IntFoo] [typekind=Typedef] [canonicaltype=Foo] [canonicaltypekind=Record] [templateargs/1= [type=int] [typekind=Int]] [isPOD=0] +// CHECK: TemplateRef=Foo:65:8 [type=] [typekind=Invalid] [isPOD=0] Index: tools/libclang/CXType.cpp =================================================================== --- tools/libclang/CXType.cpp +++ tools/libclang/CXType.cpp @@ -925,11 +925,20 @@ QualType T = GetQualType(CT); if (T.isNull()) return -1; - const TemplateSpecializationType *Specialization = - T->getAs(); - if (!Specialization) - return -1; - return Specialization->template_arguments().size(); + + if (const TemplateSpecializationType *Specialization = + T->getAs()) { + return Specialization->template_arguments().size(); + } + + if (const CXXRecordDecl *RecordDecl = T->getAsCXXRecordDecl()) { + const ClassTemplateSpecializationDecl *TemplateDecl = + dyn_cast(RecordDecl); + if (TemplateDecl) + return TemplateDecl->getTemplateArgs().size(); + } + + return -1; } CXType clang_Type_getTemplateArgumentAsType(CXType CT, unsigned i) { @@ -937,11 +946,17 @@ if (T.isNull()) return MakeCXType(QualType(), GetTU(CT)); - const TemplateSpecializationType *Specialization = - T->getAs(); - if (!Specialization) - return MakeCXType(QualType(), GetTU(CT)); - auto TA = Specialization->template_arguments(); + ArrayRef TA; + if (const TemplateSpecializationType *Specialization = + T->getAs()) { + TA = Specialization->template_arguments(); + } else if (const CXXRecordDecl *RecordDecl = T->getAsCXXRecordDecl()) { + const ClassTemplateSpecializationDecl *TemplateDecl = + dyn_cast(RecordDecl); + if (TemplateDecl) + TA = TemplateDecl->getTemplateArgs().asArray(); + } + if (TA.size() <= i) return MakeCXType(QualType(), GetTU(CT)); const TemplateArgument &A = TA[i];