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 _length;
std::vector<std::string> _arguments;
std::string* _message;
public:
Diagnostic(DiagnosticSeverity severity, DiagnosticCode code, unsigned int start, unsigned int length, std::vector<std::string> 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<std::string> GetArguments(){
return _arguments;
}
void SetMessage(std::string* s){
this -> _message = s;
}
std::string* GetMessage(){
return _message;
}
};
}
#endif //PORYGONLANG_DIAGNOSTIC_HPP

View File

@ -97,7 +97,11 @@ const string PrettyDiagnostic ( const char * format, vector<string> 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();
}
}

View File

@ -58,7 +58,7 @@ namespace Porygon::Diagnostics {
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());
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;
}