Skip to content

Commit 3f076dd

Browse files
Readability improvements
No indented semantic change.
1 parent 4b3eb69 commit 3f076dd

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

scripts/generate_psa_constants.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,18 @@ def __init__(self):
177177
self.key_usages = set()
178178

179179
# "#define" followed by a macro name with either no parameters
180-
# or a single parameter. Grab the macro name in group 1, the
181-
# parameter name if any in group 2 and the definition in group 3.
182-
definition_re = re.compile(r'\s*#\s*define\s+(\w+)(?:\s+|\((\w+)\)\s*)(.+)(?:/[*/])?')
180+
# or a single parameter and a non-empty expansion.
181+
# Grab the macro name in group 1, the parameter name if any in group 2
182+
# and the expansion in group 3.
183+
_define_directive_re = re.compile(r'\s*#\s*define\s+(\w+)' +
184+
r'(?:\s+|\((\w+)\)\s*)' +
185+
r'(.+)(?:/[*/])?')
183186

184187
def read_line(self, line):
185-
m = re.match(self.definition_re, line)
188+
m = re.match(self._define_directive_re, line)
186189
if not m:
187190
return
188-
name, parameter, definition = m.groups()
191+
name, parameter, expansion = m.groups()
189192
if name.endswith('_FLAG') or name.endswith('MASK'):
190193
# Macro only to build actual values
191194
return
@@ -215,10 +218,10 @@ def read_line(self, line):
215218
return
216219
self.algorithms.add(name)
217220
# Ad hoc detection of hash algorithms
218-
if re.search(r'0x010000[0-9A-Fa-f]{2}', definition):
221+
if re.search(r'0x010000[0-9A-Fa-f]{2}', expansion):
219222
self.hash_algorithms.add(name)
220223
# Ad hoc detection of key agreement algorithms
221-
if re.search(r'0x30[0-9A-Fa-f]{2}0000', definition):
224+
if re.search(r'0x30[0-9A-Fa-f]{2}0000', expansion):
222225
self.ka_algorithms.add(name)
223226
elif name.startswith('PSA_ALG_') and parameter == 'hash_alg':
224227
if name in ['PSA_ALG_DSA', 'PSA_ALG_ECDSA']:

tests/scripts/test_psa_constant_names.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,21 @@ def distribute_arguments(self, name):
132132
# Regex of macro names to exclude.
133133
excluded_name_re = re.compile('_(?:GET|IS|OF)_|_(?:BASE|FLAG|MASK)\Z')
134134
# Additional excluded macros.
135-
# PSA_ALG_ECDH and PSA_ALG_FFDH are excluded for now as the script
136-
# currently doesn't support them. Deprecated errors are also excluded.
137-
excluded_names = set(['PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH',
138-
'PSA_ALG_FULL_LENGTH_MAC',
139-
'PSA_ALG_ECDH',
140-
'PSA_ALG_FFDH',
141-
'PSA_ERROR_UNKNOWN_ERROR',
142-
'PSA_ERROR_OCCUPIED_SLOT',
143-
'PSA_ERROR_EMPTY_SLOT',
144-
'PSA_ERROR_INSUFFICIENT_CAPACITY',
145-
])
135+
excluded_names = set([
136+
# Macros that provide an alternative way to build the same
137+
# algorithm as another macro.
138+
'PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH',
139+
'PSA_ALG_FULL_LENGTH_MAC',
140+
# PSA_ALG_ECDH and PSA_ALG_FFDH are excluded for now as the script
141+
# currently doesn't support them. Deprecated errors are also excluded.
142+
'PSA_ALG_ECDH',
143+
'PSA_ALG_FFDH',
144+
# Deprecated aliases.
145+
'PSA_ERROR_UNKNOWN_ERROR',
146+
'PSA_ERROR_OCCUPIED_SLOT',
147+
'PSA_ERROR_EMPTY_SLOT',
148+
'PSA_ERROR_INSUFFICIENT_CAPACITY',
149+
])
146150
argument_split_re = re.compile(r' *, *')
147151
def parse_header_line(self, line):
148152
'''Parse a C header line, looking for "#define PSA_xxx".'''

0 commit comments

Comments
 (0)