Skip to content

Commit 1481a8b

Browse files
committed
tests/lib/cpp/cxx: add failing test case
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 <[email protected]>
1 parent ec69ccb commit 1481a8b

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

tests/lib/cpp/cxx/app.overlay

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*
66
* Application overlay for creating a fake device instance we can use to
7-
* test. Subset of tests/kernel/device/app.overlay and other app.overlay
8-
* files.
7+
* test. Nested devices have been used to avoid cross-platform issues by
8+
* forcing a specific 'reg' property size on child nodes.
99
*/
1010

1111
/ {
@@ -20,4 +20,18 @@
2020

2121
zephyr,deferred-init;
2222
};
23+
24+
test_ctrl: ctrl {
25+
compatible = "fake-cpp-driver";
26+
status = "okay";
27+
28+
#address-cells = < 1 >;
29+
#size-cells = < 0 >;
30+
31+
test_dev2_reg_addr: dev2@2 {
32+
reg = < 2 >;
33+
compatible = "fake-cpp-driver";
34+
status = "okay";
35+
};
36+
};
2337
};

tests/lib/cpp/cxx/src/main.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,22 @@ PM_DEVICE_DT_DEFINE(DT_NODELABEL(test_dev0_boot), fake_pm_action);
153153

154154
DEVICE_DT_DEFINE(DT_NODELABEL(test_dev0_boot), NULL,
155155
PM_DEVICE_DT_GET(DT_NODELABEL(test_dev0_boot)), NULL, NULL, POST_KERNEL, 34, NULL);
156+
157+
/*
158+
* Test preprocessor comment removal. The reg value is defined as 2 in the
159+
* device tree, but gen_defines.py by default outputs an entry that includes a
160+
* trailing comment, such as:
161+
*
162+
* #define DT_N_S_ctrl_S_dev2_2_REG_IDX_0_VAL_ADDRESS 2 / * 0x2 * /
163+
*
164+
* That comment, when present, confuses the C++ preprocessor and results in
165+
* issues like the following:
166+
*
167+
* error: pasting "/ * 0x2 * /" and "U" does not give a valid preprocessing token
168+
*
169+
* or, in this synthetic test case:
170+
*
171+
* error: unable to find numeric literal operator 'operator""UU'
172+
*/
173+
#define VALUE_FROM_DT DT_REG_ADDR(DT_NODELABEL(test_dev2_reg_addr))
174+
BUILD_ASSERT(UINT32_C(VALUE_FROM_DT) == 2U);

0 commit comments

Comments
 (0)