brintos

brintos / llvm-project-archived public Read only

0
0
Text · 18.7 KiB · 77ade3c Raw
537 lines · c
1/* RUN: %clang_cc1 -std=c89 -fsyntax-only -verify=expected,c89only,c17andearlier -pedantic -Wno-declaration-after-statement -Wno-c11-extensions %s2   RUN: %clang_cc1 -std=c89 -fsyntax-only -verify=expected,c89only,c17andearlier -pedantic -Wno-declaration-after-statement -Wno-c11-extensions -fno-signed-char %s3   RUN: %clang_cc1 -std=c99 -fsyntax-only -verify=expected,c99untilc2x,c17andearlier -pedantic -Wno-c11-extensions %s4   RUN: %clang_cc1 -std=c11 -fsyntax-only -verify=expected,c99untilc2x,c17andearlier -pedantic %s5   RUN: %clang_cc1 -std=c17 -fsyntax-only -verify=expected,c99untilc2x,c17andearlier -pedantic %s6   RUN: %clang_cc1 -std=c23 -fsyntax-only -verify=expected,c2xandup -pedantic %s7 */8 9/* The following are DRs which do not require tests to demonstrate10 * conformance or nonconformance.11 *12 * WG14 DR001: yes13 * Do functions return values by copying?14 *15 * WG14 DR005: yes16 * May a conforming implementation define and recognize a pragma which would17 * change the semantics of the language?18 *19 * WG14 DR008: yes20 * Can a conforming C compiler to perform dead-store elimination?21 *22 * WG14 DR020: yes23 * Is a compiler which allows the Relaxed Ref/Def linkage model to be24 * considered a conforming compiler?25 *26 * WG14 DR025: yes27 * What is meant by 'representable floating-point value?'28 *29 * WG14 DR026: yes30 * Can a strictly conforming program contain a string literal with '$' or '@'?31 *32 * WG14 DR033: yes33 * Conformance questions around 'shall' violations outside of constraints34 * sections35 *36 * WG14 DR036: yes37 * May floating-point constants be represented with more precision than implied38 * by its type?39 *40 * WG14 DR037: yes41 * Questions about multibyte characters and Unicode42 *43 * WG14 DR051: yes44 * Question on pointer arithmetic45 *46 * WG14 DR052: yes47 * Editorial corrections48 *49 * WG14 DR056: yes50 * Floating-point representation precision requirements51 *52 * WG14 DR057: yes53 * Is there an integral type for every pointer?54 *55 * WG14 DR059: yes56 * Do types have to be completed?57 *58 * WG14 DR063: dup 05659 * Floating-point representation precision requirements60 *61 * WG14 DR067: yes62 * Integer and integral type confusion63 *64 * WG14 DR069: yes65 * Questions about the representation of integer types66 *67 * WG14 DR077: yes68 * Stability of addresses69 *70 * WG14 DR080: yes71 * Merging of string constants72 *73 * WG14 DR085: yes74 * Returning from main75 *76 * WG14 DR087: yes77 * Order of evaluation78 * Note: this DR is covered by C/C11/n1282.c79 *80 * WG14 DR086: yes81 * Object-like macros in system headers82 *83 * WG14 DR091: yes84 * Multibyte encodings85 *86 * WG14 DR092: dup 06087 * Partial initialization of strings88 *89 * WG14 DR093: yes90 * Reservation of identifiers91 */92 93 94/* WG14 DR004: yes95 * Are multiple definitions of unused identifiers with external linkage96 * permitted?97 */98int dr004(void) {return 0;} /* expected-note {{previous definition is here}} */99int dr004(void) {return 1;} /* expected-error {{redefinition of 'dr004'}} */100 101/* WG14 DR007: yes102 * Are declarations of the form struct-or-union identifier ; permitted after103 * the identifier tag has already been declared?104 */105struct dr007_a;106struct dr007_a;107struct dr007_a {int a;};108struct dr007_a;109struct dr007_b {int a;};110struct dr007_b;111 112 113/* WG14 DR009: no114 * Use of typedef names in parameter declarations115 *116 * FIXME: This should be diagnosed as expecting a declaration specifier instead117 * of treated as declaring a parameter of type 'int (*)(dr009_t);'118 */119typedef int dr009_t;120void dr009_f((dr009_t)); /* c99untilc2x-error {{type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int}}121                            c2xandup-error {{a type specifier is required for all declarations}} */122 123/* WG14 DR010:124 * Is a typedef to an incomplete type legal?125 */126typedef int dr010_t[];127dr010_t dr010_a = {1};128dr010_t dr010_b = {1, 2};129int dr010_c = sizeof(dr010_t); /* expected-error {{invalid application of 'sizeof' to an incomplete type 'dr010_t' (aka 'int[]')}} */130 131/* WG14 DR011: yes132 * Merging of declarations for linked identifier133 *134 * Note: more of this DR is tested in dr011.c135 *136 * WG14 DR034: yes137 * External declarations in different scopes138 *139 * Note: DR034 has a question resolved by DR011 and another question where the140 * result is UB.141 */142static int dr011_a[]; /* expected-warning {{tentative array definition assumed to have one element}}143                         expected-warning {{tentative definition of variable with internal linkage has incomplete array type 'int[]'}}144                       */145void dr011(void) {146  extern int i[];147  {148    /* a different declaration of the same object */149    extern int i[10];150    (void)sizeof(i);151    _Static_assert(sizeof(i) == 10 * sizeof(int), "fail");152  }153  (void)sizeof(i); /* expected-error {{invalid application of 'sizeof' to an incomplete type 'int[]'}} */154 155  extern int dr011_a[10];156  (void)sizeof(dr011_a);157  _Static_assert(sizeof(dr011_a) == 10 * sizeof(int), "fail");158 159  extern int j[10];160  {161    extern int j[];162    (void)sizeof(j);163    _Static_assert(sizeof(j) == 10 * sizeof(int), "fail");164  }165}166 167/* WG14 DR012: yes168 * Is it valid to take the address of a dereferenced void pointer?169 */170void dr012(void *p) {171  /* The behavior changed between C89 and C99. */172  (void)&*p; /* c89only-warning {{ISO C forbids taking the address of an expression of type 'void'}}173                c89only-warning {{ISO C does not allow indirection on operand of type 'void *'}} */174}175 176/* WG14 DR013: yes177 * Compatible and composite function types178 */179int dr013(int a[4]);180int dr013(int a[5]);181int dr013(int *a);182 183struct dr013_t {184struct dr013_t *p;185} dr013_v[sizeof(struct dr013_t)];186 187/* WG14 DR015: yes188 * What is the promoted type of a plain int bit-field?189 */190void dr015(void) {191  struct S {192    int small_int_bitfield : 16;193    unsigned int small_uint_bitfield : 16;194    int int_bitfield : 32;195    unsigned int uint_bitfield : 32;196  } s;197  _Static_assert(__builtin_types_compatible_p(__typeof__(+s.small_int_bitfield), int), "fail");198  _Static_assert(__builtin_types_compatible_p(__typeof__(+s.small_uint_bitfield), int), "fail");199  _Static_assert(__builtin_types_compatible_p(__typeof__(+s.int_bitfield), int), "fail");200  _Static_assert(__builtin_types_compatible_p(__typeof__(+s.uint_bitfield), unsigned int), "fail");201}202 203/* WG14 DR027: yes204 * Can there be characters in the character set that are not in the required205 * source character set?206 */207#define THIS$AND$THAT(a, b) ((a) + (b)) /* expected-warning 2 {{'$' in identifier}} */208_Static_assert(THIS$AND$THAT(1, 1) == 2, "fail"); /* expected-warning 2 {{'$' in identifier}} */209 210 211/* WG14 DR029: no212 * Do two types have to have the same tag to be compatible?213 * Note: the rule changed in C99 to be different than the resolution to DR029,214 * so it's not clear there's value in implementing this DR.215 */216_Static_assert(__builtin_types_compatible_p(struct S { int a; }, union U { int a; }), "fail"); /* expected-error {{static assertion failed due to requirement '__builtin_types_compatible_p(struct S, union U)': fail}} */217 218/* WG14 DR031: yes219 * Can constant expressions overflow?220 */221void dr031(int i) {222  switch (i) {223  case __INT_MAX__ + 1: break; /* expected-warning {{overflow in expression; result is -2'147'483'648 with type 'int'}} */224  #pragma clang diagnostic push225  #pragma clang diagnostic ignored "-Wswitch"226  /* Silence the targets which issue:227   * warning: overflow converting case value to switch condition type (2147483649 to 18446744071562067969)228   */229  case __INT_MAX__ + 2ul: break;230  #pragma clang diagnostic pop231  case (__INT_MAX__ * 4) / 4: break; /* expected-warning {{overflow in expression; result is -4 with type 'int'}} */232  }233}234 235/* WG14 DR032: no236 * Must implementations diagnose extensions to the constant evaluation rules?237 *238 * This should issue a diagnostic because a constant-expression is a239 * conditional-expression, which excludes the comma operator.240 */241int dr032 = (1, 2); /* expected-warning {{left operand of comma operator has no effect}} */242 243#if __STDC_VERSION__ < 202311L244/* WG14 DR035: partial245 * Questions about definition of functions without a prototype246 */247void dr035_1(a, b) /* expected-warning {{a function definition without a prototype is deprecated in all versions of C and is not supported in C23}} */248  int a(enum b {x, y}); /* c17andearlier-warning {{declaration of 'enum b' will not be visible outside of this function}} */249  int b; {250  int test = x; /* expected-error {{use of undeclared identifier 'x'}} */251}252 253void dr035_2(c) /* expected-warning {{a function definition without a prototype is deprecated in all versions of C and is not supported in C23}} */254  enum m{q, r} c; { /* c17andearlier-warning {{declaration of 'enum m' will not be visible outside of this function}} */255  /* FIXME: This should be accepted because the scope of m, q, and r ends at256   * the closing brace of the function per C89 6.1.2.1.257   */258  int test = q; /* expected-error {{use of undeclared identifier 'q'}} */259}260#endif /* __STDC_VERSION__ < 202311L */261 262/* WG14 DR038: yes263 * Questions about argument substitution during macro expansion264 */265#define DR038_X 0x000E266#define DR038_Y 0x0100267#define DR038(a) a268_Static_assert(DR038(DR038_X + DR038_Y) == DR038_X + DR038_Y, "fail");269 270/* WG14 DR039: yes271 * Questions about the "C" locale272 */273_Static_assert(sizeof('a') == sizeof(int), "fail");274 275/* WG14 DR040: partial276 * 9 unrelated questions about C89277 *278 * Question 6279 */280struct dr040 { /* expected-note {{definition of 'struct dr040' is not complete until the closing '}'}} */281  char c;282  short s;283  int i[__builtin_offsetof(struct dr040, s)]; /* expected-error {{offsetof of incomplete type 'struct dr040'}} */284};285 286/* WG14 DR043: yes287 * On the definition of the NULL macro288 */289void dr043(void) {290  #include <stddef.h>291  /* NULL has to be an integer constant expression with the value 0, or such an292   * expression cast to void *. If it's an integer constant expression other293   * than the literal 0 (such as #define NULL 4-4), this would fail to compile294   * unless the macro replacement list is properly parenthesized as it would295   * expand to: (void)(void *)4-4;296   */297   (void)(void *)NULL;298 299  /* If the NULL macro is an integer constant expression with the value 0 and300   * it has been cast to void *, ensure that it's also fully parenthesized. If301   * it isn't (such as #define NULL (void *)0), this would fail to compile as302   * would expand to (void *)0->a; which gives a diagnostic about int not being303   * a pointer, instead of((void *)0)->a; which gives a diagnostic about the304   * base reference being void and not a structure.305   */306   NULL->a; /* expected-error {{member reference base type 'void' is not a structure or union}} */307}308 309/* WG14 DR044: yes310 * On the result of the offsetof macro311 */312void dr044(void) {313  #include <stddef.h>314  struct S { int a, b; };315  /* Ensure that the result of offsetof is usable in a constant expression. */316  _Static_assert(offsetof(struct S, b) == sizeof(int), "fail");317}318 319/* WG14 DR046: yes320 * Use of typedef names in parameter declarations321 */322typedef int dr046_t;323int dr046(int dr046_t) { return dr046_t; }324 325/* WG14 DR047: yes326 * Questions about declaration conformance327 */328struct dr047_t; /* expected-note 2 {{forward declaration of 'struct dr047_t'}} */329struct dr047_t *dr047_1(struct dr047_t *p) {return p; }330struct dr047_t *dr047_2(struct dr047_t a[]) {return a; } /* expected-error {{array has incomplete element type 'struct dr047_t'}} */331int *dr047_3(int a2[][]) {return *a2; } /* expected-error {{array has incomplete element type 'int[]'}} */332extern struct dr047_t es1;333extern struct dr047_t es2[1]; /* expected-error {{array has incomplete element type 'struct dr047_t'}} */334 335/* WG14 DR050: yes336 * Do wide string literals implicitly include <stddef.h>?337 */338void dr050(void) {339  /* The NULL macro is previously defined because we include <stddef.h> for340   * other tests. Undefine the macro to demonstrate that use of a wide string341   * literal doesn't magically include the header file.342   */343  #undef NULL344  (void)L"huttah!";345  (void)NULL; /* expected-error {{use of undeclared identifier 'NULL'}} */346}347 348#if __STDC_VERSION__ < 202311L349/* WG14 DR053: yes350 * Accessing a pointer to a function with a prototype through a pointer to351 * pointer to function without a prototype352 */353void dr053(void) {354  int f(int);355  int (*fp1)(int);356  int (*fp2)();  /* expected-warning {{a function declaration without a prototype is deprecated in all versions of C}} */357  int (**fpp)(); /* expected-warning {{a function declaration without a prototype is deprecated in all versions of C}} */358 359  fp1 = f;360  fp2 = fp1;361  (*fp2)(3);  /* expected-warning {{passing arguments to a function without a prototype is deprecated in all versions of C and is not supported in C23}} */362  fpp = &fp1;363  (**fpp)(3); /* expected-warning {{passing arguments to a function without a prototype is deprecated in all versions of C and is not supported in C23}} */364}365#endif /* __STDC_VERSION__ < 202311L */366 367/* WG14 DR064: yes368 * Null pointer constants369 */370char *dr064_1(int i, int *pi) {371  *pi = i;372  return 0;373}374 375char *dr064_2(int i, int *pi) {376  return (*pi = i, 0); /* expected-error {{incompatible integer to pointer conversion returning 'int' from a function with result type 'char *'}} */377}378 379/* WG14 DR068: yes380 * 'char' and signed vs unsigned integer types381 */382void dr068(void) {383  #include <limits.h>384 385#if CHAR_MAX == SCHAR_MAX386  /* char is signed */387  _Static_assert('\xFF' == -1, "fail");388#else389  /* char is unsigned */390  _Static_assert('\xFF' == 0xFF, "fail");391#endif392}393 394#if __STDC_VERSION__ < 202311L395/* WG14: DR070: yes396 * Interchangeability of function arguments397 *398 * Note: we could issue a pedantic warning in this case. We are claiming399 * conformance not because we diagnose the UB when we could but because we're400 * not obligated to do anything about it and we make it "just work" via the401 * usual conversion rules.402 *403 * This behavior is specific to functions without prototypes. A function with404 * a prototype causes implicit conversions rather than relying on default405 * argument promotion and warm thoughts.406 */407void dr070_1(c) /* expected-warning {{a function definition without a prototype is deprecated in all versions of C and is not supported in C23}} */408  int c; {409}410 411void dr070_2(void) {412  dr070_1(6);413  dr070_1(6U); /* Pedantically UB */414}415#endif /* __STDC_VERSION__ < 202311L */416 417/* WG14 DR071: yes418 * Enumerated types419 */420enum dr071_t { foo_A = 0, foo_B = 1, foo_C = 8 };421void dr071(void) {422  /* Test that in-range values not present in the enumeration still round-trip423   * to the original value.424   */425  _Static_assert(100 == (int)(enum dr071_t)100, "fail");426}427 428/* WG14 DR081: yes429 * Left shift operator430 */431void dr081(void) {432  /* Demonstrate that we don't crash when left shifting a signed value; that's433   * implementation defined behavior.434   */435 _Static_assert(-1 << 1 == -2, "fail"); /* expected-warning {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}436                                           expected-note {{left shift of negative value -1}} */437 _Static_assert(1 << 3 == 1u << 3u, "fail"); /* Shift of a positive signed value does sensible things. */438}439 440/* WG14 DR084: yes441 * Incomplete type in function declaration442 *443 * Note: because the situation is UB, we're free to do what we want. We elect444 * to accept and require the incomplete type to be completed before the445 * function definition.446 */447struct dr084_t; /* expected-note {{forward declaration of 'struct dr084_t'}} */448extern void (*dr084_1)(struct dr084_t);449void dr084_2(struct dr084_t);450void dr084_2(struct dr084_t val) {} /* expected-error {{variable has incomplete type 'struct dr084_t'}} */451 452/* WG14 DR088: yes453 * Compatibility of incomplete types454 */455struct dr088_t_1;456 457void dr088_f(struct dr088_t_1 *); /* expected-note {{passing argument to parameter here}} */458void dr088_1(void) {459  /* Distinct type from the file scope forward declaration. */460  struct dr088_t_1;461  /* FIXME: this diagnostic could be improved to not be utterly baffling. */462  dr088_f((struct dr088_t_1 *)0); /* expected-error {{incompatible pointer types passing 'struct dr088_t_1 *' to parameter of type 'struct dr088_t_1 *'}} */463}464 465void dr088_2(struct dr088_t_1 *p) { /* Pointer to incomplete type. */ }466struct dr088_t_1 { int i; }; /* Type is completed. */467void dr088_3(struct dr088_t_1 s) {468  /* When passing a pointer to the completed type, is it the same type as the469   * incomplete type used in the call declaration?470   */471  dr088_2(&s);472}473 474/* WG14 DR089: yes475 * Multiple definitions of macros476 */477#define DR089 object_like             /* expected-note {{previous definition is here}} */478#define DR089(argument) function_like /* expected-warning {{'DR089' macro redefined}} */479 480/* WG14 DR095: yes481 * Is initialization as constrained as assignment?482 */483void dr095(void) {484  /* Ensure that type compatibility constraints on assignment are also honored485   * for initializations.486   */487  struct One {488    int a;489  } one;490  struct Two {491    float f;492  } two = one; /* expected-error {{initializing 'struct Two' with an expression of incompatible type 'struct One'}} */493 494  two = one; /* expected-error {{assigning to 'struct Two' from incompatible type 'struct One'}} */495}496 497/* WG14 DR096: yes498 * Arrays of incomplete types499 */500void dr096(void) {501  typedef void func_type(void);502  func_type array_funcs[10]; /* expected-error {{'array_funcs' declared as array of functions of type 'func_type' (aka 'void (void)')}} */503 504  void array_void[10]; /* expected-error {{array has incomplete element type 'void'}} */505 506  struct S; /* expected-note {{forward declaration of 'struct S'}} */507  struct S s[10]; /* expected-error {{array has incomplete element type 'struct S'}} */508 509  union U; /* expected-note {{forward declaration of 'union U'}} */510  union U u[10]; /* expected-error {{array has incomplete element type 'union U'}} */511  union U { int i; };512 513  int never_completed_incomplete_array[][]; /* expected-error {{array has incomplete element type 'int[]'}} */514 515  extern int completed_later[][]; /* expected-error {{array has incomplete element type 'int[]'}} */516  extern int completed_later[10][10];517}518 519/* WG14 DR098: yes520 * Pre/post increment/decrement of function or incomplete types521 */522void dr098(void) {523  typedef void func_type(void);524  func_type fp;525  struct incomplete *incomplete_ptr;526 527  ++fp; /* expected-error {{cannot increment value of type 'func_type' (aka 'void (void)')}} */528  fp++; /* expected-error {{cannot increment value of type 'func_type' (aka 'void (void)')}} */529  --fp; /* expected-error {{cannot decrement value of type 'func_type' (aka 'void (void)')}} */530  fp--; /* expected-error {{cannot decrement value of type 'func_type' (aka 'void (void)')}} */531 532  (*incomplete_ptr)++; /* expected-error {{cannot increment value of type 'struct incomplete'}} */533  ++(*incomplete_ptr); /* expected-error {{cannot increment value of type 'struct incomplete'}} */534  (*incomplete_ptr)--; /* expected-error {{cannot decrement value of type 'struct incomplete'}} */535  --(*incomplete_ptr); /* expected-error {{cannot decrement value of type 'struct incomplete'}} */536}537