blob: 95b774b24cc2b7ea31d5cfcc31de57574a6ab175 [file] [log] [blame]
Daniel Dunbar76dd3c22010-02-14 01:47:29 +00001//===- CXSourceLocation.h - CXSourceLocations Utilities ---------*- C++ -*-===//
Ted Kremeneka297de22010-01-25 22:34:44 +00002//
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 Gregor5352ac02010-01-28 00:27:43 +000019#include "clang/Basic/SourceManager.h"
20#include "clang/Basic/LangOptions.h"
21#include "clang/AST/ASTContext.h"
Ted Kremeneka297de22010-01-25 22:34:44 +000022
23namespace clang {
24
25class ASTContext;
26
27namespace cxloc {
28
Douglas Gregor5352ac02010-01-28 00:27:43 +000029typedef llvm::PointerIntPair<const SourceManager *, 1, bool>
30 CXSourceLocationPtr;
Ted Kremeneka297de22010-01-25 22:34:44 +000031
32/// \brief Translate a Clang source location into a CIndex source location.
Douglas Gregor5352ac02010-01-28 00:27:43 +000033static inline CXSourceLocation
34translateSourceLocation(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 Kremeneka297de22010-01-25 22:34:44 +000043static inline CXSourceLocation translateSourceLocation(ASTContext &Context,
44 SourceLocation Loc,
45 bool AtEnd = false) {
Douglas Gregor5352ac02010-01-28 00:27:43 +000046 return translateSourceLocation(Context.getSourceManager(),
47 Context.getLangOptions(),
48 Loc, AtEnd);
Ted Kremeneka297de22010-01-25 22:34:44 +000049}
50
51/// \brief Translate a Clang source range into a CIndex source range.
Daniel Dunbar76dd3c22010-02-14 01:47:29 +000052///
53/// Clang internally represents ranges where the end location points to the
54/// start of the token at the end. However, for external clients it is more
55/// useful to have a CXSourceRange be a proper half-open interval. This routine
56/// does the appropriate translation.
57CXSourceRange translateSourceRange(const SourceManager &SM,
58 const LangOptions &LangOpts,
59 SourceRange R);
Douglas Gregor5352ac02010-01-28 00:27:43 +000060
61/// \brief Translate a Clang source range into a CIndex source range.
62static inline CXSourceRange translateSourceRange(ASTContext &Context,
63 SourceRange R) {
64 return translateSourceRange(Context.getSourceManager(),
65 Context.getLangOptions(),
66 R);
67}
Ted Kremeneka297de22010-01-25 22:34:44 +000068
69static inline SourceLocation translateSourceLocation(CXSourceLocation L) {
70 return SourceLocation::getFromRawEncoding(L.int_data);
71}
72
73static 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