[libcxx-commits] [PATCH] D125268: [libc++abi] Refactor exception type demangling into a separate function

Louis Dionne via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Tue May 10 07:14:19 PDT 2022


ldionne updated this revision to Diff 428366.
ldionne marked an inline comment as done.
ldionne retitled this revision from "[libc++abi][NFC] Refactor exception type demangling into a separate function" to "[libc++abi] Refactor exception type demangling into a separate function".
ldionne edited the summary of this revision.
ldionne added a comment.

Address review comment.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D125268/new/

https://reviews.llvm.org/D125268

Files:
  libcxxabi/src/cxa_default_handlers.cpp


Index: libcxxabi/src/cxa_default_handlers.cpp
===================================================================
--- libcxxabi/src/cxa_default_handlers.cpp
+++ libcxxabi/src/cxa_default_handlers.cpp
@@ -22,6 +22,22 @@
 
 static constinit const char* cause = "uncaught";
 
+// Demangle the given string, or return the string as-is in case of an error.
+// On success, the returned string has been allocated with `malloc()`, however
+// we intentionally leak it in this file because we are terminating anyways.
+static char const* demangle(char const* str) {
+#if !defined(LIBCXXABI_NON_DEMANGLING_TERMINATE)
+    int status;
+    const char* result = __cxxabiv1::__cxa_demangle(str, nullptr, nullptr, &status);
+    if (status != 0)
+        return str;
+    else
+        return result;
+#else
+    return str;
+#endif
+}
+
 __attribute__((noreturn))
 static void demangling_terminate_handler()
 {
@@ -45,17 +61,7 @@
                         exception_header + 1;
                 const __shim_type_info* thrown_type =
                     static_cast<const __shim_type_info*>(exception_header->exceptionType);
-#if !defined(LIBCXXABI_NON_DEMANGLING_TERMINATE)
-                // Try to get demangled name of thrown_type
-                int status;
-                char buf[1024];
-                size_t len = sizeof(buf);
-                const char* name = __cxa_demangle(thrown_type->name(), buf, &len, &status);
-                if (status != 0)
-                    name = thrown_type->name();
-#else
-                const char* name = thrown_type->name();
-#endif
+                char const* name = demangle(thrown_type->name());
                 // If the uncaught exception can be caught with std::exception&
                 const __shim_type_info* catch_type =
                     static_cast<const __shim_type_info*>(&typeid(std::exception));


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D125268.428366.patch
Type: text/x-patch
Size: 1862 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20220510/5a670b5e/attachment.bin>


More information about the libcxx-commits mailing list