Repository navigation
Conversation
hchataing
commented
Nov 13, 2026
Collaborator
- cxx: Fix the generation of code for the Android PDL grammars
- cxx: Align the accessor naming with generate_cxx_backend.py
- tests: Add canonical test cases for the C++ backend fixes
- tests: Add a script checking that the generated C++ code compiles
The C++ backend generated invalid code, or failed, for most of the grammars found in the Android repository (Bluetooth, NFC, UWB, ...): - Closed enum fields extracted from the same chunk all declared a local variable `raw_value`. The variables are now named after the field. - The `span` variable was not declared in the parser of packet views without fields, but used to check for trailing bytes. - Children of packets without payload (specialized by constraints only) referenced the inexistent `payload_` member of the parent view. - The payload size fields of the parent packets were declared as members of the child views and builders, conflicting with the payload size field of the child packet. - The count, size, and element size values of parent array fields were not copied to the child views. - Builders computed the payload size of parent packets from the fields of the leaf packet only, or from the leaf payload when the leaf packet declares a payload. The size of all the fields between the parent packet and the leaf packet is now accounted for. This bug produced invalid packets for any hierarchy with more than two levels, e.g. the HCI Command Status events. - Range tags were not declared in the generated enums, and the default value of enum fields used the first tag even when this tag was a range or default tag. Range tags are now declared with `_MIN` and `_MAX` bounds, followed by the tags declared inside the range. - Struct and packet declarations were generated in source order, but C++ requires types to be defined before use. The declarations are now generated in dependency order. - The payload field of structs was assigned a slice instead of a byte vector. - The generator panicked when a field of unknown size was followed by fields of non-static size (UWB TestSrRxNtf). The typedef field is now parsed from the remaining span, like the Rust backend does. - The span of a typedef field of unknown size followed by static fields was not advanced past the parsed field.
The accessors were named with heck::ToUpperCamelCase, which does not capitalize letters following a digit (ch3c_shape -> GetCh3cShape), while generate_cxx_backend.py and generate_cxx_backend_tests.py use the Python str.title() convention (ch3c_shape -> GetCh3CShape). Use the same convention to keep the generated code compatible with the code written against the headers generated by generate_cxx_backend.py.
Add test cases for the issues fixed in the C++ backend: - Packet_Enum_Field_Packed: multiple closed enum fields packed in the same bit field chunk. - ScalarGrandChild_C: three levels of inheritance with fields declared at every level, the payload size of the root packet accounts for all the fields. - ScalarChild_D: child packet declaring a payload size field, when the parent packet also declares one. - ConstrainedChild_A, ConstrainedChild_B: children of a packet without payload, specialized by constraints only. The Java backend fails to generate ConstrainedParent, and the Java tests generator sets the constrained fields of grandparent packets; these declarations are excluded from the Java tests.
cxx_compile_check.py generates the C++ backend for a list of PDL files,
generates stub implementations for the declared custom fields and
checksums, and compiles the generated headers with -fsyntax-only.
The script reports the generator failures and compilation errors, and is
used to validate the C++ backend against the PDL grammars of the Android
repository:
./tests/cxx_compile_check.py --pdlc ../target/release/pdlc \
$(find $ANDROID_BUILD_TOP -name '*.pdl')
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.