Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""StringId Linter.
 
Checks that every StringId macro in the source encodes the string it claims to. The encoded value is written into the source so it
can be used in a switch, which means a hand-edited string and value can drift apart without the compiler noticing."""
 
####################################################################################################################################
import re
 
from common.error import ToolError
from common.log import *
from common.render import bld_str_id_seq
 
# Macro invocations to check, e.g. STRID5("test", 0x2a7250) or STRID6S("test", 1, 0x1e2a7250)
_STRID_EXP = re.compile(r"STRID(5|6)(S?)\([^)]+\)")
 
# The macro definitions themselves, which name their parameters rather than encoding a string
_STRID_DEFINE_LIST = (
    "STRID5(str, strId)",
    "STRID5S(str, seq, strId)",
    "STRID6(str, strId)",
    "STRID6S(str, seq, strId)",
)
 
 
####################################################################################################################################
def lint_str_id(source):
    """Check the StringId macros in a source file and return the number of errors found."""
 
    result = 0
 
    for match in _STRID_EXP.finditer(source):
        text = match.group(0)
        param_list = [param.strip() for param in text[text.find("(") + 1 : -1].split(",")]
        param = param_list[0]
 
        # Skip macro definitions
        if text in _STRID_DEFINE_LIST:
            continue
 
        # Skip test strings, i.e. a quoted string inside a string
        if param.startswith('\\"') and param.endswith('\\"'):
            continue
 
        # Skip test values
        if len(param_list) > 1 and param_list[1].startswith("TEST_"):
            continue
 
        # Param must begin and end with a quote
        if not param.startswith('"') or not param.endswith('"'):
            log(WARN, "'%s' must have quotes around string parameter '%s'" % (text, param))
            result += 1
 
            continue
 
        # Check validity of the string
        try:
            if match.group(2) == "":
                expected = bld_str_id_seq(param[1:-1])
            else:
                expected = bld_str_id_seq(param[1:-1], int(param_list[1]))
        except ToolError as error:
            log(WARN, "'%s' is not valid: %s" % (text, error))
            result += 1
 
            continue
 
        if text != expected:
            log(WARN, "'%s' should be '%s'" % (text, expected))
            result += 1
 
    return result