History

dtc: dt-check-style: Simplify setting depth of DtsLine

When creating new DtsLine object, pass expected indentation depth as constructor, instead of assigning it immediately after, so the code will be easier to read and explicit (depth is not supposed to change during DtsLine lifetime).

Krzysztof Kozlowski committed 3 months ago

Commit d863ae61 parent 2d51fa61 file · +11 −20Browse files

Showing the changes to scripts (1 of 1 file). Show all

Unified · Split

scripts/dtc/dt-check-style+11−20
@@ -82,11 +82,12 @@ class DtsLine: · 81 unmodified lines
8282 'node_name', 'node_addr', 'label', 'ref_name', 'depth',
8383 'closures')
8484
85− def __init__(self, lineno, raw, linetype, indent_str, stripped):
85+ def __init__(self, lineno, raw, linetype, depth, indent_str, stripped):
8686 self.lineno = lineno # 1-based within the block
8787 self.raw = raw
8888 self.linetype = linetype
8989 self.indent_str = indent_str # leading whitespace as-is
90+ self.depth = depth
9091 self.stripped = stripped
9192 self.prop_name = None
9293 self.continuations = []
@@ -94,7 +95,6 @@ class DtsLine: · 1 unmodified line
9495 self.node_addr = None
9596 self.label = None
9697 self.ref_name = None
97− self.depth = 0 # filled in by classify_lines
9898 self.closures = 1 # count of '}' on a NODE_CLOSE line
9999
100100
@@ -162,16 +162,14 @@ def classify_lines(text): · 61 unmodified lines
162162 # or a blank line).
163163 if in_cpp_macro:
164164 dl = DtsLine(i, raw, LineType.PREPROCESSOR,
165− indent_str, stripped)
166− dl.depth = depth
165+ depth, indent_str, stripped)
167166 out.append(dl)
168167 in_cpp_macro = (bool(stripped) and
169168 stripped.rstrip().endswith('\\'))
170169 continue
171170
172171 if not stripped:
173− dl = DtsLine(i, raw, LineType.BLANK, '', '')
174− dl.depth = depth
172+ dl = DtsLine(i, raw, LineType.BLANK, depth, '', '')
175173 out.append(dl)
176174 continue
177175
@@ -180,15 +178,13 @@ def classify_lines(text): · 2 unmodified lines
180178 else LineType.COMMENT_BODY)
181179 if ltype == LineType.COMMENT_END:
182180 in_block_comment = False
183− dl = DtsLine(i, raw, ltype, indent_str, stripped)
184− dl.depth = depth
181+ dl = DtsLine(i, raw, ltype, depth, indent_str, stripped)
185182 out.append(dl)
186183 continue
187184
188185 if (stripped.startswith('#') or stripped.startswith('/')) and is_preprocessor(stripped):
189− dl = DtsLine(i, raw, LineType.PREPROCESSOR,
186+ dl = DtsLine(i, raw, LineType.PREPROCESSOR, depth,
190187 indent_str, stripped)
191− dl.depth = depth
192188 out.append(dl)
193189 prev_complete = True
194190 in_cpp_macro = stripped.rstrip().endswith('\\')
@@ -205,14 +201,12 @@ def classify_lines(text): · 10 unmodified lines
205201 # structural classification entirely.
206202 if not code:
207203 ltype = LineType.COMMENT_START if opens_block else LineType.COMMENT
208− dl = DtsLine(i, raw, ltype, indent_str, stripped)
209− dl.depth = depth
204+ dl = DtsLine(i, raw, ltype, depth, indent_str, stripped)
210205 out.append(dl)
211206 continue
212207
213208 if not prev_complete:
214− dl = DtsLine(i, raw, LineType.CONTINUATION, indent_str, code)
215− dl.depth = depth
209+ dl = DtsLine(i, raw, LineType.CONTINUATION, depth, indent_str, code)
216210 out.append(dl)
217211 prev_complete = (code.endswith(';') or
218212 code.endswith('{') or
@@ -227,26 +221,23 @@ def classify_lines(text): · 8 unmodified lines
227221 if re_only_closures.match(code):
228222 closures = code.count('}')
229223 depth = max(depth - closures, 0)
230− dl = DtsLine(i, raw, LineType.NODE_CLOSE, indent_str, code)
231− dl.depth = depth
224+ dl = DtsLine(i, raw, LineType.NODE_CLOSE, depth, indent_str, code)
232225 dl.closures = closures
233226 out.append(dl)
234227 prev_complete = True
235228 continue
236229
237230 if code.endswith('{'):
238− dl = DtsLine(i, raw, LineType.NODE_OPEN, indent_str, code)
231+ dl = DtsLine(i, raw, LineType.NODE_OPEN, depth, indent_str, code)
239232 parse_node_header(dl)
240− dl.depth = depth
241233 out.append(dl)
242234 depth += 1
243235 prev_complete = True
244236 continue
245237
246238 # Property (or first line of a multi-line property).
247− dl = DtsLine(i, raw, LineType.PROPERTY, indent_str, code)
239+ dl = DtsLine(i, raw, LineType.PROPERTY, depth, indent_str, code)
248240 parse_property_name(dl)
249− dl.depth = depth
250241 out.append(dl)
251242 prev_complete = code.endswith(';')
252243