17330f729Sjoerg //===- CXSourceLocation.h - CXSourceLocations Utilities ---------*- C++ -*-===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // This file defines routines for manipulating CXSourceLocations.
107330f729Sjoerg //
117330f729Sjoerg //===----------------------------------------------------------------------===//
127330f729Sjoerg
137330f729Sjoerg #ifndef LLVM_CLANG_TOOLS_LIBCLANG_CXSOURCELOCATION_H
147330f729Sjoerg #define LLVM_CLANG_TOOLS_LIBCLANG_CXSOURCELOCATION_H
157330f729Sjoerg
167330f729Sjoerg #include "clang-c/Index.h"
177330f729Sjoerg #include "clang/AST/ASTContext.h"
187330f729Sjoerg #include "clang/Basic/LangOptions.h"
197330f729Sjoerg #include "clang/Basic/SourceLocation.h"
207330f729Sjoerg
217330f729Sjoerg namespace clang {
227330f729Sjoerg
237330f729Sjoerg class SourceManager;
247330f729Sjoerg
257330f729Sjoerg namespace cxloc {
267330f729Sjoerg
277330f729Sjoerg /// Translate a Clang source location into a CIndex source location.
287330f729Sjoerg static inline CXSourceLocation
translateSourceLocation(const SourceManager & SM,const LangOptions & LangOpts,SourceLocation Loc)297330f729Sjoerg translateSourceLocation(const SourceManager &SM, const LangOptions &LangOpts,
307330f729Sjoerg SourceLocation Loc) {
317330f729Sjoerg if (Loc.isInvalid())
327330f729Sjoerg clang_getNullLocation();
337330f729Sjoerg
347330f729Sjoerg CXSourceLocation Result = { { &SM, &LangOpts, },
357330f729Sjoerg Loc.getRawEncoding() };
367330f729Sjoerg return Result;
377330f729Sjoerg }
387330f729Sjoerg
397330f729Sjoerg /// Translate a Clang source location into a CIndex source location.
translateSourceLocation(ASTContext & Context,SourceLocation Loc)407330f729Sjoerg static inline CXSourceLocation translateSourceLocation(ASTContext &Context,
417330f729Sjoerg SourceLocation Loc) {
427330f729Sjoerg return translateSourceLocation(Context.getSourceManager(),
437330f729Sjoerg Context.getLangOpts(),
447330f729Sjoerg Loc);
457330f729Sjoerg }
467330f729Sjoerg
477330f729Sjoerg /// Translate a Clang source range into a CIndex source range.
487330f729Sjoerg ///
497330f729Sjoerg /// Clang internally represents ranges where the end location points to the
507330f729Sjoerg /// start of the token at the end. However, for external clients it is more
517330f729Sjoerg /// useful to have a CXSourceRange be a proper half-open interval. This routine
527330f729Sjoerg /// does the appropriate translation.
537330f729Sjoerg CXSourceRange translateSourceRange(const SourceManager &SM,
547330f729Sjoerg const LangOptions &LangOpts,
557330f729Sjoerg const CharSourceRange &R);
567330f729Sjoerg
577330f729Sjoerg /// Translate a Clang source range into a CIndex source range.
translateSourceRange(ASTContext & Context,SourceRange R)587330f729Sjoerg static inline CXSourceRange translateSourceRange(ASTContext &Context,
597330f729Sjoerg SourceRange R) {
607330f729Sjoerg return translateSourceRange(Context.getSourceManager(),
617330f729Sjoerg Context.getLangOpts(),
627330f729Sjoerg CharSourceRange::getTokenRange(R));
637330f729Sjoerg }
647330f729Sjoerg
translateSourceLocation(CXSourceLocation L)657330f729Sjoerg static inline SourceLocation translateSourceLocation(CXSourceLocation L) {
667330f729Sjoerg return SourceLocation::getFromRawEncoding(L.int_data);
677330f729Sjoerg }
687330f729Sjoerg
translateCXSourceRange(CXSourceRange R)697330f729Sjoerg static inline SourceRange translateCXSourceRange(CXSourceRange R) {
707330f729Sjoerg return SourceRange(SourceLocation::getFromRawEncoding(R.begin_int_data),
717330f729Sjoerg SourceLocation::getFromRawEncoding(R.end_int_data));
727330f729Sjoerg }
737330f729Sjoerg
74*e038c9c4Sjoerg /// Translates CXSourceRange to CharSourceRange.
75*e038c9c4Sjoerg /// The semantics of \p R are:
76*e038c9c4Sjoerg /// R.begin_int_data is first character of the range.
77*e038c9c4Sjoerg /// R.end_int_data is one character past the end of the range.
78*e038c9c4Sjoerg CharSourceRange translateCXRangeToCharRange(CXSourceRange R);
797330f729Sjoerg }} // end namespace: clang::cxloc
807330f729Sjoerg
817330f729Sjoerg #endif
82