Add a libclang cursor kind, visitation support and USR support for C++
class templates. 


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@112627 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp
index 6629f5c..919962c 100644
--- a/tools/libclang/CIndex.cpp
+++ b/tools/libclang/CIndex.cpp
@@ -297,6 +297,7 @@
   bool VisitFieldDecl(FieldDecl *D);
   bool VisitVarDecl(VarDecl *);
   bool VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
+  bool VisitClassTemplateDecl(ClassTemplateDecl *D);
   bool VisitObjCMethodDecl(ObjCMethodDecl *ND);
   bool VisitObjCContainerDecl(ObjCContainerDecl *D);
   bool VisitObjCCategoryDecl(ObjCCategoryDecl *ND);
@@ -683,6 +684,15 @@
   return VisitFunctionDecl(D->getTemplatedDecl());
 }
 
+bool CursorVisitor::VisitClassTemplateDecl(ClassTemplateDecl *D) {
+  // FIXME: Visit the "outer" template parameter lists on the TagDecl
+  // before visiting these template parameters.
+  if (VisitTemplateParameters(D->getTemplateParameters()))
+    return true;
+  
+  return VisitCXXRecordDecl(D->getTemplatedDecl());
+}
+
 bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) {
   if (TypeSourceInfo *TSInfo = ND->getResultTypeSourceInfo())
     if (Visit(TSInfo->getTypeLoc()))
@@ -2132,6 +2142,8 @@
     return createCXString("TemplateTemplateParameter");
   case CXCursor_FunctionTemplate:
     return createCXString("FunctionTemplate");
+  case CXCursor_ClassTemplate:
+    return createCXString("ClassTemplate");
   }
 
   llvm_unreachable("Unhandled CXCursorKind");
diff --git a/tools/libclang/CIndexUSRs.cpp b/tools/libclang/CIndexUSRs.cpp
index 6dfe9df..b397f1e 100644
--- a/tools/libclang/CIndexUSRs.cpp
+++ b/tools/libclang/CIndexUSRs.cpp
@@ -66,6 +66,7 @@
   void VisitNamedDecl(NamedDecl *D);
   void VisitNamespaceDecl(NamespaceDecl *D);
   void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
+  void VisitClassTemplateDecl(ClassTemplateDecl *D);
   void VisitObjCClassDecl(ObjCClassDecl *CD);
   void VisitObjCContainerDecl(ObjCContainerDecl *CD);
   void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *P);
@@ -252,6 +253,11 @@
   VisitFunctionDecl(D->getTemplatedDecl());
 }
 
+void USRGenerator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
+  VisitTagDecl(D->getTemplatedDecl());
+}
+
+
 void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) {
   Decl *container = cast<Decl>(D->getDeclContext());
   
@@ -360,13 +366,29 @@
   D = D->getCanonicalDecl();
   VisitDeclContext(D->getDeclContext());
 
-  switch (D->getTagKind()) {
-    case TTK_Struct: Out << "@S"; break;
-    case TTK_Class:  Out << "@C"; break;
-    case TTK_Union:  Out << "@U"; break;
-    case TTK_Enum:   Out << "@E"; break;
+  bool IsTemplate = false;
+  if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(D))
+    if (ClassTemplateDecl *ClassTmpl = CXXRecord->getDescribedClassTemplate()) {
+      IsTemplate = true;
+      
+      switch (D->getTagKind()) {
+        case TTK_Struct: Out << "@ST"; break;
+        case TTK_Class:  Out << "@CT"; break;
+        case TTK_Union:  Out << "@UT"; break;
+        case TTK_Enum: llvm_unreachable("enum template");
+      }
+      VisitTemplateParameterList(ClassTmpl->getTemplateParameters());
+    }
+  
+  if (!IsTemplate) {
+    switch (D->getTagKind()) {
+      case TTK_Struct: Out << "@S"; break;
+      case TTK_Class:  Out << "@C"; break;
+      case TTK_Union:  Out << "@U"; break;
+      case TTK_Enum:   Out << "@E"; break;
+    }
   }
-
+  
   Out << '@';
   Out.flush();
   assert(Buf.size() > 0);
diff --git a/tools/libclang/CXCursor.cpp b/tools/libclang/CXCursor.cpp
index 86cf83d..283c388 100644
--- a/tools/libclang/CXCursor.cpp
+++ b/tools/libclang/CXCursor.cpp
@@ -64,6 +64,8 @@
     case Decl::NonTypeTemplateParm:return CXCursor_NonTypeTemplateParameter;
     case Decl::TemplateTemplateParm:return CXCursor_TemplateTemplateParameter;
     case Decl::FunctionTemplate:   return CXCursor_FunctionTemplate;
+    case Decl::ClassTemplate:      return CXCursor_ClassTemplate;
+      
     default:
       if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
         switch (TD->getTagKind()) {