From ed270531aa010cabcab5433cba4134ca453633d7 Mon Sep 17 00:00:00 2001 From: Martin Olivier Date: Mon, 15 Jul 2024 23:53:44 +0200 Subject: [PATCH] fix: add space before pointers and references --- CMakeLists.txt | 4 +-- src/dylib.cpp | 46 +++++---------------------------- src/unix.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ tests/lib.cpp | 4 +++ tests/tests.cpp | 11 +++++--- 5 files changed, 87 insertions(+), 45 deletions(-) create mode 100644 src/unix.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e712641..ffc4837 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,11 +13,11 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) set(SOURCES src/dylib.cpp) if(APPLE) - list(APPEND SOURCES src/mac.cpp) + list(APPEND SOURCES src/mac.cpp src/unix.cpp) elseif(WIN32) list(APPEND SOURCES src/win.cpp) else() - list(APPEND SOURCES src/linux.cpp) + list(APPEND SOURCES src/linux.cpp src/unix.cpp) endif() add_library(dylib STATIC ${SOURCES}) diff --git a/src/dylib.cpp b/src/dylib.cpp index cf5e85a..3336415 100644 --- a/src/dylib.cpp +++ b/src/dylib.cpp @@ -18,7 +18,6 @@ #else #include #include -#include #include #endif @@ -51,9 +50,12 @@ std::vector get_symbols(int fd, bool demangle); void replace_occurrences(std::string &input, const std::string &keyword, const std::string &replacement) { size_t pos = 0; - while ((pos = input.find(keyword)) != std::string::npos) { - input.erase(pos, keyword.length()); - input.insert(pos, replacement); + if (keyword.empty()) + return; + + while ((pos = input.find(keyword, pos)) != std::string::npos) { + input.replace(pos, keyword.length(), replacement); + pos += replacement.length(); } } @@ -150,42 +152,6 @@ dylib::~dylib() { #endif } -#if !(defined(_WIN32) || defined(_WIN64)) -std::string format_symbol(std::string input) { - replace_occurrences(input, "std::__1::", "std::"); - replace_occurrences(input, "std::__cxx11::", "std::"); - - replace_occurrences(input, "> >", ">>"); - replace_occurrences(input, "()", "(void)"); - - return input; -} - -std::string get_demangled_name(const char *symbol) { - std::string result; - size_t size = strlen(symbol); - int status; - char *buf; - char *res; - - buf = reinterpret_cast(malloc(size)); - if (buf == NULL) - throw std::bad_alloc(); - - res = abi::__cxa_demangle(symbol, buf, &size, &status); - if (!res) { - free(buf); - return ""; - } - - result = format_symbol(res); - - free(res); - - return result; -} -#endif - dylib::native_symbol_type dylib::get_symbol(const char *symbol_name) const { std::vector matching_symbols; std::vector all_symbols; diff --git a/src/unix.cpp b/src/unix.cpp new file mode 100644 index 0000000..b349bf9 --- /dev/null +++ b/src/unix.cpp @@ -0,0 +1,67 @@ +/** + * @file unix.cpp + * + * @author Martin Olivier + * @copyright (c) 2024 Martin Olivier + * + * This library is released under MIT license + */ + +#include +#include + +void replace_occurrences(std::string &input, const std::string &keyword, const std::string &replacement); + +static void add_sym_separator(std::string &input, char symbol) +{ + size_t pos = 0; + + if (input.empty()) { + return; + } + + while ((pos = input.find(symbol, pos)) != std::string::npos) { + if (pos && input[pos - 1] != ' ' && input[pos - 1] != '&' && input[pos - 1] != '*') { + input.replace(pos, 1, std::string(" ") + symbol); + pos += 2; + } else { + pos++; + } + } +} + +std::string format_symbol(std::string input) { + replace_occurrences(input, "std::__1::", "std::"); + replace_occurrences(input, "std::__cxx11::", "std::"); + + replace_occurrences(input, "()", "(void)"); + + add_sym_separator(input, '*'); + add_sym_separator(input, '&'); + + return input; +} + +std::string get_demangled_name(const char *symbol) { + std::string result; + size_t size = strlen(symbol); + int status; + char *buf; + char *res; + + buf = reinterpret_cast(malloc(size)); + if (buf == NULL) + throw std::bad_alloc(); + + res = abi::__cxa_demangle(symbol, buf, &size, &status); + if (!res) { + free(buf); + return ""; + } + + result = format_symbol(res); + + free(res); + + return result; +} \ No newline at end of file diff --git a/tests/lib.cpp b/tests/lib.cpp index 3f6800c..b16f788 100755 --- a/tests/lib.cpp +++ b/tests/lib.cpp @@ -49,5 +49,9 @@ namespace tools { LIB_EXPORT void println(const unsigned int& val) { std::cout << "ref: " << val << std::endl; } + + LIB_EXPORT void println(const char *val) { + std::cout << "ptr: " << val << std::endl; + } } } diff --git a/tests/tests.cpp b/tests/tests.cpp index cb257fb..94bf00a 100755 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -210,17 +210,22 @@ TEST(cpp_symbols, basic_test) { auto text = std::string("bla,bla,bla..."); testing::internal::CaptureStdout(); - auto ref_println = lib.get_function("tools::string::println(std::basic_string, std::allocator> const&)"); + auto ptr_println = lib.get_function("tools::string::println(char const *)"); + ptr_println(text.c_str()); + EXPECT_EQ(testing::internal::GetCapturedStdout(), "ptr: \x1c" "bla,bla,bla...\n"); + + testing::internal::CaptureStdout(); + auto ref_println = lib.get_function("tools::string::println(std::basic_string, std::allocator> const &)"); ref_println(text); EXPECT_EQ(testing::internal::GetCapturedStdout(), "ref: bla,bla,bla...\n"); testing::internal::CaptureStdout(); - auto mov_println = lib.get_function("tools::string::println(std::basic_string, std::allocator>&&)"); + auto mov_println = lib.get_function("tools::string::println(std::basic_string, std::allocator> &&)"); mov_println(std::move(text)); EXPECT_EQ(testing::internal::GetCapturedStdout(), "mov: bla,bla,bla...\n"); testing::internal::CaptureStdout(); - auto int_ref_println = lib.get_function("tools::string::println(unsigned int const&)"); + auto int_ref_println = lib.get_function("tools::string::println(unsigned int const &)"); int_ref_println(123); EXPECT_EQ(testing::internal::GetCapturedStdout(), "ref: 123\n"); }