Skip to content

Commit

Permalink
Add support for --exclude-declaration to python and cxx generator
Browse files Browse the repository at this point in the history
  • Loading branch information
DeltaEvo committed May 14, 2024
1 parent 8b816f9 commit 362e2d2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 25 deletions.
31 changes: 7 additions & 24 deletions pdl-compiler/scripts/generate_cxx_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1658,7 +1658,7 @@ class {struct_name} : public pdl::packet::Builder {{


def run(input: argparse.FileType, output: argparse.FileType, namespace: Optional[str], include_header: List[str],
using_namespace: List[str]):
using_namespace: List[str], exclude_declaration: List[str]):

file = ast.File.from_json(json.load(input))
core.desugar(file)
Expand All @@ -1668,28 +1668,6 @@ def run(input: argparse.FileType, output: argparse.FileType, namespace: Optional
open_namespace = f"namespace {namespace} {{" if namespace else ""
close_namespace = f"}} // {namespace}" if namespace else ""

# Disable unsupported features in the canonical test suite.
skipped_decls = [
'Packet_Custom_Field_ConstantSize',
'Packet_Custom_Field_VariableSize',
'Packet_Checksum_Field_FromStart',
'Packet_Checksum_Field_FromEnd',
'Struct_Custom_Field_ConstantSize',
'Struct_Custom_Field_VariableSize',
'Struct_Checksum_Field_FromStart',
'Struct_Checksum_Field_FromEnd',
'Struct_Custom_Field_ConstantSize_',
'Struct_Custom_Field_VariableSize_',
'Struct_Checksum_Field_FromStart_',
'Struct_Checksum_Field_FromEnd_',
'PartialParent5',
'PartialChild5_A',
'PartialChild5_B',
'PartialParent12',
'PartialChild12_A',
'PartialChild12_B',
]

output.write(
dedent("""\
// File generated from {input_name}, with the command:
Expand Down Expand Up @@ -1732,7 +1710,7 @@ def run(input: argparse.FileType, output: argparse.FileType, namespace: Optional
output.write(f"class {d.id}View;\n")

for d in file.declarations:
if d.id in skipped_decls:
if d.id in exclude_declaration:
continue

if isinstance(d, ast.EnumDeclaration):
Expand All @@ -1759,6 +1737,11 @@ def main() -> int:
default=[],
action='append',
help='Added using namespace statements')
parser.add_argument('--exclude-declaration',
type=str,
default=[],
action='append',
help='Exclude declaration from the generated output')
return run(**vars(parser.parse_args()))


Expand Down
13 changes: 12 additions & 1 deletion pdl-compiler/scripts/generate_python_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1157,13 +1157,16 @@ def generate_checksum_declaration_check(decl: ast.ChecksumDeclaration) -> str:
""").format(checksum_name=decl.id)


def run(input: argparse.FileType, output: argparse.FileType, custom_type_location: Optional[str]):
def run(input: argparse.FileType, output: argparse.FileType, custom_type_location: Optional[str], exclude_declaration: List[str]):
file = ast.File.from_json(json.load(input))
core.desugar(file)

custom_types = []
custom_type_checks = ""
for d in file.declarations:
if d.id in exclude_declaration:
continue

if isinstance(d, ast.CustomFieldDeclaration):
custom_types.append(d.id)
custom_type_checks += generate_custom_field_declaration_check(d)
Expand All @@ -1180,6 +1183,9 @@ def run(input: argparse.FileType, output: argparse.FileType, custom_type_locatio
output.write(custom_type_checks)

for d in file.declarations:
if d.id in exclude_declaration:
continue

if isinstance(d, ast.EnumDeclaration):
output.write(generate_enum_declaration(d))
elif isinstance(d, (ast.PacketDeclaration, ast.StructDeclaration)):
Expand All @@ -1195,6 +1201,11 @@ def main() -> int:
type=str,
required=False,
help='Module of declaration of custom types')
parser.add_argument('--exclude-declaration',
type=str,
default=[],
action='append',
help='Exclude declaration from the generated output')
return run(**vars(parser.parse_args()))


Expand Down
36 changes: 36 additions & 0 deletions pdl-compiler/tests/run_cxx_generator_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,46 @@ pdlc "$OUT_DIR"/be_test_file.pdl > "$OUT_DIR"/be_test_file.json
python3 scripts/generate_cxx_backend.py \
--input "$OUT_DIR"/le_test_file.json \
--output "$OUT_DIR"/le_backend.h \
--exclude-declaration Packet_Custom_Field_ConstantSize \
--exclude-declaration Packet_Custom_Field_VariableSize \
--exclude-declaration Packet_Checksum_Field_FromStart \
--exclude-declaration Packet_Checksum_Field_FromEnd \
--exclude-declaration Struct_Custom_Field_ConstantSize \
--exclude-declaration Struct_Custom_Field_VariableSize \
--exclude-declaration Struct_Checksum_Field_FromStart \
--exclude-declaration Struct_Checksum_Field_FromEnd \
--exclude-declaration Struct_Custom_Field_ConstantSize_ \
--exclude-declaration Struct_Custom_Field_VariableSize_ \
--exclude-declaration Struct_Checksum_Field_FromStart_ \
--exclude-declaration Struct_Checksum_Field_FromEnd_ \
--exclude-declaration PartialParent5 \
--exclude-declaration PartialChild5_A \
--exclude-declaration PartialChild5_B \
--exclude-declaration PartialParent12 \
--exclude-declaration PartialChild12_A \
--exclude-declaration PartialChild12_B \
--namespace le_backend
python3 scripts/generate_cxx_backend.py \
--input "$OUT_DIR"/be_test_file.json \
--output "$OUT_DIR"/be_backend.h \
--exclude-declaration Packet_Custom_Field_ConstantSize \
--exclude-declaration Packet_Custom_Field_VariableSize \
--exclude-declaration Packet_Checksum_Field_FromStart \
--exclude-declaration Packet_Checksum_Field_FromEnd \
--exclude-declaration Struct_Custom_Field_ConstantSize \
--exclude-declaration Struct_Custom_Field_VariableSize \
--exclude-declaration Struct_Checksum_Field_FromStart \
--exclude-declaration Struct_Checksum_Field_FromEnd \
--exclude-declaration Struct_Custom_Field_ConstantSize_ \
--exclude-declaration Struct_Custom_Field_VariableSize_ \
--exclude-declaration Struct_Checksum_Field_FromStart_ \
--exclude-declaration Struct_Checksum_Field_FromEnd_ \
--exclude-declaration PartialParent5 \
--exclude-declaration PartialChild5_A \
--exclude-declaration PartialChild5_B \
--exclude-declaration PartialParent12 \
--exclude-declaration PartialChild12_A \
--exclude-declaration PartialChild12_B \
--namespace be_backend

python3 scripts/generate_cxx_backend_tests.py \
Expand Down

0 comments on commit 362e2d2

Please sign in to comment.