Pull functions that translate from CXSourceLocation to SourceLocation (and back) to a separate header file.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@94462 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/tools/CIndex/CIndex.cpp b/tools/CIndex/CIndex.cpp
index 26c9273..fee5b74 100644
--- a/tools/CIndex/CIndex.cpp
+++ b/tools/CIndex/CIndex.cpp
@@ -14,6 +14,7 @@
 
 #include "CIndexer.h"
 #include "CXCursor.h"
+#include "CXSourceLocation.h"
 
 #include "clang/Basic/Version.h"
 #include "clang/AST/DeclVisitor.h"
@@ -116,34 +117,6 @@
 #endif
 #endif
 
-typedef llvm::PointerIntPair<ASTContext *, 1, bool> CXSourceLocationPtr;
-
-/// \brief Translate a Clang source location into a CIndex source location.
-static CXSourceLocation translateSourceLocation(ASTContext &Context,
-                                                SourceLocation Loc,
-                                                bool AtEnd = false) {
-  CXSourceLocationPtr Ptr(&Context, AtEnd);
-  CXSourceLocation Result = { Ptr.getOpaqueValue(), Loc.getRawEncoding() };
-  return Result;
-}
-
-/// \brief Translate a Clang source range into a CIndex source range.
-static CXSourceRange translateSourceRange(ASTContext &Context, SourceRange R) {
-  CXSourceRange Result = { &Context, 
-                           R.getBegin().getRawEncoding(),
-                           R.getEnd().getRawEncoding() };
-  return Result;
-}
-
-static SourceLocation translateSourceLocation(CXSourceLocation L) {
-  return SourceLocation::getFromRawEncoding(L.int_data);
-}
-
-static SourceRange translateSourceRange(CXSourceRange R) {
-  return SourceRange(SourceLocation::getFromRawEncoding(R.begin_int_data),
-                     SourceLocation::getFromRawEncoding(R.end_int_data));
-}
-
 /// \brief The result of comparing two source ranges.
 enum RangeComparisonResult {
   /// \brief Either the ranges overlap or one of the ranges is invalid.
@@ -315,7 +288,7 @@
 }
 
 RangeComparisonResult CursorVisitor::CompareRegionOfInterest(CXSourceRange CXR) {
-  return CompareRegionOfInterest(translateSourceRange(CXR));
+  return CompareRegionOfInterest(cxloc::translateSourceRange(CXR));
 }
 
 /// \brief Visit the given cursor and, if requested by the visitor,
@@ -346,7 +319,7 @@
   // we're done.
   if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) {
     CXSourceRange Range = clang_getCursorExtent(Cursor);
-    if (translateSourceRange(Range).isInvalid() || 
+    if (cxloc::translateSourceRange(Range).isInvalid() || 
         CompareRegionOfInterest(Range))
       return false;
   }
@@ -1122,7 +1095,7 @@
                                         static_cast<const FileEntry *>(file), 
                                               line, column);
   
-  return translateSourceLocation(CXXUnit->getASTContext(), SLoc, false);
+  return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc, false);
 }
 
 CXSourceRange clang_getRange(CXSourceLocation begin, CXSourceLocation end) {
@@ -1139,8 +1112,8 @@
                                     CXFile *file,
                                     unsigned *line,
                                     unsigned *column) {
-  CXSourceLocationPtr Ptr
-    = CXSourceLocationPtr::getFromOpaqueValue(location.ptr_data);
+  cxloc::CXSourceLocationPtr Ptr
+    = cxloc::CXSourceLocationPtr::getFromOpaqueValue(location.ptr_data);
   SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
 
   if (!Ptr.getPointer() || Loc.isInvalid()) {
@@ -1411,7 +1384,7 @@
   
   ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
 
-  SourceLocation SLoc = translateSourceLocation(Loc);
+  SourceLocation SLoc = cxloc::translateSourceLocation(Loc);
   CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound);
   if (SLoc.isValid()) {
     SourceRange RegionOfInterest(SLoc, 
@@ -1482,24 +1455,24 @@
     case CXCursor_ObjCSuperClassRef: {       
       std::pair<ObjCInterfaceDecl *, SourceLocation> P
         = getCursorObjCSuperClassRef(C);
-      return translateSourceLocation(P.first->getASTContext(), P.second);
+      return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
     }
 
     case CXCursor_ObjCProtocolRef: {       
       std::pair<ObjCProtocolDecl *, SourceLocation> P
         = getCursorObjCProtocolRef(C);
-      return translateSourceLocation(P.first->getASTContext(), P.second);
+      return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
     }
 
     case CXCursor_ObjCClassRef: {       
       std::pair<ObjCInterfaceDecl *, SourceLocation> P
         = getCursorObjCClassRef(C);
-      return translateSourceLocation(P.first->getASTContext(), P.second);
+      return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
     }
 
     case CXCursor_TypeRef: {       
       std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
-      return translateSourceLocation(P.first->getASTContext(), P.second);
+      return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
     }
       
     default:
@@ -1509,7 +1482,7 @@
   }
 
   if (clang_isExpression(C.kind))
-    return translateSourceLocation(getCursorContext(C), 
+    return cxloc::translateSourceLocation(getCursorContext(C), 
                                    getLocationFromExpr(getCursorExpr(C)));
 
   if (!getCursorDecl(C)) {
@@ -1521,7 +1494,7 @@
   SourceLocation Loc = D->getLocation();
   if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D))
     Loc = Class->getClassLoc();
-  return translateSourceLocation(D->getASTContext(), Loc);
+  return cxloc::translateSourceLocation(D->getASTContext(), Loc);
 }
 
 CXSourceRange clang_getCursorExtent(CXCursor C) {
@@ -1530,25 +1503,25 @@
       case CXCursor_ObjCSuperClassRef: {       
         std::pair<ObjCInterfaceDecl *, SourceLocation> P
           = getCursorObjCSuperClassRef(C);
-        return translateSourceRange(P.first->getASTContext(), P.second);
+        return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
       }
         
       case CXCursor_ObjCProtocolRef: {       
         std::pair<ObjCProtocolDecl *, SourceLocation> P
           = getCursorObjCProtocolRef(C);
-        return translateSourceRange(P.first->getASTContext(), P.second);
+        return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
       }
         
       case CXCursor_ObjCClassRef: {       
         std::pair<ObjCInterfaceDecl *, SourceLocation> P
           = getCursorObjCClassRef(C);
         
-        return translateSourceRange(P.first->getASTContext(), P.second);
+        return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
       }
 
       case CXCursor_TypeRef: {       
         std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
-        return translateSourceRange(P.first->getASTContext(), P.second);
+        return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
       }
         
       default:
@@ -1558,11 +1531,11 @@
   }
 
   if (clang_isExpression(C.kind))
-    return translateSourceRange(getCursorContext(C), 
+    return cxloc::translateSourceRange(getCursorContext(C), 
                                 getCursorExpr(C)->getSourceRange());
 
   if (clang_isStatement(C.kind))
-    return translateSourceRange(getCursorContext(C), 
+    return cxloc::translateSourceRange(getCursorContext(C), 
                                 getCursorStmt(C)->getSourceRange());
   
   if (!getCursorDecl(C)) {
@@ -1571,7 +1544,7 @@
   }
   
   Decl *D = getCursorDecl(C);
-  return translateSourceRange(D->getASTContext(), D->getSourceRange());
+  return cxloc::translateSourceRange(D->getASTContext(), D->getSourceRange());
 }
 
 CXCursor clang_getCursorReferenced(CXCursor C) {