Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1#!/usr/bin/env python32#3# Copyright (c) 2018-present The Bitcoin Core developers4# Distributed under the MIT software license, see the accompanying5# file COPYING or http://www.opensource.org/licenses/mit-license.php.7"""8Check include guards.9"""11import re12import sys13from subprocess import check_output15from lint_ignore_dirs import SHARED_EXCLUDED_SUBTREES18HEADER_ID_PREFIX = 'BITCOIN_'19HEADER_ID_SUFFIX = '_H'21EXCLUDE_FILES_WITH_PREFIX = ['contrib/devtools/bitcoin-tidy',22'src/tinyformat.h',23'src/bench/nanobench.h',24'src/test/fuzz/FuzzedDataProvider.h'] + SHARED_EXCLUDED_SUBTREES27def _get_header_file_lst() -> list[str]:28""" Helper function to get a list of header filepaths to be29checked for include guards.30"""31git_cmd_lst = ['git', 'ls-files', '--', '*.h']32header_file_lst = check_output(33git_cmd_lst, text=True).splitlines()35header_file_lst = [hf for hf in header_file_lst36if not any(ef in hf for ef37in EXCLUDE_FILES_WITH_PREFIX)]39return header_file_lst42def _get_header_id(header_file: str) -> str:43""" Helper function to get the header id from a header file44string.46eg: 'src/wallet/walletdb.h' -> 'BITCOIN_WALLET_WALLETDB_H'48Args:49header_file: Filepath to header file.51Returns:52The header id.53"""54header_id_base = header_file.split('/')[1:]55header_id_base = '_'.join(header_id_base)56header_id_base = header_id_base.replace('.h', '').replace('-', '_')57header_id_base = header_id_base.upper()59header_id = f'{HEADER_ID_PREFIX}{header_id_base}{HEADER_ID_SUFFIX}'61return header_id64def main():65exit_code = 067header_file_lst = _get_header_file_lst()68for header_file in header_file_lst:69header_id = _get_header_id(header_file)71regex_pattern = f'^#(ifndef|define|endif //) {header_id}'73with open(header_file, 'r') as f:74header_file_contents = f.readlines()76count = 077for header_file_contents_string in header_file_contents:78include_guard_lst = re.findall(79regex_pattern, header_file_contents_string)81count += len(include_guard_lst)83if count != 3:84print(f'{header_file} seems to be missing the expected '85'include guard to prevent the double inclusion problem:')86print(f' #ifndef {header_id}')87print(f' #define {header_id}')88print(' ...')89print(f' #endif // {header_id}\n')90exit_code = 192sys.exit(exit_code)95if __name__ == '__main__':96main()
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.