xref: /netbsd-src/external/gpl3/gcc.old/dist/contrib/check-internal-format-escaping.py (revision 627f7eb200a4419d89b531d55fccd2ee3ffdcde0)
1#!/usr/bin/env python3
2#
3# Check gcc.pot file for gcc-internal-format and print all strings
4# that contain an option that is not wrapped by %<-option_name%>.
5#
6# This file is part of GCC.
7#
8# GCC is free software; you can redistribute it and/or modify it under
9# the terms of the GNU General Public License as published by the Free
10# Software Foundation; either version 3, or (at your option) any later
11# version.
12#
13# GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14# WARRANTY; without even the implied warranty of MERCHANTABILITY or
15# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16# for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with GCC; see the file COPYING3.  If not see
20# <http://www.gnu.org/licenses/>.  */
21#
22#
23#
24
25import argparse
26import re
27
28parser = argparse.ArgumentParser(description='')
29parser.add_argument('file', help = 'pot file')
30
31args = parser.parse_args()
32
33origin = None
34internal = False
35
36lines = open(args.file).readlines()
37for i, l in enumerate(lines):
38    l = l.strip()
39    s = 'msgid '
40    if l.startswith('#: '):
41        origin = l
42    elif '#, gcc-internal-format' in l:
43        internal = True
44    if l.startswith(s) and origin and internal:
45        j = 0
46        while not lines[i + j].startswith('msgstr'):
47            l = lines[i + j]
48            if l.startswith(s):
49                l = l[len(s):]
50            text = l.strip('"').strip()
51            if text:
52                parts = text.split(' ')
53                for p in parts:
54                    if p.startswith('-'):
55                        if len(p) >= 2 and (p[1].isalpha() and p != '-INF'):
56                            print('%s: %s' % (origin, text))
57                    elif p.startswith('__builtin_'):
58                        print('%s: %s' % (origin, text))
59                    if re.search("[^%]'", p):
60                        print('%s: %s' % (origin, text))
61                    # %< should not be preceded by a non-punctuation
62                    # %character.
63                    if re.search("[a-zA-Z0-9]%<", p):
64                        print('%s: %s' % (origin, text))
65            j += 1
66
67        origin = None
68        internal = False
69