public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/115566] New: Arrays of character type initialized with parenthesized string literals shouldn't be diagnosed with -pedantic (at least in C23)
@ 2024-06-20 20:50 luigighiron at gmail dot com
  2024-06-20 20:57 ` [Bug c/115566] " pinskia at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: luigighiron at gmail dot com @ 2024-06-20 20:50 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115566

            Bug ID: 115566
           Summary: Arrays of character type initialized with
                    parenthesized string literals shouldn't be diagnosed
                    with -pedantic (at least in C23)
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: luigighiron at gmail dot com
  Target Milestone: ---

GCC does not accept initializing an array of character type with a
parenthesized string literal when compiling with -pedantic, for example:

char x[]=("abc");

This causes a warning, or an error with -pedantic-errors. This should be valid
because of the following:

> A parenthesized expression is a primary expression. Its type, value, and
> semantics are identical to those of the unparenthesized expression.
Section 6.5.2 "Primary Expressions" Paragraph 6 N3220

The semantics of the expression ("abc") are equivalent to those of "abc" so
this declaration should be valid. The wording in prior versions was not so
clear that the semantics are the same, but I assume the intent was still to
allow this. Clang accepts this, even with -std=c89 -pedantic. MSVC accepts this
with all warnings enabled. Interestingly, GCC correctly accepts using _Generic
expressions which have a string literal as the selected branch to be used in
initialization of an array of character type:

char x[]=_Generic(0,int:"abc");

Parenthesizing the _Generic expression or the string literal itself causes GCC
to create a warning again. Multiple nested _Generic expressions works as well.

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

* [Bug c/115566] Arrays of character type initialized with parenthesized string literals shouldn't be diagnosed with -pedantic (at least in C23)
  2024-06-20 20:50 [Bug c/115566] New: Arrays of character type initialized with parenthesized string literals shouldn't be diagnosed with -pedantic (at least in C23) luigighiron at gmail dot com
@ 2024-06-20 20:57 ` pinskia at gcc dot gnu.org
  2024-06-20 21:51 ` pinskia at gcc dot gnu.org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-20 20:57 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115566

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
/* If TYPE is an array type and EXPR is a parenthesized string
   constant, warn if pedantic that EXPR is being used to initialize an
   object of type TYPE.  */

void
maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
{
  if (pedantic
      && TREE_CODE (type) == ARRAY_TYPE
      && TREE_CODE (expr.value) == STRING_CST
      && expr.original_code != STRING_CST)
    pedwarn_init (loc, OPT_Wpedantic,
                  "array initialized from parenthesized string constant");
}

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

* [Bug c/115566] Arrays of character type initialized with parenthesized string literals shouldn't be diagnosed with -pedantic (at least in C23)
  2024-06-20 20:50 [Bug c/115566] New: Arrays of character type initialized with parenthesized string literals shouldn't be diagnosed with -pedantic (at least in C23) luigighiron at gmail dot com
  2024-06-20 20:57 ` [Bug c/115566] " pinskia at gcc dot gnu.org
@ 2024-06-20 21:51 ` pinskia at gcc dot gnu.org
  2024-06-20 22:06 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-20 21:51 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115566

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
>. Clang accepts this, even with -std=c89 -pedantic.

Well that is definitely a clang issue. See PR 11250 which added this warning.

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

* [Bug c/115566] Arrays of character type initialized with parenthesized string literals shouldn't be diagnosed with -pedantic (at least in C23)
  2024-06-20 20:50 [Bug c/115566] New: Arrays of character type initialized with parenthesized string literals shouldn't be diagnosed with -pedantic (at least in C23) luigighiron at gmail dot com
  2024-06-20 20:57 ` [Bug c/115566] " pinskia at gcc dot gnu.org
  2024-06-20 21:51 ` pinskia at gcc dot gnu.org
@ 2024-06-20 22:06 ` pinskia at gcc dot gnu.org
  2024-06-20 22:17 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-20 22:06 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115566

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
C11 still has it:

