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