Skip to contentSkip to conversation

staging: comedi: rewrite macro function with GNU extension typeof

PATCH In review Jules Irenge 3 participants 6 messages Nov 4, 2019 – Nov 5, 2019 Staging

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 staging: comedi: rewrite macro function with GNU extension typeof

Rewrite macro function with the GNU extension typeof to remove a possible side-effects of MACRO argument reuse "x".
- Problem could rise if arguments have different types
and different use though.

---
v1 - had no full commit log message, with changes not intended to be in the patch v2 - remove some changes not intended to be in this driver
include note of a potential problem

 drivers/staging/comedi/comedi.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/staging/comedi/comedi.h +4 -2
index 09a940066c0e..a57691a2e8d8 100644
@@ -1103,8 +1103,10 @@ enum ni_common_signal_names {
 
 /* *** END GLOBALLY-NAMED NI TERMINALS/SIGNALS *** */
 
-#define NI_USUAL_PFI_SELECT(x)	(((x) < 10) ? (0x1 + (x)) : (0xb + (x)))
-#define NI_USUAL_RTSI_SELECT(x)	(((x) < 7) ? (0xb + (x)) : 0x1b)
+#define NI_USUAL_PFI_SELECT(x)\
+	({typeof(x) x_ = (x); (x_ < 10) ? (0x1 + x_) : (0xb + x_); })
+#define NI_USUAL_RTSI_SELECT(x)\
+	({typeof(x) x_ = (x); (x_ < 7) ? (0xb + x_) : 0x1b; })
 
 /*
  * mode bits for NI general-purpose counters, set with
Signature · 1 line
2.23.0
maintainer
#1 lore raw
Quoted 4 lines · Jules Irenge

Rewrite macro function with the GNU extension typeof to remove a possible side-effects of MACRO argument reuse "x".
- Problem could rise if arguments have different types
and different use though.

You can not just get away with a potential problem by documenting it :)

You might have just broken this. Why are you trying to "fix" something that is not broken?

What is wrong with the code as-is?

thanks,

greg k-h

#2 lore raw
Quoted 8 lines · Greg KH
Quoted 4 lines · Jules Irenge

Rewrite macro function with the GNU extension typeof to remove a possible side-effects of MACRO argument reuse "x".
- Problem could rise if arguments have different types
and different use though.

You can not just get away with a potential problem by documenting it :)

Quoted 9 lines

You might have just broken this. Why are you trying to "fix" something that is not broken?

What is wrong with the code as-is?

thanks,

greg k-h

Again I made a lot of mistake. I will be more carefull. Thanks for the feedback

#3 lore raw
In reply to Jules Irenge
Quoted 21 lines · Jules Irenge

Rewrite macro function with the GNU extension typeof to remove a possible side-effects of MACRO argument reuse "x".
- Problem could rise if arguments have different types
and different use though.

---
v1 - had no full commit log message, with changes not intended to be in the patch v2 - remove some changes not intended to be in this driver
include note of a potential problem

  drivers/staging/comedi/comedi.h | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
drivers/staging/comedi/comedi.h +0 -0
index 09a940066c0e..a57691a2e8d8 100644
@@ -1103,8 +1103,10 @@ enum ni_common_signal_names {
 
  /* *** END GLOBALLY-NAMED NI TERMINALS/SIGNALS *** */

drivers/staging/comedi/comedi.h enum ni_common_signal_names { · line 1112

Quoted 10 lines
drivers/staging/comedi/comedi.h +4 -2
 
-#define NI_USUAL_PFI_SELECT(x)	(((x) < 10) ? (0x1 + (x)) : (0xb + (x)))
-#define NI_USUAL_RTSI_SELECT(x)	(((x) < 7) ? (0xb + (x)) : 0x1b)
+#define NI_USUAL_PFI_SELECT(x)\
+	({typeof(x) x_ = (x); (x_ < 10) ? (0x1 + x_) : (0xb + x_); })
+#define NI_USUAL_RTSI_SELECT(x)\
+	({typeof(x) x_ = (x); (x_ < 7) ? (0xb + x_) : 0x1b; })
 
  /*
   * mode bits for NI general-purpose counters, set with

I wasn't sure about this the first time around due to the use of GNU extensions in uapi header files, but I see there are a few, rare instances of this GNU extension elsewhere in other uapi headers (mainly in netfilter stuff), so I guess it's OK. However, it does mean that user code that uses these macros will no longer compile unless GNU extensions are enabled.

Does anyone know any "best practices" regarding use of GNU extensions in user header files under Linux?

Signature · 4 lines
-=( Ian Abbott <[email protected]> || Web: www.mev.co.uk )=-
-=( MEV Ltd. is a company registered in England & Wales. )=-
-=( Registered number: 02862268.  Registered address:    )=-
-=( 15 West Park Road, Bramhall, STOCKPORT, SK7 3JZ, UK. )=-
maintainer
#4 lore raw
Quoted 36 lines · Ian Abbott
Quoted 18 lines · Jules Irenge

Rewrite macro function with the GNU extension typeof to remove a possible side-effects of MACRO argument reuse "x".
- Problem could rise if arguments have different types
and different use though.

---
v1 - had no full commit log message, with changes not intended to be in the patch v2 - remove some changes not intended to be in this driver
include note of a potential problem

  drivers/staging/comedi/comedi.h | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
drivers/staging/comedi/comedi.h +0 -0
index 09a940066c0e..a57691a2e8d8 100644

drivers/staging/comedi/comedi.h enum ni_common_signal_names { · line 1110

Quoted 10 lines
drivers/staging/comedi/comedi.h +4 -2
@@ -1103,8 +1103,10 @@ enum ni_common_signal_names {
  /* *** END GLOBALLY-NAMED NI TERMINALS/SIGNALS *** */
-#define NI_USUAL_PFI_SELECT(x)	(((x) < 10) ? (0x1 + (x)) : (0xb + (x)))
-#define NI_USUAL_RTSI_SELECT(x)	(((x) < 7) ? (0xb + (x)) : 0x1b)
+#define NI_USUAL_PFI_SELECT(x)\
+	({typeof(x) x_ = (x); (x_ < 10) ? (0x1 + x_) : (0xb + x_); })
+#define NI_USUAL_RTSI_SELECT(x)\
+	({typeof(x) x_ = (x); (x_ < 7) ? (0xb + x_) : 0x1b; })
  /*
   * mode bits for NI general-purpose counters, set with
 

I wasn't sure about this the first time around due to the use of GNU extensions in uapi header files, but I see there are a few, rare instances of this GNU extension elsewhere in other uapi headers (mainly in netfilter stuff), so I guess it's OK. However, it does mean that user code that uses these macros will no longer compile unless GNU extensions are enabled.

Quoted 2 lines

Does anyone know any "best practices" regarding use of GNU extensions in user header files under Linux?

We try to never do it if at all possible :)

thanks,

greg k-h

#5 lore raw
In reply to Ian Abbott
Quoted 42 lines · Ian Abbott
Quoted 32 lines · Jules Irenge

Rewrite macro function with the GNU extension typeof to remove a possible side-effects of MACRO argument reuse "x".
- Problem could rise if arguments have different types
and different use though.

---
v1 - had no full commit log message, with changes not intended to be in the patch
v2 - remove some changes not intended to be in this driver
include note of a potential problem

  drivers/staging/comedi/comedi.h | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
drivers/staging/comedi/comedi.h +4 -2
index 09a940066c0e..a57691a2e8d8 100644
@@ -1103,8 +1103,10 @@ enum ni_common_signal_names {
 
  /* *** END GLOBALLY-NAMED NI TERMINALS/SIGNALS *** */
 
-#define NI_USUAL_PFI_SELECT(x)	(((x) < 10) ? (0x1 + (x)) : (0xb +
 (x)))
