Skip to content

c++: allow removing inline comments from devicetree_generated.h #86758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from

Conversation

pillo79
Copy link
Collaborator

@pillo79 pillo79 commented Mar 7, 2025

Preprocessor macros that paste together a number with a suffix, like UINT32_C, do not currently work properly in C++ code for some types of device tree data, such as the reg property. This is because gen_defines.py adds inline comments to those entries in the devicetree_generated.h files, and this confuses the C++ preprocessor during macro expansion, resulting in errors like the following:

error: pasting "/* 0x12345678 */" and "U" does not give a valid preprocessing token

or, in the synthetic test case added in this PR:

  error: unable to find numeric literal operator 'operator""UU'

This may be verified by running twister -T tests/lib/cpp/cxx on just the first commit of this PR, as done in this CI run. Also, an actual example in a downstream project: arduino/ArduinoCore-zephyr#80 (comment) .

This PR fixes the issue by adding an option to the gen_defines.py, using it in the C++ test and mentioning it in the C++ documentation page for downstream users.

This test case is meant to test the use of preprocessor macros that
paste together a number from devicetree with a suffix, like UINT32_C.
The test case is expected to fail because the C++ preprocessor does not
elide comments added by gen_dts_defines.py before expanding the macros,
resulting in errors like the following:

  error: pasting "/* 0x2 */" and "U" does not give
         a valid preprocessing token

or, in this synthetic test case:

  error: unable to find numeric literal operator 'operator""UU'

when accessing values from the devicetree_generated.h header file.

Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
@pillo79 pillo79 changed the title c++: allow removing macros in devicetree_generated.h c++: allow removing inline comments from devicetree_generated.h Mar 7, 2025
pillo79 added 2 commits March 7, 2025 14:58
Add an option to remove inline comments from the #define lines.
This allows including the generated files in C++ code, which
does not elide the comments when performing macro expansion.

Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
The DTS extraction process generates headers with inline comments, which
are not ignored properly by the C++ preprocessor. This is causing build
failures when using devicetree macros in C++ code.

Add a note to the C++ documentation to inform users about the issue and
how to work around it, and update the C++ test case.

Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
@pillo79
Copy link
Collaborator Author

pillo79 commented Mar 12, 2025

Closing after additional investigation as I have identified the actual cause - a nested macro expansion fixed by arduino/ArduinoCore-zephyr@1eade10 . Nothing related to C++. 🤦‍♂️

@pillo79 pillo79 closed this Mar 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant