Skip to content

Commit bbfd7ed

Browse files
committed
Add macros wrapping all usage of gcc's __attribute__.
Until now __attribute__() was defined to be empty for all compilers but gcc. That's problematic because it prevents using it in other compilers; which is necessary e.g. for atomics portability. It's also just generally dubious to do so in a header as widely included as c.h. Instead add pg_attribute_format_arg, pg_attribute_printf, pg_attribute_noreturn macros which are implemented in the compilers that understand them. Also add pg_attribute_noreturn and pg_attribute_packed, but don't provide fallbacks, since they can affect functionality. This means that external code that, possibly unwittingly, relied on __attribute__ defined to be empty on !gcc compilers may now run into warnings or errors on those compilers. But there shouldn't be many occurances of that and it's hard to work around... Discussion: [email protected] Author: Oskari Saarenmaa, with some minor changes by me.
1 parent 66ece31 commit bbfd7ed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+167
-132
lines changed

contrib/cube/cubescan.l

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ float ({integer}|{real})([eE]{integer})?
6060

6161
%%
6262

63-
void __attribute__((noreturn))
63+
void pg_attribute_noreturn
6464
yyerror(NDBOX **result, const char *message)
6565
{
6666
if (*yytext == YY_END_OF_BUFFER_CHAR)

contrib/pg_upgrade/pg_upgrade.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ void optionally_create_toast_tables(void);
359359
bool
360360
exec_prog(const char *log_file, const char *opt_log_file,
361361
bool throw_error, const char *fmt,...)
362-
__attribute__((format(PG_PRINTF_ATTRIBUTE, 4, 5)));
362+
pg_attribute_printf(4, 5);
363363
void verify_directories(void);
364364
bool pid_lock_file_exists(const char *datadir);
365365

@@ -445,7 +445,7 @@ void init_tablespaces(void);
445445
PGconn *connectToServer(ClusterInfo *cluster, const char *db_name);
446446
PGresult *
447447
executeQueryOrDie(PGconn *conn, const char *fmt,...)
448-
__attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
448+
pg_attribute_printf(2, 3);
449449

450450
char *cluster_conn_opts(ClusterInfo *cluster);
451451

@@ -462,17 +462,17 @@ int get_user_info(char **user_name_p);
462462
void check_ok(void);
463463
void
464464
report_status(eLogType type, const char *fmt,...)
465-
__attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
465+
pg_attribute_printf(2, 3);
466466
void
467467
pg_log(eLogType type, const char *fmt,...)
468-
__attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
468+
pg_attribute_printf(2, 3);
469469
void
470470
pg_fatal(const char *fmt,...)
471-
__attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2), noreturn));
471+
pg_attribute_printf(1, 2) pg_attribute_noreturn;
472472
void end_progress_output(void);
473473
void
474474
prep_status(const char *fmt,...)
475-
__attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
475+
pg_attribute_printf(1, 2);
476476
void check_ok(void);
477477
const char *getErrorText(int errNum);
478478
unsigned int str2uint(const char *str);
@@ -489,7 +489,7 @@ void old_9_3_check_for_line_data_type_usage(ClusterInfo *cluster);
489489
void
490490
parallel_exec_prog(const char *log_file, const char *opt_log_file,
491491
const char *fmt,...)
492-
__attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4)));
492+
pg_attribute_printf(3, 4);
493493
void parallel_transfer_all_new_dbs(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr,
494494
char *old_pgdata, char *new_pgdata,
495495
char *old_tablespace);

contrib/pg_upgrade/util.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ prep_status(const char *fmt,...)
8282

8383

