public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/105401] New: Improved diagnostics for code from "Labels as Values" documentation
@ 2022-04-27  2:03 egallager at gcc dot gnu.org
  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
  0 siblings, 2 replies; 3+ messages in thread
From: egallager at gcc dot gnu.org @ 2022-04-27  2:03 UTC (permalink / raw)
  To: gcc-bugs

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...

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

* [Bug c/105401] Improved diagnostics for code from "Labels as Values" documentation
  2022-04-27  2:03 [Bug c/105401] New: Improved diagnostics for code from "Labels as Values" documentation egallager at gcc dot gnu.org
@ 2022-04-27 11:58 ` egallager at gcc dot gnu.org
  2024-01-03  4:24 ` egallager at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: egallager at gcc dot gnu.org @ 2022-04-27 11:58 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Eric Gallager <egallager at gcc dot gnu.org> ---
Actually never mind about point 3: I do actually get warnings from -Winline
when I turn on optimizations:

$ /usr/local/bin/gcc -c -Wall -Wextra -Wc++-compat -Winline -O2
labels_as_values.c
labels_as_values.c: In function 'asdf':
labels_as_values.c:3:19: warning: function 'asdf' can never be copied because
it saves address of local label in a static variable [-Winline]
    3 | static inline int asdf(void)
      |                   ^~~~
labels_as_values.c: In function 'zxcv':
labels_as_values.c:18:19: warning: function 'zxcv' can never be copied because
it saves address of local label in a static variable [-Winline]
   18 | static inline int zxcv(int i)
      |                   ^~~~
labels_as_values.c: In function 'qwerty':
labels_as_values.c:3:19: warning: inlining failed in call to 'asdf': function
not inlinable [-Winline]
    3 | static inline int asdf(void)
      |                   ^~~~
labels_as_values.c:36:30: note: called from here
   36 |     return (zxcv(array[i]) + asdf());
      |                              ^~~~~~
labels_as_values.c:18:19: warning: inlining failed in call to 'zxcv': function
not inlinable [-Winline]
   18 | static inline int zxcv(int i)
      |                   ^~~~
labels_as_values.c:36:13: note: called from here
   36 |     return (zxcv(array[i]) + asdf());
      |             ^~~~~~~~~~~~~~
$

Points 1 and 2 still stand, though.

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

* [Bug c/105401] Improved diagnostics for code from "Labels as Values" documentation
  2022-04-27  2:03 [Bug c/105401] New: Improved diagnostics for code from "Labels as Values" documentation egallager at gcc dot gnu.org
  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
  1 sibling, 0 replies; 3+ messages in thread
From: egallager at gcc dot gnu.org @ 2024-01-03  4:24 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Eric Gallager <egallager at gcc dot gnu.org> ---
putting the words "computed gotos" here for easier searchability

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

end of thread, other threads:[~2024-01-03  4:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-27  2:03 [Bug c/105401] New: Improved diagnostics for code from "Labels as Values" documentation egallager at gcc dot gnu.org
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

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).