diff options
author | Tom Honermann <tom@honermann.net> | 2019-01-17 20:43:38 +0000 |
---|---|---|
committer | Jason Merrill <jason@gcc.gnu.org> | 2019-01-17 15:43:38 -0500 |
commit | 17ad43dd4c5dc1a39165a6b2108f4ea793e15eed (patch) | |
tree | 4985f32cf568ffe5e45c2d6f029141b9362830cd /gcc/c | |
parent | f18aa3a4078f83540903c5d1f5c4ad0e25597ab1 (diff) | |
download | gcc-17ad43dd4c5dc1a39165a6b2108f4ea793e15eed.zip gcc-17ad43dd4c5dc1a39165a6b2108f4ea793e15eed.tar.gz gcc-17ad43dd4c5dc1a39165a6b2108f4ea793e15eed.tar.bz2 |
Improve the C error for mismatched array string literal initialization.
* c-typeck.c (digest_init): Revised the error message produced for
ill-formed cases of array initialization with a string literal.
(error_init): Make variadic.
Co-Authored-By: Jason Merrill <jason@redhat.com>
From-SVN: r268047
Diffstat (limited to 'gcc/c')
-rw-r--r-- | gcc/c/ChangeLog | 7 | ||||
-rw-r--r-- | gcc/c/c-typeck.c | 44 |
2 files changed, 27 insertions, 24 deletions
diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog index 167c78a..2accb8f 100644 --- a/gcc/c/ChangeLog +++ b/gcc/c/ChangeLog @@ -1,3 +1,10 @@ +2019-01-16 Tom Honermann <tom@honermann.net> + Jason Merrill <jason@redhat.com> + + * c-typeck.c (digest_init): Revised the error message produced for + ill-formed cases of array initialization with a string literal. + (error_init): Make variadic. + 2019-01-12 Jakub Jelinek <jakub@redhat.com> * c-typeck.c (convert_for_assignment): Fix a comment typo. diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c index 63d177f..6da1f32 100644 --- a/gcc/c/c-typeck.c +++ b/gcc/c/c-typeck.c @@ -6339,17 +6339,21 @@ convert_to_anonymous_field (location_t location, tree type, tree rhs) GMSGID identifies the message. The component name is taken from the spelling stack. */ -static void -error_init (location_t loc, const char *gmsgid) +static void ATTRIBUTE_GCC_DIAG (2,0) +error_init (location_t loc, const char *gmsgid, ...) { char *ofwhat; auto_diagnostic_group d; /* The gmsgid may be a format string with %< and %>. */ - error_at (loc, gmsgid); + va_list ap; + va_start (ap, gmsgid); + bool warned = emit_diagnostic_valist (DK_ERROR, loc, -1, gmsgid, &ap); + va_end (ap); + ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); - if (*ofwhat) + if (*ofwhat && warned) inform (loc, "(near initialization for %qs)", ofwhat); } @@ -7722,6 +7726,7 @@ digest_init (location_t init_loc, tree type, tree init, tree origtype, { struct c_expr expr; tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init))); + bool incompat_string_cst = false; expr.value = inside_init; expr.original_code = (strict_string ? STRING_CST : ERROR_MARK); expr.original_type = NULL; @@ -7738,27 +7743,18 @@ digest_init (location_t init_loc, tree type, tree init, tree origtype, if (char_array) { if (typ2 != char_type_node) - { - error_init (init_loc, "char-array initialized from wide " - "string"); - return error_mark_node; - } - } - else - { - if (typ2 == char_type_node) - { - error_init (init_loc, "wide character array initialized " - "from non-wide string"); - return error_mark_node; - } - else if (!comptypes(typ1, typ2)) - { - error_init (init_loc, "wide character array initialized " - "from incompatible wide string"); - return error_mark_node; - } + incompat_string_cst = true; } + else if (!comptypes (typ1, typ2)) + incompat_string_cst = true; + + if (incompat_string_cst) + { + error_init (init_loc, "cannot initialize array of %qT from " + "a string literal with type array of %qT", + typ1, typ2); + return error_mark_node; + } if (TYPE_DOMAIN (type) != NULL_TREE && TYPE_SIZE (type) != NULL_TREE |