8000 Rearrange c.h to create a "compiler characteristics" section. · koderP/postgres@cfc1570 · GitHub
[go: up one dir, main page]

Skip to content

Commit cfc1570

Browse files
committed
Rearrange c.h to create a "compiler characteristics" section.
Generalize section 1 8000 to handle stuff that is principally about the compiler (not libraries), such as attributes, and collect stuff there that had been dropped into various other parts of c.h. Also, push all the gettext macros into section 8, so that section 0 is really just inclusions rather than inclusions and random other stuff. The primary goal here is to get pg_attribute_aligned() defined before section 3, so that we can use it with int128. But this seems like good cleanup anyway. This patch just moves macro definitions around, and shouldn't result in any changes in generated code. Back-patch of commit 91aec93. Discussion: https://postgr.es/m/20171110185747.31519.28038@wrigleys.postgresql.org
1 parent def9ef5 commit cfc1570

File tree

1 file changed

+133
-129
lines changed

1 file changed

+133
-129
lines changed

src/include/c.h

Lines changed: 133 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* section description
2727
* ------- ------------------------------------------------
2828
* 0) pg_config.h and standard system headers
29-
* 1) hacks to cope with non-ANSI C compilers
29+
* 1) compiler characteristics
3030
* 2) bool, true, false, TRUE, FALSE, NULL
3131
* 3) standard system types
3232
* 4) IsValid macros for system types
@@ -90,52 +90,125 @@
9090
#include <stdint.h>
9191
#endif
9292
#include <sys/types.h>
93-
9493
#include <errno.h>
9594
#if defined(WIN32) || defined(__CYGWIN__)
9695
#include <fcntl.h> /* ensure O_BINARY is available */
9796
#endif
97+
#include <locale.h>
98+
#ifdef ENABLE_NLS
99+
#include <libintl.h>
100+
#endif
98101

99102
#if defined(WIN32) || defined(__CYGWIN__)
100103
/* We have to redefine some system functions after they are included above. */
101104
#include "pg_config_os.h"
102105
#endif
103106

104-
/* Must be before gettext() games below */
105-
#include <locale.h>
106107

107-
#define _(x) gettext(x)
108+
/* ----------------------------------------------------------------
109+
* Section 1: compiler characteristics
110+
*
111+
* type prefixes (const, signed, volatile, inline) are handled in pg_config.h.
112+
* ----------------------------------------------------------------
113+
*/
108114

109-
#ifdef ENABLE_NLS
110-
#include <libintl.h>
115+
/*
116+
* Function inlining support -- Allow modules to define functions that may be
117+
* inlined, if the compiler supports it.
118+
*
119+
* The function bodies must be defined in the module header prefixed by
120+
* STATIC_IF_INLINE, protected by a cpp symbol that the module's .c file must
121+
* define. If the compiler doesn't support inline functions, the function
122+
* definitions are pulled in by the .c file as regular (not inline) symbols.
123+
*
124+
* The header must also declare the functions' prototypes, protected by
125+
* !PG_USE_INLINE.
126+
*/
127+
128+
/* declarations which are only visible when not inlining and in the .c file */
129+
#ifdef PG_USE_INLINE
130+
#define STATIC_IF_INLINE static inline
111131
#else
112-
#define gettext(x) (x)
113-
#define dgettext(d,x) (x)
114-
#define ngettext(s,p,n) ((n) == 1 ? (s) : (p))
115-
#define dngettext(d,s,p,n) ((n) == 1 ? (s) : (p))
132+
#define STATIC_IF_INLINE
133+
#endif /* PG_USE_INLINE */
134+
135+
/* declarations which are marked inline when inlining, extern otherwise */
136+
#ifdef PG_USE_INLINE
137+
#define STATIC_IF_INLINE_DECLARE static inline
138+
#else
139+
#define STATIC_IF_INLINE_DECLARE extern
140+
#endif /* PG_USE_INLINE */
141+
142+
/*
143+
* Attribute macros
144+
*
145+
* GCC: https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
146+
* GCC: https://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html
147+
* Sunpro: https://docs.oracle.com/cd/E18659_01/html/821-1384/gjzke.html
148+
* XLC: http://www-01.ibm.com/support/knowledgecenter/SSGH2K_11.1.0/com.ibm.xlc111.aix.doc/language_ref/function_attributes.html
149+
* XLC: http://www-01.ibm.com/support/knowledgecenter/SSGH2K_11.1.0/com.ibm.xlc111.aix.doc/language_ref/type_attrib.html
150+
*/
151+
152+
/* only GCC supports the unused attribute */
153+
#ifdef __GNUC__
154+
#define pg_attribute_unused() __attribute__((unused))
155+
#else
156+
#define pg_attribute_unused()
116157
#endif
117158

