Skip to content

Commit

Permalink
Add -boundscheck=[on|safeonly|off] option
Browse files Browse the repository at this point in the history
Replaces -noboundscheck (which becomes deprecated)

Fixes issue #12550
  • Loading branch information
brad-anderson committed Apr 11, 2014
1 parent 212814e commit 23748b9
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 6 deletions.
44 changes: 38 additions & 6 deletions src/mars.c
Expand Up @@ -445,7 +445,8 @@ Usage:\n\
-main add default main() (e.g. for unittesting)\n\
-man open web browser on manual page\n\
-map generate linker .map file\n\
-noboundscheck turns off array bounds checking for all functions (including @safe)\n\
-boundscheck=[on|safeonly|off] bounds checks on, in @safe only, or off\n\
-noboundscheck no array bounds checking (deprecated, use -boundscheck=off)\n\
-O optimize\n\
-o- do not write object file\n\
-odobjdir write object & library files to directory objdir\n\
Expand Down Expand Up @@ -528,7 +529,8 @@ int tryMain(size_t argc, const char *argv[])
Strings libmodules;
size_t argcstart = argc;
bool setdebuglib = false;
bool noboundscheck = false;
bool setboundscheck = false;
char boundscheck = 2;
bool setdefaultlib = false;
const char *inifilename = NULL;
global.init();
Expand Down Expand Up @@ -918,7 +920,37 @@ Language changes listed by -transition=id:\n\
else if (strcmp(p + 1, "betterC") == 0)
global.params.betterC = true;
else if (strcmp(p + 1, "noboundscheck") == 0)
noboundscheck = true;
{
setboundscheck = true;
boundscheck = 0;
}
else if (memcmp(p + 1, "boundscheck", 11) == 0)
{
// Parse:
// -boundscheck=[on|safeonly|off]
if (p[12] == '=')
{
if (strcmp(p + 13, "on") == 0)
{
setboundscheck = true;
boundscheck = 2;
}
else if (strcmp(p + 13, "safeonly") == 0)
{
setboundscheck = true;
boundscheck = 1;
}
else if (strcmp(p + 13, "off") == 0)
{
setboundscheck = true;
boundscheck = 0;
}
else
goto Lerror;
}
else
goto Lerror;
}
else if (strcmp(p + 1, "unittest") == 0)
global.params.useUnitTests = true;
else if (p[1] == 'I')
Expand Down Expand Up @@ -1144,8 +1176,8 @@ Language changes listed by -transition=id:\n\
global.params.useArrayBounds = 1;
global.params.useSwitchError = false;
}
if (noboundscheck)
global.params.useArrayBounds = 0;
if (setboundscheck)
global.params.useArrayBounds = boundscheck;

if (global.params.useUnitTests)
global.params.useAssert = true;
Expand Down Expand Up @@ -1234,7 +1266,7 @@ Language changes listed by -transition=id:\n\
VersionCondition::addPredefinedGlobalIdent("unittest");
if (global.params.useAssert)
VersionCondition::addPredefinedGlobalIdent("assert");
if (noboundscheck)
if (boundscheck == 0)
VersionCondition::addPredefinedGlobalIdent("D_NoBoundsChecks");

VersionCondition::addPredefinedGlobalIdent("D_HardFloat");
Expand Down
27 changes: 27 additions & 0 deletions test/runnable/testbounds_off.d
@@ -0,0 +1,27 @@
// REQUIRED_ARGS: -boundscheck=off
// PERMUTE_ARGS: -inline -g -O

import core.exception : RangeError;

// Check for RangeError is thrown
bool thrown(T)(lazy T cond)
{
import core.exception;
bool f = false;
try { cond(); } catch (RangeError e) { f = true; }
return f;
}

@safe int safeIndex (int[] arr) { return arr[2]; }
@trusted int trustedIndex(int[] arr) { return arr[2]; }
@system int systemIndex (int[] arr) { return arr[2]; }

void main()
{
int[3] data = [1,2,3];
int[] arr = data[0..2];

assert(arr. safeIndex() == 3);
assert(arr.trustedIndex() == 3);
assert(arr. systemIndex() == 3);
}
27 changes: 27 additions & 0 deletions test/runnable/testbounds_on.d
@@ -0,0 +1,27 @@
// REQUIRED_ARGS: -boundscheck=on
// PERMUTE_ARGS: -inline -g -O

import core.exception : RangeError;

// Check for RangeError is thrown
bool thrown(T)(lazy T cond)
{
import core.exception;
bool f = false;
try { cond(); } catch (RangeError e) { f = true; }
return f;
}

@safe int safeIndex (int[] arr) { return arr[2]; }
@trusted int trustedIndex(int[] arr) { return arr[2]; }
@system int systemIndex (int[] arr) { return arr[2]; }

void main()
{
int[3] data = [1,2,3];
int[] arr = data[0..2];

assert(arr. safeIndex().thrown);
assert(arr.trustedIndex().thrown);
assert(arr. systemIndex().thrown);
}
27 changes: 27 additions & 0 deletions test/runnable/testbounds_safeonly.d
@@ -0,0 +1,27 @@
// REQUIRED_ARGS: -boundscheck=safeonly
// PERMUTE_ARGS: -inline -g -O

import core.exception : RangeError;

// Check for RangeError is thrown
bool thrown(T)(lazy T cond)
{
import core.exception;
bool f = false;
try { cond(); } catch (RangeError e) { f = true; }
return f;
}

@safe int safeIndex (int[] arr) { return arr[2]; }
@trusted int trustedIndex(int[] arr) { return arr[2]; }
@system int systemIndex (int[] arr) { return arr[2]; }

void main()
{
int[3] data = [1,2,3];
int[] arr = data[0..2];

assert(arr. safeIndex().thrown);
assert(arr.trustedIndex() == 3);
assert(arr. systemIndex() == 3);
}

0 comments on commit 23748b9

Please sign in to comment.