Cache diagnostic message for easier access, and better extern C handling
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2019-06-19 15:49:44 +02:00
parent 7958576b6a
commit b76548da16
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
4 changed files with 26 additions and 6 deletions

View File

@ -16,6 +16,7 @@ namespace Porygon::Diagnostics {
unsigned int _start; unsigned int _start;
unsigned int _length; unsigned int _length;
std::vector<std::string> _arguments; std::vector<std::string> _arguments;
std::string* _message;
public: public:
Diagnostic(DiagnosticSeverity severity, DiagnosticCode code, unsigned int start, unsigned int length, std::vector<std::string> arguments) { Diagnostic(DiagnosticSeverity severity, DiagnosticCode code, unsigned int start, unsigned int length, std::vector<std::string> arguments) {
_severity = severity; _severity = severity;
@ -23,6 +24,11 @@ namespace Porygon::Diagnostics {
_start = start; _start = start;
_length = length; _length = length;
_arguments = std::move(arguments); _arguments = std::move(arguments);
_message = nullptr;
}
~Diagnostic(){
delete _message;
} }
DiagnosticSeverity GetSeverity() { DiagnosticSeverity GetSeverity() {
@ -44,6 +50,14 @@ namespace Porygon::Diagnostics {
std::vector<std::string> GetArguments(){ std::vector<std::string> GetArguments(){
return _arguments; return _arguments;
} }
void SetMessage(std::string* s){
this -> _message = s;
}
std::string* GetMessage(){
return _message;
}
}; };
} }
#endif //PORYGONLANG_DIAGNOSTIC_HPP #endif //PORYGONLANG_DIAGNOSTIC_HPP

View File

@ -97,7 +97,11 @@ const string PrettyDiagnostic ( const char * format, vector<string> arguments)
return result; 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; stringstream stream;
stream << "[" << SeverityToString(diagnostic->GetSeverity()) << "] "; stream << "[" << SeverityToString(diagnostic->GetSeverity()) << "] ";
auto startPos = diagnostic->GetStartPosition(); auto startPos = diagnostic->GetStartPosition();
@ -108,7 +112,9 @@ std::string DiagnosticsHolder::GetFullDiagnostic(Diagnostic* diagnostic) {
if (unformatted != ErrorMessages.end()){ if (unformatted != ErrorMessages.end()){
stream << PrettyDiagnostic(unformatted->second, diagnostic->GetArguments()); stream << PrettyDiagnostic(unformatted->second, diagnostic->GetArguments());
} }
return stream.str(); auto s = new string(stream.str());
diagnostic->SetMessage(s);
return s;
} }
extern "C" { extern "C" {
@ -121,6 +127,6 @@ extern "C" {
} }
const char * GetFullDiagnostic(DiagnosticsHolder* diagnostics, Diagnostic* diag){ const char * GetFullDiagnostic(DiagnosticsHolder* diagnostics, Diagnostic* diag){
return diagnostics ->GetFullDiagnostic(diag).c_str(); return diagnostics ->GetFullDiagnostic(diag)->c_str();
} }
} }

View File

@ -58,7 +58,7 @@ namespace Porygon::Diagnostics {
return _lineStarts[i]; return _lineStarts[i];
} }
std::string GetFullDiagnostic(Diagnostic* diagnostic); std::string* GetFullDiagnostic(Diagnostic* diagnostic);
}; };
} }

View File

@ -48,7 +48,7 @@ TEST_CASE( "Get full diagnostic message", "[integration]" ) {
REQUIRE(script->Diagnostics -> HasErrors()); REQUIRE(script->Diagnostics -> HasErrors());
auto diags = script->Diagnostics -> GetDiagnostics(); auto diags = script->Diagnostics -> GetDiagnostics();
auto msg = script->Diagnostics->GetFullDiagnostic(&diags[0]); 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; delete script;
} }