public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH 1/2] Fix C/107926: Wrong error message when initializing char array
@ 2022-11-30 17:18 apinski
  2022-11-30 17:18 ` [PATCH 2/2] Improve error message for excess elements in array initializer from {"a"} apinski
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: apinski @ 2022-11-30 17:18 UTC (permalink / raw)
  To: gcc-patches; +Cc: Andrew Pinski

From: Andrew Pinski <apinski@marvell.com>

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 ceck for string cst until
	after the error message.

gcc/testsuite/ChangeLog:

	PR c/107926
	* gcc.dg/init-excess-3.c: New test.
---
 gcc/c/c-typeck.cc                    | 15 ++++++++++-----
 gcc/testsuite/gcc.dg/init-excess-3.c | 15 +++++++++++++++
 2 files changed, 25 insertions(+), 5 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/init-excess-3.c

diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc
index e06f052..0fc382c 100644
--- a/gcc/c/c-typeck.cc
+++ b/gcc/c/c-typeck.cc
@@ -10623,17 +10623,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)
diff --git a/gcc/testsuite/gcc.dg/init-excess-3.c b/gcc/testsuite/gcc.dg/init-excess-3.c
new file mode 100644
index 0000000..7741261
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/init-excess-3.c
@@ -0,0 +1,15 @@
+/* Test for various cases of excess initializers for char arrays,
+   bug 107926. */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+
+char s0[] = {"abc",1}; /* { dg-error "array initializer|near init" } */
+char s1[] = {"abc","a"}; /* { dg-error "array initializer|near init" } */
+char s2[] = {1,"abc"}; /* { dg-error "array initializer|near init|computable at load time" } */
+/* { dg-warning "integer from pointer without a cast" "" { target *-*-* } .-1 } */
+
+char s3[5] = {"abc",1}; /* { dg-error "array initializer|near init" } */
+char s4[5] = {"abc","a"}; /* { dg-error "array initializer|near init" } */
+char s5[5] = {1,"abc"}; /* { dg-error "array initializer|near init|computable at load time" } */
+/* { dg-warning "integer from pointer without a cast" "" { target *-*-* } .-1 } */
-- 
1.8.3.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 2/2] Improve error message for excess elements in array initializer from {"a"}
  2022-11-30 17:18 [PATCH 1/2] Fix C/107926: Wrong error message when initializing char array apinski
@ 2022-11-30 17:18 ` apinski
  2022-11-30 23:05   ` Segher Boessenkool
  2023-04-30 16:24   ` Jeff Law
  2022-11-30 17:29 ` [PATCH 1/2] Fix C/107926: Wrong error message when initializing char array Jakub Jelinek
  2023-04-30 16:23 ` Jeff Law
  2 siblings, 2 replies; 8+ messages in thread
From: apinski @ 2022-11-30 17:18 UTC (permalink / raw)
  To: gcc-patches; +Cc: Andrew Pinski

From: Andrew Pinski <apinski@marvell.com>

So char arrays are not the only type that be initialized from {"a"}.
We can have wchar_t (L"") and char16_t (u"") types too. So let's
print out the type of the array instead of just saying char.

Note in the testsuite I used regex . to match '[' and ']' as
I could not figure out how many '\' I needed.

OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

gcc/c/ChangeLog:

	* c-typeck.cc (process_init_element): Print out array type
	for excessive elements.

gcc/testsuite/ChangeLog:

	* gcc.dg/init-bad-1.c: Update error message.
	* gcc.dg/init-bad-2.c: Likewise.
	* gcc.dg/init-bad-3.c: Likewise.
	* gcc.dg/init-excess-3.c: Likewise.
	* gcc.dg/pr61096-1.c: Likewise.
---
 gcc/c/c-typeck.cc                    |  2 +-
 gcc/testsuite/gcc.dg/init-bad-1.c    |  2 +-
 gcc/testsuite/gcc.dg/init-bad-2.c    |  2 +-
 gcc/testsuite/gcc.dg/init-bad-3.c    |  2 +-
 gcc/testsuite/gcc.dg/init-excess-3.c | 12 ++++++------
 gcc/testsuite/gcc.dg/pr61096-1.c     |  2 +-
 6 files changed, 11 insertions(+), 11 deletions(-)

diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc
index 0fc382c..f1a1752 100644
--- a/gcc/c/c-typeck.cc
+++ b/gcc/c/c-typeck.cc
@@ -10631,7 +10631,7 @@ process_init_element (location_t loc, struct c_expr value, bool implicit,
     {
       if (constructor_stack->replacement_value.value)
 	{
-	  error_init (loc, "excess elements in %<char%> array initializer");
+	  error_init (loc, "excess elements in %qT initializer", constructor_type);
 	  return;
 	}
       else if (string_flag)
diff --git a/gcc/testsuite/gcc.dg/init-bad-1.c b/gcc/testsuite/gcc.dg/init-bad-1.c
index 0da10c3..7c80006 100644
--- a/gcc/testsuite/gcc.dg/init-bad-1.c
+++ b/gcc/testsuite/gcc.dg/init-bad-1.c
@@ -18,7 +18,7 @@ char s[1] = "x";
 char s1[1] = { "x" };
 char t[1] = "xy"; /* { dg-warning "initializer-string for array of 'char' is too long" } */
 char t1[1] = { "xy" }; /* { dg-warning "initializer-string for array of 'char' is too long" } */
-char u[1] = { "x", "x" }; /* { dg-error "excess elements in 'char' array initializer" } */
+char u[1] = { "x", "x" }; /* { dg-error "excess elements in 'char.1.' initializer" } */
 /* { dg-message "near init" "near" { target *-*-* } .-1 } */
 
 int i = { };
diff --git a/gcc/testsuite/gcc.dg/init-bad-2.c b/gcc/testsuite/gcc.dg/init-bad-2.c
index 4775c48..57fd9f9 100644
--- a/gcc/testsuite/gcc.dg/init-bad-2.c
+++ b/gcc/testsuite/gcc.dg/init-bad-2.c
@@ -19,7 +19,7 @@ char s[1] = "x";
 char s1[1] = { "x" };
 char t[1] = "xy"; /* { dg-warning "initializer-string for array of 'char' is too long" } */
 char t1[1] = { "xy" }; /* { dg-warning "initializer-string for array of 'char' is too long" } */
-char u[1] = { "x", "x" }; /* { dg-error "excess elements in 'char' array initializer" } */
+char u[1] = { "x", "x" }; /* { dg-error "excess elements in 'char.1.' initializer" } */
 /* { dg-message "near init" "near" { target *-*-* } .-1 } */
 
 int j = { 1 };
diff --git a/gcc/testsuite/gcc.dg/init-bad-3.c b/gcc/testsuite/gcc.dg/init-bad-3.c
index c5c338d..c22e8ec 100644
--- a/gcc/testsuite/gcc.dg/init-bad-3.c
+++ b/gcc/testsuite/gcc.dg/init-bad-3.c
@@ -19,7 +19,7 @@ char s[1] = "x";
 char s1[1] = { "x" };
 char t[1] = "xy"; /* { dg-error "initializer-string for array of 'char' is too long" } */
 char t1[1] = { "xy" }; /* { dg-error "initializer-string for array of 'char' is too long" } */
-char u[1] = { "x", "x" }; /* { dg-error "excess elements in 'char' array initializer" } */
+char u[1] = { "x", "x" }; /* { dg-error "excess elements in 'char.1.' initializer" } */
 /* { dg-message "near init" "near" { target *-*-* } .-1 } */
 
 int j = { 1 };
diff --git a/gcc/testsuite/gcc.dg/init-excess-3.c b/gcc/testsuite/gcc.dg/init-excess-3.c
index 7741261..c03a984 100644
--- a/gcc/testsuite/gcc.dg/init-excess-3.c
+++ b/gcc/testsuite/gcc.dg/init-excess-3.c
@@ -4,12 +4,12 @@
 /* { dg-options "" } */
 
 
-char s0[] = {"abc",1}; /* { dg-error "array initializer|near init" } */
-char s1[] = {"abc","a"}; /* { dg-error "array initializer|near init" } */
-char s2[] = {1,"abc"}; /* { dg-error "array initializer|near init|computable at load time" } */
+char s0[] = {"abc",1}; /* { dg-error "'char..' initializer|near init" } */
+char s1[] = {"abc","a"}; /* { dg-error "'char..' initializer|near init" } */
+char s2[] = {1,"abc"}; /* { dg-error "'char..' initializer|near init|computable at load time" } */
 /* { dg-warning "integer from pointer without a cast" "" { target *-*-* } .-1 } */
 
-char s3[5] = {"abc",1}; /* { dg-error "array initializer|near init" } */
-char s4[5] = {"abc","a"}; /* { dg-error "array initializer|near init" } */
-char s5[5] = {1,"abc"}; /* { dg-error "array initializer|near init|computable at load time" } */
+char s3[5] = {"abc",1}; /* { dg-error "'char.5.' initializer|near init" } */
+char s4[5] = {"abc","a"}; /* { dg-error "'char.5.' initializer|near init" } */
+char s5[5] = {1,"abc"}; /* { dg-error "'char.5.' initializer|near init|computable at load time" } */
 /* { dg-warning "integer from pointer without a cast" "" { target *-*-* } .-1 } */
diff --git a/gcc/testsuite/gcc.dg/pr61096-1.c b/gcc/testsuite/gcc.dg/pr61096-1.c
index f41789c..90ffb80 100644
--- a/gcc/testsuite/gcc.dg/pr61096-1.c
+++ b/gcc/testsuite/gcc.dg/pr61096-1.c
@@ -47,7 +47,7 @@ __extension__ int a15[10] = {[2 ... 1] = 4 }; /* { dg-error "31:empty index rang
 __extension__ int a16[10] = {[2 ... 100] = 4 }; /* { dg-error "31:array index range in initializer exceeds array bounds" } */
 int a17[] = { .B = 1 }; /* { dg-error "15:field name not in record or union initializer" } */
 int a18[] = { e }; /* { dg-error "15:initializer element is not constant" } */
-char a19[1] = { "x", "x" }; /* { dg-error "22:excess elements in 'char' array initializer" } */
+char a19[1] = { "x", "x" }; /* { dg-error "22:excess elements in 'char.1.' initializer" } */
 
 void
 bar (void)
-- 
1.8.3.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/2] Fix C/107926: Wrong error message when initializing char array
  2022-11-30 17:18 [PATCH 1/2] Fix C/107926: Wrong error message when initializing char array apinski
  2022-11-30 17:18 ` [PATCH 2/2] Improve error message for excess elements in array initializer from {"a"} apinski
@ 2022-11-30 17:29 ` Jakub Jelinek
  2023-04-30 16:23 ` Jeff Law
  2 siblings, 0 replies; 8+ messages in thread
From: Jakub Jelinek @ 2022-11-30 17:29 UTC (permalink / raw)
  To: apinski; +Cc: gcc-patches

On Wed, Nov 30, 2022 at 09:18:14AM -0800, apinski--- via Gcc-patches wrote:
> gcc/c/ChangeLog:
> 
> 	PR c/107926
> 	* c-typeck.cc (process_init_element):
> 	Move the ceck for string cst until
> 	after the error message.

Just a ChangeLog nit, not a patch review for which I defer to C FE
maintainers/reviewers.
s/ceck/check/, plus don't start the description uselessly on a next line
when half of it would fit on the first line after ):.
 	* c-typeck.cc (process_init_element): Move the check for string cst
	until after the error message.

	Jakub


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/2] Improve error message for excess elements in array initializer from {"a"}
  2022-11-30 17:18 ` [PATCH 2/2] Improve error message for excess elements in array initializer from {"a"} apinski
@ 2022-11-30 23:05   ` Segher Boessenkool
  2022-11-30 23:17     ` Andreas Schwab
  2023-04-30 16:24   ` Jeff Law
  1 sibling, 1 reply; 8+ messages in thread
From: Segher Boessenkool @ 2022-11-30 23:05 UTC (permalink / raw)
  To: apinski; +Cc: gcc-patches

Hi!

On Wed, Nov 30, 2022 at 09:18:15AM -0800, apinski--- via Gcc-patches wrote:
> Note in the testsuite I used regex . to match '[' and ']' as
> I could not figure out how many '\' I needed.

Don't use double quotes then :-)  Inside double quotes all of command
substitution, variable substitution, and backslash substitution are
performed.  In a regexp you typically want none of this.  You usually do
want the whitespace to be significant, so it is good to quote it in
braces though (unless you like quoting all your whitespace).

> -char u[1] = { "x", "x" }; /* { dg-error "excess elements in 'char' array initializer" } */
> +char u[1] = { "x", "x" }; /* { dg-error "excess elements in 'char.1.' initializer" } */

char u[1] = { "x", "x" }; /* { dg-error {excess elements in 'char[1]' initializer} } */

See <https://www.tcl.tk/man/tcl8.6.13/TclCmd/Tcl.html> for a very short
page that has *all* Tcl syntax!


Segher

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/2] Improve error message for excess elements in array initializer from {"a"}
  2022-11-30 23:05   ` Segher Boessenkool
@ 2022-11-30 23:17     ` Andreas Schwab
  2022-11-30 23:31       ` Segher Boessenkool
  0 siblings, 1 reply; 8+ messages in thread
From: Andreas Schwab @ 2022-11-30 23:17 UTC (permalink / raw)
  To: Segher Boessenkool; +Cc: apinski, gcc-patches

On Nov 30 2022, Segher Boessenkool wrote:

> char u[1] = { "x", "x" }; /* { dg-error {excess elements in 'char[1]' initializer} } */

That won't work, as '[1]' is a bracket expression only matching '1'.
You'll need {... 'char\[1\]' ...}.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/2] Improve error message for excess elements in array initializer from {"a"}
  2022-11-30 23:17     ` Andreas Schwab
@ 2022-11-30 23:31       ` Segher Boessenkool
  0 siblings, 0 replies; 8+ messages in thread
From: Segher Boessenkool @ 2022-11-30 23:31 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: apinski, gcc-patches

On Thu, Dec 01, 2022 at 12:17:30AM +0100, Andreas Schwab wrote:
> On Nov 30 2022, Segher Boessenkool wrote:
> 
> > char u[1] = { "x", "x" }; /* { dg-error {excess elements in 'char[1]' initializer} } */
> 
> That won't work, as '[1]' is a bracket expression only matching '1'.
> You'll need {... 'char\[1\]' ...}.

Heh.  Yeah, char\[1].  Sorry.


Segher

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/2] Fix C/107926: Wrong error message when initializing char array
  2022-11-30 17:18 [PATCH 1/2] Fix C/107926: Wrong error message when initializing char array apinski
  2022-11-30 17:18 ` [PATCH 2/2] Improve error message for excess elements in array initializer from {"a"} apinski
  2022-11-30 17:29 ` [PATCH 1/2] Fix C/107926: Wrong error message when initializing char array Jakub Jelinek