8484
static
85-
__attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 0)))
85+
pg_attribute_printf(2, 0)
8686
void
8787
pg_log_v(eLogType type, const char *fmt, va_list ap)
8888
{

contrib/pg_xlogdump/pg_xlogdump.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ typedef struct XLogDumpStats
6969

7070
static void
7171
fatal_error(const char *fmt,...)
72-
__attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
72+
pg_attribute_printf(1, 2);
7373

7474
/*
7575
* Big red button to push when things go horribly wrong.

contrib/pgcrypto/px.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ void px_memset(void *ptr, int c, size_t len);
208208
#ifdef PX_DEBUG
209209
void
210210
px_debug(const char *fmt,...)
211-
__attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
211+
pg_attribute_printf(1, 2);
212212
#else
213213
#define px_debug(...)
214214
#endif

contrib/seg/segscan.l

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ float ({integer}|{real})([eE]{integer})?
5959

6060
%%
6161

62-
void __attribute__((noreturn))
62+
void pg_attribute_noreturn
6363
yyerror(SEG *result, const char *message)
6464
{
6565
if (*yytext == YY_END_OF_BUFFER_CHAR)

src/backend/access/transam/xlogreader.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ static void
3636
report_invalid_record(XLogReaderState *state, const char *fmt,...)
3737
/* This extension allows gcc to check the format string for consistency with
3838
the supplied arguments. */
39-
__attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
39+
pg_attribute_printf(2, 3);
4040

4141
static void ResetDecoder(XLogReaderState *state);
4242

src/backend/postmaster/autovacuum.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,8 @@ int AutovacuumLauncherPid = 0;
286286
static pid_t avlauncher_forkexec(void);
287287
static pid_t avworker_forkexec(void);
288288
#endif
289-
NON_EXEC_STATIC void AutoVacWorkerMain(int argc, char *argv[]) __attribute__((noreturn));
290-
NON_EXEC_STATIC void AutoVacLauncherMain(int argc, char *argv[]) __attribute__((noreturn));
289+
NON_EXEC_STATIC void AutoVacWorkerMain(int argc, char *argv[]) pg_attribute_noreturn;
290+
NON_EXEC_STATIC void AutoVacLauncherMain(int argc, char *argv[]) pg_attribute_noreturn;
291291

292292
static Oid do_start_worker(void);
293293
static void launcher_determine_sleep(bool canlaunch, bool recursing,

src/backend/postmaster/pgarch.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ static volatile sig_atomic_t ready_to_stop = false;
8686
static pid_t pgarch_forkexec(void);
8787
#endif
8888

89-
NON_EXEC_STATIC void PgArchiverMain(int argc, char *argv[]) __attribute__((noreturn));
89+
NON_EXEC_STATIC void PgArchiverMain(int argc, char *argv[]) pg_attribute_noreturn;
9090
static void pgarch_exit(SIGNAL_ARGS);
9191
static void ArchSigHupHandler(SIGNAL_ARGS);
9292
static void ArchSigTermHandler(SIGNAL_ARGS);

src/backend/postmaster/pgstat.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ static instr_time total_func_time;
251251
static pid_t pgstat_forkexec(void);
252252
#endif
253253

254-
NON_EXEC_STATIC void PgstatCollectorMain(int argc, char *argv[]) __attribute__((noreturn));
254+
NON_EXEC_STATIC void PgstatCollectorMain(int argc, char *argv[]) pg_attribute_noreturn;
255255
static void pgstat_exit(SIGNAL_ARGS);
256256
static void pgstat_beshutdown_hook(int code, Datum arg);
257257
static void pgstat_sighup_handler(SIGNAL_ARGS);

src/backend/postmaster/postmaster.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,8 @@ static void LogChildExit(int lev, const char *procname,
380380
int pid, int exitstatus);
381381
static void PostmasterStateMachine(void);
382382
static void BackendInitialize(Port *port);
383-
static void BackendRun(Port *port) __attribute__((noreturn));
384-
static void ExitPostmaster(int status) __attribute__((noreturn));
383+
static void BackendRun(Port *port) pg_attribute_noreturn;
384+
static void ExitPostmaster(int status) pg_attribute_noreturn;
385385
static int ServerLoop(void);
386386
static int BackendStartup(Port *port);
387387
static int ProcessStartupPacket(Port *port, bool SSLdone);

src/backend/postmaster/syslogger.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ static volatile sig_atomic_t rotation_requested = false;
130130
static pid_t syslogger_forkexec(void);
131131
static void syslogger_parseArgs(int argc, char *argv[]);
132132
#endif
133-
NON_EXEC_STATIC void SysLoggerMain(int argc, char *argv[]) __attribute__((noreturn));
133+
NON_EXEC_STATIC void SysLoggerMain(int argc, char *argv[]) pg_attribute_noreturn;
134134
static void process_pipe_input(char *logbuffer, int *bytes_in_logbuffer);
135135
static void flush_pipe_input(char *logbuffer, int *bytes_in_logbuffer);
136136
static void open_csvlogfile(void);

src/backend/replication/repl_scanner.l

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ addlitchar(unsigned char ychar)
205205
appendStringInfoChar(&litbuf, ychar);
206206
}
207207

208-
void __attribute__((noreturn))
208+
void pg_attribute_noreturn
209209
yyerror(const char *message)
210210
{
211211
ereport(ERROR,

src/backend/replication/walsender.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ typedef void (*WalSndSendDataCallback) (void);
193193
static void WalSndLoop(WalSndSendDataCallback send_data);
194194
static void InitWalSenderSlot(void);
195195
static void WalSndKill(int code, Datum arg);
196-
static void WalSndShutdown(void) __attribute__((noreturn));
196+
static void WalSndShutdown(void) pg_attribute_noreturn;
197197
static void XLogSendPhysical(void);
198198
static void XLogSendLogical(void);
199199
static void WalSndDone(WalSndSendDataCallback send_data);

src/backend/utils/error/elog.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ static const char *
8686
err_gettext(const char *str)
8787
/* This extension allows gcc to check the format string for consistency with
8888
the supplied arguments. */
89-
__attribute__((format_arg(1)));
89+
pg_attribute_format_arg(1);
9090
static void set_errdata_field(MemoryContextData *cxt, char **ptr, const char *str);
9191

9292
/* Global variables */

src/backend/utils/misc/guc.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ char *GUC_check_errhint_string;
132132
static void
133133
do_serialize(char **destptr, Size *maxbytes, const char *fmt,...)
134134
/* This lets gcc check the format string for consistency. */
135-
__attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4)));
135+
pg_attribute_printf(3, 4);
136136

