File: check_include_guards.py

package info (click to toggle)
mame 0.285%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 929,236 kB
  • sloc: cpp: 5,476,251; xml: 2,245,029; ansic: 752,116; sh: 34,431; lisp: 19,643; python: 17,598; makefile: 13,253; java: 8,492; yacc: 8,152; javascript: 7,147; cs: 6,013; asm: 4,786; ada: 1,681; pascal: 1,191; lex: 1,174; perl: 585; ruby: 373
file content (51 lines) | stat: -rwxr-xr-x 1,529 bytes parent folder | download
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
#!/usr/bin/python3
##
## license:BSD-3-Clause
## copyright-holders:Vas Crabb

import io
import os
import os.path
import re
import sys


def pathsplit(p):
    result = [ ]
    while p:
        d, n = os.path.split(p)
        if not n:
            result.insert(0, d)
            break
        else:
            result.insert(0, n)
            p = d
    return result


if __name__ == '__main__':
    extpat = re.compile('.+\\.(h|hpp)$')
    substpat = re.compile('[-.]')
    guardpat = re.compile(r'^ *# *ifndef +([^\s]+)(\s+.*)?')
    bad = False
    if len(sys.argv) < 2:
        sys.stderr.write("Error: requires at least one path defined\n")
        sys.exit(2)

    for root in sys.argv[1:]:
        for path, subdirs, files in os.walk(root):
            prefix = 'MAME_' + '_'.join([n.upper() for n in pathsplit(os.path.relpath(path, root))]) + '_'
            for f in files:
                if extpat.match(f):
                    expected = prefix + substpat.sub('_', f.upper())
                    fp = os.path.join(path, f)
                    with io.open(fp, 'r', encoding='utf-8') as fd:
                        for l in fd:
                            m = guardpat.match(l)
                            if m:
                                if m.group(1) != expected:
                                    sys.stderr.write('%s: #include guard does not appear to match expected %s\n' % (fp, expected))
                                    bad = True
                                break
    if bad:
        sys.exit(1)