Skip to contentSkip to conversation

dtc: Move the merging of fragments out of the grammar

PATCH Superseded Simon Glass 3 participants 34 messages Nov 16 – Nov 27

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 cover dtc: Move the merging of fragments out of the grammar

Note: This v4 series now only moves the merge code. The merge still happens during parsing, so none of the problems below are fixed yet. Once this one is sorted out, a new series will be started.

The parser merges each top-level fragment of a dts file into the tree as soon as it is parsed, so nothing can look at a fragment on its own. Here a fragment is a top-level part of the source, such as &label { ... }; or /delete-node/ &label;, not an overlay's fragment@N node.

This causes several problems:

- A repeated node or property name gives a check failure in the first fragment, but silently overwrites when it is in a later fragment
- Deletes cannot be dealt with consistently
- Checks which need the whole tree give false positives for a plugin, yet checks which do not need it cannot run before the merge

The ultimate aim is to run most checks on each fragment before it is merged, and only those which need the whole tree afterwards. For this to work, we need the merge to come after dt_from_source() and build_dt_info() have returned.

As a first step, move the merge logic out of the grammar actions into livetree.c, one action at a time, passing each fragment in a new struct fragment. The next series will have the parser return a list of fragments for main() to merge, then add a plugin's overlay fragments (fragment@N nodes) after the merge, rather than during it.

The output only changes for &{label} { ... } in a plugin, where label is in the plugin itself, which is now merged in the same way as &label { ... }. The only change to the errors is that, in a plugin, a label-relative reference in a fragment which adds a label is reported at the reference rather than the label.

Most of the merge errors have no tests, so the series starts by adding a test for each one, checking the message and its position in the source. At each commit, make check passes and dtc gives the same output and messages for every .dts and .dtso file in Linux v7.3-12284-g3799eeb906c9.

Changes in v4:
- Add the &{label} tests here, rather than in the first patch
- Drop build_fragment_root(), append_fragment() and the next pointer
- Drop label_srcpos, since the position of the reference is enough
- Drop merge-parse-error.dts and merge-overlay-order.dts
- Drop the checks that later fragments are merged after an error
- Drop the srcpos patch, which is applied
- Explain why a fragment which adds a label does not go in an overlay
- Free the fragment after merging it
- Leave the merge after parsing and the fragment@N changes for later
- Look up the reference once, at the top of merge_ref()
- Move the &{label} tests to the patch which changes its behaviour
- Report the label-relative error at the reference, not the label
- Shorten the comment on struct fragment
- Use 0 rather than '\0' in memset()
- Use a label outside the plugin in the label-relative tests

Changes in v3:
- Add merge-overlay-order.dts to check deletes and order with fragments
- Change the subject, since the tests are not only for errors

Changes in v2:
- Add a constructor for each kind of fragment, taking all its fields
- Add merge-label-base.dts to check a label which is not in the plugin
- Add merge-label-braces.dts to check &{label} in a plugin
- Add merge-label-order.dts to check the order of &{label} and &label
- Add merge-root-plugin.dts to check / { ... } against &{/} { ... }
- Add new patch for &{label} in a plugin
- Add the list to struct dt_info later, when it is first used
- Build the fragment in one step, with build_fragment_ref()
- Change the subject from 'Add a list' to 'Add a struct'
- Check the fragment type with assert()
- Combine merge_labelled_ref() into merge_ref()
- Combine merge_path_ref() into merge_ref()
- Combine the rules for label and path references, using dt_ref
- Drop path_ref in favour of checking the ref for a leading '/'
- Explain the aim of the series and what it does not do
- Rename merge_label_ref() to merge_ref()
- Replace FRAGMENT_ROOT and FRAGMENT_REF with FRAGMENT_NODE
- Return true on success from merge_delete_node()
- Return true on success from merge_first_ref()
- Return true on success from merge_omit_if_no_ref()
- Return true on success from merge_ref()
- Reword four comments in the test files
- Use a NULL ref for the root node
- Use dt_ref instead of fragment_ref

Simon Glass (10):
tests: Check how the fragments of a dts file are merged
dtc: Add a struct for the top-level fragments of a dts file
dtc: Move the /delete-node/ handling out of the grammar
dtc: Move the /omit-if-no-ref/ handling out of the grammar
dtc: Move is_ref_relative() to livetree.c
dtc: Move label-reference handling out of the grammar
dtc: Merge &{label} in a plugin as with &label
dtc: Move path-reference handling out of the grammar
dtc: Move labelled-reference handling out of the grammar
dtc: Move first-reference handling out of the grammar

 dtc-parser.y                       |  96 +++++-----------
 dtc.h                              |  37 +++++++
 livetree.c                         | 170 +++++++++++++++++++++++++++++
 tests/merge-delete-missing.dts     |   9 ++
 tests/merge-errors.dts             |  14 +++
 tests/merge-first-ref-relative.dts |   7 ++
 tests/merge-first-ref.dts          |   6 +
 tests/merge-label-base.dts         |  10 ++
 tests/merge-label-braces.dts       |  12 ++
 tests/merge-label-order.dts        |  17 +++
 tests/merge-labelled-plugin.dts    |  12 ++
 tests/merge-labelled-relative.dts  |  10 ++
 tests/merge-omit-missing.dts       |   9 ++
 tests/merge-path-ref-missing.dts   |  11 ++
 tests/merge-path-relative.dts      |  10 ++
 tests/merge-root-plugin.dts        |  12 ++
 tests/run_tests.sh                 |  59 ++++++++++
 17 files changed, 433 insertions(+), 68 deletions(-)
 create mode 100644 tests/merge-delete-missing.dts
 create mode 100644 tests/merge-errors.dts
 create mode 100644 tests/merge-first-ref-relative.dts
 create mode 100644 tests/merge-first-ref.dts
 create mode 100644 tests/merge-label-base.dts
 create mode 100644 tests/merge-label-braces.dts
 create mode 100644 tests/merge-label-order.dts
 create mode 100644 tests/merge-labelled-plugin.dts
 create mode 100644 tests/merge-labelled-relative.dts
 create mode 100644 tests/merge-omit-missing.dts
 create mode 100644 tests/merge-path-ref-missing.dts
 create mode 100644 tests/merge-path-relative.dts
 create mode 100644 tests/merge-root-plugin.dts

---
base-commit: 774adc25940050fb06f241b1a2a98e7421e6ed7b
branch: dt-forest4

Signature · 1 line
2.43.0
#1 lore raw

PATCH v4 1/10 tests: Check how the fragments of a dts file are merged

When the parser merges the top-level fragments of a dts file, it can report that a label or path is not found, or that a label-relative reference is not supported in a plugin. Most of these errors are not covered by any test, and the two tests which do reach them only check that dtc fails, not the message or where it is reported. The merge logic is about to move out of the grammar, so this needs better tests.

Add a test file for each error which is not covered, and check the message and its position for all of them, including the two existing ones. Also check that merging carries on after an error, so that later errors are reported too, that a labelled reference within a plugin adds the label, that / { ... } and &{/} { ... } differ in a plugin, and that a label in a plugin becomes an overlay fragment if it is not found, since nothing covers these either.

Assisted-by Claude:claude-opus-5-5

---

Changes in v4:
- Drop merge-parse-error.dts and merge-overlay-order.dts
- Drop the checks that later fragments are merged after an error
- Move the &{label} tests to the patch which changes its behaviour
- Use a label outside the plugin in the label-relative tests

Changes in v3:
- Add merge-overlay-order.dts to check deletes and order with fragments
- Change the subject, since the tests are not only for errors

Changes in v2:
- Add merge-label-base.dts to check a label which is not in the plugin
- Add merge-label-braces.dts to check &{label} in a plugin
- Add merge-label-order.dts to check the order of &{label} and &label
- Add merge-root-plugin.dts to check / { ... } against &{/} { ... }
- Reword four comments in the test files

 tests/merge-delete-missing.dts     |  9 ++++++
 tests/merge-errors.dts             | 14 +++++++++
 tests/merge-first-ref-relative.dts |  7 +++++
 tests/merge-first-ref.dts          |  6 ++++
 tests/merge-label-base.dts         | 10 +++++++
 tests/merge-labelled-plugin.dts    | 12 ++++++++
 tests/merge-labelled-relative.dts  | 10 +++++++
 tests/merge-omit-missing.dts       |  9 ++++++
 tests/merge-path-ref-missing.dts   | 11 +++++++
 tests/merge-path-relative.dts      | 10 +++++++
 tests/merge-root-plugin.dts        | 12 ++++++++
 tests/run_tests.sh                 | 47 ++++++++++++++++++++++++++++++
 12 files changed, 157 insertions(+)
 create mode 100644 tests/merge-delete-missing.dts
 create mode 100644 tests/merge-errors.dts
 create mode 100644 tests/merge-first-ref-relative.dts
 create mode 100644 tests/merge-first-ref.dts
 create mode 100644 tests/merge-label-base.dts
 create mode 100644 tests/merge-labelled-plugin.dts
 create mode 100644 tests/merge-labelled-relative.dts
 create mode 100644 tests/merge-omit-missing.dts
 create mode 100644 tests/merge-path-ref-missing.dts
 create mode 100644 tests/merge-path-relative.dts
 create mode 100644 tests/merge-root-plugin.dts
tests/merge-delete-missing.dts new file +9 -0
new file mode 100644
index 0000000..b36e39e
@@ -0,0 +1,9 @@
+/dts-v1/;
+
+/ {
+	label: node {
+	};
+};
+
+/* Try to delete a node using a non-existent label */
+/delete-node/ &nosuchnode;
tests/merge-errors.dts new file +14 -0
new file mode 100644
index 0000000..f828e86
@@ -0,0 +1,14 @@
+/dts-v1/;
+
+/ {
+	label: node {
+	};
+};
+
+/* Each fragment below refers to a missing node, and each is reported */
+&nosuchnode1 {
+	prop;
+};
+
+/delete-node/ &nosuchnode2;
+/omit-if-no-ref/ &nosuchnode3;
tests/merge-first-ref-relative.dts new file +7 -0
new file mode 100644
index 0000000..fe4cf9b
@@ -0,0 +1,7 @@
+/dts-v1/;
+/plugin/;
+
+/* A plugin cannot define a node using a label-relative reference */
+&{label/node} {
+	prop;
+};
tests/merge-first-ref.dts new file +6 -0
new file mode 100644
index 0000000..2f585d6
@@ -0,0 +1,6 @@
+/dts-v1/;
+
+/* Only a plugin can start with a reference */
+&nosuchnode {
+	prop;
+};
tests/merge-label-base.dts new file +10 -0
new file mode 100644
index 0000000..1a0a8bb
@@ -0,0 +1,10 @@
+/dts-v1/;
+/plugin/;
+
+/ {
+};
+
+/* A label which is not in the plugin may be in the base tree */
+&{baselabel} {
+	prop;
+};
tests/merge-labelled-plugin.dts new file +12 -0
new file mode 100644
index 0000000..3849cc5
@@ -0,0 +1,12 @@
+/dts-v1/;
+/plugin/;
+
+/ {
+	label: node {
+	};
+};
+
+/* Add a label to a node defined in the plugin itself */
+newlabel: &label {
+	prop = "value";
+};
tests/merge-labelled-relative.dts new file +10 -0
new file mode 100644
index 0000000..8faaf5e
@@ -0,0 +1,10 @@
+/dts-v1/;
+/plugin/;
+
+/ {
+};
+
+/* A label outside the plugin cannot be used in a label-relative reference */
+newlabel: &{baselabel/subnode} {
+	prop;
+};
tests/merge-omit-missing.dts new file +9 -0
new file mode 100644
index 0000000..0357648
@@ -0,0 +1,9 @@
+/dts-v1/;
+
+/ {
+	label: node {
+	};
+};
+
+/* Try to mark a node using a non-existent path */
+/omit-if-no-ref/ &{/nosuchnode};
tests/merge-path-ref-missing.dts new file +11 -0
new file mode 100644
index 0000000..ad2e35e
@@ -0,0 +1,11 @@
+/dts-v1/;
+
+/ {
+	node {
+	};
+};
+
+/* Try to redefine a node using a non-existent path */
+&{/nosuchnode} {
+	prop;
+};
tests/merge-path-relative.dts new file +10 -0
new file mode 100644
index 0000000..0a79379
@@ -0,0 +1,10 @@
+/dts-v1/;
+/plugin/;
+
+/ {
+};
+
+/* A label outside the plugin cannot be used in a label-relative reference */
+&{baselabel/node} {
+	prop;
+};
tests/merge-root-plugin.dts new file +12 -0
new file mode 100644
index 0000000..f67355e
@@ -0,0 +1,12 @@
+/dts-v1/;
+/plugin/;
+
+/* This is in the root node of the plugin */
+/ {
+	prop1;
+};
+
+/* This is an overlay fragment for the root node of the base tree */
+&{/} {
+	prop2;
+};
tests/run_tests.sh +47 -0

