[Support] Provide access to the full mapping in llvm::Annotations
authorEric Li <li.zhe.hua@gmail.com>
Fri, 16 Sep 2022 20:07:26 +0000 (16:07 -0400)
committerEric Li <li.zhe.hua@gmail.com>
Tue, 20 Sep 2022 15:06:21 +0000 (11:06 -0400)
Providing access to the mapping of annotations allows test helpers to
be expressive by using the annotations as expectations. For example, a
matcher could verify that all annotated points were matched by a
matcher, or that an refactoring surgically modifies specific ranges.

Differential Revision: https://reviews.llvm.org/D134072

llvm/include/llvm/Testing/Support/Annotations.h
llvm/lib/Testing/Support/Annotations.cpp
llvm/unittests/Support/AnnotationsTest.cpp

index 4e44226..1cac1f1 100644 (file)
@@ -72,6 +72,10 @@ public:
   /// Returns the position of all points marked by ^ (or $name^) in the text.
   /// Order matches the order within the text.
   std::vector<size_t> points(llvm::StringRef Name = "") const;
+  /// Returns the mapping of all names of points marked in the text to their
+  /// position. Unnamed points are mapped to the empty string. Order of points
+  /// for each name matches the order within the text.
+  const llvm::StringMap<llvm::SmallVector<size_t, 1>> &all_points() const;
 
   /// Returns the location of the range marked by [[ ]] (or $name[[ ]]).
   /// Crashes if there isn't exactly one.
@@ -79,6 +83,10 @@ public:
   /// Returns the location of all ranges marked by [[ ]] (or $name[[ ]]).
   /// They are ordered by start position within the text.
   std::vector<Range> ranges(llvm::StringRef Name = "") const;
+  /// Returns the mapping of all names of ranges marked in the text to their
+  /// location. Unnamed ranges are mapped to the empty string. Order of ranges
+  /// for each name matches the order of start positions within the text.
+  const llvm::StringMap<llvm::SmallVector<Range, 1>> &all_ranges() const;
 
 private:
   std::string Code;
index 557b6cd..388215d 100644 (file)
@@ -79,6 +79,11 @@ std::vector<size_t> Annotations::points(llvm::StringRef Name) const {
   return {I->getValue().begin(), I->getValue().end()};
 }
 
+const llvm::StringMap<llvm::SmallVector<size_t, 1>> &
+Annotations::all_points() const {
+  return Points;
+}
+
 Annotations::Range Annotations::range(llvm::StringRef Name) const {
   auto I = Ranges.find(Name);
   require(I != Ranges.end() && I->getValue().size() == 1,
@@ -94,6 +99,11 @@ Annotations::ranges(llvm::StringRef Name) const {
   return {I->getValue().begin(), I->getValue().end()};
 }
 
+const llvm::StringMap<llvm::SmallVector<Annotations::Range, 1>> &
+Annotations::all_ranges() const {
+  return Ranges;
+}
+
 llvm::raw_ostream &llvm::operator<<(llvm::raw_ostream &O,
                                     const llvm::Annotations::Range &R) {
   return O << llvm::formatv("[{0}, {1})", R.Begin, R.End);
index b2970ad..c7bbb79 100644 (file)
@@ -9,10 +9,22 @@
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
 
+using ::testing::AllOf;
 using ::testing::ElementsAre;
 using ::testing::IsEmpty;
+using ::testing::ResultOf;
+using ::testing::UnorderedElementsAre;
 
 namespace {
+MATCHER_P2(pair, first_matcher, second_matcher, "") {
+  return testing::ExplainMatchResult(
+      AllOf(ResultOf([](const auto &entry) { return entry.getKey(); },
+                     first_matcher),
+            ResultOf([](const auto &entry) { return entry.getValue(); },
+                     second_matcher)),
+      arg, result_listener);
+}
+
 llvm::Annotations::Range range(size_t Begin, size_t End) {
   llvm::Annotations::Range R;
   R.Begin = Begin;
@@ -42,6 +54,17 @@ TEST(AnnotationsTest, Points) {
   EXPECT_THAT(llvm::Annotations("ab^^^cd").points(), ElementsAre(2u, 2u, 2u));
 }
 
+TEST(AnnotationsTest, AllPoints) {
+  // Multiple points.
+  EXPECT_THAT(llvm::Annotations("0$p1^123$p2^456$p1^$p1^78^9").all_points(),
+              UnorderedElementsAre(pair("", ElementsAre(9u)),
+                                   pair("p1", ElementsAre(1u, 7u, 7u)),
+                                   pair("p2", ElementsAre(4u))));
+
+  // No points.
+  EXPECT_THAT(llvm::Annotations("ab[[cd]]").all_points(), IsEmpty());
+}
+
 TEST(AnnotationsTest, Ranges) {
   // A single range.
   EXPECT_EQ(llvm::Annotations("[[a]]bc").range(), range(0, 1));
@@ -61,6 +84,20 @@ TEST(AnnotationsTest, Ranges) {
   EXPECT_THAT(llvm::Annotations("ab^c^defef").ranges(), IsEmpty());
 }
 
+TEST(AnnotationsTest, AllRanges) {
+  // Multiple ranges.
+  EXPECT_THAT(
+      llvm::Annotations("[[]]01$outer[[2[[[[$inner[[3]]]]]]456]]7$outer[[89]]")
+          .all_ranges(),
+      UnorderedElementsAre(
+          pair("", ElementsAre(range(0, 0), range(3, 4), range(3, 4))),
+          pair("outer", ElementsAre(range(2, 7), range(8, 10))),
+          pair("inner", ElementsAre(range(3, 4)))));
+
+  // No ranges.
+  EXPECT_THAT(llvm::Annotations("ab^c^defef").all_ranges(), IsEmpty());
+}
+
 TEST(AnnotationsTest, Nested) {
   llvm::Annotations Annotated("a[[f^oo^bar[[b[[a]]z]]]]bcdef");
   EXPECT_THAT(Annotated.points(), ElementsAre(2u, 4u));