Annotations.h 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //===--- Annotations.h - Annotated source code for tests ---------*- 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. #ifndef LLVM_TESTING_SUPPORT_ANNOTATIONS_H
  9. #define LLVM_TESTING_SUPPORT_ANNOTATIONS_H
  10. #include "llvm/ADT/SmallVector.h"
  11. #include "llvm/ADT/StringMap.h"
  12. #include "llvm/ADT/StringRef.h"
  13. #include <tuple>
  14. #include <vector>
  15. namespace llvm {
  16. /// Annotations lets you mark points and ranges inside source code, for tests:
  17. ///
  18. /// Annotations Example(R"cpp(
  19. /// int complete() { x.pri^ } // ^ indicates a point
  20. /// void err() { [["hello" == 42]]; } // [[this is a range]]
  21. /// $definition^class Foo{}; // points can be named: "definition"
  22. /// $fail[[static_assert(false, "")]] // ranges can be named too: "fail"
  23. /// )cpp");
  24. ///
  25. /// StringRef Code = Example.code(); // annotations stripped.
  26. /// std::vector<size_t> PP = Example.points(); // all unnamed points
  27. /// size_t P = Example.point(); // there must be exactly one
  28. /// llvm::Range R = Example.range("fail"); // find named ranges
  29. ///
  30. /// Points/ranges are coordinated into `code()` which is stripped of
  31. /// annotations.
  32. ///
  33. /// Ranges may be nested (and points can be inside ranges), but there's no way
  34. /// to define general overlapping ranges.
  35. ///
  36. /// FIXME: the choice of the marking syntax makes it impossible to represent
  37. /// some of the C++ and Objective C constructs (including common ones
  38. /// like C++ attributes). We can fix this by:
  39. /// 1. introducing an escaping mechanism for the special characters,
  40. /// 2. making characters for marking points and ranges configurable,
  41. /// 3. changing the syntax to something less commonly used,
  42. /// 4. ...
  43. class Annotations {
  44. public:
  45. /// Two offsets pointing to a continuous substring. End is not included, i.e.
  46. /// represents a half-open range.
  47. struct Range {
  48. size_t Begin = 0;
  49. size_t End = 0;
  50. friend bool operator==(const Range &L, const Range &R) {
  51. return std::tie(L.Begin, L.End) == std::tie(R.Begin, R.End);
  52. }
  53. friend bool operator!=(const Range &L, const Range &R) { return !(L == R); }
  54. };
  55. /// Parses the annotations from Text. Crashes if it's malformed.
  56. Annotations(llvm::StringRef Text);
  57. /// The input text with all annotations stripped.
  58. /// All points and ranges are relative to this stripped text.
  59. llvm::StringRef code() const { return Code; }
  60. /// Returns the position of the point marked by ^ (or $name^) in the text.
  61. /// Crashes if there isn't exactly one.
  62. size_t point(llvm::StringRef Name = "") const;
  63. /// Returns the position of all points marked by ^ (or $name^) in the text.
  64. /// Order matches the order within the text.
  65. std::vector<size_t> points(llvm::StringRef Name = "") const;
  66. /// Returns the location of the range marked by [[ ]] (or $name[[ ]]).
  67. /// Crashes if there isn't exactly one.
  68. Range range(llvm::StringRef Name = "") const;
  69. /// Returns the location of all ranges marked by [[ ]] (or $name[[ ]]).
  70. /// They are ordered by start position within the text.
  71. std::vector<Range> ranges(llvm::StringRef Name = "") const;
  72. private:
  73. std::string Code;
  74. llvm::StringMap<llvm::SmallVector<size_t, 1>> Points;
  75. llvm::StringMap<llvm::SmallVector<Range, 1>> Ranges;
  76. };
  77. llvm::raw_ostream &operator<<(llvm::raw_ostream &O,
  78. const llvm::Annotations::Range &R);
  79. } // namespace llvm
  80. #endif