check_names: extend typo check to PSA macro/enum names

Typos of PSA macro and enum names are not checked by check_names.py.
This commit extend the check list to include PSA_XXX references.
The words should be macro/enum names defined as public_macros,
internal_macros, private_macros and enums. This commit alse extend
the scope of enums to include those are defined in library/*.c.
A new type of macros "private", which are defined in library/*.c was
also added.

Signed-off-by: Pengyu Lv <pengyu.lv@arm.com>
This commit is contained in:
Pengyu Lv 2022-11-08 15:55:00 +08:00
parent faefe62013
commit cdac0d5305

View file

@ -36,7 +36,7 @@ NameChecker performs the following checks:
declared in the header files. This uses the nm command. declared in the header files. This uses the nm command.
- All macros, constants, and identifiers (function names, struct names, etc) - All macros, constants, and identifiers (function names, struct names, etc)
follow the required regex pattern. follow the required regex pattern.
- Typo checking: All words that begin with MBED exist as macros or constants. - Typo checking: All words that begin with MBED|PSA exist as macros or constants.
The script returns 0 on success, 1 on test failure, and 2 if there is a script The script returns 0 on success, 1 on test failure, and 2 if there is a script
error. It must be run from Mbed TLS root. error. It must be run from Mbed TLS root.
@ -191,11 +191,12 @@ class PatternMismatch(Problem): # pylint: disable=too-few-public-methods
class Typo(Problem): # pylint: disable=too-few-public-methods class Typo(Problem): # pylint: disable=too-few-public-methods
""" """
A problem that occurs when a word using MBED doesn't appear to be defined as A problem that occurs when a word using MBED or PSA doesn't
constants nor enum values. Created with NameCheck.check_for_typos() appear to be defined as constants nor enum values. Created with
NameCheck.check_for_typos()
Fields: Fields:
* match: the Match object of the MBED name in question. * match: the Match object of the MBED|PSA name in question.
""" """
def __init__(self, match): def __init__(self, match):
self.match = match self.match = match
@ -245,7 +246,7 @@ class CodeParser():
.format(str(self.excluded_files)) .format(str(self.excluded_files))
) )
all_macros = {"public": [], "internal": []} all_macros = {"public": [], "internal": [], "private":[]}
all_macros["public"] = self.parse_macros([ all_macros["public"] = self.parse_macros([
"include/mbedtls/*.h", "include/mbedtls/*.h",
"include/psa/*.h", "include/psa/*.h",
@ -256,9 +257,14 @@ class CodeParser():
"library/*.h", "library/*.h",
"tests/include/test/drivers/*.h", "tests/include/test/drivers/*.h",
]) ])
all_macros["private"] = self.parse_macros([
"library/*.c",
])
enum_consts = self.parse_enum_consts([ enum_consts = self.parse_enum_consts([
"include/mbedtls/*.h", "include/mbedtls/*.h",
"include/psa/*.h",
"library/*.h", "library/*.h",
"library/*.c",
"3rdparty/everest/include/everest/everest.h", "3rdparty/everest/include/everest/everest.h",
"3rdparty/everest/include/everest/x25519.h" "3rdparty/everest/include/everest/x25519.h"
]) ])
@ -269,7 +275,7 @@ class CodeParser():
"3rdparty/everest/include/everest/everest.h", "3rdparty/everest/include/everest/everest.h",
"3rdparty/everest/include/everest/x25519.h" "3rdparty/everest/include/everest/x25519.h"
]) ])
mbed_words = self.parse_mbed_words([ mbed_psa_words = self.parse_mbed_psa_words([
"include/mbedtls/*.h", "include/mbedtls/*.h",
"include/psa/*.h", "include/psa/*.h",
"library/*.h", "library/*.h",
@ -302,10 +308,11 @@ class CodeParser():
return { return {
"public_macros": actual_macros["public"], "public_macros": actual_macros["public"],
"internal_macros": actual_macros["internal"], "internal_macros": actual_macros["internal"],
"private_macros": all_macros["private"],
"enum_consts": enum_consts, "enum_consts": enum_consts,
"identifiers": identifiers, "identifiers": identifiers,
"symbols": symbols, "symbols": symbols,
"mbed_words": mbed_words "mbed_psa_words": mbed_psa_words
} }
def is_file_excluded(self, path, exclude_wildcards): def is_file_excluded(self, path, exclude_wildcards):
@ -373,25 +380,28 @@ class CodeParser():
return macros return macros
def parse_mbed_words(self, include, exclude=None): def parse_mbed_psa_words(self, include, exclude=None):
""" """
Parse all words in the file that begin with MBED, in and out of macros, Parse all words in the file that begin with MBED|PSA, in and out of
comments, anything. macros, comments, anything.
Args: Args:
* include: A List of glob expressions to look for files through. * include: A List of glob expressions to look for files through.
* exclude: A List of glob expressions for excluding files. * exclude: A List of glob expressions for excluding files.
Returns a List of Match objects for words beginning with MBED. Returns a List of Match objects for words beginning with MBED|PSA.
""" """
# Typos of TLS are common, hence the broader check below than MBEDTLS. # Typos of TLS are common, hence the broader check below than MBEDTLS.
mbed_regex = re.compile(r"\bMBED.+?_[A-Z0-9_]*") mbed_regex = re.compile(r"\b(MBED.+?|PSA)_[A-Z0-9_]*")
exclusions = re.compile(r"// *no-check-names|#error") exclusions = re.compile(r"// *no-check-names|#error")
files = self.get_files(include, exclude) files = self.get_files(include, exclude)
self.log.debug("Looking for MBED words in {} files".format(len(files))) self.log.debug(
"Looking for MBED|PSA words in {} files"
.format(len(files))
)
mbed_words = [] mbed_psa_words = []
for filename in files: for filename in files:
with open(filename, "r", encoding="utf-8") as fp: with open(filename, "r", encoding="utf-8") as fp:
for line_no, line in enumerate(fp): for line_no, line in enumerate(fp):
@ -399,14 +409,14 @@ class CodeParser():
continue continue
for name in mbed_regex.finditer(line): for name in mbed_regex.finditer(line):
mbed_words.append(Match( mbed_psa_words.append(Match(
filename, filename,
line, line,
line_no, line_no,
name.span(0), name.span(0),
name.group(0))) name.group(0)))
return mbed_words return mbed_psa_words
def parse_enum_consts(self, include, exclude=None): def parse_enum_consts(self, include, exclude=None):
""" """
@ -832,12 +842,13 @@ class NameChecker():
for match for match
in self.parse_result["public_macros"] + in self.parse_result["public_macros"] +
self.parse_result["internal_macros"] + self.parse_result["internal_macros"] +
self.parse_result["private_macros"] +
self.parse_result["enum_consts"] self.parse_result["enum_consts"]
} }
typo_exclusion = re.compile(r"XXX|__|_$|^MBEDTLS_.*CONFIG_FILE$|" typo_exclusion = re.compile(r"XXX|__|_$|^MBEDTLS_.*CONFIG_FILE$|"
r"MBEDTLS_TEST_LIBTESTDRIVER*") r"MBEDTLS_TEST_LIBTESTDRIVER*")
for name_match in self.parse_result["mbed_words"]: for name_match in self.parse_result["mbed_psa_words"]:
found = name_match.name in all_caps_names found = name_match.name in all_caps_names
# Since MBEDTLS_PSA_ACCEL_XXX defines are defined by the # Since MBEDTLS_PSA_ACCEL_XXX defines are defined by the