blob: 66566c126891d42ea495197eb5c74047ac7e050f [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/LangOptions.h"
20#include "clang/AST/ASTContext.h"
Ted Kremeneka297de22010-01-25 22:34:44 +000021
22namespace clang {
Benjamin Kramerb846deb2010-04-12 19:45:50 +000023
24class SourceManager;
Ted Kremeneka297de22010-01-25 22:34:44 +000025
26namespace cxloc {
Ted Kremeneka297de22010-01-25 22:34:44 +000027
28/// \brief Translate a Clang source location into a CIndex source location.
Douglas Gregor5352ac02010-01-28 00:27:43 +000029static inline CXSourceLocation
30translateSourceLocation(const SourceManager &SM, const LangOptions &LangOpts,
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +000031 SourceLocation Loc) {
32 CXSourceLocation Result = { { (void*) &SM, (void*) &LangOpts, },
Douglas Gregor5352ac02010-01-28 00:27:43 +000033 Loc.getRawEncoding() };
34 return Result;
35}
36
37/// \brief Translate a Clang source location into a CIndex source location.
Ted Kremeneka297de22010-01-25 22:34:44 +000038static inline CXSourceLocation translateSourceLocation(ASTContext &Context,
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +000039 SourceLocation Loc) {
40 return translateSourceLocation(Context.getSourceManager(),
Douglas Gregor5352ac02010-01-28 00:27:43 +000041 Context.getLangOptions(),
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +000042 Loc);
Ted Kremeneka297de22010-01-25 22:34:44 +000043}
44
45/// \brief Translate a Clang source range into a CIndex source range.
Daniel Dunbar76dd3c22010-02-14 01:47:29 +000046///
47/// Clang internally represents ranges where the end location points to the
48/// start of the token at the end. However, for external clients it is more
49/// useful to have a CXSourceRange be a proper half-open interval. This routine
50/// does the appropriate translation.
51CXSourceRange translateSourceRange(const SourceManager &SM,
52 const LangOptions &LangOpts,
53 SourceRange R);
Douglas Gregor5352ac02010-01-28 00:27:43 +000054
55/// \brief Translate a Clang source range into a CIndex source range.
56static inline CXSourceRange translateSourceRange(ASTContext &Context,
57 SourceRange R) {
58 return translateSourceRange(Context.getSourceManager(),
59 Context.getLangOptions(),
60 R);
61}
Ted Kremeneka297de22010-01-25 22:34:44 +000062
63static inline SourceLocation translateSourceLocation(CXSourceLocation L) {
64 return SourceLocation::getFromRawEncoding(L.int_data);
65}
66
Daniel Dunbar85b988f2010-02-14 08:31:57 +000067static inline SourceRange translateCXSourceRange(CXSourceRange R) {
Ted Kremeneka297de22010-01-25 22:34:44 +000068 return SourceRange(SourceLocation::getFromRawEncoding(R.begin_int_data),
69 SourceLocation::getFromRawEncoding(R.end_int_data));
70}
71
72
73}} // end namespace: clang::cxloc
74
75#endif