6.7.9/14:
An array of character type may be initialized by a character string literal
orUTF−8 string literal, optionally enclosed in braces.




C23 still does too:

6.7.11/15:

Anarray of character type may be initialized by a character string literal or
UTF-8 string literal, optionally enclosed in braces. 


The wording here has not changed from C90 either.

So yes GCC is correct in the pedantic warning here.

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

* [Bug c/115566] Arrays of character type initialized with parenthesized string literals shouldn't be diagnosed with -pedantic (at least in C23)
  2024-06-20 20:50 [Bug c/115566] New: Arrays of character type initialized with parenthesized string literals shouldn't be diagnosed with -pedantic (at least in C23) luigighiron at gmail dot com
                   ` (2 preceding siblings ...)
  2024-06-20 22:06 ` pinskia at gcc dot gnu.org
@ 2024-06-20 22:17 ` pinskia at gcc dot gnu.org
  2024-06-20 22:21 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-20 22:17 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115566

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note HP's aCC errored out on this code even. So it was not just something GCC
added to be extra pedantic either (this was mentioned in PR 11250 too).

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

* [Bug c/115566] Arrays of character type initialized with parenthesized string literals shouldn't be diagnosed with -pedantic (at least in C23)
  2024-06-20 20:50 [Bug c/115566] New: Arrays of character type initialized with parenthesized string literals shouldn't be diagnosed with -pedantic (at least in C23) luigighiron at gmail dot com
                   ` (3 preceding siblings ...)
  2024-06-20 22:17 ` pinskia at gcc dot gnu.org
@ 2024-06-20 22:21 ` pinskia at gcc dot gnu.org
  2024-06-20 22:36 ` luigighiron at gmail dot com
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-20 22:21 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115566

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
https://gcc.gnu.org/pipermail/gcc-patches/2004-July/144923.html

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

* [Bug c/115566] Arrays of character type initialized with parenthesized string literals shouldn't be diagnosed with -pedantic (at least in C23)
  2024-06-20 20:50 [Bug c/115566] New: Arrays of character type initialized with parenthesized string literals shouldn't be diagnosed with -pedantic (at least in C23) luigighiron at gmail dot com
                   ` (4 preceding siblings ...)
  2024-06-20 22:21 ` pinskia at gcc dot gnu.org
@ 2024-06-20 22:36 ` luigighiron at gmail dot com
  2024-06-22 20:20 ` jsm28 at gcc dot gnu.org
  2024-06-22 23:49 ` harald at gigawatt dot nl
  7 siblings, 0 replies; 9+ messages in thread
From: luigighiron at gmail dot com @ 2024-06-20 22:36 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115566

