All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] minmax: substitute local variables using __UNIQUE_ID()
@ 2024-02-15 16:07 Yueh-Shun Li
  2024-02-15 16:40 ` Andy Shevchenko
  0 siblings, 1 reply; 2+ messages in thread
From: Yueh-Shun Li @ 2024-02-15 16:07 UTC (permalink / raw)
  To: Andrew Morton, Mark Brown
  Cc: Yueh-Shun Li, Andy Shevchenko, Herve Codina, Christophe Leroy,
	linux-kernel

Substitute identifier names of local variables used in macro
definitions inside minmax.h with those generated by __UNIQUE_ID(prefix)
to eliminate passible naming collisions.

Identifier names like __x, __y and __tmp are everywhere inside the
kernel source. This patch ensures that macros provided by minmax.h
will work even when identifiers of these names appear in the expanded
input arguments.

Signed-off-by: Yueh-Shun Li <shamrocklee@posteo.net>
---
 include/linux/minmax.h | 41 ++++++++++++++++++++++++++++-------------
 1 file changed, 28 insertions(+), 13 deletions(-)

diff --git a/include/linux/minmax.h b/include/linux/minmax.h
index 2ec559284a9f..70b740b76f73 100644
--- a/include/linux/minmax.h
+++ b/include/linux/minmax.h
@@ -129,10 +129,14 @@
  * @x: value1
  * @y: value2
  */
-#define min_not_zero(x, y) ({			\
-	typeof(x) __x = (x);			\
-	typeof(y) __y = (y);			\
-	__x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
+#define min_not_zero(x, y) \
+	__min_not_zero_impl(x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y))
+#define __min_not_zero_impl(x, y, __x, __y)                          \
+	({                                                           \
+		typeof(x) __x = (x);                                 \
+		typeof(y) __y = (y);                                 \
+		__x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); \
+	})
 
 /**
  * clamp - return a value clamped to a given range with strict typechecking
@@ -185,13 +189,19 @@
  * typeof() keeps the const qualifier. Use __unqual_scalar_typeof() in order
  * to discard the const qualifier for the __element variable.
  */
-#define __minmax_array(op, array, len) ({				\
-	typeof(&(array)[0]) __array = (array);				\
-	typeof(len) __len = (len);					\
-	__unqual_scalar_typeof(__array[0]) __element = __array[--__len];\
-	while (__len--)							\
-		__element = op(__element, __array[__len]);		\
-	__element; })
+#define __minmax_array(op, array, len)                            \
+	__minmax_array_impl(op, array, len, __UNIQUE_ID(__array), \
+			    __UNIQUE_ID(__len), __UNIQUE_ID(__element))
+#define __minmax_array_impl(op, array, len, __array, __len, __element) \
+	({                                                             \
+		typeof(&(array)[0]) __array = (array);                 \
+		typeof(len) __len = (len);                             \
+		__unqual_scalar_typeof(__array[0])                     \
+			__element = __array[--__len];                  \
+		while (__len--)                                        \
+			__element = op(__element, __array[__len]);     \
+		__element;                                             \
+	})
 
 /**
  * min_array - return minimum of values present in an array
@@ -267,7 +277,12 @@ static inline bool in_range32(u32 val, u32 start, u32 len)
  * @a: first value
  * @b: second value
  */
-#define swap(a, b) \
-	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
+#define swap(a, b) __swap_impl(a, b, __UNIQUE_ID(__tmp))
+#define __swap_impl(a, b, __tmp)       \
+	do {                           \
+		typeof(a) __tmp = (a); \
+		(a) = (b);             \
+		(b) = __tmp;           \
+	} while (0)
 
 #endif	/* _LINUX_MINMAX_H */
-- 
2.42.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] minmax: substitute local variables using __UNIQUE_ID()
  2024-02-15 16:07 [PATCH] minmax: substitute local variables using __UNIQUE_ID() Yueh-Shun Li
@ 2024-02-15 16:40 ` Andy Shevchenko
  0 siblings, 0 replies; 2+ messages in thread
From: Andy Shevchenko @ 2024-02-15 16:40 UTC (permalink / raw)
  To: Yueh-Shun Li
  Cc: Andrew Morton, Mark Brown, Herve Codina, Christophe Leroy, linux-kernel

On Thu, Feb 15, 2024 at 04:07:21PM +0000, Yueh-Shun Li wrote:
> Substitute identifier names of local variables used in macro
> definitions inside minmax.h with those generated by __UNIQUE_ID(prefix)
> to eliminate passible naming collisions.
> 
> Identifier names like __x, __y and __tmp are everywhere inside the
> kernel source. This patch ensures that macros provided by minmax.h
> will work even when identifiers of these names appear in the expanded
> input arguments.

...

> +#define min_not_zero(x, y) \
> +	__min_not_zero_impl(x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y))
> +#define __min_not_zero_impl(x, y, __x, __y)                          \

Seems like the back slashes are indented at random.

Please, use TABs and make sure in a macro / group of (semantically related)
macros they are consistently occupy _the same_ column.

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-02-15 16:41 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-15 16:07 [PATCH] minmax: substitute local variables using __UNIQUE_ID() Yueh-Shun Li
2024-02-15 16:40 ` Andy Shevchenko

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.