2007-10-27 Jakub Jelinek 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 struct A +{ + A<__builtin_offsetof(T, x)>(); // { dg-error "type/value mismatch|offsetof\\(T, x\\)" } +}; + +template struct B +{ + B<__builtin_offsetof(T, x.y)>(); // { dg-error "type/value mismatch|offsetof\\(T, x.y\\)" } +}; + +template struct C +{ + C<__builtin_offsetof(T, x[6])>(); // { dg-error "type/value mismatch|offsetof\\(T, x\\\[6\\\]\\)" } +}; + +template 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 struct F +{ + F<__builtin_offsetof(E, x)>(); // { dg-error "type/value mismatch|offsetof\\(E, x\\)" } +};