gcc/gcc41-pr33842.patch
Jakub Jelinek 0d16b6923b 4.1.2-37
2007-12-31 18:09:00 +00:00

131 lines
3.8 KiB
Diff

2007-10-27 Jakub Jelinek <jakub@redhat.com>
PR c++/33842
* cxx-pretty-print.h (pp_cxx_offsetof_expression): New prototype.
* cxx-pretty-print.c (pp_cxx_primary_expression): Handle
OFFSETOF_EXPR.
(pp_cxx_offsetof_expression_1, pp_cxx_offsetof_expression): New
functions.
* error.c (dump_expr): Handle OFFSETOF_EXPR.
* g++.dg/template/error34.C: New test.
--- gcc/cp/error.c (revision 129676)
+++ gcc/cp/error.c (revision 129677)
@@ -1887,6 +1887,10 @@ dump_expr (tree t, int flags)
dump_expr (TREE_OPERAND (t, 0), flags);
break;
+ case OFFSETOF_EXPR:
+ pp_cxx_offsetof_expression (cxx_pp, t);
+ break;
+
/* This list is incomplete, but should suffice for now.
It is very important that `sorry' does not call
`report_error_function'. That could cause an infinite loop. */
--- gcc/cp/cxx-pretty-print.c (revision 129676)
+++ gcc/cp/cxx-pretty-print.c (revision 129677)
@@ -356,6 +356,10 @@ pp_cxx_primary_expression (cxx_pretty_pr
pp_cxx_right_paren (pp);
break;
+ case OFFSETOF_EXPR:
+ pp_cxx_offsetof_expression (pp, t);
+ break;
+
default:
pp_c_primary_expression (pp_c_base (pp), t);
break;
@@ -1944,6 +1948,49 @@ typedef c_pretty_print_fn pp_fun;
/* Initialization of a C++ pretty-printer object. */
+static bool
+pp_cxx_offsetof_expression_1 (cxx_pretty_printer *pp, tree t)
+{
+ switch (TREE_CODE (t))
+ {
+ case ARROW_EXPR:
+ if (TREE_CODE (TREE_OPERAND (t, 0)) == STATIC_CAST_EXPR
+ && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
+ {
+ pp_cxx_type_id (pp, TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0))));
+ pp_cxx_separate_with (pp, ',');
+ return true;
+ }
+ return false;
+ case COMPONENT_REF:
+ if (!pp_cxx_offsetof_expression_1 (pp, TREE_OPERAND (t, 0)))
+ return false;
+ if (TREE_CODE (TREE_OPERAND (t, 0)) != ARROW_EXPR)
+ pp_cxx_dot (pp);
+ pp_cxx_expression (pp, TREE_OPERAND (t, 1));
+ return true;
+ case ARRAY_REF:
+ if (!pp_cxx_offsetof_expression_1 (pp, TREE_OPERAND (t, 0)))
+ return false;
+ pp_left_bracket (pp);
+ pp_cxx_expression (pp, TREE_OPERAND (t, 1));
+ pp_right_bracket (pp);
+ return true;
+ default:
+ return false;
+ }
+}
+
+void
+pp_cxx_offsetof_expression (cxx_pretty_printer *pp, tree t)
+{
+ pp_cxx_identifier (pp, "offsetof");
+ pp_cxx_left_paren (pp);
+ if (!pp_cxx_offsetof_expression_1 (pp, TREE_OPERAND (t, 0)))
+ pp_cxx_expression (pp, TREE_OPERAND (t, 0));
+ pp_cxx_right_paren (pp);
+}
+
void
pp_cxx_pretty_printer_init (cxx_pretty_printer *pp)
{
--- gcc/cp/cxx-pretty-print.h (revision 129676)
+++ gcc/cp/cxx-pretty-print.h (revision 129677)
@@ -70,6 +70,6 @@ void pp_cxx_separate_with (cxx_pretty_pr
void pp_cxx_declaration (cxx_pretty_printer *, tree);
void pp_cxx_canonical_template_parameter (cxx_pretty_printer *, tree);
-
+void pp_cxx_offsetof_expression (cxx_pretty_printer *, tree);
#endif /* GCC_CXX_PRETTY_PRINT_H */
--- gcc/testsuite/g++.dg/template/error34.C (revision 0)
+++ gcc/testsuite/g++.dg/template/error34.C (revision 129677)
@@ -0,0 +1,29 @@
+// PR c++/33842
+// { dg-do compile }
+
+template<typename T> struct A
+{
+ A<__builtin_offsetof(T, x)>(); // { dg-error "type/value mismatch|offsetof\\(T, x\\)" }
+};
+
+template<typename T> struct B
+{
+ B<__builtin_offsetof(T, x.y)>(); // { dg-error "type/value mismatch|offsetof\\(T, x.y\\)" }
+};
+
+template<typename T> struct C
+{
+ C<__builtin_offsetof(T, x[6])>(); // { dg-error "type/value mismatch|offsetof\\(T, x\\\[6\\\]\\)" }
+};
+
+template<typename T> struct D
+{
+ D<__builtin_offsetof(T, x.y[6].z)>(); // { dg-error "type/value mismatch|offsetof\\(T, x.y\\\[6\\\].z\\)" }
+};
+
+struct E { int x; };
+
+template<typename T> struct F
+{
+ F<__builtin_offsetof(E, x)>(); // { dg-error "type/value mismatch|offsetof\\(E, x\\)" }
+};