mirror of
https://github.com/bitcoin/bitcoin.git
synced 2024-11-20 02:25:40 +01:00
Merge bitcoin/bitcoin#24902: lint: Convert lint-include-guards.sh to Python
d5fdec5cf8
Convert lint-include-guards.sh to python (brydinh) Pull request description: This PR addresses [issue 24783](https://github.com/bitcoin/bitcoin/issues/24783). Converted lint-include-guards.sh to python. ACKs for top commit: KevinMusgrave: Tested ACKd5fdec5cf8
laanwj: Code review ACKd5fdec5cf8
Tree-SHA512: cae566fc1b222b447c0d60ea20fd012f1cfde4dd07c1762ede2b2c9f84ed59ee8e629db1264dab8ac20bcac410e4c389827addf0a59757f94b40a65ea9bab466
This commit is contained in:
commit
8b686776ef
100
test/lint/lint-include-guards.py
Executable file
100
test/lint/lint-include-guards.py
Executable file
@ -0,0 +1,100 @@
|
||||
#!/usr/bin/env python3
|
||||
#
|
||||
# Copyright (c) 2018-2022 The Bitcoin Core developers
|
||||
# Distributed under the MIT software license, see the accompanying
|
||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
"""
|
||||
Check include guards.
|
||||
"""
|
||||
|
||||
import re
|
||||
import sys
|
||||
from subprocess import check_output
|
||||
from typing import List
|
||||
|
||||
|
||||
HEADER_ID_PREFIX = 'BITCOIN_'
|
||||
HEADER_ID_SUFFIX = '_H'
|
||||
|
||||
EXCLUDE_FILES_WITH_PREFIX = ['src/crypto/ctaes',
|
||||
'src/leveldb',
|
||||
'src/crc32c',
|
||||
'src/secp256k1',
|
||||
'src/minisketch',
|
||||
'src/univalue',
|
||||
'src/tinyformat.h',
|
||||
'src/bench/nanobench.h',
|
||||
'src/test/fuzz/FuzzedDataProvider.h']
|
||||
|
||||
|
||||
def _get_header_file_lst() -> List[str]:
|
||||
""" Helper function to get a list of header filepaths to be
|
||||
checked for include guards.
|
||||
"""
|
||||
git_cmd_lst = ['git', 'ls-files', '--', '*.h']
|
||||
header_file_lst = check_output(
|
||||
git_cmd_lst).decode('utf-8').splitlines()
|
||||
|
||||
header_file_lst = [hf for hf in header_file_lst
|
||||
if not any(ef in hf for ef
|
||||
in EXCLUDE_FILES_WITH_PREFIX)]
|
||||
|
||||
return header_file_lst
|
||||
|
||||
|
||||
def _get_header_id(header_file: str) -> str:
|
||||
""" Helper function to get the header id from a header file
|
||||
string.
|
||||
|
||||
eg: 'src/wallet/walletdb.h' -> 'BITCOIN_WALLET_WALLETDB_H'
|
||||
|
||||
Args:
|
||||
header_file: Filepath to header file.
|
||||
|
||||
Returns:
|
||||
The header id.
|
||||
"""
|
||||
header_id_base = header_file.split('/')[1:]
|
||||
header_id_base = '_'.join(header_id_base)
|
||||
header_id_base = header_id_base.replace('.h', '').replace('-', '_')
|
||||
header_id_base = header_id_base.upper()
|
||||
|
||||
header_id = f'{HEADER_ID_PREFIX}{header_id_base}{HEADER_ID_SUFFIX}'
|
||||
|
||||
return header_id
|
||||
|
||||
|
||||
def main():
|
||||
exit_code = 0
|
||||
|
||||
header_file_lst = _get_header_file_lst()
|
||||
for header_file in header_file_lst:
|
||||
header_id = _get_header_id(header_file)
|
||||
|
||||
regex_pattern = f'^#(ifndef|define|endif //) {header_id}'
|
||||
|
||||
with open(header_file, 'r', encoding='utf-8') as f:
|
||||
header_file_contents = f.readlines()
|
||||
|
||||
count = 0
|
||||
for header_file_contents_string in header_file_contents:
|
||||
include_guard_lst = re.findall(
|
||||
regex_pattern, header_file_contents_string)
|
||||
|
||||
count += len(include_guard_lst)
|
||||
|
||||
if count != 3:
|
||||
print(f'{header_file} seems to be missing the expected '
|
||||
'include guard:')
|
||||
print(f' #ifndef {header_id}')
|
||||
print(f' #define {header_id}')
|
||||
print(' ...')
|
||||
print(f' #endif // {header_id}\n')
|
||||
exit_code = 1
|
||||
|
||||
sys.exit(exit_code)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
@ -1,30 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# Copyright (c) 2018-2021 The Bitcoin Core developers
|
||||
# Distributed under the MIT software license, see the accompanying
|
||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
#
|
||||
# Check include guards.
|
||||
|
||||
export LC_ALL=C
|
||||
HEADER_ID_PREFIX="BITCOIN_"
|
||||
HEADER_ID_SUFFIX="_H"
|
||||
|
||||
REGEXP_EXCLUDE_FILES_WITH_PREFIX="src/(crypto/ctaes/|leveldb/|crc32c/|secp256k1/|minisketch/|test/fuzz/FuzzedDataProvider.h|tinyformat.h|bench/nanobench.h|univalue/)"
|
||||
|
||||
EXIT_CODE=0
|
||||
for HEADER_FILE in $(git ls-files -- "*.h" | grep -vE "^${REGEXP_EXCLUDE_FILES_WITH_PREFIX}")
|
||||
do
|
||||
HEADER_ID_BASE=$(cut -f2- -d/ <<< "${HEADER_FILE}" | sed "s/\.h$//g" | tr / _ | tr - _ | tr "[:lower:]" "[:upper:]")
|
||||
HEADER_ID="${HEADER_ID_PREFIX}${HEADER_ID_BASE}${HEADER_ID_SUFFIX}"
|
||||
if [[ $(grep --count --extended-regexp "^#(ifndef|define|endif //) ${HEADER_ID}" "${HEADER_FILE}") != 3 ]]; then
|
||||
echo "${HEADER_FILE} seems to be missing the expected include guard:"
|
||||
echo " #ifndef ${HEADER_ID}"
|
||||
echo " #define ${HEADER_ID}"
|
||||
echo " ..."
|
||||
echo " #endif // ${HEADER_ID}"
|
||||
echo
|
||||
EXIT_CODE=1
|
||||
fi
|
||||
done
|
||||
exit ${EXIT_CODE}
|
Loading…
Reference in New Issue
Block a user