From: David Chisnall Date: Fri, 15 Oct 2010 17:07:39 +0000 (+0000) Subject: Add clang_getLocationForOffset() to libclang, for gives a source location from a... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=83889a7f1f338e343ef72aeeef9c27f7b62c0f0f;p=clang Add clang_getLocationForOffset() to libclang, for gives a source location from a character index into a file. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@116587 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang-c/Index.h b/include/clang-c/Index.h index bf995682c6..40d3bafaff 100644 --- a/include/clang-c/Index.h +++ b/include/clang-c/Index.h @@ -301,6 +301,13 @@ CINDEX_LINKAGE CXSourceLocation clang_getLocation(CXTranslationUnit tu, CXFile file, unsigned line, unsigned column); +/** + * \brief Retrieves the source location associated with a given character offset + * in a particular translation unit. + */ +CINDEX_LINKAGE CXSourceLocation clang_getLocationForOffset(CXTranslationUnit tu, + CXFile file, + unsigned offset); /** * \brief Retrieve a NULL (invalid) source range. diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp index 1d7efe4208..722e2cb738 100644 --- a/tools/libclang/CIndex.cpp +++ b/tools/libclang/CIndex.cpp @@ -2266,6 +2266,27 @@ CXSourceLocation clang_getLocation(CXTranslationUnit tu, = CXXUnit->getSourceManager().getLocation( static_cast(file), line, column); + if (SLoc.isInvalid()) return clang_getNullLocation(); + + return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc); +} + +CXSourceLocation clang_getLocationForOffset(CXTranslationUnit tu, + CXFile file, + unsigned offset) { + if (!tu || !file) + return clang_getNullLocation(); + + ASTUnit *CXXUnit = static_cast(tu); + SourceLocation Start + = CXXUnit->getSourceManager().getLocation( + static_cast(file), + 1, 1); + if (Start.isInvalid()) return clang_getNullLocation(); + + SourceLocation SLoc = Start.getFileLocWithOffset(offset); + + if (SLoc.isInvalid()) return clang_getNullLocation(); return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc); } diff --git a/tools/libclang/libclang.darwin.exports b/tools/libclang/libclang.darwin.exports index c63ee4d575..dbb335f9aa 100644 --- a/tools/libclang/libclang.darwin.exports +++ b/tools/libclang/libclang.darwin.exports @@ -71,6 +71,7 @@ _clang_getIBOutletCollectionType _clang_getInclusions _clang_getInstantiationLocation _clang_getLocation +_clang_getLocationForOffset _clang_getNullCursor _clang_getNullLocation _clang_getNullRange diff --git a/tools/libclang/libclang.exports b/tools/libclang/libclang.exports index 2af9a34302..06e5a8bf51 100644 --- a/tools/libclang/libclang.exports +++ b/tools/libclang/libclang.exports @@ -71,6 +71,7 @@ clang_getIBOutletCollectionType clang_getInclusions clang_getInstantiationLocation clang_getLocation +clang_getLocationForOffset clang_getNullCursor clang_getNullLocation clang_getNullRange