From: Argyrios Kyrtzidis Date: Fri, 4 Jan 2013 18:30:13 +0000 (+0000) Subject: [libclang] Introduce clang_getFileLocation. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2d5c133d3dd95507db63147997109f06e8cfa833;p=clang [libclang] Introduce clang_getFileLocation. Uses of clang_getSpellingLocation should eventually move to calling clang_getFileLocation, and clang_getSpellingLocation should do what its name represents and actually point at the 'spelling' location, e.g. inside a macro definition if the spelling of a token came from that. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@171486 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang-c/Index.h b/include/clang-c/Index.h index 94c896ec6e..011588efc8 100644 --- a/include/clang-c/Index.h +++ b/include/clang-c/Index.h @@ -32,7 +32,7 @@ * compatible, thus CINDEX_VERSION_MAJOR is expected to remain stable. */ #define CINDEX_VERSION_MAJOR 0 -#define CINDEX_VERSION_MINOR 9 +#define CINDEX_VERSION_MINOR 10 #define CINDEX_VERSION_ENCODE(major, minor) ( \ ((major) * 10000) \ @@ -530,6 +530,35 @@ CINDEX_LINKAGE void clang_getSpellingLocation(CXSourceLocation location, unsigned *column, unsigned *offset); +/** + * \brief Retrieve the file, line, column, and offset represented by + * the given source location. + * + * If the location refers into a macro expansion, return where the macro was + * expanded or where the macro argument was written, if the location points at + * a macro argument. + * + * \param location the location within a source file that will be decomposed + * into its parts. + * + * \param file [out] if non-NULL, will be set to the file to which the given + * source location points. + * + * \param line [out] if non-NULL, will be set to the line to which the given + * source location points. + * + * \param column [out] if non-NULL, will be set to the column to which the given + * source location points. + * + * \param offset [out] if non-NULL, will be set to the offset into the + * buffer to which the given source location points. + */ +CINDEX_LINKAGE void clang_getFileLocation(CXSourceLocation location, + CXFile *file, + unsigned *line, + unsigned *column, + unsigned *offset); + /** * \brief Retrieve a source location representing the first character within a * source range. diff --git a/tools/libclang/CXSourceLocation.cpp b/tools/libclang/CXSourceLocation.cpp index 5d49f42a4a..8d88a116e0 100644 --- a/tools/libclang/CXSourceLocation.cpp +++ b/tools/libclang/CXSourceLocation.cpp @@ -294,6 +294,7 @@ void clang_getSpellingLocation(CXSourceLocation location, const SourceManager &SM = *static_cast(location.ptr_data[0]); + // FIXME: This should call SourceManager::getSpellingLoc(). SourceLocation SpellLoc = SM.getFileLoc(Loc); std::pair LocInfo = SM.getDecomposedLoc(SpellLoc); FileID FID = LocInfo.first; @@ -312,5 +313,41 @@ void clang_getSpellingLocation(CXSourceLocation location, *offset = FileOffset; } -} // end extern "C" +void clang_getFileLocation(CXSourceLocation location, + CXFile *file, + unsigned *line, + unsigned *column, + unsigned *offset) { + + if (!isASTUnitSourceLocation(location)) { + CXLoadedDiagnostic::decodeLocation(location, file, line, + column, offset); + return; + } + + SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); + + if (!location.ptr_data[0] || Loc.isInvalid()) + return createNullLocation(file, line, column, offset); + const SourceManager &SM = + *static_cast(location.ptr_data[0]); + SourceLocation FileLoc = SM.getFileLoc(Loc); + std::pair LocInfo = SM.getDecomposedLoc(FileLoc); + FileID FID = LocInfo.first; + unsigned FileOffset = LocInfo.second; + + if (FID.isInvalid()) + return createNullLocation(file, line, column, offset); + + if (file) + *file = (void *)SM.getFileEntryForID(FID); + if (line) + *line = SM.getLineNumber(FID, FileOffset); + if (column) + *column = SM.getColumnNumber(FID, FileOffset); + if (offset) + *offset = FileOffset; +} + +} // end extern "C" diff --git a/tools/libclang/libclang.exports b/tools/libclang/libclang.exports index 68bfb71f18..8f3c86318c 100644 --- a/tools/libclang/libclang.exports +++ b/tools/libclang/libclang.exports @@ -163,6 +163,7 @@ clang_getEnumDeclIntegerType clang_getFieldDeclBitWidth clang_getExpansionLocation clang_getFile +clang_getFileLocation clang_getFileName clang_getFileTime clang_getFunctionTypeCallingConv