Debian Codegraph @

Tools/c-analyzer/cpython/_parser.py

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 import os.path
2 import re
3
4 from c_parser.preprocessor import (
5 get_preprocessor as _get_preprocessor,
6 )
7 from c_parser import (
8 parse_file as _parse_file,
9 parse_files as _parse_files,
10 )
11 from . import REPO_ROOT
12
13
14 GLOB_ALL = '**/*'
15
16
17 def _abs(relfile):
18 return os.path.join(REPO_ROOT, relfile)
19
20
21 def clean_lines(text):
22 """Clear out comments, blank lines, and leading/trailing whitespace."""
23 lines = (line.strip() for line in text.splitlines())
24 lines = (line.partition('#')[0].rstrip()
25 for line in lines
26 if line and not line.startswith('#'))
27 glob_all = f'{GLOB_ALL} '
28 lines = (re.sub(r'^[*] ', glob_all, line) for line in lines)
29 lines = (_abs(line) for line in lines)
30 return list(lines)
31
32
33 '''
34 @begin=sh@
35 ./python ../c-parser/cpython.py
36 --exclude '+../c-parser/EXCLUDED'
37 --macros '+../c-parser/MACROS'
38 --incldirs '+../c-parser/INCL_DIRS'
39 --same './Include/cpython/'
40 Include/*.h
41 Include/internal/*.h
42 Modules/**/*.c
43 Objects/**/*.c
44 Parser/**/*.c
45 Python/**/*.c
46 @end=sh@
47 '''
48
49 # XXX Handle these.
50 # Tab separated:
51 EXCLUDED = clean_lines('''
52 # @begin=conf@
53
54 # OSX
55 Modules/_scproxy.c # SystemConfiguration/SystemConfiguration.h
56
57 # Windows
58 Modules/_winapi.c # windows.h
59 Modules/expat/winconfig.h
60 Modules/overlapped.c # winsock.h
61 Python/dynload_win.c # windows.h
62 Python/thread_nt.h
63
64 # other OS-dependent
65 Python/dynload_aix.c # sys/ldr.h
66 Python/dynload_dl.c # dl.h
67 Python/dynload_hpux.c # dl.h
68 Python/emscripten_signal.c
69 Python/thread_pthread.h
70 Python/thread_pthread_stubs.h
71
72 # only huge constants (safe but parsing is slow)
73 Modules/_ssl_data_31.h
74 Modules/_ssl_data_300.h
75 Modules/_ssl_data_111.h
76 Modules/cjkcodecs/mappings_*.h
77 Modules/unicodedata_db.h
78 Modules/unicodename_db.h
79 Objects/unicodetype_db.h
80
81 # generated
82 Python/deepfreeze/*.c
83 Python/frozen_modules/*.h
84 Python/generated_cases.c.h
85 Python/executor_cases.c.h
86 Python/optimizer_cases.c.h
87
88 # not actually source
89 Python/bytecodes.c
90 Python/optimizer_bytecodes.c
91
92 # mimalloc
93 Objects/mimalloc/*.c
94 Include/internal/mimalloc/*.h
95 Include/internal/mimalloc/mimalloc/*.h
96
97 # @end=conf@
98 ''')
99
100 # XXX Fix the parser.
101 EXCLUDED += clean_lines('''
102 # The tool should be able to parse these...
103
104 # The problem with xmlparse.c is that something
105 # has gone wrong where # we handle "maybe inline actual"
106 # in Tools/c-analyzer/c_parser/parser/_global.py.
107 Modules/expat/internal.h
108 Modules/expat/xmlparse.c
109 ''')
110
111 INCL_DIRS = clean_lines('''
112 # @begin=tsv@
113
114 glob dirname
115 * .
116 * ./Include
117 * ./Include/internal
118 * ./Include/internal/mimalloc
119
120 Modules/_decimal/**/*.c Modules/_decimal/libmpdec
121 Modules/_elementtree.c Modules/expat
122 Modules/_hacl/*.c Modules/_hacl/include
123 Modules/_hacl/*.h Modules/_hacl/include
124 Modules/md5module.c Modules/_hacl/include
125 Modules/sha1module.c Modules/_hacl/include
126 Modules/sha2module.c Modules/_hacl/include
127 Modules/sha3module.c Modules/_hacl/include
128 Objects/stringlib/*.h Objects
129
130 # possible system-installed headers, just in case
131 Modules/_tkinter.c /usr/include/tcl8.6
132 Modules/_uuidmodule.c /usr/include/uuid
133 Modules/tkappinit.c /usr/include/tcl
134
135 # @end=tsv@
136 ''')[1:]
137
138 INCLUDES = clean_lines('''
139 # @begin=tsv@
140
141 glob include
142
143 **/*.h Python.h
144 Include/**/*.h object.h
145
146 # for Py_HAVE_CONDVAR
147 Include/internal/pycore_gil.h pycore_condvar.h
148 Python/thread_pthread.h pycore_condvar.h
149
150 # other
151
152 Objects/stringlib/join.h stringlib/stringdefs.h
153 Objects/stringlib/ctype.h stringlib/stringdefs.h
154 Objects/stringlib/transmogrify.h stringlib/stringdefs.h
155 #Objects/stringlib/fastsearch.h stringlib/stringdefs.h
156 #Objects/stringlib/count.h stringlib/stringdefs.h
157 #Objects/stringlib/find.h stringlib/stringdefs.h
158 #Objects/stringlib/partition.h stringlib/stringdefs.h
159 #Objects/stringlib/split.h stringlib/stringdefs.h
160 Objects/stringlib/fastsearch.h stringlib/ucs1lib.h
161 Objects/stringlib/count.h stringlib/ucs1lib.h
162 Objects/stringlib/find.h stringlib/ucs1lib.h
163 Objects/stringlib/partition.h stringlib/ucs1lib.h
164 Objects/stringlib/split.h stringlib/ucs1lib.h
165 Objects/stringlib/find_max_char.h Objects/stringlib/ucs1lib.h
166 Objects/stringlib/count.h Objects/stringlib/fastsearch.h
167 Objects/stringlib/find.h Objects/stringlib/fastsearch.h
168 Objects/stringlib/partition.h Objects/stringlib/fastsearch.h
169 Objects/stringlib/replace.h Objects/stringlib/fastsearch.h
170 Objects/stringlib/split.h Objects/stringlib/fastsearch.h
171
172 # @end=tsv@
173 ''')[1:]
174
175 MACROS = clean_lines('''
176 # @begin=tsv@
177
178 glob name value
179
180 Include/internal/*.h Py_BUILD_CORE 1
181 Python/**/*.c Py_BUILD_CORE 1
182 Python/**/*.h Py_BUILD_CORE 1
183 Parser/**/*.c Py_BUILD_CORE 1
184 Parser/**/*.h Py_BUILD_CORE 1
185 Objects/**/*.c Py_BUILD_CORE 1
186 Objects/**/*.h Py_BUILD_CORE 1
187
188 Modules/_asynciomodule.c Py_BUILD_CORE 1
189 Modules/_codecsmodule.c Py_BUILD_CORE 1
190 Modules/_collectionsmodule.c Py_BUILD_CORE 1
191 Modules/_ctypes/_ctypes.c Py_BUILD_CORE 1
192 Modules/_ctypes/cfield.c Py_BUILD_CORE 1
193 Modules/_cursesmodule.c Py_BUILD_CORE 1
194 Modules/_datetimemodule.c Py_BUILD_CORE 1
195 Modules/_functoolsmodule.c Py_BUILD_CORE 1
196 Modules/_heapqmodule.c Py_BUILD_CORE 1
197 Modules/_io/*.c Py_BUILD_CORE 1
198 Modules/_io/*.h Py_BUILD_CORE 1
199 Modules/_localemodule.c Py_BUILD_CORE 1
200 Modules/_operator.c Py_BUILD_CORE 1
201 Modules/_posixsubprocess.c Py_BUILD_CORE 1
202 Modules/_sre/sre.c Py_BUILD_CORE 1
203 Modules/_threadmodule.c Py_BUILD_CORE 1
204 Modules/_tracemalloc.c Py_BUILD_CORE 1
205 Modules/_weakref.c Py_BUILD_CORE 1
206 Modules/_zoneinfo.c Py_BUILD_CORE 1
207 Modules/atexitmodule.c Py_BUILD_CORE 1
208 Modules/cmathmodule.c Py_BUILD_CORE 1
209 Modules/faulthandler.c Py_BUILD_CORE 1
210 Modules/gcmodule.c Py_BUILD_CORE 1
211 Modules/getpath.c Py_BUILD_CORE 1
212 Modules/getpath_noop.c Py_BUILD_CORE 1
213 Modules/itertoolsmodule.c Py_BUILD_CORE 1
214 Modules/main.c Py_BUILD_CORE 1
215 Modules/mathmodule.c Py_BUILD_CORE 1
216 Modules/posixmodule.c Py_BUILD_CORE 1
217 Modules/sha256module.c Py_BUILD_CORE 1
218 Modules/sha512module.c Py_BUILD_CORE 1
219 Modules/signalmodule.c Py_BUILD_CORE 1
220 Modules/symtablemodule.c Py_BUILD_CORE 1
221 Modules/timemodule.c Py_BUILD_CORE 1
222 Modules/unicodedata.c Py_BUILD_CORE 1
223
224 Modules/_json.c Py_BUILD_CORE_BUILTIN 1
225 Modules/_pickle.c Py_BUILD_CORE_BUILTIN 1
226 Modules/_testinternalcapi.c Py_BUILD_CORE_BUILTIN 1
227
228 Include/cpython/abstract.h Py_CPYTHON_ABSTRACTOBJECT_H 1
229 Include/cpython/bytearrayobject.h Py_CPYTHON_BYTEARRAYOBJECT_H 1
230 Include/cpython/bytesobject.h Py_CPYTHON_BYTESOBJECT_H 1
231 Include/cpython/ceval.h Py_CPYTHON_CEVAL_H 1
232 Include/cpython/code.h Py_CPYTHON_CODE_H 1
233 Include/cpython/dictobject.h Py_CPYTHON_DICTOBJECT_H 1
234 Include/cpython/fileobject.h Py_CPYTHON_FILEOBJECT_H 1
235 Include/cpython/fileutils.h Py_CPYTHON_FILEUTILS_H 1
236 Include/cpython/frameobject.h Py_CPYTHON_FRAMEOBJECT_H 1
237 Include/cpython/import.h Py_CPYTHON_IMPORT_H 1
238 Include/cpython/interpreteridobject.h Py_CPYTHON_INTERPRETERIDOBJECT_H 1
239 Include/cpython/listobject.h Py_CPYTHON_LISTOBJECT_H 1
240 Include/cpython/methodobject.h Py_CPYTHON_METHODOBJECT_H 1
241 Include/cpython/object.h Py_CPYTHON_OBJECT_H 1
242 Include/cpython/objimpl.h Py_CPYTHON_OBJIMPL_H 1
243 Include/cpython/pyerrors.h Py_CPYTHON_ERRORS_H 1
244 Include/cpython/pylifecycle.h Py_CPYTHON_PYLIFECYCLE_H 1
245 Include/cpython/pymem.h Py_CPYTHON_PYMEM_H 1
246 Include/cpython/pystate.h Py_CPYTHON_PYSTATE_H 1
247 Include/cpython/sysmodule.h Py_CPYTHON_SYSMODULE_H 1
248 Include/cpython/traceback.h Py_CPYTHON_TRACEBACK_H 1
249 Include/cpython/tupleobject.h Py_CPYTHON_TUPLEOBJECT_H 1
250 Include/cpython/unicodeobject.h Py_CPYTHON_UNICODEOBJECT_H 1
251
252 # implied include of <unistd.h>
253 Include/**/*.h _POSIX_THREADS 1
254 Include/**/*.h HAVE_PTHREAD_H 1
255
256 # from pyconfig.h
257 Include/cpython/pthread_stubs.h HAVE_PTHREAD_STUBS 1
258 Python/thread_pthread_stubs.h HAVE_PTHREAD_STUBS 1
259
260 # from Objects/bytesobject.c
261 Objects/stringlib/partition.h STRINGLIB_GET_EMPTY() bytes_get_empty()
262 Objects/stringlib/join.h STRINGLIB_MUTABLE 0
263 Objects/stringlib/partition.h STRINGLIB_MUTABLE 0
264 Objects/stringlib/split.h STRINGLIB_MUTABLE 0
265 Objects/stringlib/transmogrify.h STRINGLIB_MUTABLE 0
266
267 # from Makefile
268 Modules/getpath.c PYTHONPATH 1
269 Modules/getpath.c PREFIX ...
270 Modules/getpath.c EXEC_PREFIX ...
271 Modules/getpath.c VERSION ...
272 Modules/getpath.c VPATH ...
273 Modules/getpath.c PLATLIBDIR ...
274 #Modules/_dbmmodule.c USE_GDBM_COMPAT 1
275 Modules/_dbmmodule.c USE_NDBM 1
276 #Modules/_dbmmodule.c USE_BERKDB 1
277
278 # See: setup.py
279 Modules/_decimal/**/*.c CONFIG_64 1
280 Modules/_decimal/**/*.c ASM 1
281 Modules/expat/xmlparse.c HAVE_EXPAT_CONFIG_H 1
282 Modules/expat/xmlparse.c XML_POOR_ENTROPY 1
283 Modules/_dbmmodule.c HAVE_GDBM_DASH_NDBM_H 1
284
285 # others
286 Modules/_sre/sre_lib.h LOCAL(type) static inline type
287 Modules/_sre/sre_lib.h SRE(F) sre_ucs2_##F
288 Objects/stringlib/codecs.h STRINGLIB_IS_UNICODE 1
289
290 # @end=tsv@
291 ''')[1:]
292
293 # -pthread
294 # -Wno-unused-result
295 # -Wsign-compare
296 # -g
297 # -Og
298 # -Wall
299 # -std=c99
300 # -Wextra
301 # -Wno-unused-result -Wno-unused-parameter
302 # -Wno-missing-field-initializers
303 # -Werror=implicit-function-declaration
304
305 SAME = {
306 _abs('Include/*.h'): [_abs('Include/cpython/')],
307 _abs('Python/ceval.c'): ['Python/generated_cases.c.h'],
308 }
309
310 MAX_SIZES = {
311 # GLOB: (MAXTEXT, MAXLINES),
312 # default: (10_000, 200)
313 # First match wins.
314 _abs('Modules/_ctypes/ctypes.h'): (5_000, 500),
315 _abs('Modules/_datetimemodule.c'): (20_000, 300),
316 _abs('Modules/_hacl/*.c'): (200_000, 500),
317 _abs('Modules/posixmodule.c'): (20_000, 500),
318 _abs('Modules/termios.c'): (10_000, 800),
319 _abs('Modules/_testcapimodule.c'): (20_000, 400),
320 _abs('Modules/expat/expat.h'): (10_000, 400),
321 _abs('Objects/stringlib/unicode_format.h'): (10_000, 400),
322 _abs('Objects/typeobject.c'): (35_000, 200),
323 _abs('Python/compile.c'): (20_000, 500),
324 _abs('Python/optimizer.c'): (100_000, 5_000),
325 _abs('Python/parking_lot.c'): (40_000, 1000),
326 _abs('Python/pylifecycle.c'): (500_000, 5000),
327 _abs('Python/pystate.c'): (500_000, 5000),
328
329 # Generated files:
330 _abs('Include/internal/pycore_opcode.h'): (10_000, 1000),
331 _abs('Include/internal/pycore_global_strings.h'): (5_000, 1000),
332 _abs('Include/internal/pycore_runtime_init_generated.h'): (5_000, 1000),
333 _abs('Python/deepfreeze/*.c'): (20_000, 500),
334 _abs('Python/frozen_modules/*.h'): (20_000, 500),
335 _abs('Python/opcode_targets.h'): (10_000, 500),
336 _abs('Python/stdlib_module_names.h'): (5_000, 500),
337
338 # These large files are currently ignored (see above).
339 _abs('Modules/_ssl_data_31.h'): (80_000, 10_000),
340 _abs('Modules/_ssl_data_300.h'): (80_000, 10_000),
341 _abs('Modules/_ssl_data_111.h'): (80_000, 10_000),
342 _abs('Modules/cjkcodecs/mappings_*.h'): (160_000, 2_000),
343 _abs('Modules/unicodedata_db.h'): (180_000, 3_000),
344 _abs('Modules/unicodename_db.h'): (1_200_000, 15_000),
345 _abs('Objects/unicodetype_db.h'): (240_000, 3_000),
346
347 # Catch-alls:
348 _abs('Include/**/*.h'): (5_000, 500),
349 }
350
351
352 def get_preprocessor(*,
353 file_macros=None,
354 file_includes=None,
355 file_incldirs=None,
356 file_same=None,
357 **kwargs
358 ):
359 macros = tuple(MACROS)
360 if file_macros:
361 macros += tuple(file_macros)
362 includes = tuple(INCLUDES)
363 if file_includes:
364 includes += tuple(file_includes)
365 incldirs = tuple(INCL_DIRS)
366 if file_incldirs:
367 incldirs += tuple(file_incldirs)
368 samefiles = dict(SAME)
369 if file_same:
370 samefiles.update(file_same)
371 return _get_preprocessor(
372 file_macros=macros,
373 file_includes=includes,
374 file_incldirs=incldirs,
375 file_same=samefiles,
376 **kwargs
377 )
378
379
380 def parse_file(filename, *,
381 match_kind=None,
382 ignore_exc=None,
383 log_err=None,
384 ):
385 get_file_preprocessor = get_preprocessor(
386 ignore_exc=ignore_exc,
387 log_err=log_err,
388 )
389 yield from _parse_file(
390 filename,
391 match_kind=match_kind,
392 get_file_preprocessor=get_file_preprocessor,
393 file_maxsizes=MAX_SIZES,
394 )
395
396
397 def parse_files(filenames=None, *,
398 match_kind=None,
399 ignore_exc=None,
400 log_err=None,
401 get_file_preprocessor=None,
402 **file_kwargs
403 ):
404 if get_file_preprocessor is None:
405 get_file_preprocessor = get_preprocessor(
406 ignore_exc=ignore_exc,
407 log_err=log_err,
408 )
409 yield from _parse_files(
410 filenames,
411 match_kind=match_kind,
412 get_file_preprocessor=get_file_preprocessor,
413 file_maxsizes=MAX_SIZES,
414 **file_kwargs
415 )