/// 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.
/// 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;
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,
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);
#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;
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));
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));