Bug 983971 - Do not use gralloc for small size on ICS gonk r=nical
[gecko.git] / config / check_spidermonkey_style.py
blob23b65f71f03994053999551b209eb1b0a8149be1
1 # vim: set ts=8 sts=4 et sw=4 tw=99:
2 # This Source Code Form is subject to the terms of the Mozilla Public
3 # License, v. 2.0. If a copy of the MPL was not distributed with this
4 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 #----------------------------------------------------------------------------
7 # This script checks various aspects of SpiderMonkey code style. The current checks are as
8 # follows.
10 # We check the following things in headers.
12 # - No cyclic dependencies.
14 # - No normal header should #include a inlines.h/-inl.h file.
16 # - #ifndef wrappers should have the right form. (XXX: not yet implemented)
17 # - Every header file should have one.
18 # - The guard name used should be appropriate for the filename.
20 # We check the following things in all files.
22 # - #includes should have full paths, e.g. "jit/Ion.h", not "Ion.h".
24 # - #includes should use the appropriate form for system headers (<...>) and
25 # local headers ("...").
27 # - #includes should be ordered correctly.
28 # - Each one should be in the correct section.
29 # - Alphabetical order should be used within sections.
30 # - Sections should be in the right order.
31 # Note that the presence of #if/#endif blocks complicates things, to the
32 # point that it's not always clear where a conditionally-compiled #include
33 # statement should go, even to a human. Therefore, we check the #include
34 # statements within each #if/#endif block (including nested ones) in
35 # isolation, but don't try to do any order checking between such blocks.
36 #----------------------------------------------------------------------------
38 from __future__ import print_function
40 import difflib
41 import os
42 import re
43 import subprocess
44 import sys
45 import traceback
47 # We don't bother checking files in these directories, because they're (a) auxiliary or (b)
48 # imported code that doesn't follow our coding style.
49 ignored_js_src_dirs = [
50 'js/src/config/', # auxiliary stuff
51 'js/src/ctypes/libffi/', # imported code
52 'js/src/devtools/', # auxiliary stuff
53 'js/src/editline/', # imported code
54 'js/src/gdb/', # auxiliary stuff
55 'js/src/vtune/' # imported code
58 # We ignore #includes of these files, because they don't follow the usual rules.
59 included_inclnames_to_ignore = set([
60 'ffi.h', # generated in ctypes/libffi/
61 'devtools/sharkctl.h', # we ignore devtools/ in general
62 'devtools/Instruments.h', # we ignore devtools/ in general
63 'double-conversion.h', # strange MFBT case
64 'javascript-trace.h', # generated in $OBJDIR if HAVE_DTRACE is defined
65 'jsautokw.h', # generated in $OBJDIR
66 'jscustomallocator.h', # provided by embedders; allowed to be missing
67 'js-config.h', # generated in $OBJDIR
68 'pratom.h', # NSPR
69 'prcvar.h', # NSPR
70 'prinit.h', # NSPR
71 'prlink.h', # NSPR
72 'prlock.h', # NSPR
73 'prprf.h', # NSPR
74 'prthread.h', # NSPR
75 'prtypes.h', # NSPR
76 'selfhosted.out.h', # generated in $OBJDIR
77 'unicode/locid.h', # ICU
78 'unicode/numsys.h', # ICU
79 'unicode/ucal.h', # ICU
80 'unicode/uclean.h', # ICU
81 'unicode/ucol.h', # ICU
82 'unicode/udat.h', # ICU
83 'unicode/udatpg.h', # ICU
84 'unicode/uenum.h', # ICU
85 'unicode/unum.h', # ICU
86 'unicode/ustring.h', # ICU
87 'unicode/utypes.h', # ICU
88 'vtune/VTuneWrapper.h' # VTune
91 # These files have additional constraints on where they are #included, so we
92 # ignore #includes of them when checking #include ordering.
93 oddly_ordered_inclnames = set([
94 'ctypes/typedefs.h', # Included multiple times in the body of ctypes/CTypes.h
95 'jsautokw.h', # Included in the body of frontend/TokenStream.h
96 'jswin.h', # Must be #included before <psapi.h>
97 'machine/endian.h', # Must be included after <sys/types.h> on BSD
98 'winbase.h', # Must precede other system headers(?)
99 'windef.h' # Must precede other system headers(?)
102 # The files in tests/style/ contain code that fails this checking in various
103 # ways. Here is the output we expect. If the actual output differs from
104 # this, one of the following must have happened.
105 # - New SpiderMonkey code violates one of the checked rules.
106 # - The tests/style/ files have changed without expected_output being changed
107 # accordingly.
108 # - This script has been broken somehow.
110 expected_output = '''\
111 js/src/tests/style/BadIncludes2.h:1: error:
112 vanilla header includes an inline-header file "tests/style/BadIncludes2-inl.h"
114 js/src/tests/style/BadIncludes.h:3: error:
115 the file includes itself
117 js/src/tests/style/BadIncludes.h:6: error:
118 "BadIncludes2.h" is included using the wrong path;
119 did you forget a prefix, or is the file not yet committed?
121 js/src/tests/style/BadIncludes.h:8: error:
122 <tests/style/BadIncludes2.h> should be included using
123 the #include "..." form
125 js/src/tests/style/BadIncludes.h:10: error:
126 "stdio.h" is included using the wrong path;
127 did you forget a prefix, or is the file not yet committed?
129 js/src/tests/style/BadIncludesOrder-inl.h:5:6: error:
130 "vm/Interpreter-inl.h" should be included after "jsscriptinlines.h"
132 js/src/tests/style/BadIncludesOrder-inl.h:6:7: error:
133 "jsscriptinlines.h" should be included after "js/Value.h"
135 js/src/tests/style/BadIncludesOrder-inl.h:7:8: error:
136 "js/Value.h" should be included after "ds/LifoAlloc.h"
138 js/src/tests/style/BadIncludesOrder-inl.h:8:9: error:
139 "ds/LifoAlloc.h" should be included after "jsapi.h"
141 js/src/tests/style/BadIncludesOrder-inl.h:9:10: error:
142 "jsapi.h" should be included after <stdio.h>
144 js/src/tests/style/BadIncludesOrder-inl.h:10:11: error:
145 <stdio.h> should be included after "mozilla/HashFunctions.h"
147 js/src/tests/style/BadIncludesOrder-inl.h:27:28: error:
148 "jsobj.h" should be included after "jsfun.h"
150 (multiple files): error:
151 header files form one or more cycles
153 tests/style/HeaderCycleA1.h
154 -> tests/style/HeaderCycleA2.h
155 -> tests/style/HeaderCycleA3.h
156 -> tests/style/HeaderCycleA1.h
158 tests/style/HeaderCycleB1-inl.h
159 -> tests/style/HeaderCycleB2-inl.h
160 -> tests/style/HeaderCycleB3-inl.h
161 -> tests/style/HeaderCycleB4-inl.h
162 -> tests/style/HeaderCycleB1-inl.h
163 -> tests/style/jsheadercycleB5inlines.h
164 -> tests/style/HeaderCycleB1-inl.h
165 -> tests/style/HeaderCycleB4-inl.h
167 '''.splitlines(True)
169 actual_output = []
172 def out(*lines):
173 for line in lines:
174 actual_output.append(line + '\n')
177 def error(filename, linenum, *lines):
178 location = filename
179 if linenum is not None:
180 location += ':' + str(linenum)
181 out(location + ': error:')
182 for line in (lines):
183 out(' ' + line)
184 out('')
187 class FileKind(object):
188 C = 1
189 CPP = 2
190 INL_H = 3
191 H = 4
192 TBL = 5
193 MSG = 6
195 @staticmethod
196 def get(filename):
197 if filename.endswith('.c'):
198 return FileKind.C
200 if filename.endswith('.cpp'):
201 return FileKind.CPP
203 if filename.endswith(('inlines.h', '-inl.h', 'Inlines.h')):
204 return FileKind.INL_H
206 if filename.endswith('.h'):
207 return FileKind.H
209 if filename.endswith('.tbl'):
210 return FileKind.TBL
212 if filename.endswith('.msg'):
213 return FileKind.MSG
215 error(filename, None, 'unknown file kind')
218 def get_all_filenames():
219 '''Get a list of all the files in the (Mercurial or Git) repository.'''
220 cmds = [['hg', 'manifest', '-q'], ['git', 'ls-files', '--full-name', '../..']]
221 for cmd in cmds:
222 try:
223 all_filenames = subprocess.check_output(cmd, universal_newlines=True,
224 stderr=subprocess.PIPE).split('\n')
225 return all_filenames
226 except:
227 continue
228 else:
229 raise Exception('failed to run any of the repo manifest commands', cmds)
232 def check_style():
233 # We deal with two kinds of name.
234 # - A "filename" is a full path to a file from the repository root.
235 # - An "inclname" is how a file is referred to in a #include statement.
237 # Examples (filename -> inclname)
238 # - "mfbt/Attributes.h" -> "mozilla/Attributes.h"
239 # - "js/public/Vector.h" -> "js/Vector.h"
240 # - "js/src/vm/String.h" -> "vm/String.h"
242 mfbt_inclnames = set() # type: set(inclname)
243 js_names = dict() # type: dict(filename, inclname)
245 # Select the appropriate files.
246 for filename in get_all_filenames():
247 if filename.startswith('mfbt/') and filename.endswith('.h'):
248 inclname = 'mozilla/' + filename[len('mfbt/'):]
249 mfbt_inclnames.add(inclname)
251 if filename.startswith('js/public/') and filename.endswith('.h'):
252 inclname = 'js/' + filename[len('js/public/'):]
253 js_names[filename] = inclname
255 if filename.startswith('js/src/') and \
256 not filename.startswith(tuple(ignored_js_src_dirs)) and \
257 filename.endswith(('.c', '.cpp', '.h', '.tbl', '.msg')):
258 inclname = filename[len('js/src/'):]
259 js_names[filename] = inclname
261 all_inclnames = mfbt_inclnames | set(js_names.values())
263 edges = dict() # type: dict(inclname, set(inclname))
265 # We don't care what's inside the MFBT files, but because they are
266 # #included from JS files we have to add them to the inclusion graph.
267 for inclname in mfbt_inclnames:
268 edges[inclname] = set()
270 # Process all the JS files.
271 for filename in js_names.keys():
272 inclname = js_names[filename]
273 file_kind = FileKind.get(filename)
274 if file_kind == FileKind.C or file_kind == FileKind.CPP or \
275 file_kind == FileKind.H or file_kind == FileKind.INL_H:
276 included_h_inclnames = set() # type: set(inclname)
278 # This script is run in js/src/, so prepend '../../' to get to the root of the Mozilla
279 # source tree.
280 with open(os.path.join('../..', filename)) as f:
281 do_file(filename, inclname, file_kind, f, all_inclnames, included_h_inclnames)
283 edges[inclname] = included_h_inclnames
285 find_cycles(all_inclnames, edges)
287 # Compare expected and actual output.
288 difflines = difflib.unified_diff(expected_output, actual_output,
289 fromfile='check_spider_monkey_style.py expected output',
290 tofile='check_spider_monkey_style.py actual output')
291 ok = True
292 for diffline in difflines:
293 ok = False
294 print(diffline, end='')
296 return ok
299 def module_name(name):
300 '''Strip the trailing .cpp, .h, inlines.h or -inl.h from a filename.'''
302 return name.replace('inlines.h', '').replace('-inl.h', '').replace('.h', '').replace('.cpp', '')
305 def is_module_header(enclosing_inclname, header_inclname):
306 '''Determine if an included name is the "module header", i.e. should be
307 first in the file.'''
309 module = module_name(enclosing_inclname)
311 # Normal case, e.g. module == "foo/Bar", header_inclname == "foo/Bar.h".
312 if module == module_name(header_inclname):
313 return True
315 # A public header, e.g. module == "foo/Bar", header_inclname == "js/Bar.h".
316 m = re.match(r'js\/(.*)\.h', header_inclname)
317 if m is not None and module.endswith('/' + m.group(1)):
318 return True
320 return False
323 class Include(object):
324 '''Important information for a single #include statement.'''
326 def __init__(self, inclname, linenum, is_system):
327 self.inclname = inclname
328 self.linenum = linenum
329 self.is_system = is_system
331 def isLeaf(self):
332 return True
334 def section(self, enclosing_inclname):
335 '''Identify which section inclname belongs to.
337 The section numbers are as follows.
338 0. Module header (e.g. jsfoo.h or jsfooinlines.h within jsfoo.cpp)
339 1. mozilla/Foo.h
340 2. <foo.h> or <foo>
341 3. jsfoo.h, prmjtime.h, etc
342 4. foo/Bar.h
343 5. jsfooinlines.h
344 6. foo/Bar-inl.h
345 7. non-.h, e.g. *.tbl, *.msg
348 if self.is_system:
349 return 2
351 if not self.inclname.endswith('.h'):
352 return 7
354 # A couple of modules have the .h file in js/ and the .cpp file elsewhere and so need
355 # special handling.
356 if is_module_header(enclosing_inclname, self.inclname):
357 return 0
359 if '/' in self.inclname:
360 if self.inclname.startswith('mozilla/'):
361 return 1
363 if self.inclname.endswith('-inl.h'):
364 return 6
366 return 4
368 if self.inclname.endswith('inlines.h'):
369 return 5
371 return 3
373 def quote(self):
374 if self.is_system:
375 return '<' + self.inclname + '>'
376 else:
377 return '"' + self.inclname + '"'
380 class HashIfBlock(object):
381 '''Important information about a #if/#endif block.
383 A #if/#endif block is the contents of a #if/#endif (or similar) section.
384 The top-level block, which is not within a #if/#endif pair, is also
385 considered a block.
387 Each leaf is either an Include (representing a #include), or another
388 nested HashIfBlock.'''
389 def __init__(self):
390 self.kids = []
392 def isLeaf(self):
393 return False
396 def do_file(filename, inclname, file_kind, f, all_inclnames, included_h_inclnames):
397 block_stack = [HashIfBlock()]
399 # Extract the #include statements as a tree of IBlocks and IIncludes.
400 for linenum, line in enumerate(f, start=1):
401 # Look for a |#include "..."| line.
402 m = re.match(r'\s*#\s*include\s+"([^"]*)"', line)
403 if m is not None:
404 block_stack[-1].kids.append(Include(m.group(1), linenum, False))
406 # Look for a |#include <...>| line.
407 m = re.match(r'\s*#\s*include\s+<([^>]*)>', line)
408 if m is not None:
409 block_stack[-1].kids.append(Include(m.group(1), linenum, True))
411 # Look for a |#{if,ifdef,ifndef}| line.
412 m = re.match(r'\s*#\s*(if|ifdef|ifndef)\b', line)
413 if m is not None:
414 # Open a new block.
415 new_block = HashIfBlock()
416 block_stack[-1].kids.append(new_block)
417 block_stack.append(new_block)
419 # Look for a |#{elif,else}| line.
420 m = re.match(r'\s*#\s*(elif|else)\b', line)
421 if m is not None:
422 # Close the current block, and open an adjacent one.
423 block_stack.pop()
424 new_block = HashIfBlock()
425 block_stack[-1].kids.append(new_block)
426 block_stack.append(new_block)
428 # Look for a |#endif| line.
429 m = re.match(r'\s*#\s*endif\b', line)
430 if m is not None:
431 # Close the current block.
432 block_stack.pop()
434 def check_include_statement(include):
435 '''Check the style of a single #include statement.'''
437 if include.is_system:
438 # Check it is not a known local file (in which case it's probably a system header).
439 if include.inclname in included_inclnames_to_ignore or \
440 include.inclname in all_inclnames:
441 error(filename, include.linenum,
442 include.quote() + ' should be included using',
443 'the #include "..." form')
445 else:
446 if include.inclname not in included_inclnames_to_ignore:
447 included_kind = FileKind.get(include.inclname)
449 # Check the #include path has the correct form.
450 if include.inclname not in all_inclnames:
451 error(filename, include.linenum,
452 include.quote() + ' is included ' + 'using the wrong path;',
453 'did you forget a prefix, or is the file not yet committed?')
455 # Record inclusions of .h files for cycle detection later.
456 # (Exclude .tbl and .msg files.)
457 elif included_kind == FileKind.H or included_kind == FileKind.INL_H:
458 included_h_inclnames.add(include.inclname)
460 # Check a H file doesn't #include an INL_H file.
461 if file_kind == FileKind.H and included_kind == FileKind.INL_H:
462 error(filename, include.linenum,
463 'vanilla header includes an inline-header file ' + include.quote())
465 # Check a file doesn't #include itself. (We do this here because the cycle
466 # detection below doesn't detect this case.)
467 if inclname == include.inclname:
468 error(filename, include.linenum, 'the file includes itself')
470 def check_includes_order(include1, include2):
471 '''Check the ordering of two #include statements.'''
473 if include1.inclname in oddly_ordered_inclnames or \
474 include2.inclname in oddly_ordered_inclnames:
475 return
477 section1 = include1.section(inclname)
478 section2 = include2.section(inclname)
479 if (section1 > section2) or \
480 ((section1 == section2) and (include1.inclname.lower() > include2.inclname.lower())):
481 error(filename, str(include1.linenum) + ':' + str(include2.linenum),
482 include1.quote() + ' should be included after ' + include2.quote())
484 # The #include statements in the files in assembler/ and yarr/ have all manner of implicit
485 # ordering requirements. Boo. Ignore them.
486 skip_order_checking = inclname.startswith(('assembler/', 'yarr/'))
488 # Check the extracted #include statements, both individually, and the ordering of
489 # adjacent pairs that live in the same block.
490 def pair_traverse(prev, this):
491 if this.isLeaf():
492 check_include_statement(this)
493 if prev is not None and prev.isLeaf() and not skip_order_checking:
494 check_includes_order(prev, this)
495 else:
496 for prev2, this2 in zip([None] + this.kids[0:-1], this.kids):
497 pair_traverse(prev2, this2)
499 pair_traverse(None, block_stack[-1])
502 def find_cycles(all_inclnames, edges):
503 '''Find and draw any cycles.'''
505 SCCs = tarjan(all_inclnames, edges)
507 # The various sorted() calls below ensure the output is deterministic.
509 def draw_SCC(c):
510 cset = set(c)
511 drawn = set()
512 def draw(v, indent):
513 out(' ' * indent + ('-> ' if indent else ' ') + v)
514 if v in drawn:
515 return
516 drawn.add(v)
517 for succ in sorted(edges[v]):
518 if succ in cset:
519 draw(succ, indent + 1)
520 draw(sorted(c)[0], 0)
521 out('')
523 have_drawn_an_SCC = False
524 for scc in sorted(SCCs):
525 if len(scc) != 1:
526 if not have_drawn_an_SCC:
527 error('(multiple files)', None, 'header files form one or more cycles')
528 have_drawn_an_SCC = True
530 draw_SCC(scc)
533 # Tarjan's algorithm for finding the strongly connected components (SCCs) of a graph.
534 # https://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm
535 def tarjan(V, E):
536 vertex_index = {}
537 vertex_lowlink = {}
538 index = 0
539 S = []
540 all_SCCs = []
542 def strongconnect(v, index):
543 # Set the depth index for v to the smallest unused index
544 vertex_index[v] = index
545 vertex_lowlink[v] = index
546 index += 1
547 S.append(v)
549 # Consider successors of v
550 for w in E[v]:
551 if w not in vertex_index:
552 # Successor w has not yet been visited; recurse on it
553 index = strongconnect(w, index)
554 vertex_lowlink[v] = min(vertex_lowlink[v], vertex_lowlink[w])
555 elif w in S:
556 # Successor w is in stack S and hence in the current SCC
557 vertex_lowlink[v] = min(vertex_lowlink[v], vertex_index[w])
559 # If v is a root node, pop the stack and generate an SCC
560 if vertex_lowlink[v] == vertex_index[v]:
561 i = S.index(v)
562 scc = S[i:]
563 del S[i:]
564 all_SCCs.append(scc)
566 return index
568 for v in V:
569 if v not in vertex_index:
570 index = strongconnect(v, index)
572 return all_SCCs
575 def main():
576 ok = check_style()
578 if ok:
579 print('TEST-PASS | check_spidermonkey_style.py | ok')
580 else:
581 print('TEST-UNEXPECTED-FAIL | check_spidermonkey_style.py | actual output does not match expected output; diff is above')
583 sys.exit(0 if ok else 1)
586 if __name__ == '__main__':
587 main()