public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Prohibit use of break/continue in a statement
@ 2022-01-31 15:54 Hirrolot
  2022-01-31 16:50 ` David Brown
  0 siblings, 1 reply; 6+ messages in thread
From: Hirrolot @ 2022-01-31 15:54 UTC (permalink / raw)
  To: gcc-help

I have a macro that expands to a for-loop. It is used as follows:

    MACRO(...) {
        // User code
    }

The for-loop is used to open a new scope with a new variable; thus,
`MACRO(...) { ... }` would be a proper C statement. The loop itself is
executed only once.

The problem is that if a user uses `break` or `continue` like this:

    while (i < 10) {
        MACRO(...) {
           break;
            // User code...
       }
    }

Then that `break` will apply to the for-loop generated by `MACRO`, not
to the outer while-loop. The same holds for `continue`. This is
unexpected behaviour, and I would like to prohibit the use of the
`break`/`continue` statements in a user statement placed after
`MACRO`. Ideally, this should somehow trigger a compilation
error/warning.

It should be clarified that `MACRO` is not of a loop itself: it is not
a for-each macro or something like this; using that for-loop is just
an implementation detail. I would be also happy with getting rid of
that for-loop but I have no idea how. If I just generate a variable
like this:

    #define MACRO(...) int x; /* Some other stuff */

Then `MACRO(...) { ... }` will no longer be a single C statement.
Moreover, two occurrences of `MACRO` calls in a single scope will
result in a compilation error, which is also quite unfortunate.

Is it possible to trigger a compilation warning/error for
`break`/`continue` with GCC?

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

* Re: Prohibit use of break/continue in a statement
  2022-01-31 15:54 Prohibit use of break/continue in a statement Hirrolot
@ 2022-01-31 16:50 ` David Brown
       [not found]   ` <CA+g-_moR9HXz3rLH6WEeAEq1DXHhyAS9-8DxwLdvNNXzAUGJCg@mail.gmail.com>
  0 siblings, 1 reply; 6+ messages in thread
From: David Brown @ 2022-01-31 16:50 UTC (permalink / raw)
  To: Hirrolot, gcc-help

On 31/01/2022 16:54, Hirrolot via Gcc-help wrote:
> I have a macro that expands to a for-loop. It is used as follows:
> 
>     MACRO(...) {
>         // User code
>     }
> 
> The for-loop is used to open a new scope with a new variable; thus,
> `MACRO(...) { ... }` would be a proper C statement. The loop itself is
> executed only once.
> 
> The problem is that if a user uses `break` or `continue` like this:
> 
>     while (i < 10) {
>         MACRO(...) {
>            break;
>             // User code...
>        }
>     }
> 
> Then that `break` will apply to the for-loop generated by `MACRO`, not
> to the outer while-loop. The same holds for `continue`. This is
> unexpected behaviour, and I would like to prohibit the use of the
> `break`/`continue` statements in a user statement placed after
> `MACRO`. Ideally, this should somehow trigger a compilation
> error/warning.
> 
> It should be clarified that `MACRO` is not of a loop itself: it is not
> a for-each macro or something like this; using that for-loop is just
> an implementation detail. I would be also happy with getting rid of
> that for-loop but I have no idea how. If I just generate a variable
> like this:
> 
>     #define MACRO(...) int x; /* Some other stuff */
> 
> Then `MACRO(...) { ... }` will no longer be a single C statement.
> Moreover, two occurrences of `MACRO` calls in a single scope will
> result in a compilation error, which is also quite unfortunate.
> 
> Is it possible to trigger a compilation warning/error for
> `break`/`continue` with GCC?
> 

The only thing I can think of is:

#define break DON'T USE BREAK HERE
#define continue DON'T USE CONTINUE HERE

That should cause a compilation error if "break" or "continue" are used.
 Of course, you need to use the #define and a matching #undef in the
places MACRO is used.  And it's undefined behaviour to use keywords as
macro names, so other bad things might happen.

A far better idea would be to explain what you are trying to achieve
with your MACRO, and show your current definition.  Then people could
perhaps give you useful advice!

David



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

* Fwd: Prohibit use of break/continue in a statement
       [not found]   ` <CA+g-_moR9HXz3rLH6WEeAEq1DXHhyAS9-8DxwLdvNNXzAUGJCg@mail.gmail.com>
