From 73d102b86a0df20aba266b1649637c354de1780d Mon Sep 17 00:00:00 2001 From: Stanislav Gromov Date: Wed, 17 Jun 2020 19:06:26 +0800 Subject: [PATCH 1/3] Add a distinct error for division by zero --- source/compiler/sc3.c | 2 +- source/compiler/sc5.c | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/source/compiler/sc3.c b/source/compiler/sc3.c index 46486997..0c69c595 100644 --- a/source/compiler/sc3.c +++ b/source/compiler/sc3.c @@ -644,7 +644,7 @@ static cell flooreddiv(cell a,cell b,int return_remainder) cell q,r; if (b==0) { - error(29); + error(94); /* division by zero */ return 0; } /* if */ /* first implement truncated division in a portable way */ diff --git a/source/compiler/sc5.c b/source/compiler/sc5.c index c5b31845..09c477ce 100644 --- a/source/compiler/sc5.c +++ b/source/compiler/sc5.c @@ -132,7 +132,8 @@ static char *errmsg[] = { /*090*/ "public functions may not return arrays (symbol \"%s\")\n", /*091*/ "ambiguous constant; tag override is required (symbol \"%s\")\n", /*092*/ "functions may not return arrays of unknown size (symbol \"%s\")\n", -/*093*/ "\"__addressof\" operator is invalid in preprocessor expressions\n" +/*093*/ "\"__addressof\" operator is invalid in preprocessor expressions\n", +/*094*/ "division by zero\n" }; static char *fatalmsg[] = { From 8272c9fbb626aa9a27bb3b96bd9da23e92683a2c Mon Sep 17 00:00:00 2001 From: Stanislav Gromov Date: Wed, 17 Jun 2020 19:24:32 +0800 Subject: [PATCH 2/3] Fix error 094 not being printed if the dividend is not a constant value --- source/compiler/sc3.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/source/compiler/sc3.c b/source/compiler/sc3.c index 0c69c595..2ac50114 100644 --- a/source/compiler/sc3.c +++ b/source/compiler/sc3.c @@ -620,6 +620,10 @@ static void plnge2(void (*oper)(void), } /* if */ /* ??? ^^^ should do same kind of error checking with functions */ + /* If we're handling a division operation, make sure the divisor is not zero. */ + if ((oper==os_div || oper==os_mod) && lval2->ident==iCONSTEXPR && lval2->constval==0) + error(94); /* division by zero */ + /* check whether an "operator" function is defined for the tag names * (a constant expression cannot be optimized in that case) */ @@ -643,10 +647,8 @@ static cell flooreddiv(cell a,cell b,int return_remainder) { cell q,r; - if (b==0) { - error(94); /* division by zero */ + if (b==0) return 0; - } /* if */ /* first implement truncated division in a portable way */ #define IABS(a) ((a)>=0 ? (a) : (-a)) q=IABS(a)/IABS(b); From 313b071e3fb4a0c75bc3956112991151787af4e2 Mon Sep 17 00:00:00 2001 From: Stanislav Gromov Date: Wed, 17 Jun 2020 19:59:59 +0800 Subject: [PATCH 3/3] Add tests --- source/compiler/tests/error_094.meta | 9 +++++++++ source/compiler/tests/error_094.pwn | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 source/compiler/tests/error_094.meta create mode 100644 source/compiler/tests/error_094.pwn diff --git a/source/compiler/tests/error_094.meta b/source/compiler/tests/error_094.meta new file mode 100644 index 00000000..fecd2763 --- /dev/null +++ b/source/compiler/tests/error_094.meta @@ -0,0 +1,9 @@ +{ + 'test_type': 'output_check', + 'errors': """ +error_094.pwn(6) : error 094: division by zero +error_094.pwn(7) : error 094: division by zero +error_094.pwn(13) : error 094: division by zero +error_094.pwn(14) : error 094: division by zero +""" +} diff --git a/source/compiler/tests/error_094.pwn b/source/compiler/tests/error_094.pwn new file mode 100644 index 00000000..81468763 --- /dev/null +++ b/source/compiler/tests/error_094.pwn @@ -0,0 +1,20 @@ +#include + +main() +{ + // Case 1: Both operands are compile-time constants + printf("%d", 1 / 0); // error 094 + printf("%d", 1 % 0); // error 094 + printf("%d", 1 / 1); + printf("%d", 1 % 1); + + // Case 2: Only the divisor is a constant + new var = 0; + printf("%d", var / 0); // error 094 + printf("%d", var % 0); // error 094 + printf("%d", var / 1); + printf("%d", var % 1); + + printf("%d", 1 / var); // Just to make sure the error works only + printf("%d", 1 % var); // if the divisor is a constant value +}