From b185a841272ef14af7dcdf99759d0e6d775f6746 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Thu, 9 Jan 2025 12:34:09 +0100 Subject: [PATCH v2 1/3] 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. - Generated header included in utils/syscache.h is marked exported. This is a very commonly used include and this avoids lots of complaints. Discussion: https://www.postgresql.org/message-id/flat/9395d484-eff4-47c2-b276-8e228526c8ae@eisentraut.org --- 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 ++ 12 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/include/c.h b/src/include/c.h index ab6a49aeca4..a14c6315162 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 33b4755a664..e7bdf909f7b 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 2e2d64f4a26..aad0886c054 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 aca6d727d30..c82f34726f7 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" +#include "utils/probes.h" /* IWYU pragma: export */ #endif /* PG_TRACE_H */ diff --git a/src/include/pgstat.h b/src/include/pgstat.h index 6475889c58c..8d1c8946b9d 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 f3df9c94639..d9891d3805d 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 cdce4823e7e..8a41a668687 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 dc6dc6e812a..a3f65b1dca4 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 537fd03e8bd..3039713259e 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 e9c74e1faf8..0a82e31d83a 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