OpenMP/Fortran: Fix handling of 'declare target' with 'link' clause [PR115559]
[official-gcc.git] / gcc / m2 / tools-src / checkmeta.py
blobc62ac19759e8c00389893a2928dd1ae55173f2f9
1 #!/usr/bin/env python3
3 # utility to check meta errors for simple format spec mistakes.
5 # Copyright (C) 2016-2024 Free Software Foundation, Inc.
7 # This file is part of GNU Modula-2.
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.
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.
19 # You should have received a copy of the GNU General Public License
20 # along with GNU Modula-2; see the file COPYING. If not, write to the
21 # Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
22 # 02110-1301, USA.
24 import argparse
25 import os
26 import pathlib
27 import sys
30 exit_code = 0
33 def visit_dir(directory, ext, func):
34 # visit_dir - call func for each file below, dir, matching extension, ext.
35 list_of_files = os.listdir(directory)
36 list_of_files.sort()
37 for filename in list_of_files:
38 path = pathlib.Path(filename)
39 full = os.path.join(directory, filename)
40 if path.suffix == ext:
41 func(full)
44 def check_format_spec(filename, line, no):
45 global exit_code
47 percent = line.find('%')
48 if percent >= 0:
49 specifier = False
50 for ch in line[percent:]:
51 if ch in ['{', '%']:
52 pass
53 elif ch in ['1', '2', '3', '4']:
54 if specifier:
55 sys.stderr.write('%s:%d: format specifier error, the symbol position digit must be before the specifier: %s\n' % (filename, no, line))
56 exit_code = 1
57 else:
58 specifier = True
61 def search_format(filename, line, no):
62 cbra = line.find('{')
63 while cbra >= 0:
64 colon = line.find(':', cbra)
65 end = line.find('}', cbra)
66 if end >= 0:
67 if (colon >= 0) and (colon < end):
68 end = colon
69 check_format_spec(filename, line[cbra:end], no)
70 cbra = line.find('{', end)
71 else:
72 return
75 def check_string_quote (filename, line, no, quote):
76 end = line.find(quote, 1)
77 if end > 0:
78 search_format(filename, line[1:end], no)
81 def check_string (filename, line, no):
82 quote = line.find("'")
83 if quote >= 0:
84 check_string_quote(filename, line[quote:], no, "'")
85 quote = line.find('"')
86 if quote >= 0:
87 check_string_quote(filename, line[quote:], no, '"')
90 def check_meta_spec (filename):
91 lines = open(filename).readlines()
92 extra = 0
93 for no, line in enumerate(lines):
94 if extra > 0:
95 extra -= 1
96 check_string(filename, line, no+1)
97 elif "Meta" in line:
98 meta = line.find("Meta")
99 if meta >= 0:
100 bra = line.find("(", meta)
101 if bra >= 0:
102 check_string(filename, line[bra:], no+1)
103 extra = 1
106 def handle_arguments():
107 # handle_arguments create and return the args object.
108 parser = argparse.ArgumentParser()
109 parser.add_argument('-s', '--srcdir',
110 help='set source directory.',
111 default='.', action='store')
112 args = parser.parse_args()
113 return args
116 def main():
117 args = handle_arguments()
118 visit_dir(args.srcdir, '.mod', check_meta_spec)
119 visit_dir(args.srcdir, '.bnf', check_meta_spec)
120 sys.exit(exit_code)
123 main()