blob: 8bfc6f9da7ab37a118700f6a385ddd451df171c1 [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 {
Ted Kremeneka297de22010-01-25 22:34:44 +000028
29/// \brief Translate a Clang source location into a CIndex source location.
Douglas Gregor5352ac02010-01-28 00:27:43 +000030static inline CXSourceLocation
31translateSourceLocation(const SourceManager &SM, const LangOptions &LangOpts,
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +000032 SourceLocation Loc) {
33 CXSourceLocation Result = { { (void*) &SM, (void*) &LangOpts, },
Douglas Gregor5352ac02010-01-28 00:27:43 +000034 Loc.getRawEncoding() };
35 return Result;
36}
37
38/// \brief Translate a Clang source location into a CIndex source location.
Ted Kremeneka297de22010-01-25 22:34:44 +000039static inline CXSourceLocation translateSourceLocation(ASTContext &Context,
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +000040 SourceLocation Loc) {
41 return translateSourceLocation(Context.getSourceManager(),
Douglas Gregor5352ac02010-01-28 00:27:43 +000042 Context.getLangOptions(),
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +000043 Loc);
Ted Kremeneka297de22010-01-25 22:34:44 +000044}
45
46/// \brief Translate a Clang source range into a CIndex source range.
Daniel Dunbar76dd3c22010-02-14 01:47:29 +000047///
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.
52CXSourceRange translateSourceRange(const SourceManager &SM,
53 const LangOptions &LangOpts,
54 SourceRange R);
Douglas Gregor5352ac02010-01-28 00:27:43 +000055
56/// \brief Translate a Clang source range into a CIndex source range.
57static inline CXSourceRange translateSourceRange(ASTContext &Context,
58 SourceRange R) {
59 return translateSourceRange(Context.getSourceManager(),
60 Context.getLangOptions(),
61 R);
62}
Ted Kremeneka297de22010-01-25 22:34:44 +000063
64static inline SourceLocation translateSourceLocation(CXSourceLocation L) {
65 return SourceLocation::getFromRawEncoding(L.int_data);
66}
67
Daniel Dunbar85b988f2010-02-14 08:31:57 +000068static inline SourceRange translateCXSourceRange(CXSourceRange R) {
Ted Kremeneka297de22010-01-25 22:34:44 +000069 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