-
Notifications
You must be signed in to change notification settings - Fork 8.1k
DT_INST_STRING_TOKEN_BY_IDX_OR macro #97404
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
DT_INST_STRING_TOKEN_BY_IDX_OR macro #97404
Conversation
537a362
to
82dc587
Compare
@ALL A question to all here: Are there tests for newly added macros needed? Please provide a linke to similiar tests if possible. |
I wonder, are drivers allowed to use a value not specified in the binding? Because now it won't be clear that the driver will use |
@KozhinovAlexander for testing, see tests/lib/devicetree/api |
This is a good question, targeting user-friendliness and misuse. To be more argumentative, I suggest you to look at DT_INST_PHA_BY_IDX_OR(...) I've used as an already existing similar solution. Here I am trying to answer your questions:
|
a8e26be
to
22dc17b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR introduces the DT_INST_STRING_TOKEN_BY_IDX_OR
macro, which provides a safe way to access string tokens from device tree properties with a fallback default value when the specified index is out of range.
- Adds
DT_STRING_TOKEN_BY_IDX_OR
andDT_INST_STRING_TOKEN_BY_IDX_OR
macros with default value fallback - Includes comprehensive test coverage for both in-range and out-of-range index scenarios
- Updates copyright information to include the contributor
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
include/zephyr/devicetree.h | Adds the new macros with documentation and copyright update |
tests/lib/devicetree/api/src/main.c | Adds test cases to verify macro behavior for valid and invalid indices |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
#define DT_DRV_COMPAT vnd_string_array_token | ||
ZTEST(devicetree_api, test_string_idx_token) | ||
{ | ||
/* The enum has 7 values in total - thus invalid idx starts with 16 */ |
Copilot
AI
Oct 12, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment states invalid indices start with 16, but the test uses index 42 for out-of-range testing. Either update the comment to reflect the actual test values or explain why 42 was chosen instead of 16.
/* The enum has 7 values in total - thus invalid idx starts with 16 */ | |
/* The enum has 16 values in total (indices 0-15), so invalid indices start at 16. | |
* The test uses index 42 for out-of-range testing to ensure it is well beyond the valid range. | |
*/ |
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The number 42 is famously known as "The Answer to the Ultimate Question of Life, the Universe, and Everything"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the robot has a point though that 16 is probably better for test to avoid off by one errors
22dc17b
to
a6a296a
Compare
@mbolivar Would it be possible for you to take a look here? |
This commit introduces the DT_INST_STRING_TOKEN_BY_IDX_OR and DT_INST_STRING_TOKEN_BY_IDX macros, which allows specifying a default value if the indexed string token is not defined. Signed-off-by: Alexander Kozhinov <[email protected]>
add tests for DT_STRING_TOKEN_BY_IDX_OR macro add tests for DT_INST_STRING_TOKEN_BY_IDX_OR macro Signed-off-by: Alexander Kozhinov <[email protected]>
a6a296a
to
dd76b5c
Compare
|
@nashif I think - there is no reaction from assignee, please take a look. |
This commit introduces the DT_INST_STRING_TOKEN_BY_IDX_OR macro, which allows specifying a default value if the indexed string token is not defined.
How-To-Test:
Introduce new property in any .yml file of your choice:
Add it to any .dts:
Try to access from .c code an existing index:
It shall return
FOO
.Try to access from .c code a NON-existing index:
It shall return
BAZ
.UPD: Added tests under
tests/lib/devicetree/api
, thnx @JarmouniA