53 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v4 01/10] tests: Check how the fragments of a dts file are merged

Quoted 15 lines · Simon Glass

When the parser merges the top-level fragments of a dts file, it can report that a label or path is not found, or that a label-relative reference is not supported in a plugin. Most of these errors are not covered by any test, and the two tests which do reach them only check that dtc fails, not the message or where it is reported. The merge logic is about to move out of the grammar, so this needs better tests.

Add a test file for each error which is not covered, and check the message and its position for all of them, including the two existing ones. Also check that merging carries on after an error, so that later errors are reported too, that a labelled reference within a plugin adds the label, that / { ... } and &{/} { ... } differ in a plugin, and that a label in a plugin becomes an overlay fragment if it is not found, since nothing covers these either.

Quoted 2 lines
Assisted-by Claude:claude-opus-5-5

I've tentatively merged this (not in 'main' for now, but I plan to put it there if no additional problems appear), because I think it's useful enough. There are a couple of warts that would be nice to address in a follow up.

Quoted 192 lines

---

Changes in v4:
- Drop merge-parse-error.dts and merge-overlay-order.dts
- Drop the checks that later fragments are merged after an error
- Move the &{label} tests to the patch which changes its behaviour
- Use a label outside the plugin in the label-relative tests

Changes in v3:
- Add merge-overlay-order.dts to check deletes and order with fragments
- Change the subject, since the tests are not only for errors

Changes in v2:
- Add merge-label-base.dts to check a label which is not in the plugin
- Add merge-label-braces.dts to check &{label} in a plugin
- Add merge-label-order.dts to check the order of &{label} and &label
- Add merge-root-plugin.dts to check / { ... } against &{/} { ... }
- Reword four comments in the test files

 tests/merge-delete-missing.dts     |  9 ++++++
 tests/merge-errors.dts             | 14 +++++++++
 tests/merge-first-ref-relative.dts |  7 +++++
 tests/merge-first-ref.dts          |  6 ++++
 tests/merge-label-base.dts         | 10 +++++++
 tests/merge-labelled-plugin.dts    | 12 ++++++++
 tests/merge-labelled-relative.dts  | 10 +++++++
 tests/merge-omit-missing.dts       |  9 ++++++
 tests/merge-path-ref-missing.dts   | 11 +++++++
 tests/merge-path-relative.dts      | 10 +++++++
 tests/merge-root-plugin.dts        | 12 ++++++++
 tests/run_tests.sh                 | 47 ++++++++++++++++++++++++++++++
 12 files changed, 157 insertions(+)
 create mode 100644 tests/merge-delete-missing.dts
 create mode 100644 tests/merge-errors.dts
 create mode 100644 tests/merge-first-ref-relative.dts
 create mode 100644 tests/merge-first-ref.dts
 create mode 100644 tests/merge-label-base.dts
 create mode 100644 tests/merge-labelled-plugin.dts
 create mode 100644 tests/merge-labelled-relative.dts
 create mode 100644 tests/merge-omit-missing.dts
 create mode 100644 tests/merge-path-ref-missing.dts
 create mode 100644 tests/merge-path-relative.dts
 create mode 100644 tests/merge-root-plugin.dts
tests/merge-delete-missing.dts new file +9 -0
new file mode 100644
index 0000000..b36e39e
@@ -0,0 +1,9 @@
+/dts-v1/;
+
+/ {
+	label: node {
+	};
+};
+
+/* Try to delete a node using a non-existent label */
+/delete-node/ &nosuchnode;
tests/merge-errors.dts new file +14 -0
new file mode 100644
index 0000000..f828e86
@@ -0,0 +1,14 @@
+/dts-v1/;
+
+/ {
+	label: node {
+	};
+};
+
+/* Each fragment below refers to a missing node, and each is reported */
+&nosuchnode1 {
+	prop;
+};
+
+/delete-node/ &nosuchnode2;
+/omit-if-no-ref/ &nosuchnode3;
tests/merge-first-ref-relative.dts new file +7 -0
new file mode 100644
index 0000000..fe4cf9b
@@ -0,0 +1,7 @@
+/dts-v1/;
+/plugin/;
+
+/* A plugin cannot define a node using a label-relative reference */
+&{label/node} {
+	prop;
+};
tests/merge-first-ref.dts new file +6 -0
new file mode 100644
index 0000000..2f585d6
@@ -0,0 +1,6 @@
+/dts-v1/;
+
+/* Only a plugin can start with a reference */
+&nosuchnode {
+	prop;
+};
tests/merge-label-base.dts new file +10 -0
new file mode 100644
index 0000000..1a0a8bb
@@ -0,0 +1,10 @@
+/dts-v1/;
+/plugin/;
+
+/ {
+};
+
+/* A label which is not in the plugin may be in the base tree */
+&{baselabel} {
+	prop;
+};
tests/merge-labelled-plugin.dts new file +12 -0
new file mode 100644
index 0000000..3849cc5
@@ -0,0 +1,12 @@
+/dts-v1/;
+/plugin/;
+
+/ {
+	label: node {
+	};
+};
+
+/* Add a label to a node defined in the plugin itself */
+newlabel: &label {
+	prop = "value";
+};
tests/merge-labelled-relative.dts new file +10 -0
new file mode 100644
index 0000000..8faaf5e
@@ -0,0 +1,10 @@
+/dts-v1/;
+/plugin/;
+
+/ {
+};
+
+/* A label outside the plugin cannot be used in a label-relative reference */
+newlabel: &{baselabel/subnode} {
+	prop;
+};
tests/merge-omit-missing.dts new file +9 -0
new file mode 100644
index 0000000..0357648
@@ -0,0 +1,9 @@
+/dts-v1/;
+
+/ {
+	label: node {
+	};
+};
+
+/* Try to mark a node using a non-existent path */
+/omit-if-no-ref/ &{/nosuchnode};
tests/merge-path-ref-missing.dts new file +11 -0
new file mode 100644
index 0000000..ad2e35e
@@ -0,0 +1,11 @@
+/dts-v1/;
+
+/ {
+	node {
+	};
+};
+
+/* Try to redefine a node using a non-existent path */
+&{/nosuchnode} {
+	prop;
+};
tests/merge-path-relative.dts new file +0 -0
new file mode 100644
index 0000000..0a79379

tests/merge-path-relative.dts line 8

Quoted 8 lines
tests/merge-path-relative.dts new file +8 -0
@@ -0,0 +1,10 @@
+/dts-v1/;
+/plugin/;
+
+/ {
+};
+
+/* A label outside the plugin cannot be used in a label-relative reference */
+&{baselabel/node} {

This seems redundant with merge-labelled-relative.dts, and arguably with nmerge-first-ref-relative.dts as well.

Quoted 10 lines
tests/merge-path-relative.dts new file +2 -0
+	prop;
+};
tests/merge-root-plugin.dts new file +2 -0
new file mode 100644
index 0000000..f67355e
@@ -0,0 +1,12 @@
+/dts-v1/;
+/plugin/;

tests/merge-root-plugin.dts line 12

Quoted 10 lines
tests/merge-root-plugin.dts new file +10 -0
+
+/* This is in the root node of the plugin */
+/ {
+	prop1;
+};
+
+/* This is an overlay fragment for the root node of the base tree */
+&{/} {
+	prop2;
+};

Might be worth commenting that this wasn't particularly intentional and it's not clear if this difference is a good thing, although it's certainly existing behaviour.

