offsetof.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // RUN: %clang_cc1 -fsyntax-only -verify %s
  2. #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
  3. typedef struct P { int i; float f; } PT;
  4. struct external_sun3_core
  5. {
  6. unsigned c_regs;
  7. PT X[100];
  8. };
  9. void swap()
  10. {
  11. int x;
  12. x = offsetof(struct external_sun3_core, c_regs);
  13. x = __builtin_offsetof(struct external_sun3_core, X[42].f);
  14. x = __builtin_offsetof(struct external_sun3_core, X[42].f2); // expected-error {{no member named 'f2'}}
  15. x = __builtin_offsetof(int, X[42].f2); // expected-error {{offsetof requires struct}}
  16. int a[__builtin_offsetof(struct external_sun3_core, X) == 4 ? 1 : -1];
  17. int b[__builtin_offsetof(struct external_sun3_core, X[42]) == 340 ? 1 : -1];
  18. int c[__builtin_offsetof(struct external_sun3_core, X[42].f2) == 344 ? 1 : -1]; // expected-error {{no member named 'f2'}}
  19. }
  20. extern int f();
  21. struct s1 { int a; };
  22. int v1 = offsetof (struct s1, a) == 0 ? 0 : f();
  23. struct s2 { int a; };
  24. int v2 = (int)(&((struct s2 *) 0)->a) == 0 ? 0 : f();
  25. struct s3 { int a; };
  26. int v3 = __builtin_offsetof(struct s3, a) == 0 ? 0 : f();
  27. // PR3396
  28. struct sockaddr_un {
  29. unsigned char sun_len;
  30. char sun_path[104];
  31. };
  32. int a(int len) {
  33. int a[__builtin_offsetof(struct sockaddr_un, sun_path[len+1])];
  34. }
  35. // PR4079
  36. union x {struct {int x;};};
  37. int x[__builtin_offsetof(union x, x)];
  38. // rdar://problem/7222956
  39. struct incomplete; // expected-note 2 {{forward declaration of 'struct incomplete'}}
  40. int test1[__builtin_offsetof(struct incomplete, foo)]; // expected-error {{offsetof of incomplete type 'struct incomplete'}}
  41. int test2[__builtin_offsetof(struct incomplete[10], [4].foo)]; // expected-error {{array has incomplete element type 'struct incomplete'}}
  42. // Bitfields
  43. struct has_bitfields {
  44. int i : 7;
  45. int j : 12; // expected-note{{bit-field is declared here}}
  46. };
  47. int test3 = __builtin_offsetof(struct has_bitfields, j); // expected-error{{cannot compute offset of bit-field 'j'}}
  48. typedef struct Array { int array[1]; } Array;
  49. int test4 = __builtin_offsetof(Array, array);
  50. int test5() {
  51. return __builtin_offsetof(Array, array[*(int*)0]); // expected-warning{{indirection of non-volatile null pointer}} expected-note{{__builtin_trap}}
  52. }