diff --git a/llvm/include/llvm/Testing/Support/Annotations.h b/llvm/include/llvm/Testing/Support/Annotations.h --- a/llvm/include/llvm/Testing/Support/Annotations.h +++ b/llvm/include/llvm/Testing/Support/Annotations.h @@ -72,6 +72,10 @@ /// 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 @@ /// 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 --- a/llvm/lib/Testing/Support/Annotations.cpp +++ b/llvm/lib/Testing/Support/Annotations.cpp @@ -79,6 +79,11 @@ 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 @@ 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 --- 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 @@ 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 @@ 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));