-#define NI_USUAL_RTSI_SELECT(x)	(((x) < 7) ? (0xb + (x)) : 0x1b)
+#define NI_USUAL_PFI_SELECT(x)\
+	({typeof(x) x_ = (x); (x_ < 10) ? (0x1 + x_) : (0xb + x_); })
+#define NI_USUAL_RTSI_SELECT(x)\
+	({typeof(x) x_ = (x); (x_ < 7) ? (0xb + x_) : 0x1b; })
 
  /*
Quoted 2 lines

* mode bits for NI general-purpose counters, set with

I wasn't sure about this the first time around due to the use of GNU extensions in uapi header files, but I see there are a few, rare instances of this GNU extension elsewhere in other uapi headers (mainly in netfilter stuff), so I guess it's OK. However, it does mean that user code that uses these macros will no longer compile unless GNU extensions are enabled.

Quoted 10 lines

Does anyone know any "best practices" regarding use of GNU extensions in user header files under Linux?

Signature · 4 lines
-=( Ian Abbott <[email protected]> || Web: www.mev.co.uk )=-
-=( MEV Ltd. is a company registered in England & Wales. )=-
-=( Registered number: 02862268.  Registered address:    )=-
-=( 15 West Park Road, Bramhall, STOCKPORT, SK7 3JZ, UK. )=-

Apology, I misinterpreted the comments and send a second version without thinking much.
Please discard it unless you wish to try out.
Any way thanks for the feedback, I really appreciate I find it educative. Kind regards,
Jules