SMLoc.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- SMLoc.h - Source location for use with diagnostics -------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file declares the SMLoc class. This class encapsulates a location in
  15. // source code for use in diagnostics.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_SUPPORT_SMLOC_H
  19. #define LLVM_SUPPORT_SMLOC_H
  20. #include <cassert>
  21. #include <optional>
  22. namespace llvm {
  23. /// Represents a location in source code.
  24. class SMLoc {
  25. const char *Ptr = nullptr;
  26. public:
  27. SMLoc() = default;
  28. bool isValid() const { return Ptr != nullptr; }
  29. bool operator==(const SMLoc &RHS) const { return RHS.Ptr == Ptr; }
  30. bool operator!=(const SMLoc &RHS) const { return RHS.Ptr != Ptr; }
  31. const char *getPointer() const { return Ptr; }
  32. static SMLoc getFromPointer(const char *Ptr) {
  33. SMLoc L;
  34. L.Ptr = Ptr;
  35. return L;
  36. }
  37. };
  38. /// Represents a range in source code.
  39. ///
  40. /// SMRange is implemented using a half-open range, as is the convention in C++.
  41. /// In the string "abc", the range [1,3) represents the substring "bc", and the
  42. /// range [2,2) represents an empty range between the characters "b" and "c".
  43. class SMRange {
  44. public:
  45. SMLoc Start, End;
  46. SMRange() = default;
  47. SMRange(std::nullopt_t) {}
  48. SMRange(SMLoc St, SMLoc En) : Start(St), End(En) {
  49. assert(Start.isValid() == End.isValid() &&
  50. "Start and End should either both be valid or both be invalid!");
  51. }
  52. bool isValid() const { return Start.isValid(); }
  53. };
  54. } // end namespace llvm
  55. #endif // LLVM_SUPPORT_SMLOC_H
  56. #ifdef __GNUC__
  57. #pragma GCC diagnostic pop
  58. #endif