summaryrefslogtreecommitdiffstats
path: root/qa/coccinelle/struct-flags.py
blob: 3186cf6bd0c27139d4ed13d13663f53a00521119 (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python
import re
import sys
import os
from string import Template

if len(sys.argv) == 2:
    SRC_DIR = sys.argv[1]
else:
    SRC_DIR = "../../src/"


class Structure:
    def __init__(self, string):
        (self.struct, self.flags, self.values) = string.split(":")

class SetterGetter:
    def __init__(self, string):
        (function, params, self.value) = string.split(":")
        self.function = function.strip("()")
        self.params = [int(a) for a in params.split(",")]


cmd = "grep -h coccinelle ../../src/*[ch] | sed -e 's/.*coccinelle: \(.*\) \*\//\1/'"

struct_list = []
setter_getter_list = []

dirList = os.listdir(SRC_DIR)
for fname in dirList:
    if re.search("\.[ch]$", fname):
        for line in open(os.path.join(SRC_DIR, fname)):
            if "coccinelle:" in line:
                m = re.search("coccinelle: (.*) \*\/", line)
                if "()" not in m.group(1):
                    struct = Structure(m.group(1))
                    struct_list.append(struct)
                else:
                    function = SetterGetter(m.group(1))
                    setter_getter_list.append(function)

header = "@flags@"
body = []

# Handle setter and getter
setter_getter = [x.function for x in setter_getter_list]
if len(setter_getter):
    header += "\nidentifier NotSetterGetter !~ \"^(%s)$\";" % ("|".join(setter_getter))

i = 0
for struct in struct_list:
    header += """
%s *struct%d;
identifier struct_flags%d =~ "^(?!%s).+";""" % (struct.struct, i, i, struct.values)

    body.append("""
struct%d->%s@p1 |= struct_flags%d
|
struct%d->%s@p1 & struct_flags%d
|
struct%d->%s@p1 &= ~struct_flags%d
""" % (i, struct.flags, i, i, struct.flags, i, i, struct.flags, i))

    i += 1

print(header)
print("position p1;")
print("@@")
print("")
print("""
NotSetterGetter(...)
{
    <...
""")
print("")
print("(" + "|".join(body) + ")")
print("")
print("""
...>
}

@script:python@
p1 << flags.p1;
@@

print "Invalid usage of flags field at %s:%s, flags value is incorrect (wrong family)." % (p1[0].file, p1[0].line)
import sys
sys.exit(1)""")

i = 1
setter_template = """
@settergetter${i}@
identifier SetterGetter =~ "${function}";
identifier f_flags =~ "^(?!${value}).+";
identifier ${params_line};
position p1;
@@

SetterGetter@p1(${prefix_param}f_flags${suffix_param})

@script:python@
p1 << settergetter${i}.p1;
@@
print "Invalid usage of ${function} at %s:%s, flags value is incorrect (wrong family)." % (p1[0].file, p1[0].line)
import sys
sys.exit(1)
"""

for sg in setter_getter_list:
    prefix_param = ""
    for index in list(range(1, sg.params[1])):
        prefix_param += "param%d, " % (index)
    if sg.params[1] < sg.params[0]:
        suffix_param = ", " + ", ".join(["param%d" % (index + 1) for index in list(range(sg.params[1], sg.params[0]))])
    else:
        suffix_param = ""
    params_elts = list(range(1, sg.params[1])) + list(range(sg.params[1] + 1, sg.params[0] + 1))
    params_line = ", ".join(["param%d" % (x) for x in params_elts])
    print(Template(setter_template).substitute(i=i, function=sg.function, value=sg.value,
                prefix_param=prefix_param, suffix_param=suffix_param, params_line=params_line))
    i += 1