Improve packaging/var-checker.

Make var-checker compare the variable type of the extern vars to ensure
that they are all consistent. Fix the remaining issues.
This commit is contained in:
Wayne Davison
2024-11-14 11:39:21 -08:00
parent 62bb9bba02
commit fa28c5d693
3 changed files with 39 additions and 11 deletions

2
main.c
View File

@@ -66,7 +66,7 @@ extern int protect_args;
extern int relative_paths;
extern int sanitize_paths;
extern int curr_dir_depth;
extern int curr_dir_len;
extern unsigned int curr_dir_len;
extern int module_id;
extern int rsync_port;
extern int whole_file;

View File

@@ -6,9 +6,10 @@
import os, sys, re, argparse, glob
VARS_RE = re.compile(r'^(?!(?:extern|enum)\s)([a-zA-Z]\S*\s+.*);', re.M)
VARS_RE = re.compile(r'^(?!(?:extern|enum)\s)([a-zA-Z][^ \n\t:]*\s+.*);', re.M)
EXTERNS_RE = re.compile(r'^extern\s+(.*);', re.M)
types = { }
sizes = { }
def main():
@@ -68,19 +69,46 @@ def parse_vars(fn, lines):
for line in lines:
line = re.sub(r'\s*\{.*\}', '', line)
line = re.sub(r'\s*\(.*\)', '', line)
for item in re.split(r'\s*,\s*', line):
item = re.sub(r'\s*=.*', '', item)
m = re.search(r'(?P<var>\w+)(?P<sz>\[.*?\])?$', item)
line = re.sub(r'\s*=\s*[^,]*', '', line)
m = re.search(r'^(?:(?:static|extern)\s+)?(?P<type>[^\[,]+?)(?P<vars>\w+([\[,].+)?)$', line)
if not m:
print(f"Bogus match? ({line})")
continue
items = m['vars']
main_type = m['type'].strip()
mt_len = len(main_type)
main_type = main_type.rstrip('*')
first_stars = '*' * (mt_len - len(main_type))
if first_stars:
main_type = main_type.rstrip()
items = first_stars + items
for item in re.split(r'\s*,\s*', items):
m = re.search(r'(?P<stars>\*+\s*)?(?P<var>\w+)(?P<sz>\[.*?\])?$', item)
if not m:
print(f"Bogus match? ({item})")
continue
if m['sz']:
if m['var'] in sizes:
if sizes[m['var']] != m['sz']:
typ = main_type
if m['stars']:
typ = typ + m['stars'].strip()
chk = [
'type', typ, types,
'size', m['sz'], sizes,
]
while chk:
label = chk.pop(0)
new = chk.pop(0)
lst = chk.pop(0)
if not new:
continue
if label == 'type':
new = ' '.join(new.split()).replace(' *', '*')
if m['var'] in lst:
old = lst[m['var']]
if new != old:
var = m['var']
print(fn, f'has inconsistent size for "{var}":', m['sz'], 'vs', sizes[var])
print(fn, f'has inconsistent {label} for "{var}":', new, 'vs', old)
else:
sizes[m['var']] = m['sz']
lst[m['var']] = new
ret.append(m['var'])
return ret

View File

@@ -28,7 +28,7 @@ int preallocate_files = 0;
int protect_args = 0;
int module_id = -1;
int relative_paths = 0;
int module_dirlen = 0;
unsigned int module_dirlen = 0;
int preserve_xattrs = 0;
int preserve_perms = 0;
int preserve_executability = 0;