From 47dd459ecf952873e64cf8c91dd9e73360085777 Mon Sep 17 00:00:00 2001 From: Alexander Herz Date: Wed, 18 Nov 2026 18:08:58 +0000 Subject: [PATCH 1/2] Give objects of empty classes storage in the generated C [GH #560] On targets where the C-generating back end uses an empty struct for an empty class (USE_EMPTY_STRUCT_IN_GENERATED_C, for g++ ABI compatibility), complete objects of the class had size zero in the generated C, although the class has size one in C++. Distinct objects could have the same address, and writing sizeof bytes to an object (e.g., with memset or memcpy) overwrote neighbouring objects. Declare variables of such a class, and arrays of it, as arrays of char of the C++ size, and refer to them through a cast of that storage. By-value parameters of such a class are still passed with size zero (as by g++); references to them use a local array of char declared at the start of the function body. The value need not be copied, as the class has no state. Address constants with an offset into an array of such a class (e.g., "&a[3]") were put out as subscripts or pointer arithmetic on the zero-size type, which yields the address of the array itself. Add the offset to a char pointer instead. The generated C for co_await is not supported, and the expected output of some coroutine tests (cmeerw/gnats-26621-run, ccs/coroutines/ gnats20948-paramcopy, and imported gnu coroutines/pr106188 and coroutines/torture/pr115905) records the errors from compiling it. Those errors change; update the recordings. The 32-bit configurations don't use empty structs, so their output is unchanged and gets recordings of its own. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/c_gen_be.c | 132 ++++++++++++-- src/il_to_str.c | 40 ++++- .../gh-560-empty-class-object-storage.txt | 9 + tests/expectations/edg_x86_32/edg.log | 1 + tests/expectations/edg_x86_32_cp/edg.log | 1 + tests/expectations/edg_x86_64/edg.log | 2 +- tests/expectations/edg_x86_64/imported.log | 1 - .../edg_x86_64_cp.1.1.txt | 5 +- .../.gnats-26621-run.rto/default.1.1.txt | 11 +- .../.gnats-26621-run.rto/edg_x86_32.1.1.txt | 8 + .../edg_x86_32_cp.1.1.txt | 8 + .../coroutines/.pr106188.rto/default.1.1.txt | 10 +- .../.pr106188.rto/edg_x86_32.1.1.txt | 11 ++ .../torture/.pr115905.rto/default.1.1.txt | 4 +- .../torture/.pr115905.rto/edg_x86_32.1.1.txt | 9 + .../default.1.1.txt | 1 + .../default.1.2.txt | 1 + .../empty-class-object-storage.sft.cpp | 167 ++++++++++++++++++ 18 files changed, 389 insertions(+), 32 deletions(-) create mode 100644 src/pending_changes/gh-560-empty-class-object-storage.txt create mode 100644 tests/tests/edg/cmeerw/.gnats-26621-run.rto/edg_x86_32.1.1.txt create mode 100644 tests/tests/edg/cmeerw/.gnats-26621-run.rto/edg_x86_32_cp.1.1.txt create mode 100644 tests/tests/imported/gnu/cpp/coroutines/.pr106188.rto/edg_x86_32.1.1.txt create mode 100644 tests/tests/imported/gnu/cpp/coroutines/torture/.pr115905.rto/edg_x86_32.1.1.txt create mode 100644 tests/tests/regressions/.empty-class-object-storage.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.empty-class-object-storage.rto/default.1.2.txt create mode 100644 tests/tests/regressions/empty-class-object-storage.sft.cpp diff --git a/src/c_gen_be.c b/src/c_gen_be.c index 849110531a..16926f070b 100644 --- a/src/c_gen_be.c +++ b/src/c_gen_be.c @@ -1552,9 +1552,84 @@ if unreferenced. strcmp((name), "__builtin_va_alist") == 0))) +static a_boolean has_char_storage(a_variable_ptr variable) +/* +Return TRUE if the given variable is an object of an empty class, or an array +of such objects, whose class is generated as an empty struct (which has size +zero in GNU C). Such a variable is given storage of the C++ size of its type +as an array of char, and references to it convert the address of that storage +to a pointer to the type of the variable. Otherwise, writing the bytes of +the object (e.g., with memcpy) would overwrite other objects. Parameters, +whose type determines how they are passed, are handled separately (see +param_has_char_storage). +*/ +{ + a_boolean result = FALSE; + a_type_ptr type = skip_typerefs(variable->type); + + if (!variable->is_parameter && type->size != 0) { + if (is_array_type(type) && !is_vla_type(type)) { + type = f_skip_typerefs(underlying_array_element_type(type)); + } /* if */ + result = is_class_struct_union_type(type) && + type->generated_as_empty_struct; + } /* if */ + return result; +} /* has_char_storage */ + + +static a_boolean param_has_char_storage(a_variable_ptr variable) +/* +Return TRUE if the given variable is a parameter passed by value whose type +is an empty class generated as an empty struct (see has_char_storage). The +parameter itself has size zero in GNU C, so the function body declares a +local array of char of the C++ size of the type (named as the parameter +with a "__s" suffix; see dump_param_char_storage) and references to the +parameter use that storage instead. The value of the parameter need not be +copied, as an empty class has no state. +*/ +{ + a_boolean result = FALSE; + a_type_ptr type = skip_typerefs(variable->type); + + if (variable->is_parameter && !variable->is_this_parameter && + type->size != 0) { + result = is_class_struct_union_type(type) && + type->generated_as_empty_struct; + } /* if */ + return result; +} /* param_has_char_storage */ + + +static void dump_declared_variable_name(a_variable_ptr variable); +static void dump_cast_to_pointer_to(a_type_ptr type); + static void dump_variable_name(a_variable_ptr variable) /* -Print the name of the indicated variable. +Print a reference to the indicated variable. This is usually just its name; +see has_char_storage and param_has_char_storage for exceptions. +*/ +{ + if (has_char_storage(variable)) { + write_tok_str("(*"); + dump_cast_to_pointer_to(variable->type); + dump_declared_variable_name(variable); + write_tok_ch(')'); + } else if (param_has_char_storage(variable)) { + write_tok_str("(*"); + dump_cast_to_pointer_to(variable->type); + dump_declared_variable_name(variable); + write_str("__s"); + write_tok_ch(')'); + } else { + dump_declared_variable_name(variable); + } /* if */ +} /* dump_variable_name */ + + +static void dump_declared_variable_name(a_variable_ptr variable) +/* +Print the name of the indicated variable, as used in its declaration. */ { if (entry_routine_scope != NULL && @@ -1651,7 +1726,7 @@ Print the name of the indicated variable. /* Nothing special about this case. */ dump_name(&variable->source_corresp); } /* if */ -} /* dump_variable_name */ +} /* dump_declared_variable_name */ static void dump_type_name(a_type_ptr type) @@ -2424,7 +2499,7 @@ name is non-NULL. } else if (scp != NULL) { /* Write the name. */ if (var != NULL) { - dump_variable_name(var); + dump_declared_variable_name(var); } else if (field != NULL) { dump_field_name(field); } else if (rout != NULL && rout->superseded_external) { @@ -8886,12 +8961,21 @@ parameters. /*need_leading_space=*/TRUE, &octl)) { write_space(); } /* if */ - dump_general_declaration_using_type(var_type, - &variable->source_corresp, - variable, NO_ROUTINE, NO_FIELD, - NO_TEMP, NO_NAME, TQ_NONE, - suppress_const, - NO_COUNTER); + if (has_char_storage(variable)) { + write_tok_str("char "); + dump_declared_variable_name(variable); + write_tok_ch('['); + write_unsigned_num( + (a_host_large_unsigned)skip_typerefs(var_type)->size); + write_tok_ch(']'); + } else { + dump_general_declaration_using_type(var_type, + &variable->source_corresp, + variable, NO_ROUTINE, NO_FIELD, + NO_TEMP, NO_NAME, TQ_NONE, + suppress_const, + NO_COUNTER); + } /* if */ #if !C_GEN_BE_GENERATES_ANSI_C } /* if */ #endif /* !C_GEN_BE_GENERATES_ANSI_C */ @@ -8917,7 +9001,11 @@ parameters. the size of the executable. However, do put out definitions for template static data members that are arrays, or otherwise the template prelinker could loop. */ - if ((dump_initializers && init_con != NULL) || + if (has_char_storage(variable)) { + /* An empty class has no value, so there is nothing to initialize + (but a weak variable needs an initializer). */ + if (force_zeroing_of_comdat_variable) write_tok_str(" = {0}"); + } else if ((dump_initializers && init_con != NULL) || force_zeroing_of_comdat_variable || (init_kind == (an_init_kind)initk_zero && !variable->suppress_inline_definition && @@ -9364,6 +9452,29 @@ issue diagnostics for them (warnings or errors depending on the version). #endif /* GNU_EXTENSIONS_ALLOWED */ +static void dump_param_char_storage(a_scope_ptr scope) +/* +Declare the local storage for the parameters of the function whose scope is +given that are referenced via such storage (see param_has_char_storage). +*/ +{ + a_variable_ptr param; + + for (param = scope->variant.routine.parameters; param != NULL; + param = param->next) { + if (param_has_char_storage(param)) { + write_tok_str("auto char "); + dump_declared_variable_name(param); + write_str("__s"); + write_tok_ch('['); + write_unsigned_num( + (a_host_large_unsigned)skip_typerefs(param->type)->size); + write_tok_str("];"); + } /* if */ + } /* for */ +} /* dump_param_char_storage */ + + static void dump_block_declarations(a_statement_ptr statement) /* Dump out the declarations (if any) for a block. @@ -9393,6 +9504,7 @@ Dump out the declarations (if any) for a block. dump_local_label_declarations(scope); #endif /* GNU_EXTENSIONS_ALLOWED */ dump_scope_pragmas(scope); + if (rout != NULL) dump_param_char_storage(scope); dump_scope_variables(scope, /*interleave_asm_decls=*/FALSE, /*dump_vars_without_initializers=*/TRUE, diff --git a/src/il_to_str.c b/src/il_to_str.c index 1abaece6e5..c946872cd8 100644 --- a/src/il_to_str.c +++ b/src/il_to_str.c @@ -4943,6 +4943,29 @@ ck_address constant). Do the output in the way described by octl. } /* form_typeid_reference */ +static a_boolean is_generated_empty_struct_object_type(a_type_ptr type) +/* +Return TRUE if type is a struct generated by the C-generating back end as +an empty struct, or an array of such structs. Such a type has size zero in +the generated code, so address offsets into it cannot be expressed by +subscripting or by pointer arithmetic on the type; they must be added as +char offsets. +*/ +{ + a_boolean result = FALSE; +#if BACK_END_IS_C_GEN_BE + type = f_skip_typerefs(type); + if (is_array_type(type)) { + type = f_skip_typerefs(underlying_array_element_type(type)); + } /* if */ + result = type->generated_as_empty_struct; +#else /* !BACK_END_IS_C_GEN_BE */ + (void)type; +#endif /* BACK_END_IS_C_GEN_BE */ + return result; +} /* is_generated_empty_struct_object_type */ + + static a_type_ptr static_unknown_type(void) /* Return a pointer to an unknown type. Don't do allocation, because that is not @@ -5184,7 +5207,9 @@ parentheses are not needed. } /* while */ } /* if */ if (path->is_offset) { - if (is_array_type(type)) { + if (is_array_type(type) && + (path->variant.ptr_offset == 0 || + !is_generated_empty_struct_object_type(type))) { if (gen_output) { octl->output_str("[", octl); form_num(path->variant.ptr_offset, octl); @@ -5194,7 +5219,9 @@ parentheses are not needed. *offset -= (path->variant.ptr_offset * (a_targ_ptrdiff_t)size_of_type(type)); } else { - /* Can only handle array subscripting operations. */ + /* Can only handle array subscripting operations, and not on + elements generated as empty structs (which have size zero in + the generated code). */ break; } /* if */ } else if (path->is_base_class) { @@ -5233,6 +5260,12 @@ parentheses are not needed. *formed_useful_lvalue = TRUE; *type_decay_used = TRUE; break; + } else if (*offset != 0 && + is_generated_empty_struct_object_type(unqual_type)) { + /* The elements have size zero in the generated code, so the + offset cannot be reached by subscripting. Give up and let the + caller add the offset as a char offset. */ + break; } else { /* Add a subscripting operation. */ a_type_ptr element_type = array_element_type(unqual_type); @@ -5600,7 +5633,8 @@ precedence confusion. Do the output in the way described by octl. if (!form_lvalue) { a_targ_ptrdiff_t size = (a_targ_ptrdiff_t)f_skip_typerefs(achieved_type)->size; - if (size != 0 && (offset % size) == 0) { + if (size != 0 && (offset % size) == 0 && + !is_generated_empty_struct_object_type(achieved_type)) { need_char_star_cast = FALSE; offset /= size; } /* if */ diff --git a/src/pending_changes/gh-560-empty-class-object-storage.txt b/src/pending_changes/gh-560-empty-class-object-storage.txt new file mode 100644 index 0000000000..12b046597d --- /dev/null +++ b/src/pending_changes/gh-560-empty-class-object-storage.txt @@ -0,0 +1,9 @@ +[GH #560] +Storage of objects of an empty class in generated C + +When the C-generating back end used an empty struct for an empty class (for +g++ ABI compatibility), objects of that class had size zero in the generated +C although they have size one in C++: writing sizeof bytes to such an object +(e.g., with memset) overwrote neighboring objects, and an address constant +such as "&a[3]" evaluated to the address of the array. Such variables are now +declared as arrays of char of the C++ size. diff --git a/tests/expectations/edg_x86_32/edg.log b/tests/expectations/edg_x86_32/edg.log index edf288421c..5f77e9cf0b 100644 --- a/tests/expectations/edg_x86_32/edg.log +++ b/tests/expectations/edg_x86_32/edg.log @@ -23,6 +23,7 @@ cmeerw/gnats-26329-dump.sft.cpp:fp:-DTEST_NUMBER=8 --c++ --gn 40900 --target lin cmeerw/gnats-26329-dump.sft.cpp:fp:-DTEST_NUMBER=9 --c++ --gn 40900 --target linux_armv7:REQUIREMENTS NOT MET: cmeerw/gnats-26329-target.sft.cpp:fp:-DTEST_NUMBER=1 --target linux_aarch64:REQUIREMENTS NOT MET: cmeerw/gnats-26329-target.sft.cpp:fp:-DTEST_NUMBER=2 --target linux_armv7:REQUIREMENTS NOT MET: +cmeerw/gnats-26621-run.sft.cpp:rp:-DTEST_NUMBER=1 --c++20 --no_exceptions --set_flag=no_checking_pragmas:BADC(COMPILE): cmeerw/gnats-27093-constexpr.sft.cpp:fp:-DTEST_NUMBER=1 --c++20 --clang_version 180100 --target linux_aarch64:REQUIREMENTS NOT MET: cmeerw/gnats-27093-errors.sft.cpp:fn:-DTEST_NUMBER=1 --gn 130200:REQUIREMENTS NOT MET: cmeerw/gnats-27093-errors.sft.cpp:fn:-DTEST_NUMBER=2 --clang_version 180100:REQUIREMENTS NOT MET: diff --git a/tests/expectations/edg_x86_32_cp/edg.log b/tests/expectations/edg_x86_32_cp/edg.log index 547a50077f..3a1280183a 100644 --- a/tests/expectations/edg_x86_32_cp/edg.log +++ b/tests/expectations/edg_x86_32_cp/edg.log @@ -132,6 +132,7 @@ cmeerw/gnats-26329-dump.sft.cpp:fp:-DTEST_NUMBER=8 --c++ --gn 40900 --target lin cmeerw/gnats-26329-dump.sft.cpp:fp:-DTEST_NUMBER=9 --c++ --gn 40900 --target linux_armv7:REQUIREMENTS NOT MET: cmeerw/gnats-26329-target.sft.cpp:fp:-DTEST_NUMBER=1 --target linux_aarch64:REQUIREMENTS NOT MET: cmeerw/gnats-26329-target.sft.cpp:fp:-DTEST_NUMBER=2 --target linux_armv7:REQUIREMENTS NOT MET: +cmeerw/gnats-26621-run.sft.cpp:rp:-DTEST_NUMBER=1 --c++20 --no_exceptions --set_flag=no_checking_pragmas:BADC(COMPILE): cmeerw/gnats-26709.sft.cpp:fn:-DTEST_NUMBER=1 --c++17 --defer_parse_function_templates -tused:CATASTROPHE: cmeerw/gnats-26709.sft.cpp:fn:-DTEST_NUMBER=2 --c++20 --defer_parse_function_templates -tused:CATASTROPHE: cmeerw/gnats-26709.sft.cpp:fn:-DTEST_NUMBER=3 --ms_c++17 --microsoft_version 1942 --no_ms_permissive --defer_parse_function_templates -tused:CATASTROPHE: diff --git a/tests/expectations/edg_x86_64/edg.log b/tests/expectations/edg_x86_64/edg.log index 7cf72b28e0..7afaae7a44 100644 --- a/tests/expectations/edg_x86_64/edg.log +++ b/tests/expectations/edg_x86_64/edg.log @@ -16,7 +16,7 @@ ccs/gnats20969f.sft.cpp:fp:-DTEST_NUMBER=5 --microsoft_version 1920:REQUIREMENTS ccs/gnats21047.sft.cpp:cp:-DTEST_NUMBER=1:REQUIREMENTS NOT MET: ccs/gnats21635.sft.cpp:cp:-DTEST_NUMBER=1:FAIL: ccs/gnats23024.sft.cpp:rp:-DTEST_NUMBER=1:REQUIREMENTS NOT MET: -cmeerw/gnats-26621-run.sft.cpp:rp:-DTEST_NUMBER=1 --c++20 --no_exceptions --set_flag=no_checking_pragmas:BADC(COMPILE): +cmeerw/gnats-26621-run.sft.cpp:rp:-DTEST_NUMBER=1 --c++20 --no_exceptions --set_flag=no_checking_pragmas:FAIL(COMPILE): cmeerw/pragma-clang-whitespace-cppbe.sft.cpp:cppbe:-DTEST_NUMBER=1 --c++20:REQUIREMENTS NOT MET: cmeerw/pragma-clang-whitespace-cppbe.sft.cpp:cppbe:-DTEST_NUMBER=2 --gn 160200:REQUIREMENTS NOT MET: cmeerw/pragma-clang-whitespace-cppbe.sft.cpp:cppbe:-DTEST_NUMBER=3 --clang_version 230100:REQUIREMENTS NOT MET: diff --git a/tests/expectations/edg_x86_64/imported.log b/tests/expectations/edg_x86_64/imported.log index 12ba47df3f..9687dfc670 100644 --- a/tests/expectations/edg_x86_64/imported.log +++ b/tests/expectations/edg_x86_64/imported.log @@ -8979,7 +8979,6 @@ gnu/cpp/torture/pr85013.sft.cpp:s:-DTEST_NUMBER=1 --c++14:SKIPPED: gnu/cpp/torture/pr85627.sft.cpp:fp:-DTEST_NUMBER=1:FAIL: gnu/cpp/torture/pr87997.sft.cpp:fp:-DTEST_NUMBER=1:FAIL: gnu/cpp/torture/pr88149.sft.cpp:fp:-DTEST_NUMBER=1:FAIL: -gnu/cpp/torture/pr92384.sft.cpp:rp:-DTEST_NUMBER=1:FAIL: gnu/cpp/torture/pr93347.sft.cpp:s:-DTEST_NUMBER=1:SKIPPED: gnu/cpp/torture/pr94582.sft.cpp:s:-DTEST_NUMBER=1:SKIPPED: gnu/cpp/torture/stackalign/unwind-0.sft.cpp:rp:-DTEST_NUMBER=1:FAIL: diff --git a/tests/tests/edg/ccs/coroutines/.gnats20948-paramcopy.rto/edg_x86_64_cp.1.1.txt b/tests/tests/edg/ccs/coroutines/.gnats20948-paramcopy.rto/edg_x86_64_cp.1.1.txt index 8864e9f925..e1a627c4ea 100644 --- a/tests/tests/edg/ccs/coroutines/.gnats20948-paramcopy.rto/edg_x86_64_cp.1.1.txt +++ b/tests/tests/edg/ccs/coroutines/.gnats20948-paramcopy.rto/edg_x86_64_cp.1.1.txt @@ -1,8 +1,7 @@ cpfe -DTEST_NUMBER=1 --c++20 --set_flag=coroutines --set_flag=no_checking_pragmas -tused Test_name.c eccp: diagnostics generated from compilation of Test_name.int.c: Test_name.c: In function '_Z1fiRf1BPS0_RS0_OS0_': -Test_name.c:33:266: error: 'co_await__T6' undeclared (first use in this function) -Test_name.c:33:266: note: each undeclared identifier is reported only once for each function it appears in -Test_name.c:38:209: error: '__T17' undeclared (first use in this function); did you mean '__T16'? +Test_name.c:38:292: error: '__T17' undeclared (first use in this function); did you mean '__T16'? +Test_name.c:38:292: note: each undeclared identifier is reported only once for each function it appears in eccp: end of diagnostics from compilation of Test_name.int.c eccp: gcc compilation of Test_name.int.c returned an exit status of 1 diff --git a/tests/tests/edg/cmeerw/.gnats-26621-run.rto/default.1.1.txt b/tests/tests/edg/cmeerw/.gnats-26621-run.rto/default.1.1.txt index 8f7a4aae1f..061687ce91 100644 --- a/tests/tests/edg/cmeerw/.gnats-26621-run.rto/default.1.1.txt +++ b/tests/tests/edg/cmeerw/.gnats-26621-run.rto/default.1.1.txt @@ -1,8 +1,5 @@ eccp -DTEST_NUMBER=1 --c++20 --no_exceptions --set_flag=no_checking_pragmas Test_name.c -eccp: diagnostics generated from compilation of Test_name.int.c: -Test_name.c: In function '_Z4coroi': -Test_name.c:45:29: error: 'co_await__T4' undeclared (first use in this function) -Test_name.c:45:29: note: each undeclared identifier is reported only once for each function it appears in -Test_name.c:61:266: error: 'co_await__T7' undeclared (first use in this function) -eccp: end of diagnostics from compilation of Test_name.int.c -eccp: gcc compilation of Test_name.int.c returned an exit status of 1 +/usr/sbin/ld: Test_name.o: in function `_Z4coroi': +Test_name.int.c:(.text+0xXX): undefined reference to `co_await' +/usr/sbin/ld: Test_name.int.c:(.text+0xXX): undefined reference to `co_await' +collect2: error: ld returned 1 exit status diff --git a/tests/tests/edg/cmeerw/.gnats-26621-run.rto/edg_x86_32.1.1.txt b/tests/tests/edg/cmeerw/.gnats-26621-run.rto/edg_x86_32.1.1.txt new file mode 100644 index 0000000000..8f7a4aae1f --- /dev/null +++ b/tests/tests/edg/cmeerw/.gnats-26621-run.rto/edg_x86_32.1.1.txt @@ -0,0 +1,8 @@ +eccp -DTEST_NUMBER=1 --c++20 --no_exceptions --set_flag=no_checking_pragmas Test_name.c +eccp: diagnostics generated from compilation of Test_name.int.c: +Test_name.c: In function '_Z4coroi': +Test_name.c:45:29: error: 'co_await__T4' undeclared (first use in this function) +Test_name.c:45:29: note: each undeclared identifier is reported only once for each function it appears in +Test_name.c:61:266: error: 'co_await__T7' undeclared (first use in this function) +eccp: end of diagnostics from compilation of Test_name.int.c +eccp: gcc compilation of Test_name.int.c returned an exit status of 1 diff --git a/tests/tests/edg/cmeerw/.gnats-26621-run.rto/edg_x86_32_cp.1.1.txt b/tests/tests/edg/cmeerw/.gnats-26621-run.rto/edg_x86_32_cp.1.1.txt new file mode 100644 index 0000000000..8f7a4aae1f --- /dev/null +++ b/tests/tests/edg/cmeerw/.gnats-26621-run.rto/edg_x86_32_cp.1.1.txt @@ -0,0 +1,8 @@ +eccp -DTEST_NUMBER=1 --c++20 --no_exceptions --set_flag=no_checking_pragmas Test_name.c +eccp: diagnostics generated from compilation of Test_name.int.c: +Test_name.c: In function '_Z4coroi': +Test_name.c:45:29: error: 'co_await__T4' undeclared (first use in this function) +Test_name.c:45:29: note: each undeclared identifier is reported only once for each function it appears in +Test_name.c:61:266: error: 'co_await__T7' undeclared (first use in this function) +eccp: end of diagnostics from compilation of Test_name.int.c +eccp: gcc compilation of Test_name.int.c returned an exit status of 1 diff --git a/tests/tests/imported/gnu/cpp/coroutines/.pr106188.rto/default.1.1.txt b/tests/tests/imported/gnu/cpp/coroutines/.pr106188.rto/default.1.1.txt index bf86288297..c6ef198263 100644 --- a/tests/tests/imported/gnu/cpp/coroutines/.pr106188.rto/default.1.1.txt +++ b/tests/tests/imported/gnu/cpp/coroutines/.pr106188.rto/default.1.1.txt @@ -1,11 +1,13 @@ eccp -DTEST_NUMBER=1 --gnu_version=999999 -tused -e 200 --no_wrap --no_wrap_diagnostics --c++20 Test_name.c eccp: diagnostics generated from compilation of Test_name.int.c: ./coroutines/pr106188.C: In function '_Z1fv': -./coroutines/pr106188.C:22:5: error: 'co_await__T7' undeclared (first use in this function) -./coroutines/pr106188.C:22:5: note: each undeclared identifier is reported only once for each function it appears in -./coroutines/pr106188.C:22:182: error: '__T14' undeclared (first use in this function); did you mean '__T13'? +./coroutines/pr106188.C:21:280: error: unknown type name 'co_await' +./coroutines/pr106188.C:21:43: error: expected declaration specifiers or '...' before '(' token +./coroutines/pr106188.C:22:214: error: '__T14' undeclared (first use in this function); did you mean '__T13'? +./coroutines/pr106188.C:22:214: note: each undeclared identifier is reported only once for each function it appears in ./coroutines/pr106188.C: In function '_Z1gv': -./coroutines/pr106188.C:26:9: error: 'co_await__T21' undeclared (first use in this function) +./coroutines/pr106188.C:25:288: error: unknown type name 'co_await' +./coroutines/pr106188.C:25:43: error: expected declaration specifiers or '...' before '(' token ./coroutines/pr106188.C:28:156: error: '__T29' undeclared (first use in this function); did you mean '__T28'? eccp: end of diagnostics from compilation of Test_name.int.c eccp: gcc compilation of Test_name.int.c returned an exit status of 1 diff --git a/tests/tests/imported/gnu/cpp/coroutines/.pr106188.rto/edg_x86_32.1.1.txt b/tests/tests/imported/gnu/cpp/coroutines/.pr106188.rto/edg_x86_32.1.1.txt new file mode 100644 index 0000000000..bf86288297 --- /dev/null +++ b/tests/tests/imported/gnu/cpp/coroutines/.pr106188.rto/edg_x86_32.1.1.txt @@ -0,0 +1,11 @@ +eccp -DTEST_NUMBER=1 --gnu_version=999999 -tused -e 200 --no_wrap --no_wrap_diagnostics --c++20 Test_name.c +eccp: diagnostics generated from compilation of Test_name.int.c: +./coroutines/pr106188.C: In function '_Z1fv': +./coroutines/pr106188.C:22:5: error: 'co_await__T7' undeclared (first use in this function) +./coroutines/pr106188.C:22:5: note: each undeclared identifier is reported only once for each function it appears in +./coroutines/pr106188.C:22:182: error: '__T14' undeclared (first use in this function); did you mean '__T13'? +./coroutines/pr106188.C: In function '_Z1gv': +./coroutines/pr106188.C:26:9: error: 'co_await__T21' undeclared (first use in this function) +./coroutines/pr106188.C:28:156: error: '__T29' undeclared (first use in this function); did you mean '__T28'? +eccp: end of diagnostics from compilation of Test_name.int.c +eccp: gcc compilation of Test_name.int.c returned an exit status of 1 diff --git a/tests/tests/imported/gnu/cpp/coroutines/torture/.pr115905.rto/default.1.1.txt b/tests/tests/imported/gnu/cpp/coroutines/torture/.pr115905.rto/default.1.1.txt index 6cefbbd94e..765c7308b3 100644 --- a/tests/tests/imported/gnu/cpp/coroutines/torture/.pr115905.rto/default.1.1.txt +++ b/tests/tests/imported/gnu/cpp/coroutines/torture/.pr115905.rto/default.1.1.txt @@ -1,9 +1,7 @@ eccp -DTEST_NUMBER=1 --gnu_version=999999 -tused -e 200 --no_wrap --no_wrap_diagnostics --c++20 Test_name.c eccp: diagnostics generated from compilation of Test_name.int.c: ./coroutines/torture/pr115905.C: In function '_Z4Corov': -./coroutines/torture/pr115905.C:45:1: error: 'co_await__T7' undeclared (first use in this function) -./coroutines/torture/pr115905.C:45:1: note: each undeclared identifier is reported only once for each function it appears in -./coroutines/torture/pr115905.C:50:1: error: 'co_await__T10' undeclared (first use in this function) ./coroutines/torture/pr115905.C:55:29: error: '__T18' undeclared (first use in this function); did you mean '__T17'? +./coroutines/torture/pr115905.C:55:29: note: each undeclared identifier is reported only once for each function it appears in eccp: end of diagnostics from compilation of Test_name.int.c eccp: gcc compilation of Test_name.int.c returned an exit status of 1 diff --git a/tests/tests/imported/gnu/cpp/coroutines/torture/.pr115905.rto/edg_x86_32.1.1.txt b/tests/tests/imported/gnu/cpp/coroutines/torture/.pr115905.rto/edg_x86_32.1.1.txt new file mode 100644 index 0000000000..6cefbbd94e --- /dev/null +++ b/tests/tests/imported/gnu/cpp/coroutines/torture/.pr115905.rto/edg_x86_32.1.1.txt @@ -0,0 +1,9 @@ +eccp -DTEST_NUMBER=1 --gnu_version=999999 -tused -e 200 --no_wrap --no_wrap_diagnostics --c++20 Test_name.c +eccp: diagnostics generated from compilation of Test_name.int.c: +./coroutines/torture/pr115905.C: In function '_Z4Corov': +./coroutines/torture/pr115905.C:45:1: error: 'co_await__T7' undeclared (first use in this function) +./coroutines/torture/pr115905.C:45:1: note: each undeclared identifier is reported only once for each function it appears in +./coroutines/torture/pr115905.C:50:1: error: 'co_await__T10' undeclared (first use in this function) +./coroutines/torture/pr115905.C:55:29: error: '__T18' undeclared (first use in this function); did you mean '__T17'? +eccp: end of diagnostics from compilation of Test_name.int.c +eccp: gcc compilation of Test_name.int.c returned an exit status of 1 diff --git a/tests/tests/regressions/.empty-class-object-storage.rto/default.1.1.txt b/tests/tests/regressions/.empty-class-object-storage.rto/default.1.1.txt new file mode 100644 index 0000000000..cba3281a9a --- /dev/null +++ b/tests/tests/regressions/.empty-class-object-storage.rto/default.1.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=1 --c++17 --g++ --exceptions Test_name.c diff --git a/tests/tests/regressions/.empty-class-object-storage.rto/default.1.2.txt b/tests/tests/regressions/.empty-class-object-storage.rto/default.1.2.txt new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/regressions/.empty-class-object-storage.rto/default.1.2.txt @@ -0,0 +1 @@ +./a.out diff --git a/tests/tests/regressions/empty-class-object-storage.sft.cpp b/tests/tests/regressions/empty-class-object-storage.sft.cpp new file mode 100644 index 0000000000..a2b30b26f0 --- /dev/null +++ b/tests/tests/regressions/empty-class-object-storage.sft.cpp @@ -0,0 +1,167 @@ +//type:rp +//options_all:--c++17 --g++ --exceptions + +// Objects of an empty class. When the generated C uses an empty struct for +// the class (size zero in GNU C), complete objects of the class must still +// get storage of the C++ size of the class (one byte), as code may write +// that many bytes (e.g., with memset or memcpy). Before the fix, such +// objects had size zero in the generated C: distinct objects could have +// the same address, and writing to one could overwrite another object. +// This applies to variables of all storage durations, arrays of the class, +// by-value parameters (passed with size zero, as by g++), and address +// constants with an offset into an array of the class (e.g., "&ga[3]", +// which pointed to "ga"). + +typedef decltype(sizeof 0) size_t; +extern "C" void *memset(void *, int, size_t); +extern "C" void *memcpy(void *, const void *, size_t); +extern "C" int strcmp(const char *, const char *); + +int errors; + +#define CHECK(cond) do { if (!(cond)) ++errors; } while (0) + +struct Z {}; +struct Inc; /* Incomplete at its first use. */ +extern Inc inc_early; +struct Inc {}; +Inc inc_early; +Inc inc_late; + +/* A nontrivial empty class. */ +struct T { + static int live; + static const void *addrs[8]; + T() { addrs[live++ & 7] = this; } + T(const T &) { addrs[live++ & 7] = this; } + ~T() { live--; } +}; +int T::live; +const void *T::addrs[8]; + +template struct Holder { static Z z; static Z za[3]; }; +template Z Holder::z; +template Z Holder::za[3]; +thread_local Z tl1, tl2; +const Z cz1{}, cz2{}; +static volatile Z vz1, vz2; +Z ga[4]; +Z *const pga = &ga[3]; + +static bool distinct(const void *a, const void *b) { return a != b; } + +static long off(const void *p, const void *base) { + return (const char *)p - (const char *)base; +} + +/* Write the C++ size of an object. */ +template static void scribble(X &x) { memset(&x, 0x5a, sizeof x); } + +static int byval(Z a, Z b, int x) { + int y = x; + scribble(a); + scribble(b); + CHECK(y == x); + return distinct(&a, &b); +} + +static int unnamed(Z, int x, Z) { return x; } + +template static int pack(int x, P... p) { + Z *ps[] = {&p...}; + for (unsigned k = 0; k < sizeof...(P); ++k) memset(ps[k], 0x5a, 1); + CHECK(sizeof...(P) < 2 || ps[0] != ps[1]); + return x; +} + +struct K { + int a; + Z z; + int b; + K(Z q, int i) : a(i), z(q), b(i) { + scribble(q); + CHECK(a == i && b == i); + } +}; + +struct B { virtual B *v(Z, int x, ...) { return this; } }; +struct C0 { int pad; }; +struct D : C0, B { + D *v(Z z, int x, ...) { + int y = x; + scribble(z); + CHECK(y == x); + return this; + } +}; + +static T make_t() { return T(); } + +int main() { + char g1[8] = "1234567"; + Z l1, l2; + Z la[3]; + auto lam1 = [] { return 1; }; + auto lam2 = [] { return 2; }; + char g2[8] = "7654321"; + + CHECK(distinct(&l1, &l2)); + CHECK(distinct(&la[0], &la[1])); + CHECK(distinct(&lam1, &lam2)); + CHECK(distinct(&inc_early, &inc_late)); + CHECK(distinct(&Holder::z, &Holder::z)); + CHECK(off(&Holder::za[2], &Holder::za[0]) == 2); + CHECK(distinct(&tl1, &tl2)); + CHECK(distinct(&cz1, &cz2)); + CHECK(distinct((const void *)&vz1, (const void *)&vz2)); + CHECK(off(&ga[3], &ga[0]) == 3); + CHECK(off(pga, ga) == 3); + CHECK(off(&la[2], &la[0]) == 2); + CHECK(sizeof la == 3 && sizeof ga == 4); + + scribble(l1); + scribble(l2); + scribble(la); + scribble(lam1); + scribble(lam2); + CHECK(lam1() == 1 && lam2() == 2); + Z src; + memcpy(&l1, &src, sizeof l1); + l2 = src; + la[1] = l1; + CHECK(strcmp(g1, "1234567") == 0); + CHECK(strcmp(g2, "7654321") == 0); + + /* By-value parameters. */ + CHECK(byval(l1, l2, 7)); + CHECK(unnamed(l1, 4, l2) == 4); + CHECK(pack(5, l1, l2, l1) == 5); + int (*fp)(Z, int) = [](Z a, int x) { int y = x; scribble(a); return y; }; + CHECK(fp(l1, 9) == 9); + auto gl = [](auto a, int x) { int y = x; scribble(a); return y; }; + CHECK(gl(l1, 8) == 8); + K k(l1, 6); + CHECK(k.a == 6 && k.b == 6); + D d; + B *bp = &d; + CHECK(bp->v(l1, 1, 2) == &d); + + /* Objects of a nontrivial empty class. */ + { + T t1, t2; + T ta[2]; + CHECK(T::live == 4); + CHECK(T::addrs[0] == &t1 && T::addrs[1] == &t2); + CHECK(T::addrs[2] == &ta[0] && T::addrs[3] == &ta[1]); + T t3 = make_t(); + CHECK(T::live == 5); + try { + T t4; + throw t4; + } catch (T &c) { + CHECK(distinct(&c, &t3)); + } + } + CHECK(T::live == 0); + return errors != 0; +} From 6df76dd09fb1ce5ae0c6f8ec4183f69d0983f159 Mon Sep 17 00:00:00 2001 From: Alexander Herz Date: Fri, 20 Nov 2026 12:42:43 +0000 Subject: [PATCH 2/2] Drop redundant casts of type sizes [GH #560] Following review feedback on a related pull request, pass the type size to write_unsigned_num without a cast: a_targ_size_t is already a_host_large_unsigned. This keeps each call on one line. No functional change. --- src/c_gen_be.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/c_gen_be.c b/src/c_gen_be.c index 16926f070b..e677ab01c6 100644 --- a/src/c_gen_be.c +++ b/src/c_gen_be.c @@ -8965,8 +8965,7 @@ parameters. write_tok_str("char "); dump_declared_variable_name(variable); write_tok_ch('['); - write_unsigned_num( - (a_host_large_unsigned)skip_typerefs(var_type)->size); + write_unsigned_num(skip_typerefs(var_type)->size); write_tok_ch(']'); } else { dump_general_declaration_using_type(var_type, @@ -9467,8 +9466,7 @@ given that are referenced via such storage (see param_has_char_storage). dump_declared_variable_name(param); write_str("__s"); write_tok_ch('['); - write_unsigned_num( - (a_host_large_unsigned)skip_typerefs(param->type)->size); + write_unsigned_num(skip_typerefs(param->type)->size); write_tok_str("];"); } /* if */ } /* for */