2010-04-29 08:18:15 +08:00
|
|
|
// RUN: %clang_cc1 -triple x86_64-apple-darwin10.0.0 -fsyntax-only -verify %s -Winvalid-offsetof
|
2009-05-02 07:20:30 +08:00
|
|
|
|
|
|
|
struct NonPOD {
|
|
|
|
virtual void f();
|
|
|
|
int m;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct P {
|
|
|
|
NonPOD fieldThatPointsToANonPODType;
|
|
|
|
};
|
|
|
|
|
|
|
|
void f() {
|
2010-03-10 19:27:22 +08:00
|
|
|
int i = __builtin_offsetof(P, fieldThatPointsToANonPODType.m); // expected-warning{{offset of on non-POD type 'P'}}
|
2009-05-02 07:20:30 +08:00
|
|
|
}
|
|
|
|
|
2009-12-04 15:18:51 +08:00
|
|
|
struct Base { int x; };
|
|
|
|
struct Derived : Base { int y; };
|
|
|
|
int o = __builtin_offsetof(Derived, x); // expected-warning{{offset of on non-POD type}}
|
2009-12-12 15:25:49 +08:00
|
|
|
|
|
|
|
const int o2 = sizeof(__builtin_offsetof(Derived, x));
|
Completely reimplement __builtin_offsetof, based on a patch by Roberto
Amadini.
This change introduces a new expression node type, OffsetOfExpr, that
describes __builtin_offsetof. Previously, __builtin_offsetof was
implemented using a unary operator whose subexpression involved
various synthesized array-subscript and member-reference expressions,
which was ugly and made it very hard to instantiate as a
template. OffsetOfExpr represents the AST more faithfully, with proper
type source information and a more compact representation.
OffsetOfExpr also has support for dependent __builtin_offsetof
expressions; it can be value-dependent, but will never be
type-dependent (like sizeof or alignof). This commit introduces
template instantiation for __builtin_offsetof as well.
There are two major caveats to this patch:
1) CodeGen cannot handle the case where __builtin_offsetof is not a
constant expression, so it produces an error. So, to avoid
regressing in C, we retain the old UnaryOperator-based
__builtin_offsetof implementation in C while using the shiny new
OffsetOfExpr implementation in C++. The old implementation can go
away once we have proper CodeGen support for this case, which we
expect won't cause much trouble in C++.
2) __builtin_offsetof doesn't work well with non-POD class types,
particularly when the designated field is found within a base
class. I will address this in a subsequent patch.
Fixes PR5880 and a bunch of assertions when building Boost.Python
tests.
llvm-svn: 102542
2010-04-29 06:16:22 +08:00
|
|
|
|
|
|
|
struct HasArray {
|
|
|
|
int array[17];
|
|
|
|
};
|
|
|
|
|
|
|
|
// Constant and non-constant offsetof expressions
|
|
|
|
void test_ice(int i) {
|
|
|
|
int array0[__builtin_offsetof(HasArray, array[5])];
|
2010-05-23 00:17:30 +08:00
|
|
|
int array1[__builtin_offsetof(HasArray, array[i])];
|
Completely reimplement __builtin_offsetof, based on a patch by Roberto
Amadini.
This change introduces a new expression node type, OffsetOfExpr, that
describes __builtin_offsetof. Previously, __builtin_offsetof was
implemented using a unary operator whose subexpression involved
various synthesized array-subscript and member-reference expressions,
which was ugly and made it very hard to instantiate as a
template. OffsetOfExpr represents the AST more faithfully, with proper
type source information and a more compact representation.
OffsetOfExpr also has support for dependent __builtin_offsetof
expressions; it can be value-dependent, but will never be
type-dependent (like sizeof or alignof). This commit introduces
template instantiation for __builtin_offsetof as well.
There are two major caveats to this patch:
1) CodeGen cannot handle the case where __builtin_offsetof is not a
constant expression, so it produces an error. So, to avoid
regressing in C, we retain the old UnaryOperator-based
__builtin_offsetof implementation in C while using the shiny new
OffsetOfExpr implementation in C++. The old implementation can go
away once we have proper CodeGen support for this case, which we
expect won't cause much trouble in C++.
2) __builtin_offsetof doesn't work well with non-POD class types,
particularly when the designated field is found within a base
class. I will address this in a subsequent patch.
Fixes PR5880 and a bunch of assertions when building Boost.Python
tests.
llvm-svn: 102542
2010-04-29 06:16:22 +08:00
|
|
|
}
|
2010-04-29 06:36:06 +08:00
|
|
|
|
|
|
|
// Bitfields
|
|
|
|
struct has_bitfields {
|
|
|
|
int i : 7;
|
|
|
|
int j : 12; // expected-note{{bit-field is declared here}}
|
|
|
|
};
|
|
|
|
|
|
|
|
int test3 = __builtin_offsetof(struct has_bitfields, j); // expected-error{{cannot compute offset of bit-field 'j'}}
|
2010-04-29 08:18:15 +08:00
|
|
|
|
|
|
|
// offsetof referring to members of a base class.
|
|
|
|
struct Base1 {
|
|
|
|
int x;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Base2 {
|
|
|
|
int y;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Derived2 : public Base1, public Base2 {
|
|
|
|
int z;
|
|
|
|
};
|
|
|
|
|
|
|
|
int derived1[__builtin_offsetof(Derived2, x) == 0? 1 : -1];
|
|
|
|
int derived2[__builtin_offsetof(Derived2, y) == 4? 1 : -1];
|
|
|
|
int derived3[__builtin_offsetof(Derived2, z) == 8? 1 : -1];
|
2010-08-05 18:11:36 +08:00
|
|
|
|
|
|
|
// offsetof referring to anonymous struct in base.
|
|
|
|
// PR7769
|
|
|
|
struct foo {
|
|
|
|
struct {
|
|
|
|
int x;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
struct bar : public foo {
|
|
|
|
};
|
|
|
|
|
|
|
|
int anonstruct[__builtin_offsetof(bar, x) == 0 ? 1 : -1];
|
2011-10-18 07:29:39 +08:00
|
|
|
|
|
|
|
struct LtoRCheck {
|
|
|
|
int a[10];
|
|
|
|
int f();
|
|
|
|
};
|
|
|
|
int ltor = __builtin_offsetof(struct LtoRCheck, a[LtoRCheck().f]); // \
|
|
|
|
expected-error {{reference to non-static member function must be called}}
|
2013-10-15 14:28:23 +08:00
|
|
|
|
|
|
|
namespace PR17578 {
|
|
|
|
struct Base {
|
|
|
|
int Field;
|
|
|
|
};
|
|
|
|
struct Derived : virtual Base {
|
|
|
|
void Fun() { (void)__builtin_offsetof(Derived, Field); } // expected-warning {{offset of on non-POD type}} \
|
|
|
|
expected-error {{invalid application of 'offsetof' to a field of a virtual base}}
|
|
|
|
};
|
|
|
|
}
|