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/SourceManager.h" |
| 20 | #include "clang/Basic/LangOptions.h" |
| 21 | #include "clang/AST/ASTContext.h" |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 22 | |
| 23 | namespace clang { |
| 24 | |
| 25 | class ASTContext; |
| 26 | |
| 27 | namespace cxloc { |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 28 | |
| 29 | /// \brief Translate a Clang source location into a CIndex source location. |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 30 | static inline CXSourceLocation |
| 31 | translateSourceLocation(const SourceManager &SM, const LangOptions &LangOpts, |
Daniel Dunbar | bb4a61a | 2010-02-14 01:47:36 +0000 | [diff] [blame] | 32 | SourceLocation Loc) { |
| 33 | CXSourceLocation Result = { { (void*) &SM, (void*) &LangOpts, }, |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 34 | Loc.getRawEncoding() }; |
| 35 | return Result; |
| 36 | } |
| 37 | |
| 38 | /// \brief Translate a Clang source location into a CIndex source location. |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 39 | static inline CXSourceLocation translateSourceLocation(ASTContext &Context, |
Daniel Dunbar | bb4a61a | 2010-02-14 01:47:36 +0000 | [diff] [blame] | 40 | SourceLocation Loc) { |
| 41 | return translateSourceLocation(Context.getSourceManager(), |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 42 | Context.getLangOptions(), |
Daniel Dunbar | bb4a61a | 2010-02-14 01:47:36 +0000 | [diff] [blame] | 43 | Loc); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | /// \brief Translate a Clang source range into a CIndex source range. |
Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 47 | /// |
| 48 | /// Clang internally represents ranges where the end location points to the |
| 49 | /// start of the token at the end. However, for external clients it is more |
| 50 | /// useful to have a CXSourceRange be a proper half-open interval. This routine |
| 51 | /// does the appropriate translation. |
| 52 | CXSourceRange translateSourceRange(const SourceManager &SM, |
| 53 | const LangOptions &LangOpts, |
| 54 | SourceRange R); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 55 | |
| 56 | /// \brief Translate a Clang source range into a CIndex source range. |
| 57 | static inline CXSourceRange translateSourceRange(ASTContext &Context, |
| 58 | SourceRange R) { |
| 59 | return translateSourceRange(Context.getSourceManager(), |
| 60 | Context.getLangOptions(), |
| 61 | R); |
| 62 | } |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 63 | |
| 64 | static inline SourceLocation translateSourceLocation(CXSourceLocation L) { |
| 65 | return SourceLocation::getFromRawEncoding(L.int_data); |
| 66 | } |
| 67 | |
Daniel Dunbar | 85b988f | 2010-02-14 08:31:57 +0000 | [diff] [blame^] | 68 | static inline SourceRange translateCXSourceRange(CXSourceRange R) { |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 69 | return SourceRange(SourceLocation::getFromRawEncoding(R.begin_int_data), |
| 70 | SourceLocation::getFromRawEncoding(R.end_int_data)); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | }} // end namespace: clang::cxloc |
| 75 | |
| 76 | #endif |