]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Improve error message for excess elements in array initializer from {"a"}
authorAndrew Pinski <apinski@marvell.com>
Wed, 30 Nov 2022 04:02:07 +0000 (04:02 +0000)
committerAndrew Pinski <apinski@marvell.com>
Sun, 30 Apr 2023 21:48:59 +0000 (21:48 +0000)
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
gcc/testsuite/gcc.dg/init-bad-1.c
gcc/testsuite/gcc.dg/init-bad-2.c
gcc/testsuite/gcc.dg/init-bad-3.c
gcc/testsuite/gcc.dg/init-excess-3.c
gcc/testsuite/gcc.dg/pr61096-1.c

index 9cc7e0143a93ccd87eebc62db89af23e315b5486..f619e2a67e7aec8cfd093b59acfc79b692b1b2c7 100644 (file)
@@ -10647,7 +10647,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)
index 0da10c31539a1645e53906fd5876d82c086f3f5e..7c80006670cca0fd66a8bfb46f8d07fef74fd38b 100644 (file)
@@ -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 = { };
index 4775c4807bb6675c75cdf6b213a05ce2b0c08c52..57fd9f99f82b12fddf6a999917fd86f202f61ccd 100644 (file)
@@ -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 };
index c5c338d9a85b3389b5557f82b0fed7a4843af737..c22e8ec669818602f7cd9907c1a9e7650cb0b56e 100644 (file)
@@ -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 };
index 7741261bd49a84cf9247337a32cbbc5c24a4451e..c03a98487b46238717f2f09fc2fe31156fc3a1f1 100644 (file)
@@ -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 } */
index f41789c5f527946fc014a391a1c6239aeeebbc16..90ffb8087d2894bb2292b07b2e918dfdc01b3ff3 100644 (file)
@@ -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)