brintos

brintos / llvm-project-archived public Read only

0
0
Text · 17.6 KiB · 7567d48 Raw
462 lines · c
1/* RUN: %clang_cc1 -std=c89 -fsyntax-only -verify=expected,c89only -pedantic -Wno-c11-extensions %s2   RUN: %clang_cc1 -std=c99 -triple x86_64-unknown-linux -fsyntax-only -verify=expected,c99untilc2x -pedantic -Wno-c11-extensions %s3   RUN: %clang_cc1 -std=c99 -triple x86_64-unknown-win32 -fms-compatibility -fsyntax-only -verify=expected,c99untilc2x -pedantic -Wno-c11-extensions %s4   RUN: %clang_cc1 -std=c11 -fsyntax-only -verify=expected,c99untilc2x -pedantic %s5   RUN: %clang_cc1 -std=c17 -fsyntax-only -verify=expected,c99untilc2x -pedantic %s6   RUN: %clang_cc1 -std=c2x -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 DR201: yes13 * Integer types longer than long14 *15 * WG14 DR211: yes16 * Accuracy of decimal string to/from "binary" (non-decimal) floating-point conversions17 *18 * WG14 DR215: yes19 * Equality operators20 *21 * WG14 DR218: yes22 * Signs of non-numeric floating point values23 *24 * WG14 DR219: yes25 * Effective types26 *27 * WG14 DR221: yes28 * Lacuna in pointer arithmetic29 *30 * WG14 DR222: yes31 * Partially initialized structures32 *33 * WG14 DR234: yes34 * Miscellaneous Typos35 *36 * WG14 DR245: yes37 * Missing paragraph numbers38 *39 * WG14 DR247: yes40 * Are values a form of behaviour?41 *42 * WG14 DR248: yes43 * Limits are required for optional types44 *45 * WG14 DR255: yes46 * Non-prototyped function calls and argument mismatches47 *48 * WG14 DR267: yes49 * Typos in 5.1.2.3, 7.24.4.4.5, 7.24.6.1, 7.24.6.150 *51 * WG14 DR273: yes52 * Meaning of __STDC_ISO_10646__53 *54 * WG14 DR278: yes55 * Lacuna in character encodings56 *57 * WG14 DR279: yes58 * Wide character code values for members of the basic character set59 *60 * WG14 DR282: yes61 * Flexible array members & struct padding62 *63 * WG14 DR292: yes64 * Use of the word variable65 */66 67 68/* WG14 DR204: yes69 * size_t and ptrdiff_t as a long long type70 */71void dr204(void) {72  __typeof__(sizeof(0)) s;73  __typeof__((int *)0 - (int *)0) p;74  signed long sl;75#if __LLONG_WIDTH__ > __LONG_WIDTH__76  /* If the implementation supports a standard integer type larger than signed77   * long, it's okay for size_t and ptrdiff_t to have a greater integer78   * conversion rank than signed long.79   *80   * Note, it's not required that the implementation use that larger conversion81   * rank; it's acceptable to use an unsigned long or unsigned int for the size82   * type (those ranks are not greater than that of signed long).83   */84   (void)_Generic(s + sl, unsigned long long : 1, unsigned long : 1, unsigned int : 1); /* c89only-warning {{'long long' is an extension when C99 mode is not enabled}} */85   (void)_Generic(p + sl, signed long long : 1, signed long : 1, signed int : 1);       /* c89only-warning {{'long long' is an extension when C99 mode is not enabled}} */86#elif __LLONG_WIDTH__ == __LONG_WIDTH__87  /* But if the implementation doesn't support a larger standard integer type88   * than signed long, the conversion rank should prefer signed long if the type89   * is signed (ptrdiff_t) or unsigned long if the type is unsigned (size_t).90   *91   * Note, as above, unsigned/signed int is also acceptable due to having a92   * lesser integer conversion rank.93   */94   (void)_Generic(s + sl, unsigned long : 1, unsigned int : 1);95   (void)_Generic(p + sl, signed long : 1, signed int : 1);96#else97#error "Something has gone off the rails"98#endif99}100 101/* WG14 DR207: partial102 * Handling of imaginary types103 *104 * FIXME: Clang recognizes the _Imaginary keyword but does not support the data105 * type.106 */107void dr207(void) {108  _Imaginary float f; /* expected-error {{imaginary types are not supported}}109                         c89only-warning {{'_Imaginary' is a C99 extension}}110                       */111}112 113/* WG14 DR216: yes114 * Source character encodings115 */116void dr216(void) {117#define A(x) _Static_assert((char)x >= 0, "no")118  A('A'); A('B'); A('C'); A('D'); A('E'); A('F'); A('G'); A('H'); A('I');119  A('J'); A('K'); A('L'); A('M'); A('N'); A('O'); A('P'); A('Q'); A('R');120  A('S'); A('T'); A('U'); A('V'); A('W'); A('X'); A('Y'); A('Z');121 122  A('a'); A('b'); A('c'); A('d'); A('e'); A('f'); A('g'); A('h'); A('i');123  A('j'); A('k'); A('l'); A('m'); A('n'); A('o'); A('p'); A('q'); A('r');124  A('s'); A('t'); A('u'); A('v'); A('w'); A('x'); A('y'); A('z');125 126  A('0'); A('1'); A('2'); A('3'); A('4');127  A('5'); A('6'); A('7'); A('8'); A('9');128 129  A('!'); A('"'); A('#'); A('%'); A('&'); A('\''); A('('); A(')'); A('*');130  A('+'); A(','); A('-'); A('.'); A('/'); A(':'); A(';'); A('<'); A('=');131  A('>'); A('?'); A('['); A('\\'); A(']'); A('^'); A('_'); A('{'); A('|');132  A('}'); A('~');133#undef A134}135 136/* WG14 DR230: yes137 * Enumerated type rank138 */139void dr230(void) {140  enum E {141    Value = __INT_MAX__142  } e;143  /* The enumeration type has a compatible type that is a signed or unsigned144   * integer type, or char. But it has to be large enough to hold all of the145   * values of the enumerators. So it needs to be at least int or unsigned int.146   *147   * The integer conversion rank for an enumeration is the same as its148   * compatible type (C99 6.3.1.1p1), so it's eligible for integer promotions149   * to either int or unsigned int, depending on the compatible type150   * (C99 6.3.1.1p2).151   */152  (void)_Generic(e, int : 1, unsigned int : 1);153  (void)_Generic((enum E)Value, int : 1, unsigned int : 1);154  /* The enumerators themselves have type int (C99 6.7.2.2p3). */155  (void)_Generic(Value, int : 1);156}157 158/* WG14 DR231: no159 * Semantics of text-line and non-directive160 *161 * One of the preprocessing groups to support is # non-directive (C99 6.10p1),162 * which is defined as pp-tokens followed by a newline. However, we fail to163 * translate the program if we don't recognize the directive, and we don't take164 * note when what follows the # is not a valid preprocessing token.165 */166 167/* FIXME: this should not fail. */168# nope /* expected-error {{invalid preprocessing directive}} */169 170/* FIXME: this should fail, but not because of the unknown directive; it should171 * fail because of the invalid preprocessing-token.172 */173# 'a174/* expected-error@-1 {{invalid preprocessing directive}} \175   expected-warning@-1 {{missing terminating ' character}}176*/177 178/* WG14 DR237: no179 * Declarations using [static]180 */181void dr237_f(int array[static 10]); /* c89only-warning {{static array size is a C99 feature}}182                                       expected-note {{callee declares array parameter as static here}}183                                     */184void dr237_1(void) {185  int array[4];186  dr237_f(array); /* expected-warning {{array argument is too small; contains 4 elements, callee requires at least 10}} */187}188 189/* FIXME: the composite type for this declaration should retain the static190 * array extent instead of losing it.191 */192void dr237_f(int array[]);193 194void dr237_2(void) {195  int array[4];196  /* FIXME: this should diagnose the same as above. */197  dr237_f(array);198}199 200/* WG14 DR246: yes201 * Completion of declarators202 */203void dr246(void) {204  int i[i]; /* expected-error {{use of undeclared identifier 'i'}} */205}206 207/* WG14 DR250: yes208 * Non-directives within macro arguments209 */210void dr250(void) {211#define dr250_nothing(x)212 213  /* FIXME: See DR231 regarding the error about an invalid preprocessing214   * directive.215   */216 217  dr250_nothing(218#nondirective    /* expected-error {{invalid preprocessing directive}}219                    expected-warning {{embedding a directive within macro arguments has undefined behavior}}220                  */221  )222 223#undef dr250_nothing224}225 226/* WG14 DR251: yes227 * Are struct fred and union fred the same type?228 */229union dr251_fred { int a; }; /* expected-note {{previous use is here}} */230void dr251(void) {231  struct dr251_fred *ptr; /* expected-error {{use of 'dr251_fred' with tag type that does not match previous declaration}} */232}233 234#if __STDC_VERSION__ < 202311L235/* WG14 DR252: yes236 * Incomplete argument types when calling non-prototyped functions237 */238void dr252_no_proto();  /* expected-warning {{a function declaration without a prototype is deprecated in all versions of C}} */239void dr252_proto(void); /* expected-note {{'dr252_proto' declared here}} */240void dr252(void) {241  /* It's a constraint violation to pass an argument to a function with a242   * prototype that specifies a void parameter.243   */244  dr252_proto(dr252_no_proto()); /* expected-error {{too many arguments to function call, expected 0, have 1}} */245 246  /* It's technically UB to pass an incomplete type to a function without a247   * prototype, but Clang treats it as an error.248   */249  dr252_no_proto(dr252_proto()); /* expected-error {{argument type 'void' is incomplete}}250                                    expected-warning {{passing arguments to 'dr252_no_proto' without a prototype is deprecated in all versions of C and is not supported in C23}}251                                  */252}253#endif /* __STDC_VERSION__ < 202311L */254 255/* WG14 DR258: yes256 * Ordering of "defined" and macro replacement257 */258void dr258(void) {259  /* We get the diagnostic twice because the argument is used twice in the260   * expansion. */261#define repeat(x) x && x262#if repeat(defined fred) /* expected-warning 2 {{macro expansion producing 'defined' has undefined behavior}} */263#endif264 265  /* We get no diagnostic because the argument is unused. */266#define forget(x) 0267#if forget(defined fred)268#endif269 270#undef repeat271#undef forget272}273 274/* WG14 DR261: yes275 * Constant expressions276 */277void dr261(void) {278  /* This is not an integer constant expression because of the overflow,279   * but we fold it as a constant expression anyway as a GNU extension. */280  enum e1 {281    ex1 = __INT_MAX__ + 1  /* expected-warning {{overflow in expression; result is -2'147'483'648 with type 'int'}}282                              expected-warning {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}283                              expected-note {{value 2147483648 is outside the range of representable values of type 'int'}} */284  };285 286  /* This is not an integer constant expression, because of the comma operator,287   * but we fold it as a constant expression anyway as a GNU extension.288   */289  enum e2 {290    ex2 = __INT_MAX__ + (0, 1) /* expected-warning {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}291                                  expected-note {{value 2147483648 is outside the range of representable values of type 'int'}}292                                  expected-warning {{left operand of comma operator has no effect}}293                                */294  };295 296  /* It's a bit weird that we issue a "congratulations, you did the thing"297   * diagnostic, but the diagnostic does help demonstrate that we correctly298   * treat it as a null pointer constant value.299   */300  char *p1 = (1 - 1); /* expected-warning {{expression which evaluates to zero treated as a null pointer constant of type 'char *'}} */301 302  /* This is an invalid initialization/assignment because the right-hand side303   * does not have pointer to void or pointer to char type and is not the null304   * pointer constant. */305  char *p2 = (42, 1 - 1); /* expected-error {{incompatible integer to pointer conversion initializing 'char *' with an expression of type 'int'}}306                             expected-warning {{left operand of comma operator has no effect}}307                           */308  p1 = (42, 1 - 1);       /* expected-error {{incompatible integer to pointer conversion assigning to 'char *' from 'int'}}309                             expected-warning {{left operand of comma operator has no effect}}310                           */311 312  /* These are both valid. The initialization doesn't require an integer313   * constant expression, nor does the assignment.314   */315  short s1 = 42 + (0, 1); /* c89only-warning {{mixing declarations and code is a C99 extension}}316                             expected-warning {{left operand of comma operator has no effect}}317                           */318  s1 = (42, 69); /* expected-warning {{left operand of comma operator has no effect}} */319 320  /* These are both valid because they are constant expressions and the value321   * is the null pointer constant.322   */323  p2 = 0;324  p2 = 1 - 1; /* expected-warning {{expression which evaluates to zero treated as a null pointer constant of type 'char *'}} */325}326 327/* WG14 DR262: yes328 * Maximum size of bit fields329 */330void dr262(void) {331  _Static_assert(sizeof(short) == 2, "short is not two chars?");332  struct S {333    short field : __CHAR_BIT__ * 2; /* ok */334    short other_field : __CHAR_BIT__ * 2 + 1; /* expected-error-re {{width of bit-field 'other_field' ({{[0-9]+}} bits) exceeds the width of its type ({{[0-9]+}} bits)}} */335  };336}337 338/* WG14 DR263: yes339 * All-zero bits representations340 *341 * This tests that the integer value 0 is not comprised of any non-zero bits,342 * which demonstrates that a value with all zero bits will be treated as the343 * integer value zero.344 */345_Static_assert(__builtin_popcount(0) < 1, "zero is not all zero bits");346 347 348/* WG14 DR265: yes349 * Preprocessor arithmetic350 */351#if __UINT_MAX__ == 0xFFFFFFFF352/* Ensure that the literal is interpreted as intptr_t instead of uintptr_t,353 * despite that being the phase 7 behavior being that the literal is unsigned.354 */355#if -0xFFFFFFFF >= 0356#error "Interpreting the literal incorrectly in the preprocessor"357#endif358#endif /* __UINT_MAX__ == 0xFFFFFFFF */359 360 361/* WG14 DR266: yes362 * Overflow of sizeof363 */364void dr266(void) {365  /* Some targets support a maximum size which cannot be represented by an366   * unsigned long, and so unsigned long long is used instead. However, C89367   * doesn't have the long long type, so we issue a pedantic warning about it.368   * Disable the warning momentarily so we don't have to add target triples to369   * the RUN lines pinning the targets down concretely.370   */371#pragma clang diagnostic push372#pragma clang diagnostic ignored "-Wlong-long"373  (void)sizeof(int[__SIZE_MAX__ / 2][__SIZE_MAX__ / 2]); /* expected-error-re 2 {{array is too large ({{[0-9']+}} elements)}} */374#pragma clang diagnostic pop375}376 377/* WG14 DR272: yes378 * Type category379 */380void dr272(void) {381  /* The crux of this DR is to confirm that lvalue conversion of the rhs on an382   * assignment expression strips top-level qualifiers, and not all qualifiers,383   * from the resulting expression type.384   */385  const int * volatile ptr;386  (void)_Generic(ptr = 0, const int * : 1); /* expected-warning {{expression with side effects has no effect in an unevaluated context}} */387}388 389/* WG14 DR277: no390 * Declarations within iteration statements391 */392void dr277(void) {393  /* FIXME: it's a bit silly to issue both of these warnings at the same time394   * in pedantic mode given that they're both effectively the same root cause.395   *396   * C99 6.8.5p3: The declaration part of a for statement shall only declare397   * identifiers for objects having storage class auto or register.398   *399   * FIXME: we don't issue a pedantic warning below for the declaration of E,400   * and its enumerators, none of which declare an object with auto or register401   * storage classes.402   */403  for (enum E { one, two } i = one; i < two; ++i) /* c89only-warning {{variable declaration in for loop is a C99-specific feature}}404                                                     c89only-warning {{GCC does not allow variable declarations in for loop initializers before C99}}405                                                   */406    ;407}408 409#if __STDC_VERSION__ >= 199901L410/* WG14 DR289: yes411 * Function prototype with [restrict]412 *413 * Ensure that we support [restrict] array syntax as an abstract declarator and414 * not just as a direct declarator.415 */416void dr289(int * restrict const [restrict]);417#endif /* __STDC_VERSION__ >= 199901L */418 419/* WG14 DR295: yes420 * Incomplete types for function parameters421 */422struct NotCompleted;                    /* expected-note {{forward declaration of 'struct NotCompleted'}} */423void dr295_1(struct NotCompleted);424void dr295_1(struct NotCompleted Val) { /* expected-error {{variable has incomplete type 'struct NotCompleted'}} */425}426 427/* There's no reason to reject this code, but it's technically undefined428 * behavior, so diagnosing it is reasonable.429 *430 * FIXME: either downgrade this error into a warning or remove it entirely; it431 * doesn't add a whole lot of value as an error.432 */433void dr295_2(void param); /* expected-error {{argument may not have 'void' type}} */434 435/* WG14 DR298: partial436 * Validity of constant in unsigned long long range437 *438 * I'm giving this one a partial because we fail to pedantically diagnose the439 * use of 'long long' through a constant value. We correctly warn about the440 * type when spelled out and when using an explicit suffix, but we fail to warn441 * otherwise.442 */443#if __LLONG_WIDTH__ >= 64 && __LONG_WIDTH__ < 64444/* This test requires that long long be at least 64-bits and long be smaller445 * because the test is whether the integer literal which is too large to fit in446 * a constant of type long long. This is undefined behavior in C, which means447 * we're free to pick a different type so long as we diagnose the extension448 * appropriately.449 */450void dr298(void) {451  /* FIXME: These uses of the constants need a pedantic warning in C89 mode;452   * we've picked a type that does not exist in C89.453   */454  (void)_Generic(9223372036854775808,     /* expected-warning {{integer literal is too large to be represented in a signed integer type, interpreting as unsigned}}455                                             c89only-warning {{'long long' is an extension when C99 mode is not enabled}}456                                           */457                 unsigned long long : 1); /* c89only-warning {{'long long' is an extension when C99 mode is not enabled}} */458  (void)_Generic(9223372036854775807,     /* c89only-warning {{'long long' is an extension when C99 mode is not enabled}} */459                 long long : 1);          /* c89only-warning {{'long long' is an extension when C99 mode is not enabled}} */460}461#endif /* __LLONG_WIDTH__ == 64 && __LONG_WIDTH__ < 64 */462