]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: libproc_macro: Fix capacity update in tokenstream
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Thu, 18 May 2023 14:18:58 +0000 (16:18 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:46:26 +0000 (18:46 +0100)
The capacity was not updated on tokenstream grow. This commit also add a
new mechanism to prevent a tokenstream to grow with a zero delta capacity.

libgrust/ChangeLog:

* libproc_macro/tokenstream.cc (TokenStream::grow): Add
minimum growing capacity.
(TokenStream::push): Change condition.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
libgrust/libproc_macro/tokenstream.cc

index 12d454316012b0abe26786e0e56989fa117aff22..25e42dc3dc91d44d6107884786d2f5dbc79cbf5f 100644 (file)
@@ -48,8 +48,9 @@ TokenStream::make_tokenstream (std::uint64_t capacity)
 void
 TokenStream::grow (std::uint64_t delta)
 {
-  auto new_capacity = capacity + delta;
+  auto new_capacity = capacity + (delta != 0 ? delta : 1);
   auto *new_data = new TokenTree[new_capacity];
+  capacity = new_capacity;
   std::memcpy (new_data, data, size);
   delete[] data;
   data = new_data;
@@ -58,7 +59,7 @@ TokenStream::grow (std::uint64_t delta)
 void
 TokenStream::push (TokenTree tree)
 {
-  if (size == capacity)
+  if (size >= capacity)
     grow (capacity);
   data[size] = tree;
   size++;