LLVM 20.0.0git
Utility.h
Go to the documentation of this file.
1//===--- Utility.h -------------------*- mode:c++;eval:(read-only-mode) -*-===//
2// Do not edit! See README.txt.
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//
9// Provide some utility classes for use in the demangler.
10// There are two copies of this file in the source tree. The one in libcxxabi
11// is the original and the one in llvm is the copy. Use cp-to-llvm.sh to update
12// the copy. See README.txt for more details.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef DEMANGLE_UTILITY_H
17#define DEMANGLE_UTILITY_H
18
19#include "DemangleConfig.h"
20
21#include <array>
22#include <cstdint>
23#include <cstdlib>
24#include <cstring>
25#include <limits>
26#include <string_view>
27
29
30// Stream that AST nodes write their string representation into after the AST
31// has been parsed.
33 char *Buffer = nullptr;
34 size_t CurrentPosition = 0;
35 size_t BufferCapacity = 0;
36
37 // Ensure there are at least N more positions in the buffer.
38 void grow(size_t N) {
39 size_t Need = N + CurrentPosition;
40 if (Need > BufferCapacity) {
41 // Reduce the number of reallocations, with a bit of hysteresis. The
42 // number here is chosen so the first allocation will more-than-likely not
43 // allocate more than 1K.
44 Need += 1024 - 32;
45 BufferCapacity *= 2;
46 if (BufferCapacity < Need)
47 BufferCapacity = Need;
48 Buffer = static_cast<char *>(std::realloc(Buffer, BufferCapacity));
49 if (Buffer == nullptr)
50 std::abort();
51 }
52 }
53
54 OutputBuffer &writeUnsigned(uint64_t N, bool isNeg = false) {
55 std::array<char, 21> Temp;
56 char *TempPtr = Temp.data() + Temp.size();
57
58 // Output at least one character.
59 do {
60 *--TempPtr = char('0' + N % 10);
61 N /= 10;
62 } while (N);
63
64 // Add negative sign.
65 if (isNeg)
66 *--TempPtr = '-';
67
68 return operator+=(
69 std::string_view(TempPtr, Temp.data() + Temp.size() - TempPtr));
70 }
71
72public:
73 OutputBuffer(char *StartBuf, size_t Size)
74 : Buffer(StartBuf), BufferCapacity(Size) {}
75 OutputBuffer(char *StartBuf, size_t *SizePtr)
76 : OutputBuffer(StartBuf, StartBuf ? *SizePtr : 0) {}
77 OutputBuffer() = default;
78 // Non-copyable
79 OutputBuffer(const OutputBuffer &) = delete;
81
82 operator std::string_view() const {
83 return std::string_view(Buffer, CurrentPosition);
84 }
85
86 /// If a ParameterPackExpansion (or similar type) is encountered, the offset
87 /// into the pack that we're currently printing.
88 unsigned CurrentPackIndex = std::numeric_limits<unsigned>::max();
89 unsigned CurrentPackMax = std::numeric_limits<unsigned>::max();
90
91 /// When zero, we're printing template args and '>' needs to be parenthesized.
92 /// Use a counter so we can simply increment inside parentheses.
93 unsigned GtIsGt = 1;
94
95 bool isGtInsideTemplateArgs() const { return GtIsGt == 0; }
96
97 void printOpen(char Open = '(') {
98 GtIsGt++;
99 *this += Open;
100 }
101 void printClose(char Close = ')') {
102 GtIsGt--;
103 *this += Close;
104 }
105
106 OutputBuffer &operator+=(std::string_view R) {
107 if (size_t Size = R.size()) {
108 grow(Size);
109 std::memcpy(Buffer + CurrentPosition, &*R.begin(), Size);
110 CurrentPosition += Size;
111 }
112 return *this;
113 }
114
116 grow(1);
117 Buffer[CurrentPosition++] = C;
118 return *this;
119 }
120
121 OutputBuffer &prepend(std::string_view R) {
122 size_t Size = R.size();
123
124 grow(Size);
125 std::memmove(Buffer + Size, Buffer, CurrentPosition);
126 std::memcpy(Buffer, &*R.begin(), Size);
127 CurrentPosition += Size;
128
129 return *this;
130 }
131
132 OutputBuffer &operator<<(std::string_view R) { return (*this += R); }
133
134 OutputBuffer &operator<<(char C) { return (*this += C); }
135
137 return writeUnsigned(static_cast<unsigned long long>(std::abs(N)), N < 0);
138 }
139
140 OutputBuffer &operator<<(unsigned long long N) {
141 return writeUnsigned(N, false);
142 }
143
145 return this->operator<<(static_cast<long long>(N));
146 }
147
148 OutputBuffer &operator<<(unsigned long N) {
149 return this->operator<<(static_cast<unsigned long long>(N));
150 }
151
153 return this->operator<<(static_cast<long long>(N));
154 }
155
156 OutputBuffer &operator<<(unsigned int N) {
157 return this->operator<<(static_cast<unsigned long long>(N));
158 }
159
160 void insert(size_t Pos, const char *S, size_t N) {
161 DEMANGLE_ASSERT(Pos <= CurrentPosition, "");
162 if (N == 0)
163 return;
164 grow(N);
165 std::memmove(Buffer + Pos + N, Buffer + Pos, CurrentPosition - Pos);
166 std::memcpy(Buffer + Pos, S, N);
167 CurrentPosition += N;
168 }
169
170 size_t getCurrentPosition() const { return CurrentPosition; }
171 void setCurrentPosition(size_t NewPos) { CurrentPosition = NewPos; }
172
173 char back() const {
174 DEMANGLE_ASSERT(CurrentPosition, "");
175 return Buffer[CurrentPosition - 1];
176 }
177
178 bool empty() const { return CurrentPosition == 0; }
179
180 char *getBuffer() { return Buffer; }
181 char *getBufferEnd() { return Buffer + CurrentPosition - 1; }
182 size_t getBufferCapacity() const { return BufferCapacity; }
183};
184
185template <class T> class ScopedOverride {
186 T &Loc;
187 T Original;
188
189public:
190 ScopedOverride(T &Loc_) : ScopedOverride(Loc_, Loc_) {}
191
192 ScopedOverride(T &Loc_, T NewVal) : Loc(Loc_), Original(Loc_) {
193 Loc_ = std::move(NewVal);
194 }
195 ~ScopedOverride() { Loc = std::move(Original); }
196
199};
200
202
203#endif
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static bool isNeg(Value *V)
Returns true if the operation is a negation of V, and it works for both integers and floats.
#define DEMANGLE_NAMESPACE_END
#define DEMANGLE_ASSERT(__expr, __msg)
#define DEMANGLE_NAMESPACE_BEGIN
uint64_t Size
char * getBufferEnd()
Definition: Utility.h:181
OutputBuffer & operator<<(unsigned long long N)
Definition: Utility.h:140
OutputBuffer()=default
OutputBuffer(const OutputBuffer &)=delete
size_t getBufferCapacity() const
Definition: Utility.h:182
OutputBuffer & operator+=(std::string_view R)
Definition: Utility.h:106
bool empty() const
Definition: Utility.h:178
OutputBuffer(char *StartBuf, size_t Size)
Definition: Utility.h:73
unsigned GtIsGt
When zero, we're printing template args and '>' needs to be parenthesized.
Definition: Utility.h:93
OutputBuffer(char *StartBuf, size_t *SizePtr)
Definition: Utility.h:75
OutputBuffer & operator<<(char C)
Definition: Utility.h:134
char back() const
Definition: Utility.h:173
OutputBuffer & operator=(const OutputBuffer &)=delete
OutputBuffer & prepend(std::string_view R)
Definition: Utility.h:121
char * getBuffer()
Definition: Utility.h:180
unsigned CurrentPackMax
Definition: Utility.h:89
OutputBuffer & operator<<(std::string_view R)
Definition: Utility.h:132
unsigned CurrentPackIndex
If a ParameterPackExpansion (or similar type) is encountered, the offset into the pack that we're cur...
Definition: Utility.h:88
OutputBuffer & operator<<(long N)
Definition: Utility.h:144
OutputBuffer & operator<<(int N)
Definition: Utility.h:152
OutputBuffer & operator<<(long long N)
Definition: Utility.h:136
void printClose(char Close=')')
Definition: Utility.h:101
void setCurrentPosition(size_t NewPos)
Definition: Utility.h:171
bool isGtInsideTemplateArgs() const
Definition: Utility.h:95
OutputBuffer & operator+=(char C)
Definition: Utility.h:115
size_t getCurrentPosition() const
Definition: Utility.h:170
void printOpen(char Open='(')
Definition: Utility.h:97
void insert(size_t Pos, const char *S, size_t N)
Definition: Utility.h:160
OutputBuffer & operator<<(unsigned long N)
Definition: Utility.h:148
OutputBuffer & operator<<(unsigned int N)
Definition: Utility.h:156
ScopedOverride(T &Loc_, T NewVal)
Definition: Utility.h:192
ScopedOverride(T &Loc_)
Definition: Utility.h:190
ScopedOverride & operator=(const ScopedOverride &)=delete
ScopedOverride(const ScopedOverride &)=delete
#define N