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