From: Douglas Gregor Date: Mon, 18 Jan 2010 22:46:11 +0000 (+0000) Subject: Introduce clang_getCursorLocation(), which supercedes X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=98258afae66bab39b0c57a3efb6b20d4fbb5746c;p=clang 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@93800 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang-c/Index.h b/include/clang-c/Index.h index 380dec7796..7f5d668303 100644 --- a/include/clang-c/Index.h +++ b/include/clang-c/Index.h @@ -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, diff --git a/tools/CIndex/CIndex.cpp b/tools/CIndex/CIndex.cpp index a11f54b879..6361174b22 100644 --- a/tools/CIndex/CIndex.cpp +++ b/tools/CIndex/CIndex.cpp @@ -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(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(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(SFile); return FEnt->getName(); } time_t clang_getFileTime(CXFile SFile) { + if (!SFile) + return 0; + assert(SFile && "Passed null CXFile"); FileEntry *FEnt = static_cast(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(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(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(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(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(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(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, diff --git a/tools/CIndex/CIndex.exports b/tools/CIndex/CIndex.exports index d5f8298997..48cbd26689 100644 --- a/tools/CIndex/CIndex.exports +++ b/tools/CIndex/CIndex.exports @@ -18,6 +18,7 @@ _clang_getCursorFromDecl _clang_getCursorKind _clang_getCursorKindSpelling _clang_getCursorLine +_clang_getCursorLocation _clang_getCursorSource _clang_getCursorSourceFile _clang_getCursorSpelling diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c index e3d6ad88bb..a2d4fe9808 100644 --- a/tools/c-index-test/c-index-test.c +++ b/tools/c-index-test/c-index-test.c @@ -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 ""; 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 = ""; + 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++; }