fix memory leak in assert, add demangling and link options to find function names

v1
Brett 2023-12-07 15:59:26 -05:00
parent f7e05930c9
commit da891be991
4 changed files with 50 additions and 14 deletions

View File

@ -96,6 +96,7 @@ if(MSVC)
else() else()
# perhaps we should warn on unused variables, but BLT will have lots of them. # perhaps we should warn on unused variables, but BLT will have lots of them.
target_compile_options(BLT PRIVATE -Wall -Wextra -Wpedantic) target_compile_options(BLT PRIVATE -Wall -Wextra -Wpedantic)
target_link_options(BLT PUBLIC -rdynamic)
endif() endif()
message("BLT ${CMAKE_PROJECT_VERSION} Successfully included!") message("BLT ${CMAKE_PROJECT_VERSION} Successfully included!")

View File

@ -21,6 +21,29 @@
#include <optional> #include <optional>
#if defined(__GNUC__)
#include <cxxabi.h>
#include <blt/compatibility.h>
#include <string>
static BLT_CPP20_CONSTEXPR inline std::string demangle(const std::string& str)
{
int status;
// only defined for GNU C++11?
char *demangled_name = abi::__cxa_demangle(str.c_str(), nullptr, nullptr, &status);
if (demangled_name == nullptr)
return str;
std::string ret_name = demangled_name;
std::free(demangled_name);
return ret_name;
}
#else
static BLT_CPP20_CONSTEXPR inline std::string demangle(const std::string& str)
{
return str;
}
#endif
namespace blt namespace blt
{ {

View File

@ -4,6 +4,7 @@
* See LICENSE file for license detail * See LICENSE file for license detail
*/ */
#include <blt/std/assert.h> #include <blt/std/assert.h>
#include <blt/std/utility.h>
#include <blt/std/logging.h> #include <blt/std/logging.h>
#include <blt/std/string.h> #include <blt/std/string.h>
#include <iostream> #include <iostream>
@ -14,7 +15,6 @@
#include <execinfo.h> #include <execinfo.h>
#include <cstdlib> #include <cstdlib>
#endif #endif
#ifdef __GNUC__ #ifdef __GNUC__
@ -23,6 +23,7 @@
char** messages = backtrace_symbols(ptrs, size); char** messages = backtrace_symbols(ptrs, size);
#define BLT_FREE_STACK_TRACE() free(messages); #define BLT_FREE_STACK_TRACE() free(messages);
#else #else
#define BLT_STACK_TRACE(number) void(); #define BLT_STACK_TRACE(number) void();
#define BLT_FREE_STACK_TRACE() void(); #define BLT_FREE_STACK_TRACE() void();
@ -59,8 +60,6 @@ namespace blt {
BLT_ERROR("The assertion '%s' has failed in file '%s:%d'", expression, path, line); BLT_ERROR("The assertion '%s' has failed in file '%s:%d'", expression, path, line);
BLT_ERROR("Stack Trace:"); BLT_ERROR("Stack Trace:");
backtrace_symbols(ptrs, size);
printStacktrace(messages, size, path, line); printStacktrace(messages, size, path, line);
BLT_FREE_STACK_TRACE(); BLT_FREE_STACK_TRACE();
@ -69,6 +68,8 @@ namespace blt {
void printStacktrace(char** messages, int size, const char* path, int line) void printStacktrace(char** messages, int size, const char* path, int line)
{ {
if (messages == nullptr)
return;
#ifdef __GNUC__ #ifdef __GNUC__
for (int i = 1; i < size; i++){ for (int i = 1; i < size; i++){
int tabs = i - 1; int tabs = i - 1;
@ -94,7 +95,7 @@ namespace blt {
loc += std::to_string(line); loc += std::to_string(line);
loc += '\''; loc += '\'';
} else } else
loc = mes.substr(0, mes.find('+')); loc = demangle(mes.substr(0, mes.find('+')));
if (!loc.empty()) if (!loc.empty())
buffer += " in "; buffer += " in ";

View File

@ -23,6 +23,7 @@
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
#include <limits> #include <limits>
#include "blt/std/assert.h"
std::optional<int> get() std::optional<int> get()
{ {
@ -35,6 +36,14 @@ void printLines(const std::vector<std::string>& lines)
std::cout << v << "\n"; std::cout << v << "\n";
} }
void getfucked(){
BLT_ASSERT(false);
}
void fuckered(){
getfucked();
}
void blt::test::utility::run() void blt::test::utility::run()
{ {
//std::vector<int> temp; //std::vector<int> temp;
@ -127,18 +136,20 @@ void blt::test::utility::run()
printLines(tableQ2i3.createTable(true, true)); printLines(tableQ2i3.createTable(true, true));
blt::string::TableFormatter tableQ4i0("Q4 Iteration 1"); blt::string::TableFormatter tableQ4i0("Q4 Iteration 0");
tableQ4i0.addColumn("Statement"); tableQ4i0.addColumn("Statement");
tableQ4i0.addColumn("IN"); tableQ4i0.addColumn("IN");
tableQ4i0.addColumn("OUT"); tableQ4i0.addColumn("OUT");
tableQ4i0.addRow({"Entry (1)", " -- ", " {} "}); tableQ4i0.addRow({"Entry (1)", " -- ", " {} "});
tableQ4i0.addRow({"(2)", "{}", "{}"}); tableQ4i0.addRow({"(2)", "{a + b, a * b, a - b, b + 1}", "{a + b, a * b, a - b, b + 1}"});
tableQ4i0.addRow({"(3)", "{}", "{}"}); tableQ4i0.addRow({"(3)", "{a + b, a * b, a - b, b + 1}", "{a + b, a * b, a - b, b + 1}"});
tableQ4i0.addRow({"(4)", "{x = a + b, y = a * b}", "{x = a + b, y = a * b}"}); tableQ4i0.addRow({"(4)", "{a + b, a * b, a - b, b + 1}", "{a + b, a * b, a - b, b + 1}"});
tableQ4i0.addRow({"(5)", "{x = a + b, y = a * b}", "{x = a + b, y = a * b}"}); tableQ4i0.addRow({"(5)", "{a + b, a * b, a - b, b + 1}", "{a + b, a * b, a - b, b + 1}"});
tableQ4i0.addRow({"(6)", "{x = a + b, y = a * b}", "{x = a + b, y = a * b}"}); tableQ4i0.addRow({"(6)", "{a + b, a * b, a - b, b + 1}", "{a + b, a * b, a - b, b + 1}"});
tableQ4i0.addRow({"(7)", "{}", "{}"}); tableQ4i0.addRow({"(7)", "{a + b, a * b, a - b, b + 1}", "{a + b, a * b, a - b, b + 1}"});
tableQ4i0.addRow({"(8)", "{}", "{}"}); tableQ4i0.addRow({"(8)", "{a + b, a * b, a - b, b + 1}", "{a + b, a * b, a - b, b + 1}"});
tableQ4i0.addRow({"(9)", "{}", "{}"}); tableQ4i0.addRow({"(9)", "{a + b, a * b, a - b, b + 1}", "{a + b, a * b, a - b, b + 1}"});
tableQ4i0.addRow({"Exit (10)", "{<factor, 1>, <factorial, 2>}", " -- "}); tableQ4i0.addRow({"Exit (10)", "{a + b, a * b, a - b, b + 1}", " -- "});
fuckered();
} }