From 86118ec2d0e9cf67a1036930d8b3a31a4e0b1744 Mon Sep 17 00:00:00 2001
From: Eric Li
Date: Fri, 16 Sep 2022 16:07:26 -0400
Subject: [PATCH] [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
---
.../llvm/Testing/Support/Annotations.h | 8 ++++
llvm/lib/Testing/Support/Annotations.cpp | 10 +++++
llvm/unittests/Support/AnnotationsTest.cpp | 37 +++++++++++++++++++
3 files changed, 55 insertions(+)
diff --git a/llvm/include/llvm/Testing/Support/Annotations.h b/llvm/include/llvm/Testing/Support/Annotations.h
index 4e442269600d..1cac1f13618f 100644
--- a/llvm/include/llvm/Testing/Support/Annotations.h
+++ b/llvm/include/llvm/Testing/Support/Annotations.h
@@ -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 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> &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 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> &all_ranges() const;
private:
std::string Code;
diff --git a/llvm/lib/Testing/Support/Annotations.cpp b/llvm/lib/Testing/Support/Annotations.cpp
index 557b6cdf98ce..388215dda181 100644
--- a/llvm/lib/Testing/Support/Annotations.cpp
+++ b/llvm/lib/Testing/Support/Annotations.cpp
@@ -79,6 +79,11 @@ std::vector Annotations::points(llvm::StringRef Name) const {
return {I->getValue().begin(), I->getValue().end()};
}
+const llvm::StringMap> &
+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> &
+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 b2970adc41cb..c7bbb79db800 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));