blob: c86f6850375bb66dd3ad18fb50c4112893328e58 [file] [log] [blame]
Daniel Dunbar474b2072010-02-14 01:47:29 +00001//===- CXSourceLocation.h - CXSourceLocations Utilities ---------*- C++ -*-===//
Ted Kremenek97a45372010-01-25 22:34:44 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Ted Kremenek97a45372010-01-25 22:34:44 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines routines for manipulating CXSourceLocations.
10//
11//===----------------------------------------------------------------------===//
12
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000013#ifndef LLVM_CLANG_TOOLS_LIBCLANG_CXSOURCELOCATION_H
14#define LLVM_CLANG_TOOLS_LIBCLANG_CXSOURCELOCATION_H
Ted Kremenek97a45372010-01-25 22:34:44 +000015
16#include "clang-c/Index.h"
Douglas Gregor4f9c3762010-01-28 00:27:43 +000017#include "clang/AST/ASTContext.h"
Chandler Carruthcc0694c2012-12-04 09:25:21 +000018#include "clang/Basic/LangOptions.h"
19#include "clang/Basic/SourceLocation.h"
Ted Kremenek97a45372010-01-25 22:34:44 +000020
21namespace clang {
Benjamin Kramer064414532010-04-12 19:45:50 +000022
23class SourceManager;
Ted Kremenek97a45372010-01-25 22:34:44 +000024
25namespace cxloc {
Ted Kremenek97a45372010-01-25 22:34:44 +000026
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000027/// Translate a Clang source location into a CIndex source location.
Douglas Gregor4f9c3762010-01-28 00:27:43 +000028static inline CXSourceLocation
29translateSourceLocation(const SourceManager &SM, const LangOptions &LangOpts,
Daniel Dunbarc4b4d392010-02-14 01:47:36 +000030 SourceLocation Loc) {
Ted Kremenek54140272010-06-28 23:54:17 +000031 if (Loc.isInvalid())
Simon Tathamfd569a12021-06-18 13:43:13 +010032 return clang_getNullLocation();
Ted Kremenek54140272010-06-28 23:54:17 +000033
Argyrios Kyrtzidis49d9d0292013-01-11 22:29:47 +000034 CXSourceLocation Result = { { &SM, &LangOpts, },
Douglas Gregor4f9c3762010-01-28 00:27:43 +000035 Loc.getRawEncoding() };
36 return Result;
37}
38
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000039/// Translate a Clang source location into a CIndex source location.
Ted Kremenek97a45372010-01-25 22:34:44 +000040static inline CXSourceLocation translateSourceLocation(ASTContext &Context,
Daniel Dunbarc4b4d392010-02-14 01:47:36 +000041 SourceLocation Loc) {
42 return translateSourceLocation(Context.getSourceManager(),
David Blaikiebbafb8a2012-03-11 07:00:24 +000043 Context.getLangOpts(),
Daniel Dunbarc4b4d392010-02-14 01:47:36 +000044 Loc);
Ted Kremenek97a45372010-01-25 22:34:44 +000045}
46
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000047/// Translate a Clang source range into a CIndex source range.
Daniel Dunbar474b2072010-02-14 01:47:29 +000048///
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,
Chris Lattnered8b6b72010-06-18 22:45:06 +000055 const CharSourceRange &R);
Douglas Gregor4f9c3762010-01-28 00:27:43 +000056
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000057/// Translate a Clang source range into a CIndex source range.
Douglas Gregor4f9c3762010-01-28 00:27:43 +000058static inline CXSourceRange translateSourceRange(ASTContext &Context,
59 SourceRange R) {
60 return translateSourceRange(Context.getSourceManager(),
David Blaikiebbafb8a2012-03-11 07:00:24 +000061 Context.getLangOpts(),
Chris Lattnered8b6b72010-06-18 22:45:06 +000062 CharSourceRange::getTokenRange(R));
Douglas Gregor4f9c3762010-01-28 00:27:43 +000063}
Ted Kremenek97a45372010-01-25 22:34:44 +000064
65static inline SourceLocation translateSourceLocation(CXSourceLocation L) {
66 return SourceLocation::getFromRawEncoding(L.int_data);
67}
68
Daniel Dunbar80daf532010-02-14 08:31:57 +000069static inline SourceRange translateCXSourceRange(CXSourceRange R) {
Ted Kremenek97a45372010-01-25 22:34:44 +000070 return SourceRange(SourceLocation::getFromRawEncoding(R.begin_int_data),
71 SourceLocation::getFromRawEncoding(R.end_int_data));
72}
73
Jan Korousbaf3c772020-09-02 13:11:35 -070074/// 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);
Alexander Kornienkoab9db512015-06-22 23:07:51 +000079}} // end namespace: clang::cxloc
Ted Kremenek97a45372010-01-25 22:34:44 +000080
81#endif