dt-check-style support for indented include fragments
cover dt-check-style support for indented include fragments
Hi,
Some .dtsi files are included from inside a node and carry the indentation of that context. This is e.g. used for the multi-die Apple SoCs and dt-check-style currently interprets that as part of the indent unit and reports spurious warnings for otherwise correctly indented Apple and Broadcom fragments.
Teach indent-unit-dts to detect and validate the inherited prefix and make indent-consistent account for it as well. Also add fixtures to test this.
With this all arm64 device trees pass the relaxed checks.
Best,
Sven
---
Sven Peter (2):
dt-check-style: Handle indented fragments in indent-unit-dts
dt-check-style: Handle indented fragments in indent-consistent
scripts/dtc/dt-check-style | 72 +++++++++++++++++----- .../dt-style-selftest/bad/dts-fragment-indent.dtsi | 12 ++++ .../bad/dts-fragment-missing-prefix.dtsi | 6 ++ .../bad/dts-indented-ref-node.dtsi | 6 ++ .../expected/dts-fragment-indent.dtsi.txt | 3 + .../expected/dts-fragment-missing-prefix.dtsi.txt | 2 + .../expected/dts-indented-ref-node.dtsi.txt | 2 + .../dtc/dt-style-selftest/good/dts-fragment.dtsi | 19 ++++++ 8 files changed, 106 insertions(+), 16 deletions(-)
---
base-commit: a23cbb05744b22719efdc34f9e329a120e81e617
change-id: 20260830-b4-dts-check-fix-db992ba0a7dd
Best regards,
Signature · 1 line
Sven Peter <[email protected]>
PATCH 1/2 dt-check-style: Handle indented fragments in indent-unit-dts
The multi-die Apple SoCs contain most of their nodes in a file that is included to instantiate the same nodes across both dies. These included fragments all have a tabs-only prefix that is correct in the final composed file. dt-check-style however finds the first line starting with two (or more) tabs and then indent-unit-dts rejects those correctly indented files.
Fix that by detecing a tabs-only prefix shared by rootless files and treat that as a constant offset. Also add fixtures to test this.
--- scripts/dtc/dt-check-style | 69 +++++++++++++++++----- .../bad/dts-fragment-missing-prefix.dtsi | 6 ++ .../bad/dts-indented-ref-node.dtsi | 6 ++ .../expected/dts-fragment-missing-prefix.dtsi.txt | 2 + .../expected/dts-indented-ref-node.dtsi.txt | 2 + 5 files changed, 70 insertions(+), 15 deletions(-)
scripts/dtc/dt-check-style +54 -15
@@ -380,12 +380,43 @@ def check_mixed_indent_chars(ctx): yield (cont.lineno, 'mixed tabs and spaces in indent') +def detect_fragment_offset(ctx): + """Return the indentation inherited from an include context. + + A fragment has neither a root nor top-level reference nodes and all + its top-level lines share the same tabs-only indentation. + """ + if ctx.file_type != 'dts': + return '' + offset = None + for dl in ctx.lines: + if dl.depth != 0: + continue + if dl.linetype not in (LineType.NODE_OPEN, LineType.NODE_CLOSE, + LineType.PROPERTY): + continue + if dl.linetype == LineType.NODE_OPEN and \ + (dl.node_name == '/' or dl.ref_name is not None): + return '' + if offset is None: + offset = dl.indent_str + elif dl.indent_str != offset: + return '' + if not offset: + return '' + if any(char != '\t' for char in offset): + return '' + return offset + + def detect_indent_unit(ctx): """Find the indent unit used at depth 1 in this block. Returns tuple of string (one of: ' ' (2 spaces), ' ' (4 spaces), - '\\t' (tab), or None if depth-1 is empty or ambiguous) and line number when - detection was made).""" + '\\t' (tab), or None if depth-1 is empty or ambiguous), line number when + detection was made, and whether the detected fragment prefix is valid. + """ + prefix = detect_fragment_offset(ctx) for dl in ctx.lines: if dl.depth != 1: continue
@@ -395,20 +426,25 @@ def detect_indent_unit(ctx): continue if not dl.indent_str: continue - if dl.indent_str == '\t': - return ('\t', dl.lineno) - if dl.indent_str == ' ': - return (' ', dl.lineno) - if dl.indent_str == ' ': - return (' ', dl.lineno) + indent = dl.indent_str + if prefix and not indent.startswith(prefix): + return (indent, dl.lineno, False) + if prefix: + indent = indent[len(prefix):] + if indent == '\t': + return ('\t', dl.lineno, True) + if indent == ' ': + return (' ', dl.lineno, True) + if indent == ' ': + return (' ', dl.lineno, True) # Anything else at depth 1 is non-canonical; flag elsewhere. - return (dl.indent_str, dl.lineno) - return (None, None) + return (indent, dl.lineno, True) + return (None, None, True) def check_indent_unit_relaxed(ctx): """YAML examples: 2 or 4 spaces. Never tabs or other widths.""" - (unit, lineno) = detect_indent_unit(ctx) + (unit, lineno, _) = detect_indent_unit(ctx) if unit is None: return if unit not in (' ', ' '):
@@ -417,7 +453,10 @@ def check_indent_unit_relaxed(ctx): def check_indent_unit_dts(ctx): """DTS files: 1 tab per level. Always required.""" - (unit, lineno) = detect_indent_unit(ctx) + (unit, lineno, prefix_valid) = detect_indent_unit(ctx) + if not prefix_valid: + yield (lineno, 'indent does not start with fragment offset') + return if unit is None: return if unit != '\t':
@@ -426,7 +465,7 @@ def check_indent_unit_dts(ctx): def check_indent_unit_strict(ctx): """YAML: must be exactly 4 spaces. DTS: 1 tab (same as relaxed).""" - (unit, lineno) = detect_indent_unit(ctx) + (unit, lineno, _) = detect_indent_unit(ctx) if unit is None: return if ctx.file_type == 'yaml':
@@ -437,8 +476,8 @@ def check_indent_unit_strict(ctx): def check_indent_consistent(ctx): """All indented lines must be a multiple of the detected unit.""" - (unit, lineno) = detect_indent_unit(ctx) - if unit is None: + (unit, lineno, prefix_valid) = detect_indent_unit(ctx) + if unit is None or not prefix_valid: return if ctx.file_type == 'yaml': if unit not in (' ', ' '):
scripts/dtc/dt-style-selftest/bad/dts-fragment-missing-prefix.dtsi new file +6 -0
@@ -0,0 +1,6 @@ +// SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause +/* Include fragment whose child does not contain the inherited indentation. */ + + bus@10000 { + compatible = "simple-bus"; + };
scripts/dtc/dt-style-selftest/bad/dts-indented-ref-node.dtsi new file +6 -0
@@ -0,0 +1,6 @@ +// SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause +/* Reference nodes must remain at column zero. */ + + &example { + status = "okay"; + };
scripts/dtc/dt-style-selftest/expected/dts-fragment-missing-prefix.dtsi.txt new file +2 -0
@@ -0,0 +1,2 @@ +# mode=relaxed +bad/dts-fragment-missing-prefix.dtsi:5: [indent-unit-dts] indent does not start with fragment offset
scripts/dtc/dt-style-selftest/expected/dts-indented-ref-node.dtsi.txt new file +2 -0
@@ -0,0 +1,2 @@ +# mode=strict +bad/dts-indented-ref-node.dtsi:5: [indent-unit-dts] indent unit must be 1 tab in DTS, got '\t\t'
Signature · 1 line
2.55.0
Re: [PATCH 1/2] dt-check-style: Handle indented fragments in indent-unit-dts
Quoted 24 lines · Sven Peter
The multi-die Apple SoCs contain most of their nodes in a file that is included to instantiate the same nodes across both dies. These included fragments all have a tabs-only prefix that is correct in the final composed file. dt-check-style however finds the first line starting with two (or more) tabs and then indent-unit-dts rejects those correctly indented files.
Fix that by detecing a tabs-only prefix shared by rootless files and treat that as a constant offset. Also add fixtures to test this.
Signed-off-by Sven Peter <[email protected]>--- scripts/dtc/dt-check-style | 69 +++++++++++++++++----- .../bad/dts-fragment-missing-prefix.dtsi | 6 ++ .../bad/dts-indented-ref-node.dtsi | 6 ++ .../expected/dts-fragment-missing-prefix.dtsi.txt | 2 + .../expected/dts-indented-ref-node.dtsi.txt | 2 + 5 files changed, 70 insertions(+), 15 deletions(-)scripts/dtc/dt-check-style +0
-0
scripts/dtc/dt-check-style def check_mixed_indent_chars(ctx): · line 381
Quoted 2 lines
scripts/dtc/dt-check-style +0
-0@@ -380,12 +380,43 @@ def check_mixed_indent_chars(ctx): yield (cont.lineno, 'mixed tabs and spaces in indent')
I did not expect simultaneous work, so I did some renaming and moving which unfortunately will cause you conflicts.
My patches got merged and they add _ to detect_indent_unit(), so please follow. Also other functions go to a bit different place after sorting.
scripts/dtc/dt-check-style def check_mixed_indent_chars(ctx): · line 386
Quoted 5 lines
scripts/dtc/dt-check-style +4
-0+def detect_fragment_offset(ctx): + """Return the indentation inherited from an include context. + + A fragment has neither a root nor top-level reference nodes and all
What is a DTS fragment?
scripts/dtc/dt-check-style def check_mixed_indent_chars(ctx): · line 387
Quoted 1 line
scripts/dtc/dt-check-style +1
-0+ its top-level lines share the same tabs-only indentation.
I don't think you are fixing right problem or at least not completely. The Apple DTS is incorrect, because - I assume we talk about t600x-dieX.dtsi - this should not be indented with a tab in the first place. Apple does not get separate coding style, although I am working on supression tags to avoid certain warnings.
Quoted 122 lines
scripts/dtc/dt-check-style +49
-15+ """ + if ctx.file_type != 'dts': + return '' + offset = None + for dl in ctx.lines: + if dl.depth != 0: + continue + if dl.linetype not in (LineType.NODE_OPEN, LineType.NODE_CLOSE, + LineType.PROPERTY): + continue + if dl.linetype == LineType.NODE_OPEN and \ + (dl.node_name == '/' or dl.ref_name is not None): + return '' + if offset is None: + offset = dl.indent_str + elif dl.indent_str != offset: + return '' + if not offset: + return '' + if any(char != '\t' for char in offset): + return '' + return offset + + def detect_indent_unit(ctx): """Find the indent unit used at depth 1 in this block. Returns tuple of string (one of: ' ' (2 spaces), ' ' (4 spaces), - '\\t' (tab), or None if depth-1 is empty or ambiguous) and line number when - detection was made).""" + '\\t' (tab), or None if depth-1 is empty or ambiguous), line number when + detection was made, and whether the detected fragment prefix is valid. + """ + prefix = detect_fragment_offset(ctx) for dl in ctx.lines: if dl.depth != 1: continue@@ -395,20 +426,25 @@ def detect_indent_unit(ctx): continue if not dl.indent_str: continue - if dl.indent_str == '\t': - return ('\t', dl.lineno) - if dl.indent_str == ' ': - return (' ', dl.lineno) - if dl.indent_str == ' ': - return (' ', dl.lineno) + indent = dl.indent_str + if prefix and not indent.startswith(prefix): + return (indent, dl.lineno, False) + if prefix: + indent = indent[len(prefix):] + if indent == '\t': + return ('\t', dl.lineno, True) + if indent == ' ': + return (' ', dl.lineno, True) + if indent == ' ': + return (' ', dl.lineno, True) # Anything else at depth 1 is non-canonical; flag elsewhere. - return (dl.indent_str, dl.lineno) - return (None, None) + return (indent, dl.lineno, True) + return (None, None, True) def check_indent_unit_relaxed(ctx): """YAML examples: 2 or 4 spaces. Never tabs or other widths.""" - (unit, lineno) = detect_indent_unit(ctx) + (unit, lineno, _) = detect_indent_unit(ctx) if unit is None: return if unit not in (' ', ' '):@@ -417,7 +453,10 @@ def check_indent_unit_relaxed(ctx): def check_indent_unit_dts(ctx): """DTS files: 1 tab per level. Always required.""" - (unit, lineno) = detect_indent_unit(ctx) + (unit, lineno, prefix_valid) = detect_indent_unit(ctx) + if not prefix_valid: + yield (lineno, 'indent does not start with fragment offset') + return if unit is None: return if unit != '\t':@@ -426,7 +465,7 @@ def check_indent_unit_dts(ctx): def check_indent_unit_strict(ctx): """YAML: must be exactly 4 spaces. DTS: 1 tab (same as relaxed).""" - (unit, lineno) = detect_indent_unit(ctx) + (unit, lineno, _) = detect_indent_unit(ctx) if unit is None: return if ctx.file_type == 'yaml':@@ -437,8 +476,8 @@ def check_indent_unit_strict(ctx): def check_indent_consistent(ctx): """All indented lines must be a multiple of the detected unit.""" - (unit, lineno) = detect_indent_unit(ctx) - if unit is None: + (unit, lineno, prefix_valid) = detect_indent_unit(ctx) + if unit is None or not prefix_valid: return if ctx.file_type == 'yaml': if unit not in (' ', ' '):scripts/dtc/dt-style-selftest/bad/dts-fragment-missing-prefix.dtsi new file +6
-0@@ -0,0 +1,6 @@ +// SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause +/* Include fragment whose child does not contain the inherited indentation. */ + + bus@10000 { + compatible = "simple-bus"; + };scripts/dtc/dt-style-selftest/bad/dts-indented-ref-node.dtsi new file +0
-0
scripts/dtc/dt-style-selftest/bad/dts-indented-ref-node.dtsi line 6
Quoted 6 lines
scripts/dtc/dt-style-selftest/bad/dts-indented-ref-node.dtsi new file +6
-0@@ -0,0 +1,6 @@ +// SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause +/* Reference nodes must remain at column zero. */ + + &example { + status = "okay"; + };
A good example would be useful and probably made the discussion easier - we would see that "good" example is actually not desired/correct style.
Best regards,
Krzysztof
PATCH 2/2 dt-check-style: Handle indented fragments in indent-consistent
In reply to Sven Peterindent-consistent compares every line against its nesting depth without accounting for the constant indentation prefix used for include fragments. It thus rejects otherwise valid fragments.
Fix that by adding the fragment prefix to the expected indentation and add test fixtures.
--- scripts/dtc/dt-check-style | 3 ++- .../dt-style-selftest/bad/dts-fragment-indent.dtsi | 12 ++++++++++++ .../expected/dts-fragment-indent.dtsi.txt | 3 +++ scripts/dtc/dt-style-selftest/good/dts-fragment.dtsi | 19 +++++++++++++++++++ 4 files changed, 36 insertions(+), 1 deletion(-)
scripts/dtc/dt-check-style +2 -1
@@ -486,6 +486,7 @@ def check_indent_consistent(ctx): if unit != '\t': return + prefix = detect_fragment_offset(ctx) for dl in ctx.lines: if dl.linetype in (LineType.BLANK, LineType.PREPROCESSOR): continue
@@ -498,7 +499,7 @@ def check_indent_consistent(ctx): # The indent must be 'unit' repeated dl.depth times, exactly. # NODE_CLOSE lines have depth equal to the post-decrement value, # which matches the indent expected. - expected = unit * dl.depth + expected = prefix + unit * dl.depth if dl.indent_str != expected: yield (dl.lineno, 'indent mismatch (expected depth %d * %r)' %
scripts/dtc/dt-style-selftest/bad/dts-fragment-indent.dtsi new file +12 -0
@@ -0,0 +1,12 @@ +// SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause +/* Incorrect indentation inside an indented include fragment. */ + + bus@10000 { + compatible = "simple-bus"; + reg = <0x10000 0x1000>; + + device@100 { + compatible = "example,test"; + reg = <0x100 0x10>; + }; + };
scripts/dtc/dt-style-selftest/expected/dts-fragment-indent.dtsi.txt new file +3 -0
@@ -0,0 +1,3 @@ +# mode=strict +bad/dts-fragment-indent.dtsi:9: [indent-consistent] indent mismatch (expected depth 2 * '\t') +bad/dts-fragment-indent.dtsi:10: [indent-consistent] indent mismatch (expected depth 2 * '\t')
scripts/dtc/dt-style-selftest/good/dts-fragment.dtsi new file +19 -0
@@ -0,0 +1,19 @@ +// SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause +/* Include fragment indented for its inclusion context. */ + + bus@10000 { + compatible = "simple-bus"; + reg = <0x10000 0x1000>; + #address-cells = <1>; + #size-cells = <1>; + + device@100 { + compatible = "example,test"; + reg = <0x100 0x10>; + }; + }; + + bus@20000 { + compatible = "simple-bus"; + reg = <0x20000 0x1000>; + };
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH 2/2] dt-check-style: Handle indented fragments in indent-consistent
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The error message for indentation mismatch ignores the newly added fragment prefix, leading to…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The error message for indentation mismatch ignores the newly added fragment prefix, leading to mathematically incorrect and highly confusing feedback for developers. --
commit f969e2d48b468cab099652ff63ddd56c43afbb19
Author: Sven Peter <[email protected]>
dt-check-style: Handle indented fragments in indent-consistent
This commit updates the indent-consistent check to account for constant indentation prefixes in include fragments. It adds the fragment prefix to the expected indentation and includes test fixtures.
Quoted 4 lines
scripts/dtc/dt-check-style +0
-0
[ ... ]
Quoted 10 lines
scripts/dtc/dt-check-style +1
-1@@ -498,7 +499,7 @@ def check_indent_consistent(ctx): # The indent must be 'unit' repeated dl.depth times, exactly. # NODE_CLOSE lines have depth equal to the post-decrement value, # which matches the indent expected. - expected = unit * dl.depth + expected = prefix + unit * dl.depth if dl.indent_str != expected: yield (dl.lineno, 'indent mismatch (expected depth %d * %r)' %(dl.depth, unit))
[Severity: Low]
Should the error message be updated to reflect the newly calculated expected indentation? Since expected now includes the prefix, printing just dl.depth * unit could be confusing for developers trying to match the indentation required by the tool.
[ ... ]
scripts/dtc/dt-style-selftest/expected/dts-fragment-indent.dtsi.txt line 3
Quoted 9 lines
scripts/dtc/dt-style-selftest/expected/dts-fragment-indent.dtsi.txt new file +3
-0@@ -0,0 +1,3 @@ +# mode=strict +bad/dts-fragment-indent.dtsi:9: [indent-consistent] indent mismatch (expected depth 2 * '\t') +bad/dts-fragment-indent.dtsi:10: [indent-consistent] indent mismatch (expected depth 2 * '\t')
[Severity: Low]
Does this test case intentionally assert the potentially misleading error message? If the message in check_indent_consistent is updated to include the prefix, this expected output would also need to be adjusted.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH 2/2] dt-check-style: Handle indented fragments in indent-consistent
In reply to Sven PeterQuoted 15 lines · Sven Peter
scripts/dtc/dt-style-selftest/expected/dts-fragment-indent.dtsi.txt new file +3
-0@@ -0,0 +1,3 @@ +# mode=strict +bad/dts-fragment-indent.dtsi:9: [indent-consistent] indent mismatch (expected depth 2 * '\t') +bad/dts-fragment-indent.dtsi:10: [indent-consistent] indent mismatch (expected depth 2 * '\t')scripts/dtc/dt-style-selftest/good/dts-fragment.dtsi new file +0
-0
scripts/dtc/dt-style-selftest/good/dts-fragment.dtsi line 4
Quoted 4 lines
scripts/dtc/dt-style-selftest/good/dts-fragment.dtsi new file +4
-0@@ -0,0 +1,19 @@ +// SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause +/* Include fragment indented for its inclusion context. */ + + bus@10000 {
So here is a good example... and this is code is wrong.
You CANNOT have MMIO node outside of MMIO bus or a new node outside of root node indented with one tab.
Best regards,
Krzysztof
Re: [PATCH 2/2] dt-check-style: Handle indented fragments in indent-consistent
Quoted 22 lines · Krzysztof Kozlowski
Quoted 15 lines · Sven Peter
scripts/dtc/dt-style-selftest/expected/dts-fragment-indent.dtsi.txt new file +3
-0@@ -0,0 +1,3 @@ +# mode=strict +bad/dts-fragment-indent.dtsi:9: [indent-consistent] indent mismatch (expected depth 2 * '\t') +bad/dts-fragment-indent.dtsi:10: [indent-consistent] indent mismatch (expected depth 2 * '\t')scripts/dtc/dt-style-selftest/good/dts-fragment.dtsi new file +0
-0scripts/dtc/dt-style-selftest/good/dts-fragment.dtsi line 4
Quoted 4 lines
scripts/dtc/dt-style-selftest/good/dts-fragment.dtsi new file +4
-0@@ -0,0 +1,19 @@ +// SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause +/* Include fragment indented for its inclusion context. */ + + bus@10000 {So here is a good example... and this is code is wrong.
Quoted 2 lines
You CANNOT have MMIO node outside of MMIO bus or a new node outside of root node indented with one tab.
Fair enough, just ignore this series then.
I'll just do the whitespace fix then and un-indent the -dieX files for apple which I think should make them pass the checker then.
Sven
Re: [PATCH 2/2] dt-check-style: Handle indented fragments in indent-consistent
Quoted 25 lines · Sven Peter
Quoted 22 lines · Krzysztof Kozlowski
Quoted 15 lines · Sven Peter
scripts/dtc/dt-style-selftest/expected/dts-fragment-indent.dtsi.txt new file +3
-0@@ -0,0 +1,3 @@ +# mode=strict +bad/dts-fragment-indent.dtsi:9: [indent-consistent] indent mismatch (expected depth 2 * '\t') +bad/dts-fragment-indent.dtsi:10: [indent-consistent] indent mismatch (expected depth 2 * '\t')scripts/dtc/dt-style-selftest/good/dts-fragment.dtsi new file +0
-0scripts/dtc/dt-style-selftest/good/dts-fragment.dtsi line 4
Quoted 4 lines
scripts/dtc/dt-style-selftest/good/dts-fragment.dtsi new file +4
-0@@ -0,0 +1,19 @@ +// SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause +/* Include fragment indented for its inclusion context. */ + + bus@10000 {So here is a good example... and this is code is wrong.
Quoted 7 lines
Quoted 2 lines
You CANNOT have MMIO node outside of MMIO bus or a new node outside of root node indented with one tab.
Fair enough, just ignore this series then.
I'll just do the whitespace fix then and un-indent the -dieX files for apple which I think should make them pass the checker then.
I see Apple DTS two soc nodes, so overriding/extending by phandle is a bit trickier, but this should work in t600x-dieX.dtsi:
// earlier macro (or however you concatenate these) #define DIE_NODE die0
&DIE_NODE {
// here goes stuff with original indentation
DIE_NODE(cpufreq_e): cpufreq@210e20000 {
...
};
};Best regards,
Krzysztof
Re: [PATCH 2/2] dt-check-style: Handle indented fragments in indent-consistent
Quoted 8 lines · Krzysztof Kozlowski
Quoted 7 lines
Quoted 2 lines
You CANNOT have MMIO node outside of MMIO bus or a new node outside of root node indented with one tab.
Fair enough, just ignore this series then.
I'll just do the whitespace fix then and un-indent the -dieX files for apple which I think should make them pass the checker then.
Quoted 7 lines
I see Apple DTS two soc nodes, so overriding/extending by phandle is a bit trickier, but this should work in
t600x-dieX.dtsi:// earlier macro (or however you concatenate these) #define
DIE_NODEdie0
&DIE_NODE{
Obviously this should be called differently not to conflict with existing DIE_NODE(), but I hope you get the point.
Quoted 8 lines
// here goes stuff with original indentation DIE_NODE(cpufreq_e): cpufreq@210e20000 { ... }; };Best regards,
Krzysztof
Best regards,
Krzysztof