Enclose all header content in include guards #36805

Manually merged
hebasto merged 2 commits from 261108-include-guard into master 2026-11-10 14:57:20 +00:00 AGit
hebasto commented 2026-11-08 16:17:29 +00:00 (Migrated from github.com)

From https://gcc.gnu.org/onlinedocs/cpp/Once-Only-Headers.html:

If a header file happens to be included twice, the compiler will process its contents twice. This is very likely to cause an error, e.g. when the compiler sees the same structure definition twice. Even if it does not, it will certainly waste time.

This PR fixes the three headers that have content outside their guards and modifies test/lint/lint-include-guards.py to catch such violations in the future.

A prerequisite for https://github.com/bitcoin/bitcoin/pull/36808.

From https://gcc.gnu.org/onlinedocs/cpp/Once-Only-Headers.html: > If a header file happens to be included twice, the compiler will process its contents twice. This is very likely to cause an error, e.g. when the compiler sees the same structure definition twice. Even if it does not, it will certainly waste time. This PR fixes the three headers that have content outside their guards and modifies [`test/lint/lint-include-guards.py`](https://github.com/bitcoin/bitcoin/blob/master/test/lint/lint-include-guards.py) to catch such violations in the future. A prerequisite for https://github.com/bitcoin/bitcoin/pull/36808.
DrahtBot commented 2026-11-08 16:17:38 +00:00 (Migrated from github.com)

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
ACK maflcko
Stale ACK jpk68

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36808 (iwyu: Fix warnings in src/test/fuzz and treat them as errors by hebasto)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36805 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36805.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36805#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | ACK | [maflcko](https://github.com/bitcoin/bitcoin/pull/36805#issuecomment-6769088424) | | Stale ACK | [jpk68](https://github.com/bitcoin/bitcoin/pull/36805#issuecomment-6726025403) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#36808](https://github.com/bitcoin/bitcoin/pull/36808) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36808.svg"></sub> (iwyu: Fix warnings in `src/test/fuzz` and treat them as errors by hebasto) If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. <!--5faf32d7da4f0f540f40219e4f7537a3-->
First-time contributor

🔵 Suggestion · Lint does not verify that the final #endif closes the include guard

The lint can accept content outside an early-closed guard when a later conditional supplies the expected final #endif; no current tracked header with this layout was identified.

Location: test/lint/lint-include-guards.py:80 (head).

Review coverage was incomplete.


