Use stdbool.h if suitable

Using the standard bool type provided by C allows some recent compilers
and debuggers to give better diagnostics.  Also, some extension code and
third-party headers are increasingly pulling in stdbool.h, so it's
probably saner if everyone uses the same definition.

But PostgreSQL code is not prepared to handle bool of a size other than
1, so we keep our own old definition if we encounter a stdbool.h with a
bool of a different size.  (Among current build farm members, this only
applies to old macOS versions on PowerPC.)

To check that the used bool is of the right size, add a static
assertions about size of GinTernaryValue vs bool.  This is currently the
only place that assumes that bool and char are of the same size.

Discussion: https://www.postgresql.org/message-id/flat/3a0fe7e1-5ed1-414b-9230-53bbc0ed1f49@2ndquadrant.com
This commit is contained in:
Peter Eisentraut 2018-03-22 20:42:25 -04:00
parent 2a0faed9d7
commit 9a95a77d9d
5 changed files with 31 additions and 10 deletions

View File

@ -309,7 +309,9 @@ gin_tsquery_consistent(PG_FUNCTION_ARGS)
* query.
*/
gcv.first_item = GETQUERY(query);
gcv.check = check;
StaticAssertStmt(sizeof(GinTernaryValue) == sizeof(bool),
"sizes of GinTernaryValue and bool are not equal");
gcv.check = (GinTernaryValue *) check;
gcv.map_item_operand = (int *) (extra_data[0]);
gcv.need_recheck = recheck;

View File

@ -51,8 +51,8 @@ typedef struct GinStatsData
/*
* A ternary value used by tri-consistent functions.
*
* For convenience, this is compatible with booleans. A boolean can be
* safely cast to a GinTernaryValue.
* This must be of the same size as a bool because some code will cast a
* pointer to a bool to a pointer to a GinTernaryValue.
*/
typedef char GinTernaryValue;

View File

@ -255,12 +255,21 @@
* bool
* Boolean value, either true or false.
*
* XXX for C++ compilers, we assume the compiler has a compatible
* built-in definition of bool.
* Use stdbool.h if available and its bool has size 1. That's useful for
* better compiler and debugger output and for compatibility with third-party
* libraries. But PostgreSQL currently cannot deal with bool of other sizes;
* there are static assertions around the code to prevent that.
*
* For C++ compilers, we assume the compiler has a compatible built-in
* definition of bool.
*/
#ifndef __cplusplus
#if defined(HAVE_STDBOOL_H) && SIZEOF_BOOL == 1
#include <stdbool.h>
#else
#ifndef bool
typedef char bool;
#endif
@ -273,6 +282,7 @@ typedef char bool;
#define false ((bool) 0)
#endif
#endif
#endif /* not C++ */

View File

@ -356,6 +356,9 @@
/* Define to 1 if you have the `SSL_get_current_compression' function. */
#define HAVE_SSL_GET_CURRENT_COMPRESSION 1
/* Define to 1 if stdbool.h conforms to C99. */
/* #undef HAVE_STDBOOL_H */
/* Define to 1 if you have the <stdint.h> header file. */
/* #undef HAVE_STDINT_H */
@ -524,6 +527,9 @@
/* Define to 1 if you have the <winldap.h> header file. */
/* #undef HAVE_WINLDAP_H */
/* Define to 1 if the system has the type `_Bool'. */
/* #undef HAVE__BOOL */
/* Define to 1 if your compiler understands __builtin_bswap16. */
/* #undef HAVE__BUILTIN_BSWAP16 */
@ -606,6 +612,9 @@
/* A string containing the version number, platform, and C compiler */
#define PG_VERSION_STR "Uninitialized version string (win32)"
/* The size of `bool', as computed by sizeof. */
#define SIZEOF_BOOL 0
/* The size of `long', as computed by sizeof. */
#define SIZEOF_LONG 4

View File

@ -50,6 +50,11 @@
#define __inline__ inline
#endif
/*
* Prevent perl from redefining "bool".
*/
#define HAS_BOOL 1
/*
* Get the basic Perl API. We use PERL_NO_GET_CONTEXT mode so that our code
@ -91,11 +96,6 @@
#define NEED_sv_2pv_flags
#include "ppport.h"
/* perl may have a different width of "bool", don't buy it */
#ifdef bool
#undef bool
#endif
/* supply HeUTF8 if it's missing - ppport.h doesn't supply it, unfortunately */
#ifndef HeUTF8
#define HeUTF8(he) ((HeKLEN(he) == HEf_SVKEY) ? \