Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) #!/usr/bin/env python3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) # SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) """Find Kconfig symbols that are referenced but not defined."""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) # (c) 2014-2017 Valentin Rothberg <valentinrothberg@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) # (c) 2014 Stefan Hengelein <stefan.hengelein@fau.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) import argparse
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) import difflib
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) import os
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) import re
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) import signal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) import subprocess
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) import sys
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) from multiprocessing import Pool, cpu_count
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) # regex expressions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) OPERATORS = r"&|\(|\)|\||\!"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) SYMBOL = r"(?:\w*[A-Z0-9]\w*){2,}"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) DEF = r"^\s*(?:menu){,1}config\s+(" + SYMBOL + r")\s*"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) EXPR = r"(?:" + OPERATORS + r"|\s|" + SYMBOL + r")+"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) DEFAULT = r"default\s+.*?(?:if\s.+){,1}"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) STMT = r"^\s*(?:if|select|imply|depends\s+on|(?:" + DEFAULT + r"))\s+" + EXPR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) SOURCE_SYMBOL = r"(?:\W|\b)+[D]{,1}CONFIG_(" + SYMBOL + r")"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) # regex objects
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) REGEX_FILE_KCONFIG = re.compile(r".*Kconfig[\.\w+\-]*$")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) REGEX_SYMBOL = re.compile(r'(?!\B)' + SYMBOL + r'(?!\B)')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) REGEX_SOURCE_SYMBOL = re.compile(SOURCE_SYMBOL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) REGEX_KCONFIG_DEF = re.compile(DEF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) REGEX_KCONFIG_EXPR = re.compile(EXPR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) REGEX_KCONFIG_STMT = re.compile(STMT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) REGEX_KCONFIG_HELP = re.compile(r"^\s+help\s*$")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) REGEX_FILTER_SYMBOLS = re.compile(r"[A-Za-z0-9]$")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) REGEX_NUMERIC = re.compile(r"0[xX][0-9a-fA-F]+|[0-9]+")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) REGEX_QUOTES = re.compile("(\"(.*?)\")")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) def parse_options():
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)     """The user interface of this module."""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)     usage = "Run this tool to detect Kconfig symbols that are referenced but " \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)             "not defined in Kconfig.  If no option is specified, "             \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)             "checkkconfigsymbols defaults to check your current tree.  "       \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)             "Please note that specifying commits will 'git reset --hard\' "    \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)             "your current tree!  You may save uncommitted changes to avoid "   \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)             "losing data."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)     parser = argparse.ArgumentParser(description=usage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)     parser.add_argument('-c', '--commit', dest='commit', action='store',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)                         default="",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)                         help="check if the specified commit (hash) introduces "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)                              "undefined Kconfig symbols")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)     parser.add_argument('-d', '--diff', dest='diff', action='store',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)                         default="",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61)                         help="diff undefined symbols between two commits "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62)                              "(e.g., -d commmit1..commit2)")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64)     parser.add_argument('-f', '--find', dest='find', action='store_true',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65)                         default=False,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66)                         help="find and show commits that may cause symbols to be "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67)                              "missing (required to run with --diff)")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69)     parser.add_argument('-i', '--ignore', dest='ignore', action='store',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70)                         default="",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71)                         help="ignore files matching this Python regex "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72)                              "(e.g., -i '.*defconfig')")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74)     parser.add_argument('-s', '--sim', dest='sim', action='store', default="",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75)                         help="print a list of max. 10 string-similar symbols")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)     parser.add_argument('--force', dest='force', action='store_true',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78)                         default=False,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79)                         help="reset current Git tree even when it's dirty")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81)     parser.add_argument('--no-color', dest='color', action='store_false',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82)                         default=True,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83)                         help="don't print colored output (default when not "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84)                              "outputting to a terminal)")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86)     args = parser.parse_args()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)     if args.commit and args.diff:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89)         sys.exit("Please specify only one option at once.")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91)     if args.diff and not re.match(r"^[\w\-\.\^]+\.\.[\w\-\.\^]+$", args.diff):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92)         sys.exit("Please specify valid input in the following format: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93)                  "\'commit1..commit2\'")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95)     if args.commit or args.diff:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96)         if not args.force and tree_is_dirty():
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97)             sys.exit("The current Git tree is dirty (see 'git status').  "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98)                      "Running this script may\ndelete important data since it "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99)                      "calls 'git reset --hard' for some performance\nreasons. "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)                      " Please run this script in a clean Git tree or pass "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)                      "'--force' if you\nwant to ignore this warning and "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)                      "continue.")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)     if args.commit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)         args.find = False
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)     if args.ignore:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)         try:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)             re.match(args.ignore, "this/is/just/a/test.c")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)         except:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)             sys.exit("Please specify a valid Python regex.")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)     return args
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) def main():
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)     """Main function of this module."""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)     args = parse_options()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)     global COLOR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)     COLOR = args.color and sys.stdout.isatty()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)     if args.sim and not args.commit and not args.diff:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)         sims = find_sims(args.sim, args.ignore)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)         if sims:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)             print("%s: %s" % (yel("Similar symbols"), ', '.join(sims)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)         else:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)             print("%s: no similar symbols found" % yel("Similar symbols"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)         sys.exit(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)     # dictionary of (un)defined symbols
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)     defined = {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)     undefined = {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)     if args.commit or args.diff:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)         head = get_head()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)         # get commit range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)         commit_a = None
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)         commit_b = None
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)         if args.commit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)             commit_a = args.commit + "~"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)             commit_b = args.commit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)         elif args.diff:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)             split = args.diff.split("..")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)             commit_a = split[0]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)             commit_b = split[1]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)             undefined_a = {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)             undefined_b = {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)         # get undefined items before the commit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)         reset(commit_a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)         undefined_a, _ = check_symbols(args.ignore)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)         # get undefined items for the commit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)         reset(commit_b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)         undefined_b, defined = check_symbols(args.ignore)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)         # report cases that are present for the commit but not before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)         for symbol in sorted(undefined_b):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)             # symbol has not been undefined before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)             if symbol not in undefined_a:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)                 files = sorted(undefined_b.get(symbol))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)                 undefined[symbol] = files
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)             # check if there are new files that reference the undefined symbol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)             else:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)                 files = sorted(undefined_b.get(symbol) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)                                undefined_a.get(symbol))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)                 if files:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)                     undefined[symbol] = files
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)         # reset to head
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)         reset(head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)     # default to check the entire tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)     else:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)         undefined, defined = check_symbols(args.ignore)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)     # now print the output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)     for symbol in sorted(undefined):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)         print(red(symbol))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)         files = sorted(undefined.get(symbol))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)         print("%s: %s" % (yel("Referencing files"), ", ".join(files)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)         sims = find_sims(symbol, args.ignore, defined)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)         sims_out = yel("Similar symbols")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)         if sims:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)             print("%s: %s" % (sims_out, ', '.join(sims)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)         else:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)             print("%s: %s" % (sims_out, "no similar symbols found"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)         if args.find:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)             print("%s:" % yel("Commits changing symbol"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)             commits = find_commits(symbol, args.diff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)             if commits:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)                 for commit in commits:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)                     commit = commit.split(" ", 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)                     print("\t- %s (\"%s\")" % (yel(commit[0]), commit[1]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)             else:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)                 print("\t- no commit found")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)         print()  # new line
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) def reset(commit):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)     """Reset current git tree to %commit."""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)     execute(["git", "reset", "--hard", commit])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) def yel(string):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)     """
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)     Color %string yellow.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)     """
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)     return "\033[33m%s\033[0m" % string if COLOR else string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) def red(string):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)     """
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)     Color %string red.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)     """
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)     return "\033[31m%s\033[0m" % string if COLOR else string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) def execute(cmd):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)     """Execute %cmd and return stdout.  Exit in case of error."""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)     try:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)         stdout = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=False)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)         stdout = stdout.decode(errors='replace')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)     except subprocess.CalledProcessError as fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)         exit(fail)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)     return stdout
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) def find_commits(symbol, diff):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)     """Find commits changing %symbol in the given range of %diff."""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)     commits = execute(["git", "log", "--pretty=oneline",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)                        "--abbrev-commit", "-G",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)                        symbol, diff])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)     return [x for x in commits.split("\n") if x]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) def tree_is_dirty():
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)     """Return true if the current working tree is dirty (i.e., if any file has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)     been added, deleted, modified, renamed or copied but not committed)."""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)     stdout = execute(["git", "status", "--porcelain"])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)     for line in stdout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)         if re.findall(r"[URMADC]{1}", line[:2]):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)             return True
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)     return False
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) def get_head():
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)     """Return commit hash of current HEAD."""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)     stdout = execute(["git", "rev-parse", "HEAD"])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)     return stdout.strip('\n')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) def partition(lst, size):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)     """Partition list @lst into eveni-sized lists of size @size."""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)     return [lst[i::size] for i in range(size)]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) def init_worker():
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)     """Set signal handler to ignore SIGINT."""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)     signal.signal(signal.SIGINT, signal.SIG_IGN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) def find_sims(symbol, ignore, defined=[]):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)     """Return a list of max. ten Kconfig symbols that are string-similar to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)     @symbol."""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)     if defined:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)         return difflib.get_close_matches(symbol, set(defined), 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)     pool = Pool(cpu_count(), init_worker)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)     kfiles = []
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)     for gitfile in get_files():
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)         if REGEX_FILE_KCONFIG.match(gitfile):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)             kfiles.append(gitfile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)     arglist = []
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)     for part in partition(kfiles, cpu_count()):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)         arglist.append((part, ignore))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)     for res in pool.map(parse_kconfig_files, arglist):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)         defined.extend(res[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)     return difflib.get_close_matches(symbol, set(defined), 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) def get_files():
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)     """Return a list of all files in the current git directory."""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)     # use 'git ls-files' to get the worklist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)     stdout = execute(["git", "ls-files"])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)     if len(stdout) > 0 and stdout[-1] == "\n":
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)         stdout = stdout[:-1]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)     files = []
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)     for gitfile in stdout.rsplit("\n"):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)         if ".git" in gitfile or "ChangeLog" in gitfile or      \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)                 ".log" in gitfile or os.path.isdir(gitfile) or \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)                 gitfile.startswith("tools/"):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)             continue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)         files.append(gitfile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)     return files
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) def check_symbols(ignore):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)     """Find undefined Kconfig symbols and return a dict with the symbol as key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)     and a list of referencing files as value.  Files matching %ignore are not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)     checked for undefined symbols."""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)     pool = Pool(cpu_count(), init_worker)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)     try:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)         return check_symbols_helper(pool, ignore)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)     except KeyboardInterrupt:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)         pool.terminate()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)         pool.join()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)         sys.exit(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) def check_symbols_helper(pool, ignore):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)     """Helper method for check_symbols().  Used to catch keyboard interrupts in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)     check_symbols() in order to properly terminate running worker processes."""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)     source_files = []
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)     kconfig_files = []
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)     defined_symbols = []
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)     referenced_symbols = dict()  # {file: [symbols]}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)     for gitfile in get_files():
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)         if REGEX_FILE_KCONFIG.match(gitfile):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)             kconfig_files.append(gitfile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)         else:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)             if ignore and not re.match(ignore, gitfile):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)                 continue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)             # add source files that do not match the ignore pattern
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)             source_files.append(gitfile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)     # parse source files
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)     arglist = partition(source_files, cpu_count())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)     for res in pool.map(parse_source_files, arglist):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)         referenced_symbols.update(res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)     # parse kconfig files
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)     arglist = []
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)     for part in partition(kconfig_files, cpu_count()):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)         arglist.append((part, ignore))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)     for res in pool.map(parse_kconfig_files, arglist):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)         defined_symbols.extend(res[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)         referenced_symbols.update(res[1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)     defined_symbols = set(defined_symbols)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)     # inverse mapping of referenced_symbols to dict(symbol: [files])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)     inv_map = dict()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)     for _file, symbols in referenced_symbols.items():
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)         for symbol in symbols:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)             inv_map[symbol] = inv_map.get(symbol, set())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)             inv_map[symbol].add(_file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)     referenced_symbols = inv_map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)     undefined = {}  # {symbol: [files]}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)     for symbol in sorted(referenced_symbols):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)         # filter some false positives
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)         if symbol == "FOO" or symbol == "BAR" or \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)                 symbol == "FOO_BAR" or symbol == "XXX":
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)             continue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)         if symbol not in defined_symbols:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)             if symbol.endswith("_MODULE"):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)                 # avoid false positives for kernel modules
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)                 if symbol[:-len("_MODULE")] in defined_symbols:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)                     continue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)             undefined[symbol] = referenced_symbols.get(symbol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)     return undefined, defined_symbols
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) def parse_source_files(source_files):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)     """Parse each source file in @source_files and return dictionary with source
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)     files as keys and lists of references Kconfig symbols as values."""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)     referenced_symbols = dict()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)     for sfile in source_files:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)         referenced_symbols[sfile] = parse_source_file(sfile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)     return referenced_symbols
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) def parse_source_file(sfile):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)     """Parse @sfile and return a list of referenced Kconfig symbols."""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)     lines = []
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)     references = []
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)     if not os.path.exists(sfile):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)         return references
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)     with open(sfile, "r", encoding='utf-8', errors='replace') as stream:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)         lines = stream.readlines()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)     for line in lines:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)         if "CONFIG_" not in line:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)             continue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)         symbols = REGEX_SOURCE_SYMBOL.findall(line)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)         for symbol in symbols:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)             if not REGEX_FILTER_SYMBOLS.search(symbol):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)                 continue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)             references.append(symbol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)     return references
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) def get_symbols_in_line(line):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)     """Return mentioned Kconfig symbols in @line."""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)     return REGEX_SYMBOL.findall(line)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) def parse_kconfig_files(args):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)     """Parse kconfig files and return tuple of defined and references Kconfig
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)     symbols.  Note, @args is a tuple of a list of files and the @ignore
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)     pattern."""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)     kconfig_files = args[0]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)     ignore = args[1]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)     defined_symbols = []
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)     referenced_symbols = dict()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)     for kfile in kconfig_files:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)         defined, references = parse_kconfig_file(kfile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)         defined_symbols.extend(defined)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)         if ignore and re.match(ignore, kfile):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)             # do not collect references for files that match the ignore pattern
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)             continue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)         referenced_symbols[kfile] = references
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)     return (defined_symbols, referenced_symbols)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) def parse_kconfig_file(kfile):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)     """Parse @kfile and update symbol definitions and references."""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)     lines = []
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)     defined = []
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)     references = []
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)     skip = False
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)     if not os.path.exists(kfile):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)         return defined, references
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)     with open(kfile, "r", encoding='utf-8', errors='replace') as stream:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)         lines = stream.readlines()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)     for i in range(len(lines)):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)         line = lines[i]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)         line = line.strip('\n')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)         line = line.split("#")[0]  # ignore comments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)         if REGEX_KCONFIG_DEF.match(line):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)             symbol_def = REGEX_KCONFIG_DEF.findall(line)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)             defined.append(symbol_def[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)             skip = False
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)         elif REGEX_KCONFIG_HELP.match(line):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)             skip = True
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)         elif skip:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)             # ignore content of help messages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)             pass
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)         elif REGEX_KCONFIG_STMT.match(line):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)             line = REGEX_QUOTES.sub("", line)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)             symbols = get_symbols_in_line(line)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)             # multi-line statements
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)             while line.endswith("\\"):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)                 i += 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)                 line = lines[i]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)                 line = line.strip('\n')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)                 symbols.extend(get_symbols_in_line(line))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)             for symbol in set(symbols):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)                 if REGEX_NUMERIC.match(symbol):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)                     # ignore numeric values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)                     continue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)                 references.append(symbol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)     return defined, references
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) if __name__ == "__main__":
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)     main()