aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/Testing/Annotations/AnnotationsTest.cpp
blob: d8c42888e5547cf56efaf48177480afd46cf66e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//===----- unittests/AnnotationsTest.cpp ----------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/Testing/Annotations/Annotations.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

using ::testing::AllOf;
using ::testing::ElementsAre;
using ::testing::IsEmpty;
using ::testing::Pair;
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;
  R.End = End;
  return R;
}

TEST(AnnotationsTest, CleanedCode) {
  EXPECT_EQ(llvm::Annotations("foo^bar$nnn[[baz$^[[qux]]]]").code(),
            "foobarbazqux");
}

TEST(AnnotationsTest, Points) {
  // A single point.
  EXPECT_EQ(llvm::Annotations("^ab").point(), 0u);
  EXPECT_EQ(llvm::Annotations("a^b").point(), 1u);
  EXPECT_EQ(llvm::Annotations("ab^").point(), 2u);

  // Multiple points.
  EXPECT_THAT(llvm::Annotations("^a^bc^d^").points(),
              ElementsAre(0u, 1u, 3u, 4u));

  // No points.
  EXPECT_THAT(llvm::Annotations("ab[[cd]]").points(), IsEmpty());

  // Consecutive 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));
  EXPECT_EQ(llvm::Annotations("a[[bc]]d").range(), range(1, 3));
  EXPECT_EQ(llvm::Annotations("ab[[cd]]").range(), range(2, 4));

  // Empty range.
  EXPECT_EQ(llvm::Annotations("[[]]ab").range(), range(0, 0));
  EXPECT_EQ(llvm::Annotations("a[[]]b").range(), range(1, 1));
  EXPECT_EQ(llvm::Annotations("ab[[]]").range(), range(2, 2));

  // Multiple ranges.
  EXPECT_THAT(llvm::Annotations("[[a]][[b]]cd[[ef]]ef").ranges(),
              ElementsAre(range(0, 1), range(1, 2), range(4, 6)));

  // No 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));
  EXPECT_THAT(Annotated.ranges(),
              ElementsAre(range(8, 9), range(7, 10), range(1, 10)));
}

TEST(AnnotationsTest, Payload) {
  // // A single unnamed point or range with unspecified payload
  EXPECT_THAT(llvm::Annotations("a$^b").pointWithPayload(), Pair(1u, ""));
  EXPECT_THAT(llvm::Annotations("a$[[b]]cdef").rangeWithPayload(),
              Pair(range(1, 2), ""));

  // A single unnamed point or range with empty payload
  EXPECT_THAT(llvm::Annotations("a$()^b").pointWithPayload(), Pair(1u, ""));
  EXPECT_THAT(llvm::Annotations("a$()[[b]]cdef").rangeWithPayload(),
              Pair(range(1, 2), ""));

  // A single unnamed point or range with payload.
  EXPECT_THAT(llvm::Annotations("a$(foo)^b").pointWithPayload(),
              Pair(1u, "foo"));
  EXPECT_THAT(llvm::Annotations("a$(foo)[[b]]cdef").rangeWithPayload(),
              Pair(range(1, 2), "foo"));

  // A single named point or range with payload
  EXPECT_THAT(llvm::Annotations("a$name(foo)^b").pointWithPayload("name"),
              Pair(1u, "foo"));
  EXPECT_THAT(
      llvm::Annotations("a$name(foo)[[b]]cdef").rangeWithPayload("name"),
      Pair(range(1, 2), "foo"));

  // Multiple named points with payload.
  llvm::Annotations Annotated("a$p1(p1)^bcd$p2(p2)^123$p1^345");
  EXPECT_THAT(Annotated.points(), IsEmpty());
  EXPECT_THAT(Annotated.pointsWithPayload("p1"),
              ElementsAre(Pair(1u, "p1"), Pair(7u, "")));
  EXPECT_THAT(Annotated.pointWithPayload("p2"), Pair(4u, "p2"));
}

TEST(AnnotationsTest, Named) {
  // A single named point or range.
  EXPECT_EQ(llvm::Annotations("a$foo^b").point("foo"), 1u);
  EXPECT_EQ(llvm::Annotations("a$foo[[b]]cdef").range("foo"), range(1, 2));

  // Empty names should also work.
  EXPECT_EQ(llvm::Annotations("a$^b").point(""), 1u);
  EXPECT_EQ(llvm::Annotations("a$[[b]]cdef").range(""), range(1, 2));

  // Multiple named points.
  llvm::Annotations Annotated("a$p1^bcd$p2^123$p1^345");
  EXPECT_THAT(Annotated.points(), IsEmpty());
  EXPECT_THAT(Annotated.points("p1"), ElementsAre(1u, 7u));
  EXPECT_EQ(Annotated.point("p2"), 4u);
}

TEST(AnnotationsTest, Errors) {
  // Annotations use llvm_unreachable, it will only crash in debug mode.
#ifndef NDEBUG
  // point() and range() crash on zero or multiple ranges.
  EXPECT_DEATH(llvm::Annotations("ab[[c]]def").point(),
               "expected exactly one point");
  EXPECT_DEATH(llvm::Annotations("a^b^cdef").point(),
               "expected exactly one point");

  EXPECT_DEATH(llvm::Annotations("a^bcdef").range(),
               "expected exactly one range");
  EXPECT_DEATH(llvm::Annotations("a[[b]]c[[d]]ef").range(),
               "expected exactly one range");

  EXPECT_DEATH(llvm::Annotations("$foo^a$foo^a").point("foo"),
               "expected exactly one point");
  EXPECT_DEATH(llvm::Annotations("$foo[[a]]bc$foo[[a]]").range("foo"),
               "expected exactly one range");

  // Parsing failures.
  EXPECT_DEATH(llvm::Annotations("ff[[fdfd"), "unmatched \\[\\[");
  EXPECT_DEATH(llvm::Annotations("ff[[fdjsfjd]]xxx]]"), "unmatched \\]\\]");
  EXPECT_DEATH(llvm::Annotations("ff$fdsfd"), "unterminated \\$name");
  EXPECT_DEATH(llvm::Annotations("ff$("), "unterminated payload");
  EXPECT_DEATH(llvm::Annotations("ff$name("), "unterminated payload");
#endif
}
} // namespace