Skip to content

Commit 7d744cd

Browse files
committed
Format function signatures so they're slightly more sane
1 parent 295ce28 commit 7d744cd

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

System/RTEError.cpp

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ namespace RTE {
9191

9292
if (SymFromAddr(procHandle, exceptAddr, nullptr, symbolInfo)) {
9393
std::string symbolName = symbolInfo->Name;
94-
return "The symbol name at this address is " + (symbolName.empty() ? "empty for reasons unknown to man." : "'" + symbolName + "'.");
94+
return "The symbol name at this address is" + (symbolName.empty() ? " empty for reasons unknown to man." : ": \"" + symbolName + "\"");
9595
} else {
9696
return "Unable to get symbol name at address because:\n\n" + getLastWinErrorAsString();
9797
}
@@ -112,7 +112,10 @@ namespace RTE {
112112
return EXCEPTION_CONTINUE_EXECUTION;
113113
}
114114

115-
exceptionDescription << getExceptionDescriptionFromCode(exceptionCode) << " at address 0x" << std::uppercase << std::hex << exceptionAddress << ".\n\n" << getSymbolNameFromAddress(processHandle, exceptionAddress) << std::endl;
115+
std::string symbolNameAtAddress = getSymbolNameFromAddress(processHandle, exceptionAddress);
116+
RTEError::FormatFunctionSignature(symbolNameAtAddress);
117+
118+
exceptionDescription << getExceptionDescriptionFromCode(exceptionCode) << " at address 0x" << std::uppercase << std::hex << exceptionAddress << ".\n\n" << symbolNameAtAddress << std::endl;
116119

117120
RTEStackTrace stackTrace;
118121

@@ -291,6 +294,7 @@ namespace RTE {
291294

292295
std::string lineNum = std::to_string(srcLocation.line());
293296
std::string funcName = srcLocation.function_name();
297+
FormatFunctionSignature(funcName);
294298

295299
std::string abortMessage = "Runtime Error in file '" + fileName + "', line " + lineNum + ",\nin function '" + funcName + "'\nbecause:\n\n" + description + "\n";
296300

@@ -406,4 +410,28 @@ namespace RTE {
406410
}
407411
return success;
408412
}
413+
414+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
415+
416+
void RTEError::FormatFunctionSignature(std::string &symbolName) {
417+
// TODO: Expand this with more dumb signatures, or make something that makes more sense.
418+
static const std::array<std::pair<std::regex, std::string>, 3> stlSigs {{
419+
{std::regex("( >)"), ">"},
420+
{std::regex("(std::basic_string<char,std::char_traits<char>,std::allocator<char>>)"), "std::string"},
421+
{std::regex("(class ?std::basic_string<char,struct ?std::char_traits<char>,class ?std::allocator<char>>)"), "std::string"}
422+
}};
423+
for (const auto &[fullSig, simpleSig] : stlSigs) {
424+
symbolName = std::regex_replace(symbolName, fullSig, simpleSig);
425+
}
426+
for (size_t pos = 0;;) {
427+
pos += 100;
428+
if (pos < symbolName.size()) {
429+
if (size_t lastCommaPos = symbolName.find_last_of(',', pos); lastCommaPos != std::string::npos) {
430+
symbolName.insert(lastCommaPos + 1, "\n");
431+
}
432+
} else {
433+
break;
434+
}
435+
}
436+
}
409437
}

System/RTEError.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ namespace RTE {
6464
/// <param name="srcLocation">std::source_location corresponding to the location of the call site.</param>
6565
static void AssertFunc(const std::string &description, const std::source_location &srcLocation);
6666

67+
/// <summary>
68+
/// Formats function signatures so they're slightly more sane.
69+
/// </summary>
70+
/// <param name="funcSig">Reference to the function signature to format.</param>
71+
static void FormatFunctionSignature(std::string &funcSig);
72+
6773
private:
6874

6975
/// <summary>

System/StandardIncludes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
#include <atomic>
8585
#include <execution>
8686
#include <source_location>
87+
#include <regex>
8788

8889
namespace std {
8990

0 commit comments

Comments
 (0)