source: buchla-68k/misc/check-glob.py@ 342a56f

Last change on this file since 342a56f was 342a56f, checked in by Thomas Lopatic <thomas@…>, 5 years ago

Script to check global types.

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