From b6093e07282c4ac8cc734301f279377eebb05954 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Sun, 23 Jan 2022 01:31:50 +0900 Subject: [PATCH] [qfcc] Add failing test for lost var use Storing a variable into a dereference pointer (*p = x) is not marking the variable as used (due to a mistake while converting to Ruamoko statement format) resulting in assignments to that variable being dropped due to it being a dead assignment as the assignment to the variable and the storing need to be in separate basic blocks (thus the call in the test, though an if would have worked, I think) for the bug to trigger. --- tools/qfcc/test/Makemodule.am | 11 +++++++++++ tools/qfcc/test/lost-use.r | 29 +++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 tools/qfcc/test/lost-use.r diff --git a/tools/qfcc/test/Makemodule.am b/tools/qfcc/test/Makemodule.am index 1f310048a..88b9e8480 100644 --- a/tools/qfcc/test/Makemodule.am +++ b/tools/qfcc/test/Makemodule.am @@ -31,6 +31,7 @@ test_progs_dat=\ tools/qfcc/test/infloop.dat \ tools/qfcc/test/ivar-struct-return.dat \ tools/qfcc/test/link_order.dat \ + tools/qfcc/test/lost-use.dat \ tools/qfcc/test/methodparams.dat \ tools/qfcc/test/modulo.dat \ tools/qfcc/test/nilparamret.dat \ @@ -386,6 +387,16 @@ tools/qfcc/test/link_order.run: $(qfcc_test_run_deps) include $(link_order_dep) # am--include-marker r_depfiles_remade += $(link_order_dep) +tools_qfcc_test_lost_use_dat_SOURCES=tools/qfcc/test/lost-use.r +lost_use_obj=$(tools_qfcc_test_lost_use_dat_SOURCES:.r=.o) +lost_use_dep=$(call qcautodep,$(tools_qfcc_test_lost_use_dat_SOURCES)) +tools/qfcc/test/lost-use.dat$(EXEEXT): $(lost_use_obj) $(QFCC_DEP) + $(V_QFCCLD)$(QLINK) -o $@ $(lost_use_obj) +tools/qfcc/test/lost-use.run: $(qfcc_test_run_deps) + @$(top_srcdir)/tools/qfcc/test/build-run $@ +include $(lost_use_dep) # am--include-marker +r_depfiles_remade += $(lost_use_dep) + tools_qfcc_test_methodparams_dat_SOURCES=tools/qfcc/test/methodparams.r methodparams_obj=$(tools_qfcc_test_methodparams_dat_SOURCES:.r=.o) methodparams_dep=$(call qcautodep,$(tools_qfcc_test_methodparams_dat_SOURCES)) diff --git a/tools/qfcc/test/lost-use.r b/tools/qfcc/test/lost-use.r new file mode 100644 index 000000000..dc9d79142 --- /dev/null +++ b/tools/qfcc/test/lost-use.r @@ -0,0 +1,29 @@ +void printf (string fmt, ...) = #0; +int getval(void) +{ + return 42; +} + +void magic (void) +{ +} + +void storeval (int *p) +{ + int x = getval (); + magic (); + *p = x; +} + +int val; + +int +main(void) +{ + storeval (&val); + if (val != 42) { + printf ("val is dead: %d\n", val); + return 1; + } + return 0; +}