@ 2022-01-31 17:00     ` Hirrolot
  2022-01-31 17:11       ` Al K
  0 siblings, 1 reply; 6+ messages in thread
From: Hirrolot @ 2022-01-31 17:00 UTC (permalink / raw)
  To: gcc-help

---------- Forwarded message ---------
From: Hirrolot <hirrolot@gmail.com>
Date: Mon, Jan 31, 2022 at 10:59 PM
Subject: Re: Prohibit use of break/continue in a statement
To: David Brown <david.brown@hesbynett.no>


Ah, actually, the implementation is rather involved. I tried to
explain it in my blog post [1]. The section "Pattern matching" should
give you a clear sense of what is happening.

Yes, `#define` directives for `break` and `continue` should work, but
defining and undefining them every time would be a true hassle.

[1] https://hirrolot.github.io/posts/compiling-algebraic-data-types-in-pure-c99.html

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

* Re: Prohibit use of break/continue in a statement
  2022-01-31 17:00     ` Fwd: " Hirrolot
@ 2022-01-31 17:11       ` Al K
  2022-01-31 18:44         ` Hirrolot
  0 siblings, 1 reply; 6+ messages in thread
From: Al K @ 2022-01-31 17:11 UTC (permalink / raw)
  To: Hirrolot; +Cc: gcc-help

Why not use statement expressions [1] ?

[1] https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html

On Mon, Jan 31, 2022 at 11:02 AM Hirrolot via Gcc-help <gcc-help@gcc.gnu.org>
wrote:

> ---------- Forwarded message ---------
> From: Hirrolot <hirrolot@gmail.com>
> Date: Mon, Jan 31, 2022 at 10:59 PM
> Subject: Re: Prohibit use of break/continue in a statement
> To: David Brown <david.brown@hesbynett.no>
>
>
> Ah, actually, the implementation is rather involved. I tried to
> explain it in my blog post [1]. The section "Pattern matching" should
> give you a clear sense of what is happening.
>
> Yes, `#define` directives for `break` and `continue` should work, but
> defining and undefining them every time would be a true hassle.
>
> [1]
> https://hirrolot.github.io/posts/compiling-algebraic-data-types-in-pure-c99.html
>

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

* Re: Prohibit use of break/continue in a statement
  2022-01-31 17:11       ` Al K
@ 2022-01-31 18:44         ` Hirrolot
  2022-02-01  8:53           ` Al K
  0 siblings, 1 reply; 6+ messages in thread
From: Hirrolot @ 2022-01-31 18:44 UTC (permalink / raw)
  To: Al K; +Cc: gcc-help

How statement expressions can help? The documentation says
`break`/`continue` are allowed in statement expressions, if I
understand it correctly.

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

* Re: Prohibit use of break/continue in a statement
  2022-01-31 18:44         ` Hirrolot
@ 2022-02-01  8:53           ` Al K
  0 siblings, 0 replies; 6+ messages in thread
From: Al K @ 2022-02-01  8:53 UTC (permalink / raw)
  To: Hirrolot; +Cc: gcc-help

You can use statement expression to get rid of the for-loop within MACRO(),
since it'll provide a new
scope for variables you define within the statement expr, i.e. instead of
`for (...) { /* code */ }` use
`({ /* code */ })`.

Cheers,
Al

On Mon, Jan 31, 2022 at 12:44 PM Hirrolot <hirrolot@gmail.com> wrote:

> How statement expressions can help? The documentation says
> `break`/`continue` are allowed in statement expressions, if I
> understand it correctly.
>

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

end of thread, other threads:[~2022-02-01  8:53 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-31 15:54 Prohibit use of break/continue in a statement Hirrolot
2022-01-31 16:50 ` David Brown
     [not found]   ` <CA+g-_moR9HXz3rLH6WEeAEq1DXHhyAS9-8DxwLdvNNXzAUGJCg@mail.gmail.com>
2022-01-31 17:00     ` Fwd: " Hirrolot
2022-01-31 17:11       ` Al K
2022-01-31 18:44         ` Hirrolot
2022-02-01  8:53           ` Al K

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