Skip to content

Commit 8526be4

Browse files
committed
fixup! add hooks for insert/prepend/setCurrentPosition
1 parent 55a231d commit 8526be4

File tree

4 files changed

+80
-11
lines changed

4 files changed

+80
-11
lines changed

libcxxabi/src/demangle/Utility.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ class OutputBuffer {
9393
virtual void printLeft(const Node &N);
9494
virtual void printRight(const Node &N);
9595

96+
/// Called when we write to this object anywhere other than the end.
97+
virtual void notifyInsertion(size_t /*Position*/, size_t /*Count*/) {}
98+
99+
/// Called when we reset the \c CurrentPosition of this object.
100+
virtual void notifyPositionChanged(size_t /*OldPos*/, size_t /*NewPos*/) {}
101+
96102
/// If a ParameterPackExpansion (or similar type) is encountered, the offset
97103
/// into the pack that we're currently printing.
98104
unsigned CurrentPackIndex = std::numeric_limits<unsigned>::max();
@@ -131,6 +137,8 @@ class OutputBuffer {
131137
OutputBuffer &prepend(std::string_view R) {
132138
size_t Size = R.size();
133139

140+
notifyInsertion(/*Position=*/0, /*Count=*/Size);
141+
134142
grow(Size);
135143
std::memmove(Buffer + Size, Buffer, CurrentPosition);
136144
std::memcpy(Buffer, &*R.begin(), Size);
@@ -171,14 +179,20 @@ class OutputBuffer {
171179
DEMANGLE_ASSERT(Pos <= CurrentPosition, "");
172180
if (N == 0)
173181
return;
182+
183+
notifyInsertion(Pos, N);
184+
174185
grow(N);
175186
std::memmove(Buffer + Pos + N, Buffer + Pos, CurrentPosition - Pos);
176187
std::memcpy(Buffer + Pos, S, N);
177188
CurrentPosition += N;
178189
}
179190

180191
size_t getCurrentPosition() const { return CurrentPosition; }
181-
void setCurrentPosition(size_t NewPos) { CurrentPosition = NewPos; }
192+
void setCurrentPosition(size_t NewPos) {
193+
notifyPositionChanged(CurrentPosition, NewPos);
194+
CurrentPosition = NewPos;
195+
}
182196

183197
char back() const {
184198
DEMANGLE_ASSERT(CurrentPosition, "");

llvm/include/llvm/Demangle/ItaniumDemangle.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@
3838
DEMANGLE_NAMESPACE_BEGIN
3939

4040
template <class T, size_t N> class PODSmallVector {
41-
static_assert(std::is_trivial<T>::value,
42-
"T is required to be a trivial type");
41+
static_assert(std::is_trivially_copyable<T>::value,
42+
"T is required to be a trivially copyable type");
43+
static_assert(std::is_trivially_default_constructible<T>::value,
44+
"T is required to be trivially default constructible");
4345
T *First = nullptr;
4446
T *Last = nullptr;
4547
T *Cap = nullptr;
@@ -5752,14 +5754,16 @@ struct FloatData<double>
57525754
template <>
57535755
struct FloatData<long double>
57545756
{
5755-
#if defined(__mips__) && defined(__mips_n64) || defined(__aarch64__) || \
5756-
defined(__wasm__) || defined(__riscv) || defined(__loongarch__) || \
5757-
defined(__ve__)
5758-
static const size_t mangled_size = 32;
5759-
#elif defined(__arm__) || defined(__mips__) || defined(__hexagon__)
5760-
static const size_t mangled_size = 16;
5757+
#if __LDBL_MANT_DIG__ == 113 || __LDBL_MANT_DIG__ == 106
5758+
static const size_t mangled_size = 32;
5759+
#elif __LDBL_MANT_DIG__ == 53 || defined(_MSC_VER)
5760+
// MSVC doesn't define __LDBL_MANT_DIG__, but it has long double equal to
5761+
// regular double on all current architectures.
5762+
static const size_t mangled_size = 16;
5763+
#elif __LDBL_MANT_DIG__ == 64
5764+
static const size_t mangled_size = 20;
57615765
#else
5762-
static const size_t mangled_size = 20; // May need to be adjusted to 16 or 24 on other platforms
5766+
#error Unknown size for __LDBL_MANT_DIG__
57635767
#endif
57645768
// `-0x1.ffffffffffffffffffffffffffffp+16383` + 'L' + '\0' == 42 bytes.
57655769
// 28 'f's * 4 bits == 112 bits, which is the number of mantissa bits.

llvm/include/llvm/Demangle/Utility.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ class OutputBuffer {
9393
virtual void printLeft(const Node &N);
9494
virtual void printRight(const Node &N);
9595

96+
/// Called when we write to this object anywhere other than the end.
97+
virtual void notifyInsertion(size_t /*Position*/, size_t /*Count*/) {}
98+
99+
/// Called when we reset the \c CurrentPosition of this object.
100+
virtual void notifyPositionChanged(size_t /*OldPos*/, size_t /*NewPos*/) {}
101+
96102
/// If a ParameterPackExpansion (or similar type) is encountered, the offset
97103
/// into the pack that we're currently printing.
98104
unsigned CurrentPackIndex = std::numeric_limits<unsigned>::max();
@@ -131,6 +137,8 @@ class OutputBuffer {
131137
OutputBuffer &prepend(std::string_view R) {
132138
size_t Size = R.size();
133139

140+
notifyInsertion(/*Position=*/0, /*Count=*/Size);
141+
134142
grow(Size);
135143
std::memmove(Buffer + Size, Buffer, CurrentPosition);
136144
std::memcpy(Buffer, &*R.begin(), Size);
@@ -171,14 +179,20 @@ class OutputBuffer {
171179
DEMANGLE_ASSERT(Pos <= CurrentPosition, "");
172180
if (N == 0)
173181
return;
182+
183+
notifyInsertion(Pos, N);
184+
174185
grow(N);
175186
std::memmove(Buffer + Pos + N, Buffer + Pos, CurrentPosition - Pos);
176187
std::memcpy(Buffer + Pos, S, N);
177188
CurrentPosition += N;
178189
}
179190

180191
size_t getCurrentPosition() const { return CurrentPosition; }
181-
void setCurrentPosition(size_t NewPos) { CurrentPosition = NewPos; }
192+
void setCurrentPosition(size_t NewPos) {
193+
notifyPositionChanged(CurrentPosition, NewPos);
194+
CurrentPosition = NewPos;
195+
}
182196

183197
char back() const {
184198
DEMANGLE_ASSERT(CurrentPosition, "");

llvm/unittests/Demangle/OutputBufferTest.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,40 @@ TEST(OutputBufferTest, Extend) {
9393

9494
std::free(OB.getBuffer());
9595
}
96+
97+
TEST(OutputBufferTest, Notifications) {
98+
struct MyOutputBuffer : public OutputBuffer {
99+
size_t Inserted = 0;
100+
size_t LatestPos = 0;
101+
102+
void notifyPositionChanged(size_t OldPos, size_t NewPos) override {
103+
LatestPos = NewPos;
104+
}
105+
106+
void notifyInsertion(size_t Position, size_t Count) override {
107+
Inserted += Count;
108+
LatestPos = Position;
109+
}
110+
} OB;
111+
112+
OB.prepend("n");
113+
EXPECT_EQ(OB.Inserted, 1U);
114+
EXPECT_EQ(OB.LatestPos, 0U);
115+
116+
OB.prepend("");
117+
EXPECT_EQ(OB.Inserted, 1U);
118+
EXPECT_EQ(OB.LatestPos, 0U);
119+
120+
OB.prepend("abc");
121+
EXPECT_EQ(OB.Inserted, 4U);
122+
EXPECT_EQ(OB.LatestPos, 0U);
123+
124+
OB.insert(2, "abc", 3U);
125+
EXPECT_EQ(OB.Inserted, 7U);
126+
EXPECT_EQ(OB.LatestPos, 2U);
127+
128+
OB.setCurrentPosition(3U);
129+
EXPECT_EQ(OB.LatestPos, 3U);
130+
131+
std::free(OB.getBuffer());
132+
}

0 commit comments

Comments
 (0)