From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from spam02.hesby.net (spam01.hesby.net [81.29.32.152]) by sourceware.org (Postfix) with ESMTP id 771A9386001F for ; Mon, 31 Jan 2022 16:50:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 771A9386001F Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=hesbynett.no Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=hesbynett.no Received: from [192.168.0.63] (unknown [79.161.10.130]) by spam02.hesby.net (Halon) with ESMTPSA id db4628a5-82b5-11ec-8d7a-506b8dfa0e58; Mon, 31 Jan 2022 17:50:12 +0100 (CET) Subject: Re: Prohibit use of break/continue in a statement To: Hirrolot , gcc-help@gcc.gnu.org References: From: David Brown Message-ID: <07c3e8cb-458e-2ec9-a0fd-0ee3cbeaf8d8@hesbynett.no> Date: Mon, 31 Jan 2022 17:50:12 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-GB Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-3033.0 required=5.0 tests=BAYES_00, KAM_DMARC_STATUS, NICE_REPLY_A, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-help@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-help mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 31 Jan 2022 16:50:16 -0000 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