blob: 7a502059634a5a7127cb728c1b47e9ac664cc49d [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) {
Ted Kremenek1a9a0bc2010-06-28 23:54:17 +000032 if (Loc.isInvalid())
33 clang_getNullLocation();
34
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +000035 CXSourceLocation Result = { { (void*) &SM, (void*) &LangOpts, },
Douglas Gregor5352ac02010-01-28 00:27:43 +000036 Loc.getRawEncoding() };
37 return Result;
38}
39
40/// \brief Translate a Clang source location into a CIndex source location.
Ted Kremeneka297de22010-01-25 22:34:44 +000041static inline CXSourceLocation translateSourceLocation(ASTContext &Context,
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +000042 SourceLocation Loc) {
43 return translateSourceLocation(Context.getSourceManager(),
Douglas Gregor5352ac02010-01-28 00:27:43 +000044 Context.getLangOptions(),
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +000045 Loc);
Ted Kremeneka297de22010-01-25 22:34:44 +000046}
47
48/// \brief Translate a Clang source range into a CIndex source range.
Daniel Dunbar76dd3c22010-02-14 01:47:29 +000049///
50/// Clang internally represents ranges where the end location points to the
51/// start of the token at the end. However, for external clients it is more
52/// useful to have a CXSourceRange be a proper half-open interval. This routine
53/// does the appropriate translation.
54CXSourceRange translateSourceRange(const SourceManager &SM,
55 const LangOptions &LangOpts,
Chris Lattner0a76aae2010-06-18 22:45:06 +000056 const CharSourceRange &R);
Douglas Gregor5352ac02010-01-28 00:27:43 +000057
58/// \brief Translate a Clang source range into a CIndex source range.
59static inline CXSourceRange translateSourceRange(ASTContext &Context,
60 SourceRange R) {
61 return translateSourceRange(Context.getSourceManager(),
62 Context.getLangOptions(),
Chris Lattner0a76aae2010-06-18 22:45:06 +000063 CharSourceRange::getTokenRange(R));
Douglas Gregor5352ac02010-01-28 00:27:43 +000064}
Ted Kremeneka297de22010-01-25 22:34:44 +000065
66static inline SourceLocation translateSourceLocation(CXSourceLocation L) {
67 return SourceLocation::getFromRawEncoding(L.int_data);
68}
69
Daniel Dunbar85b988f2010-02-14 08:31:57 +000070static inline SourceRange translateCXSourceRange(CXSourceRange R) {
Ted Kremeneka297de22010-01-25 22:34:44 +000071 return SourceRange(SourceLocation::getFromRawEncoding(R.begin_int_data),
72 SourceLocation::getFromRawEncoding(R.end_int_data));
73}
74
75
76}} // end namespace: clang::cxloc
77
78#endif