public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "egallager at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/105401] New: Improved diagnostics for code from "Labels as Values" documentation
Date: Wed, 27 Apr 2022 02:03:59 +0000	[thread overview]
Message-ID: <bug-105401-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105401

            Bug ID: 105401
           Summary: Improved diagnostics for code from "Labels as Values"
                    documentation
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Keywords: diagnostic, documentation
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: egallager at gcc dot gnu.org
  Target Milestone: ---

Continuing my GCC documentation readthrough, I'm now up to the description of
the "Labels as Values" extension:
https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html#Labels-as-Values

I pulled together this code from the examples on the page:

$ cat labels_as_values.c
void unsafe(void *);

static inline int asdf(void)
{
bar:
    void *ptr0;
    static void *ptr1 = &&bar;
    /* ... */
foo:
    ptr0 = &&foo;
    goto *ptr0;
    goto *ptr1;
    unsafe(ptr0);
    unsafe(ptr1);
    return (sizeof(ptr0) + sizeof(ptr1));
}

static inline int zxcv(int i)
{
foo:
    static void *array[] = { &&foo, &&bar, &&hack };
bar:
    goto *array[i];
hack:
    return sizeof(array[i]);
}

int qwerty(int i)
{
foo:
    static const int array[] = { &&foo - &&foo, &&bar - &&foo,
                                 &&hack - &&foo };
bar:
    goto *(&&foo + array[i]);
hack:
    return (zxcv(array[i]) + asdf());
}
$

Compiling the code with "-Wall -Wextra -Wc++-compat -Winline" produces no
warnings (-pedantic, on the other hand, produces plenty of warnings, but that's
to be expected, since this is an extension). To match this back to things the
documentation says:

1. "Note that this does not check whether the subscript is in bounds—array
indexing in C never does that." ...ok, but we have -Warray-bounds now; maybe
the documentation could be updated to mention that, or would -Warray-bounds
have to be updated to handle cases like this first?
2. "You may not use this mechanism to jump to code in a different function. If
you do that, totally unpredictable things happen. The best way to avoid this is
to store the label address only in automatic variables and never pass it as an
argument." ...ok well I store label addresses in non-automatic variables, and
pass them as arguments in my example code, and I don't get any warnings; could
those be added?
3. "The &&foo expressions for the same label might have different values if the
containing function is inlined or cloned. If a program relies on them being
always the same, __attribute__((__noinline__,__noclone__)) should be used to
prevent inlining and cloning. If &&foo is used in a static variable
initializer, inlining and cloning is forbidden." ...ok, so shouldn't -Winline
say something about my example code, then? But, it doesn't...

             reply	other threads:[~2022-04-27  2:03 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-27  2:03 egallager at gcc dot gnu.org [this message]
2022-04-27 11:58 ` [Bug c/105401] " egallager at gcc dot gnu.org
2024-01-03  4:24 ` egallager at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-105401-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).