Skip to content

Commit

Permalink
Added message severity to Quirrel compiler error handler
Browse files Browse the repository at this point in the history
To prevent compiler hints from failing tests.
  • Loading branch information
VasiliyRyabtsev committed Oct 30, 2024
1 parent 76078d9 commit c94916f
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 11 deletions.
7 changes: 5 additions & 2 deletions doc/repl/native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,13 @@ static void script_err_print_func(HSQUIRRELVM /*v*/, const SQChar* s,...)
}


static void compile_error_handler(HSQUIRRELVM /*v*/, const SQChar *desc, const SQChar *source,
static void compile_error_handler(HSQUIRRELVM /*v*/, SQMessageSeverity sev, const SQChar *desc, const SQChar *source,
SQInteger line, SQInteger column, const SQChar *extra_info)
{
output += vaformat("Squirrel compile error %s (%d:%d): %s", source, int(line), int(column), desc);
const SQChar *sevName = "error";
if (sev == SEV_HINT) sevName = "hint";
else if (sev == SEV_WARNING) sevName = "warning";
output += vaformat("Squirrel compile %s %s (%d:%d): %s", sevText, source, int(line), int(column), desc);
if (extra_info)
{
output += "\n";
Expand Down
8 changes: 7 additions & 1 deletion include/squirrel.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,18 @@ typedef struct tagSQStackInfos{
SQInteger line;
}SQStackInfos;

typedef enum tagSQMessageSeverity{
SEV_HINT,
SEV_WARNING,
SEV_ERROR,
}SQMessageSeverity;

typedef struct SQVM* HSQUIRRELVM;
typedef SQObject HSQOBJECT;
typedef SQMemberHandle HSQMEMBERHANDLE;
typedef SQInteger (*SQFUNCTION)(HSQUIRRELVM);
typedef SQInteger (*SQRELEASEHOOK)(SQUserPointer,SQInteger size);
typedef void (*SQCOMPILERERROR)(HSQUIRRELVM,const SQChar * /*desc*/,const SQChar * /*source*/,SQInteger /*line*/,SQInteger /*column*/, const SQChar * /*extra info*/);
typedef void (*SQCOMPILERERROR)(HSQUIRRELVM,SQMessageSeverity /*severity*/,const SQChar * /*desc*/,const SQChar * /*source*/,SQInteger /*line*/,SQInteger /*column*/, const SQChar * /*extra info*/);
typedef void (*SQPRINTFUNCTION)(HSQUIRRELVM,const SQChar * ,...);
typedef void (*SQDEBUGHOOK)(HSQUIRRELVM /*v*/, SQInteger /*type*/, const SQChar * /*sourcename*/, SQInteger /*line*/, const SQChar * /*funcname*/);
typedef SQInteger (*SQWRITEFUNC)(SQUserPointer,SQUserPointer,SQInteger);
Expand Down
4 changes: 2 additions & 2 deletions sqstdlib/sqstdaux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ static SQInteger _sqstd_aux_printerror(HSQUIRRELVM v)
return 0;
}

void _sqstd_compiler_error(HSQUIRRELVM v,const SQChar *sErr,const SQChar *sSource,SQInteger line,SQInteger column, const SQChar *extra)
void _sqstd_compiler_message(HSQUIRRELVM v,SQMessageSeverity severity,const SQChar *sErr,const SQChar *sSource,SQInteger line,SQInteger column, const SQChar *extra)
{
SQPRINTFUNCTION pf = sq_geterrorfunc(v);
if(pf) {
Expand All @@ -199,7 +199,7 @@ void _sqstd_compiler_error(HSQUIRRELVM v,const SQChar *sErr,const SQChar *sSourc

void sqstd_seterrorhandlers(HSQUIRRELVM v)
{
sq_setcompilererrorhandler(v,_sqstd_compiler_error);
sq_setcompilererrorhandler(v,_sqstd_compiler_message);
sq_newclosure(v,_sqstd_aux_printerror,0);
sq_seterrorhandler(v);
}
Expand Down
2 changes: 1 addition & 1 deletion squirrel/sqbinaryast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ void SQASTReader::error(const char *fmt, ...) {
vsnprintf(buffer, sizeof buffer, fmt, vl);
va_end(vl);

_ss(vm)->_compilererrorhandler(vm, buffer, "BinaryAST", -1, -1, nullptr);
_ss(vm)->_compilererrorhandler(vm, SEV_ERROR, buffer, "BinaryAST", -1, -1, nullptr);
}
longjmp(_errorjmp, 1);
}
Expand Down
9 changes: 6 additions & 3 deletions squirrel/sqcompilationcontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ void SQCompilationContext::vreportDiagnostic(enum DiagnosticsId diagId, int32_t
extra = extraInfo.c_str();
}

auto errorFunc = _ss(_vm)->_compilererrorhandler;
auto messageFunc = _ss(_vm)->_compilererrorhandler;

const char *msg = message.c_str();

Expand All @@ -539,8 +539,11 @@ void SQCompilationContext::vreportDiagnostic(enum DiagnosticsId diagId, int32_t
diagMsgFunc(_vm, &cm);
}

if (_raiseError && errorFunc) {
errorFunc(_vm, msg, _sourceName, line, pos, extra);
if (_raiseError && messageFunc) {
SQMessageSeverity sev = SEV_ERROR;
if (desc.severity == DS_HINT) sev = SEV_HINT;
else if (desc.severity == DS_WARNING) sev = SEV_WARNING;
messageFunc(_vm, sev, msg, _sourceName, line, pos, extra);
}
if (isError) {
_vm->_lasterror = SQString::Create(_ss(_vm), msg, message.length());
Expand Down
4 changes: 2 additions & 2 deletions squirrel/static_analyzer/analyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8067,7 +8067,7 @@ void StaticAnalyzer::reportGlobalNamesWarnings(HSQUIRRELVM vm) {

message.clear();
SQCompilationContext::renderDiagnosticHeader(DiagnosticsId::DI_GLOBAL_NAME_REDEF, &message, name);
errorFunc(vm, message.c_str(), loc.filename, loc.line, loc.column, "\n");
errorFunc(vm, SEV_WARNING, message.c_str(), loc.filename, loc.line, loc.column, "\n");
}
}

Expand All @@ -8092,7 +8092,7 @@ void StaticAnalyzer::reportGlobalNamesWarnings(HSQUIRRELVM vm) {

message.clear();
SQCompilationContext::renderDiagnosticHeader(DiagnosticsId::DI_UNDEFINED_GLOBAL, &message, id.c_str());
errorFunc(vm, message.c_str(), loc.filename, loc.line, loc.column, "\n");
errorFunc(vm, SEV_WARNING, message.c_str(), loc.filename, loc.line, loc.column, "\n");
}
}
}
Expand Down

0 comments on commit c94916f

Please sign in to comment.