SMLoc.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //===- SMLoc.h - Source location for use with diagnostics -------*- 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 declares the SMLoc class. This class encapsulates a location in
  10. // source code for use in diagnostics.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_SUPPORT_SMLOC_H
  14. #define LLVM_SUPPORT_SMLOC_H
  15. #include "llvm/ADT/None.h"
  16. #include <cassert>
  17. namespace llvm {
  18. /// Represents a location in source code.
  19. class SMLoc {
  20. const char *Ptr = nullptr;
  21. public:
  22. SMLoc() = default;
  23. bool isValid() const { return Ptr != nullptr; }
  24. bool operator==(const SMLoc &RHS) const { return RHS.Ptr == Ptr; }
  25. bool operator!=(const SMLoc &RHS) const { return RHS.Ptr != Ptr; }
  26. const char *getPointer() const { return Ptr; }
  27. static SMLoc getFromPointer(const char *Ptr) {
  28. SMLoc L;
  29. L.Ptr = Ptr;
  30. return L;
  31. }
  32. };
  33. /// Represents a range in source code.
  34. ///
  35. /// SMRange is implemented using a half-open range, as is the convention in C++.
  36. /// In the string "abc", the range [1,3) represents the substring "bc", and the
  37. /// range [2,2) represents an empty range between the characters "b" and "c".
  38. class SMRange {
  39. public:
  40. SMLoc Start, End;
  41. SMRange() = default;
  42. SMRange(NoneType) {}
  43. SMRange(SMLoc St, SMLoc En) : Start(St), End(En) {
  44. assert(Start.isValid() == End.isValid() &&
  45. "Start and End should either both be valid or both be invalid!");
  46. }
  47. bool isValid() const { return Start.isValid(); }
  48. };
  49. } // end namespace llvm
  50. #endif // LLVM_SUPPORT_SMLOC_H