]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c: Add more C2y tests of initializer constraints
authorJoseph Myers <josmyers@redhat.com>
Thu, 2 Oct 2025 18:06:03 +0000 (18:06 +0000)
committerJoseph Myers <josmyers@redhat.com>
Thu, 2 Oct 2025 18:06:03 +0000 (18:06 +0000)
Add further tests of C2y requirements on initializers that became
constraints (so requiring diagnostics rather than violations being
undefined behavior) in N3346.

(Some of the tests for invalid string initializers might not cover all
theoretically possible cases, e.g. where char is 64-bit, but should
work on all currently supported targets.)

Tested for x86_64-pc-linux-gnu.

* gcc.dg/c2y-init-2.c, gcc.dg/c2y-init-3.c: New tests.

gcc/testsuite/gcc.dg/c2y-init-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/c2y-init-3.c [new file with mode: 0644]

diff --git a/gcc/testsuite/gcc.dg/c2y-init-2.c b/gcc/testsuite/gcc.dg/c2y-init-2.c
new file mode 100644 (file)
index 0000000..cf62eaa
--- /dev/null
@@ -0,0 +1,33 @@
+/* Test invalid initializers that are consistent with the syntax: undefined
+   behavior ("shall" in Semantics not Constraints) before C2y, constraint
+   violation in C2y.  Structure and union cases.  */
+/* { dg-do compile } */
+/* { dg-options "-std=c2y -pedantic-errors" } */
+
+struct s1 { int a, b; };
+struct s2 { struct s1 x; };
+struct s3 { struct s2 x; };
+union u1 { int a; };
+union u2 { union u1 x; };
+union u3 { union u2 x; };
+
+struct s1 s1v;
+volatile struct s2 s2v;
+union u1 u1v;
+const union u2 u2v;
+
+void
+f ()
+{
+  struct s1 ts1a = {}, ts1b = s1v, ts1c = { 1, 2 };
+  const struct s2 ts2a = {}, ts2b = s2v, ts2c = { s1v }, ts2d = { 1 };
+  volatile struct s3 ts3a = { s2v }, ts3b = { s1v };
+  union u1 tu1a = {}, tu1b = u1v, tu1c = { 1 };
+  const union u2 tu2a = {}, tu2b = u2v, tu2c = { u1v }, tu2d = { 1 };
+  volatile union u3 tu3a = { u2v }, tu3b = { u1v };
+  struct s2 es2a = 1; /* { dg-error "invalid initializer" } */
+  struct s2 es2b = s1v; /* { dg-error "invalid initializer" } */
+  struct s1 es1a = s2v; /* { dg-error "invalid initializer" } */
+  union u2 eu2a = u1v; /* { dg-error "invalid initializer" } */
+  union u1 eu1a = 1; /* { dg-error "invalid initializer" } */
+}
diff --git a/gcc/testsuite/gcc.dg/c2y-init-3.c b/gcc/testsuite/gcc.dg/c2y-init-3.c
new file mode 100644 (file)
index 0000000..1dd0607
--- /dev/null
@@ -0,0 +1,106 @@
+/* Test invalid initializers that are consistent with the syntax: undefined
+   behavior ("shall" in Semantics not Constraints) before C2y, constraint
+   violation in C2y.  Array cases.  */
+/* { dg-do compile } */
+/* { dg-options "-std=c2y -pedantic-errors" } */
+
+typedef __WCHAR_TYPE__ wchar_t;
+typedef __CHAR8_TYPE__ char8_t;
+typedef __CHAR16_TYPE__ char16_t;
+typedef __CHAR32_TYPE__ char32_t;
+
+const char c1[] = "", c2[] = { "" }, c3[] = { "", };
+char c4[] = u8"", c5[] = { u8"" }, c6[] = { u8"", };
+
+signed char sc1[] = "", sc2[] = { "" }, sc3[] = { "", };
+volatile signed char sc4[] = u8"", sc5[] = { u8"" }, sc6[] = { u8"", };
+
+unsigned char uc1[] = "", uc2[] = { "" }, uc3[] = { "", };
+unsigned char uc4[] = u8"", uc5[] = { u8"" }, uc6[] = { u8"", };
+
+char8_t c8_1[] = "", c8_2[] = { "" }, c8_3[] = { "", };
+char8_t c8_4[] = u8"", c8_5[] = { u8"" }, c8_6[] = { u8"", };
+
+wchar_t w1[] = L"", w2[] = { L"" }, w3[] = { L"", };
+char16_t c16_1[] = u"", c16_2[] = { u"" }, c16_3[] = { u"", };
+char32_t c32_1[] = U"", c32_2[] = { U"" }, c32_3[] = { U"", };
+
+int ia[] = { 1, 2, 3 };
+
+_Atomic char ac[] = ""; /* { dg-error "inappropriate type" } */
+_Atomic wchar_t aw[] = L""; /* { dg-error "inappropriate type" } */
+_Atomic char8_t ac8[] = u8""; /* { dg-error "inappropriate type" } */
+_Atomic char16_t ac16[] = u""; /* { dg-error "inappropriate type" } */
+_Atomic char32_t ac32[] = U""; /* { dg-error "inappropriate type" } */
+
+#if __WCHAR_WIDTH__ > __SCHAR_WIDTH__
+typedef char char_not_wchar;
+typedef wchar_t wchar_not_char;
+#else
+typedef long long int char_not_wchar;
+typedef long long int wchar_not_char;
+#endif
+char_not_wchar cnw[] = L""; /* { dg-error "cannot initialize|inappropriate type" } */
+char_not_wchar cnwb[] = { L"" }; /* { dg-error "cannot initialize|inappropriate type" } */
+wchar_not_char wnc[] = ""; /* { dg-error "cannot initialize|inappropriate type" } */
+wchar_not_char wncb[] = { "" }; /* { dg-error "cannot initialize|inappropriate type" } */
+wchar_not_char wnc8[] = u8""; /* { dg-error "cannot initialize|inappropriate type" } */
+wchar_not_char wnc8b[] = { u8"" }; /* { dg-error "cannot initialize|inappropriate type" } */
+
+#if __INT_LEAST16_WIDTH__ > __SCHAR_WIDTH__
+typedef char char_not_char16;
+typedef char16_t char16_not_char;
+#else
+typedef long long int char_not_char16;
+typedef long long int char16_not_char;
+#endif
+char_not_char16 cn16[] = u""; /* { dg-error "cannot initialize|inappropriate type" } */
+char_not_char16 cn16b[] = { u"" }; /* { dg-error "cannot initialize|inappropriate type" } */
+char16_not_char c16nc[] = ""; /* { dg-error "cannot initialize|inappropriate type" } */
+char16_not_char c16ncb[] = { "" }; /* { dg-error "cannot initialize|inappropriate type" } */
+char16_not_char c16nc8[] = u8""; /* { dg-error "cannot initialize|inappropriate type" } */
+char16_not_char c16nc8b[] = { u8"" }; /* { dg-error "cannot initialize|inappropriate type" } */
+
+#if __INT_LEAST32_WIDTH__ > __SCHAR_WIDTH__
+typedef char char_not_char32;
+typedef char32_t char32_not_char;
+#else
+typedef long long int char_not_char32;
+typedef long long int char32_not_char;
+#endif
+char_not_char32 cn32[] = U""; /* { dg-error "cannot initialize|inappropriate type" } */
+char_not_char32 cn32b[] = { U"" }; /* { dg-error "cannot initialize|inappropriate type" } */
+char32_not_char c32nc[] = ""; /* { dg-error "cannot initialize|inappropriate type" } */
+char32_not_char c32ncb[] = { "" }; /* { dg-error "cannot initialize|inappropriate type" } */
+char32_not_char c32nc8[] = u8""; /* { dg-error "cannot initialize|inappropriate type" } */
+char32_not_char c32nc8b[] = { u8"" }; /* { dg-error "cannot initialize|inappropriate type" } */
+
+#if __WCHAR_WIDTH__ == __INT_LEAST16_WIDTH__
+typedef long long int wchar_not_char16;
+typedef long long int char16_not_wchar;
+#else
+typedef wchar_t wchar_not_char16;
+typedef char16_t char16_not_wchar;
+#endif
+wchar_not_char16 wcn16[] = u""; /* { dg-error "cannot initialize|inappropriate type" } */
+wchar_not_char16 wcn16b[] = { u"" }; /* { dg-error "cannot initialize|inappropriate type" } */
+char16_not_wchar c16nwc[] = L""; /* { dg-error "cannot initialize|inappropriate type" } */
+char16_not_wchar c16nwcb[] = { L"" }; /* { dg-error "cannot initialize|inappropriate type" } */
+
+#if __WCHAR_WIDTH__ == __INT_LEAST32_WIDTH__
+typedef long long int wchar_not_char32;
+typedef long long int char32_not_wchar;
+#else
+typedef wchar_t wchar_not_char32;
+typedef char32_t char32_not_wchar;
+#endif
+wchar_not_char32 wcn32[] = U""; /* { dg-error "cannot initialize|inappropriate type" } */
+wchar_not_char32 wcn32b[] = { U"" }; /* { dg-error "cannot initialize|inappropriate type" } */
+char32_not_wchar c32nwc[] = L""; /* { dg-error "cannot initialize|inappropriate type" } */
+char32_not_wchar c32nwcb[] = { L"" }; /* { dg-error "cannot initialize|inappropriate type" } */
+
+void
+f ()
+{
+  int ic[] = ia; /* { dg-error "invalid initializer" } */
+}