public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* C++ PATCH for c++/85033, ICE with enumerator in __builtin_offsetof
@ 2018-03-23 10:49 Marek Polacek
  2018-03-23 12:39 ` Jason Merrill
  0 siblings, 1 reply; 2+ messages in thread
From: Marek Polacek @ 2018-03-23 10:49 UTC (permalink / raw)
  To: GCC Patches, Jason Merrill

In this test:

struct S {
    enum { X };
};

int
foo (struct S s)
{
  return s.X;
}

unlike in C, lookup_member in C++ is able to find X in S.  So in the following
testcase finish_offsetof happily passes the CONST_DECL X down to fold_offsetof,
which then crashes because fold_offsetof_1 doesn't handle CONST_DECLs.  I think
let's simply disallow 'em in finish_offsetof; we can't take their address anyway.

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2018-03-23  Marek Polacek  <polacek@redhat.com>

	PR c++/85033
	* semantics.c (finish_offsetof): Don't allow CONST_DECLs.

	* g++.dg/ext/builtin-offsetof2.C: New test.

diff --git gcc/cp/semantics.c gcc/cp/semantics.c
index 97fa57ae94e..035e3951574 100644
--- gcc/cp/semantics.c
+++ gcc/cp/semantics.c
@@ -4072,6 +4072,11 @@ finish_offsetof (tree object_ptr, tree expr, location_t loc)
 	}
       return error_mark_node;
     }
+  if (TREE_CODE (expr) == CONST_DECL)
+    {
+      error ("cannot apply %<offsetof%> to an enumerator %qD", expr);
+      return error_mark_node;
+    }
   if (REFERENCE_REF_P (expr))
     expr = TREE_OPERAND (expr, 0);
   if (!complete_type_or_else (TREE_TYPE (TREE_TYPE (object_ptr)), object_ptr))
diff --git gcc/testsuite/g++.dg/ext/builtin-offsetof2.C gcc/testsuite/g++.dg/ext/builtin-offsetof2.C
index e69de29bb2d..07cc55a65c1 100644
--- gcc/testsuite/g++.dg/ext/builtin-offsetof2.C
+++ gcc/testsuite/g++.dg/ext/builtin-offsetof2.C
@@ -0,0 +1,7 @@
+// PR c++/85033
+
+struct S {
+  enum { E };
+};
+
+int b = __builtin_offsetof(S, E); // { dg-error "cannot apply .offsetof. to an enumerator" }

	Marek

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: C++ PATCH for c++/85033, ICE with enumerator in __builtin_offsetof
  2018-03-23 10:49 C++ PATCH for c++/85033, ICE with enumerator in __builtin_offsetof Marek Polacek
@ 2018-03-23 12:39 ` Jason Merrill
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2018-03-23 12:39 UTC (permalink / raw)
  To: Marek Polacek; +Cc: GCC Patches

OK.

On Fri, Mar 23, 2018 at 6:48 AM, Marek Polacek <polacek@redhat.com> wrote:
> In this test:
>
> struct S {
>     enum { X };
> };
>
> int
> foo (struct S s)
> {
>   return s.X;
> }
>
> unlike in C, lookup_member in C++ is able to find X in S.  So in the following
> testcase finish_offsetof happily passes the CONST_DECL X down to fold_offsetof,
> which then crashes because fold_offsetof_1 doesn't handle CONST_DECLs.  I think
> let's simply disallow 'em in finish_offsetof; we can't take their address anyway.
>
> Bootstrapped/regtested on x86_64-linux, ok for trunk?
>
> 2018-03-23  Marek Polacek  <polacek@redhat.com>
>
>         PR c++/85033
>         * semantics.c (finish_offsetof): Don't allow CONST_DECLs.
>
>         * g++.dg/ext/builtin-offsetof2.C: New test.
>
> diff --git gcc/cp/semantics.c gcc/cp/semantics.c
> index 97fa57ae94e..035e3951574 100644
> --- gcc/cp/semantics.c
> +++ gcc/cp/semantics.c
> @@ -4072,6 +4072,11 @@ finish_offsetof (tree object_ptr, tree expr, location_t loc)
>         }
>        return error_mark_node;
>      }
> +  if (TREE_CODE (expr) == CONST_DECL)
> +    {
> +      error ("cannot apply %<offsetof%> to an enumerator %qD", expr);
> +      return error_mark_node;
> +    }
>    if (REFERENCE_REF_P (expr))
>      expr = TREE_OPERAND (expr, 0);
>    if (!complete_type_or_else (TREE_TYPE (TREE_TYPE (object_ptr)), object_ptr))
> diff --git gcc/testsuite/g++.dg/ext/builtin-offsetof2.C gcc/testsuite/g++.dg/ext/builtin-offsetof2.C
> index e69de29bb2d..07cc55a65c1 100644
> --- gcc/testsuite/g++.dg/ext/builtin-offsetof2.C
> +++ gcc/testsuite/g++.dg/ext/builtin-offsetof2.C
> @@ -0,0 +1,7 @@
> +// PR c++/85033
> +
> +struct S {
> +  enum { E };
> +};
> +
> +int b = __builtin_offsetof(S, E); // { dg-error "cannot apply .offsetof. to an enumerator" }
>
>         Marek

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-03-23 12:35 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-03-23 10:49 C++ PATCH for c++/85033, ICE with enumerator in __builtin_offsetof Marek Polacek
2018-03-23 12:39 ` Jason Merrill

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).