@ 2023-04-30 16:23 ` Jeff Law
  2 siblings, 0 replies; 8+ messages in thread
From: Jeff Law @ 2023-04-30 16:23 UTC (permalink / raw)
  To: apinski, gcc-patches



On 11/30/22 10:18, apinski--- via Gcc-patches wrote:
> From: Andrew Pinski <apinski@marvell.com>
> 
> 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 ceck for string cst until
> 	after the error message.
> 
> gcc/testsuite/ChangeLog:
> 
> 	PR c/107926
> 	* gcc.dg/init-excess-3.c: New test.
OK
jeff

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/2] Improve error message for excess elements in array initializer from {"a"}
  2022-11-30 17:18 ` [PATCH 2/2] Improve error message for excess elements in array initializer from {"a"} apinski
  2022-11-30 23:05   ` Segher Boessenkool
@ 2023-04-30 16:24   ` Jeff Law
  1 sibling, 0 replies; 8+ messages in thread
From: Jeff Law @ 2023-04-30 16:24 UTC (permalink / raw)
  To: apinski, gcc-patches



On 11/30/22 10:18, apinski--- via Gcc-patches wrote:
> From: Andrew Pinski <apinski@marvell.com>
> 
> So char arrays are not the only type that be initialized from {"a"}.
> We can have wchar_t (L"") and char16_t (u"") types too. So let's
> print out the type of the array instead of just saying char.
> 
> Note in the testsuite I used regex . to match '[' and ']' as
> I could not figure out how many '\' I needed.
> 
> OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.
> 
> gcc/c/ChangeLog:
> 
> 	* c-typeck.cc (process_init_element): Print out array type
> 	for excessive elements.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* gcc.dg/init-bad-1.c: Update error message.
> 	* gcc.dg/init-bad-2.c: Likewise.
> 	* gcc.dg/init-bad-3.c: Likewise.
> 	* gcc.dg/init-excess-3.c: Likewise.
> 	* gcc.dg/pr61096-1.c: Likewise.
OK
jeff

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2023-04-30 16:24 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-30 17:18 [PATCH 1/2] Fix C/107926: Wrong error message when initializing char array apinski
2022-11-30 17:18 ` [PATCH 2/2] Improve error message for excess elements in array initializer from {"a"} apinski
2022-11-30 23:05   ` Segher Boessenkool
2022-11-30 23:17     ` Andreas Schwab
2022-11-30 23:31       ` Segher Boessenkool
2023-04-30 16:24   ` Jeff Law
2022-11-30 17:29 ` [PATCH 1/2] Fix C/107926: Wrong error message when initializing char array Jakub Jelinek
2023-04-30 16:23 ` Jeff Law

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).