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
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 = { .ptr_data: { &SM, &LangOpts, },
35 .int_data: 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(SM: Context.getSourceManager(),
43 LangOpts: 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.
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(SM: Context.getSourceManager(),
61 LangOpts: Context.getLangOpts(),
62 R: CharSourceRange::getTokenRange(R));
63}
64
65static inline SourceLocation translateSourceLocation(CXSourceLocation L) {
66 return SourceLocation::getFromRawEncoding(Encoding: L.int_data);
67}
68
69static inline SourceRange translateCXSourceRange(CXSourceRange R) {
70 return SourceRange(SourceLocation::getFromRawEncoding(Encoding: R.begin_int_data),
71 SourceLocation::getFromRawEncoding(Encoding: 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::cxloc
80
81#endif
82

source code of clang/tools/libclang/CXSourceLocation.h