IWYU widely useful pragmas
authorPeter Eisentraut <peter@eisentraut.org>
Wed, 15 Jan 2025 17:57:53 +0000 (18:57 +0100)
committerPeter Eisentraut <peter@eisentraut.org>
Wed, 15 Jan 2025 17:57:53 +0000 (18:57 +0100)
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

12 files changed:
src/include/c.h
src/include/getopt_long.h
src/include/pg_getopt.h
src/include/pg_trace.h
src/include/pgstat.h
src/include/port/pg_iovec.h
src/include/port/pg_pthread.h
src/include/postgres.h
src/include/postgres_ext.h
src/include/postgres_fe.h
src/include/utils/syscache.h
src/interfaces/libpq/libpq-int.h

index ab6a49aeca48030db847916f92205759e669acb1..a14c6315162dec35ecade3fb1975da0bf27e73e9 100644 (file)
@@ -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 */
index 33b4755a664afc17f33336f2668b4fa76a055fae..e7bdf909f7bed6000bc64204350d96b948dedbbf 100644 (file)
@@ -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
 
index 2e2d64f4a2603ece97feaee6254853d556ae5736..aad0886c054d27f03db01ee1e0ae648252e10034 100644 (file)
  *
  * 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 <unistd.h>
+#include <unistd.h>                /* IWYU pragma: export */
 
 /* rely on the system's getopt.h if present */
 #ifdef HAVE_GETOPT_H
-#include <getopt.h>
+#include <getopt.h>                /* IWYU pragma: export */
 #endif
 
 /*
index aca6d727d309f6705bdfa0f82c01dcd11128f266..c82f34726f74e903a5042647d334881d7d2e90ae 100644 (file)
@@ -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 */
index 454a2cce0e5af40bc66b1d98b5361a13d25210ee..a878402f502e3c609c4615b0885b9400bc6bc111 100644 (file)
 #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/pgstat_kind.h"
 #include "utils/relcache.h"
-#include "utils/wait_event.h"  /* for backward compatibility */
+#include "utils/wait_event.h"  /* for backward compatibility */    /* IWYU pragma: export */
 
 
 /* ----------
index f3df9c946396609dec9030dcc2110262e3d71a67..d9891d3805d1928017ff485f2db2c991caf8b02c 100644 (file)
@@ -16,7 +16,7 @@
 #ifndef WIN32
 
 #include <limits.h>
-#include <sys/uio.h>
+#include <sys/uio.h>           /* IWYU pragma: export */
 #include <unistd.h>
 
 #else
index d102ce9d6f33e10deddf9895daf5fd81f3d79fd0..4dbb3c8a52f1190e27990399f91eb0e7357f4466 100644 (file)
@@ -13,7 +13,7 @@
 #ifndef PG_PTHREAD_H
 #define PG_PTHREAD_H
 
-#include <pthread.h>
+#include <pthread.h>           /* IWYU pragma: export */
 
 #ifndef HAVE_PTHREAD_BARRIER_WAIT
 
index cdce4823e7ef3c286ec9c5cd5088efee7e79149d..8a41a6686877ff42bb11b1d0da118a8b9cb350f4 100644 (file)
@@ -14,6 +14,7 @@
  *
  *-------------------------------------------------------------------------
  */
+/* IWYU pragma: always_keep */
 /*
  *----------------------------------------------------------------
  *  TABLE OF CONTENTS
 #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
  * ----------------------------------------------------------------
index 202eb04962205ce410490f58d920cabac5a76409..9f2e3c52972d8db415e7b53766577880bf55093a 100644 (file)
@@ -19,6 +19,7 @@
  *
  *-------------------------------------------------------------------------
  */
+/* IWYU pragma: always_keep */
 
 #ifndef POSTGRES_EXT_H
 #define POSTGRES_EXT_H
index dc6dc6e812ac4a5ad3d512c24b28eb1231dec78f..a3f65b1dca45fb5c952a64f42b091a2f7f0b4458 100644 (file)
@@ -15,6 +15,7 @@
  *
  *-------------------------------------------------------------------------
  */
+/* IWYU pragma: always_keep */
 #ifndef POSTGRES_FE_H
 #define POSTGRES_FE_H
 
 #define FRONTEND 1
 #endif
 
+/* IWYU pragma: begin_exports */
+
 #include "c.h"
 
 #include "common/fe_memutils.h"
 
+/* IWYU pragma: end_exports */
+
 #endif                         /* POSTGRES_FE_H */
index 537fd03e8bd3b3996b24c4f94e27a63fdf7dfd4f..3039713259ecdd9efcf95c8aca113c1e1a68b7f8 100644 (file)
@@ -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);
index 031b93c535b9422a2b91337f0a5e44e8664ec053..4be5fd7ae4fce36dd42285dbde85fe76903af8a1 100644 (file)
@@ -44,6 +44,7 @@
 #include "fe-auth-sasl.h"
 #include "pqexpbuffer.h"
 
+/* IWYU pragma: begin_exports */
 #ifdef ENABLE_GSS
 #if defined(HAVE_GSSAPI_H)
 #include <gssapi.h>
@@ -51,6 +52,7 @@
 #include <gssapi/gssapi.h>
 #endif
 #endif
+/* IWYU pragma: end_exports */
 
 #ifdef ENABLE_SSPI
 #define SECURITY_WIN32