/ test / lint / lint-include-guards.py
lint-include-guards.py
 1  #!/usr/bin/env python3
 2  #
 3  # Copyright (c) 2018-2022 The Bitcoin Core developers
 4  # Distributed under the MIT software license, see the accompanying
 5  # file COPYING or http://www.opensource.org/licenses/mit-license.php.
 6  
 7  """
 8  Check include guards.
 9  """
10  
11  import re
12  import sys
13  from subprocess import check_output
14  
15  from lint_ignore_dirs import SHARED_EXCLUDED_SUBTREES
16  
17  
18  HEADER_ID_PREFIX = 'BITCOIN_'
19  HEADER_ID_SUFFIX = '_H'
20  
21  EXCLUDE_FILES_WITH_PREFIX = ['contrib/devtools/bitcoin-tidy',
22                               'src/crypto/ctaes',
23                               'src/tinyformat.h',
24                               'src/bench/nanobench.h',
25                               'src/test/fuzz/FuzzedDataProvider.h'] + SHARED_EXCLUDED_SUBTREES
26  
27  
28  def _get_header_file_lst() -> list[str]:
29      """ Helper function to get a list of header filepaths to be
30          checked for include guards.
31      """
32      git_cmd_lst = ['git', 'ls-files', '--', '*.h']
33      header_file_lst = check_output(
34          git_cmd_lst).decode('utf-8').splitlines()
35  
36      header_file_lst = [hf for hf in header_file_lst
37                         if not any(ef in hf for ef
38                                    in EXCLUDE_FILES_WITH_PREFIX)]
39  
40      return header_file_lst
41  
42  
43  def _get_header_id(header_file: str) -> str:
44      """ Helper function to get the header id from a header file
45          string.
46  
47          eg: 'src/wallet/walletdb.h' -> 'BITCOIN_WALLET_WALLETDB_H'
48  
49      Args:
50          header_file: Filepath to header file.
51  
52      Returns:
53          The header id.
54      """
55      header_id_base = header_file.split('/')[1:]
56      header_id_base = '_'.join(header_id_base)
57      header_id_base = header_id_base.replace('.h', '').replace('-', '_')
58      header_id_base = header_id_base.upper()
59  
60      header_id = f'{HEADER_ID_PREFIX}{header_id_base}{HEADER_ID_SUFFIX}'
61  
62      return header_id
63  
64  
65  def main():
66      exit_code = 0
67  
68      header_file_lst = _get_header_file_lst()
69      for header_file in header_file_lst:
70          header_id = _get_header_id(header_file)
71  
72          regex_pattern = f'^#(ifndef|define|endif //) {header_id}'
73  
74          with open(header_file, 'r', encoding='utf-8') as f:
75              header_file_contents = f.readlines()
76  
77          count = 0
78          for header_file_contents_string in header_file_contents:
79              include_guard_lst = re.findall(
80                  regex_pattern, header_file_contents_string)
81  
82              count += len(include_guard_lst)
83  
84          if count != 3:
85              print(f'{header_file} seems to be missing the expected '
86                    'include guard:')
87              print(f'  #ifndef {header_id}')
88              print(f'  #define {header_id}')
89              print('  ...')
90              print(f'  #endif // {header_id}\n')
91              exit_code = 1
92  
93      sys.exit(exit_code)
94  
95  
96  if __name__ == '__main__':
97      main()