--- Comment #6 from Halalaluyafail3 <luigighiron at gmail dot com> ---
(In reply to Andrew Pinski from comment #3)
> C11 still has it:
> 
> 6.7.9/14:
> An array of character type may be initialized by a character string literal
> orUTF−8 string literal, optionally enclosed in braces.
> 
> 
> 
> 
> C23 still does too:
> 
> 6.7.11/15:
> 
> Anarray of character type may be initialized by a character string literal
> or UTF-8 string literal, optionally enclosed in braces. 
> 
> 
> The wording here has not changed from C90 either.
> 
> So yes GCC is correct in the pedantic warning here.

The way that I interpret this is that an array of character type may be
initialized by a character string literal, and a parenthesized string literal
has the same semantics as a string literal so it may be used as well (this text
is under the semantics section). Also, I don't see how the warning is correct
with the interpretation that it has to be specifically an expression that is a
string literal and nothing more complicated given that _Generic is accepted.

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

* [Bug c/115566] Arrays of character type initialized with parenthesized string literals shouldn't be diagnosed with -pedantic (at least in C23)
  2024-06-20 20:50 [Bug c/115566] New: Arrays of character type initialized with parenthesized string literals shouldn't be diagnosed with -pedantic (at least in C23) luigighiron at gmail dot com
                   ` (5 preceding siblings ...)
  2024-06-20 22:36 ` luigighiron at gmail dot com
@ 2024-06-22 20:20 ` jsm28 at gcc dot gnu.org
  2024-06-22 23:49 ` harald at gigawatt dot nl
  7 siblings, 0 replies; 9+ messages in thread
From: jsm28 at gcc dot gnu.org @ 2024-06-22 20:20 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115566

--- Comment #7 from Joseph S. Myers <jsm28 at gcc dot gnu.org> ---
The initializer with _Generic should not be accepted; that should be reported
as a bug.

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

* [Bug c/115566] Arrays of character type initialized with parenthesized string literals shouldn't be diagnosed with -pedantic (at least in C23)
  2024-06-20 20:50 [Bug c/115566] New: Arrays of character type initialized with parenthesized string literals shouldn't be diagnosed with -pedantic (at least in C23) luigighiron at gmail dot com
                   ` (6 preceding siblings ...)
  2024-06-22 20:20 ` jsm28 at gcc dot gnu.org
@ 2024-06-22 23:49 ` harald at gigawatt dot nl
  7 siblings, 0 replies; 9+ messages in thread
From: harald at gigawatt dot nl @ 2024-06-22 23:49 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115566

Harald van Dijk <harald at gigawatt dot nl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |harald at gigawatt dot nl

--- Comment #8 from Harald van Dijk <harald at gigawatt dot nl> ---
I believe this bug is valid; the change from

> A parenthesized expression is a primary expression. Its type and value are
> identical to those of the unparenthesized expression. It is an lvalue, a
> function designator, or a void expression if the unparenthesized expression
> is, respectively, an lvalue, a function designator, or a void expression.

to the already quoted

> A parenthesized expression is a primary expression. Its type, value, and
> semantics are identical to those of the unparenthesized expression.

only makes sense if the intent was to to also make parenthesized expressions
equivalent to unparenthesized expressions in other ways than those previously
enumerated.

But at any rate, GCC is inconsistent. The exact same argument applies to null
pointer constants. Null pointer constants are defined as

> An integer constant expression with the value 0, or such an expression cast
> to type void *, is called a null pointer constant.

Note that such an expression cast to type void *, and then wrapped in
parentheses, is not explicitly included.

Yet GCC accepts

  #define NULL ((void*)0)
  int main(void) {
    void(*fp)(void) = NULL;
  }

when the same overly pedantic reading should result in "ISO C forbids
initialization between function pointer and 'void *'" here.

Either parenthesized expressions are just generally equivalent to
non-parenthesized expressions, and both the OP's code and this program are
valid, or parenthesized expressions are only like the non-parenthesized
expressions in the specifically enumerated ways, in which case both the OP's
code and this program violate a constraint and require a diagnostic. You can
choose which of those interpretations you prefer, but they both indicate a bug
in GCC.

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

end of thread, other threads:[~2024-06-22 23:49 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-20 20:50 [Bug c/115566] New: Arrays of character type initialized with parenthesized string literals shouldn't be diagnosed with -pedantic (at least in C23) luigighiron at gmail dot com
2024-06-20 20:57 ` [Bug c/115566] " pinskia at gcc dot gnu.org
2024-06-20 21:51 ` pinskia at gcc dot gnu.org
2024-06-20 22:06 ` pinskia at gcc dot gnu.org
2024-06-20 22:17 ` pinskia at gcc dot gnu.org
2024-06-20 22:21 ` pinskia at gcc dot gnu.org
2024-06-20 22:36 ` luigighiron at gmail dot com
2024-06-22 20:20 ` jsm28 at gcc dot gnu.org
2024-06-22 23:49 ` harald at gigawatt dot nl

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).