From db0bc504bc143caba37db7f26b5eb7dcc25e137b Mon Sep 17 00:00:00 2001 From: EmilyV99 Date: Sun, 6 Aug 2023 04:44:34 -0400 Subject: [PATCH] feat(vscode): better CONST_ASSERT handling, setting to ignore CONST_ASSERT errors --- src/parser/ASTVisitors.cpp | 3 --- src/parser/CompileError.cpp | 17 +++++++++++++++++ src/parser/CompileError.h | 2 ++ src/parser/ScriptParser.cpp | 14 +++++++++----- src/parser/SemanticAnalyzer.cpp | 16 ++++++++-------- src/parser/parser.cpp | 10 +++++----- vscode-extension/package.json | 6 ++++++ vscode-extension/server/src/server.ts | 9 +++++++-- 8 files changed, 54 insertions(+), 23 deletions(-) diff --git a/src/parser/ASTVisitors.cpp b/src/parser/ASTVisitors.cpp index 5933eda1b5..724cc0acbb 100644 --- a/src/parser/ASTVisitors.cpp +++ b/src/parser/ASTVisitors.cpp @@ -19,7 +19,6 @@ void* const RecursiveVisitor::paramWrite = new tag(); void* const RecursiveVisitor::paramReadWrite = new tag(); uint32_t zscript_failcode = 0; -bool zscript_had_warn_err = false; bool zscript_error_out = false; //////////////////////////////////////////////////////////////// @@ -71,8 +70,6 @@ void RecursiveVisitor::handleError(CompileError const& error, std::string const* char const* err_str_ptr = err_str.c_str(); while(err_str_ptr[0]==' '||err_str_ptr[0]=='\r'||err_str_ptr[0]=='\n') ++err_str_ptr; - zscript_had_warn_err = true; - if (error.isStrict()) { if(hard_error) failure_halt = true; diff --git a/src/parser/CompileError.cpp b/src/parser/CompileError.cpp index 76ae6fac86..87f924d154 100644 --- a/src/parser/CompileError.cpp +++ b/src/parser/CompileError.cpp @@ -291,6 +291,23 @@ string CompileError::toString() const return oss.str(); } +void CompileError::print() const +{ + if(isStrict()) + zconsole_error("%s",toString().c_str()); + else + zconsole_warn("%s",toString().c_str()); +} +extern bool zscript_error_out; +extern uint32_t zscript_failcode; +void CompileError::handle() const +{ + print(); + if(!zscript_failcode && isStrict()) + zscript_failcode = *getId(); + zscript_error_out = true; +} + CompileError::CompileError(CompileError::Impl* pimpl) : pimpl_(pimpl) {} void ZScript::log_error(CompileError const& error) diff --git a/src/parser/CompileError.h b/src/parser/CompileError.h index 598ed6cee7..d6b9787c9b 100644 --- a/src/parser/CompileError.h +++ b/src/parser/CompileError.h @@ -73,6 +73,8 @@ namespace ZScript // Get if strict (an error), or not (a warning). bool isStrict() const; std::string toString() const; + void print() const; + void handle() const; private: CompileError(Impl*); diff --git a/src/parser/ScriptParser.cpp b/src/parser/ScriptParser.cpp index d750f6e4cb..b5eebebe25 100644 --- a/src/parser/ScriptParser.cpp +++ b/src/parser/ScriptParser.cpp @@ -54,15 +54,16 @@ void ScriptParser::initialize() includePaths.resize(0); } extern uint32_t zscript_failcode; -extern bool zscript_had_warn_err; extern bool zscript_error_out; +extern bool delay_asserts, ignore_asserts; +vector casserts; unique_ptr ZScript::compile(string const& filename) { zscript_failcode = 0; - zscript_had_warn_err = false; zscript_error_out = false; ScriptParser::initialize(); - + if(ignore_asserts) delay_asserts = true; + casserts.clear(); try { zconsole_info("%s", "Pass 1: Parsing"); @@ -120,6 +121,9 @@ unique_ptr ZScript::compile(string const& filename) ScriptParser::assemble(id.get()); unique_ptr result(new ScriptsData(program)); + if(!ignore_asserts) + for(CompileError const& error : casserts) + error.handle(); if(zscript_error_out) return nullptr; zconsole_info("%s", "Success!"); @@ -129,7 +133,7 @@ unique_ptr ZScript::compile(string const& filename) catch (compile_exception &e) { zconsole_error(fmt::format("An unexpected compile error has occurred:\n{}",e.what())); - zscript_had_warn_err = zscript_error_out = true; + zscript_error_out = true; return nullptr; } #ifndef _DEBUG @@ -144,7 +148,7 @@ unique_ptr ZScript::compile(string const& filename) #endif zconsole_error(fmt::format("An unexpected runtime error has occurred:\n{}",e.what())); - zscript_had_warn_err = zscript_error_out = true; + zscript_error_out = true; return nullptr; } #endif diff --git a/src/parser/SemanticAnalyzer.cpp b/src/parser/SemanticAnalyzer.cpp index e77f3f5c9a..59b2e7f4dc 100644 --- a/src/parser/SemanticAnalyzer.cpp +++ b/src/parser/SemanticAnalyzer.cpp @@ -1094,6 +1094,8 @@ void SemanticAnalyzer::caseImportCondDecl(ASTImportCondDecl& host, void* param) RecursiveVisitor::caseImportCondDecl(host, param); } +extern vector casserts; +extern bool delay_asserts; void SemanticAnalyzer::caseAssert(ASTAssert& host, void* param) { visit(host.expr.get(), param); @@ -1102,14 +1104,12 @@ void SemanticAnalyzer::caseAssert(ASTAssert& host, void* param) if(val == 0) { ASTString* str = host.msg.get(); - if(str) - { - handleError(CompileError::AssertFail(&host, str->getValue().c_str())); - } - else - { - handleError(CompileError::AssertFail(&host, "")); - } + CompileError cassert = str + ? CompileError::AssertFail(&host, str->getValue().c_str()) + : CompileError::AssertFail(&host, ""); + if(delay_asserts) + casserts.push_back(cassert); + else handleError(cassert); } } diff --git a/src/parser/parser.cpp b/src/parser/parser.cpp index 51962ca880..caad618fd4 100644 --- a/src/parser/parser.cpp +++ b/src/parser/parser.cpp @@ -20,7 +20,6 @@ extern byte monochrome_console; io_manager* ConsoleWrite; extern uint32_t zscript_failcode; -extern bool zscript_had_warn_err; extern bool zscript_error_out; const int BUILDTM_YEAR = ( @@ -116,7 +115,6 @@ void zconsole_db(std::string const& str) } void zconsole_warn(const char *format,...) { - zscript_had_warn_err = true; //{ int32_t ret; char tmp[1024]; @@ -136,12 +134,10 @@ void zconsole_warn(const char *format,...) } void zconsole_warn(std::string const& str) { - zscript_had_warn_err = true; _console_print(str.c_str(), ZC_CONSOLE_WARN_CODE); } void zconsole_error(const char *format,...) { - zscript_had_warn_err = true; //{ int32_t ret; char tmp[1024]; @@ -161,7 +157,6 @@ void zconsole_error(const char *format,...) } void zconsole_error(std::string const& str) { - zscript_had_warn_err = true; _console_print(str.c_str(), ZC_CONSOLE_ERROR_CODE); } void zconsole_info(const char *format,...) @@ -268,6 +263,7 @@ void updateIncludePaths() ZQincludePaths = split(includePathString, ';'); } +bool delay_asserts = false, ignore_asserts = false; int32_t main(int32_t argc, char **argv) { common_main_setup(App::zscript, argc, argv); @@ -280,6 +276,10 @@ int32_t main(int32_t argc, char **argv) } else return 1; } + if(used_switch(argc, argv, "-ignore_cassert")) + delay_asserts = ignore_asserts = true; + else if(used_switch(argc, argv, "-delay_cassert")) + delay_asserts = true; int32_t console_path_index = used_switch(argc, argv, "-console"); if (linked && !console_path_index) diff --git a/vscode-extension/package.json b/vscode-extension/package.json index 3c98f950e4..8c0820f3ca 100644 --- a/vscode-extension/package.json +++ b/vscode-extension/package.json @@ -45,6 +45,12 @@ "default": false, "description": "Prints the output of the compiler to the Output window" }, + "zscript.ignoreConstAssert": { + "scope": "window", + "type": "boolean", + "default": false, + "description": "Ignores 'CONST_ASSERT' statement errors" + }, "zscript.alwaysInclude": { "scope": "window", "type": "array", diff --git a/vscode-extension/server/src/server.ts b/vscode-extension/server/src/server.ts index 41a639f04d..844f6d8ac0 100644 --- a/vscode-extension/server/src/server.ts +++ b/vscode-extension/server/src/server.ts @@ -87,6 +87,7 @@ interface Settings { installationFolder?: string; printCompilerOutput?: boolean; alwaysInclude?: Array; + ignoreConstAssert?: boolean; } // The global settings, used when the `workspace/configuration` request is not supported by the client. @@ -188,10 +189,14 @@ async function processScript(textDocument: TextDocument): Promise { fs.writeFileSync(tmpScript, text); const exe = os.platform() === 'win32' ? './zscript.exe' : './zscript'; try { - const cp = await execFile(exe, [ + const args = [ '-unlinked', '-input', tmpInput, - ], { + '-delay_cassert' + ]; + if (settings.ignoreConstAssert) + args.push('-ignore_cassert'); + const cp = await execFile(exe, args, { cwd: settings.installationFolder, }); success = true;