]> granicus.if.org Git - clang/commitdiff
Introduce clang_getInstantiationLocationOffset(), which decomposes a
authorDouglas Gregor <dgregor@apple.com>
Tue, 26 Jan 2010 03:07:15 +0000 (03:07 +0000)
committerDouglas Gregor <dgregor@apple.com>
Tue, 26 Jan 2010 03:07:15 +0000 (03:07 +0000)
source location in file + offset.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@94497 91177308-0d34-0410-b5e6-96231b3b80d8

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

index b38e51785122bd8ca1a26a03a749fd20ab2e5200..ab7e55bcb61e19159fa11450805e52d3738f4057 100644 (file)
@@ -372,6 +372,24 @@ CINDEX_LINKAGE void clang_getInstantiationLocation(CXSourceLocation location,
                                                    unsigned *line,
                                                    unsigned *column);
 
+/**
+ * \brief Retrieve the file and offset within that file represented by
+ * the given source location.
+ *
+ * \param location the location within a source file that will be decomposed
+ * into its parts.
+ *
+ * \param file [out] if non-NULL, will be set to the file to which the
+ * given source location points.
+ *
+ * \param offset [out] if non-NULL, will be set to the offset into the
+ * \p file to which the given source location points.
+ */
+CINDEX_LINKAGE void clang_getInstantiationLocationOffset(
+                                                     CXSourceLocation location,
+                                                     CXFile *File,
+                                                     unsigned *Offset);
+
 /**
  * \brief Retrieve a source location representing the first character within a
  * source range.
index fee5b74b49c7016596feff9d0e3c5374e7bcbd19..03519adc0a363f8cd0fbccefea952f46a12a9191 100644 (file)
@@ -1108,24 +1108,11 @@ CXSourceRange clang_getRange(CXSourceLocation begin, CXSourceLocation end) {
   return Result;
 }
 
-void clang_getInstantiationLocation(CXSourceLocation location,
-                                    CXFile *file,
-                                    unsigned *line,
-                                    unsigned *column) {
+static SourceLocation getAdjustedSourceLocation(CXSourceLocation location) {
   cxloc::CXSourceLocationPtr Ptr
     = cxloc::CXSourceLocationPtr::getFromOpaqueValue(location.ptr_data);
   SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
 
-  if (!Ptr.getPointer() || Loc.isInvalid()) {
-    if (file)
-      *file = 0;
-    if (line)
-      *line = 0;
-    if (column)
-      *column = 0;
-    return;
-  }
-
   // FIXME: This is largely copy-paste from
   ///TextDiagnosticPrinter::HighlightRange.  When it is clear that this is
   // what we want the two routines should be refactored.  
@@ -1157,6 +1144,30 @@ void clang_getInstantiationLocation(CXSourceLocation location,
       InstLoc = InstLoc.getFileLocWithOffset(Length - 1);
   }
 
+  return InstLoc;
+}
+
+void clang_getInstantiationLocation(CXSourceLocation location,
+                                    CXFile *file,
+                                    unsigned *line,
+                                    unsigned *column) {
+  cxloc::CXSourceLocationPtr Ptr
+    = cxloc::CXSourceLocationPtr::getFromOpaqueValue(location.ptr_data);
+  SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
+
+  if (!Ptr.getPointer() || Loc.isInvalid()) {
+    if (file)
+      *file = 0;
+    if (line)
+      *line = 0;
+    if (column)
+      *column = 0;
+    return;
+  }
+
+  SourceLocation InstLoc = getAdjustedSourceLocation(location);
+  ASTContext &Context = *Ptr.getPointer();
+  SourceManager &SM = Context.getSourceManager();
   if (file)
     *file = (void *)SM.getFileEntryForID(SM.getFileID(InstLoc));
   if (line)
@@ -1165,6 +1176,23 @@ void clang_getInstantiationLocation(CXSourceLocation location,
     *column = SM.getInstantiationColumnNumber(InstLoc);
 }
 
+void clang_getInstantiationLocationOffset(CXSourceLocation location,
+                                          CXFile *file,
+                                          unsigned *offset) {
+  cxloc::CXSourceLocationPtr Ptr
+    = cxloc::CXSourceLocationPtr::getFromOpaqueValue(location.ptr_data);
+  SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
+
+  ASTContext &Context = *Ptr.getPointer();
+  SourceManager &SM = Context.getSourceManager();
+  SourceLocation InstLoc = getAdjustedSourceLocation(location);
+  std::pair<FileID, unsigned> Decomposed = SM.getDecomposedLoc(InstLoc);
+  if (file)
+    *file = (void *)SM.getFileEntryForID(Decomposed.first);
+  if (offset)
+    *offset = Decomposed.second;
+}
+
 CXSourceLocation clang_getRangeStart(CXSourceRange range) {
   CXSourceLocation Result = { range.ptr_data, range.begin_int_data };
   return Result;
index d349086b9c118e833d9f5b97853793b88a498876..b2ec58e5b9cd0c50f2090f1b61b2f269c30e65ab 100644 (file)
@@ -27,6 +27,7 @@ _clang_getFile
 _clang_getFileName
 _clang_getFileTime
 _clang_getInstantiationLocation
+_clang_getInstantiationLocationOffset
 _clang_getLocation
 _clang_getNullCursor
 _clang_getNullLocation