From bdd3870d2f86ab280ffa4a49710758f85929d1c1 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Fri, 24 Dec 2021 09:53:58 +0900 Subject: [PATCH] [qfcc] Add failing test for dealloc warning I have gotten tired of chasing memory leaks caused by me forgetting to add [super dealloc] to my dealloc methods, so getting qfcc to chew me out when I do seems to be a good idea (having such a warning would have saved me many hours, just as missing return warnings have). --- tools/qfcc/test/Makemodule.am | 15 +++++++++++++ tools/qfcc/test/dealloc-nowarn.r | 36 ++++++++++++++++++++++++++++++++ tools/qfcc/test/dealloc-warn.r | 35 +++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 tools/qfcc/test/dealloc-nowarn.r create mode 100644 tools/qfcc/test/dealloc-warn.r diff --git a/tools/qfcc/test/Makemodule.am b/tools/qfcc/test/Makemodule.am index af16e954b..e20ed87ea 100644 --- a/tools/qfcc/test/Makemodule.am +++ b/tools/qfcc/test/Makemodule.am @@ -17,6 +17,7 @@ test_progs_dat=\ tools/qfcc/test/comma-expr.dat \ tools/qfcc/test/compound.dat \ tools/qfcc/test/deadbool.dat \ + tools/qfcc/test/dealloc-nowarn.dat \ tools/qfcc/test/double.dat \ tools/qfcc/test/double-alias.dat \ tools/qfcc/test/enum.dat \ @@ -63,6 +64,7 @@ fail_progs_dat= test_build_errors=\ tools/qfcc/test/classarray.r \ + tools/qfcc/test/dealloc-warn.r \ tools/qfcc/test/double-demote-float.r \ tools/qfcc/test/double-demote-float-ainit.r \ tools/qfcc/test/double-demote-float-ginit.r \ @@ -193,6 +195,16 @@ tools/qfcc/test/deadbool.run: $(qfcc_test_run_deps) include $(deadbool_dep) # am--include-marker r_depfiles_remade += $(deadbool_dep) +tools_qfcc_test_dealloc_nowarn_dat_SOURCES=tools/qfcc/test/dealloc-nowarn.r +dealloc_nowarn_obj=$(tools_qfcc_test_dealloc_nowarn_dat_SOURCES:.r=.o) +dealloc_nowarn_dep=$(call qcautodep,$(tools_qfcc_test_dealloc_nowarn_dat_SOURCES)) +tools/qfcc/test/dealloc-nowarn.dat$(EXEEXT): $(dealloc_nowarn_obj) $(QFCC_DEP) + $(V_QFCCLD)$(QLINK) -o $@ $(dealloc_nowarn_obj) +tools/qfcc/test/dealloc-nowarn.run: $(qfcc_test_run_deps) + @$(top_srcdir)/tools/qfcc/test/build-run $@ +include $(dealloc_nowarn_dep) # am--include-marker +r_depfiles_remade += $(dealloc_nowarn_dep) + tools_qfcc_test_double_dat_SOURCES=tools/qfcc/test/double.r double_obj=$(tools_qfcc_test_double_dat_SOURCES:.r=.o) double_dep=$(call qcautodep,$(tools_qfcc_test_double_dat_SOURCES)) @@ -216,6 +228,9 @@ r_depfiles_remade += $(double_alias_dep) tools/qfcc/test/classarray.run$(EXEEXT): tools/qfcc/test/classarray.r $(qfcc_fail_run_deps) @$(top_srcdir)/tools/qfcc/test/build-compile-fail-run $@ $(QFCC) $(QCFLAGS) $< +tools/qfcc/test/dealloc-warn.run$(EXEEXT): tools/qfcc/test/dealloc-warn.r $(qfcc_fail_run_deps) + @$(top_srcdir)/tools/qfcc/test/build-compile-fail-run $@ $(QFCC) $(QCFLAGS) $< + tools/qfcc/test/double-demote-int.run$(EXEEXT): tools/qfcc/test/double-demote-int.r $(qfcc_fail_run_deps) @$(top_srcdir)/tools/qfcc/test/build-compile-fail-run $@ $(QFCC) $(QCFLAGS) $< diff --git a/tools/qfcc/test/dealloc-nowarn.r b/tools/qfcc/test/dealloc-nowarn.r new file mode 100644 index 000000000..f4bc3bcc4 --- /dev/null +++ b/tools/qfcc/test/dealloc-nowarn.r @@ -0,0 +1,36 @@ +@interface Object +{ + Class isa; +} +-(void)dealloc; +@end + +@interface derived : Object +@end + +@implementation Object +-(void) dealloc +{ + // this is the root of the hierarchy, so no super to call, thus + // must not check for [super dealloc] +} +@end + +void __obj_exec_class (struct obj_module *msg) = #0; +id obj_msgSend_super (Super *class, SEL op, ...) = #0; + +@implementation derived +-(void) dealloc +{ + // as this is a derived class, failure to call [super dealloc] will + // result in a memory leak (yes, there could be special allocators + // involved, in which case something will be needed to inform the + // compiler) + [super dealloc]; +} +@end + +int main () +{ + return 0; // test passes if compile succeeds (with -Werror) +} diff --git a/tools/qfcc/test/dealloc-warn.r b/tools/qfcc/test/dealloc-warn.r new file mode 100644 index 000000000..bf218ca9c --- /dev/null +++ b/tools/qfcc/test/dealloc-warn.r @@ -0,0 +1,35 @@ +@interface Object +{ + Class isa; +} +-(void)dealloc; +@end + +@interface derived : Object +@end + +@implementation Object +-(void) dealloc +{ + // this is the root of the hierarchy, so no super to call, thus + // must not check for [super dealloc] +} +@end + +@implementation derived +-(void) dealloc +{ + // as this is a derived class, failure to call [super dealloc] will + // result in a memory leak (yes, there could be special allocators + // involved, in which case something will be needed to inform the + // compiler) +} +@end + +void __obj_exec_class (struct obj_module *msg) = #0; +id obj_msgSend_super (Super *class, SEL op, ...) = #0; + +int main () +{ + return 1; // test fails if compile succeeds (with -Werror) +}