annotate contrib/filter_params.py @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 04ced10e8804
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 #!/usr/bin/python
kono
parents:
diff changeset
2 """
kono
parents:
diff changeset
3 Filters out some of the #defines used throughout the GCC sources:
kono
parents:
diff changeset
4 - GTY(()) marks declarations for gengtype.c
kono
parents:
diff changeset
5 - PARAMS(()) is used for K&R compatibility. See ansidecl.h.
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 When passed one or more filenames, acts on those files and prints the
kono
parents:
diff changeset
8 results to stdout.
kono
parents:
diff changeset
9
kono
parents:
diff changeset
10 When run without a filename, runs a unit-testing suite.
kono
parents:
diff changeset
11 """
kono
parents:
diff changeset
12 import re
kono
parents:
diff changeset
13 import sys
kono
parents:
diff changeset
14 import unittest
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 # Optional whitespace
kono
parents:
diff changeset
17 OPT_WS = '\s*'
kono
parents:
diff changeset
18
kono
parents:
diff changeset
19 def filter_src(text):
kono
parents:
diff changeset
20 """
kono
parents:
diff changeset
21 str -> str. We operate on the whole of the source file at once
kono
parents:
diff changeset
22 (rather than individual lines) so that we can have multiline
kono
parents:
diff changeset
23 regexes.
kono
parents:
diff changeset
24 """
kono
parents:
diff changeset
25
kono
parents:
diff changeset
26 # Convert C comments from GNU coding convention of:
kono
parents:
diff changeset
27 # /* FIRST_LINE
kono
parents:
diff changeset
28 # NEXT_LINE
kono
parents:
diff changeset
29 # FINAL_LINE. */
kono
parents:
diff changeset
30 # to:
kono
parents:
diff changeset
31 # /** @verbatim FIRST_LINE
kono
parents:
diff changeset
32 # NEXT_LINE
kono
parents:
diff changeset
33 # FINAL_LINE. @endverbatim */
kono
parents:
diff changeset
34 # so that doxygen will parse them.
kono
parents:
diff changeset
35 #
kono
parents:
diff changeset
36 # Only comments that begin on the left-most column are converted.
kono
parents:
diff changeset
37 #
kono
parents:
diff changeset
38 text = re.sub(r'^/\*\* ',
kono
parents:
diff changeset
39 r'/** @verbatim ',
kono
parents:
diff changeset
40 text,
kono
parents:
diff changeset
41 flags=re.MULTILINE)
kono
parents:
diff changeset
42 text = re.sub(r'^/\* ',
kono
parents:
diff changeset
43 r'/** @verbatim ',
kono
parents:
diff changeset
44 text,
kono
parents:
diff changeset
45 flags=re.MULTILINE)
kono
parents:
diff changeset
46 text = re.sub(r'\*/',
kono
parents:
diff changeset
47 r' @endverbatim */',
kono
parents:
diff changeset
48 text)
kono
parents:
diff changeset
49
kono
parents:
diff changeset
50 # Remove GTY markings (potentially multiline ones):
kono
parents:
diff changeset
51 text = re.sub('GTY' + OPT_WS + r'\(\(.*?\)\)\s+',
kono
parents:
diff changeset
52 '',
kono
parents:
diff changeset
53 text,
kono
parents:
diff changeset
54 flags=(re.MULTILINE|re.DOTALL))
kono
parents:
diff changeset
55
kono
parents:
diff changeset
56 # Strip out 'ATTRIBUTE_UNUSED'
kono
parents:
diff changeset
57 text = re.sub('\sATTRIBUTE_UNUSED',
kono
parents:
diff changeset
58 '',
kono
parents:
diff changeset
59 text)
kono
parents:
diff changeset
60
kono
parents:
diff changeset
61 # PARAMS(()) is used for K&R compatibility. See ansidecl.h.
kono
parents:
diff changeset
62 text = re.sub('PARAMS' + OPT_WS + r'\(\((.*?)\)\)',
kono
parents:
diff changeset
63 r'(\1)',
kono
parents:
diff changeset
64 text)
kono
parents:
diff changeset
65
kono
parents:
diff changeset
66 # Replace 'ENUM_BITFIELD(enum_name)' with 'enum enum_name'.
kono
parents:
diff changeset
67 text = re.sub('ENUM_BITFIELD\s*\(([^\)]*)\)',
kono
parents:
diff changeset
68 r'enum \1',
kono
parents:
diff changeset
69 text)
kono
parents:
diff changeset
70
kono
parents:
diff changeset
71 return text
kono
parents:
diff changeset
72
kono
parents:
diff changeset
73 class FilteringTests(unittest.TestCase):
kono
parents:
diff changeset
74 '''
kono
parents:
diff changeset
75 Unit tests for filter_src.
kono
parents:
diff changeset
76 '''
kono
parents:
diff changeset
77 def assert_filters_to(self, src_input, expected_result):
kono
parents:
diff changeset
78 # assertMultiLineEqual was added to unittest in 2.7/3.1
kono
parents:
diff changeset
79 if hasattr(self, 'assertMultiLineEqual'):
kono
parents:
diff changeset
80 assertion = self.assertMultiLineEqual
kono
parents:
diff changeset
81 else:
kono
parents:
diff changeset
82 assertion = self.assertEqual
kono
parents:
diff changeset
83 assertion(expected_result, filter_src(src_input))
kono
parents:
diff changeset
84
kono
parents:
diff changeset
85 def test_comment_example(self):
kono
parents:
diff changeset
86 self.assert_filters_to(
kono
parents:
diff changeset
87 ('/* FIRST_LINE\n'
kono
parents:
diff changeset
88 ' NEXT_LINE\n'
kono
parents:
diff changeset
89 ' FINAL_LINE. */\n'),
kono
parents:
diff changeset
90 ('/** @verbatim FIRST_LINE\n'
kono
parents:
diff changeset
91 ' NEXT_LINE\n'
kono
parents:
diff changeset
92 ' FINAL_LINE. @endverbatim */\n'))
kono
parents:
diff changeset
93
kono
parents:
diff changeset
94 def test_comment_example_gengtype(self):
kono
parents:
diff changeset
95 self.assert_filters_to(
kono
parents:
diff changeset
96 ('/** Allocate and initialize an input buffer state.\n'
kono
parents:
diff changeset
97 ' * @param file A readable stream.\n'
kono
parents:
diff changeset
98 ' * @param size The character buffer size in bytes. When in doubt, use @c YY_BUF_SIZE.\n'
kono
parents:
diff changeset
99 ' * \n'
kono
parents:
diff changeset
100 ' * @return the allocated buffer state.\n'
kono
parents:
diff changeset
101 ' */'),
kono
parents:
diff changeset
102 ('/** @verbatim Allocate and initialize an input buffer state.\n'
kono
parents:
diff changeset
103 ' * @param file A readable stream.\n'
kono
parents:
diff changeset
104 ' * @param size The character buffer size in bytes. When in doubt, use @c YY_BUF_SIZE.\n'
kono
parents:
diff changeset
105 ' * \n'
kono
parents:
diff changeset
106 ' * @return the allocated buffer state.\n'
kono
parents:
diff changeset
107 ' @endverbatim */'))
kono
parents:
diff changeset
108
kono
parents:
diff changeset
109 def test_oneliner_comment(self):
kono
parents:
diff changeset
110 self.assert_filters_to(
kono
parents:
diff changeset
111 '/* Returns the string representing CLASS. */\n',
kono
parents:
diff changeset
112 ('/** @verbatim Returns the string representing CLASS. @endverbatim */\n'))
kono
parents:
diff changeset
113
kono
parents:
diff changeset
114 def test_multiline_comment(self):
kono
parents:
diff changeset
115 self.assert_filters_to(
kono
parents:
diff changeset
116 ('/* The thread-local storage model associated with a given VAR_DECL\n'
kono
parents:
diff changeset
117 " or SYMBOL_REF. This isn't used much, but both trees and RTL refer\n"
kono
parents:
diff changeset
118 " to it, so it's here. */\n"),
kono
parents:
diff changeset
119 ('/** @verbatim The thread-local storage model associated with a given VAR_DECL\n'
kono
parents:
diff changeset
120 " or SYMBOL_REF. This isn't used much, but both trees and RTL refer\n"
kono
parents:
diff changeset
121 " to it, so it's here. @endverbatim */\n"))
kono
parents:
diff changeset
122
kono
parents:
diff changeset
123 def test_GTY(self):
kono
parents:
diff changeset
124 self.assert_filters_to(
kono
parents:
diff changeset
125 ('typedef struct GTY(()) alias_pair {\n'
kono
parents:
diff changeset
126 ' tree decl;\n'
kono
parents:
diff changeset
127 ' tree target;\n'
kono
parents:
diff changeset
128 '} alias_pair;\n'),
kono
parents:
diff changeset
129 ('typedef struct alias_pair {\n'
kono
parents:
diff changeset
130 ' tree decl;\n'
kono
parents:
diff changeset
131 ' tree target;\n'
kono
parents:
diff changeset
132 '} alias_pair;\n'))
kono
parents:
diff changeset
133
kono
parents:
diff changeset
134 def test_multiline_GTY(self):
kono
parents:
diff changeset
135 # Ensure that a multiline GTY is filtered out.
kono
parents:
diff changeset
136 self.assert_filters_to(
kono
parents:
diff changeset
137 ('class GTY((desc ("%h.type"), tag ("SYMTAB_SYMBOL"),\n'
kono
parents:
diff changeset
138 '\t chain_next ("%h.next"), chain_prev ("%h.previous")))\n'
kono
parents:
diff changeset
139 ' symtab_node_base\n'
kono
parents:
diff changeset
140 '{\n'),
kono
parents:
diff changeset
141 ('class symtab_node_base\n'
kono
parents:
diff changeset
142 '{\n'))
kono
parents:
diff changeset
143
kono
parents:
diff changeset
144 def test_ATTRIBUTE_UNUSED(self):
kono
parents:
diff changeset
145 # Ensure that ATTRIBUTE_UNUSED is filtered out.
kono
parents:
diff changeset
146 self.assert_filters_to(
kono
parents:
diff changeset
147 ('static void\n'
kono
parents:
diff changeset
148 'record_set (rtx dest, const_rtx set, void *data ATTRIBUTE_UNUSED)\n'
kono
parents:
diff changeset
149 '{\n'),
kono
parents:
diff changeset
150 ('static void\n'
kono
parents:
diff changeset
151 'record_set (rtx dest, const_rtx set, void *data)\n'
kono
parents:
diff changeset
152 '{\n'))
kono
parents:
diff changeset
153
kono
parents:
diff changeset
154 def test_PARAMS(self):
kono
parents:
diff changeset
155 self.assert_filters_to(
kono
parents:
diff changeset
156 'char *strcpy PARAMS ((char *dest, char *source));\n',
kono
parents:
diff changeset
157 'char *strcpy (char *dest, char *source);\n')
kono
parents:
diff changeset
158
kono
parents:
diff changeset
159 def test_ENUM_BITFIELD(self):
kono
parents:
diff changeset
160 self.assert_filters_to(
kono
parents:
diff changeset
161 ' ENUM_BITFIELD (sym_intent) intent:2;\n',
kono
parents:
diff changeset
162 ' enum sym_intent intent:2;\n')
kono
parents:
diff changeset
163
kono
parents:
diff changeset
164 def act_on_files(argv):
kono
parents:
diff changeset
165 for filename in argv[1:]:
kono
parents:
diff changeset
166 with open(filename) as f:
kono
parents:
diff changeset
167 text = f.read()
kono
parents:
diff changeset
168 print(filter_src(text))
kono
parents:
diff changeset
169
kono
parents:
diff changeset
170 if __name__ == '__main__':
kono
parents:
diff changeset
171 if len(sys.argv) > 1:
kono
parents:
diff changeset
172 act_on_files(sys.argv)
kono
parents:
diff changeset
173 else:
kono
parents:
diff changeset
174 unittest.main()