[PATCH] libclang: New functions clang_Type_getNumTemplateArguments, clang_Type_getTemplateArgument.

Matthieu Nottale mnottale at aldebaran-robotics.com
Mon Sep 16 07:57:39 PDT 2013


http://llvm-reviews.chandlerc.com/D1688

Files:
  include/clang-c/Index.h
  tools/libclang/CXType.cpp
  tools/libclang/libclang.exports

Index: include/clang-c/Index.h
===================================================================
--- include/clang-c/Index.h
+++ include/clang-c/Index.h
@@ -2994,6 +2994,20 @@
 CINDEX_LINKAGE long long clang_Type_getOffsetOf(CXType T, const char *S);
 
 /**
+ * \brief Returns the number of template parameters for given class template
+ *   specialization, or -1 if type T is not a class template specialization.
+ *
+ */
+CINDEX_LINKAGE int clang_Type_getNumTemplateArguments(CXType T);
+
+/**
+* \brief Returns the template parameter of a template class specialization
+*    at given index.
+*/
+CINDEX_LINKAGE CXType clang_Type_getTemplateArgument(CXType T, unsigned i);
+
+
+/**
  * \brief Returns non-zero if the cursor specifies a Record member that is a
  *   bitfield.
  */
Index: tools/libclang/CXType.cpp
===================================================================
--- tools/libclang/CXType.cpp
+++ tools/libclang/CXType.cpp
@@ -832,4 +832,34 @@
   return cxstring::createDup(encoding);
 }
 
+int clang_Type_getNumTemplateArguments(CXType X)
+{
+  QualType T = GetQualType(X);
+  const CXXRecordDecl* RecordDecl =  T->getAsCXXRecordDecl();
+  if (!RecordDecl)
+    return -1;
+  const ClassTemplateSpecializationDecl* TemplateDecl = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RecordDecl);
+  if (!TemplateDecl)
+    return -1;
+  return TemplateDecl->getTemplateArgs().size();
+}
+
+CXType clang_Type_getTemplateArgument(CXType CT, unsigned i)
+{
+  QualType T = GetQualType(CT);
+  const CXXRecordDecl* RecordDecl = T->getAsCXXRecordDecl();
+  if (!RecordDecl)
+    return MakeCXType(QualType(), GetTU(CT));
+  const ClassTemplateSpecializationDecl* TemplateDecl = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RecordDecl);
+  if (!TemplateDecl)
+    return MakeCXType(QualType(), GetTU(CT));
+  const TemplateArgumentList& TA =  TemplateDecl->getTemplateArgs();
+  if (TA.size() <= i)
+    return MakeCXType(QualType(), GetTU(CT));
+  const TemplateArgument& A = TA.get(i);
+  if (A.getKind() != TemplateArgument::Type)
+    return MakeCXType(QualType(), GetTU(CT));
+  return MakeCXType(A.getAsType(), GetTU(CT));
+}
+
 } // end: extern "C"
Index: tools/libclang/libclang.exports
===================================================================
--- tools/libclang/libclang.exports
+++ tools/libclang/libclang.exports
@@ -63,6 +63,8 @@
 clang_Type_getAlignOf
 clang_Type_getSizeOf
 clang_Type_getOffsetOf
+clang_Type_getNumTemplateArguments
+clang_Type_getTemplateArgument
 clang_VerbatimBlockLineComment_getText
 clang_VerbatimLineComment_getText
 clang_HTMLTagComment_getAsString
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1688.1.patch
Type: text/x-patch
Size: 2619 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130916/45016fa3/attachment.bin>


More information about the cfe-commits mailing list