]> granicus.if.org Git - clang/commitdiff
Introduce clang_getCursorLocation(), which supercedes
authorDouglas Gregor <dgregor@apple.com>
Mon, 18 Jan 2010 22:46:11 +0000 (22:46 +0000)
committerDouglas Gregor <dgregor@apple.com>
Mon, 18 Jan 2010 22:46:11 +0000 (22:46 +0000)
clang_getCursorLine(), clang_getCursorColumn(),
clang_getCursorSource(), and clang_getCursorSourceFile(). Mark those 4
functions as deprecated and stop using them ourselves.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@93800 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 380dec7796669e8cbc53ce63e23e36054a981193..7f5d6683032c473eb80e28367cc447161e4f7a4c 100644 (file)
@@ -393,12 +393,26 @@ CINDEX_LINKAGE unsigned clang_isInvalid(enum CXCursorKind);
 
 CINDEX_LINKAGE unsigned clang_equalCursors(CXCursor, CXCursor);
 
-CINDEX_LINKAGE unsigned clang_getCursorLine(CXCursor);
-CINDEX_LINKAGE unsigned clang_getCursorColumn(CXCursor);
 CINDEX_LINKAGE CXString clang_getCursorSpelling(CXCursor);
+
+CINDEX_LINKAGE unsigned clang_getCursorLine(CXCursor); /* deprecate */
+CINDEX_LINKAGE unsigned clang_getCursorColumn(CXCursor); /* deprecate */
 CINDEX_LINKAGE const char *clang_getCursorSource(CXCursor); /* deprecate */
-CINDEX_LINKAGE CXFile clang_getCursorSourceFile(CXCursor);
+CINDEX_LINKAGE CXFile clang_getCursorSourceFile(CXCursor); /* deprecate */
 
