[libcxx-commits] [PATCH] D111948: [Demangle] Add prepend functionality to OutputString

Luís Ferreira via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Sat Oct 16 12:37:35 PDT 2021


ljmf00 created this revision.
ljmf00 added a project: LLVM.
ljmf00 requested review of this revision.
Herald added a project: libc++abi.
Herald added subscribers: llvm-commits, libcxx-commits.
Herald added a reviewer: libc++abi.

Implement the functionallity of prepend, required by D demangler.
Please read discussion https://reviews.llvm.org/D111414 for context.
See also https://reviews.llvm.org/D111947 .


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D111948

Files:
  libcxxabi/src/demangle/Utility.h
  llvm/include/llvm/Demangle/Utility.h
  llvm/unittests/Demangle/OutputStringTest.cpp


Index: llvm/unittests/Demangle/OutputStringTest.cpp
===================================================================
--- llvm/unittests/Demangle/OutputStringTest.cpp
+++ llvm/unittests/Demangle/OutputStringTest.cpp
@@ -6,6 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/Demangle/MicrosoftDemangleNodes.h"
 #include "llvm/Demangle/Utility.h"
 #include "gtest/gtest.h"
 #include <string>
@@ -59,3 +60,12 @@
 
   std::free(OS.getBuffer());
 }
+
+TEST(OutputStringTest, Prepend)
+{
+  OutputString OS;
+
+  OS << "abc";
+  OS.prepend("def");
+  EXPECT_EQ("defabc", toString(OS));
+}
Index: llvm/include/llvm/Demangle/Utility.h
===================================================================
--- llvm/include/llvm/Demangle/Utility.h
+++ llvm/include/llvm/Demangle/Utility.h
@@ -14,6 +14,7 @@
 #define DEMANGLE_UTILITY_H
 
 #include "StringView.h"
+#include "llvm/Demangle/MicrosoftDemangleNodes.h"
 #include <cstdint>
 #include <cstdlib>
 #include <cstring>
@@ -93,6 +94,23 @@
     return *this;
   }
 
+  OutputString prepend(StringView R)
+  {
+      char *TempBuffer;
+      size_t Size = R.size();
+
+    if (Size != 0) {
+      grow(Size);
+      for (TempBuffer = (Buffer + CurrentPosition) - 1; TempBuffer >= Buffer; --TempBuffer) {
+        TempBuffer[Size] = TempBuffer[0];
+      }
+      std::memcpy(Buffer, R.begin(), Size);
+      CurrentPosition += Size;
+    }
+
+    return *this;
+  }
+
   OutputString &operator<<(StringView R) { return (*this += R); }
 
   OutputString &operator<<(char C) { return (*this += C); }
Index: libcxxabi/src/demangle/Utility.h
===================================================================
--- libcxxabi/src/demangle/Utility.h
+++ libcxxabi/src/demangle/Utility.h
@@ -14,6 +14,7 @@
 #define DEMANGLE_UTILITY_H
 
 #include "StringView.h"
+#include "llvm/Demangle/MicrosoftDemangleNodes.h"
 #include <cstdint>
 #include <cstdlib>
 #include <cstring>
@@ -93,6 +94,23 @@
     return *this;
   }
 
+  OutputString prepend(StringView R)
+  {
+      char *TempBuffer;
+      size_t Size = R.size();
+
+    if (Size != 0) {
+      grow(Size);
+      for (TempBuffer = (Buffer + CurrentPosition) - 1; TempBuffer >= Buffer; --TempBuffer) {
+        TempBuffer[Size] = TempBuffer[0];
+      }
+      std::memcpy(Buffer, R.begin(), Size);
+      CurrentPosition += Size;
+    }
+
+    return *this;
+  }
+
   OutputString &operator<<(StringView R) { return (*this += R); }
 
   OutputString &operator<<(char C) { return (*this += C); }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D111948.380206.patch
Type: text/x-patch
Size: 2543 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20211016/5bf894ef/attachment.bin>


More information about the libcxx-commits mailing list