Quoted 62 lines
tests/run_tests.sh +47 -0
index 5775e0c..146d288 100755
@@ -770,6 +770,53 @@ dtc_tests () {
     run_sh_test "$SRCDIR/dtc-checkfails.sh" phandle_references -- -I dts -O dtb "$SRCDIR/nonexist-label-ref.dts"
     run_sh_test "$SRCDIR/dtc-fatal.sh" -m "Label or path nosuchnode not found" \
 	-I dts -O dtb "$SRCDIR/nonexist-node-ref2.dts"
+
+    # Check each error when merging the fragments of a dts file, with its
+    # position
+    run_wrap_error_msg_test "test_label_ref.dts:7.8-15 Label or path handle not found" \
+	$DTC -I dts -O dtb -o /dev/null "$SRCDIR/test_label_ref.dts"
+    run_wrap_error_msg_test "nonexist-node-ref2.dts:9.1-12 Label or path nosuchnode not found" \
+	$DTC -I dts -O dtb -o /dev/null "$SRCDIR/nonexist-node-ref2.dts"
+    run_wrap_error_msg_test "merge-first-ref.dts:4.13-6.3 Label or path nosuchnode not found" \
+	$DTC -I dts -O dtb -o /dev/null "$SRCDIR/merge-first-ref.dts"
+    run_wrap_error_msg_test "merge-first-ref-relative.dts:5.15-7.3 Label-relative reference label/node not supported in plugin" \
+	$DTC -I dts -O dtb -o /dev/null "$SRCDIR/merge-first-ref-relative.dts"
+    run_wrap_error_msg_test "merge-labelled-relative.dts:8.1-10 Label-relative reference baselabel/subnode not supported in plugin" \
+	$DTC -I dts -O dtb -o /dev/null "$SRCDIR/merge-labelled-relative.dts"
+    run_wrap_error_msg_test "merge-path-relative.dts:8.1-18 Label-relative reference baselabel/node not supported in plugin" \
+	$DTC -I dts -O dtb -o /dev/null "$SRCDIR/merge-path-relative.dts"
+    run_wrap_error_msg_test "merge-path-ref-missing.dts:9.1-15 Label or path /nosuchnode not found" \
+	$DTC -I dts -O dtb -o /dev/null "$SRCDIR/merge-path-ref-missing.dts"
+    run_wrap_error_msg_test "merge-delete-missing.dts:9.15-26 Label or path nosuchnode not found" \
+	$DTC -I dts -O dtb -o /dev/null "$SRCDIR/merge-delete-missing.dts"
+    run_wrap_error_msg_test "merge-omit-missing.dts:9.18-32 Label or path /nosuchnode not found" \
+	$DTC -I dts -O dtb -o /dev/null "$SRCDIR/merge-omit-missing.dts"
+    run_wrap_error_msg_test "merge-errors.dts:9.1-13 Label or path nosuchnode1 not found" \
+	$DTC -I dts -O dtb -o /dev/null "$SRCDIR/merge-errors.dts"
+    run_wrap_error_msg_test "merge-errors.dts:13.15-27 Label or path nosuchnode2 not found" \
+	$DTC -I dts -O dtb -o /dev/null "$SRCDIR/merge-errors.dts"
+    run_wrap_error_msg_test "merge-errors.dts:14.18-30 Label or path nosuchnode3 not found" \
+	$DTC -I dts -O dtb -o /dev/null "$SRCDIR/merge-errors.dts"
+
+    # A labelled reference to a node in a plugin adds the label to it
+    run_dtc_test -@ -I dts -O dtb -o merge-labelled-plugin.test.dtb \
+	"$SRCDIR/merge-labelled-plugin.dts"
+    run_fdtget_test "/node" merge-labelled-plugin.test.dtb /__symbols__ newlabel
+    run_fdtget_test "value" merge-labelled-plugin.test.dtb /node prop
+
+    # In a plugin, / { ... } and &{/} { ... } are not the same thing
+    run_dtc_test -I dts -O dtb -o merge-root-plugin.test.dtb \
+	"$SRCDIR/merge-root-plugin.dts"
+    run_fdtget_test "" merge-root-plugin.test.dtb / prop1
+    run_fdtget_test "/" merge-root-plugin.test.dtb /fragment@0 target-path
+    run_fdtget_test "" merge-root-plugin.test.dtb /fragment@0/__overlay__ prop2
+
+    # In a plugin, a label which is not found becomes an overlay fragment
+    run_dtc_test -I dts -O dtb -o merge-label-base.test.dtb \
+	"$SRCDIR/merge-label-base.dts"
+    run_fdtget_test "" merge-label-base.test.dtb /fragment@0/__overlay__ prop
+    run_fdtget_test "/fragment@0:target:0" merge-label-base.test.dtb /__fixups__ baselabel
+
     check_tests "$SRCDIR/bad-name-property.dts" name_properties
 
     check_tests "$SRCDIR/bad-ncells.dts" address_cells_is_cell size_cells_is_cell interrupts_extended_is_cell
Signature · 1 line
2.43.0
Signature · 4 lines
David Gibson (he or they)	| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you, not the other way
				| around.
http://www.ozlabs.org/~dgibson
#3 lore raw

Re: [PATCH v4 01/10] tests: Check how the fragments of a dts file are merged

Quoted 16 lines · David Gibson
Quoted 15 lines · Simon Glass

When the parser merges the top-level fragments of a dts file, it can report that a label or path is not found, or that a label-relative reference is not supported in a plugin. Most of these errors are not covered by any test, and the two tests which do reach them only check that dtc fails, not the message or where it is reported. The merge logic is about to move out of the grammar, so this needs better tests.

Add a test file for each error which is not covered, and check the message and its position for all of them, including the two existing ones. Also check that merging carries on after an error, so that later errors are reported too, that a labelled reference within a plugin adds the label, that / { ... } and &{/} { ... } differ in a plugin, and that a label in a plugin becomes an overlay fragment if it is not found, since nothing covers these either.

Quoted 7 lines
Quoted 2 lines
Assisted-by Claude:claude-opus-5-5

I've tentatively merged this (not in 'main' for now, but I plan to put it there if no additional problems appear), because I think it's useful enough. There are a couple of warts that would be nice to address in a follow up.

Now merged to 'main'.

Signature · 4 lines
David Gibson (he or they)	| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you, not the other way
				| around.
http://www.ozlabs.org/~dgibson
#4 lore raw

Re: [PATCH v4 01/10] tests: Check how the fragments of a dts file are merged

In reply to David Gibson

Hi David,

Quoted 17 lines · David Gibson
Quoted 15 lines · Simon Glass

When the parser merges the top-level fragments of a dts file, it can report that a label or path is not found, or that a label-relative reference is not supported in a plugin. Most of these errors are not covered by any test, and the two tests which do reach them only check that dtc fails, not the message or where it is reported. The merge logic is about to move out of the grammar, so this needs better tests.

Add a test file for each error which is not covered, and check the message and its position for all of them, including the two existing ones. Also check that merging carries on after an error, so that later errors are reported too, that a labelled reference within a plugin adds the label, that / { ... } and &{/} { ... } differ in a plugin, and that a label in a plugin becomes an overlay fragment if it is not found, since nothing covers these either.

Quoted 7 lines
Quoted 2 lines
Assisted-by Claude:claude-opus-5-5

I've tentatively merged this (not in 'main' for now, but I plan to put it there if no additional problems appear), because I think it's useful enough. There are a couple of warts that would be nice to address in a follow up.

OK let's drop the redundant tests as part of the next series.

[..]

Regards,
Simon

#5 lore raw

Re: [PATCH v4 01/10] tests: Check how the fragments of a dts file are merged

In reply to David Gibson

Hi David,

Quoted 5 lines · David Gibson
Quoted 2 lines

+/* A label outside the plugin cannot be used in a label-relative reference */ +&{baselabel/node} {

This seems redundant with merge-labelled-relative.dts, and arguably with nmerge-first-ref-relative.dts as well.

Actually let me be a bit more specific...

At present the three go through different grammar actions (path reference, labelled reference and first reference), which this series moves in separate patches, so each one checks its own patch. Once the series is in, the path and labelled cases both end up in merge_ref(), so I'll drop merge-path-relative.dts then.

Quoted 6 lines
Quoted 2 lines

+/* This is an overlay fragment for the root node of the base tree */ +&{/} {

Might be worth commenting that this wasn't particularly intentional and it's not clear if this difference is a good thing, although it's certainly existing behaviour.

Done in v5, in a new first patch.

Regards,
Simon

#6 lore raw

Re: [PATCH v4 01/10] tests: Check how the fragments of a dts file are merged

Quoted 7 lines · Simon Glass

Hi David,

Quoted 3 lines · David Gibson
Quoted 2 lines

+/* A label outside the plugin cannot be used in a label-relative reference */ +&{baselabel/node} {

Quoted 7 lines
Quoted 2 lines

This seems redundant with merge-labelled-relative.dts, and arguably with nmerge-first-ref-relative.dts as well.

Actually let me be a bit more specific...

At present the three go through different grammar actions (path reference, labelled reference and first reference),

Ah, ok, so it's about source coverage more than feature coverage. That's reasonable but I think the comments need some updates for clarity.

Quoted 17 lines

which this series
moves in separate patches, so each one checks its own patch. Once the series is in, the path and labelled cases both end up in merge_ref(), so I'll drop merge-path-relative.dts then.

Quoted 6 lines
Quoted 2 lines

+/* This is an overlay fragment for the root node of the base tree */ +&{/} {

Might be worth commenting that this wasn't particularly intentional and it's not clear if this difference is a good thing, although it's certainly existing behaviour.

Done in v5, in a new first patch.

Regards,
Simon

Signature · 4 lines
David Gibson (he or they)	| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you, not the other way
				| around.
http://www.ozlabs.org/~dgibson
#7 lore raw

PATCH v4 2/10 dtc: Add a struct for the top-level fragments of a dts file

In reply to Simon Glass

A dts file is made up of top-level fragments: the root node, later definitions of a node by label or path, and /delete-node/ or /omit-if-no-ref/ with a reference. We would like to deal with each fragment on its own before it is merged, for example to apply deletes consistently or to run some checks on each one, but that requires keeping the fragments apart. At present the parser merges each one into the tree as soon as it is parsed.

Add a struct fragment to hold one which refers to a node, with a function to build each kind and one to free it.

These are not used yet.

Assisted-by Claude:claude-opus-5-5

---

Changes in v4:
- Drop build_fragment_root(), append_fragment() and the next pointer
- Drop label_srcpos, since the position of the reference is enough
- Shorten the comment on struct fragment
- Use 0 rather than '\0' in memset()

Changes in v2:
- Add a constructor for each kind of fragment, taking all its fields
- Add the list to struct dt_info later, when it is first used
- Change the subject from 'Add a list' to 'Add a struct'
- Drop path_ref in favour of checking the ref for a leading '/'
- Replace FRAGMENT_ROOT and FRAGMENT_REF with FRAGMENT_NODE
- Use a NULL ref for the root node

 dtc.h      | 33 +++++++++++++++++++++++++++++++++
 livetree.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 85 insertions(+)
dtc.h +33 -0
index 791f815..ac18616 100644
@@ -318,6 +318,39 @@ struct reserve_info *chain_reserve_entry(struct reserve_info *first,
 struct reserve_info *add_reserve_entry(struct reserve_info *list,
 				       struct reserve_info *new);
 
+/*
+ * A fragment is one top-level part of a dts file, after the header and any
+ * memory reservations, which refers to a node by label or path: a later
+ * definition of the node, or a /delete-node/ or /omit-if-no-ref/. The root
+ * node, / { ... };, is not held as a fragment.
+ *
+ * This is not the same as an overlay fragment (a fragment@N node), although
+ * in a plugin a fragment may become one when it is merged.
+ */
+
+enum fragment_type {
+	FRAGMENT_NODE,		/* [label:] &ref { ... }; */
+	FRAGMENT_DELETE_NODE,	/* /delete-node/ &ref; */
+	FRAGMENT_OMIT_IF_NO_REF, /* /omit-if-no-ref/ &ref; */
+};
+
+/* The label and node are NULL if the fragment does not have them */
+struct fragment {
+	enum fragment_type type;
+	char *ref;			/* label or path referred to */
+	struct srcpos *ref_srcpos;	/* position of ref */
+	char *label;			/* label to add to the target */
+	struct node *node;		/* the node definition */
+};
+
+struct fragment *build_fragment_ref(char *label, char *ref,
+				    struct srcpos *ref_srcpos,
+				    struct node *node);
+struct fragment *build_fragment_delete_node(char *ref,
+					    struct srcpos *ref_srcpos);
+struct fragment *build_fragment_omit_if_no_ref(char *ref,
+					       struct srcpos *ref_srcpos);
+void free_fragment(struct fragment *f);
 
 struct dt_info {
 	unsigned int dtsflags;
livetree.c +52 -0

58 lines not on this page · raw message

Signature · 1 line
2.43.0
Reviewed-by on v4 2/10
#8 lore raw

Re: [PATCH v4 02/10] dtc: Add a struct for the top-level fragments of a dts file

Quoted 8 lines · Simon Glass

A dts file is made up of top-level fragments: the root node, later definitions of a node by label or path, and /delete-node/ or /omit-if-no-ref/ with a reference. We would like to deal with each fragment on its own before it is merged, for example to apply deletes consistently or to run some checks on each one, but that requires keeping the fragments apart. At present the parser merges each one into the tree as soon as it is parsed.

Quoted 4 lines

Add a struct fragment to hold one which refers to a node, with a function to build each kind and one to free it.

These are not used yet.

After consideration of our previous discussions, I'm leaning towards using the term "source layer" for these top level components (thanks for the "layer" suggestion). It's more verbose than I'd like, but I think disambiguating from dtb level "fragments" is worth it.

Quoted 3 lines
Assisted-by Claude:claude-opus-5-5

Apart from the suggested name change,

Quoted 131 lines

---

Changes in v4:
- Drop build_fragment_root(), append_fragment() and the next pointer
- Drop label_srcpos, since the position of the reference is enough
- Shorten the comment on struct fragment
- Use 0 rather than '\0' in memset()

Changes in v2:
- Add a constructor for each kind of fragment, taking all its fields
- Add the list to struct dt_info later, when it is first used
- Change the subject from 'Add a list' to 'Add a struct'
- Drop path_ref in favour of checking the ref for a leading '/'
- Replace FRAGMENT_ROOT and FRAGMENT_REF with FRAGMENT_NODE
- Use a NULL ref for the root node

 dtc.h      | 33 +++++++++++++++++++++++++++++++++
 livetree.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 85 insertions(+)
dtc.h +33 -0
index 791f815..ac18616 100644
@@ -318,6 +318,39 @@ struct reserve_info *chain_reserve_entry(struct reserve_info *first,
 struct reserve_info *add_reserve_entry(struct reserve_info *list,
 				       struct reserve_info *new);
 
+/*
+ * A fragment is one top-level part of a dts file, after the header and any
+ * memory reservations, which refers to a node by label or path: a later
+ * definition of the node, or a /delete-node/ or /omit-if-no-ref/. The root
+ * node, / { ... };, is not held as a fragment.
+ *
+ * This is not the same as an overlay fragment (a fragment@N node), although
+ * in a plugin a fragment may become one when it is merged.
+ */
+
+enum fragment_type {
+	FRAGMENT_NODE,		/* [label:] &ref { ... }; */
+	FRAGMENT_DELETE_NODE,	/* /delete-node/ &ref; */
+	FRAGMENT_OMIT_IF_NO_REF, /* /omit-if-no-ref/ &ref; */
+};
+
+/* The label and node are NULL if the fragment does not have them */
+struct fragment {
+	enum fragment_type type;
+	char *ref;			/* label or path referred to */
+	struct srcpos *ref_srcpos;	/* position of ref */
+	char *label;			/* label to add to the target */
+	struct node *node;		/* the node definition */
+};
+
+struct fragment *build_fragment_ref(char *label, char *ref,
+				    struct srcpos *ref_srcpos,
+				    struct node *node);
+struct fragment *build_fragment_delete_node(char *ref,
+					    struct srcpos *ref_srcpos);
+struct fragment *build_fragment_omit_if_no_ref(char *ref,
+					       struct srcpos *ref_srcpos);
+void free_fragment(struct fragment *f);
 
 struct dt_info {
 	unsigned int dtsflags;
livetree.c +52 -0
index abe73de..4231c64 100644
@@ -448,6 +448,58 @@ struct reserve_info *add_reserve_entry(struct reserve_info *list,
 	return list;
 }
 
+/* Create a fragment of the given type, with its other fields NULL */
+static struct fragment *new_fragment(enum fragment_type type, char *ref,
+				     struct srcpos *ref_srcpos)
+{
+	struct fragment *new = xmalloc(sizeof(*new));
+
+	memset(new, 0, sizeof(*new));
+
+	new->type = type;
+	new->ref = ref;
+	new->ref_srcpos = srcpos_copy(ref_srcpos);
+
+	return new;
+}
+
+/* Create a fragment for [label:] &ref { ... }; where label is NULL if absent */
+struct fragment *build_fragment_ref(char *label, char *ref,
+				    struct srcpos *ref_srcpos,
+				    struct node *node)
+{
+	struct fragment *new = new_fragment(FRAGMENT_NODE, ref, ref_srcpos);
+
+	new->label = label;
+	new->node = node;
+
+	return new;
+}
+
+/* Create a fragment for /delete-node/ &ref; */
+struct fragment *build_fragment_delete_node(char *ref,
+					    struct srcpos *ref_srcpos)
+{
+	return new_fragment(FRAGMENT_DELETE_NODE, ref, ref_srcpos);
+}
+
+/* Create a fragment for /omit-if-no-ref/ &ref; */
+struct fragment *build_fragment_omit_if_no_ref(char *ref,
+					       struct srcpos *ref_srcpos)
+{
+	return new_fragment(FRAGMENT_OMIT_IF_NO_REF, ref, ref_srcpos);
+}
+
+/*
+ * Free a fragment, but not its node, reference or label, since the tree may
+ * still use these
+ */
+void free_fragment(struct fragment *f)
+{
+	srcpos_free(f->ref_srcpos);
+	free(f);
+}
+
 static void fill_fullpaths(struct node *tree, const char *prefix)
 {
 	struct node *child;
Signature · 1 line
2.43.0
Signature · 4 lines
David Gibson (he or they)	| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you, not the other way
				| around.
http://www.ozlabs.org/~dgibson
#9 lore raw

Re: [PATCH v4 02/10] dtc: Add a struct for the top-level fragments of a dts file

Hi David,

Quoted 13 lines · David Gibson
Quoted 11 lines · Simon Glass

A dts file is made up of top-level fragments: the root node, later definitions of a node by label or path, and /delete-node/ or /omit-if-no-ref/ with a reference. We would like to deal with each fragment on its own before it is merged, for example to apply deletes consistently or to run some checks on each one, but that requires keeping the fragments apart. At present the parser merges each one into the tree as soon as it is parsed.

Add a struct fragment to hold one which refers to a node, with a function to build each kind and one to free it.

Quoted 6 lines
Quoted 1 line

These are not used yet.

After consideration of our previous discussions, I'm leaning towards using the term "source layer" for these top level components (thanks for the "layer" suggestion). It's more verbose than I'd like, but I think disambiguating from dtb level "fragments" is worth it.

I really like that! Will update.

Regards,
Simon

Quoted 29 lines
Quoted 4 lines
Assisted-by Claude:claude-opus-5-5

Apart from the suggested name change,

Quoted 19 lines

---

Changes in v4:
- Drop build_fragment_root(), append_fragment() and the next pointer
- Drop label_srcpos, since the position of the reference is enough
- Shorten the comment on struct fragment
- Use 0 rather than '\0' in memset()

Changes in v2:
- Add a constructor for each kind of fragment, taking all its fields
- Add the list to struct dt_info later, when it is first used
- Change the subject from 'Add a list' to 'Add a struct'
- Drop path_ref in favour of checking the ref for a leading '/'
- Replace FRAGMENT_ROOT and FRAGMENT_REF with FRAGMENT_NODE
- Use a NULL ref for the root node

 dtc.h      | 33 +++++++++++++++++++++++++++++++++
 livetree.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 85 insertions(+)
#10 lore raw

Re: [PATCH v4 02/10] dtc: Add a struct for the top-level fragments of a dts file

In reply to David Gibson

Hi David,

Quoted 4 lines · David Gibson

After consideration of our previous discussions, I'm leaning towards using the term "source layer" for these top level components (thanks for the "layer" suggestion). It's more verbose than I'd like, but I think disambiguating from dtb level "fragments" is worth it.

OK, done in v5, through the series. The struct is struct source_layer, but to keep the lines short the helpers just say 'layer':
build_layer_ref(), free_layer(), LAYER_NODE and so on. Let me know if you would prefer source_layer throughout. 'Overlay fragment' and fragment@N are unchanged.

Regards,
Simon

#11 lore raw

Re: [PATCH v4 02/10] dtc: Add a struct for the top-level fragments of a dts file

Hi Simon, David,

Quoted 9 lines · On Thu, 26 Nov 2026 10:34:24 -0800 Simon Glass <[email protected]> wrote:

Hi David,

Quoted 4 lines · David Gibson

After consideration of our previous discussions, I'm leaning towards using the term "source layer" for these top level components (thanks for the "layer" suggestion). It's more verbose than I'd like, but I think disambiguating from dtb level "fragments" is worth it.

Quoted 6 lines

OK, done in v5, through the series. The struct is struct source_layer, but to keep the lines short the helpers just say 'layer':
build_layer_ref(), free_layer(), LAYER_NODE and so on. Let me know if you would prefer source_layer throughout. 'Overlay fragment' and fragment@N are unchanged.

Maybe late but "snippet" instead of "source_layer" or "layer" comes in my mind.

Could this "snippet" make sense?

Best regards,
Hervé

#12 lore raw

Re: [PATCH v4 02/10] dtc: Add a struct for the top-level fragments of a dts file

Quoted 21 lines · Herve Codina

Hi Simon, David,

Quoted 9 lines · On Thu, 26 Nov 2026 10:34:24 -0800 Simon Glass <[email protected]> wrote:

Hi David,

Quoted 4 lines · David Gibson

After consideration of our previous discussions, I'm leaning towards using the term "source layer" for these top level components (thanks for the "layer" suggestion). It's more verbose than I'd like, but I think disambiguating from dtb level "fragments" is worth it.

Quoted 6 lines

OK, done in v5, through the series. The struct is struct source_layer, but to keep the lines short the helpers just say 'layer':
build_layer_ref(), free_layer(), LAYER_NODE and so on. Let me know if you would prefer source_layer throughout. 'Overlay fragment' and fragment@N are unchanged.

Quoted 4 lines

Maybe late but "snippet" instead of "source_layer" or "layer" comes in my mind.

Could this "snippet" make sense?

Eh. "snippet"s not a bad idea, but I prefer "source layer" even though it's longer.

1) To me "snippet" suggests something small, but the layers can be an entire dt
2) "layer" at leasts hints at the correct merge semantics: later layers "cover" (overwrite) earlier layers.

Signature · 4 lines
David Gibson (he or they)	| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you, not the other way
				| around.
http://www.ozlabs.org/~dgibson
#13 lore raw

Re: [PATCH v4 02/10] dtc: Add a struct for the top-level fragments of a dts file

Hi David,

Quoted 27 lines · On Fri, 27 Nov 2026 13:29:23 +1100 David Gibson <[email protected]>…
Quoted 21 lines · Herve Codina

Hi Simon, David,

Quoted 9 lines · On Thu, 26 Nov 2026 10:34:24 -0800 Simon Glass <[email protected]> wrote:

Hi David,

Quoted 4 lines · David Gibson

After consideration of our previous discussions, I'm leaning towards using the term "source layer" for these top level components (thanks for the "layer" suggestion). It's more verbose than I'd like, but I think disambiguating from dtb level "fragments" is worth it.

Quoted 6 lines

OK, done in v5, through the series. The struct is struct source_layer, but to keep the lines short the helpers just say 'layer':
build_layer_ref(), free_layer(), LAYER_NODE and so on. Let me know if you would prefer source_layer throughout. 'Overlay fragment' and fragment@N are unchanged.

Quoted 4 lines

Maybe late but "snippet" instead of "source_layer" or "layer" comes in my mind.

Could this "snippet" make sense?

Quoted 8 lines

Eh. "snippet"s not a bad idea, but I prefer "source layer" even though it's longer.

1) To me "snippet" suggests something small, but the layers can be an entire dt
2) "layer" at leasts hints at the correct merge semantics: later layers "cover" (overwrite) earlier layers.

Make sense. Let's go with "layer".

You can just ignore the same comment I did in the v5 iteration.

Best regards,
Hervé

#14 lore raw

Re: [PATCH v4 02/10] dtc: Add a struct for the top-level fragments of a dts file

In reply to Simon Glass
Quoted 9 lines · Simon Glass

Hi David,

Quoted 4 lines · David Gibson

After consideration of our previous discussions, I'm leaning towards using the term "source layer" for these top level components (thanks for the "layer" suggestion). It's more verbose than I'd like, but I think disambiguating from dtb level "fragments" is worth it.

Quoted 4 lines

OK, done in v5, through the series. The struct is struct source_layer, but to keep the lines short the helpers just say 'layer':
build_layer_ref(), free_layer(), LAYER_NODE and so on. Let me know if you would prefer source_layer throughout.

That's probably fine, although srclayer or sl would also be reasonable options.

Quoted 2 lines

'Overlay fragment' and
fragment@N are unchanged.

Of course.

Signature · 4 lines
David Gibson (he or they)	| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you, not the other way
				| around.
http://www.ozlabs.org/~dgibson
#15 lore raw

PATCH v4 3/10 dtc: Move the /delete-node/ handling out of the grammar

In reply to Simon Glass

The grammar actions merge each top-level fragment of a dts file into the tree as it is parsed, so the merge logic is spread over the rules for each kind of fragment. This makes it hard to follow and to change.

As a first step towards moving this logic into livetree.c, move the action for a top-level /delete-node/ into a new merge_delete_node(), passing it a struct fragment for the reference.

The output does not change.

Assisted-by Claude:claude-opus-5-5

---

Changes in v4:
- Free the fragment after merging it

Changes in v2:
- Check the fragment type with assert()
- Return true on success from merge_delete_node()
- Use dt_ref instead of fragment_ref

 dtc-parser.y | 12 +++++-------
 dtc.h        |  1 +
 livetree.c   | 20 ++++++++++++++++++++
 3 files changed, 26 insertions(+), 7 deletions(-)
dtc-parser.y +5 -7
index 45a699a..6bf96a7 100644
@@ -239,14 +239,12 @@ devicetree:
 		}
 	| devicetree DT_DEL_NODE dt_ref ';'
 		{
-			struct node *target = get_node_by_ref($1, $3);
-
-			if (target)
-				delete_node(target);
-			else
-				ERROR(&@3, "Label or path %s not found", $3);
-
+			struct fragment *f;
 
+			f = build_fragment_delete_node($3, &@3);
+			if (!merge_delete_node($1, f))
+				treesource_error = true;
+			free_fragment(f);
 			$$ = $1;
 		}
 	| devicetree DT_OMIT_NO_REF dt_ref ';'
dtc.h +1 -0
index ac18616..41ba438 100644
@@ -351,6 +351,7 @@ struct fragment *build_fragment_delete_node(char *ref,
 struct fragment *build_fragment_omit_if_no_ref(char *ref,
 					       struct srcpos *ref_srcpos);
 void free_fragment(struct fragment *f);
+bool merge_delete_node(struct node *dt, struct fragment *f);
 
 struct dt_info {
 	unsigned int dtsflags;
livetree.c +20 -0
index 4231c64..ce47fe2 100644
@@ -534,6 +534,26 @@ struct dt_info *build_dt_info(unsigned int dtsflags,
 	return dti;
 }
 
+/*
+ * Delete the node which a /delete-node/ fragment refers to
+ *
+ * Returns true on success, false on an error, which has been reported.
+ */
+bool merge_delete_node(struct node *dt, struct fragment *f)
+{
+	struct node *target = get_node_by_ref(dt, f->ref);
+
+	assert(f->type == FRAGMENT_DELETE_NODE);
+	if (!target) {
+		srcpos_error(f->ref_srcpos, "Error",
+			     "Label or path %s not found", f->ref);
+		return false;
+	}
+	delete_node(target);
+
+	return true;
+}
+
 /*
  * Tree accessor functions
  */
Signature · 1 line
2.43.0
Reviewed-by on v4 3/10
#16 lore raw

Re: [PATCH v4 03/10] dtc: Move the /delete-node/ handling out of the grammar

Quoted 4 lines · Simon Glass

The grammar actions merge each top-level fragment of a dts file into the tree as it is parsed, so the merge logic is spread over the rules for each kind of fragment. This makes it hard to follow and to change.

Quoted 8 lines

As a first step towards moving this logic into livetree.c, move the action for a top-level /delete-node/ into a new merge_delete_node(), passing it a struct fragment for the reference.

The output does not change.

Assisted-by Claude:claude-opus-5-5

Apart from the name change suggested earlier:

Quoted 86 lines

---

Changes in v4:
- Free the fragment after merging it

Changes in v2:
- Check the fragment type with assert()
- Return true on success from merge_delete_node()
- Use dt_ref instead of fragment_ref

 dtc-parser.y | 12 +++++-------
 dtc.h        |  1 +
 livetree.c   | 20 ++++++++++++++++++++
 3 files changed, 26 insertions(+), 7 deletions(-)
dtc-parser.y +5 -7
index 45a699a..6bf96a7 100644
@@ -239,14 +239,12 @@ devicetree:
 		}
 	| devicetree DT_DEL_NODE dt_ref ';'
 		{
-			struct node *target = get_node_by_ref($1, $3);
-
-			if (target)
-				delete_node(target);
-			else
-				ERROR(&@3, "Label or path %s not found", $3);
-
+			struct fragment *f;
 
+			f = build_fragment_delete_node($3, &@3);
+			if (!merge_delete_node($1, f))
+				treesource_error = true;
+			free_fragment(f);
 			$$ = $1;
 		}
 	| devicetree DT_OMIT_NO_REF dt_ref ';'
dtc.h +1 -0
index ac18616..41ba438 100644
@@ -351,6 +351,7 @@ struct fragment *build_fragment_delete_node(char *ref,
 struct fragment *build_fragment_omit_if_no_ref(char *ref,
 					       struct srcpos *ref_srcpos);
 void free_fragment(struct fragment *f);
+bool merge_delete_node(struct node *dt, struct fragment *f);
 
 struct dt_info {
 	unsigned int dtsflags;
livetree.c +20 -0
index 4231c64..ce47fe2 100644
@@ -534,6 +534,26 @@ struct dt_info *build_dt_info(unsigned int dtsflags,
 	return dti;
 }
 
+/*
+ * Delete the node which a /delete-node/ fragment refers to
+ *
+ * Returns true on success, false on an error, which has been reported.
+ */
+bool merge_delete_node(struct node *dt, struct fragment *f)
+{
+	struct node *target = get_node_by_ref(dt, f->ref);
+
+	assert(f->type == FRAGMENT_DELETE_NODE);
+	if (!target) {
+		srcpos_error(f->ref_srcpos, "Error",
+			     "Label or path %s not found", f->ref);
+		return false;
+	}
+	delete_node(target);
+
+	return true;
+}
+
 /*
  * Tree accessor functions
  */
Signature · 1 line
2.43.0
Signature · 4 lines
David Gibson (he or they)	| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you, not the other way
				| around.
http://www.ozlabs.org/~dgibson
#17 lore raw

PATCH v4 4/10 dtc: Move the /omit-if-no-ref/ handling out of the grammar

In reply to Simon Glass

The grammar action for a top-level /omit-if-no-ref/ still holds its merge logic.

Move it into a new merge_omit_if_no_ref(), passing it a struct fragment for the reference.

The output does not change.

Assisted-by Claude:claude-opus-5-5

---

Changes in v4:
- Free the fragment after merging it

Changes in v2:
- Check the fragment type with assert()
- Return true on success from merge_omit_if_no_ref()
- Use dt_ref instead of fragment_ref

 dtc-parser.y | 12 +++++-------
 dtc.h        |  1 +
 livetree.c   | 21 +++++++++++++++++++++
 3 files changed, 27 insertions(+), 7 deletions(-)
dtc-parser.y +5 -7
index 6bf96a7..1a1a52d 100644
@@ -249,14 +249,12 @@ devicetree:
 		}
 	| devicetree DT_OMIT_NO_REF dt_ref ';'
 		{
-			struct node *target = get_node_by_ref($1, $3);
-
-			if (target)
-				omit_node_if_unused(target);
-			else
-				ERROR(&@3, "Label or path %s not found", $3);
-
+			struct fragment *f;
 
+			f = build_fragment_omit_if_no_ref($3, &@3);
+			if (!merge_omit_if_no_ref($1, f))
+				treesource_error = true;
+			free_fragment(f);
 			$$ = $1;
 		}
 	;
dtc.h +1 -0
index 41ba438..b9cb5f9 100644
@@ -352,6 +352,7 @@ struct fragment *build_fragment_omit_if_no_ref(char *ref,
 					       struct srcpos *ref_srcpos);
 void free_fragment(struct fragment *f);
 bool merge_delete_node(struct node *dt, struct fragment *f);
+bool merge_omit_if_no_ref(struct node *dt, struct fragment *f);
 
 struct dt_info {
 	unsigned int dtsflags;
livetree.c +21 -0
index ce47fe2..669d8ba 100644
@@ -554,6 +554,27 @@ bool merge_delete_node(struct node *dt, struct fragment *f)
 	return true;
 }
 
+/*
+ * Mark the node which an /omit-if-no-ref/ fragment refers to, so that it is
+ * dropped if nothing refers to it
+ *
+ * Returns true on success, false on an error, which has been reported.
+ */
+bool merge_omit_if_no_ref(struct node *dt, struct fragment *f)
+{
+	struct node *target = get_node_by_ref(dt, f->ref);
+
+	assert(f->type == FRAGMENT_OMIT_IF_NO_REF);
+	if (!target) {
+		srcpos_error(f->ref_srcpos, "Error",
+			     "Label or path %s not found", f->ref);
+		return false;
+	}
+	omit_node_if_unused(target);
+
+	return true;
+}
+
 /*
  * Tree accessor functions
  */
Signature · 1 line
2.43.0
Reviewed-by on v4 4/10
#18 lore raw

Re: [PATCH v4 04/10] dtc: Move the /omit-if-no-ref/ handling out of the grammar

Quoted 11 lines · Simon Glass

The grammar action for a top-level /omit-if-no-ref/ still holds its merge logic.

Move it into a new merge_omit_if_no_ref(), passing it a struct fragment for the reference.

The output does not change.

Assisted-by Claude:claude-opus-5-5

Apart from the name change suggested elsewhere,

Quoted 87 lines

---

Changes in v4:
- Free the fragment after merging it

Changes in v2:
- Check the fragment type with assert()
- Return true on success from merge_omit_if_no_ref()
- Use dt_ref instead of fragment_ref

 dtc-parser.y | 12 +++++-------
 dtc.h        |  1 +
 livetree.c   | 21 +++++++++++++++++++++
 3 files changed, 27 insertions(+), 7 deletions(-)
dtc-parser.y +5 -7
index 6bf96a7..1a1a52d 100644
@@ -249,14 +249,12 @@ devicetree:
 		}
 	| devicetree DT_OMIT_NO_REF dt_ref ';'
 		{
-			struct node *target = get_node_by_ref($1, $3);
-
-			if (target)
-				omit_node_if_unused(target);
-			else
-				ERROR(&@3, "Label or path %s not found", $3);
-
+			struct fragment *f;
 
+			f = build_fragment_omit_if_no_ref($3, &@3);
+			if (!merge_omit_if_no_ref($1, f))
+				treesource_error = true;
+			free_fragment(f);
 			$$ = $1;
 		}
 	;
dtc.h +1 -0
index 41ba438..b9cb5f9 100644
@@ -352,6 +352,7 @@ struct fragment *build_fragment_omit_if_no_ref(char *ref,
 					       struct srcpos *ref_srcpos);
 void free_fragment(struct fragment *f);
 bool merge_delete_node(struct node *dt, struct fragment *f);
+bool merge_omit_if_no_ref(struct node *dt, struct fragment *f);
 
 struct dt_info {
 	unsigned int dtsflags;
livetree.c +21 -0
index ce47fe2..669d8ba 100644
@@ -554,6 +554,27 @@ bool merge_delete_node(struct node *dt, struct fragment *f)
 	return true;
 }
 
+/*
+ * Mark the node which an /omit-if-no-ref/ fragment refers to, so that it is
+ * dropped if nothing refers to it
+ *
+ * Returns true on success, false on an error, which has been reported.
+ */
+bool merge_omit_if_no_ref(struct node *dt, struct fragment *f)
+{
+	struct node *target = get_node_by_ref(dt, f->ref);
+
+	assert(f->type == FRAGMENT_OMIT_IF_NO_REF);
+	if (!target) {
+		srcpos_error(f->ref_srcpos, "Error",
+			     "Label or path %s not found", f->ref);
+		return false;
+	}
+	omit_node_if_unused(target);
+
+	return true;
+}
+
 /*
  * Tree accessor functions
  */
Signature · 1 line
2.43.0
Signature · 4 lines
David Gibson (he or they)	| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you, not the other way
				| around.
http://www.ozlabs.org/~dgibson
#19 lore raw

PATCH v4 5/10 dtc: Move is_ref_relative() to livetree.c

In reply to Simon Glass

The remaining grammar actions which merge a fragment use is_ref_relative() to reject a label-relative reference in a plugin.

Move it to livetree.c, so that it is available when these actions move there too, and add a comment.

Assisted-by Claude:claude-opus-5-5

---

(no changes since v1)

 dtc-parser.y | 5 -----
 dtc.h        | 1 +
 livetree.c   | 6 ++++++
 3 files changed, 7 insertions(+), 5 deletions(-)
dtc-parser.y +0 -5
index 1a1a52d..9e8091a 100644
@@ -25,11 +25,6 @@ extern void yyerror(char const *s);
 extern struct dt_info *parser_output;
 extern bool treesource_error;
 
-static bool is_ref_relative(const char *ref)
-{
-	return ref[0] != '/' && strchr(&ref[1], '/');
-}
-
 %}
 
 %union {
dtc.h +1 -0
index b9cb5f9..b236229 100644
@@ -351,6 +351,7 @@ struct fragment *build_fragment_delete_node(char *ref,
 struct fragment *build_fragment_omit_if_no_ref(char *ref,
 					       struct srcpos *ref_srcpos);
 void free_fragment(struct fragment *f);
+bool is_ref_relative(const char *ref);
 bool merge_delete_node(struct node *dt, struct fragment *f);
 bool merge_omit_if_no_ref(struct node *dt, struct fragment *f);
 
livetree.c +6 -0
index 669d8ba..388a2de 100644
@@ -534,6 +534,12 @@ struct dt_info *build_dt_info(unsigned int dtsflags,
 	return dti;
 }
 
+/* Check whether a reference is relative to a label, e.g. &{label/node} */
+bool is_ref_relative(const char *ref)
+{
+	return ref[0] != '/' && strchr(&ref[1], '/');
+}
+
 /*
  * Delete the node which a /delete-node/ fragment refers to
  *
Signature · 1 line
2.43.0
#20 lore raw

Re: [PATCH v4 05/10] dtc: Move is_ref_relative() to livetree.c

Quoted 9 lines · Simon Glass

The remaining grammar actions which merge a fragment use is_ref_relative() to reject a label-relative reference in a plugin.

Move it to livetree.c, so that it is available when these actions move there too, and add a comment.

Assisted-by Claude:claude-opus-5-5

I already merged this, you need to rebase your series.

Signature · 4 lines
David Gibson (he or they)	| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you, not the other way
				| around.
http://www.ozlabs.org/~dgibson
#21 lore raw

PATCH v4 6/10 dtc: Move label-reference handling out of the grammar

In reply to Simon Glass

The grammar action for a top-level &label { ... } holds its merge logic, including making an overlay fragment in a plugin when the label is not found.

Move it into a new merge_ref(), passing it a struct fragment for the reference and node.

The output does not change.

Assisted-by Claude:claude-opus-5-5

---

Changes in v4:
- Free the fragment after merging it

Changes in v2:
- Build the fragment in one step, with build_fragment_ref()
- Check the fragment type with assert()
- Rename merge_label_ref() to merge_ref()
- Return true on success from merge_ref()

 dtc-parser.y | 24 ++++++++++--------------
 dtc.h        |  1 +
 livetree.c   | 26 ++++++++++++++++++++++++++
 3 files changed, 37 insertions(+), 14 deletions(-)
dtc-parser.y +10 -14
index 9e8091a..2d4dcb6 100644
@@ -215,21 +215,17 @@ devicetree:
 		}
 	| devicetree DT_LABEL_REF nodedef
 		{
-			struct node *target = get_node_by_ref($1, $2);
+			struct fragment *f;
 
-			if (target) {
-				merge_nodes(target, $3);
-			} else {
-				/*
-				 * We rely on the rule being always:
-				 *   versioninfo plugindecl memreserves devicetree
-				 * so $-1 is what we want (plugindecl)
-				 */
-				if ($<flags>-1 & DTSF_PLUGIN)
-					add_orphan_node($1, $3, $2);
-				else
-					ERROR(&@2, "Label or path %s not found", $2);
-			}
+			f = build_fragment_ref(NULL, $2, &@2, $3);
+			/*
+			 * We rely on the rule being always:
+			 *   versioninfo plugindecl memreserves devicetree
+			 * so $-1 is what we want (plugindecl)
+			 */
+			if (!merge_ref($1, f, $<flags>-1 & DTSF_PLUGIN))
+				treesource_error = true;
+			free_fragment(f);
 			$$ = $1;
 		}
 	| devicetree DT_DEL_NODE dt_ref ';'
dtc.h +1 -0
index b236229..c5b8aee 100644
@@ -352,6 +352,7 @@ struct fragment *build_fragment_omit_if_no_ref(char *ref,
 					       struct srcpos *ref_srcpos);
 void free_fragment(struct fragment *f);
 bool is_ref_relative(const char *ref);
+bool merge_ref(struct node *dt, struct fragment *f, bool plugin);
 bool merge_delete_node(struct node *dt, struct fragment *f);
 bool merge_omit_if_no_ref(struct node *dt, struct fragment *f);
 
livetree.c +26 -0
index 388a2de..2ed57ae 100644
@@ -540,6 +540,32 @@ bool is_ref_relative(const char *ref)
 	return ref[0] != '/' && strchr(&ref[1], '/');
 }
 
+/*
+ * Merge a fragment which refers to a label into the node with that label. In
+ * a plugin, a label which is not found becomes an overlay fragment.
+ *
+ * Returns true on success, false on an error, which has been reported.
+ */
+bool merge_ref(struct node *dt, struct fragment *f, bool plugin)
+{
+	struct node *target = get_node_by_ref(dt, f->ref);
+
+	assert(f->type == FRAGMENT_NODE);
+	if (target) {
+		merge_nodes(target, f->node);
+		return true;
+	}
+	if (!plugin) {
+		srcpos_error(f->ref_srcpos, "Error",
+			     "Label or path %s not found", f->ref);
+		return false;
+	}
+	/* The label may be in the base tree, so add a fragment@N node for it */
+	add_orphan_node(dt, f->node, f->ref);
+
+	return true;
+}
+
 /*
  * Delete the node which a /delete-node/ fragment refers to
  *
Signature · 1 line
2.43.0
#22 lore raw

Re: [PATCH v4 06/10] dtc: Move label-reference handling out of the grammar

Quoted 4 lines · Simon Glass

The grammar action for a top-level &label { ... } holds its merge logic, including making an overlay fragment in a plugin when the label is not found.

Quoted 8 lines

Move it into a new merge_ref(), passing it a struct fragment for the reference and node.

The output does not change.

Assisted-by Claude:claude-opus-5-5

Still, excepting the name change suggested elsewhere.

Signature · 4 lines
David Gibson (he or they)	| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you, not the other way
				| around.
http://www.ozlabs.org/~dgibson
#23 lore raw

PATCH v4 7/10 dtc: Merge &{label} in a plugin as with &label

In reply to Simon Glass

In a plugin, &label { ... } is merged into the labelled node if the label is in the plugin, as of commit 0fcffda15e9f ("Merge nodes with local target label references"). But &{label} { ... } always becomes a separate fragment@N node, since anything in braces is lexed as a path reference. This adds a fragment which is not needed, which that commit aims to avoid, and fails with FDT_ERR_NOTFOUND if the label is not within a fragment.

Treat a reference as a path only if it starts with '/', so that both forms are merged into the node. This changes the output for such a file. Applying an overlay which already works gives the same result, apart from the order of the properties and that a later &label { ... } now overrides an earlier &{label} { ... }, as the source order suggests.

Other references are not affected. An absolute path still becomes a fragment@N node, as does a label which is not in the plugin, since it may be in the base tree.

This also means that dtc does not need to record whether a reference is written in braces. The merge code, which is about to move out of the grammar, can tell a path from a label by looking at the reference.

Add the merge-label-braces.dts and merge-label-order.dts tests to check this.

Assisted-by Claude:claude-opus-5-5

---

Changes in v4:
- Add the &{label} tests here, rather than in the first patch

Changes in v2:
- Add new patch for &{label} in a plugin

 dtc-parser.y                 | 10 +++++++---
 tests/merge-label-braces.dts | 12 ++++++++++++
 tests/merge-label-order.dts  | 17 +++++++++++++++++
 tests/run_tests.sh           | 12 ++++++++++++
 4 files changed, 48 insertions(+), 3 deletions(-)
 create mode 100644 tests/merge-label-braces.dts
 create mode 100644 tests/merge-label-order.dts
dtc-parser.y +7 -3
index 2d4dcb6..7e1e6c7 100644
@@ -194,6 +194,8 @@ devicetree:
 		}
 	| devicetree DT_PATH_REF nodedef
 		{
+			struct node *target = get_node_by_ref($1, $2);
+
 			/*
 			 * We rely on the rule being always:
 			 *   versioninfo plugindecl memreserves devicetree
@@ -202,10 +204,12 @@ devicetree:
 			if ($<flags>-1 & DTSF_PLUGIN) {
 				if (is_ref_relative($2))
 					ERROR(&@2, "Label-relative reference %s not supported in plugin", $2);
-				add_orphan_node($1, $3, $2);
+				/* A path is in the base tree and a label may be */
+				if ($2[0] == '/' || !target)
+					add_orphan_node($1, $3, $2);
+				else
+					merge_nodes(target, $3);
 			} else {
-				struct node *target = get_node_by_ref($1, $2);
-
 				if (target)
 					merge_nodes(target, $3);
 				else
tests/merge-label-braces.dts new file +12 -0
new file mode 100644
index 0000000..569e7f8
@@ -0,0 +1,12 @@
+/dts-v1/;
+/plugin/;
+
+/ {
+	label: node {
+	};
+};
+
+/* A label in the plugin itself is merged, however it is written */
+&{label} {
+	prop;
+};
tests/merge-label-order.dts new file +17 -0
new file mode 100644
index 0000000..40fa668
@@ -0,0 +1,17 @@
+/dts-v1/;
+/plugin/;
+
+&basenode {
+	label: foo {
+		p = "first";
+	};
+};
+
+/* The last definition wins */
+&{label} {
+	p = "second";
+};
+
+&label {
+	p = "third";
+};
tests/run_tests.sh +12 -0
index 146d288..7f9a24a 100755
@@ -804,6 +804,18 @@ dtc_tests () {
     run_fdtget_test "/node" merge-labelled-plugin.test.dtb /__symbols__ newlabel
     run_fdtget_test "value" merge-labelled-plugin.test.dtb /node prop
 
+    # In a plugin, &{label} is the same as &label
+    run_dtc_test -I dts -O dtb -o merge-label-braces.test.dtb \
+	"$SRCDIR/merge-label-braces.dts"
+    run_fdtget_test "" merge-label-braces.test.dtb /node prop
+    run_fdtget_test "node" merge-label-braces.test.dtb -l /
+
+    # The last definition wins, with no fragment for &{label}
+    run_dtc_test -I dts -O dtb -o merge-label-order.test.dtb \
+	"$SRCDIR/merge-label-order.dts"
+    run_fdtget_test "third" merge-label-order.test.dtb /fragment@0/__overlay__/foo p
+    run_fdtget_test "fragment@0\n__fixups__" merge-label-order.test.dtb -l /
+
     # In a plugin, / { ... } and &{/} { ... } are not the same thing
     run_dtc_test -I dts -O dtb -o merge-root-plugin.test.dtb \
 	"$SRCDIR/merge-root-plugin.dts"
Signature · 1 line
2.43.0
Reviewed-by on v4 7/10
#24 lore raw

Re: [PATCH v4 07/10] dtc: Merge &{label} in a plugin as with &label

Quoted 22 lines · Simon Glass

In a plugin, &label { ... } is merged into the labelled node if the label is in the plugin, as of commit 0fcffda15e9f ("Merge nodes with local target label references"). But &{label} { ... } always becomes a separate fragment@N node, since anything in braces is lexed as a path reference. This adds a fragment which is not needed, which that commit aims to avoid, and fails with FDT_ERR_NOTFOUND if the label is not within a fragment.

Treat a reference as a path only if it starts with '/', so that both forms are merged into the node. This changes the output for such a file. Applying an overlay which already works gives the same result, apart from the order of the properties and that a later &label { ... } now overrides an earlier &{label} { ... }, as the source order suggests.

Other references are not affected. An absolute path still becomes a fragment@N node, as does a label which is not in the plugin, since it may be in the base tree.

This also means that dtc does not need to record whether a reference is written in braces. The merge code, which is about to move out of the grammar, can tell a path from a label by looking at the reference.

Quoted 5 lines

Add the merge-label-braces.dts and merge-label-order.dts tests to check this.

Assisted-by Claude:claude-opus-5-5

Excepting the name change.

Quoted 113 lines

---

Changes in v4:
- Add the &{label} tests here, rather than in the first patch

Changes in v2:
- Add new patch for &{label} in a plugin

 dtc-parser.y                 | 10 +++++++---
 tests/merge-label-braces.dts | 12 ++++++++++++
 tests/merge-label-order.dts  | 17 +++++++++++++++++
 tests/run_tests.sh           | 12 ++++++++++++
 4 files changed, 48 insertions(+), 3 deletions(-)
 create mode 100644 tests/merge-label-braces.dts
 create mode 100644 tests/merge-label-order.dts
dtc-parser.y +7 -3
index 2d4dcb6..7e1e6c7 100644
@@ -194,6 +194,8 @@ devicetree:
 		}
 	| devicetree DT_PATH_REF nodedef
 		{
+			struct node *target = get_node_by_ref($1, $2);
+
 			/*
 			 * We rely on the rule being always:
 			 *   versioninfo plugindecl memreserves devicetree
@@ -202,10 +204,12 @@ devicetree:
 			if ($<flags>-1 & DTSF_PLUGIN) {
 				if (is_ref_relative($2))
 					ERROR(&@2, "Label-relative reference %s not supported in plugin", $2);
-				add_orphan_node($1, $3, $2);
+				/* A path is in the base tree and a label may be */
+				if ($2[0] == '/' || !target)
+					add_orphan_node($1, $3, $2);
+				else
+					merge_nodes(target, $3);
 			} else {
-				struct node *target = get_node_by_ref($1, $2);
-
 				if (target)
 					merge_nodes(target, $3);
 				else
tests/merge-label-braces.dts new file +12 -0
new file mode 100644
index 0000000..569e7f8
@@ -0,0 +1,12 @@
+/dts-v1/;
+/plugin/;
+
+/ {
+	label: node {
+	};
+};
+
+/* A label in the plugin itself is merged, however it is written */
+&{label} {
+	prop;
+};
tests/merge-label-order.dts new file +17 -0
new file mode 100644
index 0000000..40fa668
@@ -0,0 +1,17 @@
+/dts-v1/;
+/plugin/;
+
+&basenode {
+	label: foo {
+		p = "first";
+	};
+};
+
+/* The last definition wins */
+&{label} {
+	p = "second";
+};
+
+&label {
+	p = "third";
+};
tests/run_tests.sh +12 -0
index 146d288..7f9a24a 100755
@@ -804,6 +804,18 @@ dtc_tests () {
     run_fdtget_test "/node" merge-labelled-plugin.test.dtb /__symbols__ newlabel
     run_fdtget_test "value" merge-labelled-plugin.test.dtb /node prop
 
+    # In a plugin, &{label} is the same as &label
+    run_dtc_test -I dts -O dtb -o merge-label-braces.test.dtb \
+	"$SRCDIR/merge-label-braces.dts"
+    run_fdtget_test "" merge-label-braces.test.dtb /node prop
+    run_fdtget_test "node" merge-label-braces.test.dtb -l /
+
+    # The last definition wins, with no fragment for &{label}
+    run_dtc_test -I dts -O dtb -o merge-label-order.test.dtb \
+	"$SRCDIR/merge-label-order.dts"
+    run_fdtget_test "third" merge-label-order.test.dtb /fragment@0/__overlay__/foo p
+    run_fdtget_test "fragment@0\n__fixups__" merge-label-order.test.dtb -l /
+
     # In a plugin, / { ... } and &{/} { ... } are not the same thing
     run_dtc_test -I dts -O dtb -o merge-root-plugin.test.dtb \
 	"$SRCDIR/merge-root-plugin.dts"
Signature · 1 line
2.43.0
Signature · 4 lines
David Gibson (he or they)	| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you, not the other way
				| around.
http://www.ozlabs.org/~dgibson
#25 lore raw

PATCH v4 8/10 dtc: Move path-reference handling out of the grammar

In reply to Simon Glass

The grammar action for a top-level &{/path} { ... } holds its merge logic, including making an overlay fragment in a plugin.

Move it into merge_ref(), which can tell a path from a label by looking at the reference, and use a single rule for both kinds of reference.

The output does not change.

Assisted-by Claude:claude-opus-5-5

---

Changes in v4:
- Look up the reference once, at the top of merge_ref()

Changes in v2:
- Combine merge_path_ref() into merge_ref()
- Combine the rules for label and path references, using dt_ref

 dtc-parser.y | 27 +--------------------------
 livetree.c   | 30 +++++++++++++++++++++---------
 2 files changed, 22 insertions(+), 35 deletions(-)
dtc-parser.y +1 -26
index 7e1e6c7..c85b77d 100644
@@ -192,32 +192,7 @@ devicetree:
 				ERROR(&@3, "Label or path %s not found", $3);
 			$$ = $1;
 		}
-	| devicetree DT_PATH_REF nodedef
-		{
-			struct node *target = get_node_by_ref($1, $2);
-
-			/*
-			 * We rely on the rule being always:
-			 *   versioninfo plugindecl memreserves devicetree
-			 * so $-1 is what we want (plugindecl)
-			 */
-			if ($<flags>-1 & DTSF_PLUGIN) {
-				if (is_ref_relative($2))
-					ERROR(&@2, "Label-relative reference %s not supported in plugin", $2);
-				/* A path is in the base tree and a label may be */
-				if ($2[0] == '/' || !target)
-					add_orphan_node($1, $3, $2);
-				else
-					merge_nodes(target, $3);
-			} else {
-				if (target)
-					merge_nodes(target, $3);
-				else
-					ERROR(&@2, "Label or path %s not found", $2);
-			}
-			$$ = $1;
-		}
-	| devicetree DT_LABEL_REF nodedef
+	| devicetree dt_ref nodedef
 		{
 			struct fragment *f;
 
livetree.c +21 -9
index 2ed57ae..290467f 100644
@@ -541,29 +541,41 @@ bool is_ref_relative(const char *ref)
 }
 
 /*
- * Merge a fragment which refers to a label into the node with that label. In
- * a plugin, a label which is not found becomes an overlay fragment.
+ * Merge a fragment which refers to a label or path into the node it refers to.
+ *
+ * In a plugin, a path always refers to the base tree and a label may do so, if
+ * it is not found. In both cases, the fragment becomes an overlay fragment.
  *
  * Returns true on success, false on an error, which has been reported.
  */
 bool merge_ref(struct node *dt, struct fragment *f, bool plugin)
 {
 	struct node *target = get_node_by_ref(dt, f->ref);
+	bool ok = true;
 
 	assert(f->type == FRAGMENT_NODE);
-	if (target) {
-		merge_nodes(target, f->node);
-		return true;
+	if (plugin) {
+		if (is_ref_relative(f->ref)) {
+			srcpos_error(f->ref_srcpos, "Error",
+				     "Label-relative reference %s not supported in plugin",
+				     f->ref);
+			ok = false;
+		}
+		if (f->ref[0] == '/' || !target) {
+			/* Add a fragment@N node for the reference */
+			add_orphan_node(dt, f->node, f->ref);
+			return ok;
+		}
 	}
-	if (!plugin) {
+
+	if (!target) {
 		srcpos_error(f->ref_srcpos, "Error",
 			     "Label or path %s not found", f->ref);
 		return false;
 	}
-	/* The label may be in the base tree, so add a fragment@N node for it */
-	add_orphan_node(dt, f->node, f->ref);
+	merge_nodes(target, f->node);
 
-	return true;
+	return ok;
 }
 
 /*
Signature · 1 line
2.43.0
Reviewed-by on v4 8/10
#26 lore raw

Re: [PATCH v4 08/10] dtc: Move path-reference handling out of the grammar

Quoted 10 lines · Simon Glass

The grammar action for a top-level &{/path} { ... } holds its merge logic, including making an overlay fragment in a plugin.

Move it into merge_ref(), which can tell a path from a label by looking at the reference, and use a single rule for both kinds of reference.

The output does not change.

Assisted-by Claude:claude-opus-5-5

Excepting the name change.

Quoted 110 lines

---

Changes in v4:
- Look up the reference once, at the top of merge_ref()

Changes in v2:
- Combine merge_path_ref() into merge_ref()
- Combine the rules for label and path references, using dt_ref

 dtc-parser.y | 27 +--------------------------
 livetree.c   | 30 +++++++++++++++++++++---------
 2 files changed, 22 insertions(+), 35 deletions(-)
dtc-parser.y +1 -26
index 7e1e6c7..c85b77d 100644
@@ -192,32 +192,7 @@ devicetree:
 				ERROR(&@3, "Label or path %s not found", $3);
 			$$ = $1;
 		}
-	| devicetree DT_PATH_REF nodedef
-		{
-			struct node *target = get_node_by_ref($1, $2);
-
-			/*
-			 * We rely on the rule being always:
-			 *   versioninfo plugindecl memreserves devicetree
-			 * so $-1 is what we want (plugindecl)
-			 */
-			if ($<flags>-1 & DTSF_PLUGIN) {
-				if (is_ref_relative($2))
-					ERROR(&@2, "Label-relative reference %s not supported in plugin", $2);
-				/* A path is in the base tree and a label may be */
-				if ($2[0] == '/' || !target)
-					add_orphan_node($1, $3, $2);
-				else
-					merge_nodes(target, $3);
-			} else {
-				if (target)
-					merge_nodes(target, $3);
-				else
-					ERROR(&@2, "Label or path %s not found", $2);
-			}
-			$$ = $1;
-		}
-	| devicetree DT_LABEL_REF nodedef
+	| devicetree dt_ref nodedef
 		{
 			struct fragment *f;
 
livetree.c +21 -9
index 2ed57ae..290467f 100644
@@ -541,29 +541,41 @@ bool is_ref_relative(const char *ref)
 }
 
 /*
- * Merge a fragment which refers to a label into the node with that label. In
- * a plugin, a label which is not found becomes an overlay fragment.
+ * Merge a fragment which refers to a label or path into the node it refers to.
+ *
+ * In a plugin, a path always refers to the base tree and a label may do so, if
+ * it is not found. In both cases, the fragment becomes an overlay fragment.
  *
  * Returns true on success, false on an error, which has been reported.
  */
 bool merge_ref(struct node *dt, struct fragment *f, bool plugin)
 {
 	struct node *target = get_node_by_ref(dt, f->ref);
+	bool ok = true;
 
 	assert(f->type == FRAGMENT_NODE);
-	if (target) {
-		merge_nodes(target, f->node);
-		return true;
+	if (plugin) {
+		if (is_ref_relative(f->ref)) {
+			srcpos_error(f->ref_srcpos, "Error",
+				     "Label-relative reference %s not supported in plugin",
+				     f->ref);
+			ok = false;
+		}
+		if (f->ref[0] == '/' || !target) {
+			/* Add a fragment@N node for the reference */
+			add_orphan_node(dt, f->node, f->ref);
+			return ok;
+		}
 	}
-	if (!plugin) {
+
+	if (!target) {
 		srcpos_error(f->ref_srcpos, "Error",
 			     "Label or path %s not found", f->ref);
 		return false;
 	}
-	/* The label may be in the base tree, so add a fragment@N node for it */
-	add_orphan_node(dt, f->node, f->ref);
+	merge_nodes(target, f->node);
 
-	return true;
+	return ok;
 }
 
 /*
Signature · 1 line
2.43.0
Signature · 4 lines
David Gibson (he or they)	| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you, not the other way
				| around.
http://www.ozlabs.org/~dgibson
#27 lore raw

PATCH v4 9/10 dtc: Move labelled-reference handling out of the grammar

In reply to Simon Glass

The grammar action for a top-level label: &ref { ... } holds its merge logic, which adds the label to the node referred to.

Move it into merge_ref(), passing it a struct fragment with the label.

The output does not change. In a plugin, the error for a label-relative reference now gives the position of the reference rather than the label, since the problem is with the reference. Update the merge-labelled-relative.dts test to match.

Assisted-by Claude:claude-opus-5-5

---

Changes in v4:
- Explain why a fragment which adds a label does not go in an overlay
- Free the fragment after merging it
- Report the label-relative error at the reference, not the label

Changes in v2:
- Combine merge_labelled_ref() into merge_ref()
- Use dt_ref instead of fragment_ref

 dtc-parser.y       | 14 +++++---------
 livetree.c         | 11 ++++++++---
 tests/run_tests.sh |  2 +-
 3 files changed, 14 insertions(+), 13 deletions(-)
dtc-parser.y +5 -9
index c85b77d..6f7ef24 100644
@@ -180,16 +180,12 @@ devicetree:
 		}
 	| devicetree DT_LABEL dt_ref nodedef
 		{
-			struct node *target = get_node_by_ref($1, $3);
-
-			if (($<flags>-1 & DTSF_PLUGIN) && is_ref_relative($3))
-				ERROR(&@2, "Label-relative reference %s not supported in plugin", $3);
+			struct fragment *f;
 
-			if (target) {
-				add_label(&target->labels, $2);
-				merge_nodes(target, $4);
-			} else
-				ERROR(&@3, "Label or path %s not found", $3);
+			f = build_fragment_ref($2, $3, &@3, $4);
+			if (!merge_ref($1, f, $<flags>-1 & DTSF_PLUGIN))
+				treesource_error = true;
+			free_fragment(f);
 			$$ = $1;
 		}
 	| devicetree dt_ref nodedef
livetree.c +8 -3
index 290467f..cf6849a 100644
@@ -541,10 +541,13 @@ bool is_ref_relative(const char *ref)
 }
 
 /*
- * Merge a fragment which refers to a label or path into the node it refers to.
+ * Merge a fragment which refers to a label or path into the node it refers to,
+ * adding any label in the fragment to that node.
  *
  * In a plugin, a path always refers to the base tree and a label may do so, if
- * it is not found. In both cases, the fragment becomes an overlay fragment.
+ * it is not found. In both cases, the fragment becomes an overlay fragment,
+ * unless it adds a label, since dtc cannot put a label on the target of an
+ * overlay fragment.
  *
  * Returns true on success, false on an error, which has been reported.
  */
@@ -561,7 +564,7 @@ bool merge_ref(struct node *dt, struct fragment *f, bool plugin)
 				     f->ref);
 			ok = false;
 		}
-		if (f->ref[0] == '/' || !target) {
+		if (!f->label && (f->ref[0] == '/' || !target)) {
 			/* Add a fragment@N node for the reference */
 			add_orphan_node(dt, f->node, f->ref);
 			return ok;
@@ -573,6 +576,8 @@ bool merge_ref(struct node *dt, struct fragment *f, bool plugin)
 			     "Label or path %s not found", f->ref);
 		return false;
 	}
+	if (f->label)
+		add_label(&target->labels, f->label);
 	merge_nodes(target, f->node);
 
 	return ok;
tests/run_tests.sh +1 -1
index 7f9a24a..62732a4 100755
@@ -781,7 +781,7 @@ dtc_tests () {
 	$DTC -I dts -O dtb -o /dev/null "$SRCDIR/merge-first-ref.dts"
     run_wrap_error_msg_test "merge-first-ref-relative.dts:5.15-7.3 Label-relative reference label/node not supported in plugin" \
 	$DTC -I dts -O dtb -o /dev/null "$SRCDIR/merge-first-ref-relative.dts"
-    run_wrap_error_msg_test "merge-labelled-relative.dts:8.1-10 Label-relative reference baselabel/subnode not supported in plugin" \
+    run_wrap_error_msg_test "merge-labelled-relative.dts:8.11-31 Label-relative reference baselabel/subnode not supported in plugin" \
 	$DTC -I dts -O dtb -o /dev/null "$SRCDIR/merge-labelled-relative.dts"
     run_wrap_error_msg_test "merge-path-relative.dts:8.1-18 Label-relative reference baselabel/node not supported in plugin" \
 	$DTC -I dts -O dtb -o /dev/null "$SRCDIR/merge-path-relative.dts"
Signature · 1 line
2.43.0
#28 lore raw

Re: [PATCH v4 09/10] dtc: Move labelled-reference handling out of the grammar

Quoted 61 lines · Simon Glass

The grammar action for a top-level label: &ref { ... } holds its merge logic, which adds the label to the node referred to.

Move it into merge_ref(), passing it a struct fragment with the label.

The output does not change. In a plugin, the error for a label-relative reference now gives the position of the reference rather than the label, since the problem is with the reference. Update the merge-labelled-relative.dts test to match.

Assisted-by Claude:claude-opus-5-5

---

Changes in v4:
- Explain why a fragment which adds a label does not go in an overlay
- Free the fragment after merging it
- Report the label-relative error at the reference, not the label

Changes in v2:
- Combine merge_labelled_ref() into merge_ref()
- Use dt_ref instead of fragment_ref

 dtc-parser.y       | 14 +++++---------
 livetree.c         | 11 ++++++++---
 tests/run_tests.sh |  2 +-
 3 files changed, 14 insertions(+), 13 deletions(-)
dtc-parser.y +5 -9
index c85b77d..6f7ef24 100644
@@ -180,16 +180,12 @@ devicetree:
 		}
 	| devicetree DT_LABEL dt_ref nodedef
 		{
-			struct node *target = get_node_by_ref($1, $3);
-
-			if (($<flags>-1 & DTSF_PLUGIN) && is_ref_relative($3))
-				ERROR(&@2, "Label-relative reference %s not supported in plugin", $3);
+			struct fragment *f;
 
-			if (target) {
-				add_label(&target->labels, $2);
-				merge_nodes(target, $4);
-			} else
-				ERROR(&@3, "Label or path %s not found", $3);
+			f = build_fragment_ref($2, $3, &@3, $4);
+			if (!merge_ref($1, f, $<flags>-1 & DTSF_PLUGIN))
+				treesource_error = true;
+			free_fragment(f);
 			$$ = $1;
 		}
 	| devicetree dt_ref nodedef
livetree.c +0 -0
index 290467f..cf6849a 100644
@@ -541,10 +541,13 @@ bool is_ref_relative(const char *ref)
 }
 

livetree.c bool is_ref_relative(const char *ref) · line 550

Quoted 10 lines
livetree.c +5 -2
 /*
- * Merge a fragment which refers to a label or path into the node it refers to.
+ * Merge a fragment which refers to a label or path into the node it refers to,
+ * adding any label in the fragment to that node.
  *
  * In a plugin, a path always refers to the base tree and a label may do so, if
- * it is not found. In both cases, the fragment becomes an overlay fragment.
+ * it is not found. In both cases, the fragment becomes an overlay fragment,
+ * unless it adds a label, since dtc cannot put a label on the target of an
+ * overlay fragment.

This doesn't seem quite right. It looks like we ignore the label in that case, whereas we should throw an error, since we can't create a dtbo which does what was requested.

Quoted 38 lines
livetree.c +3 -1
  *
  * Returns true on success, false on an error, which has been reported.
  */
@@ -561,7 +564,7 @@ bool merge_ref(struct node *dt, struct fragment *f, bool plugin)
 				     f->ref);
 			ok = false;
 		}
-		if (f->ref[0] == '/' || !target) {
+		if (!f->label && (f->ref[0] == '/' || !target)) {
 			/* Add a fragment@N node for the reference */
 			add_orphan_node(dt, f->node, f->ref);
 			return ok;
@@ -573,6 +576,8 @@ bool merge_ref(struct node *dt, struct fragment *f, bool plugin)
 			     "Label or path %s not found", f->ref);
 		return false;
 	}
+	if (f->label)
+		add_label(&target->labels, f->label);
 	merge_nodes(target, f->node);
 
 	return ok;
tests/run_tests.sh +1 -1
index 7f9a24a..62732a4 100755
@@ -781,7 +781,7 @@ dtc_tests () {
 	$DTC -I dts -O dtb -o /dev/null "$SRCDIR/merge-first-ref.dts"
     run_wrap_error_msg_test "merge-first-ref-relative.dts:5.15-7.3 Label-relative reference label/node not supported in plugin" \
 	$DTC -I dts -O dtb -o /dev/null "$SRCDIR/merge-first-ref-relative.dts"
-    run_wrap_error_msg_test "merge-labelled-relative.dts:8.1-10 Label-relative reference baselabel/subnode not supported in plugin" \
+    run_wrap_error_msg_test "merge-labelled-relative.dts:8.11-31 Label-relative reference baselabel/subnode not supported in plugin" \
 	$DTC -I dts -O dtb -o /dev/null "$SRCDIR/merge-labelled-relative.dts"
     run_wrap_error_msg_test "merge-path-relative.dts:8.1-18 Label-relative reference baselabel/node not supported in plugin" \
 	$DTC -I dts -O dtb -o /dev/null "$SRCDIR/merge-path-relative.dts"
Signature · 1 line
2.43.0
Signature · 4 lines
David Gibson (he or they)	| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you, not the other way
				| around.
http://www.ozlabs.org/~dgibson
#29 lore raw

Re: [PATCH v4 09/10] dtc: Move labelled-reference handling out of the grammar

Hi David,

Quoted 7 lines · David Gibson
Quoted 3 lines

+ * it is not found. In both cases, the fragment becomes an overlay fragment,
+ * unless it adds a label, since dtc cannot put a label on the target of an
+ * overlay fragment.

This doesn't seem quite right. It looks like we ignore the label in that case, whereas we should throw an error, since we can't create a dtbo which does what was requested.

The label isn't ignored. A source layer which adds a label is merged into the plugin's own tree, so if the node is not there, dtc reports an error. For example, in a plugin:

    newlabel: &nosuchnode { ... };

gives 'Label or path nosuchnode not found', and the same for a path. But the comment gave the wrong impression, so I'll change it to say that this is an error, with a new patch adds a test for both cases.

Regards,
Simon

#30 lore raw

Re: [PATCH v4 09/10] dtc: Move labelled-reference handling out of the grammar

Quoted 12 lines · Simon Glass

Hi David,

Quoted 7 lines · David Gibson
Quoted 3 lines

+ * it is not found. In both cases, the fragment becomes an overlay fragment,
+ * unless it adds a label, since dtc cannot put a label on the target of an
+ * overlay fragment.

This doesn't seem quite right. It looks like we ignore the label in that case, whereas we should throw an error, since we can't create a dtbo which does what was requested.

Quoted 5 lines

The label isn't ignored. A source layer which adds a label is merged into the plugin's own tree, so if the node is not there, dtc reports an error. For example, in a plugin:

    newlabel: &nosuchnode { ... };

Oh, sorry, I missed that.

Quoted 3 lines

gives 'Label or path nosuchnode not found', and the same for a path. But the comment gave the wrong impression, so I'll change it to say that this is an error, with a new patch adds a test for both cases.

Thanks.

Signature · 4 lines
David Gibson (he or they)	| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you, not the other way
				| around.
http://www.ozlabs.org/~dgibson
#31 lore raw

PATCH v4 10/10 dtc: Move first-reference handling out of the grammar

In reply to Simon Glass

In a plugin, the first top-level fragment can refer to a label or path instead of being the root node. The grammar action for this holds its merge logic, which makes an overlay fragment in a new tree.

Move it into a new merge_first_ref(), passing it a struct fragment for the reference and node. This is the last user of is_ref_relative() in the grammar, so make that function static.

The output does not change.

Assisted-by Claude:claude-opus-5-5

---

Changes in v4:
- Free the fragment after merging it

Changes in v2:
- Return true on success from merge_first_ref()
- Use dt_ref instead of fragment_ref

 dtc-parser.y | 14 ++++++--------
 dtc.h        |  2 +-
 livetree.c   | 30 +++++++++++++++++++++++++++++-
 3 files changed, 36 insertions(+), 10 deletions(-)
dtc-parser.y +6 -8
index 6f7ef24..0f254b5 100644
@@ -164,19 +164,17 @@ devicetree:
 		}
 	| dt_ref nodedef
 		{
+			struct fragment *f;
+
+			f = build_fragment_ref(NULL, $1, &@1, $2);
 			/*
 			 * We rely on the rule being always:
 			 *   versioninfo plugindecl memreserves devicetree
 			 * so $-1 is what we want (plugindecl)
 			 */
-			if (!($<flags>-1 & DTSF_PLUGIN))
-				ERROR(&@2, "Label or path %s not found", $1);
-			else if (is_ref_relative($1))
-				ERROR(&@2, "Label-relative reference %s not supported in plugin", $1);
-			$$ = add_orphan_node(
-					name_node(build_node(NULL, NULL, NULL),
-						  ""),
-					$2, $1);
+			if (!merge_first_ref(&$$, f, $<flags>-1 & DTSF_PLUGIN))
+				treesource_error = true;
+			free_fragment(f);
 		}
 	| devicetree DT_LABEL dt_ref nodedef
 		{
dtc.h +1 -1
index c5b8aee..f856edb 100644
@@ -351,7 +351,7 @@ struct fragment *build_fragment_delete_node(char *ref,
 struct fragment *build_fragment_omit_if_no_ref(char *ref,
 					       struct srcpos *ref_srcpos);
 void free_fragment(struct fragment *f);
-bool is_ref_relative(const char *ref);
+bool merge_first_ref(struct node **dtp, struct fragment *f, bool plugin);
 bool merge_ref(struct node *dt, struct fragment *f, bool plugin);
 bool merge_delete_node(struct node *dt, struct fragment *f);
 bool merge_omit_if_no_ref(struct node *dt, struct fragment *f);
livetree.c +29 -1
index cf6849a..4756946 100644
@@ -535,11 +535,39 @@ struct dt_info *build_dt_info(unsigned int dtsflags,
 }
 
 /* Check whether a reference is relative to a label, e.g. &{label/node} */
-bool is_ref_relative(const char *ref)
+static bool is_ref_relative(const char *ref)
 {
 	return ref[0] != '/' && strchr(&ref[1], '/');
 }
 
+/*
+ * Make the tree at *dtp from a first fragment which refers to a label or path,
+ * rather than being the root node. This is only valid in a plugin, where the
+ * fragment becomes an overlay fragment.
+ *
+ * Returns true on success, false on an error, which has been reported.
+ */
+bool merge_first_ref(struct node **dtp, struct fragment *f, bool plugin)
+{
+	bool ok = true;
+
+	if (!plugin) {
+		srcpos_error(f->node->srcpos, "Error",
+			     "Label or path %s not found", f->ref);
+		ok = false;
+	} else if (is_ref_relative(f->ref)) {
+		srcpos_error(f->node->srcpos, "Error",
+			     "Label-relative reference %s not supported in plugin",
+			     f->ref);
+		ok = false;
+	}
+	/* Make a new, empty tree with a fragment@N node for the reference */
+	*dtp = add_orphan_node(name_node(build_node(NULL, NULL, NULL), ""),
+			       f->node, f->ref);
+
+	return ok;
+}
+
 /*
  * Merge a fragment which refers to a label or path into the node it refers to,
  * adding any label in the fragment to that node.
Signature · 1 line
2.43.0
#32 lore raw

Re: [PATCH v4 10/10] dtc: Move first-reference handling out of the grammar

Quoted 4 lines · Simon Glass

In a plugin, the first top-level fragment can refer to a label or path instead of being the root node. The grammar action for this holds its merge logic, which makes an overlay fragment in a new tree.

Quoted 8 lines

Move it into a new merge_first_ref(), passing it a struct fragment for the reference and node. This is the last user of is_ref_relative() in the grammar, so make that function static.

The output does not change.

Assisted-by Claude:claude-opus-5-5

I'm still hoping we can unify the first source layer handling with other source layers a bit more neatly. But this is an ok interim step towards that.

Quoted 106 lines

---

Changes in v4:
- Free the fragment after merging it

Changes in v2:
- Return true on success from merge_first_ref()
- Use dt_ref instead of fragment_ref

 dtc-parser.y | 14 ++++++--------
 dtc.h        |  2 +-
 livetree.c   | 30 +++++++++++++++++++++++++++++-
 3 files changed, 36 insertions(+), 10 deletions(-)
dtc-parser.y +6 -8
index 6f7ef24..0f254b5 100644
@@ -164,19 +164,17 @@ devicetree:
 		}
 	| dt_ref nodedef
 		{
+			struct fragment *f;
+
+			f = build_fragment_ref(NULL, $1, &@1, $2);
 			/*
 			 * We rely on the rule being always:
 			 *   versioninfo plugindecl memreserves devicetree
 			 * so $-1 is what we want (plugindecl)
 			 */
-			if (!($<flags>-1 & DTSF_PLUGIN))
-				ERROR(&@2, "Label or path %s not found", $1);
-			else if (is_ref_relative($1))
-				ERROR(&@2, "Label-relative reference %s not supported in plugin", $1);
-			$$ = add_orphan_node(
-					name_node(build_node(NULL, NULL, NULL),
-						  ""),
-					$2, $1);
+			if (!merge_first_ref(&$$, f, $<flags>-1 & DTSF_PLUGIN))
+				treesource_error = true;
+			free_fragment(f);
 		}
 	| devicetree DT_LABEL dt_ref nodedef
 		{
dtc.h +1 -1
index c5b8aee..f856edb 100644
@@ -351,7 +351,7 @@ struct fragment *build_fragment_delete_node(char *ref,
 struct fragment *build_fragment_omit_if_no_ref(char *ref,
 					       struct srcpos *ref_srcpos);
 void free_fragment(struct fragment *f);
-bool is_ref_relative(const char *ref);
+bool merge_first_ref(struct node **dtp, struct fragment *f, bool plugin);
 bool merge_ref(struct node *dt, struct fragment *f, bool plugin);
 bool merge_delete_node(struct node *dt, struct fragment *f);
 bool merge_omit_if_no_ref(struct node *dt, struct fragment *f);
livetree.c +29 -1
index cf6849a..4756946 100644
@@ -535,11 +535,39 @@ struct dt_info *build_dt_info(unsigned int dtsflags,
 }
 
 /* Check whether a reference is relative to a label, e.g. &{label/node} */
-bool is_ref_relative(const char *ref)
+static bool is_ref_relative(const char *ref)
 {
 	return ref[0] != '/' && strchr(&ref[1], '/');
 }
 
+/*
+ * Make the tree at *dtp from a first fragment which refers to a label or path,
+ * rather than being the root node. This is only valid in a plugin, where the
+ * fragment becomes an overlay fragment.
+ *
+ * Returns true on success, false on an error, which has been reported.
+ */
+bool merge_first_ref(struct node **dtp, struct fragment *f, bool plugin)
+{
+	bool ok = true;
+
+	if (!plugin) {
+		srcpos_error(f->node->srcpos, "Error",
+			     "Label or path %s not found", f->ref);
+		ok = false;
+	} else if (is_ref_relative(f->ref)) {
+		srcpos_error(f->node->srcpos, "Error",
+			     "Label-relative reference %s not supported in plugin",
+			     f->ref);
+		ok = false;
+	}
+	/* Make a new, empty tree with a fragment@N node for the reference */
+	*dtp = add_orphan_node(name_node(build_node(NULL, NULL, NULL), ""),
+			       f->node, f->ref);
+
+	return ok;
+}
+
 /*
  * Merge a fragment which refers to a label or path into the node it refers to,
  * adding any label in the fragment to that node.
Signature · 1 line
2.43.0
Signature · 4 lines
David Gibson (he or they)	| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you, not the other way
				| around.
http://www.ozlabs.org/~dgibson
#33 lore raw

Re: [PATCH v4 10/10] dtc: Move first-reference handling out of the grammar

Hi David,

Quoted 10 lines · David Gibson
Quoted 8 lines · Simon Glass

In a plugin, the first top-level fragment can refer to a label or path instead of being the root node. The grammar action for this holds its merge logic, which makes an overlay fragment in a new tree.

Move it into a new merge_first_ref(), passing it a struct fragment for the reference and node. This is the last user of is_ref_relative() in the grammar, so make that function static.

Quoted 8 lines
Quoted 4 lines

The output does not change.

Assisted-by Claude:claude-opus-5-5

I'm still hoping we can unify the first source layer handling with other source layers a bit more neatly. But this is an ok interim step towards that.

Yes, will look at this for the next series, where the parser returns the list of source layers.

[..]

Regards,
Simon