Skip to content
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

edtlib: always insert root node to the graph #63799

Merged
merged 1 commit into from
Oct 11, 2023

Conversation

gmarull
Copy link
Member

@gmarull gmarull commented Oct 11, 2023

When we have an empty Devicetree, ie,

/dts-v1/;

/ {

};

The node's dep_ordinal is never initialized because the node graph is empty. This ends up with invalid ordinal tokens (-1) in devicetree_generated.h which in turn produce some cryptic compiler errors, see e.g.

error: pasting "dts_ord_" and "-" does not give a valid preprocessing
token
   95 | #define Z_DEVICE_DT_DEV_ID(node_id) _CONCAT(dts_ord_,
      DT_DEP_ORD(node_id))

...

include/zephyr/devicetree.h:2498:41:
note: in expansion of macro 'DT_FOREACH_OKAY_HELPER'
 2498 | #define DT_FOREACH_STATUS_OKAY_NODE(fn)
      DT_FOREACH_OKAY_HELPER(fn)
            |
	    ^~~~~~~~~~~~~~~~~~~~~~
include/zephyr/device.h:1022:1:
note: in expansion of macro 'DT_FOREACH_STATUS_OKAY_NODE'
     1022 |
	  DT_FOREACH_STATUS_OKAY_NODE(Z_MAYBE_DEVICE_DECLARE_INTERNAL)

(devicetree_generated.h)

...
 #define DT_N_ORD -1
 #define DT_N_ORD_STR_SORTABLE 000-1
...

This patch makes sure root node is always inserted (without any target)
so that it gets initialized later.

Discovered as part of
#63696

Fixes #63808

@gmarull gmarull added the bug The issue is a bug, or the PR is fixing a bug label Oct 11, 2023
@gmarull gmarull requested a review from 57300 October 11, 2023 11:01
@gmarull gmarull mentioned this pull request Oct 11, 2023
tejlmand
tejlmand previously approved these changes Oct 11, 2023
@gmarull gmarull added this to the v3.5.0 milestone Oct 11, 2023
@gmarull gmarull requested a review from fabiobaltieri October 11, 2023 11:06
carlescufi
carlescufi previously approved these changes Oct 11, 2023
fabiobaltieri
fabiobaltieri previously approved these changes Oct 11, 2023
Copy link
Member

@fabiobaltieri fabiobaltieri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat!

Copy link
Contributor

@57300 57300 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree with this workaround, because it can hide real issues, like #58501.

The node's dep_ordinal is never initialized because the node graph is empty.

I don't think this should be the case. If the root node exists, then the graph should be non-empty. The problem is that edtlib always adds nodes in pairs, via add_edge(). A better solution would be to always add the root node to the graph. Then, ordinal 0 will be assigned normally.

When we have an empty Devicetree, ie,

```
/dts-v1/;

/ {

};
```

The node's dep_ordinal is never initialized because the node graph is
empty. This ends up with invalid ordinal tokens (-1) in
devicetree_generated.h which in turn produce some cryptic compiler
errors, see e.g.

```
error: pasting "dts_ord_" and "-" does not give a valid preprocessing
token
   95 | #define Z_DEVICE_DT_DEV_ID(node_id) _CONCAT(dts_ord_,
      DT_DEP_ORD(node_id))

...

include/zephyr/devicetree.h:2498:41:
note: in expansion of macro 'DT_FOREACH_OKAY_HELPER'
 2498 | #define DT_FOREACH_STATUS_OKAY_NODE(fn)
      DT_FOREACH_OKAY_HELPER(fn)
            |
	    ^~~~~~~~~~~~~~~~~~~~~~
include/zephyr/device.h:1022:1:
note: in expansion of macro 'DT_FOREACH_STATUS_OKAY_NODE'
     1022 |
	  DT_FOREACH_STATUS_OKAY_NODE(Z_MAYBE_DEVICE_DECLARE_INTERNAL)

```

(devicetree_generated.h)

```
...
 #define DT_N_ORD -1
 #define DT_N_ORD_STR_SORTABLE 000-1
...
```

This patch makes sure root node is always inserted (without any target)
so that it gets initialized later.

Discovered as part of
zephyrproject-rtos#63696

Signed-off-by: Gerard Marull-Paretas <[email protected]>
@gmarull
Copy link
Member Author

gmarull commented Oct 11, 2023

I disagree with this workaround, because it can hide real issues, like #58501.

The node's dep_ordinal is never initialized because the node graph is empty.

I don't think this should be the case. If the root node exists, then the graph should be non-empty. The problem is that edtlib always adds nodes in pairs, via add_edge(). A better solution would be to always add the root node to the graph. Then, ordinal 0 will be assigned normally.

good point, please check latest revision.

@gmarull gmarull changed the title edtlib: initialize dep_ordinal to 0 @gmarull edtlib: always insert root node to the graph Oct 11, 2023
@gmarull gmarull changed the title @gmarull edtlib: always insert root node to the graph edtlib: always insert root node to the graph Oct 11, 2023
Copy link
Contributor

@57300 57300 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, LGTM

@jhedberg jhedberg merged commit 7772cec into zephyrproject-rtos:main Oct 11, 2023
27 checks passed
@gmarull gmarull deleted the edtlib-ordinal-init-value branch October 11, 2023 15:35
@mbolivar-ampere
Copy link
Contributor

This got merged before I had a chance to review, but LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Devicetree bug The issue is a bug, or the PR is fixing a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Projects with empty devicetree files may fail to compile
9 participants