tools/lib/python/abi/abi_regex.py

Source file repositories/reference/linux-study-clean/tools/lib/python/abi/abi_regex.py

File Facts

System
Linux kernel
Corpus path
tools/lib/python/abi/abi_regex.py
Extension
.py
Size
8430 bytes
Lines
249
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: tools
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
# xxpylint: disable=R0903
# Copyright(c) 2025: Mauro Carvalho Chehab <mchehab@kernel.org>.
# SPDX-License-Identifier: GPL-2.0

"""
Convert ABI what into regular expressions
"""

import re
import sys

from pprint import pformat

from abi.abi_parser import AbiParser
from abi.helpers import AbiDebug

class AbiRegex(AbiParser):
    """
    Extends AbiParser to search ABI nodes with regular expressions.

    There some optimizations here to allow a quick symbol search:
    instead of trying to place all symbols altogether an doing linear
    search which is very time consuming, create a tree with one depth,
    grouping similar symbols altogether.

    Yet, sometimes a full search will be needed, so we have a special branch
    on such group tree where other symbols are placed.
    """

    #: Escape only ASCII visible characters.
    escape_symbols = r"([\x21-\x29\x2b-\x2d\x3a-\x40\x5c\x60\x7b-\x7e])"

    #: Special group for other nodes.
    leave_others = "others"

    # Tuples with regular expressions to be compiled and replacement data
    re_whats = [
        # Drop escape characters that might exist
        (re.compile("\\\\"), ""),

        # Temporarily escape dot characters
        (re.compile(r"\."),  "\xf6"),

        # Temporarily change [0-9]+ type of patterns
        (re.compile(r"\[0\-9\]\+"),  "\xff"),

        # Temporarily change [\d+-\d+] type of patterns
        (re.compile(r"\[0\-\d+\]"),  "\xff"),
        (re.compile(r"\[0:\d+\]"),  "\xff"),
        (re.compile(r"\[(\d+)\]"),  "\xf4\\\\d+\xf5"),

        # Temporarily change [0-9] type of patterns
        (re.compile(r"\[(\d)\-(\d)\]"),  "\xf4\1-\2\xf5"),

        # Handle multiple option patterns
        (re.compile(r"[\{\<\[]([\w_]+)(?:[,|]+([\w_]+)){1,}[\}\>\]]"), r"(\1|\2)"),

        # Handle wildcards
        (re.compile(r"([^\/])\*"), "\\1\\\\w\xf7"),
        (re.compile(r"/\*/"), "/.*/"),
        (re.compile(r"/\xf6\xf6\xf6"), "/.*"),
        (re.compile(r"\<[^\>]+\>"), "\\\\w\xf7"),
        (re.compile(r"\{[^\}]+\}"), "\\\\w\xf7"),
        (re.compile(r"\[[^\]]+\]"), "\\\\w\xf7"),

        (re.compile(r"XX+"), "\\\\w\xf7"),
        (re.compile(r"([^A-Z])[XYZ]([^A-Z])"), "\\1\\\\w\xf7\\2"),
        (re.compile(r"([^A-Z])[XYZ]$"), "\\1\\\\w\xf7"),
        (re.compile(r"_[AB]_"), "_\\\\w\xf7_"),

Annotation

Implementation Notes