CXSourceLocation.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //===- CXSourceLocation.h - CXSourceLocations Utilities ---------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines routines for manipulating CXSourceLocations.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_CLANG_TOOLS_LIBCLANG_CXSOURCELOCATION_H
  13. #define LLVM_CLANG_TOOLS_LIBCLANG_CXSOURCELOCATION_H
  14. #include "clang-c/Index.h"
  15. #include "clang/AST/ASTContext.h"
  16. #include "clang/Basic/LangOptions.h"
  17. #include "clang/Basic/SourceLocation.h"
  18. namespace clang {
  19. class SourceManager;
  20. namespace cxloc {
  21. /// Translate a Clang source location into a CIndex source location.
  22. static inline CXSourceLocation
  23. translateSourceLocation(const SourceManager &SM, const LangOptions &LangOpts,
  24. SourceLocation Loc) {
  25. if (Loc.isInvalid())
  26. return clang_getNullLocation();
  27. CXSourceLocation Result = { { &SM, &LangOpts, },
  28. Loc.getRawEncoding() };
  29. return Result;
  30. }
  31. /// Translate a Clang source location into a CIndex source location.
  32. static inline CXSourceLocation translateSourceLocation(ASTContext &Context,
  33. SourceLocation Loc) {
  34. return translateSourceLocation(Context.getSourceManager(),
  35. Context.getLangOpts(),
  36. Loc);
  37. }
  38. /// Translate a Clang source range into a CIndex source range.
  39. ///
  40. /// Clang internally represents ranges where the end location points to the
  41. /// start of the token at the end. However, for external clients it is more
  42. /// useful to have a CXSourceRange be a proper half-open interval. This routine
  43. /// does the appropriate translation.
  44. CXSourceRange translateSourceRange(const SourceManager &SM,
  45. const LangOptions &LangOpts,
  46. const CharSourceRange &R);
  47. /// Translate a Clang source range into a CIndex source range.
  48. static inline CXSourceRange translateSourceRange(ASTContext &Context,
  49. SourceRange R) {
  50. return translateSourceRange(Context.getSourceManager(),
  51. Context.getLangOpts(),
  52. CharSourceRange::getTokenRange(R));
  53. }
  54. static inline SourceLocation translateSourceLocation(CXSourceLocation L) {
  55. return SourceLocation::getFromRawEncoding(L.int_data);
  56. }
  57. static inline SourceRange translateCXSourceRange(CXSourceRange R) {
  58. return SourceRange(SourceLocation::getFromRawEncoding(R.begin_int_data),
  59. SourceLocation::getFromRawEncoding(R.end_int_data));
  60. }
  61. /// Translates CXSourceRange to CharSourceRange.
  62. /// The semantics of \p R are:
  63. /// R.begin_int_data is first character of the range.
  64. /// R.end_int_data is one character past the end of the range.
  65. CharSourceRange translateCXRangeToCharRange(CXSourceRange R);
  66. }} // end namespace: clang::cxloc
  67. #endif