Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 1 | //===- CXSourceLocation.h - CXSourceLocations Utilities ---------*- C++ -*-===// |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines routines for manipulating CXSourceLocations. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_CLANG_CXSOURCELOCATION_H |
| 15 | #define LLVM_CLANG_CXSOURCELOCATION_H |
| 16 | |
| 17 | #include "clang-c/Index.h" |
| 18 | #include "clang/Basic/SourceLocation.h" |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 19 | #include "clang/Basic/LangOptions.h" |
| 20 | #include "clang/AST/ASTContext.h" |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 21 | |
| 22 | namespace clang { |
Benjamin Kramer | b846deb | 2010-04-12 19:45:50 +0000 | [diff] [blame] | 23 | |
| 24 | class SourceManager; |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 25 | |
| 26 | namespace cxloc { |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 27 | |
| 28 | /// \brief Translate a Clang source location into a CIndex source location. |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 29 | static inline CXSourceLocation |
| 30 | translateSourceLocation(const SourceManager &SM, const LangOptions &LangOpts, |
Daniel Dunbar | bb4a61a | 2010-02-14 01:47:36 +0000 | [diff] [blame] | 31 | SourceLocation Loc) { |
Ted Kremenek | 1a9a0bc | 2010-06-28 23:54:17 +0000 | [diff] [blame] | 32 | if (Loc.isInvalid()) |
| 33 | clang_getNullLocation(); |
| 34 | |
Daniel Dunbar | bb4a61a | 2010-02-14 01:47:36 +0000 | [diff] [blame] | 35 | CXSourceLocation Result = { { (void*) &SM, (void*) &LangOpts, }, |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 36 | Loc.getRawEncoding() }; |
| 37 | return Result; |
| 38 | } |
| 39 | |
| 40 | /// \brief Translate a Clang source location into a CIndex source location. |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 41 | static inline CXSourceLocation translateSourceLocation(ASTContext &Context, |
Daniel Dunbar | bb4a61a | 2010-02-14 01:47:36 +0000 | [diff] [blame] | 42 | SourceLocation Loc) { |
| 43 | return translateSourceLocation(Context.getSourceManager(), |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 44 | Context.getLangOptions(), |
Daniel Dunbar | bb4a61a | 2010-02-14 01:47:36 +0000 | [diff] [blame] | 45 | Loc); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | /// \brief Translate a Clang source range into a CIndex source range. |
Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 49 | /// |
| 50 | /// Clang internally represents ranges where the end location points to the |
| 51 | /// start of the token at the end. However, for external clients it is more |
| 52 | /// useful to have a CXSourceRange be a proper half-open interval. This routine |
| 53 | /// does the appropriate translation. |
| 54 | CXSourceRange translateSourceRange(const SourceManager &SM, |
| 55 | const LangOptions &LangOpts, |
Chris Lattner | 0a76aae | 2010-06-18 22:45:06 +0000 | [diff] [blame] | 56 | const CharSourceRange &R); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 57 | |
| 58 | /// \brief Translate a Clang source range into a CIndex source range. |
| 59 | static inline CXSourceRange translateSourceRange(ASTContext &Context, |
| 60 | SourceRange R) { |
| 61 | return translateSourceRange(Context.getSourceManager(), |
| 62 | Context.getLangOptions(), |
Chris Lattner | 0a76aae | 2010-06-18 22:45:06 +0000 | [diff] [blame] | 63 | CharSourceRange::getTokenRange(R)); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 64 | } |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 65 | |
| 66 | static inline SourceLocation translateSourceLocation(CXSourceLocation L) { |
| 67 | return SourceLocation::getFromRawEncoding(L.int_data); |
| 68 | } |
| 69 | |
Daniel Dunbar | 85b988f | 2010-02-14 08:31:57 +0000 | [diff] [blame] | 70 | static inline SourceRange translateCXSourceRange(CXSourceRange R) { |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 71 | return SourceRange(SourceLocation::getFromRawEncoding(R.begin_int_data), |
| 72 | SourceLocation::getFromRawEncoding(R.end_int_data)); |
| 73 | } |
| 74 | |
| 75 | |
| 76 | }} // end namespace: clang::cxloc |
| 77 | |
| 78 | #endif |