diff --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst index a051eb95898ec..0998e6b30e229 100644 --- a/clang/docs/LanguageExtensions.rst +++ b/clang/docs/LanguageExtensions.rst @@ -5975,6 +5975,17 @@ Clang guarantees the following behaviors: Currently, the above extension only applies to C source code, not C++. + +Empty Objects in C +================== +The declaration of a structure or union type which has no named members is +undefined behavior (C23 and earlier) or implementation-defined behavior (C2y). +Clang allows the declaration of a structure or union type with no named members +in all C language modes. `sizeof` for such a type returns `0`, which is +different behavior than in C++ (where the size of such an object is typically +`1`). + + Qualified function types in C ============================= Declaring a function with a qualified type in C is undefined behavior (C23 and diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 77ba5f1d79bcd..c3424e0e6f34c 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -298,6 +298,12 @@ C2y Feature Support paper adopts Clang's existing practice, so there were no changes to compiler behavior. +- Implemented support for `N3341 `_ + which makes empty structure and union objects implementation-defined in C. + ``-Wgnu-empty-struct`` will be emitted in C23 and earlier modes because the + behavior is a conforming GNU extension in those modes, but will no longer + have an effect in C2y mode. + - Updated conformance for `N3342 `_ which made qualified function types implementation-defined rather than undefined. Clang has always accepted ``const`` and ``volatile`` qualified diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 6b0b4840a1eb2..61c29e320d5c7 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -19365,11 +19365,12 @@ void Sema::ActOnFields(Scope *S, SourceLocation RecLoc, Decl *EnclosingDecl, } // Structs without named members are extension in C (C99 6.7.2.1p7), - // but are accepted by GCC. - if (NonBitFields == 0 && !getLangOpts().CPlusPlus) { - Diag(RecLoc, IsEmpty ? diag::ext_empty_struct_union : - diag::ext_no_named_members_in_struct_union) - << Record->isUnion(); + // but are accepted by GCC. In C2y, this became implementation-defined + // (C2y 6.7.3.2p10). + if (NonBitFields == 0 && !getLangOpts().CPlusPlus && !getLangOpts().C2y) { + Diag(RecLoc, IsEmpty ? diag::ext_empty_struct_union + : diag::ext_no_named_members_in_struct_union) + << Record->isUnion(); } } } else { diff --git a/clang/test/C/C2y/n3341.c b/clang/test/C/C2y/n3341.c new file mode 100644 index 0000000000000..523c3dd945ac1 --- /dev/null +++ b/clang/test/C/C2y/n3341.c @@ -0,0 +1,16 @@ +// RUN: %clang_cc1 -verify -std=c2y -Wall -pedantic %s +// RUN: %clang_cc1 -verify=gnu -Wall -pedantic %s + +/* WG14 N3341: Yes + * Slay Some Earthly Demons III + * + * Empty structure and union objects are now implementation-defined. + */ + +// expected-no-diagnostics + +struct R {}; // gnu-warning {{empty struct is a GNU extension}} +struct S { struct { }; }; // gnu-warning {{empty struct is a GNU extension}} +struct T { int : 0; }; // gnu-warning {{struct without named members is a GNU extension}} +union U {}; // gnu-warning {{empty union is a GNU extension}} + diff --git a/clang/www/c_status.html b/clang/www/c_status.html index 663cc2e33f515..793e7006822cc 100644 --- a/clang/www/c_status.html +++ b/clang/www/c_status.html @@ -191,7 +191,7 @@

C2y implementation status

Slay Some Earthly Demons III N3341 - Unknown + Yes Slay Some Earthly Demons IV