dtlib: fix double empty line before root node

The root node is the first node in the DTS string representation, and is
currently separated from the headers by two empty lines.

Adjust the spacing so that only one line is printed in all situations. A
small adjustment is added to the test suite to keep the current expected
outputs unchanged.

Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
This commit is contained in:
Luca Burelli 2025-05-01 18:04:20 +02:00 committed by Benjamin Cabé
commit a63cb8e74d
2 changed files with 4 additions and 2 deletions

View file

@ -945,15 +945,15 @@ class DT:
Returns a DTS representation of the devicetree. Called automatically if
the DT instance is print()ed.
"""
s = "/dts-v1/;\n\n"
s = "/dts-v1/;\n"
if self.memreserves:
s += "\n"
for labels, address, offset in self.memreserves:
# List the labels in a consistent order to help with testing
for label in labels:
s += f"{label}: "
s += f"/memreserve/ {address:#018x} {offset:#018x};\n"
s += "\n"
return s + str(self.root)

View file

@ -30,7 +30,9 @@ def uncomment(dts):
'''Trim added comments from a DT string.'''
# remove node comments, including leading empty line
# but keep the one before the root node
dts = re.sub(r'\n\n[ \t]*/\*.*?\*/\n', '\n', dts, flags=re.DOTALL)
dts = re.sub(r'\n/ {\n', r'\n\n/ {\n', dts)
# remove property comments
dts = re.sub(r'[ \t]*/\*.*?\*/\n', '\n', dts)