137137
static void set_config_sourcefile(const char *name, char *sourcefile,
138138
int sourceline);

src/bin/pg_ctl/pg_ctl.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ static void
120120
write_stderr(const char *fmt,...)
121121
/* This extension allows gcc to check the format string for consistency with
122122
the supplied arguments. */
123-
__attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
123+
pg_attribute_printf(1, 2);
124124
static void do_advice(void);
125125
static void do_help(void);
126126
static void set_mode(char *modeopt);

src/bin/pg_dump/parallel.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ static ParallelSlot *GetMyPSlot(ParallelState *pstate);
8181
static void
8282
parallel_msg_master(ParallelSlot *slot, const char *modulename,
8383
const char *fmt, va_list ap)
84-
__attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 0)));
84+
pg_attribute_printf(3, 0);
8585
static void archive_close_connection(int code, void *arg);
8686
static void ShutdownWorkersHard(ParallelState *pstate);
8787
static void WaitForTerminatingWorkers(ParallelState *pstate);

src/bin/pg_dump/pg_backup.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ extern void archputs(const char *s, Archive *AH);
280280
extern int
281281
archprintf(Archive *AH, const char *fmt,...)
282282
/* This extension allows gcc to check the format string */
283-
__attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
283+
pg_attribute_printf(2, 3);
284284

285285
#define appendStringLiteralAH(buf,str,AH) \
286286
appendStringLiteral(buf, str, (AH)->encoding, (AH)->std_strings)

src/bin/pg_dump/pg_backup_archiver.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ struct _tocEntry
378378
extern int parallel_restore(struct ParallelArgs *args);
379379
extern void on_exit_close_archive(Archive *AHX);
380380

381-
extern void warn_or_exit_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...) __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4)));
381+
extern void warn_or_exit_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...) pg_attribute_printf(3, 4);
382382

