[llvm-branch-commits] [cfe-branch] r115436 - in /cfe/branches/Apple/whitney: include/clang-c/Index.h test/Index/print-display-names.cpp tools/c-index-test/c-index-test.c tools/libclang/CIndex.cpp tools/libclang/libclang.darwin.exports tools/libclang/libclang.exports

Daniel Dunbar daniel at zuster.org
Sat Oct 2 16:24:05 PDT 2010


Author: ddunbar
Date: Sat Oct  2 18:24:05 2010
New Revision: 115436

URL: http://llvm.org/viewvc/llvm-project?rev=115436&view=rev
Log:
Merge r115428:
--
Author: Douglas Gregor <dgregor at apple.com>
Date:   Sat Oct 2 22:49:11 2010 +0000

    Introduce a new libclang function, clang_getCursorDisplayName(), which
    produces a simple "display" name that captures the
    arguments/parameters for a function, function template, class
    template, or class template specialization.

Added:
    cfe/branches/Apple/whitney/test/Index/print-display-names.cpp
Modified:
    cfe/branches/Apple/whitney/include/clang-c/Index.h
    cfe/branches/Apple/whitney/tools/c-index-test/c-index-test.c
    cfe/branches/Apple/whitney/tools/libclang/CIndex.cpp
    cfe/branches/Apple/whitney/tools/libclang/libclang.darwin.exports
    cfe/branches/Apple/whitney/tools/libclang/libclang.exports

Modified: cfe/branches/Apple/whitney/include/clang-c/Index.h
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/include/clang-c/Index.h?rev=115436&r1=115435&r2=115436&view=diff
==============================================================================
--- cfe/branches/Apple/whitney/include/clang-c/Index.h (original)
+++ cfe/branches/Apple/whitney/include/clang-c/Index.h Sat Oct  2 18:24:05 2010
@@ -1832,6 +1832,15 @@
  */
 CINDEX_LINKAGE CXString clang_getCursorSpelling(CXCursor);
 
+/**
+ * \brief Retrieve the display name for the entity referenced by this cursor.
+ *
+ * The display name contains extra information that helps identify the cursor,
+ * such as the parameters of a function or template or the arguments of a 
+ * class template specialization.
+ */
+CINDEX_LINKAGE CXString clang_getCursorDisplayName(CXCursor);
+  
 /** \brief For a cursor that is a reference, retrieve a cursor representing the
  * entity that it references.
  *

Added: cfe/branches/Apple/whitney/test/Index/print-display-names.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/test/Index/print-display-names.cpp?rev=115436&view=auto
==============================================================================
--- cfe/branches/Apple/whitney/test/Index/print-display-names.cpp (added)
+++ cfe/branches/Apple/whitney/test/Index/print-display-names.cpp Sat Oct  2 18:24:05 2010
@@ -0,0 +1,20 @@
+template<typename T, typename>
+class ClassTmpl { };
+
+typedef int Integer;
+
+template class ClassTmpl<Integer, Integer>;
+
+void f(ClassTmpl<float, Integer> p);
+
+template<typename T>
+void g(ClassTmpl<T, T>);
+
+template<> void g<int>(ClassTmpl<int, int>);
+
+// RUN: c-index-test -test-load-source all-display %s | FileCheck %s
+// CHECK: print-display-names.cpp:2:7: ClassTemplate=ClassTmpl<T, typename>:2:7
+// CHECK: print-display-names.cpp:6:16: ClassDecl=ClassTmpl<Integer, Integer>:6:16 (Definition)
+// CHECK: print-display-names.cpp:8:6: FunctionDecl=f(ClassTmpl<float, Integer>):8:6
+// CHECK: print-display-names.cpp:11:6: FunctionTemplate=g(ClassTmpl<T, T>):11:6
+// CHECK: print-display-names.cpp:13:17: FunctionDecl=g<>(ClassTmpl<int, int>):13:17 [Specialization of g:11:6]

Modified: cfe/branches/Apple/whitney/tools/c-index-test/c-index-test.c
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/tools/c-index-test/c-index-test.c?rev=115436&r1=115435&r2=115436&view=diff
==============================================================================
--- cfe/branches/Apple/whitney/tools/c-index-test/c-index-test.c (original)
+++ cfe/branches/Apple/whitney/tools/c-index-test/c-index-test.c Sat Oct  2 18:24:05 2010
@@ -156,6 +156,8 @@
 /* Pretty-printing.                                                           */
 /******************************************************************************/
 
