[clang] 2fc5a34 - Add test coverage for more C DRs
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 29 11:44:46 PST 2022
Author: Aaron Ballman
Date: 2022-11-29T14:44:37-05:00
New Revision: 2fc5a3410087c209567c7e4a2c457b6896c127a3
URL: https://github.com/llvm/llvm-project/commit/2fc5a3410087c209567c7e4a2c457b6896c127a3
DIFF: https://github.com/llvm/llvm-project/commit/2fc5a3410087c209567c7e4a2c457b6896c127a3.diff
LOG: Add test coverage for more C DRs
This completes the initial pass over all of the C DRs.
Added:
clang/test/C/drs/dr5xx.c
Modified:
clang/test/C/drs/dr4xx.c
clang/www/c_dr_status.html
Removed:
################################################################################
diff --git a/clang/test/C/drs/dr4xx.c b/clang/test/C/drs/dr4xx.c
index 6acb47901bc4..768897cd4f2b 100644
--- a/clang/test/C/drs/dr4xx.c
+++ b/clang/test/C/drs/dr4xx.c
@@ -322,3 +322,63 @@ struct dr492_t {
};
int m13;
} dr492;
+
+/* WG14 DR496: yes
+ * offsetof questions
+ */
+void dr496(void) {
+ struct A { int n, a [2]; };
+ struct B { struct A a; };
+ struct C { struct A a[1]; };
+
+ /* The standard does not require either of these examples to work, but we
+ * support them just the same. The first one is invalid because it's
+ * referencing a member of a
diff erent struct, and the second one is invalid
+ * because it references an array of another struct. Clang calculates the
+ * correct offset to each of those fields.
+ */
+ _Static_assert(__builtin_offsetof(struct B, a.n) == 0, "");
+ /* First int below is for 'n' and the second int is for 'a[0]'; this presumes
+ * there is no padding involved.
+ */
+ _Static_assert(__builtin_offsetof(struct B, a.a[1]) == sizeof(int) + sizeof(int), "");
+
+ /* However, we do not support using the -> operator to access a member, even
+ * if that would be a valid expression. FIXME: GCC accepts this, perhaps we
+ * should as well.
+ */
+ (void)__builtin_offsetof(struct C, a->n); /* expected-error {{expected ')'}} \
+ expected-note {{to match this '('}}
+ */
+
+ /* The DR asked a question about whether defining a new type within offsetof
+ * is allowed. C2x N2350 made this explicitly undefined behavior, but Clang
+ * has always supported defining a type in this location, and GCC also
+ * supports it.
+ */
+ (void)__builtin_offsetof(struct S { int a; }, a);
+}
+
+/* WG14 DR499: yes
+ * Anonymous structure in union behavior
+ */
+void dr499(void) {
+ union U {
+ struct {
+ char B1;
+ char B2;
+ char B3;
+ char B4;
+ };
+ int word;
+ } u;
+
+ /* Validate that B1, B2, B3, and B4 do not have overlapping storage, only the
+ * anonymous structure and 'word' overlap.
+ */
+ _Static_assert(__builtin_offsetof(union U, B1) == 0, "");
+ _Static_assert(__builtin_offsetof(union U, B2) == 1, "");
+ _Static_assert(__builtin_offsetof(union U, B3) == 2, "");
+ _Static_assert(__builtin_offsetof(union U, B4) == 3, "");
+ _Static_assert(__builtin_offsetof(union U, word) == 0, "");
+}
diff --git a/clang/test/C/drs/dr5xx.c b/clang/test/C/drs/dr5xx.c
new file mode 100644
index 000000000000..68bcef78bacc
--- /dev/null
+++ b/clang/test/C/drs/dr5xx.c
@@ -0,0 +1,38 @@
+/* RUN: %clang_cc1 -std=c89 -verify=expected,c89only -pedantic -Wno-c11-extensions %s
+ RUN: %clang_cc1 -std=c99 -verify=expected -pedantic -Wno-c11-extensions %s
+ RUN: %clang_cc1 -std=c11 -verify=expected -pedantic %s
+ RUN: %clang_cc1 -std=c17 -verify=expected -pedantic %s
+ RUN: %clang_cc1 -std=c2x -verify=expected -pedantic %s
+ */
+
+/* WG14 DR502:
+ * Flexible array member in an anonymous struct
+ */
+void dr502(void) {
+ /* This is EXAMPLE 3 from 6.7.2.1 and is intended to show that a flexible
+ * array member can be used when the only other members of the class are from
+ * an anonymous structure member.
+ */
+ struct s {
+ struct { int i; };
+ int a[]; /* c89only-warning {{flexible array members are a C99 feature}} */
+ };
+
+ /* This is a slightly modified example that looks to see whether the
+ * anonymous structure itself can provide a flexible array member for the
+ * containing class.
+ *
+ * The committee does not think this is valid because it would mean the
+ * anonymous structure would have size 0. Additionally, the anonymous
+ * structure has no additional members and so the flexible array member is
+ * not valid within the anonymous structure.
+ */
+ struct t {
+ int i;
+ struct { int a[]; }; /* expected-error {{flexible array member 'a' not allowed in otherwise empty struct}}
+ c89only-warning {{flexible array members are a C99 feature}}
+ expected-warning {{'' may not be nested in a struct due to flexible array member}}
+ */
+ };
+}
+
diff --git a/clang/www/c_dr_status.html b/clang/www/c_dr_status.html
index 47a7a036a506..ea3859771736 100644
--- a/clang/www/c_dr_status.html
+++ b/clang/www/c_dr_status.html
@@ -2632,7 +2632,7 @@ <h2 id="cdr">C defect report implementation status</h2>
<td><a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2148.htm#dr_496">496</a></td>
<td>NAD</td>
<td>offsetof questions</td>
- <td class="unknown" align="center">Unknown</td>
+ <td class="full" align="center">Yes</td>
</tr>
<tr id="497">
<td><a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2148.htm#dr_497">497</a></td>
@@ -2650,7 +2650,7 @@ <h2 id="cdr">C defect report implementation status</h2>
<td><a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2396.htm#dr_499">499</a></td>
<td>C17</td>
<td>Anonymous structure in union behavior</td>
- <td class="unknown" align="center">Unknown</td>
+ <td class="full" align="center">Yes</td>
</tr>
<tr id="500">
<td><a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2396.htm#dr_500">500</a></td>
@@ -2668,13 +2668,13 @@ <h2 id="cdr">C defect report implementation status</h2>
<td><a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2396.htm#dr_502">502</a></td>
<td>NAD</td>
<td>Flexible array member in an anonymous struct</td>
- <td class="unknown" align="center">Unknown</td>
+ <td class="full" align="center">Yes</td>
</tr>
<tr id="503">
<td><a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2396.htm#dr_503">503</a></td>
<td>NAD</td>
<td>Hexadecimal floating-point and strtod</td>
- <td class="unknown" align="center">Unknown</td>
+ <td class="na" align="center">N/A</td>
</tr>
More information about the cfe-commits
mailing list