[cfe-commits] r80585 - in /cfe/trunk: include/clang-c/Index.h tools/CIndex/CIndex.cpp tools/c-index-test/c-index-test.c

Steve Naroff snaroff at apple.com
Mon Aug 31 07:26:52 PDT 2009


Author: snaroff
Date: Mon Aug 31 09:26:51 2009
New Revision: 80585

URL: http://llvm.org/viewvc/llvm-project?rev=80585&view=rev
Log:
Implement source/line/column hooks.

Modified:
    cfe/trunk/include/clang-c/Index.h
    cfe/trunk/tools/CIndex/CIndex.cpp
    cfe/trunk/tools/c-index-test/c-index-test.c

Modified: cfe/trunk/include/clang-c/Index.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=80585&r1=80584&r2=80585&view=diff

==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Mon Aug 31 09:26:51 2009
@@ -25,7 +25,7 @@
    clangs PCH file (which contains AST's, or Abstract Syntax Trees). PCH files
    are created by the following command:
    
-   "clang -emit-pch <sourcefile.langsuffix> -o <sourcefile.ast>". 
+   "clang -S -Xclang -emit-pch <sourcefile.langsuffix> -o <sourcefile.ast>". 
    
    If the ast file format ends up diverging from the pch file format, we will 
    need to add a new switch (-emit-ast). For now, the contents are identical.

Modified: cfe/trunk/tools/CIndex/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndex.cpp?rev=80585&r1=80584&r2=80585&view=diff

==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndex.cpp Mon Aug 31 09:26:51 2009
@@ -16,6 +16,7 @@
 #include "clang/Index/Indexer.h"
 #include "clang/AST/DeclVisitor.h"
 #include "clang/Basic/FileManager.h"
+#include "clang/Basic/SourceManager.h"
 #include "clang/Frontend/ASTUnit.h"
 #include <cstdio>
 using namespace clang;
@@ -208,17 +209,39 @@
 {
   return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
 }
-unsigned clang_getCursorLine(CXCursor)
-{
-  return 0;
-}
-unsigned clang_getCursorColumn(CXCursor)
-{
-  return 0;
-}
-const char *clang_getCursorSource(CXCursor) 
+
+unsigned clang_getCursorLine(CXCursor C)
 {
-  return "";
+  assert(C.decl && "CXCursor has null decl");
+  NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
+  SourceLocation SLoc = ND->getLocation();
+  if (SLoc.isInvalid())
+    return 0;
+  SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
+  SLoc = SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
+  return SourceMgr.getSpellingLineNumber(SLoc);
+}
+unsigned clang_getCursorColumn(CXCursor C)
+{
+  assert(C.decl && "CXCursor has null decl");
+  NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
+  SourceLocation SLoc = ND->getLocation();
+  if (SLoc.isInvalid())
+    return 0;
+  SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
+  SLoc = SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
+  return SourceMgr.getSpellingColumnNumber(SLoc);
+}
+const char *clang_getCursorSource(CXCursor C) 
+{
+  assert(C.decl && "CXCursor has null decl");
+  NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
+  SourceLocation SLoc = ND->getLocation();
+  if (SLoc.isInvalid())
+    return "<invalid source location>";
+  SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
+  SLoc = SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
+  return SourceMgr.getBufferName(SLoc);
 }
 
 // If CXCursorKind == Cursor_Reference, then this will return the referenced declaration.

Modified: cfe/trunk/tools/c-index-test/c-index-test.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/c-index-test/c-index-test.c?rev=80585&r1=80584&r2=80585&view=diff

==============================================================================
--- cfe/trunk/tools/c-index-test/c-index-test.c (original)
+++ cfe/trunk/tools/c-index-test/c-index-test.c Mon Aug 31 09:26:51 2009
@@ -3,9 +3,13 @@
 #include <stdio.h>
 
 static void PrintDecls(CXTranslationUnit Unit, CXCursor Cursor) {
- if (clang_isDeclaration(Cursor.kind))
-   printf("%s => %s\n", clang_getKindSpelling(Cursor.kind),
-                        clang_getDeclSpelling(Cursor.decl));
+  if (clang_isDeclaration(Cursor.kind)) {
+    printf("%s => %s", clang_getKindSpelling(Cursor.kind),
+                       clang_getDeclSpelling(Cursor.decl));
+    printf(" (%s,%d:%d)\n", clang_getCursorSource(Cursor),
+                            clang_getCursorLine(Cursor),
+                            clang_getCursorColumn(Cursor));
+  }
 }
 
 /*





More information about the cfe-commits mailing list