-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathelastos_cast_checker.py
More file actions
186 lines (156 loc) · 6.35 KB
/
elastos_cast_checker.py
File metadata and controls
186 lines (156 loc) · 6.35 KB
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
# elastos_cast_checker.py
# encoding: UTF-8
# usages:
# sudo chmod a+x elastos_cast_checker.py
# python elastos_cast_checker.py
import re
import os
import sys
def read_file(path):
lines = []
if (path.endswith('.cpp') or path.endswith('.h')):
if(os.path.isfile(path)):
handle = open(path, 'r')
for line in handle:
lines.append(line.strip())
handle.close()
return lines
def find_declare_match(param, line):
pattern = re.compile(r'AutoPtr\s*<(.*)>\s*(.*)'+'[, ]'+param+'[; ,]')
return pattern.search(line)
def check_declare_match(usedType, param, declLine):
pattern = re.compile(r'AutoPtr\s*<\s*'+usedType+'\s*>\s*(.*)'+'[, ]'+param+'[; ,]')
return pattern.search(declLine)
def find_declare_line(param, lines, lineIndex):
if len(lines) == 0:
return -1
for i in range(lineIndex, 0, -1):
line = lines[i]
if (len(line) > 1) and (line.startswith("//") == False):
match = find_declare_match(param, line)
if match:
#print line, 'match', match.group()
return i;
return -1
def check_match(firstLog, logFile, cppFilepath, usedMatch, usedLineNum, declLine, declLineNum, isHeader = True):
usedType = usedMatch.group(2)
param = usedMatch.group(4)
matchInfo = usedMatch.group()
match = check_declare_match(usedType, param, declLine)
if match == None:
if firstLog:
firstLog = False
logInfo ='\n>> process file: ' + cppFilepath + '\n'
logFile.write(logInfo)
print logInfo
fileInfo = ''
if isHeader:
fileInfo = 'in .h file'
logInfo = " > error: invalid using of {0} at line {1:d}, it is declared as {2} '{3}' at line {4:d}.\n" \
.format(matchInfo, usedLineNum + 1, declLine, fileInfo, declLineNum + 1)
logFile.write(logInfo)
print logInfo
else:
#print 'match ', matchInfo, declLine
return firstLog
def process_declare_line_in_header(logFile, firstLog, cppFilepath, match, lines, lineNum, headerFilepath):
headerLines = read_file(headerFilepath)
param = match.group(4)
matchInfo = match.group()
declLineNum = find_declare_line(param, headerLines, len(headerLines)-1)
if (declLineNum != -1):
declLine = headerLines[declLineNum]
#print 'declLine', declLine
firstLog = check_match(firstLog, logFile, cppFilepath, match, lineNum, declLine, declLineNum)
else:
logInfo = ''
if firstLog:
firstLog = False
logInfo ='\n>> process file: ' + cppFilepath + '\n'
logFile.write(logInfo)
print logInfo
if param.startswith('m'):
logInfo = " = warning: declaration for {0} at line {1:d} not found! is it declared in super class's .h file?\n".format(matchInfo, lineNum + 1)
else:
logInfo = " = warning: declaration for {0} at line {1:d} not found!\n".format(matchInfo, lineNum + 1)
logFile.write(logInfo)
print logInfo
return firstLog
def process_file(path, logFile):
if path.endswith('.cpp') == False:
return
firstLog = True;
lines = read_file(path)
lineNum = 0
for eachLine in lines:
if (len(eachLine) > 1) and (eachLine.startswith("//") == False):
pattern = re.compile(r'(\()(I\w*)(\*\*\)&)([a-zA-Z]\w*)(\))')
match = pattern.search(eachLine)
if match:
#print match.group() match.groups()
#print match.group(2), match.group(4)
usedType = match.group(2)
param = match.group(4)
# do not check weak-reference Resolve
if usedType == 'IInterface' and eachLine.find('->Resolve(') != -1:
pass
else:
declLineNum = find_declare_line(param, lines, lineNum)
if (declLineNum != -1):
declLine = lines[declLineNum]
#print 'declLine', declLine
firstLog = check_match(firstLog, logFile, path, match, lineNum, declLine, declLineNum, False)
else:
headerFilepath = path.replace("/src/", "/inc/").replace(".cpp", ".h")
firstLog = process_declare_line_in_header(logFile, firstLog, path, match, lines, lineNum, headerFilepath)
lineNum = lineNum +1
def process_dir(path, logFile):
listfile = os.listdir(path)
for filename in listfile:
filepath = path + '/' + filename
if(os.path.isdir(filepath)):
# exclude hidden dirs
if(filename[0] == '.'):
pass
else:
process_dir(filepath, logFile)
elif(os.path.isfile(filepath)):
process_file(filepath, logFile)
def summarize_log(logPath):
if(os.path.isfile(logPath)):
errorCount = 0
warningCount = 0
# summarize
logFile = open(logPath, 'r')
for line in logFile:
line = line.strip()
if line.startswith('> error:') == True:
errorCount = errorCount + 1
elif line.startswith('= warning:') == True:
warningCount = warningCount + 1
logFile.close()
# log
logFile = open(logPath, 'a')
logInfo = '\ntotal: {0:d} errors, {1:d} warnings.'.format(errorCount, warningCount)
logFile.write(logInfo)
print logInfo
logFile.close()
def process(path, logPath):
if(os.path.isfile(logPath)):
os.remove(logPath)
logFile = open(logPath, 'a')
print 'output to', logPath
if(os.path.isdir(path)):
process_dir(path, logFile)
elif(os.path.isfile(path)):
process_file(path, logFile)
else:
print 'invalid path:', path
logFile.close()
summarize_log(logPath)
#process('/home/kesalin/test/python/test.cpp', 'elastos_cast_checker.log')
#total: 2 errors, 10 warnings.
#process('/home/kesalin/Elastos5/Sources/Elastos/LibCore/src', '/home/kesalin/elastos_cast_checker.log')
#process('/home/kesalin/Elastos5/Sources/Elastos/Frameworks/Droid/Base/Core/src/', '/home/kesalin/elastos_cast_checker.log')
#total: 7 errors, 0 warnings.
process('/home/kesalin/Elastos5/Sources/Elastos/Frameworks/Droid/Base/Services/Server/src', '/home/kesalin/elastos_cast_checker.log')