Skip to contentSkip to conversation

minmax: substitute local variables using __UNIQUE_ID()

PATCH Superseded Yueh-Shun Li 2 participants 2 messages Feb 15, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 minmax: substitute local variables using __UNIQUE_ID()

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.

---
 include/linux/minmax.h | 41 ++++++++++++++++++++++++++++-------------
 1 file changed, 28 insertions(+), 13 deletions(-)
include/linux/minmax.h +28 -13
index 2ec559284a9f..70b740b76f73 100644
@@ -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 */
Signature · 1 line
2.42.0
#1 lore raw
Quoted 8 lines · Yueh-Shun Li

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.

...

include/linux/minmax.h line 134

Quoted 3 lines
include/linux/minmax.h +3 -0
+#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.

Signature · 2 lines
With Best Regards,
Andy Shevchenko