• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- CXSourceLocation.h - CXSourceLocations Utilities ---------*- C++ -*-===//
2 //
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_TOOLS_LIBCLANG_CXSOURCELOCATION_H
15 #define LLVM_CLANG_TOOLS_LIBCLANG_CXSOURCELOCATION_H
16 
17 #include "clang-c/Index.h"
18 #include "clang/AST/ASTContext.h"
19 #include "clang/Basic/LangOptions.h"
20 #include "clang/Basic/SourceLocation.h"
21 
22 namespace clang {
23 
24 class SourceManager;
25 
26 namespace cxloc {
27 
28 /// \brief Translate a Clang source location into a CIndex source location.
29 static inline CXSourceLocation
translateSourceLocation(const SourceManager & SM,const LangOptions & LangOpts,SourceLocation Loc)30 translateSourceLocation(const SourceManager &SM, const LangOptions &LangOpts,
31                         SourceLocation Loc) {
32   if (Loc.isInvalid())
33     clang_getNullLocation();
34 
35   CXSourceLocation Result = { { &SM, &LangOpts, },
36                               Loc.getRawEncoding() };
37   return Result;
38 }
39 
40 /// \brief Translate a Clang source location into a CIndex source location.
translateSourceLocation(ASTContext & Context,SourceLocation Loc)41 static inline CXSourceLocation translateSourceLocation(ASTContext &Context,
42                                                        SourceLocation Loc) {
43   return translateSourceLocation(Context.getSourceManager(),
44                                  Context.getLangOpts(),
45                                  Loc);
46 }
47 
48 /// \brief Translate a Clang source range into a CIndex source range.
49 ///
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.
54 CXSourceRange translateSourceRange(const SourceManager &SM,
55                                    const LangOptions &LangOpts,
56                                    const CharSourceRange &R);
57 
58 /// \brief Translate a Clang source range into a CIndex source range.
translateSourceRange(ASTContext & Context,SourceRange R)59 static inline CXSourceRange translateSourceRange(ASTContext &Context,
60                                                  SourceRange R) {
61   return translateSourceRange(Context.getSourceManager(),
62                               Context.getLangOpts(),
63                               CharSourceRange::getTokenRange(R));
64 }
65 
translateSourceLocation(CXSourceLocation L)66 static inline SourceLocation translateSourceLocation(CXSourceLocation L) {
67   return SourceLocation::getFromRawEncoding(L.int_data);
68 }
69 
translateCXSourceRange(CXSourceRange R)70 static inline SourceRange translateCXSourceRange(CXSourceRange R) {
71   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
79