bitfield.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // RUN: %clang_cc1 %s -fsyntax-only -verify -std=c11 -Wno-unused-value
  2. enum e0; // expected-note{{forward declaration of 'enum e0'}}
  3. struct a {
  4. int a : -1; // expected-error{{bit-field 'a' has negative width}}
  5. // rdar://6081627
  6. int b : 33; // expected-error{{size of bit-field 'b' (33 bits) exceeds size of its type (32 bits)}}
  7. int c : (1 + 0.25); // expected-error{{expression is not an integer constant expression}}
  8. int d : (int)(1 + 0.25);
  9. // rdar://6138816
  10. int e : 0; // expected-error {{bit-field 'e' has zero width}}
  11. float xx : 4; // expected-error {{bit-field 'xx' has non-integral type}}
  12. // PR3607
  13. enum e0 f : 1; // expected-error {{field has incomplete type 'enum e0'}}
  14. int g : (_Bool)1;
  15. // PR4017
  16. char : 10; // expected-error {{size of anonymous bit-field (10 bits) exceeds size of its type (8 bits)}}
  17. unsigned : -2; // expected-error {{anonymous bit-field has negative width (-2)}}
  18. float : 12; // expected-error {{anonymous bit-field has non-integral type 'float'}}
  19. };
  20. struct b {unsigned x : 2;} x;
  21. __typeof__(x.x+1) y;
  22. int y;
  23. struct {unsigned x : 2;} x2;
  24. __typeof__((x.x+=1)+1) y;
  25. __typeof__((0,x.x)+1) y;
  26. __typeof__(x.x<<1) y;
  27. int y;
  28. struct PR8025 {
  29. double : 2; // expected-error{{anonymous bit-field has non-integral type 'double'}}
  30. };
  31. struct Test4 {
  32. unsigned bitX : 4;
  33. unsigned bitY : 4;
  34. unsigned var;
  35. };
  36. void test4(struct Test4 *t) {
  37. (void) sizeof(t->bitX); // expected-error {{invalid application of 'sizeof' to bit-field}}
  38. (void) sizeof((t->bitY)); // expected-error {{invalid application of 'sizeof' to bit-field}}
  39. (void) sizeof(t->bitX = 4); // not a bitfield designator in C
  40. (void) sizeof(t->bitX += 4); // not a bitfield designator in C
  41. (void) sizeof((void) 0, t->bitX); // not a bitfield designator in C
  42. (void) sizeof(t->var ? t->bitX : t->bitY); // not a bitfield designator in C
  43. (void) sizeof(t->var ? t->bitX : t->bitX); // not a bitfield designator in C
  44. }
  45. typedef unsigned Unsigned;
  46. typedef signed Signed;
  47. struct Test5 { unsigned n : 2; } t5;
  48. typedef __typeof__(t5.n) Unsigned; // Bitfield is unsigned
  49. typedef __typeof__(+t5.n) Signed; // ... but promotes to signed.
  50. typedef __typeof__(t5.n + 0) Signed; // Arithmetic promotes.
  51. typedef __typeof__(+(t5.n = 0)) Signed; // FIXME: Assignment should not; the result
  52. typedef __typeof__(+(t5.n += 0)) Signed; // is a non-bit-field lvalue of type unsigned.
  53. typedef __typeof__(+(t5.n *= 0)) Signed;
  54. typedef __typeof__(+(++t5.n)) Signed; // FIXME: Increment is equivalent to compound-assignment.
  55. typedef __typeof__(+(--t5.n)) Signed; // This should not promote to signed.
  56. typedef __typeof__(+(t5.n++)) Unsigned; // Post-increment is underspecified, but seems to
  57. typedef __typeof__(+(t5.n--)) Unsigned; // also act like compound-assignment.
  58. struct Test6 {
  59. : 0.0; // expected-error{{type name requires a specifier or qualifier}}
  60. };