summaryrefslogtreecommitdiff
path: root/tools/validateinterfaces.py
blob: c9a86979cb759d9ca6e7fda5a3f73fb696606b8f (plain) (blame)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#! /usr/bin/env python
# -*- coding: utf8 -*-
#
# Copyright (C) 2014  Andrei Karas (4144)

import os
import re
import sys
from sets import Set

interfaceRe = re.compile("struct (?P<name1>[a-z_]+)_interface (?P<name2>[a-z_]+)_s;")

class Tracker:
    pass

def searchDefault(r, ifname):
    defaultStr = "void {0}_defaults(void)".format(ifname);
    for line in r:
        if line.find(defaultStr) == 0:
            return True
    return False

def searchStructStart(r, ifname):
    for line in r:
        if line.find("struct {0}_interface".format(ifname)) == 0:
            return True
    return False

def readCFile(tracker, cFile):
    methods = Set()
    shortIfName = ""
    with open(cFile, "r") as r:
        for line in r:
#            print "cline1: " + line
            m = interfaceRe.search(line)
            if m != None and m.group("name1") == m.group("name2"):
                # found C file with interface
                ifname = m.group("name1")
                if searchDefault(r, ifname) == False:
                    return (None, shortIfName, methods)
                lineRe = re.compile("(?P<ifname>[a-z_]+)->(?P<method>[\w_]+)[ ][=][ ](?P<fullmethod>[^;]+);")
                for line in r:
#                    print "cline2: " + line
                    test = line.strip()
                    if len(test) > 2 and test[0:2] == "//":
                        continue
                    if len(line) > 0 and line[0] == "}":
                        break
                    m = lineRe.search(line)
                    if m != None:
                        tmp = m.group("ifname")
                        if len(tmp) < 2 or tmp[0] != ifname[0]:
                            continue
                        if shortIfName == "":
                            shortIfName = m.group("ifname")
#                        print "{2}: add {0}, from: {1}".format(m.group("method"), line, ifname)
                        methods.add(m.group("method"))
                        tracker.interfaces.add(ifname);
                        tracker.fullmethods.add(m.group("fullmethod"));
                return (ifname, shortIfName, methods)
    return (None, shortIfName, methods)

def readHFile(tracker, hFile, ifname):
    methods = Set()
    with open(hFile, "r") as r:
        if searchStructStart(r, ifname) == False:
            return methods
        lineRe = re.compile("[(][*](?P<method>[^)]+)[)]".format(ifname))
        for line in r:
#            print "hline: " + line
            test = line.strip()
            if len(test) > 2 and test[0:2] == "//":
                continue
            if len(line) > 0 and line[0] == "}":
                break
            m = lineRe.search(line)
            if m != None:
#                print "{2}: add {0}, from: {1}".format(m.group("method"), line, ifname)
                methods.add(m.group("method"))
                tracker.fullmethods.add(ifname + "_" + m.group("method"))
    return methods

def checkIfFile(tracker, cFile, hFile):
    data = readCFile(tracker, cFile)
    cMethods = data[2]
    ifname = data[0]
    shortIfName = data[1]
    if len(cMethods) > 0:
        hMethods = readHFile(tracker, hFile, ifname)
        for method in hMethods:
            tracker.arr[ifname + "_" + method] = list()
            tracker.methods.add(ifname + "_" + method)
            if method not in cMethods:
                print "Missing initialisation in file {0}: {1}".format(cFile, method)
                tracker.retCode = 1
#        for method in cMethods:
#            if method not in hMethods:
#                print "Extra method in file {0}: {1}".format(cFile, method)

def processIfDir(tracker, srcDir):
    files = os.listdir(srcDir)
    for file1 in files:
        if file1[0] == '.' or file1 == "..":
            continue
        cPath = os.path.abspath(srcDir + os.path.sep + file1)
        if not os.path.isfile(cPath):
            processIfDir(tracker, cPath)
        else:
            if file1[-2:] == ".c":
                file2 = file1[:-2] + ".h"
                hPath = srcDir + os.path.sep + file2;
                if os.path.exists(hPath) and os.path.isfile(hPath):
                    checkIfFile(tracker, cPath, hPath)


