On 04/08/14 10:23, Marek Polacek wrote:
On Fri, Mar 28, 2014 at 02:44:21PM +0000, Joseph S. Myers wrote:
On Fri, 28 Mar 2014, Marek Polacek wrote:

PR51088 contains some Really Bizzare code.  We should tell users
not to do any shenanigans like that.

Ok for trunk?

I don't think a doc patch resolves this bug.  The compiler should never
generate code with an undefined reference to a local label like that;
either the code should get a compile-time error (that's what I suggest),
or it should generate output that links but has undefined behavior at
runtime.

Ok, with this patch the compiler should issue an error if someone's
trying to take an address of a label defined in a statement expression
outside of that statement expression.
I admit this was very tricky; I had to completely revamp the patch
several times, this one is the least disrupting and simplest one
I could come up with.  It works by marking labels that are declared
outside of stmt expr while we're entering a stmt expr (but we mustn't
do this for nested stmt exprs).  If we're then defining the label in
stmt expr and it was referenced outside of this stmt expr, raise an error.
This patch doesn't catch cases like ({ A:0; }); &&A;, in that case the
behavior is just undefined.
Does this approach make sense?

Regtested/bootstrapped on x86_64-linux.  I don't think it's stage4 material,
so ok for next stage1?

2014-04-08  Marek Polacek  <pola...@redhat.com>

        PR c/51088
        * c-decl.c (stmt_expr_depth): New variable.
        (struct c_label_vars): Add seen_outside_stmt_expr variable.
        (c_bindings_start_stmt_expr): Bump stmt_expr_depth.  Mark labels
        declared outside of statement expressions.
        (c_bindings_end_stmt_expr): Decrement stmt_expr_depth.
        (make_label): Set seen_outside_stmt_expr.
        (check_earlier_gotos): Return true if error was issued.
        (define_label): Give error if taking an address of a label defined
        in statement expression outside of the statement expression.

        * doc/extend.texi (Statement Exprs): Add note about taking
        addresses of labels inside of statement expressions.

        * gcc.c-torture/compile/pr17913.c (f): Add dg-error.
        * gcc.dg/pr51088.c: New test.
So this seems OK if you just want to warn when we take the address of the label at function scope, but don't you want to warn when the depth of the address taken operation is different (lower) then the depth of the when the label is defined?

Am I missing something here?

jeff

Reply via email to