Skip to content

[C2y] Add test coverage and documentation for WG14 N3341 #115478

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions clang/docs/LanguageExtensions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3341.pdf>`_
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 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3342.pdf>`_
which made qualified function types implementation-defined rather than
undefined. Clang has always accepted ``const`` and ``volatile`` qualified
Expand Down
11 changes: 6 additions & 5 deletions clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions clang/test/C/C2y/n3341.c
Original file line number Diff line number Diff line change
@@ -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}}

2 changes: 1 addition & 1 deletion clang/www/c_status.html
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ <h2 id="c2y">C2y implementation status</h2>
<tr>
<td>Slay Some Earthly Demons III</td>
<td><a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3341.pdf">N3341</a></td>
<td class="unknown" align="center">Unknown</td>
<td class="full" align="center">Yes</td>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why the patch comes with release note, yet the official support is not claimed starting from clang 20?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It went from UB to implementation-defined, but we've always defined (and failed to document) the behavior. So there were no substantive changes in Clang 20, which is why I claimed "Yes" instead of Clang 20. The release note is mostly for user awareness about the change in diagnostic behavior.

</tr>
<tr>
<td>Slay Some Earthly Demons IV</td>
Expand Down
Loading