mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-11-22 14:19:57 +08:00
build(style): skip parameter member renames referenced in other files
check_param_names.py --fix only renamed members in the declaring file and its implementation file. Uses in other files, like the AFBRS50 calibration offsets in AFBRS50_Calibration.cpp, kept the old name and broke the build, while the declaration-only check still passed. --fix now looks for the old name in all tracked sources first. If it appears in a file whose stem does not declare it as its own parameter member, it skips that member, reports it for manual renaming and exits with an error. A real use can't be told apart from an unrelated member with the same name without a compiler, so it reports both.
This commit is contained in:
1 parent
6d43f6e807
commit
a72242a215
1 file changed
+72
-17
@@ -10,9 +10,13 @@ Usage:
|
||||
check_param_names.py --fix rename violating members
|
||||
|
||||
--fix renames the member in the declaring file and its implementation file
|
||||
(same stem, optionally with _main suffix). The same wrong name is often used by
|
||||
other classes for unrelated members, so uses elsewhere (e.g. derived classes)
|
||||
are left for the compiler to find.
|
||||
(same stem, optionally with _main suffix). If the old name also appears in any
|
||||
other source file that does not declare it as a parameter member itself, it
|
||||
might be a use (e.g. a derived class or a helper file) or an unrelated member
|
||||
with the same name. Telling those apart needs a compiler, so such members are
|
||||
not renamed but reported for manual renaming, and --fix exits with an error.
|
||||
This keeps --fix from renaming a declaration while leaving its uses elsewhere
|
||||
behind, which would break code that compiled before.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
@@ -23,6 +27,7 @@ import sys
|
||||
from collections import defaultdict
|
||||
|
||||
DECLARATION = re.compile(r'<\s*px4::params::(\w+)\s*>\s*\)\s*([A-Za-z_]\w*)')
|
||||
IDENTIFIER = re.compile(r'[A-Za-z_]\w*')
|
||||
SOURCE_EXTENSIONS = ('.c', '.cpp', '.h', '.hpp')
|
||||
|
||||
|
||||
@@ -55,6 +60,47 @@ def stem(path):
|
||||
return name[:-len('_main')] if name.endswith('_main') else name
|
||||
|
||||
|
||||
def stem_files(file_stem):
|
||||
return [file_stem + suffix + extension
|
||||
for suffix in ('', '_main')
|
||||
for extension in SOURCE_EXTENSIONS
|
||||
if os.path.isfile(file_stem + suffix + extension)]
|
||||
|
||||
|
||||
def all_sources():
|
||||
"""All tracked C/C++ sources as absolute path -> (identifiers used, parameter members declared)."""
|
||||
top = subprocess.run(['git', 'rev-parse', '--show-toplevel'],
|
||||
capture_output=True, text=True, check=True).stdout.strip()
|
||||
paths = subprocess.run(['git', 'ls-files', '-z', '--'] + ['*' + e for e in SOURCE_EXTENSIONS],
|
||||
cwd=top, capture_output=True, text=True, check=True).stdout.split('\0')
|
||||
sources = {}
|
||||
|
||||
for path in filter(None, paths):
|
||||
path = os.path.join(top, path)
|
||||
|
||||
if os.path.isfile(path):
|
||||
with open(path, encoding='utf-8', errors='replace', newline='') as f:
|
||||
text = f.read()
|
||||
|
||||
sources[path] = (set(IDENTIFIER.findall(text)), {m.group(2) for m in DECLARATION.finditer(text)})
|
||||
|
||||
# a class usually declares its parameters in the header and uses them in the implementation file
|
||||
declared_by_stem = defaultdict(set)
|
||||
|
||||
for path, (_, declared) in sources.items():
|
||||
declared_by_stem[stem(path)] |= declared
|
||||
|
||||
return {path: (identifiers, declared_by_stem[stem(path)]) for path, (identifiers, _) in sources.items()}
|
||||
|
||||
|
||||
def outside_references(member, own_files, sources):
|
||||
"""Files outside own_files mentioning member without their stem declaring it as own parameter member."""
|
||||
own_files = {os.path.abspath(path) for path in own_files}
|
||||
|
||||
return sorted(os.path.relpath(path) for path, (identifiers, declared) in sources.items()
|
||||
if path not in own_files and member in identifiers and member not in declared)
|
||||
|
||||
|
||||
def fix(violations):
|
||||
# old name -> new name per file stem
|
||||
renames = defaultdict(dict)
|
||||
@@ -71,26 +117,35 @@ def fix(violations):
|
||||
|
||||
stem_renames[member] = new
|
||||
|
||||
sources = all_sources() if renames else {}
|
||||
|
||||
for file_stem, stem_renames in renames.items():
|
||||
files = stem_files(file_stem)
|
||||
|
||||
for member in list(stem_renames):
|
||||
references = outside_references(member, files, sources)
|
||||
|
||||
if references:
|
||||
print(f'{member} is also used in {", ".join(references)}, rename to {stem_renames[member]} manually')
|
||||
del stem_renames[member]
|
||||
ok = False
|
||||
|
||||
if not stem_renames:
|
||||
continue
|
||||
|
||||
pattern = re.compile(r'\b(' + '|'.join(map(re.escape, stem_renames)) + r')\b')
|
||||
|
||||
for suffix in ('', '_main'):
|
||||
for extension in SOURCE_EXTENSIONS:
|
||||
path = file_stem + suffix + extension
|
||||
for path in files:
|
||||
with open(path, encoding='utf-8', newline='') as f:
|
||||
text = f.read()
|
||||
|
||||
if not os.path.isfile(path):
|
||||
continue
|
||||
new_text = pattern.sub(lambda m: stem_renames[m.group(1)], text)
|
||||
|
||||
with open(path, encoding='utf-8', newline='') as f:
|
||||
text = f.read()
|
||||
if new_text != text:
|
||||
with open(path, 'w', encoding='utf-8', newline='') as f:
|
||||
f.write(new_text)
|
||||
|
||||
new_text = pattern.sub(lambda m: stem_renames[m.group(1)], text)
|
||||
|
||||
if new_text != text:
|
||||
with open(path, 'w', encoding='utf-8', newline='') as f:
|
||||
f.write(new_text)
|
||||
|
||||
print(f'renamed parameter members in {path}')
|
||||
print(f'renamed parameter members in {path}')
|
||||
|
||||
return ok
|
||||
|
||||
|
||||
Reference in new issue
Block a user