[PATCH] D149104: [Demangle] use moar std::string_view

Nick Desaulniers via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 24 15:55:57 PDT 2023


nickdesaulniers created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
nickdesaulniers requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

As suggested by @erichkeane in
https://reviews.llvm.org/D141451#inline-1429549

There's potential for a lot more cleanups around these APIs. This is
just a start.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D149104

Files:
  llvm/include/llvm/Demangle/Demangle.h
  llvm/lib/Demangle/Demangle.cpp


Index: llvm/lib/Demangle/Demangle.cpp
===================================================================
--- llvm/lib/Demangle/Demangle.cpp
+++ llvm/lib/Demangle/Demangle.cpp
@@ -11,24 +11,14 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Demangle/Demangle.h"
+#include "llvm/Demangle/StringViewExtras.h"
 #include <cstdlib>
-#include <cstring>
 
-static bool isItaniumEncoding(const char *S) {
-  // Itanium encoding requires 1 or 3 leading underscores, followed by 'Z'.
-  return std::strncmp(S, "_Z", 2) == 0 || std::strncmp(S, "___Z", 4) == 0;
-}
-
-static bool isRustEncoding(const char *S) { return S[0] == '_' && S[1] == 'R'; }
-
-static bool isDLangEncoding(const std::string &MangledName) {
-  return MangledName.size() >= 2 && MangledName[0] == '_' &&
-         MangledName[1] == 'D';
-}
+using llvm::itanium_demangle::starts_with;
 
-std::string llvm::demangle(const std::string &MangledName) {
+std::string llvm::demangle(const std::string_view MangledName) {
   std::string Result;
-  const char *S = MangledName.c_str();
+  const char *S = MangledName.data();
 
   if (nonMicrosoftDemangle(S, Result))
     return Result;
@@ -39,12 +29,22 @@
   if (char *Demangled = microsoftDemangle(S, nullptr, nullptr)) {
     Result = Demangled;
     std::free(Demangled);
-    return Result;
+  } else {
+    Result = MangledName;
   }
+  return Result;
+}
 
-  return MangledName;
+namespace {
+bool isItaniumEncoding(std::string_view S) {
+  return starts_with(S, "_Z") || starts_with(S, "___Z");
 }
 
+bool isRustEncoding(std::string_view S) { return starts_with(S, "_R"); }
+
+bool isDLangEncoding(std::string_view S) { return starts_with(S, "_D"); }
+} // namespace
+
 bool llvm::nonMicrosoftDemangle(const char *MangledName, std::string &Result) {
   char *Demangled = nullptr;
   if (isItaniumEncoding(MangledName))
Index: llvm/include/llvm/Demangle/Demangle.h
===================================================================
--- llvm/include/llvm/Demangle/Demangle.h
+++ llvm/include/llvm/Demangle/Demangle.h
@@ -11,6 +11,7 @@
 
 #include <cstddef>
 #include <string>
+#include <string_view>
 
 namespace llvm {
 /// This is a llvm local version of __cxa_demangle. Other than the name and
@@ -62,7 +63,7 @@
 /// \param MangledName - reference to string to demangle.
 /// \returns - the demangled string, or a copy of the input string if no
 /// demangling occurred.
-std::string demangle(const std::string &MangledName);
+std::string demangle(const std::string_view MangledName);
 
 bool nonMicrosoftDemangle(const char *MangledName, std::string &Result);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149104.516563.patch
Type: text/x-patch
Size: 2629 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230424/4a552b4f/attachment.bin>


More information about the llvm-commits mailing list