scripts/dtc/dt-check-style

Source file repositories/reference/linux-study-clean/scripts/dtc/dt-check-style

File Facts

System
Linux kernel
Corpus path
scripts/dtc/dt-check-style
Extension
[no extension]
Size
43843 bytes
Lines
1193
Domain
Support Tooling And Documentation
Bucket
scripts
Inferred role
Support Tooling And Documentation: scripts
Status
atlas-only

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

#!/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

Annotation

Implementation Notes