Devicetree Compiler
 help / color / mirror / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: Simon Glass <sjg@chromium.org>
Cc: Devicetree Compiler <devicetree-compiler@vger.kernel.org>,
	Herve Codina <herve.codina@bootlin.com>
Subject: Re: [PATCH v3 13/18] dtc: Add helpers for the errors when merging a fragment
Date: Mon, 16 Nov 2026 14:49:43 +1100	[thread overview]
Message-ID: <avp9VgVC94lznIeY@gractus.seuss> (raw)
In-Reply-To: <20261106195159.2623314-14-sjg@chromium.org>

[-- Attachment #1: Type: text/plain, Size: 5731 bytes --]

On Fri, Nov 06, 2026 at 12:51:40PM -0700, Simon Glass wrote:
> Three of the merge functions look up the node which a fragment refers to
> and report 'Label or path not found' if it is missing, and two check
> for a label-relative reference in a plugin. Each of these has its own
> copy of the error, so a change to a message or its position must be made
> in several places.
> 
> Add lookup_ref() to look up the node, reporting an error if it is not
> found, and check_relative_ref() to report a label-relative reference.
> Use these in the merge functions.
> 
> The output does not change.
> 
> Assisted-by: Claude:claude-opus-5-5
> Signed-off-by: Simon Glass <sjg@chromium.org>

A helper function is certainly an improvement over duplication.  But
could we do this more elegantly by putting the checks into
merge_fragment() and passing the already resolved reference to the
individual merge_*() functions?

> ---
> 
> (no changes since v2)
> 
> Changes in v2:
> - Return true from check_relative_ref() if the reference is OK
> 
>  livetree.c | 75 +++++++++++++++++++++++++++++++++---------------------
>  1 file changed, 46 insertions(+), 29 deletions(-)
> 
> diff --git a/livetree.c b/livetree.c
> index a1d5d71..a70a7ba 100644
> --- a/livetree.c
> +++ b/livetree.c
> @@ -573,6 +573,40 @@ static bool is_ref_relative(const char *ref)
>  	return ref[0] != '/' && strchr(&ref[1], '/');
>  }
>  
> +/*
> + * Report an error at pos if a fragment uses a label-relative reference, which
> + * a plugin does not support
> + *
> + * Returns true if the reference is OK, false if there is an error
> + */
> +static bool check_relative_ref(struct fragment *f, struct srcpos *pos)
> +{
> +	if (!is_ref_relative(f->ref))
> +		return true;
> +	srcpos_error(pos, "Error",
> +		     "Label-relative reference %s not supported in plugin",
> +		     f->ref);
> +
> +	return false;
> +}
> +
> +/*
> + * Look up the node which a fragment refers to, reporting an error if it is
> + * not found
> + *
> + * Returns the node, or NULL if it is not found
> + */
> +static struct node *lookup_ref(struct node *dt, struct fragment *f)
> +{
> +	struct node *target = get_node_by_ref(dt, f->ref);
> +
> +	if (!target)
> +		srcpos_error(f->ref_srcpos, "Error",
> +			     "Label or path %s not found", f->ref);
> +
> +	return target;
> +}
> +
>  /*
>   * 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
> @@ -582,17 +616,14 @@ static bool is_ref_relative(const char *ref)
>   */
>  static bool merge_first_ref(struct node **dtp, struct fragment *f, bool plugin)
>  {
> -	bool ok = true;
> +	bool ok;
>  
> -	if (!plugin) {
> +	if (plugin) {
> +		ok = check_relative_ref(f, f->node->srcpos);
> +	} else {
>  		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), ""),
> @@ -618,13 +649,8 @@ static bool merge_ref(struct node *dt, struct fragment *f, bool plugin)
>  
>  	assert(f->type == FRAGMENT_NODE);
>  	if (plugin) {
> -		if (is_ref_relative(f->ref)) {
> -			srcpos_error(f->label ? f->label_srcpos : f->ref_srcpos,
> -				     "Error",
> -				     "Label-relative reference %s not supported in plugin",
> -				     f->ref);
> -			ok = false;
> -		}
> +		ok = check_relative_ref(f, f->label ? f->label_srcpos :
> +					f->ref_srcpos);
>  		if (!f->label &&
>  		    (f->ref[0] == '/' || !get_node_by_ref(dt, f->ref))) {
>  			/* Add a fragment@N node for the reference */
> @@ -633,12 +659,9 @@ static bool merge_ref(struct node *dt, struct fragment *f, bool plugin)
>  		}
>  	}
>  
> -	target = get_node_by_ref(dt, f->ref);
> -	if (!target) {
> -		srcpos_error(f->ref_srcpos, "Error",
> -			     "Label or path %s not found", f->ref);
> +	target = lookup_ref(dt, f);
> +	if (!target)
>  		return false;
> -	}
>  	if (f->label)
>  		add_label(&target->labels, f->label);
>  	merge_nodes(target, f->node);
> @@ -653,14 +676,11 @@ static bool merge_ref(struct node *dt, struct fragment *f, bool plugin)
>   */
>  static bool merge_delete_node(struct node *dt, struct fragment *f)
>  {
> -	struct node *target = get_node_by_ref(dt, f->ref);
> +	struct node *target = lookup_ref(dt, f);
>  
>  	assert(f->type == FRAGMENT_DELETE_NODE);
> -	if (!target) {
> -		srcpos_error(f->ref_srcpos, "Error",
> -			     "Label or path %s not found", f->ref);
> +	if (!target)
>  		return false;
> -	}
>  	delete_node(target);
>  
>  	return true;
> @@ -674,14 +694,11 @@ static bool merge_delete_node(struct node *dt, struct fragment *f)
>   */
>  static bool merge_omit_if_no_ref(struct node *dt, struct fragment *f)
>  {
> -	struct node *target = get_node_by_ref(dt, f->ref);
> +	struct node *target = lookup_ref(dt, f);
>  
>  	assert(f->type == FRAGMENT_OMIT_IF_NO_REF);
> -	if (!target) {
> -		srcpos_error(f->ref_srcpos, "Error",
> -			     "Label or path %s not found", f->ref);
> +	if (!target)
>  		return false;
> -	}
>  	omit_node_if_unused(target);
>  
>  	return true;
> -- 
> 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 --]

  reply	other threads:[~2026-11-16  4:34 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-06 19:51 [PATCH v3 00/18] dtc: Merge the fragments of a dts file after parsing Simon Glass
2026-11-06 19:51 ` [PATCH v3 01/18] tests: Check how the fragments of a dts file are merged Simon Glass
2026-11-13  3:20   ` David Gibson
2026-11-06 19:51 ` [PATCH v3 02/18] srcpos: Allow srcpos_string() to accept NULL Simon Glass
2026-11-11  4:56   ` David Gibson
2026-11-06 19:51 ` [PATCH v3 03/18] dtc: Add a struct for the top-level fragments of a dts file Simon Glass
2026-11-13  3:46   ` David Gibson
2026-11-06 19:51 ` [PATCH v3 04/18] dtc: Move the /delete-node/ handling out of the grammar Simon Glass
2026-11-13  3:49   ` David Gibson
2026-11-06 19:51 ` [PATCH v3 05/18] dtc: Move the /omit-if-no-ref/ " Simon Glass
2026-11-13  3:50   ` David Gibson
2026-11-06 19:51 ` [PATCH v3 06/18] dtc: Move is_ref_relative() to livetree.c Simon Glass
2026-11-13  3:55   ` David Gibson
2026-11-06 19:51 ` [PATCH v3 07/18] dtc: Move label-reference handling out of the grammar Simon Glass
2026-11-13  3:58   ` David Gibson
2026-11-06 19:51 ` [PATCH v3 08/18] dtc: Merge &{label} in a plugin as with &label Simon Glass
2026-11-13  4:01   ` David Gibson
2026-11-06 19:51 ` [PATCH v3 09/18] dtc: Move path-reference handling out of the grammar Simon Glass
2026-11-13  4:09   ` David Gibson
2026-11-06 19:51 ` [PATCH v3 10/18] dtc: Move labelled-reference " Simon Glass
2026-11-13  4:13   ` David Gibson
2026-11-06 19:51 ` [PATCH v3 11/18] dtc: Move first-reference " Simon Glass
2026-11-13  4:19   ` David Gibson
2026-11-06 19:51 ` [PATCH v3 12/18] dtc: Add merge_fragment() to merge any fragment Simon Glass
2026-11-13  4:24   ` David Gibson
2026-11-06 19:51 ` [PATCH v3 13/18] dtc: Add helpers for the errors when merging a fragment Simon Glass
2026-11-16  3:49   ` David Gibson [this message]
2026-11-06 19:51 ` [PATCH v3 14/18] dtc: Merge the fragments of a dts file after parsing Simon Glass
2026-11-16  4:04   ` David Gibson
2026-11-06 19:51 ` [PATCH v3 15/18] dtc: Merge the fragments of a dts file in main() Simon Glass
2026-11-16  4:08   ` David Gibson
2026-11-06 19:51 ` [PATCH v3 16/18] dtc: Add the overlay fragments from merge_fragments() Simon Glass
2026-11-16  4:12   ` David Gibson
2026-11-06 19:51 ` [PATCH v3 17/18] tests: Check references to a generated overlay fragment Simon Glass
2026-11-16  4:15   ` David Gibson
2026-11-06 19:51 ` [PATCH v3 18/18] dtc: Add the overlay fragments after merging a plugin Simon Glass
2026-11-16  4:34   ` David Gibson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=avp9VgVC94lznIeY@gractus.seuss \
    --to=david@gibson.dropbear.id.au \
    --cc=devicetree-compiler@vger.kernel.org \
    --cc=herve.codina@bootlin.com \
    --cc=sjg@chromium.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox