Debian Codegraph @

src/test/generate-sym-test.py

raw
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3 #
4 # ruff: noqa: UP015
5
6 import os
7 import re
8 import sys
9 from pathlib import Path
10 from typing import IO
11
12
13 def process_sym_file(file: IO[str]) -> None:
14 for line in file:
15 m = re.search(r'^ +([a-zA-Z0-9_]+);', line)
16 if m:
17 if m[1] == 'sd_bus_object_vtable_format':
18 print(f' {{ "{m[1]}", &{m[1]} }},')
19 else:
20 print(f' {{ "{m[1]}", {m[1]} }},')
21
22
23 def process_header_file(file: IO[str]) -> str:
24 text = ''
25
26 for line in file:
27 if (
28 line.startswith('#')
29 or line.startswith('typedef')
30 or line.startswith('extern "C"')
31 or line.startswith('__extension__')
32 or line.startswith('/*')
33 or ' __inline__ ' in line
34 or re.search(r'^\s+', line)
35 ):
36 continue
37
38 m = re.search(r'^(.*)\s*__attribute__', line)
39 if m:
40 line = m[1]
41
42 m = re.search(r'^(.*)\s*_sd_printf_', line)
43 if m:
44 line = m[1]
45
46 # Functions
47 m = re.search(r'^(\S+\s+)+\**(\w+)\s*\(', line)
48 if m:
49 text += f' {{ "{m[2]}", {m[2]} }},\n'
50 continue
51
52 # Variables
53 m = re.search(r'^extern\s', line)
54 if m:
55 n = line.split()[-1].rstrip(';')
56 text += f' {{ "{n}", &{n} }},\n'
57 continue
58
59 # Functions defined by macro
60 m = re.search(r'_SD_DEFINE_POINTER_CLEANUP_FUNC\(\w+,\s*(\w+)\)', line)
61 if m:
62 text += f' {{ "{m[1]}", {m[1]} }},\n'
63 continue
64
65 # Functions declared by ref/unref macros
66 m = re.search(r'_SD_DECLARE_TRIVIAL_REF_UNREF_FUNC\((\w+)\)', line)
67 if m:
68 text += f' {{ "{m[1]}_ref", {m[1]}_ref }},\n'
69 text += f' {{ "{m[1]}_unref", {m[1]}_unref }},\n'
70 continue
71
72 m = re.search(r'_SD_DECLARE_TRIVIAL_REF_FUNC\((\w+)\)', line)
73 if m:
74 text += f' {{ "{m[1]}_ref", {m[1]}_ref }},\n'
75 continue
76
77 m = re.search(r'_SD_DECLARE_TRIVIAL_UNREF_FUNC\((\w+)\)', line)
78 if m:
79 text += f' {{ "{m[1]}_unref", {m[1]}_unref }},\n'
80 continue
81
82 return text
83
84
85 def process_source_file(file: IO[str]) -> None:
86 for line in file:
87 # Functions
88 m = re.search(r'^_public_\s+(\S+\s+)+\**(\w+)\s*\(', line)
89 if m:
90 print(f' {{ "{m[2]}", {m[2]} }},')
91 continue
92
93 # Variables
94 m = re.search(r'^_public_\s+(\S+\s+)+\**(\w+)\s*=', line)
95 if m:
96 print(f' {{ "{m[2]}", &{m[2]} }},')
97 continue
98
99 # Functions defined through a macro
100 m = re.search(r'^DEFINE_PUBLIC_TRIVIAL_REF_FUNC\([^,]+,\s*(\w+)\s*\)', line)
101 if m:
102 print(f' {{ "{m[1]}_ref", {m[1]}_ref }},')
103 continue
104
105 m = re.search(r'^DEFINE_PUBLIC_TRIVIAL_UNREF_FUNC\([^,]+,\s*(\w+)\s*,', line)
106 if m:
107 print(f' {{ "{m[1]}_unref", {m[1]}_unref }},')
108 continue
109
110 m = re.search(r'^DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC\([^,]+,\s*(\w+)\s*,', line)
111 if m:
112 print(f' {{ "{m[1]}_ref", {m[1]}_ref }},')
113 print(f' {{ "{m[1]}_unref", {m[1]}_unref }},')
114 continue
115
116 m = re.search(r'^_DEFINE_STRING_TABLE_LOOKUP\((\w+),\s*\w+,\s*_public_\s*\)', line)
117 if m:
118 print(f' {{ "{m[1]}_from_string", {m[1]}_from_string }},')
119 print(f' {{ "{m[1]}_to_string", {m[1]}_to_string }},')
120 continue
121
122
123 print('''/* SPDX-License-Identifier: LGPL-2.1-or-later */
124
125 #include <stdio.h>
126 #include <stdlib.h>
127 #include <string.h>
128
129 ''')
130
131 for header in sys.argv[3:]:
132 with open(header, 'r') as f:
133 if process_header_file(f):
134 print('#include "{}"'.format(header.split('/')[-1]))
135
136 print('''
137 /* We want to check deprecated symbols too, without complaining */
138 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
139 ''')
140
141 print('''
142 struct symbol {
143 const char *name;
144 const void *symbol;
145 };
146 static struct symbol symbols_from_sym[] = {''')
147
148 with open(sys.argv[1], 'r') as f:
149 process_sym_file(f)
150
151 print(''' {}
152 }, symbols_from_header[] = {''')
153
154 for header in sys.argv[3:]:
155 with open(header, 'r') as f:
156 print(process_header_file(f), end='')
157
158 print(''' {}
159 }, symbols_from_source[] = {''')
160
161 for dirpath, _, filenames in sorted(os.walk(sys.argv[2])):
162 for filename in sorted(filenames):
163 if not filename.endswith('.c') and not filename.endswith('.h'):
164 continue
165 p = Path(dirpath) / filename
166 if p.is_symlink():
167 continue
168 with p.open('rt') as f:
169 process_source_file(f)
170
171 print(''' {}
172 };
173
174 static int sort_callback(const void *a, const void *b) {
175 const struct symbol *x = a, *y = b;
176 return strcmp(x->name, y->name);
177 }
178
179 int main(void) {
180 size_t size = sizeof(symbols_from_sym[0]),
181 n_sym = sizeof(symbols_from_sym)/sizeof(symbols_from_sym[0]) - 1,
182 n_header = sizeof(symbols_from_header)/sizeof(symbols_from_header[0]) - 1,
183 n_source = sizeof(symbols_from_source)/sizeof(symbols_from_source[0]) - 1;
184
185 qsort(symbols_from_sym, n_sym, size, sort_callback);
186 qsort(symbols_from_header, n_header, size, sort_callback);
187 qsort(symbols_from_source, n_source, size, sort_callback);
188
189 puts("From symbol file:");
190 for (size_t i = 0; i < n_sym; i++)
191 printf("%p: %s\\n", symbols_from_sym[i].symbol, symbols_from_sym[i].name);
192
193 puts("\\nFrom header files:");
194 for (size_t i = 0; i < n_header; i++)
195 printf("%p: %s\\n", symbols_from_header[i].symbol, symbols_from_header[i].name);
196
197 puts("\\nFrom source files:");
198 for (size_t i = 0; i < n_source; i++)
199 printf("%p: %s\\n", symbols_from_source[i].symbol, symbols_from_source[i].name);
200
201 puts("");
202 printf("Found %zu symbols from symbol file.\\n", n_sym);
203 printf("Found %zu symbols from header files.\\n", n_header);
204 printf("Found %zu symbols from source files.\\n", n_source);
205
206 unsigned n_error = 0;
207
208 for (size_t i = 0; i < n_sym; i++) {
209 if (!bsearch(symbols_from_sym+i, symbols_from_header, n_header, size, sort_callback)) {
210 printf("Found in symbol file, but not in headers: %s\\n", symbols_from_sym[i].name);
211 n_error++;
212 }
213 if (!bsearch(symbols_from_sym+i, symbols_from_source, n_source, size, sort_callback)) {
214 printf("Found in symbol file, but not in sources: %s\\n", symbols_from_sym[i].name);
215 n_error++;
216 }
217 }
218
219 for (size_t i = 0; i < n_header; i++) {
220 if (!bsearch(symbols_from_header+i, symbols_from_sym, n_sym, size, sort_callback)) {
221 printf("Found in header file, but not in symbol file: %s\\n", symbols_from_header[i].name);
222 n_error++;
223 }
224 if (!bsearch(symbols_from_header+i, symbols_from_source, n_source, size, sort_callback)) {
225 printf("Found in header file, but not in sources: %s\\n", symbols_from_header[i].name);
226 n_error++;
227 }
228 }
229
230 for (size_t i = 0; i < n_source; i++) {
231 if (!bsearch(symbols_from_source+i, symbols_from_sym, n_sym, size, sort_callback)) {
232 printf("Found in source file, but not in symbol file: %s\\n", symbols_from_source[i].name);
233 n_error++;
234 }
235 if (!bsearch(symbols_from_source+i, symbols_from_header, n_header, size, sort_callback)) {
236 printf("Found in source file, but not in header: %s\\n", symbols_from_source[i].name);
237 n_error++;
238 }
239 }
240
241 return n_error == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
242 }''')