Skip to contentSkip to conversation

ctype.h: remove duplicate isdigit() helper

PATCH Superseded Arnd Bergmann 2 participants 3 messages Oct 26, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ctype.h: remove duplicate isdigit() helper

From: Arnd Bergmann <[email protected]>

gcc warns a few thousand times about the isdigit() shadow:

include/linux/ctype.h:26:19: warning: declaration of 'isdigit' shadows a built-in function [-Wshadow]

As there is already a compiler builtin, just use that, and make it clear we do that by defining a macro. Unfortunately, clang does not have the isdigit() builtin, so this has to be conditional.

---
 include/linux/ctype.h | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
include/linux/ctype.h +12 -4
index 363b004426db..c407acb258c2 100644
@@ -23,10 +23,6 @@ extern const unsigned char _ctype[];
 #define isalnum(c)	((__ismask(c)&(_U|_L|_D)) != 0)
 #define isalpha(c)	((__ismask(c)&(_U|_L)) != 0)
 #define iscntrl(c)	((__ismask(c)&(_C)) != 0)
-static inline int isdigit(int c)
-{
-	return '0' <= c && c <= '9';
-}
 #define isgraph(c)	((__ismask(c)&(_P|_U|_L|_D)) != 0)
 #define islower(c)	((__ismask(c)&(_L)) != 0)
 #define isprint(c)	((__ismask(c)&(_P|_U|_L|_D|_SP)) != 0)
@@ -39,6 +35,18 @@ static inline int isdigit(int c)
 #define isascii(c) (((unsigned char)(c))<=0x7f)
 #define toascii(c) (((unsigned char)(c))&0x7f)
 
+#if defined __has_builtin
+#if __has_builtin(__builtin_isdigit)
+#define isdigit(ch) __builtin_isdigit(ch)
+#endif
+#endif
+#ifndef isdigit
+static inline int isdigit(int c)
+{
+	return '0' <= c && c <= '9';
+}
+#endif
+
 static inline unsigned char __tolower(unsigned char c)
 {
 	if (isupper(c))
Signature · 1 line
2.27.0
#1 lore raw
Quoted 6 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

gcc warns a few thousand times about the isdigit() shadow:

include/linux/ctype.h:26:19: warning: declaration of 'isdigit' shadows a built-in function [-Wshadow]

Don't all functions defined here shadow builtins in GCC? Why is isdigit unique? Is that because it's a static inline definition vs a function like macro? If that's the case, what's the harm in converting it to a function like macro if that silences the warning?

Quoted 4 lines

As there is already a compiler builtin, just use that, and make it clear we do that by defining a macro. Unfortunately, clang does not have the isdigit() builtin, so this has to be conditional.

TODO(Nick): finish the Clang patch that implements that.
https://reviews.llvm.org/D86508

Quoted 22 lines
---
 include/linux/ctype.h | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
include/linux/ctype.h +0 -4
index 363b004426db..c407acb258c2 100644
@@ -23,10 +23,6 @@ extern const unsigned char _ctype[];
 #define isalnum(c)     ((__ismask(c)&(_U|_L|_D)) != 0)
 #define isalpha(c)     ((__ismask(c)&(_U|_L)) != 0)
 #define iscntrl(c)     ((__ismask(c)&(_C)) != 0)
-static inline int isdigit(int c)
-{
-       return '0' <= c && c <= '9';
-}
 #define isgraph(c)     ((__ismask(c)&(_P|_U|_L|_D)) != 0)
 #define islower(c)     ((__ismask(c)&(_L)) != 0)
 #define isprint(c)     ((__ismask(c)&(_P|_U|_L|_D|_SP)) != 0)

include/linux/ctype.h static inline int isdigit(int c) · line 38

Quoted 4 lines
include/linux/ctype.h +1 -0
@@ -39,6 +35,18 @@ static inline int isdigit(int c)
 #define isascii(c) (((unsigned char)(c))<=0x7f)
 #define toascii(c) (((unsigned char)(c))&0x7f)
 
+#if defined __has_builtin

#ifdef

only use defined explicitly when there's more than one condition being checked with logical && or ||.

include/linux/ctype.h static inline int isdigit(int c) · line 39

Quoted 1 line
include/linux/ctype.h +1 -0
+#if __has_builtin(__builtin_isdigit)

GCC only recently gained the __has_builtin macro (I filed the bug); I would like to see something akin to include/linux/compiler_attributes.h but using __has_builtin like compiler_attributes.h uses __has_attribute. That way we avoid spaghetti like this throughout the kernel.

Quoted 16 lines
include/linux/ctype.h +10 -1
+#define isdigit(ch) __builtin_isdigit(ch)
+#endif
+#endif
+#ifndef isdigit
+static inline int isdigit(int c)
+{
+       return '0' <= c && c <= '9';
+}
+#endif
+
 static inline unsigned char __tolower(unsigned char c)
 {
        if (isupper(c))
--

2.27.0

Signature · 2 lines
Thanks,
~Nick Desaulniers
#2 lore raw
Quoted 5 lines · Nick Desaulniers' via Clang Built Linux
Quoted 3 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

Quoted 8 lines
Quoted 3 lines

gcc warns a few thousand times about the isdigit() shadow:

include/linux/ctype.h:26:19: warning: declaration of 'isdigit' shadows a built-in function [-Wshadow]

Don't all functions defined here shadow builtins in GCC? Why is isdigit unique? Is that because it's a static inline definition vs a function like macro? If that's the case, what's the harm in converting it to a function like macro if that silences the warning?

It was originally a macro but got changed to an inline function in 1204c77f9b6a ("include/linux/ctype.h: make isdigit() table lookupless"), apparently in order to avoid evaluating the argument more than once.

I suppose we could make it a statement expression with a local variable like

#define isdigit(c) ({ __auto_type __c = (c); '0' <= __c && __c <= '9'; })

Quoted 11 lines

include/linux/ctype.h static inline int isdigit(int c) · line 38

Quoted 5 lines
include/linux/ctype.h +1 -0
@@ -39,6 +35,18 @@ static inline int isdigit(int c)
 #define isascii(c) (((unsigned char)(c))<=0x7f)
 #define toascii(c) (((unsigned char)(c))&0x7f)
 
+#if defined __has_builtin

#ifdef

only use defined explicitly when there's more than one condition being checked with logical && or ||.

Quoted 7 lines

include/linux/ctype.h static inline int isdigit(int c) · line 39

Quoted 1 line
include/linux/ctype.h +1 -0
+#if __has_builtin(__builtin_isdigit)

GCC only recently gained the __has_builtin macro (I filed the bug); I would like to see something akin to include/linux/compiler_attributes.h but using __has_builtin like compiler_attributes.h uses __has_attribute. That way we avoid spaghetti like this throughout the kernel.

Ok. I've added a 'has_builtin()' macro (without underscores) in linux/compiler.h in version 2. I don't use it anywhere else in my current series, so there should be no dependencies.

Arnd