LLVM 23.0.0git
Annotations.cpp
Go to the documentation of this file.
1//===--- Annotations.cpp - Annotated source code for unit tests --*- C++-*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10
14
15using namespace llvm;
16
17// Crash if the assertion fails, printing the message and testcase.
18// More elegant error handling isn't needed for unit tests.
19static void require(bool Assertion, const llvm::Twine &Msg,
20 llvm::StringRef Code) {
21 if (!Assertion) {
22 llvm::errs() << "Annotated testcase: " << Msg << "\n" << Code << "\n";
23 llvm_unreachable("Annotated testcase assertion failed!");
24 }
25}
26
28
30 assert(!Markers.Point.empty() && "point marker cannot be empty");
31 assert(!Markers.Name.empty() && "name marker cannot be empty");
32 assert(!Markers.RangeBegin.empty() && "range begin marker cannot be empty");
33 assert(!Markers.RangeEnd.empty() && "range end marker cannot be empty");
34
37 "point and range begin markers cannot be prefixes of each other");
40 "point and range end markers cannot be prefixes of each other");
43 "point and name markers cannot be prefixes of each other");
46 "range begin and range end markers cannot be prefixes of each other");
49 "range begin and name markers cannot be prefixes of each other");
52 "range end and name markers cannot be prefixes of each other");
53
54 auto Require = [Text](bool Assertion, const llvm::Twine &Msg) {
55 require(Assertion, Msg, Text);
56 };
57 std::optional<llvm::StringRef> Name;
58 std::optional<llvm::StringRef> Payload;
60
61 Code.reserve(Text.size());
62 while (!Text.empty()) {
63 if (Text.consume_front(Markers.Point)) {
64 All.push_back(
65 {Code.size(), size_t(-1), Name.value_or(""), Payload.value_or("")});
66 Points[Name.value_or("")].push_back(All.size() - 1);
67 Name = std::nullopt;
68 Payload = std::nullopt;
69 continue;
70 }
71 if (Text.consume_front(Markers.RangeBegin)) {
72 OpenRanges.push_back(
73 {Code.size(), size_t(-1), Name.value_or(""), Payload.value_or("")});
74 Name = std::nullopt;
75 Payload = std::nullopt;
76 continue;
77 }
78 Require(!Name, Markers.Name + "name should be followed by " +
79 Markers.Point + " or " + Markers.RangeBegin);
80 if (Text.consume_front(Markers.RangeEnd)) {
81 Require(!OpenRanges.empty(), "unmatched " + Markers.RangeEnd);
82
83 const Annotation &NewRange = OpenRanges.back();
84 All.push_back(
85 {NewRange.Begin, Code.size(), NewRange.Name, NewRange.Payload});
86 Ranges[NewRange.Name].push_back(All.size() - 1);
87
88 OpenRanges.pop_back();
89 continue;
90 }
91 if (Text.consume_front(Markers.Name)) {
92 Name =
93 Text.take_while([](char C) { return llvm::isAlnum(C) || C == '_'; });
94 Text = Text.drop_front(Name->size());
95
96 if (Text.consume_front("(")) {
97 Payload = Text.take_while([](char C) { return C != ')'; });
98 Require(Text.size() > Payload->size(), "unterminated payload");
99 Text = Text.drop_front(Payload->size() + 1);
100 }
101
102 continue;
103 }
104 Code.push_back(Text.front());
105 Text = Text.drop_front();
106 }
107 Require(!Name, "unterminated " + Markers.Name + "name");
108 Require(OpenRanges.empty(), "unmatched " + Markers.RangeBegin);
109}
110
112 return pointWithPayload(Name).first;
113}
114
115std::pair<size_t, llvm::StringRef>
117 auto I = Points.find(Name);
118 require(I != Points.end() && I->getValue().size() == 1,
119 "expected exactly one point", Code);
120 const Annotation &P = All[I->getValue()[0]];
121 return {P.Begin, P.Payload};
122}
123
124std::vector<size_t> Annotations::points(llvm::StringRef Name) const {
125 auto Pts = pointsWithPayload(Name);
126 std::vector<size_t> Positions;
127 Positions.reserve(Pts.size());
128 for (const auto &[Point, Payload] : Pts)
129 Positions.push_back(Point);
130 return Positions;
131}
132
133std::vector<std::pair<size_t, llvm::StringRef>>
135 auto Iter = Points.find(Name);
136 if (Iter == Points.end())
137 return {};
138
139 std::vector<std::pair<size_t, llvm::StringRef>> Res;
140 Res.reserve(Iter->getValue().size());
141 for (size_t I : Iter->getValue())
142 Res.push_back({All[I].Begin, All[I].Payload});
143
144 return Res;
145}
146
149 for (const auto &Name : Points.keys()) {
150 auto Pts = points(Name);
151 Result[Name] = {Pts.begin(), Pts.end()};
152 }
153 return Result;
154}
155
159
160std::pair<Annotations::Range, llvm::StringRef>
162 auto I = Ranges.find(Name);
163 require(I != Ranges.end() && I->getValue().size() == 1,
164 "expected exactly one range", Code);
165 const Annotation &R = All[I->getValue()[0]];
166 return {{R.Begin, R.End}, R.Payload};
167}
168
169std::vector<Annotations::Range>
171 auto WithPayload = rangesWithPayload(Name);
172 std::vector<Annotations::Range> Res;
173 Res.reserve(WithPayload.size());
174 for (const auto &[Range, Payload] : WithPayload)
175 Res.push_back(Range);
176 return Res;
177}
178std::vector<std::pair<Annotations::Range, llvm::StringRef>>
180 auto Iter = Ranges.find(Name);
181 if (Iter == Ranges.end())
182 return {};
183
184 std::vector<std::pair<Annotations::Range, llvm::StringRef>> Res;
185 Res.reserve(Iter->getValue().size());
186 for (size_t I : Iter->getValue())
187 Res.emplace_back(Annotations::Range{All[I].Begin, All[I].End},
188 All[I].Payload);
189
190 return Res;
191}
192
196 for (const llvm::StringRef &Name : Ranges.keys()) {
197 auto R = ranges(Name);
198 Res[Name] = {R.begin(), R.end()};
199 }
200 return Res;
201}
202
204 const llvm::Annotations::Range &R) {
205 return O << llvm::formatv("[{0}, {1})", R.Begin, R.End);
206}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static void require(bool Assertion, const llvm::Twine &Msg, llvm::StringRef Code)
#define I(x, y, z)
Definition MD5.cpp:57
#define P(N)
This file contains some functions that are useful when dealing with strings.
Annotations(llvm::StringRef Text)
Parses the annotations from Text. Crashes if it's malformed.
std::vector< std::pair< size_t, llvm::StringRef > > pointsWithPayload(llvm::StringRef Name="") const
Returns the positions and payloads (if any) of all points named Name.
Range range(llvm::StringRef Name="") const
Returns the location of the range marked by [[ ]] (or $name[[ ]]).
size_t point(llvm::StringRef Name="") const
Returns the position of the point marked by ^ (or $name^) in the text.
std::pair< size_t, llvm::StringRef > pointWithPayload(llvm::StringRef Name="") const
Returns the position of the point with Name and its payload (if any).
llvm::StringMap< llvm::SmallVector< size_t, 1 > > all_points() const
Returns the mapping of all names of points marked in the text to their position.
std::pair< Range, llvm::StringRef > rangeWithPayload(llvm::StringRef Name="") const
Returns the location and payload of the range marked by [[ ]] (or $name(payload)[[ ]]).
std::vector< Range > ranges(llvm::StringRef Name="") const
Returns the location of all ranges marked by [[ ]] (or $name[[ ]]).
std::vector< std::pair< Range, llvm::StringRef > > rangesWithPayload(llvm::StringRef Name="") const
Returns the location of all ranges marked by [[ ]] (or $name(payload)[[ ]]).
llvm::StringMap< llvm::SmallVector< Range, 1 > > all_ranges() const
Returns the mapping of all names of ranges marked in the text to their location.
std::vector< size_t > points(llvm::StringRef Name="") const
Returns the position of all points marked by ^ (or $name^) in the text.
void reserve(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition StringMap.h:133
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition StringRef.h:258
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
bool isAlnum(char C)
Checks whether character C is either a decimal digit or an uppercase or lowercase letter as classifie...
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Markers used to denote points, names/payloads and ranges in the annotated text.
Definition Annotations.h:83
llvm::StringRef RangeEnd
Definition Annotations.h:87
llvm::StringRef RangeBegin
Definition Annotations.h:86
Two offsets pointing to a continuous substring.
Definition Annotations.h:71