[cfe-dev] libclang source location from buffer index
Douglas Gregor
dgregor at apple.com
Fri Oct 15 10:01:40 PDT 2010
On Oct 15, 2010, at 6:05 AM, David Chisnall wrote:
> Hi,
>
> When interacting with the libclang stuff for an in-memory file, it seems quite common not to have the line and column indexes. I've added a function for generating locations from simple offsets into a file, but I'm not overly familiar with the SourceLocation stuff, so it might not be the most sensible way of doing it. I have two questions:
>
> - Does anyone object to this interface?
Nope, it looks good. The patch needs updates to clang-c/Index.h and the exports file, of course :)
> - Is this implementation sane?
Yes, but please check for offset >= the size of the file and that Start is not invalid, so that this routine returns clang_getNullLocation() rather than a potentially-invalid location. libclang needs to be resilient to API use errors.
- Doug
> David
>
> -- Sent from my Cray X1
>
> Index: CIndex.cpp
> ===================================================================
> --- CIndex.cpp (revision 116439)
> +++ CIndex.cpp (working copy)
> @@ -2270,6 +2270,22 @@
> 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<ASTUnit *>(tu);
> + SourceLocation Start
> + = CXXUnit->getSourceManager().getLocation(
> + static_cast<const FileEntry *>(file),
> + 1, 1);
> + SourceLocation SLoc = Start.getFileLocWithOffset(offset);
> +
> + return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc);
> +}
> +
> CXSourceRange clang_getNullRange() {
> CXSourceRange Result = { { 0, 0 }, 0, 0 };
> return Result;
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
More information about the cfe-dev
mailing list