blob: f0b3f4954919bf905f75ed232d71665b4df2e5a2 [file] [log] [blame]
Daniel Dunbar474b2072010-02-14 01:47:29 +00001//===- CXSourceLocation.h - CXSourceLocations Utilities ---------*- C++ -*-===//
Ted Kremenek97a45372010-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
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000014#ifndef LLVM_CLANG_TOOLS_LIBCLANG_CXSOURCELOCATION_H
15#define LLVM_CLANG_TOOLS_LIBCLANG_CXSOURCELOCATION_H
Ted Kremenek97a45372010-01-25 22:34:44 +000016
17#include "clang-c/Index.h"
Douglas Gregor4f9c3762010-01-28 00:27:43 +000018#include "clang/AST/ASTContext.h"
Chandler Carruthcc0694c2012-12-04 09:25:21 +000019#include "clang/Basic/LangOptions.h"
20#include "clang/Basic/SourceLocation.h"
Ted Kremenek97a45372010-01-25 22:34:44 +000021
22namespace clang {
Benjamin Kramer064414532010-04-12 19:45:50 +000023
24class SourceManager;
Ted Kremenek97a45372010-01-25 22:34:44 +000025
26namespace cxloc {
Ted Kremenek97a45372010-01-25 22:34:44 +000027
28/// \brief Translate a Clang source location into a CIndex source location.
Douglas Gregor4f9c3762010-01-28 00:27:43 +000029static inline CXSourceLocation
30translateSourceLocation(const SourceManager &SM, const LangOptions &LangOpts,
Daniel Dunbarc4b4d392010-02-14 01:47:36 +000031 SourceLocation Loc) {
Ted Kremenek54140272010-06-28 23:54:17 +000032 if (Loc.isInvalid())
33 clang_getNullLocation();
34
Argyrios Kyrtzidis49d9d0292013-01-11 22:29:47 +000035 CXSourceLocation Result = { { &SM, &LangOpts, },
Douglas Gregor4f9c3762010-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 Kremenek97a45372010-01-25 22:34:44 +000041static inline CXSourceLocation translateSourceLocation(ASTContext &Context,
Daniel Dunbarc4b4d392010-02-14 01:47:36 +000042 SourceLocation Loc) {
43 return translateSourceLocation(Context.getSourceManager(),
David Blaikiebbafb8a2012-03-11 07:00:24 +000044 Context.getLangOpts(),
Daniel Dunbarc4b4d392010-02-14 01:47:36 +000045 Loc);
Ted Kremenek97a45372010-01-25 22:34:44 +000046}
47
48/// \brief Translate a Clang source range into a CIndex source range.
Daniel Dunbar474b2072010-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 Lattnered8b6b72010-06-18 22:45:06 +000056 const CharSourceRange &R);
Douglas Gregor4f9c3762010-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(),
David Blaikiebbafb8a2012-03-11 07:00:24 +000062 Context.getLangOpts(),
Chris Lattnered8b6b72010-06-18 22:45:06 +000063 CharSourceRange::getTokenRange(R));
Douglas Gregor4f9c3762010-01-28 00:27:43 +000064}
Ted Kremenek97a45372010-01-25 22:34:44 +000065
66static inline SourceLocation translateSourceLocation(CXSourceLocation L) {
67 return SourceLocation::getFromRawEncoding(L.int_data);
68}
69
Daniel Dunbar80daf532010-02-14 08:31:57 +000070static inline SourceRange translateCXSourceRange(CXSourceRange R) {
Ted Kremenek97a45372010-01-25 22:34:44 +000071 return SourceRange(SourceLocation::getFromRawEncoding(R.begin_int_data),
72 SourceLocation::getFromRawEncoding(R.end_int_data));
73}
74
75
Alexander Kornienkoab9db512015-06-22 23:07:51 +000076}} // end namespace: clang::cxloc
Ted Kremenek97a45372010-01-25 22:34:44 +000077
78#endif