]> granicus.if.org Git - clang/commitdiff
Add clang_getLocationForOffset() to libclang, for gives a source location from a...
authorDavid Chisnall <csdavec@swan.ac.uk>
Fri, 15 Oct 2010 17:07:39 +0000 (17:07 +0000)
committerDavid Chisnall <csdavec@swan.ac.uk>
Fri, 15 Oct 2010 17:07:39 +0000 (17:07 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@116587 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang-c/Index.h
tools/libclang/CIndex.cpp
tools/libclang/libclang.darwin.exports
tools/libclang/libclang.exports

index bf995682c63a0c000aaa82e64eb4734ed59e6e1d..40d3bafaff3d681892d972e57aa2e1ce6f9cc988 100644 (file)
@@ -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.
index 1d7efe42082fe1e241cba403ae6a42b519a17ebc..722e2cb73818ac32093b22f619d42cc7ca626419 100644 (file)
@@ -2266,6 +2266,27 @@ CXSourceLocation clang_getLocation(CXTranslationUnit tu,
     = CXXUnit->getSourceManager().getLocation(
                                         static_cast<const FileEntry *>(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<ASTUnit *>(tu);
+  SourceLocation Start 
+    = CXXUnit->getSourceManager().getLocation(
+                                        static_cast<const FileEntry *>(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);
 }
index c63ee4d5753c85c0b98dc68383af567e2d722e97..dbb335f9aa7e5e28b4e457f6db90620ff389db43 100644 (file)
@@ -71,6 +71,7 @@ _clang_getIBOutletCollectionType
 _clang_getInclusions
 _clang_getInstantiationLocation
 _clang_getLocation
+_clang_getLocationForOffset
 _clang_getNullCursor
 _clang_getNullLocation
 _clang_getNullRange
index 2af9a343025eb5bc9ddc0c7e3fffd3ba79a0ccad..06e5a8bf5172850e36f27bb421787f0c5588ec80 100644 (file)
@@ -71,6 +71,7 @@ clang_getIBOutletCollectionType
 clang_getInclusions
 clang_getInstantiationLocation
 clang_getLocation
+clang_getLocationForOffset
 clang_getNullCursor
 clang_getNullLocation
 clang_getNullRange