#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0-only
#
# Check DTS coding style on YAML binding examples and on
# .dts/.dtsi/.dtso source files. Enforces rules from
# Documentation/devicetree/bindings/dts-coding-style.rst.
#
# Two modes:
# --mode=relaxed (default)
# Only rules that produce zero warnings on the current tree.
# Suitable for dt_binding_check.
# --mode=strict
# All rules. Required for new submissions.
#
# Two input types (auto-detected by file extension):
# *.yaml -- DT binding; check each example block
# *.dts/*.dtsi/*.dtso -- DTS source; whole file is one block
#
# Rules are declared in a registry (see RULES below); each rule is
# tagged with the lowest mode that runs it. Promoting a rule from
# 'strict' to 'relaxed' is a one-line change.
import argparse
import re
import sys
from enum import Enum, auto
import ruamel.yaml
# ---------------------------------------------------------------------------
# Line classification
# ---------------------------------------------------------------------------
class LineType(Enum):
BLANK = auto()
COMMENT = auto() # // ... or /* ... */ on one line
COMMENT_START = auto() # /* without closing */
COMMENT_BODY = auto() # inside a multi-line comment
COMMENT_END = auto() # closing */
PREPROCESSOR = auto() # #include / #define / #ifdef / ...
NODE_OPEN = auto() # something { (with optional label/name/addr)
NODE_CLOSE = auto() # };
PROPERTY = auto() # name = value; or name;
CONTINUATION = auto() # continuation of a multi-line property
re_cpp_directive = re.compile(
r'^#\s*(include|define|undef|ifdef|ifndef|if|else|elif|endif|'
r'pragma|error|warning)\b')
# label: name@addr { -- label and addr optional; name can be "/"
# Per the DT spec a node name may start with a digit (e.g. 1wire@...).
# The address part is captured loosely (any non-space, non-brace run) so
# malformed addresses (e.g. memory@0x1000) still reach
# check_unit_address_format() instead of silently bypassing the check.
re_node_header = re.compile(
r'^(?:([a-zA-Z_][a-zA-Z0-9_]*):\s*)?'
r'([a-zA-Z0-9][a-zA-Z0-9,._+-]*|/)'
r'(?:@([^\s{]+))?'
r'\s*\{$')
re_ref_node = re.compile(
r'^&([a-zA-Z_][a-zA-Z0-9_]*)\s*\{$')
def is_preprocessor(stripped):
"""Tell C preprocessor directives apart from DTS '#'-prefixed props."""
return re_cpp_directive.match(stripped) is not None
class DtsLine:
__slots__ = ('lineno', 'raw', 'linetype', 'indent_str', 'stripped',
'prop_name', 'continuations',
'node_name', 'node_addr', 'label', 'ref_name', 'depth',
'closures')
def __init__(self, lineno, raw, linetype, indent_str, stripped):
self.lineno = lineno # 1-based within the block
self.raw = raw
self.linetype = linetype
self.indent_str = indent_str # leading whitespace as-is
self.stripped = stripped
self.prop_name = None
self.continuations = []
self.node_name = None
self.node_addr = None
self.label = None
self.ref_name = None
self.depth = 0 # filled in by classify_lines
self.closures = 1 # count of '}' on a NODE_CLOSE line
def _split_code(text):
"""Return (code, opens_block) for a leading-stripped line: the
code portion with // and /* */ comments removed (string literals
kept verbatim), and whether a /* */ block comment is left open.
The code portion is right-stripped so the endswith() checks in
classify_lines see code only, not a trailing comment or blanks."""
out = []
i = 0
n = len(text)
while i < n:
c = text[i]
if c == '"':
j = i + 1
while j < n:
if text[j] == '\\':
j += 2
continue
if text[j] == '"':
j += 1
break
j += 1
out.append(text[i:j])
i = j
continue
if c == '/' and i + 1 < n and text[i + 1] == '/':
break
if c == '/' and i + 1 < n and text[i + 1] == '*':
end = text.find('*/', i + 2)
if end < 0:
return (''.join(out).rstrip(), True)
i = end + 2
continue
out.append(c)
i += 1
return (