r287024 - [libclang] Generalize clang_getNumTemplateArguments and clang_getTemplateArgumentAsType to other kind of specializations.

Argyrios Kyrtzidis via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 15 12:51:47 PST 2016


Author: akirtzidis
Date: Tue Nov 15 14:51:46 2016
New Revision: 287024

URL: http://llvm.org/viewvc/llvm-project?rev=287024&view=rev
Log:
[libclang] Generalize clang_getNumTemplateArguments and clang_getTemplateArgumentAsType to other kind of specializations.

Patch by Emilio Cobos Álvarez!
https://reviews.llvm.org/D26663

Modified:
    cfe/trunk/include/clang-c/Index.h
    cfe/trunk/test/Index/print-type.cpp
    cfe/trunk/tools/libclang/CXType.cpp

Modified: cfe/trunk/include/clang-c/Index.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=287024&r1=287023&r2=287024&view=diff
==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Tue Nov 15 14:51:46 2016
@@ -3516,11 +3516,8 @@ enum CXRefQualifierKind {
 };
 
 /**
- * \brief Returns the number of template arguments for given class template
- * specialization, or -1 if type \c T is not a class template specialization.
- *
- * Variadic argument packs count as only one argument, and can not be inspected
- * further.
+ * \brief Returns the number of template arguments for given template
+ * specialization, or -1 if type \c T is not a template specialization.
  */
 CINDEX_LINKAGE int clang_Type_getNumTemplateArguments(CXType T);
 

Modified: cfe/trunk/test/Index/print-type.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/print-type.cpp?rev=287024&r1=287023&r2=287024&view=diff
==============================================================================
--- cfe/trunk/test/Index/print-type.cpp (original)
+++ cfe/trunk/test/Index/print-type.cpp Tue Nov 15 14:51:46 2016
@@ -56,6 +56,11 @@ auto autoBlob = new Blob();
 auto autoFunction(){return int();}
 decltype(auto) autoInt = 5;
 
+template <typename T>
+using TypeAlias = outer::Qux<T>;
+
+struct TypeAliasUser { TypeAlias<int> foo; };
+
 // 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]
@@ -99,7 +104,7 @@ decltype(auto) autoInt = 5;
 // CHECK: TemplateRef=Baz:9:8 [type=] [typekind=Invalid] [isPOD=0]
 // CHECK: IntegerLiteral= [type=int] [typekind=Int] [isPOD=1]
 // CHECK: TemplateRef=Foo:4:8 [type=] [typekind=Invalid] [isPOD=0]
-// CHECK: FieldDecl=qux:28:29 (Definition) [type=Qux<int, char *, Foo<int> >] [typekind=Unexposed] [canonicaltype=outer::Qux<int, char *, outer::Foo<int> >] [canonicaltypekind=Record] [templateargs/1=] [isPOD=1]
+// CHECK: FieldDecl=qux:28:29 (Definition) [type=Qux<int, char *, Foo<int> >] [typekind=Unexposed] [canonicaltype=outer::Qux<int, char *, outer::Foo<int> >] [canonicaltypekind=Record] [templateargs/3= [type=int] [typekind=Int] [type=char *] [typekind=Pointer] [type=Foo<int>] [typekind=Unexposed]] [isPOD=1]
 // CHECK: TemplateRef=Qux:12:8 [type=] [typekind=Invalid] [isPOD=0]
 // CHECK: TemplateRef=Foo:4:8 [type=] [typekind=Invalid] [isPOD=0]
 // CHECK: FunctionTemplate=tbar:35:3 [type=T (int)] [typekind=FunctionProto] [canonicaltype=type-parameter-0-0 (int)] [canonicaltypekind=FunctionProto] [resulttype=T] [resulttypekind=Unexposed] [isPOD=0]
@@ -148,3 +153,7 @@ decltype(auto) autoInt = 5;
 // CHECK: UnexposedExpr= [type=int] [typekind=Int] [isPOD=1]
 // CHECK: VarDecl=autoInt:57:16 (Definition) [type=int] [typekind=Auto] [canonicaltype=int] [canonicaltypekind=Int] [isPOD=1]
 // CHECK: IntegerLiteral= [type=int] [typekind=Int] [isPOD=1]
+// CHECK: TypeAliasTemplateDecl=TypeAlias:60:1 (Definition) [type=] [typekind=Invalid] [isPOD=0]
+// 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<int>] [typekind=Unexposed] [canonicaltype=outer::Qux<int>] [canonicaltypekind=Record] [templateargs/1= [type=int] [typekind=Int]] [isPOD=1]
+// CHECK: TemplateRef=TypeAlias:60:1 [type=] [typekind=Invalid] [isPOD=0]

Modified: cfe/trunk/tools/libclang/CXType.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXType.cpp?rev=287024&r1=287023&r2=287024&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CXType.cpp (original)
+++ cfe/trunk/tools/libclang/CXType.cpp Tue Nov 15 14:51:46 2016
@@ -925,31 +925,26 @@ int clang_Type_getNumTemplateArguments(C
   QualType T = GetQualType(CT);
   if (T.isNull())
     return -1;
-  const CXXRecordDecl *RecordDecl = T->getAsCXXRecordDecl();
-  if (!RecordDecl)
+  const TemplateSpecializationType *Specialization =
+    T->getAs<TemplateSpecializationType>();
+  if (!Specialization)
     return -1;
-  const ClassTemplateSpecializationDecl *TemplateDecl =
-      dyn_cast<ClassTemplateSpecializationDecl>(RecordDecl);
-  if (!TemplateDecl)
-    return -1;
-  return TemplateDecl->getTemplateArgs().size();
+  return Specialization->template_arguments().size();
 }
 
 CXType clang_Type_getTemplateArgumentAsType(CXType CT, unsigned i) {
   QualType T = GetQualType(CT);
   if (T.isNull())
     return MakeCXType(QualType(), GetTU(CT));
-  const CXXRecordDecl *RecordDecl = T->getAsCXXRecordDecl();
-  if (!RecordDecl)
-    return MakeCXType(QualType(), GetTU(CT));
-  const ClassTemplateSpecializationDecl *TemplateDecl =
-      dyn_cast<ClassTemplateSpecializationDecl>(RecordDecl);
-  if (!TemplateDecl)
+
+  const TemplateSpecializationType *Specialization =
+    T->getAs<TemplateSpecializationType>();
+  if (!Specialization)
     return MakeCXType(QualType(), GetTU(CT));
-  const TemplateArgumentList &TA = TemplateDecl->getTemplateArgs();
+  auto TA = Specialization->template_arguments();
   if (TA.size() <= i)
     return MakeCXType(QualType(), GetTU(CT));
-  const TemplateArgument &A = TA.get(i);
+  const TemplateArgument &A = TA[i];
   if (A.getKind() != TemplateArgument::Type)
     return MakeCXType(QualType(), GetTU(CT));
   return MakeCXType(A.getAsType(), GetTU(CT));




More information about the cfe-commits mailing list