mirror of
https://github.com/libvirt/libvirt.git
synced 2025-02-25 18:55:26 -06:00
This implements a C code generator that emits code that is (almost) identical to the classic 'rpcgen' program. The key differences are: - Skip inlining of calls for struct fields - Skip K&R style function prototypes in headers - Use int64_t instead of quad_t for OS portability - Saner whitespace / indentation The tests/demo.c and tests/demo.h files were created using the traditional 'rpcgen' program, and then editted to cut out the leading boilerplate, and the differences mentioned above. Reviewed-by: Michal Privoznik <mprivozn@redhat.com> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
56 lines
1.3 KiB
Python
56 lines
1.3 KiB
Python
# SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
import os
|
|
from pathlib import Path
|
|
|
|
from rpcgen.parser import XDRParser
|
|
from rpcgen.generator import (
|
|
XDRTypeDeclarationGenerator,
|
|
XDRMarshallDeclarationGenerator,
|
|
XDRMarshallImplementationGenerator,
|
|
)
|
|
|
|
|
|
def test_generate_header():
|
|
x = Path(Path(__file__).parent, "demo.x")
|
|
h = Path(Path(__file__).parent, "demo.h")
|
|
with x.open("r") as fp:
|
|
parser = XDRParser(fp)
|
|
spec = parser.parse()
|
|
|
|
got = (
|
|
XDRTypeDeclarationGenerator(spec).visit()
|
|
+ "\n"
|
|
+ XDRMarshallDeclarationGenerator(spec).visit()
|
|
)
|
|
|
|
with h.open("r") as fp:
|
|
want = fp.read()
|
|
|
|
if "VIR_TEST_REGENERATE_OUTPUT" in os.environ:
|
|
want = got
|
|
with h.open("w") as fp:
|
|
fp.write(want)
|
|
|
|
assert got == want
|
|
|
|
|
|
def test_generate_source():
|
|
x = Path(Path(__file__).parent, "demo.x")
|
|
h = Path(Path(__file__).parent, "demo.c")
|
|
with x.open("r") as fp:
|
|
parser = XDRParser(fp)
|
|
spec = parser.parse()
|
|
|
|
got = XDRMarshallImplementationGenerator(spec).visit()
|
|
|
|
with h.open("r") as fp:
|
|
want = fp.read()
|
|
|
|
if "VIR_TEST_REGENERATE_OUTPUT" in os.environ:
|
|
want = got
|
|
with h.open("w") as fp:
|
|
fp.write(want)
|
|
|
|
assert got == want
|