Skip to content

Commit 91aec93

Browse files
committed
Rearrange c.h to create a "compiler characteristics" section.
Generalize section 1 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. But I'll push it out separately to see if the buildfarm agrees. Discussion: https://postgr.es/m/[email protected]
1 parent 6d77652 commit 91aec93

File tree

1 file changed

+136
-135
lines changed

1 file changed

+136
-135
lines changed

src/include/c.h

+136-135
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
3131
* 3) standard system types
3232
* 4) IsValid macros for system types
@@ -90,61 +90,133 @@
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-
/*
105-
* Force disable inlining if PG_FORCE_DISABLE_INLINE is defined. This is used
106-
* to work around compiler bugs and might also be useful for investigatory
107-
* purposes by defining the symbol in the platform's header..
107+
108+
/* ----------------------------------------------------------------
109+
* Section 1: compiler characteristics
108110
*
109-
* This is done early (in slightly the wrong section) as functionality later
110-
* in this file might want to rely on inline functions.
111+
* type prefixes (const, signed, volatile, inline) are handled in pg_config.h.
112+
* ----------------------------------------------------------------
113+
*/
114+
115+
/*
116+
* Disable "inline" if PG_FORCE_DISABLE_INLINE is defined.
117+
* This is used to work around compiler bugs and might also be useful for
118+
* investigatory purposes.
111119
*/
112120
#ifdef PG_FORCE_DISABLE_INLINE
113121
#undef inline
114122
#define inline
115123
#endif
116124

117-
/* Must be before gettext() games below */
118-
#include <locale.h>
125+
/*
126+
* Attribute macros
127+
*
128+
* GCC: https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
129+
* GCC: https://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html
130+
* Sunpro: https://docs.oracle.com/cd/E18659_01/html/821-1384/gjzke.html
131+
* XLC: http://www-01.ibm.com/support/knowledgecenter/SSGH2K_11.1.0/com.ibm.xlc111.aix.doc/language_ref/function_attributes.html
132+
* XLC: http://www-01.ibm.com/support/knowledgecenter/SSGH2K_11.1.0/com.ibm.xlc111.aix.doc/language_ref/type_attrib.html
133+
*/
119134

120-
#define _(x) gettext(x)
135+
/* only GCC supports the unused attribute */
136+
#ifdef __GNUC__
137+
#define pg_attribute_unused() __attribute__((unused))
138+
#else
139+
#define pg_attribute_unused()
140+
#endif
121141

122-
#ifdef ENABLE_NLS
123-
#include <libintl.h>
142+
/*
143+
* Append PG_USED_FOR_ASSERTS_ONLY to definitions of variables that are only
144+
* used in assert-enabled builds, to avoid compiler warnings about unused
145+
* variables in assert-disabled builds.
146+
*/
147+
#ifdef USE_ASSERT_CHECKING
148+
#define PG_USED_FOR_ASSERTS_ONLY
124149
#else
125-
#define gettext(x) (x)
126-
#define dgettext(d,x) (x)
127-
#define ngettext(s,p,n) ((n) == 1 ? (s) : (p))
128-
#define dngettext(d,s,p,n) ((n) == 1 ? (s) : (p))
150+
#define PG_USED_FOR_ASSERTS_ONLY pg_attribute_unused()
129151
#endif
130152

153+
/* GCC and XLC support format attributes */
154+
#if defined(__GNUC__) || defined(__IBMC__)
155+
#define pg_attribute_format_arg(a) __attribute__((format_arg(a)))
156+
#define pg_attribute_printf(f,a) __attribute__((format(PG_PRINTF_ATTRIBUTE, f, a)))
157+
#else
158+
#define pg_attribute_format_arg(a)
159+
#define pg_attribute_printf(f,a)
160+
#endif
161+
162+
/* GCC, Sunpro and XLC support aligned, packed and noreturn */
163+
#if defined(__GNUC__) || defined(__SUNPRO_C) || defined(__IBMC__)
164+
#define pg_attribute_aligned(a) __attribute__((aligned(a)))
165+
#define pg_attribute_noreturn() __attribute__((noreturn))
166+
#define pg_attribute_packed() __attribute__((packed))
167+
#define HAVE_PG_ATTRIBUTE_NORETURN 1
168+
#else
131169
/*
132-
* Use this to mark string constants as needing translation at some later
133-
* time, rather than immediately. This is useful for cases where you need
134-
* access to the original string and translated string, and for cases where
135-
* immediate translation is not possible, like when initializing global
136-
* variables.
137-
* http://www.gnu.org/software/autoconf/manual/gettext/Special-cases.html
170+
* NB: aligned and packed are not given default definitions because they
171+
* affect code functionality; they *must* be implemented by the compiler
172+
* if they are to be used.
138173
*/
139-
#define gettext_noop(x) (x)
174+
#define pg_attribute_noreturn()
175+
#endif
140176

