Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 1 | //===- CXSourceLocation.h - Routines for manipulating CXSourceLocations ---===// |
| 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 { |
| 28 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame^] | 29 | typedef llvm::PointerIntPair<const SourceManager *, 1, bool> |
| 30 | CXSourceLocationPtr; |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 31 | |
| 32 | /// \brief Translate a Clang source location into a CIndex source location. |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame^] | 33 | static inline CXSourceLocation |
| 34 | translateSourceLocation(const SourceManager &SM, const LangOptions &LangOpts, |
| 35 | SourceLocation Loc, bool AtEnd = false) { |
| 36 | CXSourceLocationPtr Ptr(&SM, AtEnd); |
| 37 | CXSourceLocation Result = { { Ptr.getOpaqueValue(), (void *)&LangOpts, }, |
| 38 | Loc.getRawEncoding() }; |
| 39 | return Result; |
| 40 | } |
| 41 | |
| 42 | /// \brief Translate a Clang source location into a CIndex source location. |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 43 | static inline CXSourceLocation translateSourceLocation(ASTContext &Context, |
| 44 | SourceLocation Loc, |
| 45 | bool AtEnd = false) { |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame^] | 46 | return translateSourceLocation(Context.getSourceManager(), |
| 47 | Context.getLangOptions(), |
| 48 | Loc, AtEnd); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /// \brief Translate a Clang source range into a CIndex source range. |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame^] | 52 | static inline CXSourceRange translateSourceRange(const SourceManager &SM, |
| 53 | const LangOptions &LangOpts, |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 54 | SourceRange R) { |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame^] | 55 | CXSourceRange Result = { { (void *)&SM, (void *)&LangOpts }, |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 56 | R.getBegin().getRawEncoding(), |
| 57 | R.getEnd().getRawEncoding() }; |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 58 | return Result; |
| 59 | } |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame^] | 60 | |
| 61 | /// \brief Translate a Clang source range into a CIndex source range. |
| 62 | static inline CXSourceRange translateSourceRange(ASTContext &Context, |
| 63 | SourceRange R) { |
| 64 | return translateSourceRange(Context.getSourceManager(), |
| 65 | Context.getLangOptions(), |
| 66 | R); |
| 67 | } |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 68 | |
| 69 | static inline SourceLocation translateSourceLocation(CXSourceLocation L) { |
| 70 | return SourceLocation::getFromRawEncoding(L.int_data); |
| 71 | } |
| 72 | |
| 73 | static inline SourceRange translateSourceRange(CXSourceRange R) { |
| 74 | return SourceRange(SourceLocation::getFromRawEncoding(R.begin_int_data), |
| 75 | SourceLocation::getFromRawEncoding(R.end_int_data)); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | }} // end namespace: clang::cxloc |
| 80 | |
| 81 | #endif |