Add C API hook 'clang_getDeclExtent()', which returns the source extent of a declaration.  This implements <rdar://problem/7280072>.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@92802 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/tools/CIndex/CIndex.cpp b/tools/CIndex/CIndex.cpp
index 7b624e6..fb359c9 100644
--- a/tools/CIndex/CIndex.cpp
+++ b/tools/CIndex/CIndex.cpp
@@ -569,6 +569,25 @@
   SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
   return SourceMgr.getSpellingColumnNumber(ND->getLocation());
 }
+  
+CXDeclExtent clang_getDeclExtent(CXDecl AnonDecl) {
+  assert(AnonDecl && "Passed null CXDecl");
+  NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
+  SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
+  SourceRange R = ND->getSourceRange();
+
+  CXDeclExtent extent;
+  
+  SourceLocation L = SourceMgr.getSpellingLoc(R.getBegin());
+  extent.begin.line = SourceMgr.getSpellingLineNumber(L);
+  extent.begin.column = SourceMgr.getSpellingColumnNumber(L);
+  
+  L = SourceMgr.getSpellingLoc(R.getEnd());
+  extent.end.line = SourceMgr.getSpellingLineNumber(L);
+  extent.end.column = SourceMgr.getSpellingColumnNumber(L);
+  
+  return extent;  
+}
 
 const char *clang_getDeclSource(CXDecl AnonDecl) {
   assert(AnonDecl && "Passed null CXDecl");
diff --git a/tools/CIndex/CIndex.exports b/tools/CIndex/CIndex.exports
index 2548c82..ab68217 100644
--- a/tools/CIndex/CIndex.exports
+++ b/tools/CIndex/CIndex.exports
@@ -23,6 +23,7 @@
 _clang_getCursorSpelling
 _clang_getDeclColumn
 _clang_getDeclLine
+_clang_getDeclExtent
 _clang_getDeclSource
 _clang_getDeclSourceFile
 _clang_getDeclSpelling
diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c
index 33013f3..e7253e0 100644
--- a/tools/c-index-test/c-index-test.c
+++ b/tools/c-index-test/c-index-test.c
@@ -70,16 +70,32 @@
 /* Logic for testing clang_loadTranslationUnit().                             */
 /******************************************************************************/
 
+static const char *FileCheckPrefix = "CHECK";
+
+static void PrintDeclExtent(CXDecl Dcl) {
+  CXSourceExtent extent = clang_getDeclExtent(Dcl);
+  printf(" [Extent=%d:%d:%d:%d]", extent.begin.line, extent.begin.column,
+         extent.end.line, extent.end.column);
+}
+
 static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter) {
   if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
     CXString string;
-    printf("// CHECK: %s:%d:%d: ", GetCursorSource(Cursor),
-                                 clang_getCursorLine(Cursor),
-                                 clang_getCursorColumn(Cursor));
+    CXDecl subDecl;
+    CXSourceExtent extent;
+    printf("// %s: %s:%d:%d: ", FileCheckPrefix,
+                                GetCursorSource(Cursor),
+                                clang_getCursorLine(Cursor),
+                                clang_getCursorColumn(Cursor));
     PrintCursor(Cursor);
+
     string = clang_getDeclSpelling(Dcl);
-    printf(" [Context=%s]\n", clang_getCString(string));
+    printf(" [Context=%s]", clang_getCString(string));
     clang_disposeString(string);
+    
+    PrintDeclExtent(clang_getCursorDecl(Cursor));
+
+    printf("\n");
   }
 }
 
@@ -87,15 +103,19 @@
                                    CXClientData Filter) {
   if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
     CXString string;
-    printf("// CHECK: %s:%d:%d: ", GetCursorSource(Cursor),
-                                 clang_getCursorLine(Cursor),
-                                 clang_getCursorColumn(Cursor));
+    printf("// %s: %s:%d:%d: ", FileCheckPrefix,
+           GetCursorSource(Cursor), clang_getCursorLine(Cursor),
+           clang_getCursorColumn(Cursor));
     PrintCursor(Cursor);
     string = clang_getTranslationUnitSpelling(Unit);
-    printf(" [Context=%s]\n",
+    printf(" [Context=%s]",
           basename(clang_getCString(string)));
     clang_disposeString(string);
+    
+    PrintDeclExtent(Cursor.decl);
 
+    printf("\n");
+    
     clang_loadDeclaration(Cursor.decl, DeclVisitor, 0);
   }
 }
@@ -130,7 +150,7 @@
       /* Nothing found here; that's fine. */
     } else if (Ref.kind != CXCursor_FunctionDecl) {
       CXString string;
-      printf("// CHECK: %s:%d:%d: ", GetCursorSource(Ref),
+      printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
              curLine, curColumn);
       PrintCursor(Ref);
       string = clang_getDeclSpelling(Ref.decl);
@@ -142,11 +162,14 @@
 }
 
 static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
-                             const char *filter) {
+                             const char *filter, const char *prefix) {
   enum CXCursorKind K = CXCursor_NotImplemented;
   CXTranslationUnitIterator Visitor = TranslationUnitVisitor;
   enum CXCursorKind *ck = &K;
 
+  if (prefix)
+    FileCheckPrefix = prefix;  
+  
   /* Perform some simple filtering. */
   if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
   else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
@@ -165,7 +188,8 @@
   return 0;
 }
 
-int perform_test_load_tu(const char *file, const char *filter) {
+int perform_test_load_tu(const char *file, const char *filter,
+                         const char *prefix) {
   CXIndex Idx;
   CXTranslationUnit TU;
   Idx = clang_createIndex(/* excludeDeclsFromPCH */ 
@@ -175,7 +199,7 @@
   if (!CreateTranslationUnit(Idx, file, &TU))
     return 1;
 
-  return perform_test_load(Idx, TU, filter);
+  return perform_test_load(Idx, TU, filter, prefix);
 }
 
 int perform_test_load_source(int argc, const char **argv, const char *filter) {
@@ -196,7 +220,7 @@
     return 1;
   }
 
-  return perform_test_load(Idx, TU, filter);
+  return perform_test_load(Idx, TU, filter, NULL);
 }
 
 /******************************************************************************/
@@ -536,7 +560,8 @@
     "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
     "       c-index-test -test-file-scan <AST file> <source file> "
           "[FileCheck prefix]\n"
-    "       c-index-test -test-load-tu <AST file> <symbol filter>\n\n"
+    "       c-index-test -test-load-tu <AST file> <symbol filter> "
+          "[FileCheck prefix]\n"
     "       c-index-test -test-load-source <symbol filter> {<args>}*\n\n"
     " <symbol filter> options for -test-load-tu and -test-load-source:\n%s",
     "   all - load all symbols, including those from PCH\n"
@@ -552,8 +577,9 @@
 int main(int argc, const char **argv) {
   if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
     return perform_code_completion(argc, argv);
-  if (argc == 4 && strcmp(argv[1], "-test-load-tu") == 0)
-    return perform_test_load_tu(argv[2], argv[3]);
+  if (argc >= 4 && strcmp(argv[1], "-test-load-tu") == 0)
+    return perform_test_load_tu(argv[2], argv[3],
+                                argc >= 5 ? argv[4] : 0);
   if (argc >= 4 && strcmp(argv[1], "-test-load-source") == 0)
     return perform_test_load_source(argc - 3, argv + 3, argv[2]);
   if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)