Permalink
Browse files

Add __cxa_demangle support

  • Loading branch information...
1 parent becd5d8 commit a283c64f6b95ec2db1b4b6004f27b1e448961e73 anonymous committed Sep 29, 2010
Showing with 81 additions and 16 deletions.
  1. +2 −0 CMakeLists.txt
  2. +2 −0 src/CMakeLists.txt
  3. +3 −4 src/exception.cc
  4. +38 −0 src/typeinfo.cc
  5. +30 −0 test/test.cc
  6. +2 −0 test/test_exception.cc
  7. +4 −1 test/test_guard.cc
  8. +0 −11 test/test_typeinfo.cc
View
@@ -7,5 +7,7 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/lib")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/lib")
add_subdirectory(src)
+IF(BUILD_TESTS)
add_subdirectory(test)
+ENDIF()
View
@@ -8,6 +8,8 @@ set(CXXRT_SOURCES
stdexcept.cc
memory.cc
aux.cc
+ libelftc_dem_gnu3.c
+ libelftc_vstr.c
)
View
@@ -443,18 +443,17 @@ static void report_failure(_Unwind_Reason_Code err, void *thrown_exception)
fprintf(stderr, "Fatal error during phase 2 unwinding\n");
break;
case _URC_END_OF_STACK:
- fprintf(stderr, "Terminating due to uncaught exception %p:\n",
+ fprintf(stderr, "Terminating due to uncaught exception %p",
thrown_exception);
-/* TODO: Uncomment this when __cxa_demangle() is implemented.
size_t bufferSize = 128;
char *demangled = (char*)malloc(bufferSize);
const char *mangled = __cxa_current_exception_type()->name();
int status;
__cxa_demangle(mangled, demangled, &bufferSize, &status);
- fprintf(stderr, "Terminating due to uncaught of type %s:\n",
+ fprintf(stderr, " of type %s\n",
status == 0 ? (const char*)demangled : mangled);
-*/
+ if (status == 0) { free(demangled); }
// Print a back trace if no handler is found.
// TODO: Make this optional
_Unwind_Backtrace(trace, 0);
View
@@ -1,4 +1,7 @@
#include "typeinfo.h"
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
using std::type_info;
@@ -39,3 +42,38 @@ ABI_NAMESPACE::__vmi_class_type_info::~__vmi_class_type_info() {}
ABI_NAMESPACE::__pbase_type_info::~__pbase_type_info() {}
ABI_NAMESPACE::__pointer_type_info::~__pointer_type_info() {}
ABI_NAMESPACE::__pointer_to_member_type_info::~__pointer_to_member_type_info() {}
+
+// From libelftc
+extern "C" char *cpp_demangle_gnu3(const char *);
+extern "C" bool is_cpp_mangled_gnu3(const char *);
+
+extern "C" char* __cxa_demangle(const char* mangled_name,
+ char* buf,
+ size_t* n,
+ int* status)
+{
+ char *demangled = cpp_demangle_gnu3(mangled_name);
+ if (NULL != demangled)
+ {
+ size_t len = strlen(demangled);
+ buf = (char*)realloc(buf, len+1);
+ if (0 != buf)
+ {
+ memcpy(buf, demangled, len);
+ buf[len] = 0;
+ *n = len;
+ *status = 0;
+ }
+ else
+ {
+ *status = -1;
+ }
+ free(demangled);
+ }
+ else
+ {
+ *status = -2;
+ return NULL;
+ }
+ return buf;
+}
View
@@ -1,13 +1,20 @@
#include <stdio.h>
#include <stdlib.h>
+#include <unistd.h>
+
static int succeeded;
static int failed;
+static bool verbose;
void log_test(bool predicate, const char *file, int line, const char *message)
{
if (predicate)
{
+ if (verbose)
+ {
+ printf("Test passed: %s:%d: %s\n", file, line, message);
+ }
succeeded++;
return;
}
@@ -24,3 +31,26 @@ static void __attribute__((constructor)) init(void)
{
atexit(log_totals);
}
+
+void test_type_info(void);
+void test_exceptions();
+void test_guards(void);
+int main(int argc, char **argv)
+{
+ int ch;
+
+ while ((ch = getopt(argc, argv, "v")) != -1)
+ {
+ switch (ch)
+ {
+ case 'v':
+ verbose = true;
+ default: break;
+ }
+ }
+
+ test_type_info();
+ test_guards();
+ test_exceptions();
+ return 0;
+}
View
@@ -5,6 +5,8 @@
#include <exception>
+#define fprintf(...)
+
void log(void* ignored)
{
printf("Cleanup called on %s\n", *(char**)ignored);
View
@@ -1,12 +1,12 @@
#include <stdio.h>
+#include "test.h"
static int static_count;
struct static_struct
{
int i;
static_struct()
{
- fprintf(stderr, "Initialized static\n");
static_count++;
i = 12;
};
@@ -23,4 +23,7 @@ int init_static(void)
void test_guards(void)
{
init_static();
+ int i = init_static();
+ TEST(i == 12, "Static initialized");
+ TEST(static_count == 2, "Each static only initialized once");
}
View
@@ -2,9 +2,6 @@
#include "test.h"
#include <stdio.h>
-// FIXME: Move this and main() to a different file.
-void test_exceptions();
-
struct Virt1;
struct Virt2;
struct Diamond;
@@ -110,11 +107,3 @@ void test_type_info(void)
TEST(0 == dynamic_cast<Sub1*>(b2), "Casting Root to Sub1 (0 expected)");
}
-void test_guards(void);
-int main(void)
-{
- test_type_info();
- test_guards();
- test_exceptions();
- return 0;
-}

0 comments on commit a283c64

Please sign in to comment.