Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 120 additions & 10 deletions src/c_gen_be.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -8886,12 +8961,20 @@ 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(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 */
Expand All @@ -8917,7 +9000,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 &&
Expand Down Expand Up @@ -9364,6 +9451,28 @@ 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(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.
Expand Down Expand Up @@ -9393,6 +9502,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,
Expand Down
40 changes: 37 additions & 3 deletions src/il_to_str.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 */
Expand Down
9 changes: 9 additions & 0 deletions src/pending_changes/gh-560-empty-class-object-storage.txt
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions tests/expectations/edg_x86_32/edg.log
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions tests/expectations/edg_x86_32_cp/edg.log
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/edg_x86_64/edg.log
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion tests/expectations/edg_x86_64/imported.log
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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
11 changes: 4 additions & 7 deletions tests/tests/edg/cmeerw/.gnats-26621-run.rto/default.1.1.txt
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Loading
Loading