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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion doc/develop/languages/cpp/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,17 @@ The C++ standard requires the ``main()`` function to have the return type of
has to be selected. Zephyr ignores the return value from main, so applications
should not return status information and should, instead, return zero.

If you need to access data from the device tree in C++ files, add the following
argument to the ``west build`` command to prevent compile-time macro expansion
issues:

.. code-block:: console

EXTRA_GEN_DEFINES_ARGS=--no-inline-comments

.. note::
Do not use C++ for kernel, driver, or system initialization code.

Do not use C++ for kernel, driver, or system initialization code.

Language Features
*****************
Expand Down
7 changes: 7 additions & 0 deletions scripts/dts/gen_defines.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
def main():
global header_file
global flash_area_num
global no_inline_comments

args = parse_args()
no_inline_comments = args.no_inline_comments

edtlib_logger.setup_edtlib_logging()

Expand Down Expand Up @@ -136,6 +138,8 @@ def parse_args() -> argparse.Namespace:
help="path to write header to")
parser.add_argument("--edt-pickle",
help="path to read pickled edtlib.EDT object from")
parser.add_argument("--no-inline-comments", action="store_true",
help="remove comments from #define lines")

return parser.parse_args()

Expand Down Expand Up @@ -1016,6 +1020,9 @@ def out_define(

warn = fr' __WARN("{deprecation_msg}")' if deprecation_msg else ""

if no_inline_comments:
val = re.sub("\\s*/\\*.*?\\*/\\s*", "", str(val))

if width:
s = f"#define {macro.ljust(width)}{warn} {val}"
else:
Expand Down
18 changes: 16 additions & 2 deletions tests/lib/cpp/cxx/app.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* SPDX-License-Identifier: Apache-2.0
*
* Application overlay for creating a fake device instance we can use to
* test. Subset of tests/kernel/device/app.overlay and other app.overlay
* files.
* test. Nested devices have been used to avoid cross-platform issues by
* forcing a specific 'reg' property size on child nodes.
*/

/ {
Expand All @@ -20,4 +20,18 @@

zephyr,deferred-init;
};

test_ctrl: ctrl {
compatible = "fake-cpp-driver";
status = "okay";

#address-cells = < 1 >;
#size-cells = < 0 >;

test_dev2_reg_addr: dev2@2 {
reg = < 2 >;
compatible = "fake-cpp-driver";
status = "okay";
};
};
};
19 changes: 19 additions & 0 deletions tests/lib/cpp/cxx/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,22 @@ PM_DEVICE_DT_DEFINE(DT_NODELABEL(test_dev0_boot), fake_pm_action);

DEVICE_DT_DEFINE(DT_NODELABEL(test_dev0_boot), NULL,
PM_DEVICE_DT_GET(DT_NODELABEL(test_dev0_boot)), NULL, NULL, POST_KERNEL, 34, NULL);

/*
* Test preprocessor comment removal. The reg value is defined as 2 in the
* device tree, but gen_defines.py by default outputs an entry that includes a
* trailing comment, such as:
*
* #define DT_N_S_ctrl_S_dev2_2_REG_IDX_0_VAL_ADDRESS 2 / * 0x2 * /
*
* That comment, when present, confuses the C++ preprocessor and results in
* issues 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'
*/
#define VALUE_FROM_DT DT_REG_ADDR(DT_NODELABEL(test_dev2_reg_addr))
BUILD_ASSERT(UINT32_C(VALUE_FROM_DT) == 2U);
2 changes: 2 additions & 0 deletions tests/lib/cpp/cxx/testcase.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ common:
integration_platforms:
- mps2/an385
- qemu_cortex_a53
extra_args:
- EXTRA_GEN_DEFINES_ARGS=--no-inline-comments

tests:
cpp.main.minimal:
Expand Down
Loading