Skip to content

Commit 7390338

Browse files
mbolivarkartben
authored andcommitted
devicetree: document limitations related to for-each macros
Document a C preprocessor limitation that prevents "nested" use of for-each style devicetree macros in the way users sometimes expect. Put this in the devicetree.h header where it will show up in the detailed doxygen description in our HTML documentation, in addition to being in the sources themselves for people who prefer to look for docstrings there. Signed-off-by: Martí Bolívar <[email protected]>
1 parent e4b04b4 commit 7390338

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

include/zephyr/devicetree.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2929,6 +2929,36 @@
29292929
* @defgroup devicetree-generic-foreach "For-each" macros
29302930
* @ingroup devicetree
29312931
* @{
2932+
*
2933+
* IMPORTANT: you can't use the DT for-each macros in their own expansions.
2934+
*
2935+
* For example, something like this won't work the way you might expect:
2936+
*
2937+
* @code{.c}
2938+
* #define FOO(node_id) [...] DT_FOREACH_NODE(...) [...]
2939+
* DT_FOREACH_NODE(FOO)
2940+
* @endcode
2941+
*
2942+
* In this example, the C preprocessor won't expand the
2943+
* DT_FOREACH_NODE() macro inside of FOO() while it's already
2944+
* expanding DT_FOREACH_NODE() at the top level of the file.
2945+
*
2946+
* This is true of any macro, not just DT_FOREACH_NODE(). The C
2947+
* language works this way to avoid infinite recursions inside of
2948+
* macro expansions.
2949+
*
2950+
* If you need to "nest" calls to one of these macros, you can work
2951+
* around this preprocessor limitation by using a different, related
2952+
* macro instead, like this:
2953+
*
2954+
* @code{.c}
2955+
* #define BAR(node_id) [...] DT_FOREACH_NODE_VARGS(...) [...]
2956+
* DT_FOREACH_NODE(BAR)
2957+
* @endcode
2958+
*
2959+
* Here, we use DT_FOREACH_NODE_VARGS() "inside" BAR() "inside"
2960+
* DT_FOREACH_NODE(). Because of this, the preprocessor will expand
2961+
* both DT_FOREACH_NODE_VARGS() and DT_FOREACH_NODE() as expected.
29322962
*/
29332963

29342964
/**

0 commit comments

Comments
 (0)