Cache diagnostic message for easier access, and better extern C handling
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
7958576b6a
commit
b76548da16
|
@ -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
|
||||
|
|
|
@ -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" {
|
||||
|
@ -121,6 +127,6 @@ extern "C" {
|
|||
}
|
||||
|
||||
const char * GetFullDiagnostic(DiagnosticsHolder* diagnostics, Diagnostic* diag){
|
||||
return diagnostics ->GetFullDiagnostic(diag).c_str();
|
||||
return diagnostics ->GetFullDiagnostic(diag)->c_str();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -58,7 +58,7 @@ namespace Porygon::Diagnostics {
|
|||
return _lineStarts[i];
|
||||
}
|
||||
|
||||
std::string GetFullDiagnostic(Diagnostic* diagnostic);
|
||||
std::string* GetFullDiagnostic(Diagnostic* diagnostic);
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue