LLVM  16.0.0git
Annotations.h
Go to the documentation of this file.
1 //===--- Annotations.h - Annotated source code for 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 #ifndef LLVM_TESTING_SUPPORT_ANNOTATIONS_H
9 #define LLVM_TESTING_SUPPORT_ANNOTATIONS_H
10 
11 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/ADT/StringMap.h"
13 #include "llvm/ADT/StringRef.h"
14 #include <tuple>
15 #include <vector>
16 
17 namespace llvm {
18 
19 class raw_ostream;
20 
21 /// Annotations lets you mark points and ranges inside source code, for tests:
22 ///
23 /// Annotations Example(R"cpp(
24 /// int complete() { x.pri^ } // ^ indicates a point
25 /// void err() { [["hello" == 42]]; } // [[this is a range]]
26 /// $definition^class Foo{}; // points can be named: "definition"
27 /// $fail[[static_assert(false, "")]] // ranges can be named too: "fail"
28 /// )cpp");
29 ///
30 /// StringRef Code = Example.code(); // annotations stripped.
31 /// std::vector<size_t> PP = Example.points(); // all unnamed points
32 /// size_t P = Example.point(); // there must be exactly one
33 /// llvm::Range R = Example.range("fail"); // find named ranges
34 ///
35 /// Points/ranges are coordinated into `code()` which is stripped of
36 /// annotations.
37 ///
38 /// Ranges may be nested (and points can be inside ranges), but there's no way
39 /// to define general overlapping ranges.
40 ///
41 /// FIXME: the choice of the marking syntax makes it impossible to represent
42 /// some of the C++ and Objective C constructs (including common ones
43 /// like C++ attributes). We can fix this by:
44 /// 1. introducing an escaping mechanism for the special characters,
45 /// 2. making characters for marking points and ranges configurable,
46 /// 3. changing the syntax to something less commonly used,
47 /// 4. ...
48 class Annotations {
49 public:
50  /// Two offsets pointing to a continuous substring. End is not included, i.e.
51  /// represents a half-open range.
52  struct Range {
53  size_t Begin = 0;
54  size_t End = 0;
55 
56  friend bool operator==(const Range &L, const Range &R) {
57  return std::tie(L.Begin, L.End) == std::tie(R.Begin, R.End);
58  }
59  friend bool operator!=(const Range &L, const Range &R) { return !(L == R); }
60  };
61 
62  /// Parses the annotations from Text. Crashes if it's malformed.
64 
65  /// The input text with all annotations stripped.
66  /// All points and ranges are relative to this stripped text.
67  llvm::StringRef code() const { return Code; }
68 
69  /// Returns the position of the point marked by ^ (or $name^) in the text.
70  /// Crashes if there isn't exactly one.
71  size_t point(llvm::StringRef Name = "") const;
72  /// Returns the position of all points marked by ^ (or $name^) in the text.
73  /// Order matches the order within the text.
74  std::vector<size_t> points(llvm::StringRef Name = "") const;
75  /// Returns the mapping of all names of points marked in the text to their
76  /// position. Unnamed points are mapped to the empty string. The positions are
77  /// sorted.
79 
80  /// Returns the location of the range marked by [[ ]] (or $name[[ ]]).
81  /// Crashes if there isn't exactly one.
82  Range range(llvm::StringRef Name = "") const;
83  /// Returns the location of all ranges marked by [[ ]] (or $name[[ ]]).
84  /// They are ordered by start position within the text.
85  std::vector<Range> ranges(llvm::StringRef Name = "") const;
86  /// Returns the mapping of all names of ranges marked in the text to their
87  /// location. Unnamed ranges are mapped to the empty string. The ranges are
88  /// sorted by their start position.
90 
91 private:
92  std::string Code;
95 };
96 
98  const llvm::Annotations::Range &R);
99 
100 } // namespace llvm
101 
102 #endif
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::Annotations::Range::Begin
size_t Begin
Definition: Annotations.h:53
StringRef.h
llvm::Annotations::point
size_t point(llvm::StringRef Name="") const
Returns the position of the point marked by ^ (or $name^) in the text.
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::Annotations::Range::End
size_t End
Definition: Annotations.h:54
llvm::Annotations::ranges
std::vector< Range > ranges(llvm::StringRef Name="") const
Returns the location of all ranges marked by [[ ]] (or $name[[ ]]).
llvm::Annotations
Annotations lets you mark points and ranges inside source code, for tests:
Definition: Annotations.h:48
llvm::Annotations::Range::operator==
friend bool operator==(const Range &L, const Range &R)
Definition: Annotations.h:56
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
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::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:264
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::Annotations::Range::operator!=
friend bool operator!=(const Range &L, const Range &R)
Definition: Annotations.h:59
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
llvm::Annotations::code
llvm::StringRef code() const
The input text with all annotations stripped.
Definition: Annotations.h:67
SmallVector.h
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.
StringMap.h
llvm::Annotations::range
Range range(llvm::StringRef Name="") const
Returns the location of the range marked by [[ ]] (or $name[[ ]]).