[cfe-commits] r116587 - in /cfe/trunk: include/clang-c/Index.h tools/libclang/CIndex.cpp tools/libclang/libclang.darwin.exports tools/libclang/libclang.exports

David Chisnall csdavec at swan.ac.uk
Fri Oct 15 10:07:39 PDT 2010


Author: theraven
Date: Fri Oct 15 12:07:39 2010
New Revision: 116587

URL: http://llvm.org/viewvc/llvm-project?rev=116587&view=rev
Log:
Add clang_getLocationForOffset() to libclang, for gives a source location from a character index into a file.


Modified:
    cfe/trunk/include/clang-c/Index.h
    cfe/trunk/tools/libclang/CIndex.cpp
    cfe/trunk/tools/libclang/libclang.darwin.exports
    cfe/trunk/tools/libclang/libclang.exports

Modified: cfe/trunk/include/clang-c/Index.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=116587&r1=116586&r2=116587&view=diff
==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Fri Oct 15 12:07:39 2010
@@ -301,6 +301,13 @@
                                                   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.

Modified: cfe/trunk/tools/libclang/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndex.cpp?rev=116587&r1=116586&r2=116587&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndex.cpp (original)
+++ cfe/trunk/tools/libclang/CIndex.cpp Fri Oct 15 12:07:39 2010
@@ -2266,6 +2266,27 @@
     = 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);
 }

Modified: cfe/trunk/tools/libclang/libclang.darwin.exports
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/libclang.darwin.exports?rev=116587&r1=116586&r2=116587&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/libclang.darwin.exports (original)
+++ cfe/trunk/tools/libclang/libclang.darwin.exports Fri Oct 15 12:07:39 2010
@@ -71,6 +71,7 @@
 _clang_getInclusions
 _clang_getInstantiationLocation
 _clang_getLocation
+_clang_getLocationForOffset
 _clang_getNullCursor
 _clang_getNullLocation
 _clang_getNullRange

Modified: cfe/trunk/tools/libclang/libclang.exports
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/libclang.exports?rev=116587&r1=116586&r2=116587&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/libclang.exports (original)
+++ cfe/trunk/tools/libclang/libclang.exports Fri Oct 15 12:07:39 2010
@@ -71,6 +71,7 @@
 clang_getInclusions
 clang_getInstantiationLocation
 clang_getLocation
+clang_getLocationForOffset
 clang_getNullCursor
 clang_getNullLocation
 clang_getNullRange





More information about the cfe-commits mailing list