This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[C++ diagnostic] Handle OFFSETOF_EXPR in C++ dump_expr (PR c++/33842)
- From: Jakub Jelinek <jakub at redhat dot com>
- To: Mark Mitchell <mark at codesourcery dot com>, Jason Merrill <jason at redhat dot com>
- Cc: gcc-patches at gcc dot gnu dot org
- Date: Fri, 26 Oct 2007 11:14:15 -0400
- Subject: [C++ diagnostic] Handle OFFSETOF_EXPR in C++ dump_expr (PR c++/33842)
- Reply-to: Jakub Jelinek <jakub at redhat dot com>
Hi!
This patch handles printing OFFSETOF_EXPR during diagnostic.
If the OFFSETOF_EXPR argument isn't bogus, then it will print it
as offsetof looks like (e.g.. offsetof(type, foo.bar[12].baz) )
but falls back to printing just the inner expression if there are errors in
it.
Ok for trunk?
2007-10-26 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/error33.C: New test.
--- gcc/cp/cxx-pretty-print.h.jj 2007-09-28 08:29:16.000000000 +0200
+++ gcc/cp/cxx-pretty-print.h 2007-10-26 16:20:10.000000000 +0200
@@ -72,6 +72,7 @@ void pp_cxx_canonical_template_parameter
void pp_cxx_trait_expression (cxx_pretty_printer *, tree);
void pp_cxx_typeid_expression (cxx_pretty_printer *, tree);
void pp_cxx_va_arg_expression (cxx_pretty_printer *, tree);
+void pp_cxx_offsetof_expression (cxx_pretty_printer *, tree);
void pp_cxx_delete_expression (cxx_pretty_printer *, tree);
#endif /* GCC_CXX_PRETTY_PRINT_H */
--- gcc/cp/error.c.jj 2007-09-30 18:56:20.000000000 +0200
+++ gcc/cp/error.c 2007-10-26 16:20:28.000000000 +0200
@@ -2048,6 +2048,10 @@ dump_expr (tree t, int flags)
pp_cxx_va_arg_expression (cxx_pp, t);
break;
+ case OFFSETOF_EXPR:
+ pp_cxx_offsetof_expression (cxx_pp, t);
+ break;
+
case DELETE_EXPR:
case VEC_DELETE_EXPR:
pp_cxx_delete_expression (cxx_pp, t);
--- gcc/cp/cxx-pretty-print.c.jj 2007-10-26 13:45:43.000000000 +0200
+++ gcc/cp/cxx-pretty-print.c 2007-10-26 16:56:36.000000000 +0200
@@ -421,6 +421,10 @@ pp_cxx_primary_expression (cxx_pretty_pr
pp_cxx_va_arg_expression (pp, t);
break;
+ case OFFSETOF_EXPR:
+ pp_cxx_offsetof_expression (pp, t);
+ break;
+
default:
pp_c_primary_expression (pp_c_base (pp), t);
break;
@@ -2177,6 +2181,49 @@ pp_cxx_va_arg_expression (cxx_pretty_pri
pp_cxx_right_paren (pp);
}
+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_trait_expression (cxx_pretty_printer *pp, tree t)
{
--- gcc/testsuite/g++.dg/template/error33.C.jj 2007-10-26 17:02:22.000000000 +0200
+++ gcc/testsuite/g++.dg/template/error33.C 2007-10-26 17:07:28.000000000 +0200
@@ -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\\)" }
+};
Jakub