From: Marc-André Lureau <marcandre.lur...@redhat.com> Generate Rust #[cfg(...)] guards from QAPI 'if' conditions.
Signed-off-by: Marc-André Lureau <marcandre.lur...@redhat.com> Link: https://lore.kernel.org/r/20210907121943.3498701-15-marcandre.lur...@redhat.com Signed-off-by: Paolo Bonzini <pbonz...@redhat.com> --- scripts/qapi/common.py | 16 ++++++++++++++++ scripts/qapi/schema.py | 4 ++++ 2 files changed, 20 insertions(+) diff --git a/scripts/qapi/common.py b/scripts/qapi/common.py index d7c8aa3365c..a8ea5792c11 100644 --- a/scripts/qapi/common.py +++ b/scripts/qapi/common.py @@ -199,6 +199,22 @@ def guardend(name: str) -> str: name=c_fname(name).upper()) +def rsgen_ifcond(ifcond: Union[str, Dict[str, Any]]) -> str: + + def cfg(ifcond: Union[str, Dict[str, Any]]): + if isinstance(ifcond, str): + return ifcond + if isinstance(ifcond, list): + return ', '.join([cfg(c) for c in ifcond]) + oper, operands = next(iter(ifcond.items())) + operands = cfg(operands) + return f'{oper}({operands})' + + if not ifcond: + return '' + return '#[cfg(%s)]' % cfg(ifcond) + + def gen_ifcond(ifcond: Optional[Union[str, Dict[str, Any]]], cond_fmt: str, not_fmt: str, all_operator: str, any_operator: str) -> str: diff --git a/scripts/qapi/schema.py b/scripts/qapi/schema.py index cbe3b5aa91e..0fb151b5d89 100644 --- a/scripts/qapi/schema.py +++ b/scripts/qapi/schema.py @@ -39,6 +39,7 @@ docgen_ifcond, gen_endif, gen_if, + rsgen_ifcond, ) from .error import QAPIError, QAPISemError, QAPISourceError from .expr import check_exprs @@ -65,6 +66,9 @@ def gen_endif(self) -> str: def docgen(self) -> str: return docgen_ifcond(self.ifcond) + def rsgen(self) -> str: + return rsgen_ifcond(self.ifcond) + def is_present(self) -> bool: return bool(self.ifcond) -- 2.49.0