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.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@102542 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/test/PCH/exprs.c b/test/PCH/exprs.c
index 2b588a2..038a18b 100644
--- a/test/PCH/exprs.c
+++ b/test/PCH/exprs.c
@@ -5,6 +5,7 @@
// RUN: %clang_cc1 -emit-pch -fblocks -o %t %S/exprs.h
// RUN: %clang_cc1 -fblocks -include-pch %t -fsyntax-only -verify %s
+__SIZE_TYPE__ size_type_value;
int integer;
long long_integer;
double floating;
@@ -35,6 +36,9 @@
// UnaryOperator
negate_enum *int_ptr4 = &integer;
+// OffsetOfExpr
+offsetof_type *offsetof_ptr = &size_type_value;
+
// SizeOfAlignOfExpr
typeof(sizeof(float)) size_t_value;
typeof_sizeof *size_t_ptr = &size_t_value;
diff --git a/test/PCH/exprs.h b/test/PCH/exprs.h
index 7012422..5af8c7c 100644
--- a/test/PCH/exprs.h
+++ b/test/PCH/exprs.h
@@ -25,6 +25,19 @@
// UnaryOperator
typedef typeof(-Enumerator) negate_enum;
+// OffsetOfExpr
+struct X {
+ int member;
+};
+struct Y {
+ struct X array[5];
+};
+struct Z {
+ struct Y y;
+};
+typedef typeof(__builtin_offsetof(struct Z, y.array[1 + 2].member))
+ offsetof_type;
+
// SizeOfAlignOfExpr
typedef typeof(sizeof(int)) typeof_sizeof;
typedef typeof(sizeof(Enumerator)) typeof_sizeof2;
diff --git a/test/SemaCXX/offsetof.cpp b/test/SemaCXX/offsetof.cpp
index 3283270..a1d907a 100644
--- a/test/SemaCXX/offsetof.cpp
+++ b/test/SemaCXX/offsetof.cpp
@@ -18,3 +18,13 @@
int o = __builtin_offsetof(Derived, x); // expected-warning{{offset of on non-POD type}}
const int o2 = sizeof(__builtin_offsetof(Derived, x));
+
+struct HasArray {
+ int array[17];
+};
+
+// Constant and non-constant offsetof expressions
+void test_ice(int i) {
+ int array0[__builtin_offsetof(HasArray, array[5])];
+ int array1[__builtin_offsetof(HasArray, array[i])]; // expected-error{{variable length arrays are not permitted in C++}}
+}
diff --git a/test/SemaTemplate/instantiate-expr-5.cpp b/test/SemaTemplate/instantiate-expr-5.cpp
index 941dae4..916c4f9 100644
--- a/test/SemaTemplate/instantiate-expr-5.cpp
+++ b/test/SemaTemplate/instantiate-expr-5.cpp
@@ -1,4 +1,22 @@
-// RUN: %clang_cc1 -fsyntax-only %s
+// RUN: %clang_cc1 -fsyntax-only -verify %s
template <class A> int x(A x) { return x++; }
int y() { return x<int>(1); }
+
+namespace PR5880 {
+ template<typename T>
+ struct A {
+ static const int a = __builtin_offsetof(T, a.array[5].m); // expected-error{{error: no member named 'a' in 'HasM'}}
+ };
+ struct HasM {
+ float m;
+ };
+
+ struct ArrayOfHasM {
+ HasM array[10];
+ };
+
+ struct B { ArrayOfHasM a; };
+ A<B> x;
+ A<HasM> x2; // expected-note{{in instantiation of}}
+}