Debian Codegraph @

gcc/m2/tools-src/checkmeta.py

raw
1 #!/usr/bin/env python3
2
3 # utility to check meta errors for simple format spec mistakes.
4
5 # Copyright (C) 2016-2025 Free Software Foundation, Inc.
6 #
7 # This file is part of GNU Modula-2.
8 #
9 # GNU Modula-2 is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3, or (at your option)
12 # any later version.
13 #
14 # GNU Modula-2 is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with GCC; see the file COPYING3. If not see
21 # <http://www.gnu.org/licenses/>.
22
23 import argparse
24 import os
25 import pathlib
26 import sys
27
28
29 exit_code = 0
30
31
32 def visit_dir(directory, ext, func):
33 # visit_dir - call func for each file below, dir, matching extension, ext.
34 list_of_files = os.listdir(directory)
35 list_of_files.sort()
36 for filename in list_of_files:
37 path = pathlib.Path(filename)
38 full = os.path.join(directory, filename)
39 if path.suffix == ext:
40 func(full)
41
42
43 def check_format_spec(filename, line, no):
44 global exit_code
45
46 percent = line.find('%')
47 if percent >= 0:
48 specifier = False
49 for ch in line[percent:]:
50 if ch in ['{', '%']:
51 pass
52 elif ch in ['1', '2', '3', '4']:
53 if specifier:
54 sys.stderr.write('%s:%d: format specifier error, the symbol position digit must be before the specifier: %s\n' % (filename, no, line))
55 exit_code = 1
56 else:
57 specifier = True
58
59
60 def search_format(filename, line, no):
61 cbra = line.find('{')
62 while cbra >= 0:
63 colon = line.find(':', cbra)
64 end = line.find('}', cbra)
65 if end >= 0:
66 if (colon >= 0) and (colon < end):
67 end = colon
68 check_format_spec(filename, line[cbra:end], no)
69 cbra = line.find('{', end)
70 else:
71 return
72
73
74 def check_string_quote (filename, line, no, quote):
75 end = line.find(quote, 1)
76 if end > 0:
77 search_format(filename, line[1:end], no)
78
79
80 def check_string (filename, line, no):
81 quote = line.find("'")
82 if quote >= 0:
83 check_string_quote(filename, line[quote:], no, "'")
84 quote = line.find('"')
85 if quote >= 0:
86 check_string_quote(filename, line[quote:], no, '"')
87
88
89 def check_meta_spec (filename):
90 lines = open(filename).readlines()
91 extra = 0
92 for no, line in enumerate(lines):
93 if extra > 0:
94 extra -= 1
95 check_string(filename, line, no+1)
96 elif "Meta" in line:
97 meta = line.find("Meta")
98 if meta >= 0:
99 bra = line.find("(", meta)
100 if bra >= 0:
101 check_string(filename, line[bra:], no+1)
102 extra = 1
103
104
105 def handle_arguments():
106 # handle_arguments create and return the args object.
107 parser = argparse.ArgumentParser()
108 parser.add_argument('-s', '--srcdir',
109 help='set source directory.',
110 default='.', action='store')
111 args = parser.parse_args()
112 return args
113
114
115 def main():
116 args = handle_arguments()
117 visit_dir(args.srcdir, '.mod', check_meta_spec)
118 visit_dir(args.srcdir, '.bnf', check_meta_spec)
119 sys.exit(exit_code)
120
121
122 main()