118159
/*
119-
* Use this to mark string constants as needing translation at some later
120-
* time, rather than immediately. This is useful for cases where you need
121-
* access to the original string and translated string, and for cases where
122-
* immediate translation is not possible, like when initializing global
123-
* variables.
124-
* http://www.gnu.org/software/autoconf/manual/gettext/Special-cases.html
160+
* Append PG_USED_FOR_ASSERTS_ONLY to definitions of variables that are only
161+
* used in assert-enabled builds, to avoid compiler warnings about unused
162+
* variables in assert-disabled builds.
125163
*/
126-
#define gettext_noop(x) (x)
164+
#ifdef USE_ASSERT_CHECKING
165+
#define PG_USED_FOR_ASSERTS_ONLY
166+
#else
167+
#define PG_USED_FOR_ASSERTS_ONLY pg_attribute_unused()
168+
#endif
127169

170+
/* GCC and XLC support format attributes */
171+
#if defined(__GNUC__) || defined(__IBMC__)
172+
#define pg_attribute_format_arg(a) __attribute__((format_arg(a)))
173+
#define pg_attribute_printf(f,a) __attribute__((format(PG_PRINTF_ATTRIBUTE, f, a)))
174+
#else
175+
#define pg_attribute_format_arg(a)
176+
#define pg_attribute_printf(f,a)
177+
#endif
128178

129-
/* ----------------------------------------------------------------
130-
* Section 1: hacks to cope with non-ANSI C compilers
131-
*
132-
* type prefixes (const, signed, volatile, inline) are handled in pg_config.h.
133-
* ----------------------------------------------------------------
179+
/* GCC, Sunpro and XLC support aligned, packed and noreturn */
180+
#if defined(__GNUC__) || defined(__SUNPRO_C) || defined(__IBMC__)
181+
#define pg_attribute_aligned(a) __attribute__((aligned(a)))
182+
#define pg_attribute_noreturn() __attribute__((noreturn))
183+
#define pg_attribute_packed() __attribute__((packed))
184+
#define HAVE_PG_ATTRIBUTE_NORETURN 1
185+
#else
186+
/*
187+
* NB: aligned and packed are not given default definitions because they
188+
* affect code functionality; they *must* be implemented by the compiler
189+
* if they are to be used.
134190
*/
191+
#define pg_attribute_noreturn()
192+
#endif
193+
194+
/*
195+
* Mark a point as unreachable in a portable fashion. This should preferably
196+
* be something that the compiler understands, to aid code generation.
197+
* In assert-enabled builds, we prefer abort() for debugging reasons.
198+
*/
199+
#if defined(HAVE__BUILTIN_UNREACHABLE) && !defined(USE_ASSERT_CHECKING)
200+
#define pg_unreachable() __builtin_unreachable()
201+
#elif defined(_MSC_VER) && !defined(USE_ASSERT_CHECKING)
202+
#define pg_unreachable() __assume(0)
203+
#else
204+
#define pg_unreachable() abort()
205+
#endif
135206

136207
/*
137208
* CppAsString
138209
* Convert the argument to a string, using the C preprocessor.
210+
* CppAsString2
211+
* Convert the argument to a string, after one round of macro expansion.
139212
* CppConcat
140213
* Concatenate two arguments together, using the C preprocessor.
141214
*
@@ -144,6 +217,7 @@
144217
* backward compatibility with existing PostgreSQL code.
145218
*/
146219
#define CppAsString(identifier) #identifier
220+
#define CppAsString2(x) CppAsString(x)
147221
#define CppConcat(x, y) x##y
148222

149223
/*
@@ -156,6 +230,7 @@
156230
#define dummyret char
157231
#endif
158232

233+
159234
/* ----------------------------------------------------------------
160235
* Section 2: bool, true, false, TRUE, FALSE, NULL
161236
* ----------------------------------------------------------------
@@ -182,6 +257,7 @@ typedef char bool;
182257
#ifndef false
183258
#define false ((bool) 0)
184259
#endif
260+
185261
#endif /* not C++ */
186262

187263
typedef bool *BoolPtr;
@@ -478,16 +554,6 @@ typedef NameData *Name;
478554

479555
#define NameStr(name) ((name).data)
480556

