Skip to contentSkip to conversation

fixed a macro coding style issue

PATCH In review Baodong Chen 4 participants 7 messages Jul 25, 2012 – Jul 31, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fixed a macro coding style issue

Fixed a coding style issue in driver/input/input.c

---
 drivers/input/input.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
drivers/input/input.c +4 -2
index 8921c61..c96e983 100644
@@ -845,11 +845,13 @@ int input_set_keycode(struct input_dev *dev,
 EXPORT_SYMBOL(input_set_keycode);
 
 #define MATCH_BIT(bit, max) \
+	do { \
 		for (i = 0; i < BITS_TO_LONGS(max); i++) \
 			if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
 				break; \
-		if (i != BITS_TO_LONGS(max)) \
-			continue;
+			if (i != BITS_TO_LONGS(max)) \
+				continue; \
+	} while (0)
 
 static const struct input_device_id *input_match_device(struct input_handler *handler,
 							struct input_dev *dev)
Signature · 1 line
1.7.0.4
#1 lore raw
Quoted 22 lines

-----Original Message-----
From: [email protected] [mailto:linux-kernel- [email protected]] On Behalf Of Baodong Chen
Sent: Wednesday, July 25, 2012 10:51 AM
To: [email protected]
Cc: [email protected]; [email protected]; Baodong Chen
Subject: [PATCH] fixed a macro coding style issue

Fixed a coding style issue in driver/input/input.c

---
 drivers/input/input.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
drivers/input/input.c +0 -0
index 8921c61..c96e983 100644
@@ -845,11 +845,13 @@ int input_set_keycode(struct input_dev *dev,
 EXPORT_SYMBOL(input_set_keycode);

drivers/input/input.c int input_set_keycode(struct input_dev *dev, · line 853

Quoted 10 lines
drivers/input/input.c +3 -2
 
 #define MATCH_BIT(bit, max) \
+	do { \
 		for (i = 0; i < BITS_TO_LONGS(max); i++) \
 			if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
 				break; \
-		if (i != BITS_TO_LONGS(max)) \
-			continue;
+			if (i != BITS_TO_LONGS(max)) \
+				continue; \

What is the need for extra indentation here?

Quoted 14 lines
drivers/input/input.c +1 -0
+	} while (0)
 
 static const struct input_device_id *input_match_device(struct
 input_handler *handler,

struct input_dev
*dev)
--
1.7.0.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

maintainer
#2 lore raw
In reply to Baodong Chen
Quoted 14 lines · Baodong Chen

Fixed a coding style issue in driver/input/input.c

---
 drivers/input/input.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
drivers/input/input.c +0 -0
index 8921c61..c96e983 100644
@@ -845,11 +845,13 @@ int input_set_keycode(struct input_dev *dev,
 EXPORT_SYMBOL(input_set_keycode);
 

drivers/input/input.c int input_set_keycode(struct input_dev *dev, · line 854

Quoted 10 lines
drivers/input/input.c +4 -2
 #define MATCH_BIT(bit, max) \
+	do { \
 		for (i = 0; i < BITS_TO_LONGS(max); i++) \
 			if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
 				break; \
-		if (i != BITS_TO_LONGS(max)) \
-			continue;
+			if (i != BITS_TO_LONGS(max)) \
+				continue; \
+	} while (0)

This changes semantics. While current implementation of MATCH_BIT might not be great style, it is not supposed to be used outside of input_match_device(), and the replacement is completely broken.

Thanks.

Signature · 1 line
Dmitry
#3 lore raw

this patch definitely changes semantics.
so please discard it, sorry about that!

Quoted 34 lines · Dmitry Torokhov
Quoted 14 lines · Baodong Chen

Fixed a coding style issue in driver/input/input.c

---
 drivers/input/input.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
drivers/input/input.c +0 -0
index 8921c61..c96e983 100644
@@ -845,11 +845,13 @@ int input_set_keycode(struct input_dev *dev,
 EXPORT_SYMBOL(input_set_keycode);
 

drivers/input/input.c int input_set_keycode(struct input_dev *dev, · line 854

Quoted 10 lines
drivers/input/input.c +4 -2
 #define MATCH_BIT(bit, max) \
+     do { \
              for (i = 0; i < BITS_TO_LONGS(max); i++) \
                      if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
                              break; \
-             if (i != BITS_TO_LONGS(max)) \
-                     continue;
+                     if (i != BITS_TO_LONGS(max)) \
+                             continue; \
+     } while (0)

This changes semantics. While current implementation of MATCH_BIT might not be great style, it is not supposed to be used outside of input_match_device(), and the replacement is completely broken.

Thanks.

--
Dmitry

#4 lore raw
In reply to Dmitry Torokhov
Quoted 15 lines · Dmitry Torokhov
Quoted 14 lines · Baodong Chen

Fixed a coding style issue in driver/input/input.c

---
 drivers/input/input.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
drivers/input/input.c +0 -0
index 8921c61..c96e983 100644
@@ -845,11 +845,13 @@ int input_set_keycode(struct input_dev *dev,
 EXPORT_SYMBOL(input_set_keycode);
 
Quoted 14 lines

drivers/input/input.c int input_set_keycode(struct input_dev *dev, · line 854

Quoted 10 lines
drivers/input/input.c +4 -2
 #define MATCH_BIT(bit, max) \
+	do { \
 		for (i = 0; i < BITS_TO_LONGS(max); i++) \
 			if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
 				break; \
-		if (i != BITS_TO_LONGS(max)) \
-			continue;
+			if (i != BITS_TO_LONGS(max)) \
+				continue; \
+	} while (0)

This changes semantics. While current implementation of MATCH_BIT might not be great style, it is not supposed to be used outside of input_match_device(), and the replacement is completely broken.

Replacement is certainly completely broken, but "might not be great style" is quite an understatement. Everything else aside, it's too obfuscated and ugly for anything outside of IOCCC and too verbose for IOCCC.

Damnit, at least turn that into
static inline int is_subset(unsigned long *bitmap1, unsigned long *bitmap2, size_t bits)

{
	int i;
	for (i = BITS_TO_LONGS(bits); i; bitmap1++, bitmap2++, i--)
		if ((*bitmap1 & *bitmap2) != *bitmap1)
			return 0;
	return 1;
}

and just before your loop
/* ignore ones that don't have bits required by id */
#define MATCH_BIT(array, bits) if (!is_subset(id->array, dev->array, bits)) continue; with #undef MATCH_BIT right after the loop, to make it damn clean that it's local. And frankly, I'd consider expanding that variant and killing the macro off. Macros that affect control flow are vile and actively confusing for reader.

maintainer
#5 lore raw
Quoted 44 lines · Al Viro
Quoted 15 lines · Dmitry Torokhov
Quoted 14 lines · Baodong Chen

Fixed a coding style issue in driver/input/input.c

---
 drivers/input/input.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
drivers/input/input.c +0 -0
index 8921c61..c96e983 100644
@@ -845,11 +845,13 @@ int input_set_keycode(struct input_dev *dev,
 EXPORT_SYMBOL(input_set_keycode);
 
Quoted 14 lines

drivers/input/input.c int input_set_keycode(struct input_dev *dev, · line 854

Quoted 10 lines
drivers/input/input.c +4 -2
 #define MATCH_BIT(bit, max) \
+	do { \
 		for (i = 0; i < BITS_TO_LONGS(max); i++) \
 			if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
 				break; \
-		if (i != BITS_TO_LONGS(max)) \
-			continue;
+			if (i != BITS_TO_LONGS(max)) \
+				continue; \
+	} while (0)

This changes semantics. While current implementation of MATCH_BIT might not be great style, it is not supposed to be used outside of input_match_device(), and the replacement is completely broken.

Replacement is certainly completely broken, but "might not be great style" is quite an understatement. Everything else aside, it's too obfuscated and ugly for anything outside of IOCCC and too verbose for IOCCC.

Damnit, at least turn that into
static inline int is_subset(unsigned long *bitmap1, unsigned long *bitmap2, size_t bits)

{
	int i;
	for (i = BITS_TO_LONGS(bits); i; bitmap1++, bitmap2++, i--)
		if ((*bitmap1 & *bitmap2) != *bitmap1)
			return 0;
	return 1;
}
Quoted 3 lines

and just before your loop
/* ignore ones that don't have bits required by id */
#define MATCH_BIT(array, bits) if (!is_subset(id->array, dev->array, bits)) continue;

Hmm, we already have bitmap_subset(), I think we can simply use it.

Quoted 3 lines

with #undef MATCH_BIT right after the loop, to make it damn clean that it's local. And frankly, I'd consider expanding that variant and killing the macro off. Macros that affect control flow are vile and actively confusing for reader.

I agree. In my defense it is quite old code...

Signature · 1 line
Dmitry
maintainer
#6 lore raw
Quoted 49 lines · Dmitry Torokhov
Quoted 44 lines · Al Viro
Quoted 15 lines · Dmitry Torokhov
Quoted 14 lines · Baodong Chen

Fixed a coding style issue in driver/input/input.c

---
 drivers/input/input.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
drivers/input/input.c +0 -0
index 8921c61..c96e983 100644
@@ -845,11 +845,13 @@ int input_set_keycode(struct input_dev *dev,
 EXPORT_SYMBOL(input_set_keycode);
 
Quoted 14 lines

drivers/input/input.c int input_set_keycode(struct input_dev *dev, · line 854

Quoted 10 lines
drivers/input/input.c +4 -2
 #define MATCH_BIT(bit, max) \
+	do { \
 		for (i = 0; i < BITS_TO_LONGS(max); i++) \
 			if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
 				break; \
-		if (i != BITS_TO_LONGS(max)) \
-			continue;
+			if (i != BITS_TO_LONGS(max)) \
+				continue; \
+	} while (0)

This changes semantics. While current implementation of MATCH_BIT might not be great style, it is not supposed to be used outside of input_match_device(), and the replacement is completely broken.

Replacement is certainly completely broken, but "might not be great style" is quite an understatement. Everything else aside, it's too obfuscated and ugly for anything outside of IOCCC and too verbose for IOCCC.

Damnit, at least turn that into
static inline int is_subset(unsigned long *bitmap1, unsigned long *bitmap2, size_t bits)

{
	int i;
	for (i = BITS_TO_LONGS(bits); i; bitmap1++, bitmap2++, i--)
		if ((*bitmap1 & *bitmap2) != *bitmap1)
			return 0;
	return 1;
}
Quoted 3 lines

and just before your loop
/* ignore ones that don't have bits required by id */
#define MATCH_BIT(array, bits) if (!is_subset(id->array, dev->array, bits)) continue;

Quoted 7 lines

Hmm, we already have bitmap_subset(), I think we can simply use it.

Quoted 3 lines

with #undef MATCH_BIT right after the loop, to make it damn clean that it's local. And frankly, I'd consider expanding that variant and killing the macro off. Macros that affect control flow are vile and actively confusing for reader.

I agree. In my defense it is quite old code...

So how about the patch below?

Signature · 15 lines
Dmitry

Input: get rid of MATCH_BIT() macro

From: Dmitry Torokhov <[email protected]>

MATCH_BIT() is ugly and stupid, we have much nicer bitmap_subset() which
does the same and does not hide control flow.

Reported-by: Baodong Chen <[email protected]>
Signed-off-by: Dmitry Torokhov <[email protected]>
---

 drivers/input/input.c |   43 ++++++++++++++++++++++++++-----------------
 1 file changed, 26 insertions(+), 17 deletions(-)
drivers/input/input.c +26 -17
index 8921c61..768e46b 100644
@@ -844,18 +844,10 @@ int input_set_keycode(struct input_dev *dev,
 }
 EXPORT_SYMBOL(input_set_keycode);
 
-#define MATCH_BIT(bit, max) \
-		for (i = 0; i < BITS_TO_LONGS(max); i++) \
-			if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
-				break; \
-		if (i != BITS_TO_LONGS(max)) \
-			continue;
-
 static const struct input_device_id *input_match_device(struct input_handler *handler,
 							struct input_dev *dev)
 {
 	const struct input_device_id *id;
-	int i;
 
 	for (id = handler->id_table; id->flags || id->driver_info; id++) {
 
@@ -875,15 +867,32 @@ static const struct input_device_id *input_match_device(struct input_handler *ha
 			if (id->version != dev->id.version)
 				continue;
 
-		MATCH_BIT(evbit,  EV_MAX);
-		MATCH_BIT(keybit, KEY_MAX);
-		MATCH_BIT(relbit, REL_MAX);
-		MATCH_BIT(absbit, ABS_MAX);
-		MATCH_BIT(mscbit, MSC_MAX);
-		MATCH_BIT(ledbit, LED_MAX);
-		MATCH_BIT(sndbit, SND_MAX);
-		MATCH_BIT(ffbit,  FF_MAX);
-		MATCH_BIT(swbit,  SW_MAX);
+		if (!bitmap_subset(id->evbit, dev->evbit, EV_MAX))
+			continue;
+
+		if (!bitmap_subset(id->keybit, dev->keybit, KEY_MAX))
+			continue;
+
+		if (!bitmap_subset(id->relbit, dev->relbit, REL_MAX))
+			continue;
+
+		if (!bitmap_subset(id->absbit, dev->absbit, ABS_MAX))
+			continue;
+
+		if (!bitmap_subset(id->mscbit, dev->mscbit, MSC_MAX))
+			continue;
+
+		if (!bitmap_subset(id->ledbit, dev->ledbit, LED_MAX))
+			continue;
+
+		if (!bitmap_subset(id->sndbit, dev->sndbit, SND_MAX))
+			continue;
+
+		if (!bitmap_subset(id->ffbit, dev->ffbit, FF_MAX))
+			continue;
+
+		if (!bitmap_subset(id->swbit, dev->swbit, SW_MAX))
+			continue;
 
 		if (!handler->match || handler->match(handler, dev))
 			return id;