Bug 105401 - Improved diagnostics for code from "Labels as Values" documentation
Summary: Improved diagnostics for code from "Labels as Values" documentation
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 12.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic, documentation
Depends on:
Blocks:
 
Reported: 2022-04-27 02:03 UTC by Eric Gallager
Modified: 2024-01-03 04:24 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Eric Gallager 2022-04-27 02:03:59 UTC
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...
Comment 1 Eric Gallager 2022-04-27 11:58:43 UTC
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.
Comment 2 Eric Gallager 2024-01-03 04:24:38 UTC
putting the words "computed gotos" here for easier searchability