From d95c135498abd866b98faf289ab6207583f02afe Mon Sep 17 00:00:00 2001 From: Ronald Cron Date: Mon, 1 Dec 2025 10:28:24 +0100 Subject: [PATCH] test_driver.py: Add get_identifiers_with_prefixes method Add get_identifiers_with_prefixes method that returns the list of identifiers that potentially need to be prefixed in the test driver code. Signed-off-by: Ronald Cron --- scripts/mbedtls_framework/test_driver.py | 51 ++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/scripts/mbedtls_framework/test_driver.py b/scripts/mbedtls_framework/test_driver.py index c40e46ce8..bce53f584 100644 --- a/scripts/mbedtls_framework/test_driver.py +++ b/scripts/mbedtls_framework/test_driver.py @@ -8,6 +8,7 @@ import argparse import re import shutil +import subprocess from fnmatch import fnmatch from pathlib import Path @@ -37,6 +38,37 @@ def iter_code_files(root: Path) -> Iterable[Path]: for ext in (".c", ".h"): yield from directory_path.rglob(f"*{ext}") +def run_ctags(file: Path) -> Set[str]: + """ + Extract the C identifiers in `file` using ctags. + + Identifiers of the following types are returned (with their corresponding + ctags c-kinds flag in parentheses): + + - macro definitions (d) + - enum values (e) + - functions (f) + - enum tags (g) + - function prototypes (p) + - struct tags (s) + - typedefs (t) + - union tags (u) + - global variables (v) + """ + + result = subprocess.run( + ["ctags", "-x", "--language-force=C", "--c-kinds=defgpstuv", str(file)], + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + universal_newlines=True, + check=True + ) + identifiers = set() + for line in result.stdout.splitlines(): + identifiers.add(line.split()[0]) + + return identifiers + class TestDriverGenerator: """A TF-PSA-Crypto test driver generator""" def __init__(self, dst_dir: Path, driver: str): @@ -105,6 +137,25 @@ class TestDriverGenerator: self.__rewrite_inclusions_in_file(f, headers, \ src_include_dir_name, self.driver) + def get_identifiers_with_prefixes(self, prefixes: Set[str]): + """ + Return the identifiers in the test driver that start with any of the given + prefixes. + + All exposed identifiers are expected to start with one of these prefixes. + The returned set is therefore a superset of the exposed identifiers that + need to be prefixed. + """ + identifiers = set() + for file in iter_code_files(self.dst_dir): + identifiers.update(run_ctags(file)) + + identifiers_with_prefixes = set() + for identifier in identifiers: + if any(identifier.startswith(prefix) for prefix in prefixes): + identifiers_with_prefixes.add(identifier) + return identifiers_with_prefixes + @staticmethod def __rewrite_inclusions_in_file(file: Path, headers: Set[str], src_include_dir: str, driver: str,) -> None: