[llvm] 86118ec - [Support] Provide access to the full mapping in llvm::Annotations

Eric Li via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 20 08:08:54 PDT 2022


Author: Eric Li
Date: 2022-09-20T11:06:21-04:00
New Revision: 86118ec2d0e9cf67a1036930d8b3a31a4e0b1744

URL: https://github.com/llvm/llvm-project/commit/86118ec2d0e9cf67a1036930d8b3a31a4e0b1744
DIFF: https://github.com/llvm/llvm-project/commit/86118ec2d0e9cf67a1036930d8b3a31a4e0b1744.diff

LOG: [Support] Provide access to the full mapping in llvm::Annotations

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Testing/Support/Annotations.h b/llvm/include/llvm/Testing/Support/Annotations.h
index 4e442269600d0..1cac1f13618f7 100644
--- a/llvm/include/llvm/Testing/Support/Annotations.h
+++ b/llvm/include/llvm/Testing/Support/Annotations.h
@@ -72,6 +72,10 @@ class Annotations {
   /// 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 @@ class Annotations {
   /// 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;

diff  --git a/llvm/lib/Testing/Support/Annotations.cpp b/llvm/lib/Testing/Support/Annotations.cpp
index 557b6cdf98ce2..388215dda1813 100644
--- a/llvm/lib/Testing/Support/Annotations.cpp
+++ b/llvm/lib/Testing/Support/Annotations.cpp
@@ -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);

diff  --git a/llvm/unittests/Support/AnnotationsTest.cpp b/llvm/unittests/Support/AnnotationsTest.cpp
index b2970adc41cbe..c7bbb79db800c 100644
--- a/llvm/unittests/Support/AnnotationsTest.cpp
+++ b/llvm/unittests/Support/AnnotationsTest.cpp
@@ -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));


        


More information about the llvm-commits mailing list