Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e0734c31 authored by Henri Chataing's avatar Henri Chataing
Browse files

pdl: Fix generation of packets with children AND no payload

The parser and serialize both have issues in this case
with parts of the generated code assuming the presence of the
payload and other not.

The added test packet_decl_parent_with_no_payload
validates the generated code for this condition.

Bug: 279494407
Test: atest pdl_generated_files_compile
Change-Id: Ibbd97441a51fc499250e2072ec8da7f3b7806970
parent 43a42f5a
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -97,6 +97,8 @@ filegroup {
        "tests/generated/packet_decl_mask_scalar_value_little_endian.rs",
        "tests/generated/packet_decl_mixed_scalars_enums_big_endian.rs",
        "tests/generated/packet_decl_mixed_scalars_enums_little_endian.rs",
        "tests/generated/packet_decl_parent_with_no_payload_big_endian.rs",
        "tests/generated/packet_decl_parent_with_no_payload_little_endian.rs",
        "tests/generated/packet_decl_payload_field_unknown_size_big_endian.rs",
        "tests/generated/packet_decl_payload_field_unknown_size_little_endian.rs",
        "tests/generated/packet_decl_payload_field_unknown_size_terminal_big_endian.rs",
+20 −1
Original line number Diff line number Diff line
@@ -477,6 +477,9 @@ fn generate_packet_decl(
                    #parent_data_child::#prev_parent_id(#prev_parent_id_lower)
                });
            }
        } else if scope.iter_children(parent_id).next().is_some() {
            field.push(format_ident!("child"));
            value.push(quote! { #parent_data_child::None });
        }

        quote! {
@@ -907,7 +910,7 @@ fn generate_custom_field_decl(id: &str, width: usize) -> proc_macro2::TokenStrea
    let id = format_ident!("{}", id);
    let backing_type = types::Integer::new(width);
    let backing_type_str = proc_macro2::Literal::string(&format!("u{}", backing_type.width));
    let max_value = mask_bits(width, "usize");
    let max_value = mask_bits(width, &format!("u{}", backing_type.width));
    let common = quote! {
        impl From<&#id> for #backing_type {
            fn from(value: &#id) -> #backing_type {
@@ -1479,6 +1482,22 @@ mod tests {
        "
    );

    test_pdl!(
        packet_decl_parent_with_no_payload,
        "
          enum Enum8 : 8 {
            A = 0,
          }

          packet Parent {
            v : Enum8,
          }

          packet Child : Parent (v = A) {
          }
        "
    );

    // TODO(mgeisler): enable this test when we have an approach to
    // struct fields with parents.
    //
+11 −0
Original line number Diff line number Diff line
@@ -672,6 +672,17 @@ impl<'a> FieldParser<'a> {
            quote!(#(, #fields)*)
        });
        let packet_data_child = format_ident!("{}DataChild", self.packet_name);
        // Parsing of packet children requires having a payload field;
        // it is allowed to inherit from a packet with empty payload, in this
        // case generate an empty payload value.
        if !decl
            .fields()
            .any(|f| matches!(&f.desc, ast::FieldDesc::Payload { .. } | ast::FieldDesc::Body))
        {
            self.code.push(quote! {
                let payload: &[u8] = &[];
            })
        }
        self.code.push(quote! {
            let child = match (#(#constrained_field_idents),*) {
                #(#match_values if #child_ids_data::conforms(&payload) => {
+1 −1
Original line number Diff line number Diff line
@@ -90,7 +90,7 @@ pub fn rust_borrow(
        ast::FieldDesc::Typedef { type_id, .. } => match &scope.typedef[type_id].desc {
            ast::DeclDesc::Enum { .. } => quote!(),
            ast::DeclDesc::Struct { .. } => quote!(&),
            ast::DeclDesc::CustomField { .. } => quote!(&),
            ast::DeclDesc::CustomField { .. } => quote!(),
            desc => unreachable!("unexpected declaration: {desc:?}"),
        },
        ast::FieldDesc::Array { .. } => quote!(&),
+4 −4
Original line number Diff line number Diff line
@@ -168,11 +168,11 @@ impl Foo {
    fn new(foo: Arc<FooData>) -> Result<Self> {
        Ok(Self { foo })
    }
    pub fn get_a(&self) -> &Bar1 {
        &self.foo.as_ref().a
    pub fn get_a(&self) -> Bar1 {
        self.foo.as_ref().a
    }
    pub fn get_b(&self) -> &Bar2 {
        &self.foo.as_ref().b
    pub fn get_b(&self) -> Bar2 {
        self.foo.as_ref().b
    }
    fn write_to(&self, buffer: &mut BytesMut) {
        self.foo.write_to(buffer)
Loading