Re: [RFC PATCH 05/13] list.h: Fix parentheses around macro pointer parameter use

From: Mathieu Desnoyers
Date: Fri May 12 2023 - 10:32:31 EST


On 2023-05-12 07:02, Andy Shevchenko wrote:
On Mon, May 08, 2023 at 09:46:40AM -0400, Mathieu Desnoyers wrote:
On 2023-05-08 08:16, Andy Shevchenko wrote:

...

The only use I found that would break is as follows:

LIST_HEAD(testlist);

int f2(void)
{
return 1;
}

#define eval(...) __VA_ARGS__

void f(void)
{
struct list_head *pos;

list_for_each(pos, eval(f2(), &testlist)) {
//...
}
}

Because "eval()" will evaluate "f(), &testlist" with comma and all, without
enclosing parentheses.

So the question is: do we want to support this kind-of-odd macro evaluation,
considering that it requires adding parentheses around pretty much all macro
parameters when used as expressions between commas?

Similar question can be asked for your initial motivation to support indirect
pointers. I found the double pointer as weird as this macro case. But it can be
only me. Hence I left this to the more experienced developers to express their
opinions.


The main motivation behind my changes is to make macro code consistent, and to eliminate classes of issues that can arise from unexpected operator precedence around use of macro arguments that lack parentheses.

The examples I provide in the commit messages are just instances showing how the lack of parentheses can lead to unexpected effects due to operator precedence.

My end goal is not to "support" specific use-cases. My goal is to eliminate inconsistency, increase robustness of the kernel macros, and lessen the cognitive burden that comes with using and maintaining those macros.

I hope that we can spend less time figuring out operator precedence corner-cases and more brain power thinking about and documenting things that really matter like memory barriers and synchronization.

Thanks,

Mathieu


--
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com