FileOffset.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- FileOffset.h - Offset in a file --------------------------*- 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. #ifndef LLVM_CLANG_EDIT_FILEOFFSET_H
  14. #define LLVM_CLANG_EDIT_FILEOFFSET_H
  15. #include "clang/Basic/SourceLocation.h"
  16. #include <tuple>
  17. namespace clang {
  18. namespace edit {
  19. class FileOffset {
  20. FileID FID;
  21. unsigned Offs = 0;
  22. public:
  23. FileOffset() = default;
  24. FileOffset(FileID fid, unsigned offs) : FID(fid), Offs(offs) {}
  25. bool isInvalid() const { return FID.isInvalid(); }
  26. FileID getFID() const { return FID; }
  27. unsigned getOffset() const { return Offs; }
  28. FileOffset getWithOffset(unsigned offset) const {
  29. FileOffset NewOffs = *this;
  30. NewOffs.Offs += offset;
  31. return NewOffs;
  32. }
  33. friend bool operator==(FileOffset LHS, FileOffset RHS) {
  34. return LHS.FID == RHS.FID && LHS.Offs == RHS.Offs;
  35. }
  36. friend bool operator!=(FileOffset LHS, FileOffset RHS) {
  37. return !(LHS == RHS);
  38. }
  39. friend bool operator<(FileOffset LHS, FileOffset RHS) {
  40. return std::tie(LHS.FID, LHS.Offs) < std::tie(RHS.FID, RHS.Offs);
  41. }
  42. friend bool operator>(FileOffset LHS, FileOffset RHS) {
  43. return RHS < LHS;
  44. }
  45. friend bool operator>=(FileOffset LHS, FileOffset RHS) {
  46. return !(LHS < RHS);
  47. }
  48. friend bool operator<=(FileOffset LHS, FileOffset RHS) {
  49. return !(RHS < LHS);
  50. }
  51. };
  52. } // namespace edit
  53. } // namespace clang
  54. #endif // LLVM_CLANG_EDIT_FILEOFFSET_H
  55. #ifdef __GNUC__
  56. #pragma GCC diagnostic pop
  57. #endif