Enclose all header content in include guards #36805

pull hebasto wants to merge 2 commits into bitcoin:master from hebasto:261108-include-guard changing 4 files +17 −17
  1. hebasto commented at 4:17 PM on November 8, 2026: member

    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 #36808.

  2. DrahtBot commented at 4:17 PM on November 8, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    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 <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 <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-->

  3. jpk68 commented at 7:27 PM on November 8, 2026: contributor

    ACK c385c1ff18b5f7e5bb0ff658bc5ba467897904ec

  4. in test/lint/lint-include-guards.py:69 in c385c1ff18 outdated
      60 | @@ -61,6 +61,27 @@ def _get_header_id(header_file: str) -> str:
      61 |      return header_id
      62 |  
      63 |  
      64 | +def _is_comment_or_blank(line: str, in_block_comment: bool) -> tuple[bool, bool]:
      65 | +    """ Helper function to tell whether a line carries no code.
      66 | +
      67 | +    Args:
      68 | +        line: A line of the header file.
      69 | +        in_block_comment: Whether the previous line left a /* ... */ comment open.
    


    maflcko commented at 7:13 AM on November 9, 2026:

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



    maflcko commented at 10:31 AM on November 9, 2026:

    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 commented at 2:13 PM on November 9, 2026:

    Right. Fixed in #36815.

    Marking this PR as a draft for now.


    hebasto commented at 11:35 AM on November 10, 2026:

    ... this dead logic should be dropped

    Thanks! Done.

  5. hebasto marked this as a draft on Nov 9, 2026
  6. hebasto force-pushed on Nov 9, 2026
  7. hebasto force-pushed on Nov 9, 2026
  8. DrahtBot added the label CI failed on Nov 9, 2026
  9. DrahtBot removed the label CI failed on Nov 9, 2026
  10. fanquake referenced this in commit 229e79321c on Nov 10, 2026
  11. hebasto force-pushed on Nov 10, 2026
  12. hebasto commented at 11:34 AM on November 10, 2026: member

    Right. Fixed in #36815.

    Marking this PR as a draft for now.

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

  13. hebasto marked this as ready for review on Nov 10, 2026
  14. Enclose all header content in include guards 00e1f35524
  15. lint: Require all code in a header to be inside its include guard
    Only empty lines and `//` comments may precede `#ifndef` or follow
    `#endif`.
    ca14da9025
  16. in test/lint/lint-include-guards.py:78 in 9b0ef7e98b
      77 | -        for header_file_contents_string in header_file_contents:
      78 | -            include_guard_lst = re.findall(
      79 | -                regex_pattern, header_file_contents_string)
      80 | +        guard_line_nums = [
      81 | +            line_num for line_num, line in enumerate(header_file_contents, start=1)
      82 | +            if re.match(regex_pattern, line)
    


    maflcko commented at 12:54 PM on November 10, 2026:

    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)


    maflcko commented at 12:59 PM on November 10, 2026:

    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 commented at 1:39 PM on November 10, 2026:

    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.

  17. hebasto force-pushed on Nov 10, 2026
  18. maflcko commented at 1:51 PM on November 10, 2026: member

    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>

  19. hebasto commented at 2:05 PM on November 10, 2026: member

    ... 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
    
  20. fanquake merged this on Nov 10, 2026
  21. fanquake closed this on Nov 10, 2026

  22. hebasto deleted the branch on Nov 10, 2026

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-11-30 04:51 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me