def checkChr(ch):
    if (ch >= "a" and ch <= "z") or ch == "_" or (ch >= "0" and ch <= "9" or ch == "\"" or ch == ">"):
        return True
    return False

def checkFile(tracker, cFile):
#    print "Checking: " + cFile
    with open(cFile, "r") as r:
        for line in r:
            parts = re.findall(r'[\w_]+', line)
            for part in parts:
                if part in tracker.methods:
                    idx = line.find(part)
                    if idx > 0:
                        if idx + len(part) >= len(line):
                            continue
                        if checkChr(line[idx + len(part)]):
                            continue
                        if checkChr(line[idx - 1]):
                            continue
                        if line[-1] == "\n":
                            line = line[:-1]
                        tracker.arr[part].append(line)

def processDir(tracker, srcDir):
    files = os.listdir(srcDir)
    for file1 in files:
        if file1[0] == '.' or file1 == "..":
            continue
        cPath = os.path.abspath(srcDir + os.path.sep + file1)
        if not os.path.isfile(cPath):
            processDir(tracker, cPath)
        elif file1[-2:] == ".c" or file1[-2:] == ".h":
#        elif file1[-2:] == ".c":
            checkFile(tracker, cPath)

def reportMethods(tracker):
    print "\n"
    for method in tracker.methods:
        if len(tracker.arr[method]) > 2:
            print method
            for t in tracker.arr[method]:
                print t
            print "\n"


def checkLostFile(tracker, cFile):
#    print "Checking: " + cFile
    methodRe = re.compile("^([\w0-9* _]*)([ ]|[*])(?P<ifname>[a-z_]+)_(?P<method>[\w_]+)(|[ ])[(]")
    with open(cFile, "r") as r:
        for line in r:
            if line.find("(") < 1 or len(line) < 3 or line[0] == "\t" or line[0] == " " or line.find("_defaults") > 0:
                continue
            m = methodRe.search(line)
            if m != None:
                name = "{0}_{1}".format(m.group("ifname"), m.group("method"))
                if m.group("ifname") not in tracker.interfaces:
                    continue
                if name not in tracker.fullmethods:
#                    print "src  : " + line
                    print name

def processLostDir(tracker, srcDir):
    files = os.listdir(srcDir)
    for file1 in files:
        if file1[0] == '.' or file1 == "..":
            continue
        cPath = os.path.abspath(srcDir + os.path.sep + file1)
        if not os.path.isfile(cPath):
            processLostDir(tracker, cPath)
        elif file1[-2:] == ".c":
            checkLostFile(tracker, cPath)

def runIf():
    processIfDir(tracker, "../src/char");
    processIfDir(tracker, "../src/map");
    processIfDir(tracker, "../src/login");
    processIfDir(tracker, "../src/common");

def runLost():
    processLostDir(tracker, "../src/char");
    processLostDir(tracker, "../src/map");
    processLostDir(tracker, "../src/login");
    processLostDir(tracker, "../src/common");

def runLong():
    processDir(tracker, "../src/char");
    processDir(tracker, "../src/map");
    processDir(tracker, "../src/login");
    processDir(tracker, "../src/common");
    reportMethods(tracker)

tracker = Tracker()
tracker.arr = dict()
tracker.methods = Set()
tracker.fullmethods = Set()
tracker.interfaces = Set()
tracker.retCode = 0

if len(sys.argv) > 1:
    cmd = sys.argv[1]
else:
    cmd = "default"

if cmd == "silent":
    runIf()
elif cmd == "init":
    print "Checking interfaces initialisation"
    runIf()
elif cmd == "lost":
    print "Checking not added functions to interfaces"
    runLost();
elif cmd == "long":
    print "Checking interfaces usage"
    runLong();
else:
    print "Checking interfaces initialisation"
    runIf()
    print "Checking not added functions to interfaces"
    runLost();
    print "Checking interfaces usage"
    runLong();
exit(tracker.retCode)