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 <ronald.cron@arm.com>
This commit is contained in:
Ronald Cron
2025-12-01 10:28:24 +01:00
parent 415201cc36
commit d95c135498
+51
View File
@@ -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: