Issue 176170
Summary Clang accepts parenthesized aggregate initializer for comma _expression_
Labels clang
Assignees
Reporter vrukesh
    **What code / commands /steps will reproduce the problem?**
Compile the below sample code.

_struct {
    char a[100];
    char b[100];
    int c;
} explodes[] = {
    ("", "", 1)
};int main(void) {
    return 0;
}_


**Compiler Explorer link:** 
https://godbolt.org/z/x4rbjKPG1

**What is the expected result?**
Compilation should fail.

**What happens instead?**
The initializer uses parentheses ("", "", 1), so it is parsed as a single comma-_expression_ of type int (value 1), not as a brace-enclosed initializer for the struct.
As per the standard:

_A declarator can specify an initial value for the identifier being declared. The identifier designates a
variable being initialized. The process of initialization described in the remainder of 11.6 applies also to
initializations specified by other syntactic contexts, such as the initialization of function parameters (8.2.2) or
the initialization of return values (9.6.3).
initializer:
brace-or-equal-initializer
( _expression_-list )
brace-or-equal-initializer:
= initializer-clause
braced-init-list
initializer-clause:
assignment-_expression_
braced-init-list
initializer-list:
initializer-clause ...opt
initializer-list , initializer-clause ...opt
braced-init-list:
{ initializer-list ,opt }
{}
expr-or-braced-init-list:
_expression_
braced-init-list_



**GCC fails compilation.**
<source>:6:5: error: initializer element is not constant
    6 |     ("", "", 1)
      |     ^
<source>:6:5: note: (near initialization for 'explodes[0].a[0]')
Compiler returned: 1


**Clang just emits warning.**
<source>:6:6: warning: left operand of comma operator has no effect [-Wunused-value]
    6 |     ("", "", 1)
 |      ^~
<source>:6:10: warning: left operand of comma operator has no effect [-Wunused-value]
    6 |     ("", "", 1)
      |          ^~
2 warnings generated.
ASM generation compiler returned: 0
<source>:6:6: warning: left operand of comma operator has no effect [-Wunused-value]
    6 |     ("", "", 1)
      |      ^~
<source>:6:10: warning: left operand of comma operator has no effect [-Wunused-value]
    6 |     ("", "", 1)
      |          ^~
2 warnings generated.
Execution build compiler returned: 0
Program returned: 0
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to