From 0ee1c6c9f620ca8e1e3f9c601e5aa335a95c91d3 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Mon, 9 Dec 2024 12:25:10 +0100 Subject: [PATCH 1/2] IWYU widely useful pragmas Add various widely useful "IWYU pragma" annotations, such as - Common header files such as c.h, postgres.h should be "always_keep". - System headers included in c.h, postgres.h etc. should be considered "export". - Some portability headers such as getopt_long.h should be "always_keep", so they are not considered superfluous on some platforms. - Certain system headers included from portability headers should be considered "export" because the purpose of the portability header is to wrap them. - Superfluous includes marked as "for backward compatibility" get a formal IWYU annotation. - Special handling of access/rmgrlist.h in src/bin/pg_waldump/rmgrdesc.c, similar to what was already done in src/backend/access/transam/rmgr.c. - Generated header included in utils/syscache.h is marked exported. This is a very commonly used include and this avoids lots of complaints. --- src/bin/pg_waldump/rmgrdesc.c | 10 +++++++--- src/include/c.h | 5 +++++ src/include/getopt_long.h | 3 ++- src/include/pg_getopt.h | 5 +++-- src/include/pg_trace.h | 2 +- src/include/pgstat.h | 6 +++--- src/include/port/pg_iovec.h | 2 +- src/include/port/pg_pthread.h | 2 +- src/include/postgres.h | 5 +++++ src/include/postgres_ext.h | 1 + src/include/postgres_fe.h | 5 +++++ src/include/utils/syscache.h | 2 +- src/interfaces/libpq/libpq-int.h | 2 ++ 13 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/bin/pg_waldump/rmgrdesc.c b/src/bin/pg_waldump/rmgrdesc.c index 6b8c17bb4c4..f5f53ceda32 100644 --- a/src/bin/pg_waldump/rmgrdesc.c +++ b/src/bin/pg_waldump/rmgrdesc.c @@ -8,6 +8,12 @@ #define FRONTEND 1 #include "postgres.h" +#include "access/rmgr.h" +#include "access/xlog_internal.h" +#include "rmgrdesc.h" + +/* includes needed for "access/rmgrlist.h" */ +/* IWYU pragma: begin_keep */ #include "access/brin_xlog.h" #include "access/clog.h" #include "access/commit_ts.h" @@ -18,19 +24,17 @@ #include "access/heapam_xlog.h" #include "access/multixact.h" #include "access/nbtxlog.h" -#include "access/rmgr.h" #include "access/spgxlog.h" #include "access/xact.h" -#include "access/xlog_internal.h" #include "catalog/storage_xlog.h" #include "commands/dbcommands_xlog.h" #include "commands/sequence.h" #include "commands/tablespace.h" #include "replication/message.h" #include "replication/origin.h" -#include "rmgrdesc.h" #include "storage/standbydefs.h" #include "utils/relmapper.h" +/* IWYU pragma: end_keep */ #define PG_RMGR(symname,name,redo,desc,identify,startup,cleanup,mask,decode) \ { name, desc, identify}, diff --git a/src/include/c.h b/src/include/c.h index 13bb39fdef3..884128e781d 100644 --- a/src/include/c.h +++ b/src/include/c.h @@ -16,6 +16,7 @@ * *------------------------------------------------------------------------- */ +/* IWYU pragma: always_keep */ /* *---------------------------------------------------------------- * TABLE OF CONTENTS @@ -46,6 +47,8 @@ #ifndef C_H #define C_H +/* IWYU pragma: begin_exports */ + /* * These headers must be included before any system headers, because on some * platforms they affect the behavior of the system headers (for example, by @@ -1327,4 +1330,6 @@ typedef intptr_t sigjmp_buf[5]; /* /port compatibility functions */ #include "port.h" +/* IWYU pragma: end_exports */ + #endif /* C_H */ diff --git a/src/include/getopt_long.h b/src/include/getopt_long.h index 0fdbefae7fb..a15ebbc0037 100644 --- a/src/include/getopt_long.h +++ b/src/include/getopt_long.h @@ -6,10 +6,11 @@ * * src/include/getopt_long.h */ +/* IWYU pragma: always_keep */ #ifndef GETOPT_LONG_H #define GETOPT_LONG_H -#include "pg_getopt.h" +#include "pg_getopt.h" /* IWYU pragma: export */ #ifndef HAVE_STRUCT_OPTION diff --git a/src/include/pg_getopt.h b/src/include/pg_getopt.h index c87ea20b14f..698cc61ce83 100644 --- a/src/include/pg_getopt.h +++ b/src/include/pg_getopt.h @@ -15,15 +15,16 @@ * * src/include/pg_getopt.h */ +/* IWYU pragma: always_keep */ #ifndef PG_GETOPT_H #define PG_GETOPT_H /* POSIX says getopt() is provided by unistd.h */ -#include +#include /* IWYU pragma: export */ /* rely on the system's getopt.h if present */ #ifdef HAVE_GETOPT_H -#include +#include /* IWYU pragma: export */ #endif /* diff --git a/src/include/pg_trace.h b/src/include/pg_trace.h index bae819ab466..00c172d7435 100644 --- a/src/include/pg_trace.h +++ b/src/include/pg_trace.h @@ -12,6 +12,6 @@ #ifndef PG_TRACE_H #define PG_TRACE_H -#include "utils/probes.h" /* pgrminclude ignore */ +#include "utils/probes.h" /* pgrminclude ignore */ /* IWYU pragma: export */ #endif /* PG_TRACE_H */ diff --git a/src/include/pgstat.h b/src/include/pgstat.h index 59c28b4aca8..5fe9158559f 100644 --- a/src/include/pgstat.h +++ b/src/include/pgstat.h @@ -16,10 +16,10 @@ #include "portability/instr_time.h" #include "postmaster/pgarch.h" /* for MAX_XFN_CHARS */ #include "replication/conflict.h" -#include "utils/backend_progress.h" /* for backward compatibility */ -#include "utils/backend_status.h" /* for backward compatibility */ +#include "utils/backend_progress.h" /* for backward compatibility */ /* IWYU pragma: export */ +#include "utils/backend_status.h" /* for backward compatibility */ /* IWYU pragma: export */ #include "utils/relcache.h" -#include "utils/wait_event.h" /* for backward compatibility */ +#include "utils/wait_event.h" /* for backward compatibility */ /* IWYU pragma: export */ /* ---------- diff --git a/src/include/port/pg_iovec.h b/src/include/port/pg_iovec.h index e5fe677b371..7df67b081fa 100644 --- a/src/include/port/pg_iovec.h +++ b/src/include/port/pg_iovec.h @@ -16,7 +16,7 @@ #ifndef WIN32 #include -#include +#include /* IWYU pragma: export */ #include #else diff --git a/src/include/port/pg_pthread.h b/src/include/port/pg_pthread.h index d102ce9d6f3..4dbb3c8a52f 100644 --- a/src/include/port/pg_pthread.h +++ b/src/include/port/pg_pthread.h @@ -13,7 +13,7 @@ #ifndef PG_PTHREAD_H #define PG_PTHREAD_H -#include +#include /* IWYU pragma: export */ #ifndef HAVE_PTHREAD_BARRIER_WAIT diff --git a/src/include/postgres.h b/src/include/postgres.h index 5d5fd7813e8..65e574c8570 100644 --- a/src/include/postgres.h +++ b/src/include/postgres.h @@ -14,6 +14,7 @@ * *------------------------------------------------------------------------- */ +/* IWYU pragma: always_keep */ /* *---------------------------------------------------------------- * TABLE OF CONTENTS @@ -42,10 +43,14 @@ #ifndef POSTGRES_H #define POSTGRES_H +/* IWYU pragma: begin_exports */ + #include "c.h" #include "utils/elog.h" #include "utils/palloc.h" +/* IWYU pragma: end_exports */ + /* ---------------------------------------------------------------- * Section 1: Datum type + support functions * ---------------------------------------------------------------- diff --git a/src/include/postgres_ext.h b/src/include/postgres_ext.h index 202eb049622..9f2e3c52972 100644 --- a/src/include/postgres_ext.h +++ b/src/include/postgres_ext.h @@ -19,6 +19,7 @@ * *------------------------------------------------------------------------- */ +/* IWYU pragma: always_keep */ #ifndef POSTGRES_EXT_H #define POSTGRES_EXT_H diff --git a/src/include/postgres_fe.h b/src/include/postgres_fe.h index 9c605daa365..33eb594380c 100644 --- a/src/include/postgres_fe.h +++ b/src/include/postgres_fe.h @@ -15,6 +15,7 @@ * *------------------------------------------------------------------------- */ +/* IWYU pragma: always_keep */ #ifndef POSTGRES_FE_H #define POSTGRES_FE_H @@ -22,8 +23,12 @@ #define FRONTEND 1 #endif +/* IWYU pragma: begin_exports */ + #include "c.h" #include "common/fe_memutils.h" +/* IWYU pragma: end_exports */ + #endif /* POSTGRES_FE_H */ diff --git a/src/include/utils/syscache.h b/src/include/utils/syscache.h index b541911c8fc..c71934f1e86 100644 --- a/src/include/utils/syscache.h +++ b/src/include/utils/syscache.h @@ -20,7 +20,7 @@ #include "access/htup.h" /* we intentionally do not include utils/catcache.h here */ -#include "catalog/syscache_ids.h" +#include "catalog/syscache_ids.h" /* IWYU pragma: export */ extern void InitCatalogCache(void); extern void InitCatalogCachePhase2(void); diff --git a/src/interfaces/libpq/libpq-int.h b/src/interfaces/libpq/libpq-int.h index 4606eb2ad34..3fff069598e 100644 --- a/src/interfaces/libpq/libpq-int.h +++ b/src/interfaces/libpq/libpq-int.h @@ -41,6 +41,7 @@ #include "fe-auth-sasl.h" #include "pqexpbuffer.h" +/* IWYU pragma: begin_exports */ #ifdef ENABLE_GSS #if defined(HAVE_GSSAPI_H) #include @@ -48,6 +49,7 @@ #include #endif #endif +/* IWYU pragma: end_exports */ #ifdef ENABLE_SSPI #define SECURITY_WIN32 -- 2.47.1