Avoid circular header file dependency - Mailing list pgsql-hackers

From Bertrand Drouvot
Subject Avoid circular header file dependency
Date
Msg-id aAxQ6fcY5QQV1lo3@ip-10-97-1-34.eu-west-3.compute.internal
Whole thread Raw
Responses Re: Avoid circular header file dependency
List pgsql-hackers
Hi hackers,

While working on wait events I faced some compilation issues due to circular
header file dependency (introduced in fa88928470b5) between wait_event.h and
wait_event_types.h. Those files have include guards but could still lead to
compilation issues in some cases due to the circular dependency. 

Currently, on HEAD, this doesn't cause any issues but I think it's better to
avoid circular header file dependencies (harder to maintain and understand).

Please find attached a patch to $SUBJECT between those 2 header files: it
extracts (in a new header file) from wait_event.h what is strictly needed in
wait_event_types.h.

Out of curiosity, I ran clang-tidy with misc-header-include-cycle ([1]) and it
also reports:

../src/pl/plpython/plpy_util.h:9:10: warning: circular header file dependency detected while including 'plpython.h'

This one worries me less because plpy_util.h only contains simple external 
function declarations.

I was surprised that clang-tidy does report only the plpy_util.h one (in addition
to the wait_event_types.h one) so I wondered if misc-header-include-cycle was
failing to discover circular dependencies in nested cases.

I did a quick test with: a.h → c.h → b.h → d.h → a.h and it found it:

"
../d.h:6:10: warning: circular header file dependency detected while including 'a.h'
    6 | #include "a.h"
      |          ^
../b.h:6:10: note: 'd.h' included from here
    6 | #include "d.h"
      |          ^
../c.h:6:10: note: 'b.h' included from here
    6 | #include "b.h"
      |          ^
../a.h:6:10: note: 'c.h' included from here
    6 | #include "c.h"
      |          ^
../main.c:2:10: note: 'a.h' included from here
    2 | #include "a.h"
"

So it looks like that our code base only contains those 2: plpy_util.h and
wait_event_types.h cases.

Thoughts?

[1]: https://clang.llvm.org/extra/clang-tidy/checks/misc/header-include-cycle.html

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com

Attachment

pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Re: Sanding down some edge cases for PL/pgSQL reserved words
Next
From: jian he
Date:
Subject: Re: Support NOT VALID / VALIDATE constraint options for named NOT NULL constraints