130 lines · c
1// RUN: %clang_cc1 -std=c11 -Wgnu-folding-constant -fsyntax-only -verify %s2// RUN: %clang_cc1 -fms-compatibility -Wgnu-folding-constant -DMS -fsyntax-only -verify=expected,ms %s3// RUN: %clang_cc1 -std=c99 -fms-compatibility -Wgnu-folding-constant -DMS -fsyntax-only -verify=expected,ms %s4// RUN: %clang_cc1 -std=c99 -pedantic -Wgnu-folding-constant -fsyntax-only -verify=expected,ext %s5// RUN: %clang_cc1 -xc++ -std=c++11 -pedantic -fsyntax-only -verify=expected,ext,cxx %s6 7_Static_assert("foo", "string is nonzero"); // ext-warning {{'_Static_assert' is a C11 extension}}8#ifndef __cplusplus9// expected-warning@-2 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}10#endif11 12_Static_assert(1, "1 is nonzero"); // ext-warning {{'_Static_assert' is a C11 extension}}13_Static_assert(0, "0 is nonzero"); // expected-error {{static assertion failed: 0 is nonzero}} \14 // ext-warning {{'_Static_assert' is a C11 extension}}15 16#ifdef MS17static_assert(1, "1 is nonzero");18#endif19 20void foo(void) {21 _Static_assert(1, "1 is nonzero"); // ext-warning {{'_Static_assert' is a C11 extension}}22 _Static_assert(0, "0 is nonzero"); // expected-error {{static assertion failed: 0 is nonzero}} \23 // ext-warning {{'_Static_assert' is a C11 extension}}24#ifdef MS25 static_assert(1, "1 is nonzero");26#endif27}28 29_Static_assert(1, invalid); // ext-warning {{'_Static_assert' is a C11 extension}}30#ifndef __cplusplus31// expected-error@-2 {{expected string literal for diagnostic message in static_assert}}32#endif33// cxx-error@-4 {{use of undeclared identifier 'invalid'}}34// cxx-warning@-5 {{'static_assert' with a user-generated message is a C++26 extension}}35 36struct A {37 int a;38 _Static_assert(1, "1 is nonzero"); // ext-warning {{'_Static_assert' is a C11 extension}}39 _Static_assert(0, "0 is nonzero"); // expected-error {{static assertion failed: 0 is nonzero}} \40 // ext-warning {{'_Static_assert' is a C11 extension}}41#ifdef MS42 static_assert(1, "1 is nonzero");43#endif44};45 46#ifdef __cplusplus47#define ASSERT_IS_TYPE(T) __is_same(T, T)48#else49#define ASSERT_IS_TYPE(T) __builtin_types_compatible_p(T, T)50#endif51 52#define UNION(T1, T2) union { \53 __typeof__(T1) one; \54 __typeof__(T2) two; \55 _Static_assert(ASSERT_IS_TYPE(T1), "T1 is not a type"); \56 _Static_assert(ASSERT_IS_TYPE(T2), "T2 is not a type"); \57 _Static_assert(sizeof(T1) == sizeof(T2), "type size mismatch"); \58 }59 60typedef UNION(unsigned, struct A) U1; // ext-warning 3 {{'_Static_assert' is a C11 extension}}61UNION(char[2], short) u2 = { .one = { 'a', 'b' } }; // ext-warning 3 {{'_Static_assert' is a C11 extension}} cxx-warning {{designated initializers are a C++20 extension}}62typedef UNION(char, short) U3; // expected-error {{static assertion failed due to requirement 'sizeof(char) == sizeof(short)': type size mismatch}} \63 // expected-note{{evaluates to '1 == 2'}} \64 // ext-warning 3 {{'_Static_assert' is a C11 extension}}65typedef UNION(float, 0.5f) U4; // expected-error {{expected a type}} \66 // ext-warning 3 {{'_Static_assert' is a C11 extension}}67 68// MSVC accepts static_assert in all language modes without including <assert.h>69// and so do we in ms-compatibility mode. Unfortunately, there is no good way70// to diagnose that with a pedantic warning. We'd have to track inclusion of71// <assert.h> which is difficult when modules and PCH are involved. Adding72// implicit definition of the macro causes unexpected results in c99 mode.73#ifdef MS74 75#if __STDC_VERSION__ < 201112L76#if defined(static_assert)77static_assert(0, "0 is nonzero"); // ok because we should not define static_assert78 // macro in c99.79#endif80 81static_assert(0, "0 is nonzero"); // ms-error {{static assertion failed: 0 is nonzero}}82#endif83 84#define assert(expr)85static_assert(1, "1 is nonzero"); // ok86 87#undef static_assert88static_assert(1, "1 is nonzero"); // yes, still ok.89#endif90 91_Static_assert(1 , "") // expected-error {{expected ';' after '_Static_assert'}} \92 // ext-warning {{'_Static_assert' is a C11 extension}}93 94static int static_var;95_Static_assert(&static_var != 0, ""); // ext-warning {{'_Static_assert' is a C11 extension}} \96 // expected-warning {{comparison of address of 'static_var' not equal to a null pointer is always true}}97_Static_assert("" != 0, ""); // ext-warning {{'_Static_assert' is a C11 extension}}98_Static_assert(("" != 0), ""); // ext-warning {{'_Static_assert' is a C11 extension}}99_Static_assert(*"1", ""); // ext-warning {{'_Static_assert' is a C11 extension}}100_Static_assert("1"[0], ""); // ext-warning {{'_Static_assert' is a C11 extension}}101_Static_assert(1.0 != 0, ""); // ext-warning {{'_Static_assert' is a C11 extension}}102_Static_assert(__builtin_strlen("1"), ""); // ext-warning {{'_Static_assert' is a C11 extension}}103#ifndef __cplusplus104// expected-warning@-9 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}105// expected-warning@-8 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}106// expected-warning@-8 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}107// expected-warning@-8 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}108// expected-warning@-8 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}109// expected-warning@-8 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}110// __builtin_strlen(literal) is considered an integer constant expression111// and doesn't cause a pedantic warning112#endif113 114 115_Static_assert(0, L"\xFFFFFFFF"); // expected-warning {{encoding prefix 'L' on an unevaluated string literal has no effect}} \116 // expected-error {{invalid escape sequence '\xFFFFFFFF' in an unevaluated string literal}} \117 // expected-error {{hex escape sequence out of range}} \118 // ext-warning {{'_Static_assert' is a C11 extension}}119_Static_assert(0, L"\u1234"); // expected-warning {{encoding prefix 'L' on an unevaluated string literal has no effect}} \120 // expected-error {{static assertion failed: ሴ}} \121 // ext-warning {{'_Static_assert' is a C11 extension}}122 123_Static_assert(0, L"\x1ff" // expected-warning {{encoding prefix 'L' on an unevaluated string literal has no effect}} \124 // expected-error {{hex escape sequence out of range}} \125 // expected-error {{invalid escape sequence '\x1ff' in an unevaluated string literal}} \126 // ext-warning {{'_Static_assert' is a C11 extension}}127 "0\x123" // expected-error {{invalid escape sequence '\x123' in an unevaluated string literal}}128 "fx\xfffff" // expected-error {{invalid escape sequence '\xfffff' in an unevaluated string literal}}129 "goop");130