-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
722b1bf
commit bb4889e
Showing
10 changed files
with
327 additions
and
334 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.