xref: /freebsd-src/contrib/llvm-project/llvm/lib/Testing/Annotations/Annotations.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
1*bdd1243dSDimitry Andric //===--- Annotations.cpp - Annotated source code for unit tests --*- C++-*-===//
2*bdd1243dSDimitry Andric //
3*bdd1243dSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*bdd1243dSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*bdd1243dSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*bdd1243dSDimitry Andric //
7*bdd1243dSDimitry Andric //===----------------------------------------------------------------------===//
8*bdd1243dSDimitry Andric 
9*bdd1243dSDimitry Andric #include "llvm/Testing/Annotations/Annotations.h"
10*bdd1243dSDimitry Andric 
11*bdd1243dSDimitry Andric #include "llvm/ADT/StringExtras.h"
12*bdd1243dSDimitry Andric #include "llvm/Support/FormatVariadic.h"
13*bdd1243dSDimitry Andric #include "llvm/Support/raw_ostream.h"
14*bdd1243dSDimitry Andric 
15*bdd1243dSDimitry Andric using namespace llvm;
16*bdd1243dSDimitry Andric 
17*bdd1243dSDimitry Andric // Crash if the assertion fails, printing the message and testcase.
18*bdd1243dSDimitry Andric // More elegant error handling isn't needed for unit tests.
require(bool Assertion,const char * Msg,llvm::StringRef Code)19*bdd1243dSDimitry Andric static void require(bool Assertion, const char *Msg, llvm::StringRef Code) {
20*bdd1243dSDimitry Andric   if (!Assertion) {
21*bdd1243dSDimitry Andric     llvm::errs() << "Annotated testcase: " << Msg << "\n" << Code << "\n";
22*bdd1243dSDimitry Andric     llvm_unreachable("Annotated testcase assertion failed!");
23*bdd1243dSDimitry Andric   }
24*bdd1243dSDimitry Andric }
25*bdd1243dSDimitry Andric 
Annotations(llvm::StringRef Text)26*bdd1243dSDimitry Andric Annotations::Annotations(llvm::StringRef Text) {
27*bdd1243dSDimitry Andric   auto Require = [Text](bool Assertion, const char *Msg) {
28*bdd1243dSDimitry Andric     require(Assertion, Msg, Text);
29*bdd1243dSDimitry Andric   };
30*bdd1243dSDimitry Andric   std::optional<llvm::StringRef> Name;
31*bdd1243dSDimitry Andric   std::optional<llvm::StringRef> Payload;
32*bdd1243dSDimitry Andric   llvm::SmallVector<Annotation, 8> OpenRanges;
33*bdd1243dSDimitry Andric 
34*bdd1243dSDimitry Andric   Code.reserve(Text.size());
35*bdd1243dSDimitry Andric   while (!Text.empty()) {
36*bdd1243dSDimitry Andric     if (Text.consume_front("^")) {
37*bdd1243dSDimitry Andric       All.push_back(
38*bdd1243dSDimitry Andric           {Code.size(), size_t(-1), Name.value_or(""), Payload.value_or("")});
39*bdd1243dSDimitry Andric       Points[Name.value_or("")].push_back(All.size() - 1);
40*bdd1243dSDimitry Andric       Name = std::nullopt;
41*bdd1243dSDimitry Andric       Payload = std::nullopt;
42*bdd1243dSDimitry Andric       continue;
43*bdd1243dSDimitry Andric     }
44*bdd1243dSDimitry Andric     if (Text.consume_front("[[")) {
45*bdd1243dSDimitry Andric       OpenRanges.push_back(
46*bdd1243dSDimitry Andric           {Code.size(), size_t(-1), Name.value_or(""), Payload.value_or("")});
47*bdd1243dSDimitry Andric       Name = std::nullopt;
48*bdd1243dSDimitry Andric       Payload = std::nullopt;
49*bdd1243dSDimitry Andric       continue;
50*bdd1243dSDimitry Andric     }
51*bdd1243dSDimitry Andric     Require(!Name, "$name should be followed by ^ or [[");
52*bdd1243dSDimitry Andric     if (Text.consume_front("]]")) {
53*bdd1243dSDimitry Andric       Require(!OpenRanges.empty(), "unmatched ]]");
54*bdd1243dSDimitry Andric 
55*bdd1243dSDimitry Andric       const Annotation &NewRange = OpenRanges.back();
56*bdd1243dSDimitry Andric       All.push_back(
57*bdd1243dSDimitry Andric           {NewRange.Begin, Code.size(), NewRange.Name, NewRange.Payload});
58*bdd1243dSDimitry Andric       Ranges[NewRange.Name].push_back(All.size() - 1);
59*bdd1243dSDimitry Andric 
60*bdd1243dSDimitry Andric       OpenRanges.pop_back();
61*bdd1243dSDimitry Andric       continue;
62*bdd1243dSDimitry Andric     }
63*bdd1243dSDimitry Andric     if (Text.consume_front("$")) {
64*bdd1243dSDimitry Andric       Name =
65*bdd1243dSDimitry Andric           Text.take_while([](char C) { return llvm::isAlnum(C) || C == '_'; });
66*bdd1243dSDimitry Andric       Text = Text.drop_front(Name->size());
67*bdd1243dSDimitry Andric 
68*bdd1243dSDimitry Andric       if (Text.consume_front("(")) {
69*bdd1243dSDimitry Andric         Payload = Text.take_while([](char C) { return C != ')'; });
70*bdd1243dSDimitry Andric         Require(Text.size() > Payload->size(), "unterminated payload");
71*bdd1243dSDimitry Andric         Text = Text.drop_front(Payload->size() + 1);
72*bdd1243dSDimitry Andric       }
73*bdd1243dSDimitry Andric 
74*bdd1243dSDimitry Andric       continue;
75*bdd1243dSDimitry Andric     }
76*bdd1243dSDimitry Andric     Code.push_back(Text.front());
77*bdd1243dSDimitry Andric     Text = Text.drop_front();
78*bdd1243dSDimitry Andric   }
79*bdd1243dSDimitry Andric   Require(!Name, "unterminated $name");
80*bdd1243dSDimitry Andric   Require(OpenRanges.empty(), "unmatched [[");
81*bdd1243dSDimitry Andric }
82*bdd1243dSDimitry Andric 
point(llvm::StringRef Name) const83*bdd1243dSDimitry Andric size_t Annotations::point(llvm::StringRef Name) const {
84*bdd1243dSDimitry Andric   return pointWithPayload(Name).first;
85*bdd1243dSDimitry Andric }
86*bdd1243dSDimitry Andric 
87*bdd1243dSDimitry Andric std::pair<size_t, llvm::StringRef>
pointWithPayload(llvm::StringRef Name) const88*bdd1243dSDimitry Andric Annotations::pointWithPayload(llvm::StringRef Name) const {
89*bdd1243dSDimitry Andric   auto I = Points.find(Name);
90*bdd1243dSDimitry Andric   require(I != Points.end() && I->getValue().size() == 1,
91*bdd1243dSDimitry Andric           "expected exactly one point", Code);
92*bdd1243dSDimitry Andric   const Annotation &P = All[I->getValue()[0]];
93*bdd1243dSDimitry Andric   return {P.Begin, P.Payload};
94*bdd1243dSDimitry Andric }
95*bdd1243dSDimitry Andric 
points(llvm::StringRef Name) const96*bdd1243dSDimitry Andric std::vector<size_t> Annotations::points(llvm::StringRef Name) const {
97*bdd1243dSDimitry Andric   auto Pts = pointsWithPayload(Name);
98*bdd1243dSDimitry Andric   std::vector<size_t> Positions;
99*bdd1243dSDimitry Andric   Positions.reserve(Pts.size());
100*bdd1243dSDimitry Andric   for (const auto &[Point, Payload] : Pts)
101*bdd1243dSDimitry Andric     Positions.push_back(Point);
102*bdd1243dSDimitry Andric   return Positions;
103*bdd1243dSDimitry Andric }
104*bdd1243dSDimitry Andric 
105*bdd1243dSDimitry Andric std::vector<std::pair<size_t, llvm::StringRef>>
pointsWithPayload(llvm::StringRef Name) const106*bdd1243dSDimitry Andric Annotations::pointsWithPayload(llvm::StringRef Name) const {
107*bdd1243dSDimitry Andric   auto Iter = Points.find(Name);
108*bdd1243dSDimitry Andric   if (Iter == Points.end())
109*bdd1243dSDimitry Andric     return {};
110*bdd1243dSDimitry Andric 
111*bdd1243dSDimitry Andric   std::vector<std::pair<size_t, llvm::StringRef>> Res;
112*bdd1243dSDimitry Andric   Res.reserve(Iter->getValue().size());
113*bdd1243dSDimitry Andric   for (size_t I : Iter->getValue())
114*bdd1243dSDimitry Andric     Res.push_back({All[I].Begin, All[I].Payload});
115*bdd1243dSDimitry Andric 
116*bdd1243dSDimitry Andric   return Res;
117*bdd1243dSDimitry Andric }
118*bdd1243dSDimitry Andric 
all_points() const119*bdd1243dSDimitry Andric llvm::StringMap<llvm::SmallVector<size_t, 1>> Annotations::all_points() const {
120*bdd1243dSDimitry Andric   llvm::StringMap<llvm::SmallVector<size_t, 1>> Result;
121*bdd1243dSDimitry Andric   for (const auto &Name : Points.keys()) {
122*bdd1243dSDimitry Andric     auto Pts = points(Name);
123*bdd1243dSDimitry Andric     Result[Name] = {Pts.begin(), Pts.end()};
124*bdd1243dSDimitry Andric   }
125*bdd1243dSDimitry Andric   return Result;
126*bdd1243dSDimitry Andric }
127*bdd1243dSDimitry Andric 
range(llvm::StringRef Name) const128*bdd1243dSDimitry Andric Annotations::Range Annotations::range(llvm::StringRef Name) const {
129*bdd1243dSDimitry Andric   return rangeWithPayload(Name).first;
130*bdd1243dSDimitry Andric }
131*bdd1243dSDimitry Andric 
132*bdd1243dSDimitry Andric std::pair<Annotations::Range, llvm::StringRef>
rangeWithPayload(llvm::StringRef Name) const133*bdd1243dSDimitry Andric Annotations::rangeWithPayload(llvm::StringRef Name) const {
134*bdd1243dSDimitry Andric   auto I = Ranges.find(Name);
135*bdd1243dSDimitry Andric   require(I != Ranges.end() && I->getValue().size() == 1,
136*bdd1243dSDimitry Andric           "expected exactly one range", Code);
137*bdd1243dSDimitry Andric   const Annotation &R = All[I->getValue()[0]];
138*bdd1243dSDimitry Andric   return {{R.Begin, R.End}, R.Payload};
139*bdd1243dSDimitry Andric }
140*bdd1243dSDimitry Andric 
141*bdd1243dSDimitry Andric std::vector<Annotations::Range>
ranges(llvm::StringRef Name) const142*bdd1243dSDimitry Andric Annotations::ranges(llvm::StringRef Name) const {
143*bdd1243dSDimitry Andric   auto WithPayload = rangesWithPayload(Name);
144*bdd1243dSDimitry Andric   std::vector<Annotations::Range> Res;
145*bdd1243dSDimitry Andric   Res.reserve(WithPayload.size());
146*bdd1243dSDimitry Andric   for (const auto &[Range, Payload] : WithPayload)
147*bdd1243dSDimitry Andric     Res.push_back(Range);
148*bdd1243dSDimitry Andric   return Res;
149*bdd1243dSDimitry Andric }
150*bdd1243dSDimitry Andric std::vector<std::pair<Annotations::Range, llvm::StringRef>>
rangesWithPayload(llvm::StringRef Name) const151*bdd1243dSDimitry Andric Annotations::rangesWithPayload(llvm::StringRef Name) const {
152*bdd1243dSDimitry Andric   auto Iter = Ranges.find(Name);
153*bdd1243dSDimitry Andric   if (Iter == Ranges.end())
154*bdd1243dSDimitry Andric     return {};
155*bdd1243dSDimitry Andric 
156*bdd1243dSDimitry Andric   std::vector<std::pair<Annotations::Range, llvm::StringRef>> Res;
157*bdd1243dSDimitry Andric   Res.reserve(Iter->getValue().size());
158*bdd1243dSDimitry Andric   for (size_t I : Iter->getValue())
159*bdd1243dSDimitry Andric     Res.emplace_back(Annotations::Range{All[I].Begin, All[I].End},
160*bdd1243dSDimitry Andric                      All[I].Payload);
161*bdd1243dSDimitry Andric 
162*bdd1243dSDimitry Andric   return Res;
163*bdd1243dSDimitry Andric }
164*bdd1243dSDimitry Andric 
165*bdd1243dSDimitry Andric llvm::StringMap<llvm::SmallVector<Annotations::Range, 1>>
all_ranges() const166*bdd1243dSDimitry Andric Annotations::all_ranges() const {
167*bdd1243dSDimitry Andric   llvm::StringMap<llvm::SmallVector<Annotations::Range, 1>> Res;
168*bdd1243dSDimitry Andric   for (const llvm::StringRef &Name : Ranges.keys()) {
169*bdd1243dSDimitry Andric     auto R = ranges(Name);
170*bdd1243dSDimitry Andric     Res[Name] = {R.begin(), R.end()};
171*bdd1243dSDimitry Andric   }
172*bdd1243dSDimitry Andric   return Res;
173*bdd1243dSDimitry Andric }
174*bdd1243dSDimitry Andric 
operator <<(llvm::raw_ostream & O,const llvm::Annotations::Range & R)175*bdd1243dSDimitry Andric llvm::raw_ostream &llvm::operator<<(llvm::raw_ostream &O,
176*bdd1243dSDimitry Andric                                     const llvm::Annotations::Range &R) {
177*bdd1243dSDimitry Andric   return O << llvm::formatv("[{0}, {1})", R.Begin, R.End);
178*bdd1243dSDimitry Andric }
179