[cfe-dev] libclang source location from buffer index
David Chisnall
csdavec at swan.ac.uk
Fri Oct 15 06:05:28 PDT 2010
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?
- Is this implementation sane?
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;
More information about the cfe-dev
mailing list