From 5a2b3a8c0bcf3f693d41411b960b8999026d3c51 Mon Sep 17 00:00:00 2001 From: Phanindra Kumar Yellapu <44427783+phani92@users.noreply.github.com> Date: Mon, 4 Mar 2024 16:37:42 +0100 Subject: [PATCH 1/3] Adds a test for variadic function --- tests/hammocking_test.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/hammocking_test.py b/tests/hammocking_test.py index 9c87c0f..d01fd0c 100644 --- a/tests/hammocking_test.py +++ b/tests/hammocking_test.py @@ -338,6 +338,16 @@ def test_langmode_override(self): assert len(mock.writer.functions) == 1, "Mockup shall have a function" assert mock.writer.functions[0].get_signature() == "_Bool bool_status()", "Function shall be created with C99 bool type" + def test_variadic_function(self): + """Mock a variadic function""" + mock = Hammock(["printf"]) + mock.parse("extern int printf(const char * format, ...);") + assert mock.done, "Should be done now" + assert len(mock.writer.functions) == 1, "Mockup shall have a function" + self.assertEqual( + mock.writer.functions[0].get_signature(), "int printf(const char * format, ...)", "Function shall be created in the mockup" + ) + if __name__ == "__main__": unittest.main() From be5c36bfccd5c8c80a137d6b787be22653fec22b Mon Sep 17 00:00:00 2001 From: Phanindra Kumar Yellapu <44427783+phani92@users.noreply.github.com> Date: Mon, 4 Mar 2024 16:40:51 +0100 Subject: [PATCH 2/3] Support mocking variadic functions --- hammocking/hammocking.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/hammocking/hammocking.py b/hammocking/hammocking.py index 40bf99f..63562b8 100755 --- a/hammocking/hammocking.py +++ b/hammocking/hammocking.py @@ -11,7 +11,7 @@ from argparse import ArgumentParser from pathlib import Path from typing import List, Union, Tuple, Iterator, Iterable -from clang.cindex import Index, TranslationUnit, Cursor, CursorKind, Config +from clang.cindex import Index, TranslationUnit, Cursor, CursorKind, Config, TypeKind from jinja2 import Environment, FileSystemLoader import logging @@ -27,13 +27,14 @@ def get_definition(self) -> str: class Function: - def __init__(self, type: str, name: str, params: List[Variable]) -> None: + def __init__(self, type: str, name: str, params: List[Variable], is_variadic: bool = False) -> None: self.type = type self.name = name self.params = params + self.is_variadic = is_variadic def get_signature(self) -> str: - return f"{self.type} {self.name}({self._collect_arguments(True)})" + return f"{self.type} {self.name}({self._collect_arguments(True)}{', ...' if self.is_variadic else ''})" def _collect_arguments(self, with_types: bool) -> str: unnamed_index = 1 @@ -84,10 +85,10 @@ def add_variable(self, type: str, name: str, size: int = 0) -> None: logging.info(f"HammocKing: Create mockup for variable {name}") self.variables.append(Variable(type, name, size)) - def add_function(self, type: str, name: str, params: List[Tuple[str, str]] = []) -> None: + def add_function(self, type: str, name: str, params: List[Tuple[str, str]] = [], is_variadic: bool = False) -> None: """Add a variable definition""" logging.info(f"Create mockup for function {name}") - self.functions.append(Function(type, name, [Variable(param[0], param[1]) for param in params])) + self.functions.append(Function(type, name, [Variable(param[0], param[1]) for param in params], is_variadic)) def get_mockup(self, file: str) -> str: return self.render(Path(file + '.j2')) @@ -180,6 +181,7 @@ def parse(self, input: Union[Path, str]) -> None: child.type.get_result().spelling, child.spelling, [(arg.type.spelling, arg.spelling) for arg in child.get_arguments()], + is_variadic = child.type.is_function_variadic() if child.type.kind == TypeKind.FUNCTIONPROTO else False ) else: self.logger.warning(f"Unknown kind of symbol: {child.kind}") From 5a132ff8ec4b263a4050aea885d17ff553d1213f Mon Sep 17 00:00:00 2001 From: Phanindra Kumar Yellapu <44427783+phani92@users.noreply.github.com> Date: Mon, 4 Mar 2024 16:42:33 +0100 Subject: [PATCH 3/3] Removes space --- tests/hammocking_test.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/hammocking_test.py b/tests/hammocking_test.py index d01fd0c..86c9411 100644 --- a/tests/hammocking_test.py +++ b/tests/hammocking_test.py @@ -348,6 +348,5 @@ def test_variadic_function(self): mock.writer.functions[0].get_signature(), "int printf(const char * format, ...)", "Function shall be created in the mockup" ) - if __name__ == "__main__": unittest.main()