aboutsummaryrefslogtreecommitdiff
path: root/gcc/c
diff options
context:
space:
mode:
authorAndrew Pinski <apinski@marvell.com>2022-11-30 02:54:57 +0000
committerAndrew Pinski <apinski@marvell.com>2023-04-30 21:48:59 +0000
commita6b810ae783acf8cec2d2272a46bd6de0976f496 (patch)
treeaf3d7726b73d91e3425f7a814a7f128f4f4a7b32 /gcc/c
parentc53237cefbad0c52799927d6fd99775426a8a92a (diff)
downloadgcc-a6b810ae783acf8cec2d2272a46bd6de0976f496.zip
gcc-a6b810ae783acf8cec2d2272a46bd6de0976f496.tar.gz
gcc-a6b810ae783acf8cec2d2272a46bd6de0976f496.tar.bz2
Fix C/107926: Wrong error message when initializing char array
The problem here is the code which handles {"a"} is supposed to handle the case where the is something after the string but it only handles the case where there is another string so we go down the other path and error out saying "excess elements in struct initializer" even though this was a character array. To fix this, we need to move the ckeck if the initializer is a string after the check for array and initializer. OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. Thanks, Adnrew Pinski gcc/c/ChangeLog: PR c/107926 * c-typeck.cc (process_init_element): Move the check for string cst until after the error message. gcc/testsuite/ChangeLog: PR c/107926 * gcc.dg/init-excess-3.c: New test.
Diffstat (limited to 'gcc/c')
-rw-r--r--gcc/c/c-typeck.cc15
1 files changed, 10 insertions, 5 deletions
diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc
index a178796..9cc7e01 100644
--- a/gcc/c/c-typeck.cc
+++ b/gcc/c/c-typeck.cc
@@ -10639,17 +10639,22 @@ process_init_element (location_t loc, struct c_expr value, bool implicit,
/* Handle superfluous braces around string cst as in
char x[] = {"foo"}; */
- if (string_flag
- && constructor_type
+ if (constructor_type
&& !was_designated
&& TREE_CODE (constructor_type) == ARRAY_TYPE
&& INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
&& integer_zerop (constructor_unfilled_index))
{
if (constructor_stack->replacement_value.value)
- error_init (loc, "excess elements in %<char%> array initializer");
- constructor_stack->replacement_value = value;
- return;
+ {
+ error_init (loc, "excess elements in %<char%> array initializer");
+ return;
+ }
+ else if (string_flag)
+ {
+ constructor_stack->replacement_value = value;
+ return;
+ }
}
if (constructor_stack->replacement_value.value != NULL_TREE)