|
|
Log in / Subscribe / Register

Moving the kernel to modern C

Moving the kernel to modern C

Posted Feb 24, 2022 15:12 UTC (Thu) by ballombe (subscriber, #9523)
Parent article: Moving the kernel to modern C

Note that, if for some reason you need to stay with c89, you can always add a block around the for() statement to hold the loop variable.


to post comments

Moving the kernel to modern C

Posted Feb 24, 2022 15:51 UTC (Thu) by smurf (subscriber, #17840) [Link] (7 responses)

You'd need to do that to each caller, which is a *lot* of code churn.

Moving the kernel to modern C

Posted Feb 25, 2022 3:08 UTC (Fri) by kmeyer (subscriber, #50720) [Link] (5 responses)

You would just do it once, in the macro definition.

Moving the kernel to modern C

Posted Feb 25, 2022 6:24 UTC (Fri) by NYKevin (subscriber, #129325) [Link] (1 responses)

Based on the code shown in this article, I'm not clear on how you would actually do that. It *looks* like the macro expands to while(something) or for(something), and doesn't even have the "regular" set of braces, so you can't edit it to include an outer set of braces without modifying the call sites.

Moving the kernel to modern C

Posted Feb 25, 2022 7:08 UTC (Fri) by josh (subscriber, #17465) [Link]

I don't think you could with Linux's list macros. You could with Sparse's, which have pairs of macros invoked at the start and end of each loop (and which use two levels of braces).

But C99 makes this easy.

Moving the kernel to modern C

Posted Feb 25, 2022 11:33 UTC (Fri) by 4m1rk (guest, #157085) [Link] (2 responses)

The macro just creates the `for` expression. If you need to put the for expression inside a block then the macro needs to accept the body of the for too. Do C macros allow passing a code block?

Moving the kernel to modern C

Posted Feb 27, 2022 16:24 UTC (Sun) by mina86 (guest, #68442) [Link] (1 responses)

> Do C macros allow passing a code block?

Yes. You can pass pretty much whatever you want so long as parenthesise match and there is no comma outside of parenthesise.

Moving the kernel to modern C

Posted Mar 9, 2022 23:56 UTC (Wed) by bartoc (guest, #124262) [Link]

The comma is a bit of a huge pita if you're passing a whole loop body.

Moving the kernel to modern C

Posted Feb 27, 2022 23:59 UTC (Sun) by Sesse (subscriber, #53779) [Link]

#define for if (false); else for

:-)


Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds