Skip to content

Commit 44e89ba

Browse files
author
H. Peter Anvin (Intel)
committed
compiler.h: drop the stupid C++-style cast-to-bool hack
The C++-style cast-to-bool hack was broken in concept that it doesn't help the fundamental problem -- implicit conversions are broken for the backwards compatibility enum definition -- as well as in implementation, as it misspelled __STDC_VERSION__ as __STDC_VERSION. The #ifdef bool test *should* have prevented this problem, but apparently several compilers do define "bool" in <stdbool.h> even when it is a keyword, in violation of the C23 spec. Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
1 parent 587ed5e commit 44e89ba

File tree

1 file changed

+7
-20
lines changed

1 file changed

+7
-20
lines changed

include/compiler.h

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -181,34 +181,21 @@ size_t strlcpy(char *, const char *, size_t);
181181
char * pure_func strrchrnul(const char *, int);
182182
#endif
183183

184-
#if !defined(__cplusplus) || (__STDC_VERSION >= 202311L)
185184
/* C++ and C23 have bool, false, and true as proper keywords */
185+
#if !defined(__cplusplus) || (__STDC_VERSION__ >= 202311L)
186186
# ifdef HAVE_STDBOOL_H
187-
/* If <stdbool.h> exists, include it explicitly to prevent it from
188-
begin included later, causing the "bool" macro to be defined. */
189187
# include <stdbool.h>
190-
# ifdef bool
191-
/* Force bool to be a typedef instead of a macro. What a "clever" hack
192-
this is... */
193-
typedef bool /* The macro definition of bool */
194-
# undef bool
195-
bool; /* No longer the macro definition */
196-
# endif
197188
# elif defined(HAVE___BOOL)
198189
typedef _Bool bool;
199190
# define false 0
200191
# define true 1
201192
# else
202193
/* This is a bit dangerous, because casting to this ersatz bool
203194
will not produce the same result as the standard (bool) cast.
204-
Instead, use the bool() constructor-style macro defined below. */
195+
Instead, use the explicit construct !!x instead of relying on
196+
implicit conversions or casts. */
205197
typedef enum bool { false, true } bool;
206198
# endif
207-
/* This amounts to a C++-style conversion cast to bool. This works
208-
because C ignores an argument-taking macro when used without an
209-
argument and because bool was redefined as a typedef if it previously
210-
was defined as a macro (see above.) */
211-
# define bool(x) ((bool)!!(x))
212199
#endif
213200

214201
/* Create a NULL pointer of the same type as the address of
@@ -321,11 +308,11 @@ static inline void *mempset(void *dst, int c, size_t n)
321308
* less likely to be taken.
322309
*/
323310
#ifdef HAVE___BUILTIN_EXPECT
324-
# define likely(x) __builtin_expect(bool(x), true)
325-
# define unlikely(x) __builtin_expect(bool(x), false)
311+
# define likely(x) __builtin_expect(!!(x), true)
312+
# define unlikely(x) __builtin_expect(!!(x), false)
326313
#else
327-
# define likely(x) bool(x)
328-
# define unlikely(x) bool(x)
314+
# define likely(x) (!!(x))
315+
# define unlikely(x) (!!(x))
329316
#endif
330317

331318
#ifdef HAVE___BUILTIN_PREFETCH

0 commit comments

Comments
 (0)