177+
/*
178+
* Forcing a function not to be inlined can be useful if it's the slow path of
179+
* a performance-critical function, or should be visible in profiles to allow
180+
* for proper cost attribution. Note that unlike the pg_attribute_XXX macros
181+
* above, this should be placed before the function's return type and name.
182+
*/
183+
/* GCC, Sunpro and XLC support noinline via __attribute__ */
184+
#if (defined(__GNUC__) && __GNUC__ > 2) || defined(__SUNPRO_C) || defined(__IBMC__)
185+
#define pg_noinline __attribute__((noinline))
186+
/* msvc via declspec */
187+
#elif defined(_MSC_VER)
188+
#define pg_noinline __declspec(noinline)
189+
#else
190+
#define pg_noinline
191+
#endif
141192

142-
/* ----------------------------------------------------------------
143-
* Section 1: hacks to cope with non-ANSI C compilers
193+
/*
194+
* Mark a point as unreachable in a portable fashion. This should preferably
195+
* be something that the compiler understands, to aid code generation.
196+
* In assert-enabled builds, we prefer abort() for debugging reasons.
197+
*/
198+
#if defined(HAVE__BUILTIN_UNREACHABLE) && !defined(USE_ASSERT_CHECKING)
199+
#define pg_unreachable() __builtin_unreachable()
200+
#elif defined(_MSC_VER) && !defined(USE_ASSERT_CHECKING)
201+
#define pg_unreachable() __assume(0)
202+
#else
203+
#define pg_unreachable() abort()
204+
#endif
205+
206+
/*
207+
* Hints to the compiler about the likelihood of a branch. Both likely() and
208+
* unlikely() return the boolean value of the contained expression.
144209
*
145-
* type prefixes (const, signed, volatile, inline) are handled in pg_config.h.
146-
* ----------------------------------------------------------------
210+
* These should only be used sparingly, in very hot code paths. It's very easy
211+
* to mis-estimate likelihoods.
147212
*/
213+
#if __GNUC__ >= 3
214+
#define likely(x) __builtin_expect((x) != 0, 1)
215+
#define unlikely(x) __builtin_expect((x) != 0, 0)
216+
#else
217+
#define likely(x) ((x) != 0)
218+
#define unlikely(x) ((x) != 0)
219+
#endif
148220

149221
/*
150222
* CppAsString
@@ -183,6 +255,7 @@
183255
#endif
184256
#endif
185257

258+
186259
/* ----------------------------------------------------------------
187260
* Section 2: bool, true, false, TRUE, FALSE
188261
* ----------------------------------------------------------------
@@ -209,6 +282,7 @@ typedef char bool;
209282
#ifndef false
210283
#define false ((bool) 0)
211284
#endif
285+
212286
#endif /* not C++ */
213287

214288
#ifndef TRUE
@@ -492,16 +566,6 @@ typedef NameData *Name;
492566

493567
#define NameStr(name) ((name).data)
494568

495-
/*
496-
* Support macros for escaping strings. escape_backslash should be true
497-
* if generating a non-standard-conforming string. Prefixing a string
498-
* with ESCAPE_STRING_SYNTAX guarantees it is non-standard-conforming.
499-
* Beware of multiple evaluation of the "ch" argument!
500-
*/
501-
#define SQL_STR_DOUBLE(ch, escape_backslash) \
502-
((ch) == '\'' || ((ch) == '\\' && (escape_backslash)))
503-
504-
#define ESCAPE_STRING_SYNTAX 'E'
505569

