Re: [PATCH v2 0/2] Lock and Pointer guards

Peter Zijlstra
2023-06-06T09:43:34Z

On Tue, May 30, 2023 at 11:23:42AM +0200, Peter Zijlstra wrote:

> Yes, it's a wee bit more involved, but I'm thinking it gives a fair
> amount of flexibility and we don't need to ret rid of
> -Wdeclaration-after-statement.

So I made all that work and .. Yes, you're absolutely right.

Busting -Wdeclaration-after-statement is the right thing to do for
guards.

So then I came up with:

#define __ptr_guard(_guard, _name) \
guard_##_guard##_t _name __cleanup(guard_##_guard##_cleanup)

#define ptr_guard(_guard, _name) \
__diag(push) \
__diag(ignored "-Wdeclaration-after-statement") \
__ptr_guard(_guard, _name) \
__diag(pop)

#define guard_init(_guard, _var...) \
guard_##_guard##_init(_var)

#define named_guard(_guard, _name, _var...) \
ptr_guard(_guard, _name) = guard_init(_guard, _var)

#define guard(_guard, _var...) \
named_guard(_guard, __UNIQUE_ID(guard), _var)

#define scoped_guard(_guard, _var...) \
for (__ptr_guard(_guard, scope) = guard_init(_guard, _var), \
*done = NULL; !done; done = (void *)1)

And that all (mostly) works on clang, but not GCC :-( GCC refuses to
accept _Pragma() inside an expression.

So I now have that ptr_guard() with push/pop for clang but without for
GCC, which means that only clang has a fighting chance to report
-Wdeclaration-after-statement warns until such a time as that we can get
GCC 'fixed'.

https://godbolt.org/z/5MPeq5W6K

FWIW: the work-in-progress patches I have are here: