[PATCH] kernel.h: Add for_each_if()

Andrew Morton akpm at linux-foundation.org
Mon Jul 9 23:30:01 UTC 2018


On Mon,  9 Jul 2018 18:25:09 +0200 Daniel Vetter <daniel.vetter at ffwll.ch> wrote:

> To avoid compilers complainig about ambigious else blocks when putting
> an if condition into a for_each macro one needs to invert the
> condition and add a dummy else. We have a nice little convenience
> macro for that in drm headers, let's move it out. Subsequent patches
> will roll it out to other places.
> 
> The issue the compilers complain about are nested if with an else
> block and no {} to disambiguate which if the else belongs to. The C
> standard is clear, but in practice people forget:
> 
> if (foo)
> 	if (bar)
> 		/* something */
> 	else
> 		/* something else

um, yeah, don't do that.  Kernel coding style is very much to do

	if (foo) {
		if (bar)
			/* something */
		else
			/* something else
	}

And if not doing that generates a warning then, well, do that.

> The same can happen in a for_each macro when it also contains an if
> condition at the end, except the compiler message is now really
> confusing since there's only 1 if:
> 
> for_each_something()
> 	if (bar)
> 		/* something */
> 	else
> 		/* something else
> 
> The for_each_if() macro, by inverting the condition and adding an
> else, avoids the compiler warning.

Ditto.

> Motivated by a discussion with Andy and Yisheng, who want to add
> another for_each_macro which would benefit from for_each_if() instead
> of hand-rolling it.

Ditto.

> v2: Explain a bit better what this is good for, after the discussion
> with Peter Z.

Presumably the above was discussed in whatever-thread-that-was.



More information about the dri-devel mailing list