CXSourceLocation.h revision f51f20fa34654da75d15a9e2a1a0cd2fc0d8603d
1//===- CXSourceLocation.h - CXSourceLocations Utilities ---------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines routines for manipulating CXSourceLocations. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CLANG_CXSOURCELOCATION_H 15#define LLVM_CLANG_CXSOURCELOCATION_H 16 17#include "clang-c/Index.h" 18#include "clang/Basic/SourceLocation.h" 19#include "clang/Basic/LangOptions.h" 20#include "clang/AST/ASTContext.h" 21 22namespace clang { 23 24class SourceManager; 25 26namespace cxloc { 27 28/// \brief Translate a Clang source location into a CIndex source location. 29static inline CXSourceLocation 30translateSourceLocation(const SourceManager &SM, const LangOptions &LangOpts, 31 SourceLocation Loc) { 32 CXSourceLocation Result = { { (void*) &SM, (void*) &LangOpts, }, 33 Loc.getRawEncoding() }; 34 return Result; 35} 36 37/// \brief Translate a Clang source location into a CIndex source location. 38static inline CXSourceLocation translateSourceLocation(ASTContext &Context, 39 SourceLocation Loc) { 40 return translateSourceLocation(Context.getSourceManager(), 41 Context.getLangOptions(), 42 Loc); 43} 44 45/// \brief Translate a Clang source range into a CIndex source range. 46/// 47/// Clang internally represents ranges where the end location points to the 48/// start of the token at the end. However, for external clients it is more 49/// useful to have a CXSourceRange be a proper half-open interval. This routine 50/// does the appropriate translation. 51CXSourceRange translateSourceRange(const SourceManager &SM, 52 const LangOptions &LangOpts, 53 SourceRange R); 54 55/// \brief Translate a Clang source range into a CIndex source range. 56static inline CXSourceRange translateSourceRange(ASTContext &Context, 57 SourceRange R) { 58 return translateSourceRange(Context.getSourceManager(), 59 Context.getLangOptions(), 60 R); 61} 62 63static inline SourceLocation translateSourceLocation(CXSourceLocation L) { 64 return SourceLocation::getFromRawEncoding(L.int_data); 65} 66 67static inline SourceRange translateCXSourceRange(CXSourceRange R) { 68 return SourceRange(SourceLocation::getFromRawEncoding(R.begin_int_data), 69 SourceLocation::getFromRawEncoding(R.end_int_data)); 70} 71 72 73}} // end namespace: clang::cxloc 74 75#endif 76