Skip to content

Commit 267684e

Browse files
authored
lint: convert format strings linter test to python
1 parent decde9b commit 267684e

File tree

2 files changed

+98
-44
lines changed

2 files changed

+98
-44
lines changed

test/lint/lint-format-strings.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
"""
9+
Lint format strings: This program checks that the number of arguments passed
10+
to a variadic format string function matches the number of format specifiers
11+
in the format string.
12+
"""
13+
14+
import subprocess
15+
import re
16+
import sys
17+
18+
FUNCTION_NAMES_AND_NUMBER_OF_LEADING_ARGUMENTS = [
19+
'FatalError,0',
20+
'fprintf,1',
21+
'tfm::format,1', # Assuming tfm::::format(std::ostream&, ...
22+
'LogConnectFailure,1',
23+
'LogPrint,1',
24+
'LogPrintf,0',
25+
'printf,0',
26+
'snprintf,2',
27+
'sprintf,1',
28+
'strprintf,0',
29+
'vfprintf,1',
30+
'vprintf,1',
31+
'vsnprintf,1',
32+
'vsprintf,1',
33+
'WalletLogPrintf,0',
34+
]
35+
RUN_LINT_FILE = 'test/lint/run-lint-format-strings.py'
36+
37+
def check_doctest():
38+
command = [
39+
'python3',
40+
'-m',
41+
'doctest',
42+
RUN_LINT_FILE,
43+
]
44+
try:
45+
subprocess.run(command, check = True)
46+
except subprocess.CalledProcessError:
47+
sys.exit(1)
48+
49+
def get_matching_files(function_name):
50+
command = [
51+
'git',
52+
'grep',
53+
'--full-name',
54+
'-l',
55+
function_name,
56+
'--',
57+
'*.c',
58+
'*.cpp',
59+
'*.h',
60+
]
61+
try:
62+
return subprocess.check_output(command, stderr = subprocess.STDOUT).decode('utf-8').splitlines()
63+
except subprocess.CalledProcessError as e:
64+
if e.returncode > 1: # return code is 1 when match is empty
65+
print(e.output.decode('utf-8'), end='')
66+
sys.exit(1)
67+
return []
68+
69+
def main():
70+
exit_code = 0
71+
check_doctest()
72+
for s in FUNCTION_NAMES_AND_NUMBER_OF_LEADING_ARGUMENTS:
73+
function_name, skip_arguments = s.split(',')
74+
matching_files = get_matching_files(function_name)
75+
76+
matching_files_filtered = []
77+
for matching_file in matching_files:
78+
if not re.search('^src/(leveldb|secp256k1|minisketch|tinyformat|univalue|test/fuzz/strprintf.cpp)', matching_file):
79+
matching_files_filtered.append(matching_file)
80+
matching_files_filtered.sort()
81+
82+
run_lint_args = [
83+
RUN_LINT_FILE,
84+
'--skip-arguments',
85+
skip_arguments,
86+
function_name,
87+
]
88+
run_lint_args.extend(matching_files_filtered)
89+
90+
try:
91+
subprocess.run(run_lint_args, check = True)
92+
except subprocess.CalledProcessError:
93+
exit_code = 1
94+
95+
sys.exit(exit_code)
96+
97+
if __name__ == '__main__':
98+
main()

test/lint/lint-format-strings.sh

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)