+/**
+ * \brief Retrieve the physical location of the source constructor referenced
+ * by the given cursor.
+ *
+ * The location of a declaration is typically the location of the name of that
+ * declaration, where the name of that declaration would occur if it is 
+ * unnamed, or some keyword that introduces that particular declaration. 
+ * The location of a reference is where that reference occurs within the 
+ * source code.
+ */
+CINDEX_LINKAGE CXSourceLocation clang_getCursorLocation(CXCursor);
+    
+  
 /* for debug/testing */
 CINDEX_LINKAGE const char *clang_getCursorKindSpelling(enum CXCursorKind Kind); 
 CINDEX_LINKAGE void clang_getDefinitionSpellingAndExtent(CXCursor, 
index a11f54b8793e59960b86848bd6d74c1e4306d34d..6361174b222f4fedf79d2e7d8d8bb58209190a3c 100644 (file)
@@ -113,6 +113,23 @@ public:
 #endif
 #endif
 
+/// \brief Translate a Clang source location into a CIndex source location.
+static CXSourceLocation translateSourceLocation(SourceManager &SourceMgr,
+                                                SourceLocation Loc) {
+  SourceLocation InstLoc = SourceMgr.getInstantiationLoc(Loc);
+  if (InstLoc.isInvalid()) {
+      CXSourceLocation Loc = { 0, 0, 0 };
+      return Loc;
+    }
+  CXSourceLocation Result;
+  Result.file 
+    = (void*)SourceMgr.getFileEntryForID(SourceMgr.getFileID(InstLoc));
+  Result.line = SourceMgr.getInstantiationLineNumber(InstLoc);
+  Result.column = SourceMgr.getInstantiationColumnNumber(InstLoc);
+  return Result;
+}
+
 //===----------------------------------------------------------------------===//
 // Visitors.
 //===----------------------------------------------------------------------===//
@@ -350,14 +367,14 @@ static SourceLocation getLocationFromCursor(CXCursor C,
       return getCursorObjCProtocolRef(C).second;
     case CXCursor_ObjCSelectorRef: {
       ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(getCursorStmt(C));
-      assert(OME && "clang_getCursorLine(): Missing message expr");
+      assert(OME && "getLocationFromCursor(): Missing message expr");
       return OME->getLeftLoc(); /* FIXME: should be a range */
     }
     case CXCursor_VarRef:
     case CXCursor_FunctionRef:
     case CXCursor_EnumConstantRef: {
       DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(getCursorStmt(C));
-      assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
+      assert(DRE && "getLocationFromCursor(): Missing decl ref expr");
       return DRE->getLocation();
     }
     default:
@@ -719,12 +736,18 @@ CXFile clang_getDeclSourceFile(CXDecl AnonDecl) {
 
 extern "C" {
 const char *clang_getFileName(CXFile SFile) {
+  if (!SFile)
+    return 0;
+  
   assert(SFile && "Passed null CXFile");
   FileEntry *FEnt = static_cast<FileEntry *>(SFile);
   return FEnt->getName();
 }
 
 time_t clang_getFileTime(CXFile SFile) {
+  if (!SFile)
+    return 0;
+  
   assert(SFile && "Passed null CXFile");
   FileEntry *FEnt = static_cast<FileEntry *>(SFile);
   return FEnt->getModificationTime();
@@ -768,12 +791,12 @@ CXString clang_getCursorSpelling(CXCursor C) {
     }
     case CXCursor_ObjCProtocolRef: {
       ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first;
-      assert(OID && "clang_getCursorLine(): Missing protocol decl");
+      assert(OID && "getLocationFromCursor(): Missing protocol decl");
       return CIndexer::createCXString(OID->getIdentifier()->getNameStart());
     }
     case CXCursor_ObjCSelectorRef: {
       ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(getCursorStmt(C));
-      assert(OME && "clang_getCursorLine(): Missing message expr");
+      assert(OME && "getLocationFromCursor(): Missing message expr");
       return CIndexer::createCXString(OME->getSelector().getAsString().c_str(),
                                       true);
     }
@@ -781,7 +804,7 @@ CXString clang_getCursorSpelling(CXCursor C) {
     case CXCursor_FunctionRef:
     case CXCursor_EnumConstantRef: {
       DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(getCursorStmt(C));
-      assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
+      assert(DRE && "getLocationFromCursor(): Missing decl ref expr");
       return CIndexer::createCXString(DRE->getDecl()->getIdentifier()
                                       ->getNameStart());
     }
@@ -928,56 +951,38 @@ CXDecl clang_getCursorDecl(CXCursor C) {
 }
 
 unsigned clang_getCursorLine(CXCursor C) {
-  assert(getCursorDecl(C) && "CXCursor has null decl");
-  NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
-  SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
-
-  SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
-  return SourceMgr.getSpellingLineNumber(SLoc);
+  return clang_getCursorLocation(C).line;
 }
   
 unsigned clang_getCursorColumn(CXCursor C) {
-  assert(getCursorDecl(C) && "CXCursor has null decl");
-  NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
-  SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
-  
-  SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
-  return SourceMgr.getSpellingColumnNumber(SLoc);
+  return clang_getCursorLocation(C).column;
 }
 
 const char *clang_getCursorSource(CXCursor C) {
-  assert(getCursorDecl(C) && "CXCursor has null decl");
-  NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
-  SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
-  
-  SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
-  
-  if (SLoc.isFileID()) {
-    const char *bufferName = SourceMgr.getBufferName(SLoc);
-    return bufferName[0] == '<' ? NULL : bufferName;
-  }
-  
-  // Retrieve the file in which the macro was instantiated, then provide that
-  // buffer name.
-  // FIXME: Do we want to give specific macro-instantiation information?
-  const llvm::MemoryBuffer *Buffer
-  = SourceMgr.getBuffer(SourceMgr.getDecomposedSpellingLoc(SLoc).first);
-  if (!Buffer)
-    return 0;
-  
-  return Buffer->getBufferIdentifier();
+  return clang_getFileName(clang_getCursorLocation(C).file);
 }
 
 CXFile clang_getCursorSourceFile(CXCursor C) {
-  assert(getCursorDecl(C) && "CXCursor has null decl");
+  return clang_getCursorLocation(C).file;
+}
+
+CXSourceLocation clang_getCursorLocation(CXCursor C) {
+  if (clang_isReference(C.kind)) {
+    // FIXME: Return the location of the reference, not of the underlying
+    // declaration (which may not even exist!).
+  }
+  
+  if (!getCursorDecl(C)) {
+    CXSourceLocation empty = { 0, 0, 0 };
+    return empty;
+  }
+
   NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
-  SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
+  SourceManager &SM = ND->getASTContext().getSourceManager();
   
-  return (void *)
-  getFileEntryFromSourceLocation(SourceMgr, getLocationFromCursor(C,SourceMgr,
-                                                                  ND));
+  return translateSourceLocation(SM, getLocationFromCursor(C, SM, ND));
 }
-
+  
 void clang_getDefinitionSpellingAndExtent(CXCursor C,
                                           const char **startBuf,
                                           const char **endBuf,
index d5f8298997beb2a3f73e75dea5d1286ba6e5ad38..48cbd2668953b0f46ec6ba609669399b17833309 100644 (file)
@@ -18,6 +18,7 @@ _clang_getCursorFromDecl
 _clang_getCursorKind
 _clang_getCursorKindSpelling
 _clang_getCursorLine
+_clang_getCursorLocation
 _clang_getCursorSource
 _clang_getCursorSourceFile
 _clang_getCursorSpelling
index e3d6ad88bb8faa5353ddf07d52a3d203eba3bd2a..a2d4fe980814c182c11a0cf4a0b1b34f0962c1ec 100644 (file)
@@ -61,7 +61,7 @@ static void PrintCursor(CXCursor Cursor) {
 }
 
 static const char* GetCursorSource(CXCursor Cursor) {  
-  const char *source = clang_getCursorSource(Cursor);
+  const char *source = clang_getFileName(clang_getCursorLocation(Cursor).file);
   if (!source)
     return "<invalid loc>";  
   return basename(source);
@@ -84,10 +84,11 @@ static void PrintDeclExtent(CXDecl Dcl) {
 
 static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter) {
   if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
-    printf("// %s: %s:%d:%d: ", FileCheckPrefix,
-                                GetCursorSource(Cursor),
-                                clang_getCursorLine(Cursor),
-                                clang_getCursorColumn(Cursor));
+    CXSourceLocation Loc = clang_getCursorLocation(Cursor);
+    const char *source = clang_getFileName(Loc.file);
+    if (!source)
+      source = "<invalid loc>";  
+    printf("// %s: %s:%d:%d: ", FileCheckPrefix, source, Loc.line, Loc.column);
     PrintCursor(Cursor);    
     PrintDeclExtent(clang_getCursorDecl(Cursor));
 
@@ -99,9 +100,9 @@ static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor,
                                    CXClientData Filter) {
   if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
     CXDecl D;
+    CXSourceLocation Loc = clang_getCursorLocation(Cursor);
     printf("// %s: %s:%d:%d: ", FileCheckPrefix,
-           GetCursorSource(Cursor), clang_getCursorLine(Cursor),
-           clang_getCursorColumn(Cursor));
+           GetCursorSource(Cursor), Loc.line, Loc.column);
     PrintCursor(Cursor);
     
     D = clang_getCursorDecl(Cursor);
@@ -133,6 +134,9 @@ static void FunctionScanVisitor(CXTranslationUnit Unit, CXCursor Cursor,
   curColumn = startColumn;
 
   while (startBuf < endBuf) {
+    CXSourceLocation Loc;
+    const char *source = 0;
+    
     if (*startBuf == '\n') {
       startBuf++;
       curLine++;
@@ -140,15 +144,18 @@ static void FunctionScanVisitor(CXTranslationUnit Unit, CXCursor Cursor,
     } else if (*startBuf != '\t')
       curColumn++;
           
-    Ref = clang_getCursor(Unit, clang_getCursorSource(Cursor),
-                          curLine, curColumn);
-    if (Ref.kind == CXCursor_NoDeclFound) {
-      /* Nothing found here; that's fine. */
-    } else if (Ref.kind != CXCursor_FunctionDecl) {
-      printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
-             curLine, curColumn);
-      PrintCursor(Ref);
-      printf("\n");
+    Loc = clang_getCursorLocation(Cursor);
+    source = clang_getFileName(Loc.file);
+    if (source) {
+      Ref = clang_getCursor(Unit, source, curLine, curColumn);
+      if (Ref.kind == CXCursor_NoDeclFound) {
+        /* Nothing found here; that's fine. */
+      } else if (Ref.kind != CXCursor_FunctionDecl) {
+        printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
+               curLine, curColumn);
+        PrintCursor(Ref);
+        printf("\n");
+      }
     }
     startBuf++;
   }