Skip to content

Commit

Permalink
fix: reorganise source files
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-olivier committed Jul 26, 2024
1 parent 722b1bf commit bb4889e
Show file tree
Hide file tree
Showing 10 changed files with 327 additions and 334 deletions.
15 changes: 6 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@ if(NOT "${CMAKE_CXX_STANDARD}")
endif()
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(SOURCES src/dylib.cpp)

if(APPLE)
list(APPEND SOURCES src/mac.cpp src/unix.cpp)
elseif(WIN32 AND NOT MINGW)
list(APPEND SOURCES src/win.cpp)
else()
list(APPEND SOURCES src/linux.cpp src/unix.cpp)
endif()
set(SOURCES
src/dylib.cpp
src/symbols.cpp
src/demangle.cpp
src/format.cpp
)

add_library(dylib STATIC ${SOURCES})

Expand Down
65 changes: 65 additions & 0 deletions src/demangle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <string>

std::string format_symbol(std::string input);

#if (defined(_WIN32) || defined(_WIN64))

std::string get_demangled_name(const char *symbol) {
char undecorated[MAX_SYM_NAME];
DWORD flags = UNDNAME_COMPLETE | UNDNAME_NO_FUNCTION_RETURNS | UNDNAME_NO_MS_KEYWORDS;

// Get undecorated symbol signature
if (UnDecorateSymbolName(symbol, undecorated, MAX_SYM_NAME, flags)) {
std::string signature = undecorated;

// Get undecorated symbol name
if (UnDecorateSymbolName(symbol, undecorated, MAX_SYM_NAME, UNDNAME_NAME_ONLY)) {
/*
* If symbol signature starts with symbol name, it means
* that this is a function, otherwise, this is a variable:
*
* signature: tools::adder(double, double)
* symbol name: tools::adder
*
* signature: long ptr
* symbol name: ptr
*/
if (signature.find(undecorated) == 0)
return format_symbol(signature);
else
return format_symbol(undecorated);
}
}

return "";
}

#else

#include <cxxabi.h>

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
35 changes: 2 additions & 33 deletions src/dylib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,41 +37,10 @@
#define DYLIB_WIN_OTHER(win_def, other_def) other_def
#endif

/* PRIVATE */
using os_fd_t = DYLIB_WIN_OTHER(HMODULE, int);

std::string get_demangled_name(const char *symbol);

#if (defined(_WIN32) || defined(_WIN64))
std::vector<std::string> get_symbols(HMODULE hModule, bool demangle);
#else
std::vector<std::string> get_symbols(int fd, bool demangle);
#endif

void replace_occurrences(std::string &input, const std::string &keyword, const std::string &replacement) {
size_t pos = 0;

if (keyword.empty())
return;

while ((pos = input.find(keyword, pos)) != std::string::npos) {
input.replace(pos, keyword.length(), replacement);
pos += replacement.length();
}
}

void add_space_after_comma(std::string &input) {
std::string result;

for (char c : input) {
if (c == ',') {
result += ", ";
} else {
result += c;
}
}

input = result;
}
std::vector<std::string> get_symbols(os_fd_t fd, bool demangle);

static dylib::native_handle_type open_lib(const char *path) noexcept {
#if (defined(_WIN32) || defined(_WIN64))
Expand Down
93 changes: 93 additions & 0 deletions src/format.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* @file format.cpp
*
* @author Martin Olivier <[email protected]>
* @copyright (c) 2024 Martin Olivier
*
* This library is released under MIT license
*/

#include <string>

static void replace_occurrences(std::string &input, const std::string &keyword, const std::string &replacement) {
size_t pos = 0;

if (keyword.empty())
return;

while ((pos = input.find(keyword, pos)) != std::string::npos) {
input.replace(pos, keyword.length(), replacement);
pos += replacement.length();
}
}

#if (defined(_WIN32) || defined(_WIN64))

static void add_space_after_comma(std::string &input) {
std::string result;

for (char c : input) {
if (c == ',') {
result += ", ";
} else {
result += c;
}
}

input = result;
}

std::string format_symbol(std::string input) {
replace_occurrences(input, "(class ", "(");
replace_occurrences(input, "<class ", "<");
replace_occurrences(input, ",class ", ",");

replace_occurrences(input, "(struct ", "(");
replace_occurrences(input, "<struct ", "<");
replace_occurrences(input, ",struct ", ",");

replace_occurrences(input, "> >", ">>");
replace_occurrences(input, ">const", "> const");

add_space_after_comma(input);

return input;
}

#else

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, "[abi:cxx11]", "");
replace_occurrences(input, "[abi:ue170006]", "");

replace_occurrences(input, "()", "(void)");
replace_occurrences(input, "> >", ">>");

add_sym_separator(input, '*');
add_sym_separator(input, '&');

return input;
}

#endif

90 changes: 0 additions & 90 deletions src/linux.cpp

This file was deleted.

Loading

0 comments on commit bb4889e

Please sign in to comment.