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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines routines for manipulating CXSourceLocations.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_CLANG_TOOLS_LIBCLANG_CXSOURCELOCATION_H
14 #define LLVM_CLANG_TOOLS_LIBCLANG_CXSOURCELOCATION_H
15
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
21 namespace clang {
22
23 class SourceManager;
24
25 namespace cxloc {
26
27 /// Translate a Clang source location into a CIndex source location.
28 static inline CXSourceLocation
translateSourceLocation(const SourceManager & SM,const LangOptions & LangOpts,SourceLocation Loc)29 translateSourceLocation(const SourceManager &SM, const LangOptions &LangOpts,
30 SourceLocation Loc) {
31 if (Loc.isInvalid())
32 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.
translateSourceLocation(ASTContext & Context,SourceLocation Loc)40 static 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 the
50 /// start of the token at the end. However, for external clients it is more
51 /// useful to have a CXSourceRange be a proper half-open interval. This routine
52 /// does the appropriate translation.
53 CXSourceRange translateSourceRange(const SourceManager &SM,
54 const LangOptions &LangOpts,
55 const CharSourceRange &R);
56
57 /// Translate a Clang source range into a CIndex source range.
translateSourceRange(ASTContext & Context,SourceRange R)58 static inline CXSourceRange translateSourceRange(ASTContext &Context,
59 SourceRange R) {
60 return translateSourceRange(Context.getSourceManager(),
61 Context.getLangOpts(),
62 CharSourceRange::getTokenRange(R));
63 }
64
translateSourceLocation(CXSourceLocation L)65 static inline SourceLocation translateSourceLocation(CXSourceLocation L) {
66 return SourceLocation::getFromRawEncoding(L.int_data);
67 }
68
translateCXSourceRange(CXSourceRange R)69 static 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.
78 CharSourceRange translateCXRangeToCharRange(CXSourceRange R);
79 }} // end namespace: clang::cxloc
80
81 #endif
82