Skip to content

Commit

Permalink
fix: add space before pointers and references
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-olivier committed Jul 15, 2024
1 parent 3e88b2a commit ed27053
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 45 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
46 changes: 6 additions & 40 deletions src/dylib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#else
#include <dlfcn.h>
#include <unistd.h>
#include <cxxabi.h>
#include <algorithm>
#endif

Expand Down Expand Up @@ -51,9 +50,12 @@ std::vector<std::string> 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();
}
}

Expand Down Expand Up @@ -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<char *>(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<std::string> matching_symbols;
std::vector<std::string> all_symbols;
Expand Down
67 changes: 67 additions & 0 deletions src/unix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* @file unix.cpp
*
* @author Martin Olivier <[email protected]>
* @copyright (c) 2024 Martin Olivier
*
* This library is released under MIT license
*/

#include <string>
#include <cxxabi.h>

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<char *>(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;
}
4 changes: 4 additions & 0 deletions tests/lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
11 changes: 8 additions & 3 deletions tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<void(const std::string&)>("tools::string::println(std::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)");
auto ptr_println = lib.get_function<void(std::string&&)>("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<void(const std::string&)>("tools::string::println(std::basic_string<char, std::char_traits<char>, std::allocator<char>> const &)");
ref_println(text);
EXPECT_EQ(testing::internal::GetCapturedStdout(), "ref: bla,bla,bla...\n");

testing::internal::CaptureStdout();
auto mov_println = lib.get_function<void(std::string&&)>("tools::string::println(std::basic_string<char, std::char_traits<char>, std::allocator<char>>&&)");
auto mov_println = lib.get_function<void(std::string&&)>("tools::string::println(std::basic_string<char, std::char_traits<char>, std::allocator<char>> &&)");
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<void(const unsigned int&)>("tools::string::println(unsigned int const&)");
auto int_ref_println = lib.get_function<void(const unsigned int&)>("tools::string::println(unsigned int const &)");
int_ref_println(123);
EXPECT_EQ(testing::internal::GetCapturedStdout(), "ref: 123\n");
}
Expand Down

0 comments on commit ed27053

Please sign in to comment.