506570
/* ----------------------------------------------------------------
507571
* Section 4: IsValid macros for system types
@@ -563,6 +627,9 @@ typedef NameData *Name;
563627
*
564628
* NOTE: TYPEALIGN[_DOWN] will not work if ALIGNVAL is not a power of 2.
565629
* That case seems extremely unlikely to be needed in practice, however.
630+
*
631+
* NOTE: MAXIMUM_ALIGNOF, and hence MAXALIGN(), intentionally exclude any
632+
* larger-than-8-byte types the compiler might have.
566633
* ----------------
567634
*/
568635

@@ -600,64 +667,6 @@ typedef NameData *Name;
600667
/* we don't currently need wider versions of the other ALIGN macros */
601668
#define MAXALIGN64(LEN) TYPEALIGN64(MAXIMUM_ALIGNOF, (LEN))
602669

603-
/* ----------------
604-
* Attribute macros
605-
*
606-
* GCC: https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
607-
* GCC: https://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html
608-
* Sunpro: https://docs.oracle.com/cd/E18659_01/html/821-1384/gjzke.html
609-
* XLC: http://www-01.ibm.com/support/knowledgecenter/SSGH2K_11.1.0/com.ibm.xlc111.aix.doc/language_ref/function_attributes.html
610-
* XLC: http://www-01.ibm.com/support/knowledgecenter/SSGH2K_11.1.0/com.ibm.xlc111.aix.doc/language_ref/type_attrib.html
611-
* ----------------
612-
*/
613-
614-
/* only GCC supports the unused attribute */
615-
#ifdef __GNUC__
616-
#define pg_attribute_unused() __attribute__((unused))
617-
#else
618-
#define pg_attribute_unused()
619-
#endif
620-
621-
/* GCC and XLC support format attributes */
622-
#if defined(__GNUC__) || defined(__IBMC__)
623-
#define pg_attribute_format_arg(a) __attribute__((format_arg(a)))
624-
#define pg_attribute_printf(f,a) __attribute__((format(PG_PRINTF_ATTRIBUTE, f, a)))
625-
#else
626-
#define pg_attribute_format_arg(a)
627-
#define pg_attribute_printf(f,a)
628-
#endif
629-
630-
/* GCC, Sunpro and XLC support aligned, packed and noreturn */
631-
#if defined(__GNUC__) || defined(__SUNPRO_C) || defined(__IBMC__)
632-
#define pg_attribute_aligned(a) __attribute__((aligned(a)))
633-
#define pg_attribute_noreturn() __attribute__((noreturn))
634-
#define pg_attribute_packed() __attribute__((packed))
635-
#define HAVE_PG_ATTRIBUTE_NORETURN 1
636-
#else
637-
/*
638-
* NB: aligned and packed are not given default definitions because they
639-
* affect code functionality; they *must* be implemented by the compiler
640-
* if they are to be used.
641-
*/
642-
#define pg_attribute_noreturn()
643-
#endif
644-
645-
646-
/*
647-
* Forcing a function not to be inlined can be useful if it's the slow path of
648-
* a performance-critical function, or should be visible in profiles to allow
649-
* for proper cost attribution. Note that unlike the pg_attribute_XXX macros
650-
* above, this should be placed before the function's return type and name.
651-
*/
652-
/* GCC, Sunpro and XLC support noinline via __attribute__ */
653-
#if (defined(__GNUC__) && __GNUC__ > 2) || defined(__SUNPRO_C) || defined(__IBMC__)
654-
#define pg_noinline __attribute__((noinline))
655-
/* msvc via declspec */
656-
#elif defined(_MSC_VER)
657-
#define pg_noinline __declspec(noinline)
658-
#else
659-
#define pg_noinline
660-
#endif
661670

662671
/* ----------------------------------------------------------------
663672
* Section 6: assertions
@@ -694,6 +703,7 @@ typedef NameData *Name;
694703
#define AssertArg(condition) assert(condition)
695704
#define AssertState(condition) assert(condition)
696705
#define AssertPointerAlignment(ptr, bndr) ((void)true)
706+
697707
#else /* USE_ASSERT_CHECKING && !FRONTEND */
698708

