]> granicus.if.org Git - clang/commitdiff
Implement source/line/column hooks.
authorSteve Naroff <snaroff@apple.com>
Mon, 31 Aug 2009 14:26:51 +0000 (14:26 +0000)
committerSteve Naroff <snaroff@apple.com>
Mon, 31 Aug 2009 14:26:51 +0000 (14:26 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@80585 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang-c/Index.h
tools/CIndex/CIndex.cpp
tools/c-index-test/c-index-test.c

index 2af3fee77445b98651083085dced545b2453e2ff..de2b5d68987bc087daa0021b92256dd751fedf54 100644 (file)
@@ -25,7 +25,7 @@ extern "C" {
    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.
index 9ef51b9db9fa4c415a000d7b3664d170e4935eec..f8d223a51092b132be2e777814a1359872e50acb 100644 (file)
@@ -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 @@ unsigned clang_isDeclaration(enum CXCursorKind K)
 {
   return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
 }
-unsigned clang_getCursorLine(CXCursor)
-{
-  return 0;
-}
-unsigned clang_getCursorColumn(CXCursor)
-{
-  return 0;
-}
-const char *clang_getCursorSource(CXCursor) 
-{
-  return "";
+
+unsigned clang_getCursorLine(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.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.
index 3126b319efdad17c9a44484bb6cb346cfb9f1eec..1ef8e924bc3923925f7b00b0867f1b228eef57c2 100644 (file)
@@ -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));
+  }
 }
 
 /*