+int want_display_name = 0;
+
 static void PrintCursor(CXCursor Cursor) {
   if (clang_isInvalid(Cursor.kind)) {
     CXString ks = clang_getCursorKindSpelling(Cursor.kind);
@@ -169,7 +171,8 @@
     CXCursor SpecializationOf;
     
     ks = clang_getCursorKindSpelling(Cursor.kind);
-    string = clang_getCursorSpelling(Cursor);
+    string = want_display_name? clang_getCursorDisplayName(Cursor) 
+                              : clang_getCursorSpelling(Cursor);
     printf("%s=%s", clang_getCString(ks),
                     clang_getCString(string));
     clang_disposeString(ks);
@@ -587,6 +590,11 @@
 
     /* Perform some simple filtering. */
     if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
+    else if (!strcmp(filter, "all-display") || 
+             !strcmp(filter, "local-display")) {
+      ck = NULL;
+      want_display_name = 1;
+    }
     else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
     else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
     else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
@@ -644,7 +652,8 @@
   int result;
   
   Idx = clang_createIndex(/* excludeDeclsFromPCH */
-                          !strcmp(filter, "local") ? 1 : 0,
+                          (!strcmp(filter, "local") || 
+                           !strcmp(filter, "local-display"))? 1 : 0,
                           /* displayDiagnosics=*/1);
 
   if (UseExternalASTs && strlen(UseExternalASTs))

Modified: cfe/branches/Apple/whitney/tools/libclang/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/tools/libclang/CIndex.cpp?rev=115436&r1=115435&r2=115436&view=diff
==============================================================================
--- cfe/branches/Apple/whitney/tools/libclang/CIndex.cpp (original)
+++ cfe/branches/Apple/whitney/tools/libclang/CIndex.cpp Sat Oct  2 18:24:05 2010
@@ -33,6 +33,7 @@
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/Support/CrashRecoveryContext.h"
 #include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/raw_ostream.h"
 #include "llvm/Support/Timer.h"
 #include "llvm/System/Mutex.h"
 #include "llvm/System/Program.h"
@@ -2695,6 +2696,90 @@
   return createCXString("");
 }
 
+CXString clang_getCursorDisplayName(CXCursor C) {
+  if (!clang_isDeclaration(C.kind))
+    return clang_getCursorSpelling(C);
+  
+  Decl *D = getCursorDecl(C);
+  if (!D)
+    return createCXString("");
+
+  PrintingPolicy &Policy = getCursorContext(C).PrintingPolicy;
+  if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D))
+    D = FunTmpl->getTemplatedDecl();
+  
+  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
+    llvm::SmallString<64> Str;
+    llvm::raw_svector_ostream OS(Str);
+    OS << Function->getNameAsString();
+    if (Function->getPrimaryTemplate())
+      OS << "<>";
+    OS << "(";
+    for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) {
+      if (I)
+        OS << ", ";
+      OS << Function->getParamDecl(I)->getType().getAsString(Policy);
+    }
+    
+    if (Function->isVariadic()) {
+      if (Function->getNumParams())
+        OS << ", ";
+      OS << "...";
+    }
+    OS << ")";
+    return createCXString(OS.str());
+  }
+  
+  if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) {
+    llvm::SmallString<64> Str;
+    llvm::raw_svector_ostream OS(Str);
+    OS << ClassTemplate->getNameAsString();
+    OS << "<";
+    TemplateParameterList *Params = ClassTemplate->getTemplateParameters();
+    for (unsigned I = 0, N = Params->size(); I != N; ++I) {
+      if (I)
+        OS << ", ";
+      
+      NamedDecl *Param = Params->getParam(I);
+      if (Param->getIdentifier()) {
+        OS << Param->getIdentifier()->getName();
+        continue;
+      }
+      
+      // There is no parameter name, which makes this tricky. Try to come up
+      // with something useful that isn't too long.
+      if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
+        OS << (TTP->wasDeclaredWithTypename()? "typename" : "class");
+      else if (NonTypeTemplateParmDecl *NTTP
+                                    = dyn_cast<NonTypeTemplateParmDecl>(Param))
+        OS << NTTP->getType().getAsString(Policy);
+      else
+        OS << "template<...> class";
+    }
+    
+    OS << ">";
+    return createCXString(OS.str());
+  }
+  
+  if (ClassTemplateSpecializationDecl *ClassSpec
+                              = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
+    // If the type was explicitly written, use that.
+    if (TypeSourceInfo *TSInfo = ClassSpec->getTypeAsWritten())
+      return createCXString(TSInfo->getType().getAsString(Policy));
+    
+    llvm::SmallString<64> Str;
+    llvm::raw_svector_ostream OS(Str);
+    OS << ClassSpec->getNameAsString();
+    OS << TemplateSpecializationType::PrintTemplateArgumentList(
+                            ClassSpec->getTemplateArgs().getFlatArgumentList(),
+                                      ClassSpec->getTemplateArgs().flat_size(),
+                                                                Policy);
+    return createCXString(OS.str());
+  }
+  
+  return clang_getCursorSpelling(C);
+}
+  
 CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) {
   switch (Kind) {
   case CXCursor_FunctionDecl:

Modified: cfe/branches/Apple/whitney/tools/libclang/libclang.darwin.exports
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/tools/libclang/libclang.darwin.exports?rev=115436&r1=115435&r2=115436&view=diff
==============================================================================
--- cfe/branches/Apple/whitney/tools/libclang/libclang.darwin.exports (original)
+++ cfe/branches/Apple/whitney/tools/libclang/libclang.darwin.exports Sat Oct  2 18:24:05 2010
@@ -41,6 +41,7 @@
 _clang_getCursor
 _clang_getCursorAvailability
 _clang_getCursorDefinition
+_clang_getCursorDisplayName
 _clang_getCursorExtent
 _clang_getCursorKind
 _clang_getCursorKindSpelling

Modified: cfe/branches/Apple/whitney/tools/libclang/libclang.exports
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/tools/libclang/libclang.exports?rev=115436&r1=115435&r2=115436&view=diff
==============================================================================
--- cfe/branches/Apple/whitney/tools/libclang/libclang.exports (original)
+++ cfe/branches/Apple/whitney/tools/libclang/libclang.exports Sat Oct  2 18:24:05 2010
@@ -41,6 +41,7 @@
 clang_getCursor
 clang_getCursorAvailability
 clang_getCursorDefinition
+clang_getCursorDisplayName
 clang_getCursorExtent
 clang_getCursorKind
 clang_getCursorKindSpelling





More information about the llvm-branch-commits mailing list