Daniel Dunbar | 474b207 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 1 | //===- CXSourceLocation.h - CXSourceLocations Utilities ---------*- C++ -*-===// |
Ted Kremenek | 97a4537 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Ted Kremenek | 97a4537 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines routines for manipulating CXSourceLocations. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Benjamin Kramer | 2f5db8b | 2014-08-13 16:25:19 +0000 | [diff] [blame] | 13 | #ifndef LLVM_CLANG_TOOLS_LIBCLANG_CXSOURCELOCATION_H |
| 14 | #define LLVM_CLANG_TOOLS_LIBCLANG_CXSOURCELOCATION_H |
Ted Kremenek | 97a4537 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 15 | |
| 16 | #include "clang-c/Index.h" |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Chandler Carruth | cc0694c | 2012-12-04 09:25:21 +0000 | [diff] [blame] | 18 | #include "clang/Basic/LangOptions.h" |
| 19 | #include "clang/Basic/SourceLocation.h" |
Ted Kremenek | 97a4537 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 20 | |
| 21 | namespace clang { |
Benjamin Kramer | 06441453 | 2010-04-12 19:45:50 +0000 | [diff] [blame] | 22 | |
| 23 | class SourceManager; |
Ted Kremenek | 97a4537 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 24 | |
| 25 | namespace cxloc { |
Ted Kremenek | 97a4537 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 26 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 27 | /// Translate a Clang source location into a CIndex source location. |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 28 | static inline CXSourceLocation |
| 29 | translateSourceLocation(const SourceManager &SM, const LangOptions &LangOpts, |
Daniel Dunbar | c4b4d39 | 2010-02-14 01:47:36 +0000 | [diff] [blame] | 30 | SourceLocation Loc) { |
Ted Kremenek | 5414027 | 2010-06-28 23:54:17 +0000 | [diff] [blame] | 31 | if (Loc.isInvalid()) |
Simon Tatham | fd569a1 | 2021-06-18 13:43:13 +0100 | [diff] [blame] | 32 | return clang_getNullLocation(); |
Ted Kremenek | 5414027 | 2010-06-28 23:54:17 +0000 | [diff] [blame] | 33 | |
Argyrios Kyrtzidis | 49d9d029 | 2013-01-11 22:29:47 +0000 | [diff] [blame] | 34 | CXSourceLocation Result = { { &SM, &LangOpts, }, |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 35 | Loc.getRawEncoding() }; |
| 36 | return Result; |
| 37 | } |
| 38 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 39 | /// Translate a Clang source location into a CIndex source location. |
Ted Kremenek | 97a4537 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 40 | static inline CXSourceLocation translateSourceLocation(ASTContext &Context, |
Daniel Dunbar | c4b4d39 | 2010-02-14 01:47:36 +0000 | [diff] [blame] | 41 | SourceLocation Loc) { |
| 42 | return translateSourceLocation(Context.getSourceManager(), |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 43 | Context.getLangOpts(), |
Daniel Dunbar | c4b4d39 | 2010-02-14 01:47:36 +0000 | [diff] [blame] | 44 | Loc); |
Ted Kremenek | 97a4537 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 47 | /// Translate a Clang source range into a CIndex source range. |
Daniel Dunbar | 474b207 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 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, |
Chris Lattner | ed8b6b7 | 2010-06-18 22:45:06 +0000 | [diff] [blame] | 55 | const CharSourceRange &R); |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 56 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 57 | /// Translate a Clang source range into a CIndex source range. |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 58 | static inline CXSourceRange translateSourceRange(ASTContext &Context, |
| 59 | SourceRange R) { |
| 60 | return translateSourceRange(Context.getSourceManager(), |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 61 | Context.getLangOpts(), |
Chris Lattner | ed8b6b7 | 2010-06-18 22:45:06 +0000 | [diff] [blame] | 62 | CharSourceRange::getTokenRange(R)); |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 63 | } |
Ted Kremenek | 97a4537 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 64 | |
| 65 | static inline SourceLocation translateSourceLocation(CXSourceLocation L) { |
| 66 | return SourceLocation::getFromRawEncoding(L.int_data); |
| 67 | } |
| 68 | |
Daniel Dunbar | 80daf53 | 2010-02-14 08:31:57 +0000 | [diff] [blame] | 69 | static inline SourceRange translateCXSourceRange(CXSourceRange R) { |
Ted Kremenek | 97a4537 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 70 | return SourceRange(SourceLocation::getFromRawEncoding(R.begin_int_data), |
| 71 | SourceLocation::getFromRawEncoding(R.end_int_data)); |
| 72 | } |
| 73 | |
Jan Korous | baf3c77 | 2020-09-02 13:11:35 -0700 | [diff] [blame] | 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); |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 79 | }} // end namespace: clang::cxloc |
Ted Kremenek | 97a4537 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 80 | |
| 81 | #endif |