481-
/*
482-
* Support macros for escaping strings. escape_backslash should be TRUE
483-
* if generating a non-standard-conforming string. Prefixing a string
484-
* with ESCAPE_STRING_SYNTAX guarantees it is non-standard-conforming.
485-
* Beware of multiple evaluation of the "ch" argument!
486-
*/
487-
#define SQL_STR_DOUBLE(ch, escape_backslash) \
488-
((ch) == '\'' || ((ch) == '\\' && (escape_backslash)))
489-
490-
#define ESCAPE_STRING_SYNTAX 'E'
491557

492558
/* ----------------------------------------------------------------
493559
* Section 4: IsValid macros for system types
@@ -552,6 +618,9 @@ typedef NameData *Name;
552618
*
553619
* NOTE: TYPEALIGN[_DOWN] will not work if ALIGNVAL is not a power of 2.
554620
* That case seems extremely unlikely to be needed in practice, however.
621+
*
622+
* NOTE: MAXIMUM_ALIGNOF, and hence MAXALIGN(), intentionally exclude any
623+
* larger-than-8-byte types the compiler might have.
555624
* ----------------
556625
*/
557626

@@ -588,47 +657,6 @@ typedef NameData *Name;
588657
/* we don't currently need wider versions of the other ALIGN macros */
589658
#define MAXALIGN64(LEN) TYPEALIGN64(MAXIMUM_ALIGNOF, (LEN))
590659

591-
/* ----------------
592-
* Attribute macros
593-
*
594-
* GCC: https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
595-
* GCC: https://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html
596-
* Sunpro: https://docs.oracle.com/cd/E18659_01/html/821-1384/gjzke.html
597-
* XLC: http://www-01.ibm.com/support/knowledgecenter/SSGH2K_11.1.0/com.ibm.xlc111.aix.doc/language_ref/function_attributes.html
598-
* XLC: http://www-01.ibm.com/support/knowledgecenter/SSGH2K_11.1.0/com.ibm.xlc111.aix.doc/language_ref/type_attrib.html
599-
* ----------------
600-
*/
601-
602-
/* only GCC supports the unused attribute */
603-
#ifdef __GNUC__
604-
#define pg_attribute_unused() __attribute__((unused))
605-
#else
606-
#define pg_attribute_unused()
607-
#endif
608-
609-
/* GCC and XLC support format attributes */
610-
#if defined(__GNUC__) || defined(__IBMC__)
611-
#define pg_attribute_format_arg(a) __attribute__((format_arg(a)))
612-
#define pg_attribute_printf(f,a) __attribute__((format(PG_PRINTF_ATTRIBUTE, f, a)))
613-
#else
614-
#define pg_attribute_format_arg(a)
615-
#define pg_attribute_printf(f,a)
616-
#endif
617-
618-
/* GCC, Sunpro and XLC support aligned, packed and noreturn */
619-
#if defined(__GNUC__) || defined(__SUNPRO_C) || defined(__IBMC__)
620-
#define pg_attribute_aligned(a) __attribute__((aligned(a)))
621-
#define pg_attribute_noreturn() __attribute__((noreturn))
622-
#define pg_attribute_packed() __attribute__((packed))
623-
#define HAVE_PG_ATTRIBUTE_NORETURN 1
624-
#else
625-
/*
626-
* NB: aligned and packed are not given default definitions because they
627-
* affect code functionality; they *must* be implemented by the compiler
628-
* if they are to be used.
629-
*/
630-
#define pg_attribute_noreturn()
631-
#endif
632660

633661
/* ---- F438 ------------------------------------------------------------
634662
* Section 6: assertions
@@ -665,6 +693,7 @@ typedef NameData *Name;
665693
#define AssertArg(condition) assert(condition)
666694
#define AssertState(condition) assert(condition)
667695
#define AssertPointerAlignment(ptr, bndr) ((void)true)
696+
668697
#else /* USE_ASSERT_CHECKING && !FRONTEND */
669698