383383
extern void WriteTOC(ArchiveHandle *AH);
384384
extern void ReadTOC(ArchiveHandle *AH);
@@ -429,8 +429,8 @@ extern int ReconnectToServer(ArchiveHandle *AH, const char *dbname, const char *
429429
extern void DropBlobIfExists(ArchiveHandle *AH, Oid oid);
430430

431431
void ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH);
432-
int ahprintf(ArchiveHandle *AH, const char *fmt,...) __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
432+
int ahprintf(ArchiveHandle *AH, const char *fmt,...) pg_attribute_printf(2, 3);
433433

434-
void ahlog(ArchiveHandle *AH, int level, const char *fmt,...) __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4)));
434+
void ahlog(ArchiveHandle *AH, int level, const char *fmt,...) pg_attribute_printf(3, 4);
435435

436436
#endif

src/bin/pg_dump/pg_backup_tar.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ static void tarClose(ArchiveHandle *AH, TAR_MEMBER *TH);
115115
#ifdef __NOT_USED__
116116
static char *tarGets(char *buf, size_t len, TAR_MEMBER *th);
117117
#endif
118-
static int tarPrintf(ArchiveHandle *AH, TAR_MEMBER *th, const char *fmt,...) __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4)));
118+
static int tarPrintf(ArchiveHandle *AH, TAR_MEMBER *th, const char *fmt,...) pg_attribute_printf(3, 4);
119119

120120
static void _tarAddFile(ArchiveHandle *AH, TAR_MEMBER *th);
121121
static TAR_MEMBER *_tarPositionTo(ArchiveHandle *AH, const char *filename);

src/bin/pg_dump/pg_backup_utils.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ extern const char *progname;
3030
extern void set_dump_section(const char *arg, int *dumpSections);
3131
extern void
3232
write_msg(const char *modulename, const char *fmt,...)
33-
__attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
33+
pg_attribute_printf(2, 3);
3434
extern void
3535
vwrite_msg(const char *modulename, const char *fmt, va_list ap)
36-
__attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 0)));
36+
pg_attribute_printf(2, 0);
3737
extern void on_exit_nicely(on_exit_nicely_callback function, void *arg);
38-
extern void exit_nicely(int code) __attribute__((noreturn));
38+
extern void exit_nicely(int code) pg_attribute_noreturn;
3939

4040
extern void
4141
exit_horribly(const char *modulename, const char *fmt,...)
42-
__attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3), noreturn));
42+
pg_attribute_printf(2, 3) pg_attribute_noreturn;
4343

4444
#endif /* PG_BACKUP_UTILS_H */

src/bin/psql/common.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extern bool setQFout(const char *fname);
2121
extern void
2222
psql_error(const char *fmt,...)
2323
/* This lets gcc check the format string for consistency. */
24-
__attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
24+
pg_attribute_printf(1, 2);
2525

2626
extern void NoticeProcessor(void *arg, const char *message);
2727

src/bin/psql/large_obj.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
static void
1616
print_lo_result(const char *fmt,...)
17-
__attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
17+
pg_attribute_printf(1, 2);
1818

1919
static void
2020
print_lo_result(const char *fmt,...)

src/include/bootstrap/bootstrap.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ extern Form_pg_attribute attrtypes[MAXATTR];
3232
extern int numattr;
3333

3434

35-
extern void AuxiliaryProcessMain(int argc, char *argv[]) __attribute__((noreturn));
35+
extern void AuxiliaryProcessMain(int argc, char *argv[]) pg_attribute_noreturn;
3636

3737
extern void err_out(void);
3838

