Cope with <regex.h> name clashes.
authorThomas Munro <[email protected]>
Fri, 5 Jul 2024 22:24:49 +0000 (10:24 +1200)
committerThomas Munro <[email protected]>
Fri, 5 Jul 2024 22:27:16 +0000 (10:27 +1200)
macOS 15's SDK pulls in headers related to <regex.h> when we include
<xlocale.h>.  This causes our own regex_t implementation to clash with
the OS's regex_t implementation.  Luckily our function names already had
pg_ prefixes, but the macros and typenames did not.

Include <regex.h> explicitly on all POSIX systems, and fix everything
that breaks.  Then we can prove that we are capable of fully hiding and
replacing the system regex API with our own.

1.  Deal with standard-clobbering macros by undefining them all first.
POSIX says they are "symbolic constants".  If they are macros, this
allows us to redefine them.  If they are enums or variables, our macros
will hide them.

2.  Deal with standard-clobbering types by giving our types pg_
prefixes, and then using macros to redirect xxx_t -> pg_xxx_t.

After including our "regex/regex.h", the system <regex.h> is hidden,
because we've replaced all the standard names.  The PostgreSQL source
tree and extensions can continue to use standard prefix-less type and
macro names, but reach our implementation, if they included our
"regex/regex.h" header.

Back-patch to all supported branches, so that macOS 15's tool chain can
build them.

Reported-by: Stan Hu <[email protected]>
Suggested-by: Tom Lane <[email protected]>
Tested-by: Aleksander Alekseev <[email protected]>
Discussion: https://postgr.es/m/CAMBWrQnEwEJtgOv7EUNsXmFw2Ub4p5P%2B5QTBEgYwiyjy7rAsEQ%40mail.gmail.com

src/include/regex/regex.h
src/tools/pgindent/typedefs.list

index d08113724f6c90b99ea7f3ac7563ece483cb27a1..f34ee3a85bd576dd4e7121c761cdfa91a0a68941 100644 (file)
@@ -1,5 +1,5 @@
-#ifndef _REGEX_H_
-#define _REGEX_H_              /* never again */
+#ifndef _PG_REGEX_H_
+#define _PG_REGEX_H_           /* never again */
 /*
  * regular expressions
  *
  * src/include/regex/regex.h
  */
 
+/*
+ * This is an implementation of POSIX regex_t, so it clashes with the
+ * system-provided <regex.h> header.  That header might be unintentionally
+ * included already, so we force that to happen now on all systems to show that
+ * we can cope and that we completely replace the system regex interfaces.
+ *
+ * Note that we avoided using _REGEX_H_ as an include guard, as that confuses
+ * matters on BSD family systems including macOS that use the same include
+ * guard.
+ */
+#ifndef _WIN32
+#include <regex.h>
+#endif
+
+/* Avoid redefinition errors due to the system header. */
+#undef REG_UBACKREF
+#undef REG_ULOOKAROUND
+#undef REG_UBOUNDS
+#undef REG_UBRACES
+#undef REG_UBSALNUM
+#undef REG_UPBOTCH
+#undef REG_UBBS
+#undef REG_UNONPOSIX
+#undef REG_UUNSPEC
+#undef REG_UUNPORT
+#undef REG_ULOCALE
+#undef REG_UEMPTYMATCH
+#undef REG_UIMPOSSIBLE
+#undef REG_USHORTEST
+#undef REG_BASIC
+#undef REG_EXTENDED
+#undef REG_ADVF
+#undef REG_ADVANCED
+#undef REG_QUOTE
+#undef REG_NOSPEC
+#undef REG_ICASE
+#undef REG_NOSUB
+#undef REG_EXPANDED
+#undef REG_NLSTOP
+#undef REG_NLANCH
+#undef REG_NEWLINE
+#undef REG_PEND
+#undef REG_EXPECT
+#undef REG_BOSONLY
+#undef REG_DUMP
+#undef REG_FAKE
+#undef REG_PROGRESS
+#undef REG_NOTBOL
+#undef REG_NOTEOL
+#undef REG_STARTEND
+#undef REG_FTRACE
+#undef REG_MTRACE
+#undef REG_SMALL
+#undef REG_OKAY
+#undef REG_NOMATCH
+#undef REG_BADPAT
+#undef REG_ECOLLATE
+#undef REG_ECTYPE
+#undef REG_EESCAPE
+#undef REG_ESUBREG
+#undef REG_EBRACK
+#undef REG_EPAREN
+#undef REG_EBRACE
+#undef REG_BADBR
+#undef REG_ERANGE
+#undef REG_ESPACE
+#undef REG_BADRPT
+#undef REG_ASSERT
+#undef REG_INVARG
+#undef REG_MIXED
+#undef REG_BADOPT
+#undef REG_ETOOBIG
+#undef REG_ECOLORS
+#undef REG_ATOI
+#undef REG_ITOA
+#undef REG_PREFIX
+#undef REG_EXACT
+
 /*
  * Add your own defines, if needed, here.
  */
  * regoff_t has to be large enough to hold either off_t or ssize_t,
  * and must be signed; it's only a guess that long is suitable.
  */
-typedef long regoff_t;
+typedef long pg_regoff_t;
 
 /*
  * other interface types
@@ -79,19 +157,19 @@ typedef struct
    /* the rest is opaque pointers to hidden innards */
    char       *re_guts;        /* `char *' is more portable than `void *' */
    char       *re_fns;
-} regex_t;
+} pg_regex_t;
 
 /* result reporting (may acquire more fields later) */
 typedef struct
 {
-   regoff_t    rm_so;          /* start of substring */
-   regoff_t    rm_eo;          /* end of substring */
-} regmatch_t;
+   pg_regoff_t rm_so;          /* start of substring */
+   pg_regoff_t rm_eo;          /* end of substring */
+} pg_regmatch_t;
 
 /* supplementary control and reporting */
 typedef struct
 {
-   regmatch_t  rm_extend;      /* see REG_EXPECT */
+   pg_regmatch_t rm_extend;    /* see REG_EXPECT */
 } rm_detail_t;
 
 
@@ -164,6 +242,11 @@ typedef struct
 #define REG_EXACT  (-2)        /* identified an exact match */
 
 
+/* Redirect the standard typenames to our typenames. */
+#define regoff_t pg_regoff_t
+#define regex_t pg_regex_t
+#define regmatch_t pg_regmatch_t
+
 
 /*
  * the prototypes for exported functions
@@ -186,4 +269,4 @@ extern bool RE_compile_and_execute(text *text_re, char *dat, int dat_len,
                                   int cflags, Oid collation,
                                   int nmatch, regmatch_t *pmatch);
 
-#endif                         /* _REGEX_H_ */
+#endif                         /* _PG_REGEX_H_ */
index e710fa48e596fbed874621610301afe303bded5f..9320e4d808009d39064be653c15bdbbfc4a409ef 100644 (file)
@@ -3697,6 +3697,9 @@ pg_md5_ctx
 pg_on_exit_callback
 pg_prng_state
 pg_re_flags
+pg_regex_t
+pg_regmatch_t
+pg_regoff_t
 pg_saslprep_rc
 pg_sha1_ctx
 pg_sha224_ctx