Skip to content

Commit

Permalink
feat(vscode): better CONST_ASSERT handling, setting to ignore CONST_A…
Browse files Browse the repository at this point in the history
…SSERT errors
  • Loading branch information
EmilyV99 authored and connorjclark committed Aug 6, 2023
1 parent 053e6bb commit db0bc50
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 23 deletions.
3 changes: 0 additions & 3 deletions src/parser/ASTVisitors.cpp
Expand Up @@ -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;

////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions src/parser/CompileError.cpp
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/parser/CompileError.h
Expand Up @@ -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*);
Expand Down
14 changes: 9 additions & 5 deletions src/parser/ScriptParser.cpp
Expand Up @@ -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<ZScript::CompileError> casserts;
unique_ptr<ScriptsData> 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");
Expand Down Expand Up @@ -120,6 +121,9 @@ unique_ptr<ScriptsData> ZScript::compile(string const& filename)
ScriptParser::assemble(id.get());

unique_ptr<ScriptsData> result(new ScriptsData(program));
if(!ignore_asserts)
for(CompileError const& error : casserts)
error.handle();
if(zscript_error_out) return nullptr;

zconsole_info("%s", "Success!");
Expand All @@ -129,7 +133,7 @@ unique_ptr<ScriptsData> 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
Expand All @@ -144,7 +148,7 @@ unique_ptr<ScriptsData> 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
Expand Down
16 changes: 8 additions & 8 deletions src/parser/SemanticAnalyzer.cpp
Expand Up @@ -1094,6 +1094,8 @@ void SemanticAnalyzer::caseImportCondDecl(ASTImportCondDecl& host, void* param)
RecursiveVisitor::caseImportCondDecl(host, param);
}

extern vector<CompileError> casserts;
extern bool delay_asserts;
void SemanticAnalyzer::caseAssert(ASTAssert& host, void* param)
{
visit(host.expr.get(), param);
Expand All @@ -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);
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/parser/parser.cpp
Expand Up @@ -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 = (
Expand Down Expand Up @@ -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];
Expand All @@ -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];
Expand All @@ -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,...)
Expand Down Expand Up @@ -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);
Expand All @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions vscode-extension/package.json
Expand Up @@ -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",
Expand Down
9 changes: 7 additions & 2 deletions vscode-extension/server/src/server.ts
Expand Up @@ -87,6 +87,7 @@ interface Settings {
installationFolder?: string;
printCompilerOutput?: boolean;
alwaysInclude?: Array<string>;
ignoreConstAssert?: boolean;
}

// The global settings, used when the `workspace/configuration` request is not supported by the client.
Expand Down Expand Up @@ -188,10 +189,14 @@ async function processScript(textDocument: TextDocument): Promise<void> {
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;
Expand Down

0 comments on commit db0bc50

Please sign in to comment.