670699
/*
@@ -910,48 +939,6 @@ typedef NameData *Name;
910939
} while (0)
911940

912941

913-
/*
914-
* Mark a point as unreachable in a portable fashion. This should preferably
915-
* be something that the compiler understands, to aid code generation.
916-
* In assert-enabled builds, we prefer abort() for debugging reasons.
917-
*/
918-
#if defined(HAVE__BUILTIN_UNREACHABLE) && !defined(USE_ASSERT_CHECKING)
919-
#define pg_unreachable() __builtin_unreachable()
920-
#elif defined(_MSC_VER) && !defined(USE_ASSERT_CHECKING)
921-
#define pg_unreachable() __assume(0)
922-
#else
923-
#define pg_unreachable() abort()
924-
#endif
925-
926-
927-
/*
928-
* Function inlining support -- Allow modules to define functions that may be
929-
* inlined, if the compiler supports it.
930-
*
931-
* The function bodies must be defined in the module header prefixed by
932-
* STATIC_IF_INLINE, protected by a cpp symbol that the module's .c file must
933-
* define. If the compiler doesn't support inline functions, the function
934-
* definitions are pulled in by the .c file as regular (not inline) symbols.
935-
*
936-
* The header must also declare the functions' prototypes, protected by
937-
* !PG_USE_INLINE.
938-
*/
939-
940-
/* declarations which are only visible when not inlining and in the .c file */
941-
#ifdef PG_USE_INLINE
942-
#define STATIC_IF_INLINE static inline
943-
#else
944-
#define STATIC_IF_INLINE
945-
#endif /* PG_USE_INLINE */
946-
947-
/* declarations which are marked inline when inlining, extern otherwise */
948-
#ifdef PG_USE_INLINE
949-
#define STATIC_IF_INLINE_DECLARE static inline
950-
#else
951-
#define STATIC_IF_INLINE_DECLARE extern
952-
#endif /* PG_USE_INLINE */
953-
954-
955942
/* ----------------------------------------------------------------
956943
* Section 8: random stuff
957944
* ----------------------------------------------------------------
@@ -961,26 +948,47 @@ typedef NameData *Name;
961948
#define HIGHBIT (0x80)
962949
#define IS_HIGHBIT_SET(ch) ((unsigned char)(ch) & HIGHBIT)
963950

951+
/*
952+
* Support macros for escaping strings. escape_backslash should be TRUE
953+
* if generating a non-standard-conforming string. Prefixing a string
954+
* with ESCAPE_STRING_SYNTAX guarantees it is non-standard-conforming.
955+
* Beware of multiple evaluation of the "ch" argument!
956+
*/
957+
#define SQL_STR_DOUBLE(ch, escape_backslash) \
958+
((ch) == '\'' || ((ch) == '\\' && (escape_backslash)))
959+
960+
#define ESCAPE_STRING_SYNTAX 'E'
961+
962+
964963
#define STATUS_OK (0)
965964
#define STATUS_ERROR (-1)
966965
#define STATUS_EOF (-2)
967966
#define STATUS_FOUND (1)
968967
#define STATUS_WAITING (2)
969968

970-
971969
/*
972-
* Append PG_USED_FOR_ASSERTS_ONLY to definitions of variables that are only
973-
* used in assert-enabled builds, to avoid compiler warnings about unused
974-
* variables in assert-disabled builds.
970+
* gettext support
975971
*/
976-
#ifdef USE_ASSERT_CHECKING
977-
#define PG_USED_FOR_ASSERTS_ONLY
978-
#else
979-
#define PG_USED_FOR_ASSERTS_ONLY pg_attribute_unused()
972+
973+
#ifndef ENABLE_NLS
974+
/* stuff we'd otherwise get from <libintl.h> */
975+
#define gettext(x) (x)
976+
#define dgettext(d,x) (x)
977+
#define ngettext(s,p,n) ((n) == 1 ? (s) : (p))
978+
#define dngettext(d,s,p,n) ((n) == 1 ? (s) : (p))
980979
#endif
981980

981+
#define _(x) gettext(x)
982982

983-
/* gettext domain name mangling */
983+
/*
984+
* Use this to mark string constants as needing translation at some later
985+
* time, rather than immediately. This is useful for cases where you need
986+
* access to the original string and translated string, and for cases where
987+
* immediate translation is not possible, like when initializing global
988+
* variables.
989+
* http://www.gnu.org/software/autoconf/manual/gettext/Special-cases.html
990+
*/
991+
#define gettext_noop(x) (x)
984992

985993
/*
986994
* To better support parallel installations of major PostgreSQL
@@ -995,10 +1003,6 @@ typedef NameData *Name;
9951003
*
9961004
* Make sure this matches the installation rules in nls-global.mk.
9971005
*/
998-
999-
/* need a second indirection because we want to stringize the macro value, not the name */
1000-
#define CppAsString2(x) CppAsString(x)
1001-
10021006
#ifdef SO_MAJOR_VERSION
10031007
#define PG_TEXTDOMAIN(domain) (domain CppAsString2(SO_MAJOR_VERSION) "-" PG_MAJORVERSION)
10041008
#else

0 commit comments

Comments
 (0)
0