@@ -61,6 +61,6 @@ extern void boot_get_type_io_data(Oid typid,
6161
extern int boot_yyparse(void);
6262

6363
extern int boot_yylex(void);
64-
extern void boot_yyerror(const char *str) __attribute__((noreturn));
64+
extern void boot_yyerror(const char *str) pg_attribute_noreturn;
6565

6666
#endif /* BOOTSTRAP_H */

src/include/c.h

+43-6
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,6 @@
156156
#define dummyret char
157157
#endif
158158

159-
#ifndef __GNUC__
160-
#define __attribute__(_arg_)
161-
#endif
162-
163159
/* ----------------------------------------------------------------
164160
* Section 2: bool, true, false, TRUE, FALSE, NULL
165161
* ----------------------------------------------------------------
@@ -560,6 +556,47 @@ typedef NameData *Name;
560556
/* we don't currently need wider versions of the other ALIGN macros */
561557
#define MAXALIGN64(LEN) TYPEALIGN64(MAXIMUM_ALIGNOF, (LEN))
562558

559+
/* ----------------
560+
* Attribute macros
561+
*
562+
* GCC: https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
563+
* GCC: https://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html
564+
* Sunpro: https://docs.oracle.com/cd/E18659_01/html/821-1384/gjzke.html
565+
* XLC: http://www-01.ibm.com/support/knowledgecenter/SSGH2K_11.1.0/com.ibm.xlc111.aix.doc/language_ref/function_attributes.html
566+
* XLC: http://www-01.ibm.com/support/knowledgecenter/SSGH2K_11.1.0/com.ibm.xlc111.aix.doc/language_ref/type_attrib.html
567+
* ----------------
568+
*/
569+
570+
/* only GCC supports the unused attribute */
571+
#ifdef __GNUC__
572+
#define pg_attribute_unused __attribute__((unused))
573+
#else
574+
#define pg_attribute_unused
575+
#endif
576+
577+
/* GCC and XLC support format attributes */
578+
#if defined(__GNUC__) || defined(__IBMC__)
579+
#define pg_attribute_format_arg(a) __attribute__((format_arg(a)))
580+
#define pg_attribute_printf(f,a) __attribute__((format(PG_PRINTF_ATTRIBUTE, f, a)))
581+
#else
582+
#define pg_attribute_format_arg(a)
583+
#define pg_attribute_printf(f,a)
584+
#endif
585+
586+
/* GCC, Sunpro and XLC support aligned, packed and noreturn */
587+
#if defined(__GNUC__) || defined(__SUNPRO_C) || defined(__IBMC__)
588+
#define pg_attribute_aligned(a) __attribute__((aligned(a)))
589+
#define pg_attribute_noreturn __attribute__((noreturn))
590+
#define pg_attribute_packed __attribute__((packed))
591+
#else
592+
/*
593+
* NB: aligned and packed are not defined as empty as they affect code
594+
* functionality; they must be implemented by the compiler if they are to be
595+
* used.
596+
*/
597+
#define pg_attribute_noreturn
598+
#endif
599+
563600
/* ----------------------------------------------------------------
564601
* Section 6: assertions
565602
* ----------------------------------------------------------------
@@ -906,7 +943,7 @@ typedef NameData *Name;
906943
#ifdef USE_ASSERT_CHECKING
907944
#define PG_USED_FOR_ASSERTS_ONLY
908945
#else
909-
#define PG_USED_FOR_ASSERTS_ONLY __attribute__((unused))
946+
#define PG_USED_FOR_ASSERTS_ONLY pg_attribute_unused
910947
#endif
911948

912949

@@ -973,7 +1010,7 @@ typedef NameData *Name;
9731010
extern int
9741011
snprintf(char *str, size_t count, const char *fmt,...)
9751012
/* This extension allows gcc to check the format string */
976-
__attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4)));
1013+
pg_attribute_printf(3, 4);
9771014
#endif
9781015

9791016
#if !HAVE_DECL_VSNPRINTF

src/include/common/fe_memutils.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ extern void pfree(void *pointer);
2626
/* sprintf into a palloc'd buffer --- these are in psprintf.c */
2727
extern char *
2828
psprintf(const char *fmt,...)
29-
__attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
29+
pg_attribute_printf(1, 2);
3030
extern size_t
3131
pvsnprintf(char *buf, size_t len, const char *fmt, va_list args)
32-
__attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 0)));
32+
pg_attribute_printf(3, 0);
3333

3434
#endif /* FE_MEMUTILS_H */

0 commit comments

Comments
 (0)