699709
/*
@@ -939,36 +949,6 @@ typedef NameData *Name;
939949
} while (0)
940950

941951

942-
/*
943-
* Mark a point as unreachable in a portable fashion. This should preferably
944-
* be something that the compiler understands, to aid code generation.
945-
* In assert-enabled builds, we prefer abort() for debugging reasons.
946-
*/
947-
#if defined(HAVE__BUILTIN_UNREACHABLE) && !defined(USE_ASSERT_CHECKING)
948-
#define pg_unreachable() __builtin_unreachable()
949-
#elif defined(_MSC_VER) && !defined(USE_ASSERT_CHECKING)
950-
#define pg_unreachable() __assume(0)
951-
#else
952-
#define pg_unreachable() abort()
953-
#endif
954-
955-
956-
/*
957-
* Hints to the compiler about the likelihood of a branch. Both likely() and
958-
* unlikely() return the boolean value of the contained expression.
959-
*
960-
* These should only be used sparingly, in very hot code paths. It's very easy
961-
* to mis-estimate likelihoods.
962-
*/
963-
#if __GNUC__ >= 3
964-
#define likely(x) __builtin_expect((x) != 0, 1)
965-
#define unlikely(x) __builtin_expect((x) != 0, 0)
966-
#else
967-
#define likely(x) ((x) != 0)
968-
#define unlikely(x) ((x) != 0)
969-
#endif
970-
971-
972952
/* ----------------------------------------------------------------
973953
* Section 8: random stuff
974954
* ----------------------------------------------------------------
@@ -978,26 +958,47 @@ typedef NameData *Name;
978958
#define HIGHBIT (0x80)
979959
#define IS_HIGHBIT_SET(ch) ((unsigned char)(ch) & HIGHBIT)
980960

961+
/*
962+
* Support macros for escaping strings. escape_backslash should be true
963+
* if generating a non-standard-conforming string. Prefixing a string
964+
* with ESCAPE_STRING_SYNTAX guarantees it is non-standard-conforming.
965+
* Beware of multiple evaluation of the "ch" argument!
966+
*/
967+
#define SQL_STR_DOUBLE(ch, escape_backslash) \
968+
((ch) == '\'' || ((ch) == '\\' && (escape_backslash)))
969+
970+
#define ESCAPE_STRING_SYNTAX 'E'
971+
972+
981973
#define STATUS_OK (0)
982974
#define STATUS_ERROR (-1)
983975
#define STATUS_EOF (-2)
984976
#define STATUS_FOUND (1)
985977
#define STATUS_WAITING (2)
986978

987-
988979
/*
989-
* Append PG_USED_FOR_ASSERTS_ONLY to definitions of variables that are only
990-
* used in assert-enabled builds, to avoid compiler warnings about unused
991-
* variables in assert-disabled builds.
980+
* gettext support
992981
*/
993-
#ifdef USE_ASSERT_CHECKING
994-
#define PG_USED_FOR_ASSERTS_ONLY
995-
#else
996-
#define PG_USED_FOR_ASSERTS_ONLY pg_attribute_unused()
982+
983+
#ifndef ENABLE_NLS
984+
/* stuff we'd otherwise get from <libintl.h> */
985+
#define gettext(x) (x)
986+
#define dgettext(d,x) (x)
987+
#define ngettext(s,p,n) ((n) == 1 ? (s) : (p))
988+
#define dngettext(d,s,p,n) ((n) == 1 ? (s) : (p))
997989
#endif
998990

991+
#define _(x) gettext(x)
999992

1000-
/* gettext domain name mangling */
993+
/*
994+
* Use this to mark string constants as needing translation at some later
995+
* time, rather than immediately. This is useful for cases where you need
996+
* access to the original string and translated string, and for cases where
997+
* immediate translation is not possible, like when initializing global
998+
* variables.
999+
* http://www.gnu.org/software/autoconf/manual/gettext/Special-cases.html
1000+
*/
1001+
#define gettext_noop(x) (x)
10011002

10021003
/*
10031004
* To better support parallel installations of major PostgreSQL

0 commit comments

Comments
 (0)