build(style): check parameter member naming convention in check_format

Parameter accessing members should according to convention be named _param_{lower case parameter name} so full text search reveals all use of a value in the code.
Nothing enforced it, and 429 members drifted into violating the convention.

There was a script to rename them once which got replaced with a new one to allow checking (in CI) to enforce the naming.
This commit is contained in:
Matthias Grob committed 2026-11-18 18:40:12 +01:00
1 parent 30621ae2c3
commit 9a4fef9c02
3 files changed
+131 -94

No files matched your search

+2
View File
@@ -440,10 +440,12 @@ px4_metadata: parameters_metadata airframe_metadata module_documentation extract
check_format:
$(call colorecho,'Checking formatting with astyle')
@"$(SRC_DIR)"/Tools/astyle/check_code_style_all.sh
@"$(SRC_DIR)"/Tools/astyle/check_param_names.py
@cd "$(SRC_DIR)" && git diff --check
format:
$(call colorecho,'Formatting with astyle')
@"$(SRC_DIR)"/Tools/astyle/check_param_names.py --fix
@"$(SRC_DIR)"/Tools/astyle/check_code_style_all.sh --fix
format_changed:
+129
View File
@@ -0,0 +1,129 @@
#!/usr/bin/env python3
"""
Check that parameter members declared in DEFINE_PARAMETERS() follow the naming
convention _param_<lower case parameter name>, e.g.
(ParamFloat<px4::params::MPC_XY_VEL_MAX>) _param_mpc_xy_vel_max
Usage:
check_param_names.py report violations, exit 1 if there are any
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.
"""
import argparse
import os
import re
import subprocess
import sys
from collections import defaultdict
DECLARATION = re.compile(r'<\s*px4::params::(\w+)\s*>\s*\)\s*([A-Za-z_]\w*)')
SOURCE_EXTENSIONS = ('.c', '.cpp', '.h', '.hpp')
def expected_name(param):
return '_param_' + param.lower()
def find_violations(files):
violations = []
for path in files:
with open(path, encoding='utf-8', errors='replace', newline='') as f:
text = f.read()
if 'px4::params::' not in text:
continue
for match in DECLARATION.finditer(text):
param, member = match.groups()
if member != expected_name(param):
line = text.count('\n', 0, match.start(2)) + 1
violations.append((path, line, param, member))
return violations
def stem(path):
name = os.path.splitext(path)[0]
return name[:-len('_main')] if name.endswith('_main') else name
def fix(violations):
# old name -> new name per file stem
renames = defaultdict(dict)
ok = True
for path, line, param, member in violations:
stem_renames = renames[stem(path)]
new = expected_name(param)
if stem_renames.get(member, new) != new:
print(f'{path}:{line}: {member} maps to both {stem_renames[member]} and {new}, rename manually')
ok = False
continue
stem_renames[member] = new
for file_stem, stem_renames in renames.items():
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
if not os.path.isfile(path):
continue
with open(path, encoding='utf-8', newline='') as f:
text = f.read()
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}')
return ok
def main():
parser = argparse.ArgumentParser(description='Check parameter member naming')
parser.add_argument('--fix', action='store_true', help='rename violating members')
parser.add_argument('files', nargs='*', help='files to check (default: all checked by astyle)')
args = parser.parse_args()
files = args.files
if not files:
script_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(os.path.join(script_dir, '..', '..'))
files = subprocess.run([os.path.join(script_dir, 'files_to_check_code_style.sh')],
capture_output=True, text=True, check=True).stdout.split()
violations = find_violations(files)
if args.fix:
return 0 if fix(violations) else 1
for path, line, param, member in violations:
print(f'{path}:{line}: parameter member {member} should be named {expected_name(param)}')
if violations:
print(f'{len(violations)} parameter members not named _param_<lower case parameter name>, '
'run `make format` to fix')
return 1
return 0
if __name__ == '__main__':
sys.exit(main())
-94
View File
@@ -1,94 +0,0 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
############################################################################
#
# Copyright (c) 2018 PX4 Development Team. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name PX4 nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
"""
File: parameter_update.py
Description:
This script searches for declarations of BlockParam, creates a variable name of the
form "_param_name_of_parameter" and replaces the old variable with the new one.
Example:
(ParamExtFloat<px4::params::EKF2_GYR_NOISE>) _gyroNoise,
[...]
_gyroNoise.set(1.f);
is replaced by
(ParamExtFloat<px4::params::EKF2_GYR_NOISE>) _param_ekf2_gyr_noise,
[...]
_param_ekf2_gyr_noise.set(1.f);
Usage:
The script needs a list of files as an input. To quickly find and feed all the file
that it needs to inspect, one can simply pipe the result of a ripgrep -l command as follows:
rg -l '\.[gs]et\(|px4::params::' -tcpp | python3 parameter_update.py
"""
import re
import fileinput
import sys
filenames = []
for filename in sys.stdin:
filenames.append(filename.rstrip())
pattern = r'^.*<px4::params::([A-Za-z0-9_]+)>\)\s*([A-Za-z0-9_]+)[,\s]?.*$'
regex = re.compile(pattern, re.MULTILINE)
replace_dict = dict()
print("Searching for parameters...")
for filename in filenames:
print(filename)
with open(filename, 'r') as f:
text = f.read()
for match in regex.finditer(text):
replace_dict[match.group(2)] = "_param_" + match.group(1).strip().lower()
print("The following variables will be changed")
for old_var in replace_dict:
print("{} -> {}".format(old_var, replace_dict[old_var]))
for filename in filenames:
print(filename)
for old_var in replace_dict:
for line in fileinput.input(filename, inplace=1):
line = re.sub(r'(>\)\s*)\b' + old_var + r'\b', r'\1' + replace_dict[old_var], line.rstrip()) # replace the declaration
line = re.sub(r'(^\s*)\b' + old_var + r'\b', r'\1' + replace_dict[old_var], line.rstrip()) # replace the 2 liner declaration and when the variable is used a the beginning of a new line
line = re.sub(r'\b' + old_var + r'\b(\.([gs]et|commit)\()',replace_dict[old_var] + r'\1' , line.rstrip()) # replace the usages (with get/set/commit)
line = re.sub(r'(\()\b' + old_var + r'\b', r'\1' + replace_dict[old_var], line.rstrip()) # replace the 2 liner declaration
print(line)