diff --git a/src/Diagnostics/Diagnostic.hpp b/src/Diagnostics/Diagnostic.hpp index 3d6a23c..88ee57e 100644 --- a/src/Diagnostics/Diagnostic.hpp +++ b/src/Diagnostics/Diagnostic.hpp @@ -16,6 +16,7 @@ namespace Porygon::Diagnostics { unsigned int _start; unsigned int _length; std::vector _arguments; + std::string* _message; public: Diagnostic(DiagnosticSeverity severity, DiagnosticCode code, unsigned int start, unsigned int length, std::vector arguments) { _severity = severity; @@ -23,6 +24,11 @@ namespace Porygon::Diagnostics { _start = start; _length = length; _arguments = std::move(arguments); + _message = nullptr; + } + + ~Diagnostic(){ + delete _message; } DiagnosticSeverity GetSeverity() { @@ -44,6 +50,14 @@ namespace Porygon::Diagnostics { std::vector GetArguments(){ return _arguments; } + + void SetMessage(std::string* s){ + this -> _message = s; + } + + std::string* GetMessage(){ + return _message; + } }; } #endif //PORYGONLANG_DIAGNOSTIC_HPP diff --git a/src/Diagnostics/DiagnosticsHolder.cpp b/src/Diagnostics/DiagnosticsHolder.cpp index 3891b19..bf5603c 100644 --- a/src/Diagnostics/DiagnosticsHolder.cpp +++ b/src/Diagnostics/DiagnosticsHolder.cpp @@ -97,7 +97,11 @@ const string PrettyDiagnostic ( const char * format, vector arguments) return result; } -std::string DiagnosticsHolder::GetFullDiagnostic(Diagnostic* diagnostic) { +std::string* DiagnosticsHolder::GetFullDiagnostic(Diagnostic* diagnostic) { + auto previousMsg = diagnostic->GetMessage(); + if (previousMsg != nullptr){ + return previousMsg; + } stringstream stream; stream << "[" << SeverityToString(diagnostic->GetSeverity()) << "] "; auto startPos = diagnostic->GetStartPosition(); @@ -108,7 +112,9 @@ std::string DiagnosticsHolder::GetFullDiagnostic(Diagnostic* diagnostic) { if (unformatted != ErrorMessages.end()){ stream << PrettyDiagnostic(unformatted->second, diagnostic->GetArguments()); } - return stream.str(); + auto s = new string(stream.str()); + diagnostic->SetMessage(s); + return s; } extern "C" { @@ -120,7 +126,7 @@ extern "C" { return diagnostics->GetDiagnosticAt(position); } - const char* GetFullDiagnostic(DiagnosticsHolder* diagnostics, Diagnostic* diag){ - return diagnostics ->GetFullDiagnostic(diag).c_str(); + const char * GetFullDiagnostic(DiagnosticsHolder* diagnostics, Diagnostic* diag){ + return diagnostics ->GetFullDiagnostic(diag)->c_str(); } } diff --git a/src/Diagnostics/DiagnosticsHolder.hpp b/src/Diagnostics/DiagnosticsHolder.hpp index 695c279..6341153 100644 --- a/src/Diagnostics/DiagnosticsHolder.hpp +++ b/src/Diagnostics/DiagnosticsHolder.hpp @@ -58,7 +58,7 @@ namespace Porygon::Diagnostics { return _lineStarts[i]; } - std::string GetFullDiagnostic(Diagnostic* diagnostic); + std::string* GetFullDiagnostic(Diagnostic* diagnostic); }; } diff --git a/tests/integration/DiagnosticsTests.cpp b/tests/integration/DiagnosticsTests.cpp index 565dd05..17f3818 100644 --- a/tests/integration/DiagnosticsTests.cpp +++ b/tests/integration/DiagnosticsTests.cpp @@ -48,7 +48,7 @@ TEST_CASE( "Get full diagnostic message", "[integration]" ) { REQUIRE(script->Diagnostics -> HasErrors()); auto diags = script->Diagnostics -> GetDiagnostics(); auto msg = script->Diagnostics->GetFullDiagnostic(&diags[0]); - REQUIRE(msg == "[Error] (2, 2) '\\x' is not a valid control character."); + REQUIRE(*msg == "[Error] (2, 2) '\\x' is not a valid control character."); delete script; }