Convert TestOldCompletionCallback in WebSocketJobTest.
[chromium-blink-merge.git] / ppapi / generators / idl_diff.py
blob9d1abbbe24407474ca17b0b559f43ce10aa0da1a
1 #!/usr/bin/python
3 # Copyright (c) 2011 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
7 import glob
8 import os
9 import subprocess
10 import sys
12 from idl_option import GetOption, Option, ParseOptions
13 from idl_outfile import IDLOutFile
15 # IDLDiff
17 # IDLDiff is a tool for comparing sets of IDL generated header files
18 # with the standard checked in headers. It does this by capturing the
19 # output of the standard diff tool, parsing it into separate changes, then
20 # ignoring changes that are know to be safe, such as adding or removing
21 # blank lines, etc...
24 Option('gen', 'IDL generated files', default='hdir')
25 Option('src', 'Original ".h" files', default='../c')
26 Option('halt', 'Stop if a difference is found')
27 Option('diff', 'Directory holding acceptable diffs', default='diff')
28 Option('ok', 'Write out the diff file.')
29 # Change
31 # A Change object contains the previous lines, new news and change type.
33 class Change(object):
34 def __init__(self, mode, was, now):
35 self.mode = mode
36 self.was = was
37 self.now = now
39 def Dump(self):
40 if not self.was:
41 print 'Adding %s' % self.mode
42 elif not self.now:
43 print 'Missing %s' % self.mode
44 else:
45 print 'Modifying %s' % self.mode
47 for line in self.was:
48 print 'src: >>%s<<' % line
49 for line in self.now:
50 print 'gen: >>%s<<' % line
51 print
54 # IsCopyright
56 # Return True if this change is only a one line change in the copyright notice
57 # such as non-matching years.
59 def IsCopyright(change):
60 if len(change.now) != 1 or len(change.was) != 1: return False
61 if 'Copyright (c)' not in change.now[0]: return False
62 if 'Copyright (c)' not in change.was[0]: return False
63 return True
66 # IsBlankComment
68 # Return True if this change only removes a blank line from a comment
70 def IsBlankComment(change):
71 if change.now: return False
72 if len(change.was) != 1: return False
73 if change.was[0].strip() != '*': return False
74 return True
77 # IsBlank
79 # Return True if this change only adds or removes blank lines
81 def IsBlank(change):
82 for line in change.now:
83 if line: return False
84 for line in change.was:
85 if line: return False
86 return True
90 # IsCppComment
92 # Return True if this change only going from C++ to C style
94 def IsToCppComment(change):
95 if not len(change.now) or len(change.now) != len(change.was):
96 return False
97 for index in range(len(change.now)):
98 was = change.was[index].strip()
99 if was[:2] != '//':
100 return False
101 was = was[2:].strip()
102 now = change.now[index].strip()
103 if now[:2] != '/*':
104 return False
105 now = now[2:-2].strip()
106 if now != was:
107 return False
108 return True
111 return True
113 def IsMergeComment(change):
114 if len(change.was) != 1: return False
115 if change.was[0].strip() != '*': return False
116 for line in change.now:
117 stripped = line.strip()
118 if stripped != '*' and stripped[:2] != '/*' and stripped[-2:] != '*/':
119 return False
120 return True
122 # IsSpacing
124 # Return True if this change is only different in the way 'words' are spaced
125 # such as in an enum:
126 # ENUM_XXX = 1,
127 # ENUM_XYY_Y = 2,
128 # vs
129 # ENUM_XXX = 1,
130 # ENUM_XYY_Y = 2,
132 def IsSpacing(change):
133 if len(change.now) != len(change.was): return False
134 for i in range(len(change.now)):
135 # Also ignore right side comments
136 line = change.was[i]
137 offs = line.find('//')
138 if offs == -1:
139 offs = line.find('/*')
140 if offs >-1:
141 line = line[:offs-1]
143 words1 = change.now[i].split()
144 words2 = line.split()
145 if words1 != words2: return False
146 return True
149 # IsInclude
151 # Return True if change has extra includes
153 def IsInclude(change):
154 for line in change.was:
155 if line.strip().find('struct'): return False
156 for line in change.now:
157 if line and '#include' not in line: return False
158 return True
161 # IsCppComment
163 # Return True if the change is only missing C++ comments
165 def IsCppComment(change):
166 if len(change.now): return False
167 for line in change.was:
168 line = line.strip()
169 if line[:2] != '//': return False
170 return True
172 # ValidChange
174 # Return True if none of the changes does not patch an above "bogus" change.
176 def ValidChange(change):
177 if IsToCppComment(change): return False
178 if IsCopyright(change): return False
179 if IsBlankComment(change): return False
180 if IsMergeComment(change): return False
181 if IsBlank(change): return False
182 if IsSpacing(change): return False
183 if IsInclude(change): return False
184 if IsCppComment(change): return False
185 return True
189 # Swapped
191 # Check if the combination of last + next change signals they are both
192 # invalid such as swap of line around an invalid block.
194 def Swapped(last, next):
195 if not last.now and not next.was and len(last.was) == len(next.now):
196 cnt = len(last.was)
197 for i in range(cnt):
198 match = True
199 for j in range(cnt):
200 if last.was[j] != next.now[(i + j) % cnt]:
201 match = False
202 break;
203 if match: return True
204 if not last.was and not next.now and len(last.now) == len(next.was):
205 cnt = len(last.now)
206 for i in range(cnt):
207 match = True
208 for j in range(cnt):
209 if last.now[i] != next.was[(i + j) % cnt]:
210 match = False
211 break;
212 if match: return True
213 return False
216 def FilterLinesIn(output):
217 was = []
218 now = []
219 filter = []
220 for index in range(len(output)):
221 filter.append(False)
222 line = output[index]
223 if len(line) < 2: continue
224 if line[0] == '<':
225 if line[2:].strip() == '': continue
226 was.append((index, line[2:]))
227 elif line[0] == '>':
228 if line[2:].strip() == '': continue
229 now.append((index, line[2:]))
230 for windex, wline in was:
231 for nindex, nline in now:
232 if filter[nindex]: continue
233 if filter[windex]: continue
234 if wline == nline:
235 filter[nindex] = True
236 filter[windex] = True
237 if GetOption('verbose'):
238 print "Found %d, %d >>%s<<" % (windex + 1, nindex + 1, wline)
239 out = []
240 for index in range(len(output)):
241 if not filter[index]:
242 out.append(output[index])
244 return out
246 # GetChanges
248 # Parse the output into discrete change blocks.
250 def GetChanges(output):
251 # Split on lines, adding an END marker to simply add logic
252 lines = output.split('\n')
253 lines = FilterLinesIn(lines)
254 lines.append('END')
256 changes = []
257 was = []
258 now = []
259 mode = ''
260 last = None
262 for line in lines:
263 # print "LINE=%s" % line
264 if not line: continue
266 elif line[0] == '<':
267 if line[2:].strip() == '': continue
268 # Ignore prototypes
269 if len(line) > 10:
270 words = line[2:].split()
271 if len(words) == 2 and words[1][-1] == ';':
272 if words[0] == 'struct' or words[0] == 'union':
273 continue
274 was.append(line[2:])
275 elif line[0] == '>':
276 if line[2:].strip() == '': continue
277 if line[2:10] == '#include': continue
278 now.append(line[2:])
279 elif line[0] == '-':
280 continue
281 else:
282 change = Change(line, was, now)
283 was = []
284 now = []
285 if ValidChange(change):
286 changes.append(change)
287 if line == 'END':
288 break
290 return FilterChanges(changes)
292 def FilterChanges(changes):
293 if len(changes) < 2: return changes
294 out = []
295 filter = [False for change in changes]
296 for cur in range(len(changes)):
297 for cmp in range(cur+1, len(changes)):
298 if filter[cmp]:
299 continue
300 if Swapped(changes[cur], changes[cmp]):
301 filter[cur] = True
302 filter[cmp] = True
303 for cur in range(len(changes)):
304 if filter[cur]: continue
305 out.append(changes[cur])
306 return out
308 def Main(args):
309 filenames = ParseOptions(args)
310 if not filenames:
311 gendir = os.path.join(GetOption('gen'), '*.h')
312 filenames = sorted(glob.glob(gendir))
313 srcdir = os.path.join(GetOption('src'), '*.h')
314 srcs = sorted(glob.glob(srcdir))
315 for name in srcs:
316 name = os.path.split(name)[1]
317 name = os.path.join(GetOption('gen'), name)
318 if name not in filenames:
319 print 'Missing: %s' % name
321 for filename in filenames:
322 gen = filename
323 filename = filename[len(GetOption('gen')) + 1:]
324 src = os.path.join(GetOption('src'), filename)
325 diff = os.path.join(GetOption('diff'), filename)
326 p = subprocess.Popen(['diff', src, gen], stdout=subprocess.PIPE)
327 output, errors = p.communicate()
329 try:
330 input = open(diff, 'rt').read()
331 except:
332 input = ''
334 if input != output:
335 changes = GetChanges(output)
336 else:
337 changes = []
339 if changes:
340 print "\n\nDelta between:\n src=%s\n gen=%s\n" % (src, gen)
341 for change in changes:
342 change.Dump()
343 print 'Done with %s\n\n' % src
344 if GetOption('ok'):
345 open(diff, 'wt').write(output)
346 if GetOption('halt'):
347 return 1
348 else:
349 print "\nSAME:\n src=%s\n gen=%s" % (src, gen)
350 if input: print ' ** Matched expected diff. **'
351 print '\n'
353 if __name__ == '__main__':
354 sys.exit(Main(sys.argv[1:]))