LLVM  16.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 
11 #include "llvm/ADT/StringExtras.h"
14 
15 using 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.
19 static void require(bool Assertion, const char *Msg, llvm::StringRef Code) {
20  if (!Assertion) {
21  llvm::errs() << "Annotated testcase: " << Msg << "\n" << Code << "\n";
22  llvm_unreachable("Annotated testcase assertion failed!");
23  }
24 }
25 
27  auto Require = [Text](bool Assertion, const char *Msg) {
28  require(Assertion, Msg, Text);
29  };
32 
33  Code.reserve(Text.size());
34  while (!Text.empty()) {
35  if (Text.consume_front("^")) {
36  Points[Name.value_or("")].push_back(Code.size());
37  Name = llvm::None;
38  continue;
39  }
40  if (Text.consume_front("[[")) {
41  OpenRanges.emplace_back(Name.value_or(""), Code.size());
42  Name = llvm::None;
43  continue;
44  }
45  Require(!Name, "$name should be followed by ^ or [[");
46  if (Text.consume_front("]]")) {
47  Require(!OpenRanges.empty(), "unmatched ]]");
48  Range R;
49  R.Begin = OpenRanges.back().second;
50  R.End = Code.size();
51  Ranges[OpenRanges.back().first].push_back(R);
52  OpenRanges.pop_back();
53  continue;
54  }
55  if (Text.consume_front("$")) {
56  Name =
57  Text.take_while([](char C) { return llvm::isAlnum(C) || C == '_'; });
58  Text = Text.drop_front(Name->size());
59  continue;
60  }
61  Code.push_back(Text.front());
62  Text = Text.drop_front();
63  }
64  Require(!Name, "unterminated $name");
65  Require(OpenRanges.empty(), "unmatched [[");
66 }
67 
69  auto I = Points.find(Name);
70  require(I != Points.end() && I->getValue().size() == 1,
71  "expected exactly one point", Code);
72  return I->getValue()[0];
73 }
74 
75 std::vector<size_t> Annotations::points(llvm::StringRef Name) const {
76  auto I = Points.find(Name);
77  if (I == Points.end())
78  return {};
79  return {I->getValue().begin(), I->getValue().end()};
80 }
81 
84  return Points;
85 }
86 
88  auto I = Ranges.find(Name);
89  require(I != Ranges.end() && I->getValue().size() == 1,
90  "expected exactly one range", Code);
91  return I->getValue()[0];
92 }
93 
94 std::vector<Annotations::Range>
96  auto I = Ranges.find(Name);
97  if (I == Ranges.end())
98  return {};
99  return {I->getValue().begin(), I->getValue().end()};
100 }
101 
103 Annotations::all_ranges() const {
104  return Ranges;
105 }
106 
108  const llvm::Annotations::Range &R) {
109  return O << llvm::formatv("[{0}, {1})", R.Begin, R.End);
110 }
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::StringRef::front
char front() const
front - Get the first character in the string.
Definition: StringRef.h:140
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1181
llvm::Annotations::point
size_t point(llvm::StringRef Name="") const
Returns the position of the point marked by ^ (or $name^) in the text.
llvm::StringRef::consume_front
bool consume_front(StringRef Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
Definition: StringRef.h:624
llvm::StringMap::end
iterator end()
Definition: StringMap.h:204
llvm::Annotations::points
std::vector< size_t > points(llvm::StringRef Name="") const
Returns the position of all points marked by ^ (or $name^) in the text.
llvm::Optional< llvm::StringRef >
llvm::errs
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Definition: raw_ostream.cpp:891
llvm::StringMap::find
iterator find(StringRef Key)
Definition: StringMap.h:217
llvm::Annotations::ranges
std::vector< Range > ranges(llvm::StringRef Name="") const
Returns the location of all ranges marked by [[ ]] (or $name[[ ]]).
llvm::RISCVFenceField::R
@ R
Definition: RISCVBaseInfo.h:265
llvm::formatv
auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
Definition: FormatVariadic.h:251
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::Annotations::Annotations
Annotations(llvm::StringRef Text)
Parses the annotations from Text. Crashes if it's malformed.
Definition: Annotations.cpp:26
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
llvm::operator<<
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Definition: APFixedPoint.h:230
FormatVariadic.h
llvm::None
const NoneType None
Definition: None.h:24
llvm::StringMap
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:110
llvm::Annotations::Range
Two offsets pointing to a continuous substring.
Definition: Annotations.h:52
llvm::StringRef::empty
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:264
I
#define I(x, y, z)
Definition: MD5.cpp:58
StringExtras.h
Annotations.h
llvm::Annotations::all_points
const 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.
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
llvm::AMDGPU::SendMsg::Msg
const CustomOperand< const MCSubtargetInfo & > Msg[]
Definition: AMDGPUAsmUtils.cpp:39
llvm::StringRef::size
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
require
static void require(bool Assertion, const char *Msg, llvm::StringRef Code)
Definition: Annotations.cpp:19
llvm::StringRef::take_while
StringRef take_while(function_ref< bool(char)> F) const
Return the longest prefix of 'this' such that every character in the prefix satisfies the given predi...
Definition: StringRef.h:586
llvm::Annotations::all_ranges
const llvm::StringMap< llvm::SmallVector< Range, 1 > > & all_ranges() const
Returns the mapping of all names of ranges marked in the text to their location.
llvm::Annotations::range
Range range(llvm::StringRef Name="") const
Returns the location of the range marked by [[ ]] (or $name[[ ]]).
raw_ostream.h
llvm::StringRef::drop_front
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition: StringRef.h:598
llvm::SmallVectorImpl::emplace_back
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:923