* [PATCH v4 01/10] tests: Check how the fragments of a dts file are merged
2026-11-16 23:57 [PATCH v4 00/10] dtc: Move the merging of fragments out of the grammar Simon Glass
@ 2026-11-16 23:57 ` Simon Glass
2026-11-25 0:34 ` David Gibson
2026-11-16 23:57 ` [PATCH v4 02/10] dtc: Add a struct for the top-level fragments of a dts file Simon Glass
` (8 subsequent siblings)
9 siblings, 1 reply; 22+ messages in thread
From: Simon Glass @ 2026-11-16 23:57 UTC (permalink / raw)
To: Devicetree Compiler; +Cc: David Gibson, Herve Codina, 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.
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Simon Glass <sjg@chromium.org>
---
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
diff --git a/tests/merge-delete-missing.dts b/tests/merge-delete-missing.dts
new file mode 100644
index 0000000..b36e39e
--- /dev/null
+++ b/tests/merge-delete-missing.dts
@@ -0,0 +1,9 @@
+/dts-v1/;
+
+/ {
+ label: node {
+ };
+};
+
+/* Try to delete a node using a non-existent label */
+/delete-node/ &nosuchnode;
diff --git a/tests/merge-errors.dts b/tests/merge-errors.dts
new file mode 100644
index 0000000..f828e86
--- /dev/null
+++ b/tests/merge-errors.dts
@@ -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;
diff --git a/tests/merge-first-ref-relative.dts b/tests/merge-first-ref-relative.dts
new file mode 100644
index 0000000..fe4cf9b
--- /dev/null
+++ b/tests/merge-first-ref-relative.dts
@@ -0,0 +1,7 @@
+/dts-v1/;
+/plugin/;
+
+/* A plugin cannot define a node using a label-relative reference */
+&{label/node} {
+ prop;
+};
diff --git a/tests/merge-first-ref.dts b/tests/merge-first-ref.dts
new file mode 100644
index 0000000..2f585d6
--- /dev/null
+++ b/tests/merge-first-ref.dts
@@ -0,0 +1,6 @@
+/dts-v1/;
+
+/* Only a plugin can start with a reference */
+&nosuchnode {
+ prop;
+};
diff --git a/tests/merge-label-base.dts b/tests/merge-label-base.dts
new file mode 100644
index 0000000..1a0a8bb
--- /dev/null
+++ b/tests/merge-label-base.dts
@@ -0,0 +1,10 @@
+/dts-v1/;
+/plugin/;
+
+/ {
+};
+
+/* A label which is not in the plugin may be in the base tree */
+&{baselabel} {
+ prop;
+};
diff --git a/tests/merge-labelled-plugin.dts b/tests/merge-labelled-plugin.dts
new file mode 100644
index 0000000..3849cc5
--- /dev/null
+++ b/tests/merge-labelled-plugin.dts
@@ -0,0 +1,12 @@
+/dts-v1/;
+/plugin/;
+
+/ {
+ label: node {
+ };
+};
+
+/* Add a label to a node defined in the plugin itself */
+newlabel: &label {
+ prop = "value";
+};
diff --git a/tests/merge-labelled-relative.dts b/tests/merge-labelled-relative.dts
new file mode 100644
index 0000000..8faaf5e
--- /dev/null
+++ b/tests/merge-labelled-relative.dts
@@ -0,0 +1,10 @@
+/dts-v1/;
+/plugin/;
+
+/ {
+};
+
+/* A label outside the plugin cannot be used in a label-relative reference */
+newlabel: &{baselabel/subnode} {
+ prop;
+};
diff --git a/tests/merge-omit-missing.dts b/tests/merge-omit-missing.dts
new file mode 100644
index 0000000..0357648
--- /dev/null
+++ b/tests/merge-omit-missing.dts
@@ -0,0 +1,9 @@
+/dts-v1/;
+
+/ {
+ label: node {
+ };
+};
+
+/* Try to mark a node using a non-existent path */
+/omit-if-no-ref/ &{/nosuchnode};
diff --git a/tests/merge-path-ref-missing.dts b/tests/merge-path-ref-missing.dts
new file mode 100644
index 0000000..ad2e35e
--- /dev/null
+++ b/tests/merge-path-ref-missing.dts
@@ -0,0 +1,11 @@
+/dts-v1/;
+
+/ {
+ node {
+ };
+};
+
+/* Try to redefine a node using a non-existent path */
+&{/nosuchnode} {
+ prop;
+};
diff --git a/tests/merge-path-relative.dts b/tests/merge-path-relative.dts
new file mode 100644
index 0000000..0a79379
--- /dev/null
+++ b/tests/merge-path-relative.dts
@@ -0,0 +1,10 @@
+/dts-v1/;
+/plugin/;
+
+/ {
+};
+
+/* A label outside the plugin cannot be used in a label-relative reference */
+&{baselabel/node} {
+ prop;
+};
diff --git a/tests/merge-root-plugin.dts b/tests/merge-root-plugin.dts
new file mode 100644
index 0000000..f67355e
--- /dev/null
+++ b/tests/merge-root-plugin.dts
@@ -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;
+};
diff --git a/tests/run_tests.sh b/tests/run_tests.sh
index 5775e0c..146d288 100755
--- a/tests/run_tests.sh
+++ b/tests/run_tests.sh
@@ -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
--
2.43.0
^ permalink raw reply related [flat|nested] 22+ messages in thread* Re: [PATCH v4 01/10] tests: Check how the fragments of a dts file are merged
2026-11-16 23:57 ` [PATCH v4 01/10] tests: Check how the fragments of a dts file are merged Simon Glass
@ 2026-11-25 0:34 ` David Gibson
2026-11-25 6:42 ` David Gibson
0 siblings, 1 reply; 22+ messages in thread
From: David Gibson @ 2026-11-25 0:34 UTC (permalink / raw)
To: Simon Glass; +Cc: Devicetree Compiler, Herve Codina
[-- Attachment #1: Type: text/plain, Size: 12015 bytes --]
On Mon, Nov 16, 2026 at 04:57:55PM -0700, Simon Glass wrote:
> 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
> Signed-off-by: Simon Glass <sjg@chromium.org>
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.
> ---
>
> 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
>
> diff --git a/tests/merge-delete-missing.dts b/tests/merge-delete-missing.dts
> new file mode 100644
> index 0000000..b36e39e
> --- /dev/null
> +++ b/tests/merge-delete-missing.dts
> @@ -0,0 +1,9 @@
> +/dts-v1/;
> +
> +/ {
> + label: node {
> + };
> +};
> +
> +/* Try to delete a node using a non-existent label */
> +/delete-node/ &nosuchnode;
> diff --git a/tests/merge-errors.dts b/tests/merge-errors.dts
> new file mode 100644
> index 0000000..f828e86
> --- /dev/null
> +++ b/tests/merge-errors.dts
> @@ -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;
> diff --git a/tests/merge-first-ref-relative.dts b/tests/merge-first-ref-relative.dts
> new file mode 100644
> index 0000000..fe4cf9b
> --- /dev/null
> +++ b/tests/merge-first-ref-relative.dts
> @@ -0,0 +1,7 @@
> +/dts-v1/;
> +/plugin/;
> +
> +/* A plugin cannot define a node using a label-relative reference */
> +&{label/node} {
> + prop;
> +};
> diff --git a/tests/merge-first-ref.dts b/tests/merge-first-ref.dts
> new file mode 100644
> index 0000000..2f585d6
> --- /dev/null
> +++ b/tests/merge-first-ref.dts
> @@ -0,0 +1,6 @@
> +/dts-v1/;
> +
> +/* Only a plugin can start with a reference */
> +&nosuchnode {
> + prop;
> +};
> diff --git a/tests/merge-label-base.dts b/tests/merge-label-base.dts
> new file mode 100644
> index 0000000..1a0a8bb
> --- /dev/null
> +++ b/tests/merge-label-base.dts
> @@ -0,0 +1,10 @@
> +/dts-v1/;
> +/plugin/;
> +
> +/ {
> +};
> +
> +/* A label which is not in the plugin may be in the base tree */
> +&{baselabel} {
> + prop;
> +};
> diff --git a/tests/merge-labelled-plugin.dts b/tests/merge-labelled-plugin.dts
> new file mode 100644
> index 0000000..3849cc5
> --- /dev/null
> +++ b/tests/merge-labelled-plugin.dts
> @@ -0,0 +1,12 @@
> +/dts-v1/;
> +/plugin/;
> +
> +/ {
> + label: node {
> + };
> +};
> +
> +/* Add a label to a node defined in the plugin itself */
> +newlabel: &label {
> + prop = "value";
> +};
> diff --git a/tests/merge-labelled-relative.dts b/tests/merge-labelled-relative.dts
> new file mode 100644
> index 0000000..8faaf5e
> --- /dev/null
> +++ b/tests/merge-labelled-relative.dts
> @@ -0,0 +1,10 @@
> +/dts-v1/;
> +/plugin/;
> +
> +/ {
> +};
> +
> +/* A label outside the plugin cannot be used in a label-relative reference */
> +newlabel: &{baselabel/subnode} {
> + prop;
> +};
> diff --git a/tests/merge-omit-missing.dts b/tests/merge-omit-missing.dts
> new file mode 100644
> index 0000000..0357648
> --- /dev/null
> +++ b/tests/merge-omit-missing.dts
> @@ -0,0 +1,9 @@
> +/dts-v1/;
> +
> +/ {
> + label: node {
> + };
> +};
> +
> +/* Try to mark a node using a non-existent path */
> +/omit-if-no-ref/ &{/nosuchnode};
> diff --git a/tests/merge-path-ref-missing.dts b/tests/merge-path-ref-missing.dts
> new file mode 100644
> index 0000000..ad2e35e
> --- /dev/null
> +++ b/tests/merge-path-ref-missing.dts
> @@ -0,0 +1,11 @@
> +/dts-v1/;
> +
> +/ {
> + node {
> + };
> +};
> +
> +/* Try to redefine a node using a non-existent path */
> +&{/nosuchnode} {
> + prop;
> +};
> diff --git a/tests/merge-path-relative.dts b/tests/merge-path-relative.dts
> new file mode 100644
> index 0000000..0a79379
> --- /dev/null
> +++ b/tests/merge-path-relative.dts
> @@ -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.
> + prop;
> +};
> diff --git a/tests/merge-root-plugin.dts b/tests/merge-root-plugin.dts
> new file mode 100644
> index 0000000..f67355e
> --- /dev/null
> +++ b/tests/merge-root-plugin.dts
> @@ -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;
> +};
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.
> diff --git a/tests/run_tests.sh b/tests/run_tests.sh
> index 5775e0c..146d288 100755
> --- a/tests/run_tests.sh
> +++ b/tests/run_tests.sh
> @@ -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
> --
> 2.43.0
>
>
--
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
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 22+ messages in thread* Re: [PATCH v4 01/10] tests: Check how the fragments of a dts file are merged
2026-11-25 0:34 ` David Gibson
@ 2026-11-25 6:42 ` David Gibson
0 siblings, 0 replies; 22+ messages in thread
From: David Gibson @ 2026-11-25 6:42 UTC (permalink / raw)
To: Simon Glass; +Cc: Devicetree Compiler, Herve Codina
[-- Attachment #1: Type: text/plain, Size: 1630 bytes --]
On Wed, Nov 25, 2026 at 11:34:30AM +1100, David Gibson wrote:
> On Mon, Nov 16, 2026 at 04:57:55PM -0700, Simon Glass wrote:
> > 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
> > Signed-off-by: Simon Glass <sjg@chromium.org>
>
> 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'.
--
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
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH v4 02/10] dtc: Add a struct for the top-level fragments of a dts file
2026-11-16 23:57 [PATCH v4 00/10] dtc: Move the merging of fragments out of the grammar Simon Glass
2026-11-16 23:57 ` [PATCH v4 01/10] tests: Check how the fragments of a dts file are merged Simon Glass
@ 2026-11-16 23:57 ` Simon Glass
2026-11-25 0:44 ` David Gibson
2026-11-16 23:57 ` [PATCH v4 03/10] dtc: Move the /delete-node/ handling out of the grammar Simon Glass
` (7 subsequent siblings)
9 siblings, 1 reply; 22+ messages in thread
From: Simon Glass @ 2026-11-16 23:57 UTC (permalink / raw)
To: Devicetree Compiler; +Cc: David Gibson, Herve Codina, 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
Signed-off-by: Simon Glass <sjg@chromium.org>
---
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(+)
diff --git a/dtc.h b/dtc.h
index 791f815..ac18616 100644
--- a/dtc.h
+++ b/dtc.h
@@ -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;
diff --git a/livetree.c b/livetree.c
index abe73de..4231c64 100644
--- a/livetree.c
+++ b/livetree.c
@@ -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;
--
2.43.0
^ permalink raw reply related [flat|nested] 22+ messages in thread* Re: [PATCH v4 02/10] dtc: Add a struct for the top-level fragments of a dts file
2026-11-16 23:57 ` [PATCH v4 02/10] dtc: Add a struct for the top-level fragments of a dts file Simon Glass
@ 2026-11-25 0:44 ` David Gibson
0 siblings, 0 replies; 22+ messages in thread
From: David Gibson @ 2026-11-25 0:44 UTC (permalink / raw)
To: Simon Glass; +Cc: Devicetree Compiler, Herve Codina
[-- Attachment #1: Type: text/plain, Size: 5769 bytes --]
On Mon, Nov 16, 2026 at 04:57:56PM -0700, Simon Glass wrote:
> 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.
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.
>
> Assisted-by: Claude:claude-opus-5-5
> Signed-off-by: Simon Glass <sjg@chromium.org>
Apart from the suggested name change,
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
> ---
>
> 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(+)
>
> diff --git a/dtc.h b/dtc.h
> index 791f815..ac18616 100644
> --- a/dtc.h
> +++ b/dtc.h
> @@ -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;
> diff --git a/livetree.c b/livetree.c
> index abe73de..4231c64 100644
> --- a/livetree.c
> +++ b/livetree.c
> @@ -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;
> --
> 2.43.0
>
>
--
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
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH v4 03/10] dtc: Move the /delete-node/ handling out of the grammar
2026-11-16 23:57 [PATCH v4 00/10] dtc: Move the merging of fragments out of the grammar Simon Glass
2026-11-16 23:57 ` [PATCH v4 01/10] tests: Check how the fragments of a dts file are merged Simon Glass
2026-11-16 23:57 ` [PATCH v4 02/10] dtc: Add a struct for the top-level fragments of a dts file Simon Glass
@ 2026-11-16 23:57 ` Simon Glass
2026-11-25 0:48 ` David Gibson
2026-11-16 23:57 ` [PATCH v4 04/10] dtc: Move the /omit-if-no-ref/ " Simon Glass
` (6 subsequent siblings)
9 siblings, 1 reply; 22+ messages in thread
From: Simon Glass @ 2026-11-16 23:57 UTC (permalink / raw)
To: Devicetree Compiler; +Cc: David Gibson, Herve Codina, 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
Signed-off-by: Simon Glass <sjg@chromium.org>
---
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(-)
diff --git a/dtc-parser.y b/dtc-parser.y
index 45a699a..6bf96a7 100644
--- a/dtc-parser.y
+++ b/dtc-parser.y
@@ -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 ';'
diff --git a/dtc.h b/dtc.h
index ac18616..41ba438 100644
--- a/dtc.h
+++ b/dtc.h
@@ -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;
diff --git a/livetree.c b/livetree.c
index 4231c64..ce47fe2 100644
--- a/livetree.c
+++ b/livetree.c
@@ -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
*/
--
2.43.0
^ permalink raw reply related [flat|nested] 22+ messages in thread* Re: [PATCH v4 03/10] dtc: Move the /delete-node/ handling out of the grammar
2026-11-16 23:57 ` [PATCH v4 03/10] dtc: Move the /delete-node/ handling out of the grammar Simon Glass
@ 2026-11-25 0:48 ` David Gibson
0 siblings, 0 replies; 22+ messages in thread
From: David Gibson @ 2026-11-25 0:48 UTC (permalink / raw)
To: Simon Glass; +Cc: Devicetree Compiler, Herve Codina
[-- Attachment #1: Type: text/plain, Size: 3211 bytes --]
On Mon, Nov 16, 2026 at 04:57:57PM -0700, Simon Glass wrote:
> 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
> Signed-off-by: Simon Glass <sjg@chromium.org>
Apart from the name change suggested earlier:
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
> ---
>
> 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(-)
>
> diff --git a/dtc-parser.y b/dtc-parser.y
> index 45a699a..6bf96a7 100644
> --- a/dtc-parser.y
> +++ b/dtc-parser.y
> @@ -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 ';'
> diff --git a/dtc.h b/dtc.h
> index ac18616..41ba438 100644
> --- a/dtc.h
> +++ b/dtc.h
> @@ -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;
> diff --git a/livetree.c b/livetree.c
> index 4231c64..ce47fe2 100644
> --- a/livetree.c
> +++ b/livetree.c
> @@ -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
> */
> --
> 2.43.0
>
>
--
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
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH v4 04/10] dtc: Move the /omit-if-no-ref/ handling out of the grammar
2026-11-16 23:57 [PATCH v4 00/10] dtc: Move the merging of fragments out of the grammar Simon Glass
` (2 preceding siblings ...)
2026-11-16 23:57 ` [PATCH v4 03/10] dtc: Move the /delete-node/ handling out of the grammar Simon Glass
@ 2026-11-16 23:57 ` Simon Glass
2026-11-25 1:15 ` David Gibson
2026-11-16 23:57 ` [PATCH v4 05/10] dtc: Move is_ref_relative() to livetree.c Simon Glass
` (5 subsequent siblings)
9 siblings, 1 reply; 22+ messages in thread
From: Simon Glass @ 2026-11-16 23:57 UTC (permalink / raw)
To: Devicetree Compiler; +Cc: David Gibson, Herve Codina, 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
Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
---
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(-)
diff --git a/dtc-parser.y b/dtc-parser.y
index 6bf96a7..1a1a52d 100644
--- a/dtc-parser.y
+++ b/dtc-parser.y
@@ -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;
}
;
diff --git a/dtc.h b/dtc.h
index 41ba438..b9cb5f9 100644
--- a/dtc.h
+++ b/dtc.h
@@ -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;
diff --git a/livetree.c b/livetree.c
index ce47fe2..669d8ba 100644
--- a/livetree.c
+++ b/livetree.c
@@ -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
*/
--
2.43.0
^ permalink raw reply related [flat|nested] 22+ messages in thread* Re: [PATCH v4 04/10] dtc: Move the /omit-if-no-ref/ handling out of the grammar
2026-11-16 23:57 ` [PATCH v4 04/10] dtc: Move the /omit-if-no-ref/ " Simon Glass
@ 2026-11-25 1:15 ` David Gibson
0 siblings, 0 replies; 22+ messages in thread
From: David Gibson @ 2026-11-25 1:15 UTC (permalink / raw)
To: Simon Glass; +Cc: Devicetree Compiler, Herve Codina
[-- Attachment #1: Type: text/plain, Size: 3114 bytes --]
On Mon, Nov 16, 2026 at 04:57:58PM -0700, Simon Glass wrote:
> 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
> Signed-off-by: Simon Glass <sjg@chromium.org>
> Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
Apart from the name change suggested elsewhere,
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
> ---
>
> 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(-)
>
> diff --git a/dtc-parser.y b/dtc-parser.y
> index 6bf96a7..1a1a52d 100644
> --- a/dtc-parser.y
> +++ b/dtc-parser.y
> @@ -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;
> }
> ;
> diff --git a/dtc.h b/dtc.h
> index 41ba438..b9cb5f9 100644
> --- a/dtc.h
> +++ b/dtc.h
> @@ -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;
> diff --git a/livetree.c b/livetree.c
> index ce47fe2..669d8ba 100644
> --- a/livetree.c
> +++ b/livetree.c
> @@ -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
> */
> --
> 2.43.0
>
>
--
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
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH v4 05/10] dtc: Move is_ref_relative() to livetree.c
2026-11-16 23:57 [PATCH v4 00/10] dtc: Move the merging of fragments out of the grammar Simon Glass
` (3 preceding siblings ...)
2026-11-16 23:57 ` [PATCH v4 04/10] dtc: Move the /omit-if-no-ref/ " Simon Glass
@ 2026-11-16 23:57 ` Simon Glass
2026-11-25 1:16 ` David Gibson
2026-11-16 23:58 ` [PATCH v4 06/10] dtc: Move label-reference handling out of the grammar Simon Glass
` (4 subsequent siblings)
9 siblings, 1 reply; 22+ messages in thread
From: Simon Glass @ 2026-11-16 23:57 UTC (permalink / raw)
To: Devicetree Compiler; +Cc: David Gibson, Herve Codina, 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
Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
---
(no changes since v1)
dtc-parser.y | 5 -----
dtc.h | 1 +
livetree.c | 6 ++++++
3 files changed, 7 insertions(+), 5 deletions(-)
diff --git a/dtc-parser.y b/dtc-parser.y
index 1a1a52d..9e8091a 100644
--- a/dtc-parser.y
+++ b/dtc-parser.y
@@ -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 {
diff --git a/dtc.h b/dtc.h
index b9cb5f9..b236229 100644
--- a/dtc.h
+++ b/dtc.h
@@ -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);
diff --git a/livetree.c b/livetree.c
index 669d8ba..388a2de 100644
--- a/livetree.c
+++ b/livetree.c
@@ -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
*
--
2.43.0
^ permalink raw reply related [flat|nested] 22+ messages in thread* Re: [PATCH v4 05/10] dtc: Move is_ref_relative() to livetree.c
2026-11-16 23:57 ` [PATCH v4 05/10] dtc: Move is_ref_relative() to livetree.c Simon Glass
@ 2026-11-25 1:16 ` David Gibson
0 siblings, 0 replies; 22+ messages in thread
From: David Gibson @ 2026-11-25 1:16 UTC (permalink / raw)
To: Simon Glass; +Cc: Devicetree Compiler, Herve Codina
[-- Attachment #1: Type: text/plain, Size: 711 bytes --]
On Mon, Nov 16, 2026 at 04:57:59PM -0700, Simon Glass wrote:
> 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
> Signed-off-by: Simon Glass <sjg@chromium.org>
> Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
I already merged this, you need to rebase your series.
--
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
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH v4 06/10] dtc: Move label-reference handling out of the grammar
2026-11-16 23:57 [PATCH v4 00/10] dtc: Move the merging of fragments out of the grammar Simon Glass
` (4 preceding siblings ...)
2026-11-16 23:57 ` [PATCH v4 05/10] dtc: Move is_ref_relative() to livetree.c Simon Glass
@ 2026-11-16 23:58 ` Simon Glass
2026-11-25 6:12 ` David Gibson
2026-11-16 23:58 ` [PATCH v4 07/10] dtc: Merge &{label} in a plugin as with &label Simon Glass
` (3 subsequent siblings)
9 siblings, 1 reply; 22+ messages in thread
From: Simon Glass @ 2026-11-16 23:58 UTC (permalink / raw)
To: Devicetree Compiler; +Cc: David Gibson, Herve Codina, 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
Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
---
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(-)
diff --git a/dtc-parser.y b/dtc-parser.y
index 9e8091a..2d4dcb6 100644
--- a/dtc-parser.y
+++ b/dtc-parser.y
@@ -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 ';'
diff --git a/dtc.h b/dtc.h
index b236229..c5b8aee 100644
--- a/dtc.h
+++ b/dtc.h
@@ -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);
diff --git a/livetree.c b/livetree.c
index 388a2de..2ed57ae 100644
--- a/livetree.c
+++ b/livetree.c
@@ -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
*
--
2.43.0
^ permalink raw reply related [flat|nested] 22+ messages in thread* Re: [PATCH v4 06/10] dtc: Move label-reference handling out of the grammar
2026-11-16 23:58 ` [PATCH v4 06/10] dtc: Move label-reference handling out of the grammar Simon Glass
@ 2026-11-25 6:12 ` David Gibson
0 siblings, 0 replies; 22+ messages in thread
From: David Gibson @ 2026-11-25 6:12 UTC (permalink / raw)
To: Simon Glass; +Cc: Devicetree Compiler, Herve Codina
[-- Attachment #1: Type: text/plain, Size: 762 bytes --]
On Mon, Nov 16, 2026 at 04:58:00PM -0700, Simon Glass wrote:
> 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
> Signed-off-by: Simon Glass <sjg@chromium.org>
> Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
Still, excepting the name change suggested elsewhere.
--
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
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH v4 07/10] dtc: Merge &{label} in a plugin as with &label
2026-11-16 23:57 [PATCH v4 00/10] dtc: Move the merging of fragments out of the grammar Simon Glass
` (5 preceding siblings ...)
2026-11-16 23:58 ` [PATCH v4 06/10] dtc: Move label-reference handling out of the grammar Simon Glass
@ 2026-11-16 23:58 ` Simon Glass
2026-11-25 6:16 ` David Gibson
2026-11-16 23:58 ` [PATCH v4 08/10] dtc: Move path-reference handling out of the grammar Simon Glass
` (2 subsequent siblings)
9 siblings, 1 reply; 22+ messages in thread
From: Simon Glass @ 2026-11-16 23:58 UTC (permalink / raw)
To: Devicetree Compiler; +Cc: David Gibson, Herve Codina, 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
Signed-off-by: Simon Glass <sjg@chromium.org>
---
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
diff --git a/dtc-parser.y b/dtc-parser.y
index 2d4dcb6..7e1e6c7 100644
--- a/dtc-parser.y
+++ b/dtc-parser.y
@@ -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
diff --git a/tests/merge-label-braces.dts b/tests/merge-label-braces.dts
new file mode 100644
index 0000000..569e7f8
--- /dev/null
+++ b/tests/merge-label-braces.dts
@@ -0,0 +1,12 @@
+/dts-v1/;
+/plugin/;
+
+/ {
+ label: node {
+ };
+};
+
+/* A label in the plugin itself is merged, however it is written */
+&{label} {
+ prop;
+};
diff --git a/tests/merge-label-order.dts b/tests/merge-label-order.dts
new file mode 100644
index 0000000..40fa668
--- /dev/null
+++ b/tests/merge-label-order.dts
@@ -0,0 +1,17 @@
+/dts-v1/;
+/plugin/;
+
+&basenode {
+ label: foo {
+ p = "first";
+ };
+};
+
+/* The last definition wins */
+&{label} {
+ p = "second";
+};
+
+&label {
+ p = "third";
+};
diff --git a/tests/run_tests.sh b/tests/run_tests.sh
index 146d288..7f9a24a 100755
--- a/tests/run_tests.sh
+++ b/tests/run_tests.sh
@@ -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"
--
2.43.0
^ permalink raw reply related [flat|nested] 22+ messages in thread* Re: [PATCH v4 07/10] dtc: Merge &{label} in a plugin as with &label
2026-11-16 23:58 ` [PATCH v4 07/10] dtc: Merge &{label} in a plugin as with &label Simon Glass
@ 2026-11-25 6:16 ` David Gibson
0 siblings, 0 replies; 22+ messages in thread
From: David Gibson @ 2026-11-25 6:16 UTC (permalink / raw)
To: Simon Glass; +Cc: Devicetree Compiler, Herve Codina
[-- Attachment #1: Type: text/plain, Size: 5215 bytes --]
On Mon, Nov 16, 2026 at 04:58:01PM -0700, Simon Glass wrote:
> 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
> Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
Excepting the name change.
> ---
>
> 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
>
> diff --git a/dtc-parser.y b/dtc-parser.y
> index 2d4dcb6..7e1e6c7 100644
> --- a/dtc-parser.y
> +++ b/dtc-parser.y
> @@ -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
> diff --git a/tests/merge-label-braces.dts b/tests/merge-label-braces.dts
> new file mode 100644
> index 0000000..569e7f8
> --- /dev/null
> +++ b/tests/merge-label-braces.dts
> @@ -0,0 +1,12 @@
> +/dts-v1/;
> +/plugin/;
> +
> +/ {
> + label: node {
> + };
> +};
> +
> +/* A label in the plugin itself is merged, however it is written */
> +&{label} {
> + prop;
> +};
> diff --git a/tests/merge-label-order.dts b/tests/merge-label-order.dts
> new file mode 100644
> index 0000000..40fa668
> --- /dev/null
> +++ b/tests/merge-label-order.dts
> @@ -0,0 +1,17 @@
> +/dts-v1/;
> +/plugin/;
> +
> +&basenode {
> + label: foo {
> + p = "first";
> + };
> +};
> +
> +/* The last definition wins */
> +&{label} {
> + p = "second";
> +};
> +
> +&label {
> + p = "third";
> +};
> diff --git a/tests/run_tests.sh b/tests/run_tests.sh
> index 146d288..7f9a24a 100755
> --- a/tests/run_tests.sh
> +++ b/tests/run_tests.sh
> @@ -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"
> --
> 2.43.0
>
>
--
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
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH v4 08/10] dtc: Move path-reference handling out of the grammar
2026-11-16 23:57 [PATCH v4 00/10] dtc: Move the merging of fragments out of the grammar Simon Glass
` (6 preceding siblings ...)
2026-11-16 23:58 ` [PATCH v4 07/10] dtc: Merge &{label} in a plugin as with &label Simon Glass
@ 2026-11-16 23:58 ` Simon Glass
2026-11-25 6:18 ` David Gibson
2026-11-16 23:58 ` [PATCH v4 09/10] dtc: Move labelled-reference " Simon Glass
2026-11-16 23:58 ` [PATCH v4 10/10] dtc: Move first-reference " Simon Glass
9 siblings, 1 reply; 22+ messages in thread
From: Simon Glass @ 2026-11-16 23:58 UTC (permalink / raw)
To: Devicetree Compiler; +Cc: David Gibson, Herve Codina, 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
Signed-off-by: Simon Glass <sjg@chromium.org>
---
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(-)
diff --git a/dtc-parser.y b/dtc-parser.y
index 7e1e6c7..c85b77d 100644
--- a/dtc-parser.y
+++ b/dtc-parser.y
@@ -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;
diff --git a/livetree.c b/livetree.c
index 2ed57ae..290467f 100644
--- a/livetree.c
+++ b/livetree.c
@@ -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;
}
/*
--
2.43.0
^ permalink raw reply related [flat|nested] 22+ messages in thread* Re: [PATCH v4 08/10] dtc: Move path-reference handling out of the grammar
2026-11-16 23:58 ` [PATCH v4 08/10] dtc: Move path-reference handling out of the grammar Simon Glass
@ 2026-11-25 6:18 ` David Gibson
0 siblings, 0 replies; 22+ messages in thread
From: David Gibson @ 2026-11-25 6:18 UTC (permalink / raw)
To: Simon Glass; +Cc: Devicetree Compiler, Herve Codina
[-- Attachment #1: Type: text/plain, Size: 4035 bytes --]
On Mon, Nov 16, 2026 at 04:58:02PM -0700, Simon Glass wrote:
> 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
> Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
Excepting the name change.
> ---
>
> 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(-)
>
> diff --git a/dtc-parser.y b/dtc-parser.y
> index 7e1e6c7..c85b77d 100644
> --- a/dtc-parser.y
> +++ b/dtc-parser.y
> @@ -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;
>
> diff --git a/livetree.c b/livetree.c
> index 2ed57ae..290467f 100644
> --- a/livetree.c
> +++ b/livetree.c
> @@ -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;
> }
>
> /*
> --
> 2.43.0
>
>
--
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
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH v4 09/10] dtc: Move labelled-reference handling out of the grammar
2026-11-16 23:57 [PATCH v4 00/10] dtc: Move the merging of fragments out of the grammar Simon Glass
` (7 preceding siblings ...)
2026-11-16 23:58 ` [PATCH v4 08/10] dtc: Move path-reference handling out of the grammar Simon Glass
@ 2026-11-16 23:58 ` Simon Glass
2026-11-25 6:21 ` David Gibson
2026-11-16 23:58 ` [PATCH v4 10/10] dtc: Move first-reference " Simon Glass
9 siblings, 1 reply; 22+ messages in thread
From: Simon Glass @ 2026-11-16 23:58 UTC (permalink / raw)
To: Devicetree Compiler; +Cc: David Gibson, Herve Codina, 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
Signed-off-by: Simon Glass <sjg@chromium.org>
---
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(-)
diff --git a/dtc-parser.y b/dtc-parser.y
index c85b77d..6f7ef24 100644
--- a/dtc-parser.y
+++ b/dtc-parser.y
@@ -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
diff --git a/livetree.c b/livetree.c
index 290467f..cf6849a 100644
--- a/livetree.c
+++ b/livetree.c
@@ -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;
diff --git a/tests/run_tests.sh b/tests/run_tests.sh
index 7f9a24a..62732a4 100755
--- a/tests/run_tests.sh
+++ b/tests/run_tests.sh
@@ -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"
--
2.43.0
^ permalink raw reply related [flat|nested] 22+ messages in thread* Re: [PATCH v4 09/10] dtc: Move labelled-reference handling out of the grammar
2026-11-16 23:58 ` [PATCH v4 09/10] dtc: Move labelled-reference " Simon Glass
@ 2026-11-25 6:21 ` David Gibson
0 siblings, 0 replies; 22+ messages in thread
From: David Gibson @ 2026-11-25 6:21 UTC (permalink / raw)
To: Simon Glass; +Cc: Devicetree Compiler, Herve Codina
[-- Attachment #1: Type: text/plain, Size: 4866 bytes --]
On Mon, Nov 16, 2026 at 04:58:03PM -0700, Simon Glass wrote:
> 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
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
>
> 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(-)
>
> diff --git a/dtc-parser.y b/dtc-parser.y
> index c85b77d..6f7ef24 100644
> --- a/dtc-parser.y
> +++ b/dtc-parser.y
> @@ -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
> diff --git a/livetree.c b/livetree.c
> index 290467f..cf6849a 100644
> --- a/livetree.c
> +++ b/livetree.c
> @@ -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.
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.
> *
> * 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;
> diff --git a/tests/run_tests.sh b/tests/run_tests.sh
> index 7f9a24a..62732a4 100755
> --- a/tests/run_tests.sh
> +++ b/tests/run_tests.sh
> @@ -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"
> --
> 2.43.0
>
>
--
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
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH v4 10/10] dtc: Move first-reference handling out of the grammar
2026-11-16 23:57 [PATCH v4 00/10] dtc: Move the merging of fragments out of the grammar Simon Glass
` (8 preceding siblings ...)
2026-11-16 23:58 ` [PATCH v4 09/10] dtc: Move labelled-reference " Simon Glass
@ 2026-11-16 23:58 ` Simon Glass
2026-11-25 6:24 ` David Gibson
9 siblings, 1 reply; 22+ messages in thread
From: Simon Glass @ 2026-11-16 23:58 UTC (permalink / raw)
To: Devicetree Compiler; +Cc: David Gibson, Herve Codina, 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
Signed-off-by: Simon Glass <sjg@chromium.org>
---
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(-)
diff --git a/dtc-parser.y b/dtc-parser.y
index 6f7ef24..0f254b5 100644
--- a/dtc-parser.y
+++ b/dtc-parser.y
@@ -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
{
diff --git a/dtc.h b/dtc.h
index c5b8aee..f856edb 100644
--- a/dtc.h
+++ b/dtc.h
@@ -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);
diff --git a/livetree.c b/livetree.c
index cf6849a..4756946 100644
--- a/livetree.c
+++ b/livetree.c
@@ -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.
--
2.43.0
^ permalink raw reply related [flat|nested] 22+ messages in thread* Re: [PATCH v4 10/10] dtc: Move first-reference handling out of the grammar
2026-11-16 23:58 ` [PATCH v4 10/10] dtc: Move first-reference " Simon Glass
@ 2026-11-25 6:24 ` David Gibson
0 siblings, 0 replies; 22+ messages in thread
From: David Gibson @ 2026-11-25 6:24 UTC (permalink / raw)
To: Simon Glass; +Cc: Devicetree Compiler, Herve Codina
[-- Attachment #1: Type: text/plain, Size: 4482 bytes --]
On Mon, Nov 16, 2026 at 04:58:04PM -0700, Simon Glass wrote:
> 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
> Signed-off-by: Simon Glass <sjg@chromium.org>
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.
> ---
>
> 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(-)
>
> diff --git a/dtc-parser.y b/dtc-parser.y
> index 6f7ef24..0f254b5 100644
> --- a/dtc-parser.y
> +++ b/dtc-parser.y
> @@ -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
> {
> diff --git a/dtc.h b/dtc.h
> index c5b8aee..f856edb 100644
> --- a/dtc.h
> +++ b/dtc.h
> @@ -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);
> diff --git a/livetree.c b/livetree.c
> index cf6849a..4756946 100644
> --- a/livetree.c
> +++ b/livetree.c
> @@ -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.
> --
> 2.43.0
>
>
--
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
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 22+ messages in thread