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

Douglas Gregor dgregor at apple.com
Mon Jan 18 14:46:12 PST 2010


Author: dgregor
Date: Mon Jan 18 16:46:11 2010
New Revision: 93800

URL: http://llvm.org/viewvc/llvm-project?rev=93800&view=rev
Log:
Introduce clang_getCursorLocation(), which supercedes
clang_getCursorLine(), clang_getCursorColumn(),
clang_getCursorSource(), and clang_getCursorSourceFile(). Mark those 4
functions as deprecated and stop using them ourselves.

Modified:
    cfe/trunk/include/clang-c/Index.h
    cfe/trunk/tools/CIndex/CIndex.cpp
    cfe/trunk/tools/CIndex/CIndex.exports
    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=93800&r1=93799&r2=93800&view=diff

==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Mon Jan 18 16:46:11 2010
@@ -393,12 +393,26 @@
 
 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, 

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

==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndex.cpp Mon Jan 18 16:46:11 2010
@@ -113,6 +113,23 @@
 #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 @@
       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 @@
 
 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 @@
     }
     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 @@
     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 @@
 }
 
 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,

Modified: cfe/trunk/tools/CIndex/CIndex.exports
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndex.exports?rev=93800&r1=93799&r2=93800&view=diff

==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.exports (original)
+++ cfe/trunk/tools/CIndex/CIndex.exports Mon Jan 18 16:46:11 2010
@@ -18,6 +18,7 @@
 _clang_getCursorKind
 _clang_getCursorKindSpelling
 _clang_getCursorLine
+_clang_getCursorLocation
 _clang_getCursorSource
 _clang_getCursorSourceFile
 _clang_getCursorSpelling

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=93800&r1=93799&r2=93800&view=diff

==============================================================================
--- cfe/trunk/tools/c-index-test/c-index-test.c (original)
+++ cfe/trunk/tools/c-index-test/c-index-test.c Mon Jan 18 16:46:11 2010
@@ -61,7 +61,7 @@
 }
 
 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 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 @@
                                    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 @@
   curColumn = startColumn;
 
   while (startBuf < endBuf) {
+    CXSourceLocation Loc;
+    const char *source = 0;
+    
     if (*startBuf == '\n') {
       startBuf++;
       curLine++;
@@ -140,15 +144,18 @@
     } 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++;
   }





More information about the cfe-commits mailing list