Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement -assert=[on|off] to enable/disable asserts on demand #6289

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/mars.d
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ Where:
<option>:
@<cmdfile> read arguments from cmdfile
-allinst generate code for all template instantiations
-assert=[on|off] Turn asserts on/off, regardless of the compilation mode
-betterC omit generating some runtime information and helper functions
-boundscheck=[on|safeonly|off] bounds checks on, in @safe only, or off
-c do not link
Expand Down Expand Up @@ -367,13 +368,24 @@ private int tryMain(size_t argc, const(char)** argv)
printf("arguments[%d] = '%s'\n", i, arguments[i]);
}
}

byte assert_flag; // 0 => Default, < 0 => No, > 0 => Yes
for (size_t i = 1; i < arguments.dim; i++)
{
const(char)* p = arguments[i];
if (*p == '-')
{
if (strcmp(p + 1, "allinst") == 0)
global.params.allInst = true;
else if (strncmp(p + 1, "assert", "assert".length) == 0)
{
if (strcmp(p, "-assert=on") == 0)
assert_flag = 1;
else if (strcmp(p, "-assert=off") == 0)
assert_flag = -1;
else
goto Lerror;
}
else if (strcmp(p + 1, "de") == 0)
global.params.useDeprecated = 0;
else if (strcmp(p + 1, "d") == 0)
Expand Down Expand Up @@ -1104,6 +1116,9 @@ Language changes listed by -transition=id:
}
}

if (assert_flag != 0)
global.params.useAssert = assert_flag > 0 ? true : false;

// Predefined version identifiers
addDefaultVersionIdentifiers();
objc_tryMain_dObjc();
Expand Down
10 changes: 10 additions & 0 deletions test/runnable/assert_switch_off.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// REQUIRED_ARGS: -assert=off

void main ()
{
// If asserts are on => assert gets triggered
// If they are off => The assert 'pass'
int i = 42;

assert(i == 0);
}
13 changes: 13 additions & 0 deletions test/runnable/assert_switch_on.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// REQUIRED_ARGS: -release -assert=on

import core.exception;

void main ()
{
// If asserts are off => assert(0) leads to a SEGV
// If they are on => The exception is caught as expected
try { assert(0); }
catch (AssertError e) return;

assert(0);
}