From d7e06d5675b62b5d3d89e6d6210c34b74a1a8356 Mon Sep 17 00:00:00 2001 From: Aaron Ballman Date: Wed, 17 Aug 2022 08:11:07 -0400 Subject: [PATCH] Update the status of some more C99 features This also adds some test coverage to demonstrate we implement what was standardized. --- clang/test/C/C99/n617.c | 24 ++++++++++ clang/test/C/C99/n629.c | 116 ++++++++++++++++++++++++++++++++++++++++++++++++ clang/test/C/C99/n636.c | 12 +++++ clang/test/C/C99/n736.c | 47 ++++++++++++++++++++ clang/www/c_status.html | 8 ++-- 5 files changed, 203 insertions(+), 4 deletions(-) create mode 100644 clang/test/C/C99/n617.c create mode 100644 clang/test/C/C99/n629.c create mode 100644 clang/test/C/C99/n636.c create mode 100644 clang/test/C/C99/n736.c diff --git a/clang/test/C/C99/n617.c b/clang/test/C/C99/n617.c new file mode 100644 index 0000000..be263cc --- /dev/null +++ b/clang/test/C/C99/n617.c @@ -0,0 +1,24 @@ +// RUN: %clang_cc1 -verify %s + +// expected-no-diagnostics + +/* WG14 N617: yes + * reliable integer division + */ +void test(void) { + _Static_assert(59 / 4 == 14, "we didn't truncate properly"); + _Static_assert(59 / -4 == -14, "we didn't truncate properly"); + _Static_assert(-59 / 4 == -14, "we didn't truncate properly"); + _Static_assert(-59 / -4 == 14, "we didn't truncate properly"); + + // Might as well test % for the quotient. + _Static_assert(59 % 4 == 3, "we didn't truncate properly"); + _Static_assert(59 % -4 == 3, "we didn't truncate properly"); + _Static_assert(-59 % 4 == -3, "we didn't truncate properly"); + _Static_assert(-59 % -4 == -3, "we didn't truncate properly"); + + // Test the idiom for rounding up. + _Static_assert((59 + (4 - 1)) / 4 == 15, "failed to 'round up' with the usual idiom"); + _Static_assert((59 + (4 - 1)) % 4 == 2, "failed to 'round up' with the usual idiom"); +} + diff --git a/clang/test/C/C99/n629.c b/clang/test/C/C99/n629.c new file mode 100644 index 0000000..01e32fb --- /dev/null +++ b/clang/test/C/C99/n629.c @@ -0,0 +1,116 @@ +// RUN: %clang_cc1 -triple x86_64-unknown-linux -verify %s +// RUN: %clang_cc1 -triple i686-unknown-linux -verify %s +// RUN: %clang_cc1 -triple x86_64-unknown-win32 -verify %s +// RUN: %clang_cc1 -triple i686-unknown-win32 -verify %s + +/* WG14 N629: yes + * integer constant type rules + */ + +// expected-no-diagnostics + +void test_decimal_constants(void) { + // Easy cases where the value fits into the type you'd expect. + (void)_Generic(2, int : 1); + (void)_Generic(2u, unsigned int : 1); + (void)_Generic(2l, long : 1); + (void)_Generic(2ul, unsigned long : 1); + (void)_Generic(2ll, long long : 1); + (void)_Generic(2ull, unsigned long long : 1); + +#if __INT_WIDTH__ == 16 + #if __LONG_WIDTH__ > 16 + (void)_Generic(65536, long : 1); + (void)_Generic(65536U, unsigned long : 1); + #else + (void)_Generic(65536, long long : 1); + (void)_Generic(65536U, unsigned long : 1); + #endif // __LONG_WIDTH__ > 16 +#elif __INT_WIDTH__ == 32 + #if __LONG_WIDTH__ > 32 + (void)_Generic(4294967296, long : 1); + (void)_Generic(4294967296U, unsigned long : 1); + #else + (void)_Generic(4294967296, long long : 1); + (void)_Generic(4294967296U, unsigned long long : 1); + #endif // __LONG_WIDTH__ > 32 +#endif + +#if __LONG_WIDTH__ > 32 + (void)_Generic(4294967296L, long : 1); + (void)_Generic(4294967296U, unsigned long : 1); +#else + (void)_Generic(4294967296L, long long : 1); + (void)_Generic(4294967296U, unsigned long long : 1); +#endif +} + +void test_octal_constants(void) { + (void)_Generic(02, int : 1); + (void)_Generic(02u, unsigned int : 1); + (void)_Generic(02l, long : 1); + (void)_Generic(02ul, unsigned long : 1); + (void)_Generic(02ll, long long : 1); + (void)_Generic(02ull, unsigned long long : 1); + +#if __INT_WIDTH__ == 16 + #if __LONG_WIDTH__ > 16 + (void)_Generic(0200000, long : 1); + (void)_Generic(0200000U, unsigned long : 1); + #else + (void)_Generic(0200000, long long : 1); + (void)_Generic(0200000U, unsigned long : 1); + #endif // __LONG_WIDTH__ > 16 +#elif __INT_WIDTH__ == 32 + #if __LONG_WIDTH__ > 32 + (void)_Generic(040000000000, long : 1); + (void)_Generic(040000000000U, unsigned long : 1); + #else + (void)_Generic(040000000000, long long : 1); + (void)_Generic(040000000000U, unsigned long long : 1); + #endif // __LONG_WIDTH__ > 32 +#endif + +#if __LONG_WIDTH__ > 32 + (void)_Generic(040000000000L, long : 1); + (void)_Generic(040000000000U, unsigned long : 1); +#else + (void)_Generic(040000000000L, long long : 1); + (void)_Generic(040000000000U, unsigned long long : 1); +#endif +} + +void test_hexadecimal_constants(void) { + (void)_Generic(0x2, int : 1); + (void)_Generic(0x2u, unsigned int : 1); + (void)_Generic(0x2l, long : 1); + (void)_Generic(0x2ul, unsigned long : 1); + (void)_Generic(0x2ll, long long : 1); + (void)_Generic(0x2ull, unsigned long long : 1); + +#if __INT_WIDTH__ == 16 + #if __LONG_WIDTH__ > 16 + (void)_Generic(0x10000, long : 1); + (void)_Generic(0x10000U, unsigned long : 1); + #else + (void)_Generic(0x10000, long long : 1); + (void)_Generic(0x10000U, unsigned long : 1); + #endif // __LONG_WIDTH__ > 16 +#elif __INT_WIDTH__ == 32 + #if __LONG_WIDTH__ > 32 + (void)_Generic(0x100000000, long : 1); + (void)_Generic(0x100000000U, unsigned long : 1); + #else + (void)_Generic(0x100000000, long long : 1); + (void)_Generic(0x100000000U, unsigned long long : 1); + #endif // __LONG_WIDTH__ > 32 +#endif + +#if __LONG_WIDTH__ > 32 + (void)_Generic(0x100000000L, long : 1); + (void)_Generic(0x100000000U, unsigned long : 1); +#else + (void)_Generic(0x100000000L, long long : 1); + (void)_Generic(0x100000000U, unsigned long long : 1); +#endif +} diff --git a/clang/test/C/C99/n636.c b/clang/test/C/C99/n636.c new file mode 100644 index 0000000..fb69b28 --- /dev/null +++ b/clang/test/C/C99/n636.c @@ -0,0 +1,12 @@ +// RUN: %clang_cc1 -verify %s +// RUN: %clang_cc1 -verify=c2x -std=c2x %s + +/* WG14 N636: yes + * remove implicit function declaration + */ + +void test(void) { + frobble(); // expected-error {{call to undeclared function 'frobble'; ISO C99 and later do not support implicit function declarations}} \ + c2x-error {{undeclared identifier 'frobble'}} +} + diff --git a/clang/test/C/C99/n736.c b/clang/test/C/C99/n736.c new file mode 100644 index 0000000..df912d2 --- /dev/null +++ b/clang/test/C/C99/n736.c @@ -0,0 +1,47 @@ +// RUN: %clang_cc1 -verify %s + +/* WG14 N736: yes + * preprocessor arithmetic done in intmax_t/uintmax_t + */ + +// There is not a standard requirement that this relationships holds. If these +// asserts fail, it means we have another test scenario to consider. +_Static_assert(__INTMAX_MAX__ == __LONG_LONG_MAX__, + "intmax_t is not the same width as long long?"); +_Static_assert((-__INTMAX_MAX__ - 1) == (-__LONG_LONG_MAX__ - 1LL), + "intmax_t is not the same width as long long?"); +_Static_assert(__UINTMAX_MAX__ == (__LONG_LONG_MAX__ * 2ULL + 1ULL), + "uintmax_t is not the same width as unsigned long long?"); + +// Test that arithmetic on the largest positive signed intmax_t works. +#if 9223372036854775807LL + 0LL != 9223372036854775807LL +#error "uh oh" +#endif + +// Same for negative. +#if -9223372036854775807LL - 1LL + 0LL != -9223372036854775807LL - 1LL +#error "uh oh" +#endif + +// Then test the same for unsigned +#if 18446744073709551615ULL + 0ULL != 18446744073709551615ULL +#error "uh oh" +#endif + +// Test that unsigned overflow causes silent wraparound. +#if 18446744073709551615ULL + 1ULL != 0 // Silently wraps to 0. +#error "uh oh" +#endif + +#if 0ULL - 1ULL != 18446744073709551615ULL // Silently wraps to 0xFFFF'FFFF'FFFF'FFFF. +#error "uh oh" +#endif + +// Now test that signed arithmetic that pushes us over a limit is properly +// diagnosed. +#if 9223372036854775807LL + 1LL // expected-warning {{integer overflow in preprocessor expression}} +#endif + +#if -9223372036854775807LL - 2LL // expected-warning {{integer overflow in preprocessor expression}} +#endif + diff --git a/clang/www/c_status.html b/clang/www/c_status.html index 5050947..8d4dbe2 100644 --- a/clang/www/c_status.html +++ b/clang/www/c_status.html @@ -198,7 +198,7 @@ conformance.

reliable integer division N617 - Unknown + Yes universal character names (\u and \U) @@ -245,12 +245,12 @@ conformance.

remove implicit function declaration N636 - Unknown + Yes preprocessor arithmetic done in intmax_t/uintmax_t N736 - Unknown + Yes mixed declarations and code @@ -265,7 +265,7 @@ conformance.

integer constant type rules N629 - Unknown + Yes integer promotion rules -- 2.7.4