82 lines · c
1//===- CXSourceLocation.h - CXSourceLocations Utilities ---------*- C++ -*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// This file defines routines for manipulating CXSourceLocations.10//11//===----------------------------------------------------------------------===//12 13#ifndef LLVM_CLANG_TOOLS_LIBCLANG_CXSOURCELOCATION_H14#define LLVM_CLANG_TOOLS_LIBCLANG_CXSOURCELOCATION_H15 16#include "clang-c/Index.h"17#include "clang/AST/ASTContext.h"18#include "clang/Basic/LangOptions.h"19#include "clang/Basic/SourceLocation.h"20 21namespace clang {22 23class SourceManager;24 25namespace cxloc {26 27/// Translate a Clang source location into a CIndex source location.28static inline CXSourceLocation 29translateSourceLocation(const SourceManager &SM, const LangOptions &LangOpts,30 SourceLocation Loc) {31 if (Loc.isInvalid())32 return clang_getNullLocation();33 34 CXSourceLocation Result = { { &SM, &LangOpts, },35 Loc.getRawEncoding() };36 return Result;37}38 39/// Translate a Clang source location into a CIndex source location.40static inline CXSourceLocation translateSourceLocation(ASTContext &Context,41 SourceLocation Loc) {42 return translateSourceLocation(Context.getSourceManager(),43 Context.getLangOpts(),44 Loc);45}46 47/// Translate a Clang source range into a CIndex source range.48///49/// Clang internally represents ranges where the end location points to the50/// start of the token at the end. However, for external clients it is more51/// useful to have a CXSourceRange be a proper half-open interval. This routine52/// does the appropriate translation.53CXSourceRange translateSourceRange(const SourceManager &SM, 54 const LangOptions &LangOpts,55 const CharSourceRange &R);56 57/// Translate a Clang source range into a CIndex source range.58static inline CXSourceRange translateSourceRange(ASTContext &Context,59 SourceRange R) {60 return translateSourceRange(Context.getSourceManager(),61 Context.getLangOpts(),62 CharSourceRange::getTokenRange(R));63}64 65static inline SourceLocation translateSourceLocation(CXSourceLocation L) {66 return SourceLocation::getFromRawEncoding(L.int_data);67}68 69static inline SourceRange translateCXSourceRange(CXSourceRange R) {70 return SourceRange(SourceLocation::getFromRawEncoding(R.begin_int_data),71 SourceLocation::getFromRawEncoding(R.end_int_data));72}73 74/// Translates CXSourceRange to CharSourceRange.75/// The semantics of \p R are:76/// R.begin_int_data is first character of the range.77/// R.end_int_data is one character past the end of the range.78CharSourceRange translateCXRangeToCharRange(CXSourceRange R);79}} // end namespace: clang::cxloc80 81#endif82