1 | #!/usr/bin/env python3
|
---|
2 |
|
---|
3 | from sys import stdout
|
---|
4 | from pycparser import c_ast, c_generator, parse_file
|
---|
5 | from re import sub
|
---|
6 |
|
---|
7 | cross_gcc = "/opt/cross-m68k/bin/m68k-none-elf-gcc"
|
---|
8 |
|
---|
9 | class Visitor(c_ast.NodeVisitor):
|
---|
10 | gen = c_generator.CGenerator()
|
---|
11 |
|
---|
12 | def __init__(self):
|
---|
13 | self.path = None
|
---|
14 | self.decl = {}
|
---|
15 | self.errs = []
|
---|
16 |
|
---|
17 | def visit_Typedef(self, node):
|
---|
18 | return
|
---|
19 |
|
---|
20 | def visit_Decl(self, node):
|
---|
21 | if node.coord.file != self.path or node.name is None:
|
---|
22 | return
|
---|
23 |
|
---|
24 | node.init = None
|
---|
25 | node.storage = [x for x in node.storage if
|
---|
26 | x != "static" and
|
---|
27 | x != "extern"]
|
---|
28 |
|
---|
29 | decl = sub(r"\[[^\]]+\]", "[]", self.gen.visit(node))
|
---|
30 |
|
---|
31 | if node.name not in self.decl:
|
---|
32 | self.decl[node.name] = decl
|
---|
33 |
|
---|
34 | elif self.decl[node.name] != decl:
|
---|
35 | self.errs.append("mismatch for {} in {}:{}: {} <-> {}". \
|
---|
36 | format(node.name, node.coord.file, node.coord.line, \
|
---|
37 | self.decl[node.name], decl))
|
---|
38 |
|
---|
39 | def visit_FuncDef(self, node):
|
---|
40 | if node.coord.file != self.path:
|
---|
41 | return
|
---|
42 |
|
---|
43 | self.visit_Decl(node.decl)
|
---|
44 |
|
---|
45 | def visit_path(self, path, ast):
|
---|
46 | self.path = path
|
---|
47 | self.visit(ast)
|
---|
48 | self.path = None
|
---|
49 |
|
---|
50 | def show_errs(self):
|
---|
51 | for err in self.errs:
|
---|
52 | print(err)
|
---|
53 |
|
---|
54 | path_ast = []
|
---|
55 |
|
---|
56 | with open("misc/c-files.txt", "r") as f:
|
---|
57 | for path in f:
|
---|
58 | path = path.rstrip()
|
---|
59 |
|
---|
60 | if path == "ram/wdfield.c": # breaks pycparser
|
---|
61 | continue
|
---|
62 |
|
---|
63 | stdout.write("parsing {} \r".format(path))
|
---|
64 | stdout.flush()
|
---|
65 |
|
---|
66 | ast = parse_file(path, use_cpp = True, cpp_path = cross_gcc,
|
---|
67 | cpp_args = ["-E", "-I", "include", "-include", "predef.h"])
|
---|
68 | path_ast.append((path, ast))
|
---|
69 | # ast.show()
|
---|
70 |
|
---|
71 | print("")
|
---|
72 |
|
---|
73 | vis = Visitor()
|
---|
74 |
|
---|
75 | for (path, ast) in path_ast:
|
---|
76 | stdout.write("visiting {} \r".format(path))
|
---|
77 | stdout.flush()
|
---|
78 |
|
---|
79 | vis.visit_path(path, ast)
|
---|
80 |
|
---|
81 | print("")
|
---|
82 | vis.show_errs()
|
---|