Reviewed: ca14da90250f · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:7eebed8027c96ac27bcd79eeb7a88e35799cd96eb8fbacf34d3257986a959f64:cdc1bd61213c9363d5bb4c7b7ee073ccfd6fecdafbb2c4c18d9d9e031a6afcf8 --> <!-- ralph-base:232cb5410a139de15ac1f6233aab1e89ba2a408f --> <!-- ralph-head:ca14da90250fb5211983021a754473cde194d59e --> <!-- ralph-case:2fbaff5c0794a4f48253ec88e78d4cc010b8cd130d2b1e96f2f3e2f1845a74c9 --> **🔵 Suggestion · Lint does not verify that the final `#endif` closes the include guard** The lint can accept content outside an early-closed guard when a later conditional supplies the expected final `#endif`; no current tracked header with this layout was identified. Location: [`test/lint/lint-include-guards.py:80`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/ca14da90250fb5211983021a754473cde194d59e/test/lint/lint-include-guards.py#L80>) (head). Review coverage was incomplete. <!-- Review tier: standard --> --- Reviewed: [`ca14da90250f`](<https://git.fish.foo/bitcoin/bitcoin/commit/ca14da90250fb5211983021a754473cde194d59e>) · [Review report](<https://review.fish.foo/report/36805-ca14da90250fb5211983021a754473cde194d59e-2452-4.html>) · [Previous reports](<https://review.fish.foo/traces/36805/>) [permalink](<https://review.fish.foo/report/36805-ca14da90250fb5211983021a754473cde194d59e-2452-4.html>)
jpk68 commented 2026-11-08 19:27:04 +00:00 (Migrated from github.com)
ACK c385c1ff18b5f7e5bb0ff658bc5ba467897904ec
maflcko (Migrated from github.com) reviewed 2026-11-09 07:13:56 +00:00
maflcko (Migrated from github.com) commented 2026-11-09 07:13:56 +00:00

We don't use /* comments for the header and we shouldn't start doing that, so this dead logic should be dropped

We don't use /* comments for the header and we shouldn't start doing that, so this dead logic should be dropped
hebasto (Migrated from github.com) reviewed 2026-11-09 09:30:56 +00:00
hebasto (Migrated from github.com) commented 2026-11-09 09:30:56 +00:00
https://github.com/bitcoin/bitcoin/blob/4ef7904c39fa2c9eedc66d1a75caaf34c0c531b9/src/util/moneystr.h#L6-L8 or https://github.com/bitcoin/bitcoin/blob/4ef7904c39fa2c9eedc66d1a75caaf34c0c531b9/src/torcontrol.h#L5-L7
maflcko (Migrated from github.com) reviewed 2026-11-09 10:31:42 +00:00
maflcko (Migrated from github.com) commented 2026-11-09 10:31:42 +00:00

Sure, but they are wrong, because doxygen doesn't attach them to the guard, but to the next variable:

[DEFAULT_TOR_SOCKS_PORT](https://doxygen.bitcoincore.org/torcontrol_8h.html#a3213ad4529bf3b1a9a0665ce210d19b1) {9050}
 	Functionality for communicating with Tor. 

Implying that this would be possible doesn't seem useful. I guess I missed that there are existing violations, but it may be best to fix them instead of ignoring them?

Sure, but they are wrong, because doxygen doesn't attach them to the guard, but to the next variable: ``` [DEFAULT_TOR_SOCKS_PORT](https://doxygen.bitcoincore.org/torcontrol_8h.html#a3213ad4529bf3b1a9a0665ce210d19b1) {9050} Functionality for communicating with Tor. ``` Implying that this would be possible doesn't seem useful. I guess I missed that there are existing violations, but it may be best to fix them instead of ignoring them?
hebasto (Migrated from github.com) reviewed 2026-11-09 14:13:52 +00:00
hebasto (Migrated from github.com) commented 2026-11-09 14:13:52 +00:00
Right. Fixed in https://github.com/bitcoin/bitcoin/pull/36815. Marking this PR as a draft for now.
hebasto commented 2026-11-10 11:34:30 +00:00 (Migrated from github.com)

Right. Fixed in https://github.com/bitcoin/bitcoin/pull/36815.

Marking this PR as a draft for now.

Rebased on top of the merged bitcoin/bitcoin#36815 and undrafted.

> Right. Fixed in https://github.com/bitcoin/bitcoin/pull/36815. > > Marking this PR as a draft for now. Rebased on top of the merged bitcoin/bitcoin#36815 and undrafted.
hebasto (Migrated from github.com) reviewed 2026-11-10 11:35:16 +00:00
hebasto (Migrated from github.com) commented 2026-11-10 11:35:16 +00:00

... this dead logic should be dropped

Thanks! Done.

> ... this dead logic should be dropped Thanks! Done.
maflcko (Migrated from github.com) reviewed 2026-11-10 12:54:37 +00:00
maflcko (Migrated from github.com) commented 2026-11-10 12:54:37 +00:00

Seems a lot of hand-holding to use regex here and double-parsing just to print a line number. Should be trivial to just use something like:

with open(header_file, 'r') as f:
    header_file_contents = f.readlines()

# Filter out empty lines ('\n') and lines starting with '//'
code = [line for line in header_file_contents if line != '\n' and not line.startswith('//')]

if (
    len(code) < 3
    or code[0] != f'#ifndef {header_id}\n'
    or code[1] != f'#define {header_id}\n'
    or code[-1] != f'#endif // {header_id}\n'
):
    print(f'{header_file} seems to be missing the expected '
          'include guard to prevent the double inclusion problem:')
    print(f'  #ifndef {header_id}')
    print(f'  #define {header_id}')
    print('  ...')
    print(f'  #endif // {header_id}\n')
    print("Note that only empty lines or //-comment lines are allowed outside the header.")
    exit_code = 1

If you feel strongly about hand-holding, you can echo back code[0] (or so)

Seems a lot of hand-holding to use regex here and double-parsing just to print a line number. Should be trivial to just use something like: ```py with open(header_file, 'r') as f: header_file_contents = f.readlines() # Filter out empty lines ('\n') and lines starting with '//' code = [line for line in header_file_contents if line != '\n' and not line.startswith('//')] if ( len(code) < 3 or code[0] != f'#ifndef {header_id}\n' or code[1] != f'#define {header_id}\n' or code[-1] != f'#endif // {header_id}\n' ): print(f'{header_file} seems to be missing the expected ' 'include guard to prevent the double inclusion problem:') print(f' #ifndef {header_id}') print(f' #define {header_id}') print(' ...') print(f' #endif // {header_id}\n') print("Note that only empty lines or //-comment lines are allowed outside the header.") exit_code = 1 ``` If you feel strongly about hand-holding, you can echo back `code[0]` (or so)
maflcko (Migrated from github.com) reviewed 2026-11-10 12:59:00 +00:00
maflcko (Migrated from github.com) commented 2026-11-10 12:59:00 +00:00

otherwise:

lgtm 9b0ef7e98b00409e6b8871bd6c8fbcad6ffc26b7~0 🥐

Show signature

Signature:

untrusted comment: signature from minisign secret key on empty file; verify via: minisign -Vm "${path_to_any_empty_file}" -P RWTRmVTMeKV5noAMqVlsMugDDCyyTSbA3Re5AkUrhvLVln0tSaFWglOw -x "${path_to_this_whole_four_line_signature_blob}"
RUTRmVTMeKV5npGrKx1nqXCw5zeVHdtdYURB/KlyA/LMFgpNCs+SkW9a8N95d+U4AP1RJMi+krxU1A3Yux4bpwZNLvVBKy0wLgM=
trusted comment: lgtm 9b0ef7e98b00409e6b8871bd6c8fbcad6ffc26b7~0 🥐
518oFgHZ1GRzh2jT2MXTBB7Wuc04tNbX6v7wd29WfUC3WDKGhEZmA9RKN4hSncwwmXKRAtKw+4kbeO26GtwVCg==
otherwise: lgtm 9b0ef7e98b00409e6b8871bd6c8fbcad6ffc26b7~0 🥐 <details><summary>Show signature</summary> Signature: ``` untrusted comment: signature from minisign secret key on empty file; verify via: minisign -Vm "${path_to_any_empty_file}" -P RWTRmVTMeKV5noAMqVlsMugDDCyyTSbA3Re5AkUrhvLVln0tSaFWglOw -x "${path_to_this_whole_four_line_signature_blob}" RUTRmVTMeKV5npGrKx1nqXCw5zeVHdtdYURB/KlyA/LMFgpNCs+SkW9a8N95d+U4AP1RJMi+krxU1A3Yux4bpwZNLvVBKy0wLgM= trusted comment: lgtm 9b0ef7e98b00409e6b8871bd6c8fbcad6ffc26b7~0 🥐 518oFgHZ1GRzh2jT2MXTBB7Wuc04tNbX6v7wd29WfUC3WDKGhEZmA9RKN4hSncwwmXKRAtKw+4kbeO26GtwVCg== ``` </details>
hebasto (Migrated from github.com) reviewed 2026-11-10 13:39:52 +00:00
hebasto (Migrated from github.com) commented 2026-11-10 13:39:52 +00:00

Seems a lot of hand-holding to use regex here and double-parsing just to print a line number. Should be trivial to just use something like...

Thanks! Taken.

> Seems a lot of hand-holding to use regex here and double-parsing just to print a line number. Should be trivial to just use something like... Thanks! Taken.
maflcko commented 2026-11-10 13:51:27 +00:00 (Migrated from github.com)

I tested the build compile failure fixed in the first commit, but I haven't tested the linter.

review ACK ca14da9025 🖨

Show signature

Signature:

untrusted comment: signature from minisign secret key on empty file; verify via: minisign -Vm "${path_to_any_empty_file}" -P RWTRmVTMeKV5noAMqVlsMugDDCyyTSbA3Re5AkUrhvLVln0tSaFWglOw -x "${path_to_this_whole_four_line_signature_blob}"
RUTRmVTMeKV5npGrKx1nqXCw5zeVHdtdYURB/KlyA/LMFgpNCs+SkW9a8N95d+U4AP1RJMi+krxU1A3Yux4bpwZNLvVBKy0wLgM=
trusted comment: review ACK ca14da90250fb5211983021a754473cde194d59e 🖨
OztzZjx9+exeeeBYZcxVwM5TYYpKYK7vlACxKnrXVSmh4xs+tiZqJy1BYyt6X2Y18krkeE3DVVbT6PNzC7FoBQ==
I tested the build compile failure fixed in the first commit, but I haven't tested the linter. review ACK ca14da90250fb5211983021a754473cde194d59e 🖨 <details><summary>Show signature</summary> Signature: ``` untrusted comment: signature from minisign secret key on empty file; verify via: minisign -Vm "${path_to_any_empty_file}" -P RWTRmVTMeKV5noAMqVlsMugDDCyyTSbA3Re5AkUrhvLVln0tSaFWglOw -x "${path_to_this_whole_four_line_signature_blob}" RUTRmVTMeKV5npGrKx1nqXCw5zeVHdtdYURB/KlyA/LMFgpNCs+SkW9a8N95d+U4AP1RJMi+krxU1A3Yux4bpwZNLvVBKy0wLgM= trusted comment: review ACK ca14da90250fb5211983021a754473cde194d59e 🖨 OztzZjx9+exeeeBYZcxVwM5TYYpKYK7vlACxKnrXVSmh4xs+tiZqJy1BYyt6X2Y18krkeE3DVVbT6PNzC7FoBQ== ``` </details>
hebasto commented 2026-11-10 14:05:57 +00:00 (Migrated from github.com)

... but I haven't tested the linter.

A CI job with the reverted first commit is available here: https://github.com/hebasto/bitcoin/actions/runs/42830416553/job/129878658971.

UPDATE:

[135](https://github.com/hebasto/bitcoin/actions/runs/42830416553/job/129878658971#step:5:1136)
src/net_permissions.h seems to be missing the expected include guard to prevent the double inclusion problem:
  #ifndef BITCOIN_NET_PERMISSIONS_H
  #define BITCOIN_NET_PERMISSIONS_H
  ...
  #endif // BITCOIN_NET_PERMISSIONS_H
Note that only empty lines or //-comment lines are allowed outside the include guard.
src/txgraph.h seems to be missing the expected include guard to prevent the double inclusion problem:
  #ifndef BITCOIN_TXGRAPH_H
  #define BITCOIN_TXGRAPH_H
  ...
  #endif // BITCOIN_TXGRAPH_H
Note that only empty lines or //-comment lines are allowed outside the include guard.
src/wallet/test/init_test_fixture.h seems to be missing the expected include guard to prevent the double inclusion problem:
  #ifndef BITCOIN_WALLET_TEST_INIT_TEST_FIXTURE_H
  #define BITCOIN_WALLET_TEST_INIT_TEST_FIXTURE_H
  ...
  #endif // BITCOIN_WALLET_TEST_INIT_TEST_FIXTURE_H
Note that only empty lines or //-comment lines are allowed outside the include guard.
^---- ⚠️ Failure generated from lint-include-guards.py
> ... but I haven't tested the linter. A CI job with the reverted first commit is available here: https://github.com/hebasto/bitcoin/actions/runs/42830416553/job/129878658971. UPDATE: ``` [135](https://github.com/hebasto/bitcoin/actions/runs/42830416553/job/129878658971#step:5:1136) src/net_permissions.h seems to be missing the expected include guard to prevent the double inclusion problem: #ifndef BITCOIN_NET_PERMISSIONS_H #define BITCOIN_NET_PERMISSIONS_H ... #endif // BITCOIN_NET_PERMISSIONS_H Note that only empty lines or //-comment lines are allowed outside the include guard. src/txgraph.h seems to be missing the expected include guard to prevent the double inclusion problem: #ifndef BITCOIN_TXGRAPH_H #define BITCOIN_TXGRAPH_H ... #endif // BITCOIN_TXGRAPH_H Note that only empty lines or //-comment lines are allowed outside the include guard. src/wallet/test/init_test_fixture.h seems to be missing the expected include guard to prevent the double inclusion problem: #ifndef BITCOIN_WALLET_TEST_INIT_TEST_FIXTURE_H #define BITCOIN_WALLET_TEST_INIT_TEST_FIXTURE_H ... #endif // BITCOIN_WALLET_TEST_INIT_TEST_FIXTURE_H Note that only empty lines or //-comment lines are allowed outside the include guard. ^---- ⚠️ Failure generated from lint-include-guards.py ```
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36805
No description provided.