From c5ce18591f9cf3fd9b6658a8844fa1039f425e2f Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Sat, 15 Feb 2020 17:24:53 +0900 Subject: [PATCH] Catch and warn demotion of double in assignments --- tools/qfcc/source/expr_assign.c | 4 ++++ tools/qfcc/test/Makefile.am | 10 +++++++++- tools/qfcc/test/double-demote-float-linit.r | 6 ++++++ tools/qfcc/test/double-demote-int-linit.r | 6 ++++++ 4 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 tools/qfcc/test/double-demote-float-linit.r create mode 100644 tools/qfcc/test/double-demote-int-linit.r diff --git a/tools/qfcc/source/expr_assign.c b/tools/qfcc/source/expr_assign.c index 1797f7677..600c6140a 100644 --- a/tools/qfcc/source/expr_assign.c +++ b/tools/qfcc/source/expr_assign.c @@ -151,6 +151,10 @@ check_types_compatible (expr_t *dst, expr_t *src) if (type_assignable (dst_type, src_type)) { if (is_scalar (dst_type) && is_scalar (src_type)) { + if (is_double (src_type)) { + warning (dst, "assignment of double to %s (use a cast)\n", + dst_type->name); + } // the types are different but cast-compatible expr_t *new = cast_expr (dst_type, src); // the cast was a no-op, so the types are compatible at the diff --git a/tools/qfcc/test/Makefile.am b/tools/qfcc/test/Makefile.am index 32e5cd5ec..1d3d9de9c 100644 --- a/tools/qfcc/test/Makefile.am +++ b/tools/qfcc/test/Makefile.am @@ -61,8 +61,10 @@ test_progs_dat=\ fail_progs_dat= test_build_errors=\ - double-demote-int.r \ double-demote-float.r \ + double-demote-float-linit.r \ + double-demote-int.r \ + double-demote-int-linit.r \ double-int-compare.r \ double-float-compare.r @@ -161,6 +163,12 @@ double-demote-int.run$(EXEEXT): double-demote-int.r Makefile build-compile-fail- double-demote-float.run$(EXEEXT): double-demote-float.r Makefile build-compile-fail-run $(srcdir)/build-compile-fail-run $@ $(QFCC) $(QCFLAGS) $< +double-demote-int-linit.run$(EXEEXT): double-demote-int-linit.r Makefile build-compile-fail-run + $(srcdir)/build-compile-fail-run $@ $(QFCC) $(QCFLAGS) $< + +double-demote-float-linit.run$(EXEEXT): double-demote-float-linit.r Makefile build-compile-fail-run + $(srcdir)/build-compile-fail-run $@ $(QFCC) $(QCFLAGS) $< + double-int-compare.run$(EXEEXT): double-int-compare.r Makefile build-compile-fail-run $(srcdir)/build-compile-fail-run $@ $(QFCC) $(QCFLAGS) $< diff --git a/tools/qfcc/test/double-demote-float-linit.r b/tools/qfcc/test/double-demote-float-linit.r new file mode 100644 index 000000000..796b2040f --- /dev/null +++ b/tools/qfcc/test/double-demote-float-linit.r @@ -0,0 +1,6 @@ +double a; +int main () +{ + float b = a; + return 1; // test fails if compile succeeds +} diff --git a/tools/qfcc/test/double-demote-int-linit.r b/tools/qfcc/test/double-demote-int-linit.r new file mode 100644 index 000000000..3a206f4b5 --- /dev/null +++ b/tools/qfcc/test/double-demote-int-linit.r @@ -0,0 +1,6 @@ +double a; +int main () +{ + int b = a; + return 1; // test fails if compile succeeds +}