From da891be9911b8c45435486bb9b869ccd67c4fc5c Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Thu, 7 Dec 2023 15:59:26 -0500 Subject: [PATCH 1/4] fix memory leak in assert, add demangling and link options to find function names --- CMakeLists.txt | 1 + include/blt/std/utility.h | 23 +++++++++++++++++++++++ src/blt/std/assert.cpp | 9 +++++---- tests/src/utility_test.cpp | 31 +++++++++++++++++++++---------- 4 files changed, 50 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 73de8a7..4a533a3 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -96,6 +96,7 @@ if(MSVC) else() # perhaps we should warn on unused variables, but BLT will have lots of them. target_compile_options(BLT PRIVATE -Wall -Wextra -Wpedantic) + target_link_options(BLT PUBLIC -rdynamic) endif() message("BLT ${CMAKE_PROJECT_VERSION} Successfully included!") diff --git a/include/blt/std/utility.h b/include/blt/std/utility.h index fc92f92..6be8b89 100644 --- a/include/blt/std/utility.h +++ b/include/blt/std/utility.h @@ -21,6 +21,29 @@ #include +#if defined(__GNUC__) + #include + #include + #include + + 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 { diff --git a/src/blt/std/assert.cpp b/src/blt/std/assert.cpp index 124c277..30a53c9 100644 --- a/src/blt/std/assert.cpp +++ b/src/blt/std/assert.cpp @@ -4,6 +4,7 @@ * See LICENSE file for license detail */ #include +#include #include #include #include @@ -14,7 +15,6 @@ #include #include - #endif #ifdef __GNUC__ @@ -23,6 +23,7 @@ char** messages = backtrace_symbols(ptrs, size); #define BLT_FREE_STACK_TRACE() free(messages); + #else #define BLT_STACK_TRACE(number) 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("Stack Trace:"); - backtrace_symbols(ptrs, size); - printStacktrace(messages, size, path, line); BLT_FREE_STACK_TRACE(); @@ -69,6 +68,8 @@ namespace blt { void printStacktrace(char** messages, int size, const char* path, int line) { + if (messages == nullptr) + return; #ifdef __GNUC__ for (int i = 1; i < size; i++){ int tabs = i - 1; @@ -94,7 +95,7 @@ namespace blt { loc += std::to_string(line); loc += '\''; } else - loc = mes.substr(0, mes.find('+')); + loc = demangle(mes.substr(0, mes.find('+'))); if (!loc.empty()) buffer += " in "; diff --git a/tests/src/utility_test.cpp b/tests/src/utility_test.cpp index c2b7590..eebf876 100644 --- a/tests/src/utility_test.cpp +++ b/tests/src/utility_test.cpp @@ -23,6 +23,7 @@ #include #include #include +#include "blt/std/assert.h" std::optional get() { @@ -35,6 +36,14 @@ void printLines(const std::vector& lines) std::cout << v << "\n"; } +void getfucked(){ + BLT_ASSERT(false); +} + +void fuckered(){ + getfucked(); +} + void blt::test::utility::run() { //std::vector temp; @@ -127,18 +136,20 @@ void blt::test::utility::run() printLines(tableQ2i3.createTable(true, true)); - blt::string::TableFormatter tableQ4i0("Q4 Iteration 1"); + blt::string::TableFormatter tableQ4i0("Q4 Iteration 0"); tableQ4i0.addColumn("Statement"); tableQ4i0.addColumn("IN"); tableQ4i0.addColumn("OUT"); tableQ4i0.addRow({"Entry (1)", " -- ", " {} "}); - tableQ4i0.addRow({"(2)", "{}", "{}"}); - tableQ4i0.addRow({"(3)", "{}", "{}"}); - tableQ4i0.addRow({"(4)", "{x = a + b, y = a * b}", "{x = a + b, y = a * b}"}); - tableQ4i0.addRow({"(5)", "{x = a + b, y = a * b}", "{x = a + b, y = a * b}"}); - tableQ4i0.addRow({"(6)", "{x = a + b, y = a * b}", "{x = a + b, y = a * b}"}); - tableQ4i0.addRow({"(7)", "{}", "{}"}); - tableQ4i0.addRow({"(8)", "{}", "{}"}); - tableQ4i0.addRow({"(9)", "{}", "{}"}); - tableQ4i0.addRow({"Exit (10)", "{, }", " -- "}); + tableQ4i0.addRow({"(2)", "{a + b, a * b, a - b, b + 1}", "{a + b, a * b, a - b, b + 1}"}); + tableQ4i0.addRow({"(3)", "{a + b, a * b, a - b, b + 1}", "{a + b, a * b, a - b, b + 1}"}); + tableQ4i0.addRow({"(4)", "{a + b, a * b, a - b, b + 1}", "{a + b, a * b, a - b, b + 1}"}); + tableQ4i0.addRow({"(5)", "{a + b, a * b, a - b, b + 1}", "{a + b, a * b, a - b, b + 1}"}); + tableQ4i0.addRow({"(6)", "{a + b, a * b, a - b, b + 1}", "{a + b, a * b, a - b, b + 1}"}); + tableQ4i0.addRow({"(7)", "{a + b, a * b, a - b, b + 1}", "{a + b, a * b, a - b, b + 1}"}); + tableQ4i0.addRow({"(8)", "{a + b, a * b, a - b, b + 1}", "{a + b, a * b, a - b, b + 1}"}); + tableQ4i0.addRow({"(9)", "{a + b, a * b, a - b, b + 1}", "{a + b, a * b, a - b, b + 1}"}); + tableQ4i0.addRow({"Exit (10)", "{a + b, a * b, a - b, b + 1}", " -- "}); + + fuckered(); } From 8defbaf70dc0b525c3c3778ac6453563408a5c64 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Thu, 7 Dec 2023 16:03:38 -0500 Subject: [PATCH 2/4] hot chocolate --- include/blt/std/utility.h | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/include/blt/std/utility.h b/include/blt/std/utility.h index 6be8b89..1a37de8 100644 --- a/include/blt/std/utility.h +++ b/include/blt/std/utility.h @@ -26,21 +26,27 @@ #include #include - static BLT_CPP20_CONSTEXPR inline std::string demangle(const std::string& str) + namespace blt { - 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; + 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) + namespace blt { - return str; + static BLT_CPP20_CONSTEXPR inline std::string demangle(const std::string& str) + { + return str; + } } #endif From a4220e3c0de22e0f3dc51f152f46f3a34d55bcb8 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Fri, 8 Dec 2023 20:11:58 -0500 Subject: [PATCH 3/4] test --- include/blt/std/utility.h | 59 ++++++++++++++++++++++++-------------- tests/src/utility_test.cpp | 5 ++++ 2 files changed, 42 insertions(+), 22 deletions(-) diff --git a/include/blt/std/utility.h b/include/blt/std/utility.h index 1a37de8..04e55d3 100644 --- a/include/blt/std/utility.h +++ b/include/blt/std/utility.h @@ -22,42 +22,57 @@ #include #if defined(__GNUC__) + #include #include #include - namespace blt +namespace blt +{ + static BLT_CPP20_CONSTEXPR inline std::string demangle(const std::string& str) { - 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 - namespace blt - { - 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 +namespace blt +{ + static BLT_CPP20_CONSTEXPR inline std::string demangle(const std::string& str) + { + return str; + } +} #endif namespace blt { - - template + template class enumerate { private: - + size_t index = 0; + TYPE_ITR begin; + TYPE_ITR end; + public: + class enumerate_itr + { + public: + using iterator_category = std::input_iterator_tag; + using value_type = typename TYPE_ITR::value_type; + using difference_type = typename TYPE_ITR::difference_type; + using pointer = typename TYPE_ITR::pointer; + using reference = typename TYPE_ITR::reference; + private: + size_t index = 0; + public: + }; }; #if defined(__GNUC__) || defined(__llvm__) diff --git a/tests/src/utility_test.cpp b/tests/src/utility_test.cpp index eebf876..f1ceb9a 100644 --- a/tests/src/utility_test.cpp +++ b/tests/src/utility_test.cpp @@ -36,6 +36,10 @@ void printLines(const std::vector& lines) std::cout << v << "\n"; } +void testEnumerate(const std::vector& test){ + +} + void getfucked(){ BLT_ASSERT(false); } @@ -65,6 +69,7 @@ void blt::test::utility::run() tableTest.addRow({"Sleeping Together (Sexual)", "10,000"}); tableTest.addRow({"Relationship (I would do anything for you)", "1,000,000,000,000"}); + testEnumerate(tableTest.createTable(true, true)); printLines(tableTest.createTable(true, true)); From 704e77419f8c671f89c5695eb74e39f89d168aaf Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Fri, 8 Dec 2023 20:44:45 -0500 Subject: [PATCH 4/4] test --- include/blt/std/utility.h | 60 ++++++++++++++++++++++++++++++++++---- tests/src/utility_test.cpp | 27 ++++++++++------- 2 files changed, 71 insertions(+), 16 deletions(-) diff --git a/include/blt/std/utility.h b/include/blt/std/utility.h index 04e55d3..60c1a43 100644 --- a/include/blt/std/utility.h +++ b/include/blt/std/utility.h @@ -54,14 +54,10 @@ namespace blt namespace blt { template - class enumerate + class enumerator { - private: - size_t index = 0; - TYPE_ITR begin; - TYPE_ITR end; public: - class enumerate_itr + class iterator { public: using iterator_category = std::input_iterator_tag; @@ -71,9 +67,61 @@ namespace blt using reference = typename TYPE_ITR::reference; private: size_t index = 0; + TYPE_ITR current; public: + explicit iterator(TYPE_ITR current): current(current) + {}; + + iterator& operator++() + { + index++; + ++current; + return *this; + } + + iterator operator++(int) + { + iterator retval = *this; + ++(*this); + return retval; + } + + bool operator==(iterator other) const + { return current == other.current; } + + bool operator!=(iterator other) const + { return !(*this == other); } + + std::pair operator*() const + { + return {index, *current}; + }; }; + + explicit enumerator(TYPE_ITR begin, TYPE_ITR end): begin_(begin), end_(end) + {} + + iterator begin() + { + return begin_; + } + + iterator end() + { + return end_; + } + + private: + iterator begin_; + iterator end_; }; + + template + static inline enumerator enumerate(T container) + { + return enumerator{container.begin(), container.end()}; + } + #if defined(__GNUC__) || defined(__llvm__) #define BLT_ATTRIB_NO_INLINE __attribute__ ((noinline)) diff --git a/tests/src/utility_test.cpp b/tests/src/utility_test.cpp index f1ceb9a..f1fc01f 100644 --- a/tests/src/utility_test.cpp +++ b/tests/src/utility_test.cpp @@ -36,15 +36,21 @@ void printLines(const std::vector& lines) std::cout << v << "\n"; } -void testEnumerate(const std::vector& test){ - +void testEnumerate(const std::vector& test) +{ + for (auto pair : blt::enumerate(test)) + { + std::cout << pair.first << ": " << pair.second << std::endl; + } } -void getfucked(){ +void getfucked() +{ BLT_ASSERT(false); } -void fuckered(){ +void fuckered() +{ getfucked(); } @@ -89,14 +95,15 @@ void blt::test::utility::run() assign1.getRoot()->with( // left (new string::BinaryTreeFormatter::Node("member")) - ->with((new string::BinaryTreeFormatter::Node("total -= total * 0.15")) - ->with((new string::BinaryTreeFormatter::Node("total > 500"))->with(new string::BinaryTreeFormatter::Node("total -= 25"))), - (new string::BinaryTreeFormatter::Node("total -= total * 0.05"))), + ->with((new string::BinaryTreeFormatter::Node("total -= total * 0.15")) + ->with((new string::BinaryTreeFormatter::Node("total > 500")) + ->with(new string::BinaryTreeFormatter::Node("total -= 25"))), + (new string::BinaryTreeFormatter::Node("total -= total * 0.05"))), // right (new string::BinaryTreeFormatter::Node("quality")) - ->with((new string::BinaryTreeFormatter::Node("total -= total * 0.02")), - (new string::BinaryTreeFormatter::Node("total -= total * 0.05"))) - ); + ->with((new string::BinaryTreeFormatter::Node("total -= total * 0.02")), + (new string::BinaryTreeFormatter::Node("total -= total * 0.05"))) + ); printLines(assign1.construct()); blt::string::TableFormatter tableQ2i1("Iteration 0");