Skip to content

Commit 8606697

Browse files
gh-90110: Fix the c-analyzer Tool (#102483)
Some incompatible changes had gone in, and the "ignore" lists weren't properly undated. This change fixes that. It's necessary prior to enabling test_check_c_globals, which I hope to do soon. Note that this does include moving last_resort_memory_error to PyInterpreterState. #90110
1 parent f9cdeb7 commit 8606697

File tree

11 files changed

+85
-34
lines changed

11 files changed

+85
-34
lines changed

Include/internal/pycore_global_objects.h

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ struct _Py_interp_static_objects {
8686
// hamt_empty is here instead of global because of its weakreflist.
8787
_PyGC_Head_UNUSED _hamt_empty_gc_not_used;
8888
PyHamtObject hamt_empty;
89+
PyBaseExceptionObject last_resort_memory_error;
8990
} singletons;
9091
};
9192

Include/internal/pycore_intrinsics.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
typedef PyObject *(*instrinsic_func1)(PyThreadState* tstate, PyObject *value);
2222
typedef PyObject *(*instrinsic_func2)(PyThreadState* tstate, PyObject *value1, PyObject *value2);
2323

24-
extern instrinsic_func1 _PyIntrinsics_UnaryFunctions[];
25-
extern instrinsic_func2 _PyIntrinsics_BinaryFunctions[];
24+
extern const instrinsic_func1 _PyIntrinsics_UnaryFunctions[];
25+
extern const instrinsic_func2 _PyIntrinsics_BinaryFunctions[];
2626

Include/internal/pycore_runtime_init.h

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ extern "C" {
1414
#include "pycore_obmalloc_init.h"
1515

1616

17+
extern PyTypeObject _PyExc_MemoryError;
18+
19+
1720
/* The static initializers defined here should only be used
1821
in the runtime init code (in pystate.c and pylifecycle.c). */
1922

@@ -120,6 +123,9 @@ extern "C" {
120123
.ob_base = _PyObject_IMMORTAL_INIT(&_PyHamt_Type), \
121124
.h_root = (PyHamtNode*)&_Py_SINGLETON(hamt_bitmap_node_empty), \
122125
}, \
126+
.last_resort_memory_error = { \
127+
_PyObject_IMMORTAL_INIT(&_PyExc_MemoryError), \
128+
}, \
123129
}, \
124130
}, \
125131
._initial_thread = _PyThreadState_INIT, \

Objects/exceptions.c

+4-9
Original file line numberDiff line numberDiff line change
@@ -3207,16 +3207,16 @@ SimpleExtendsException(PyExc_Exception, ReferenceError,
32073207

32083208
#define MEMERRORS_SAVE 16
32093209

3210-
static PyBaseExceptionObject last_resort_memory_error;
3211-
32123210
static PyObject *
32133211
get_memory_error(int allow_allocation, PyObject *args, PyObject *kwds)
32143212
{
32153213
PyBaseExceptionObject *self;
32163214
struct _Py_exc_state *state = get_exc_state();
32173215
if (state->memerrors_freelist == NULL) {
32183216
if (!allow_allocation) {
3219-
return Py_NewRef(&last_resort_memory_error);
3217+
PyInterpreterState *interp = _PyInterpreterState_GET();
3218+
return Py_NewRef(
3219+
&_Py_INTERP_SINGLETON(interp, last_resort_memory_error));
32203220
}
32213221
PyObject *result = BaseException_new((PyTypeObject *)PyExc_MemoryError, args, kwds);
32223222
return result;
@@ -3239,8 +3239,6 @@ get_memory_error(int allow_allocation, PyObject *args, PyObject *kwds)
32393239
return (PyObject *)self;
32403240
}
32413241

3242-
static PyBaseExceptionObject last_resort_memory_error;
3243-
32443242
static PyObject *
32453243
MemoryError_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
32463244
{
@@ -3325,7 +3323,7 @@ free_preallocated_memerrors(struct _Py_exc_state *state)
33253323
}
33263324

33273325

3328-
static PyTypeObject _PyExc_MemoryError = {
3326+
PyTypeObject _PyExc_MemoryError = {
33293327
PyVarObject_HEAD_INIT(NULL, 0)
33303328
"MemoryError",
33313329
sizeof(PyBaseExceptionObject),
@@ -3339,9 +3337,6 @@ static PyTypeObject _PyExc_MemoryError = {
33393337
};
33403338
PyObject *PyExc_MemoryError = (PyObject *) &_PyExc_MemoryError;
33413339

3342-
static PyBaseExceptionObject last_resort_memory_error = {
3343-
_PyObject_IMMORTAL_INIT(&_PyExc_MemoryError)
3344-
};
33453340

33463341
/*
33473342
* BufferError extends Exception

Python/intrinsics.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ list_to_tuple(PyThreadState* unused, PyObject *v)
199199
return _PyTuple_FromArray(((PyListObject *)v)->ob_item, Py_SIZE(v));
200200
}
201201

202-
instrinsic_func1
202+
const instrinsic_func1
203203
_PyIntrinsics_UnaryFunctions[] = {
204204
[0] = no_intrinsic,
205205
[INTRINSIC_PRINT] = print_expr,
@@ -221,7 +221,7 @@ prep_reraise_star(PyThreadState* unused, PyObject *orig, PyObject *excs)
221221
return _PyExc_PrepReraiseStar(orig, excs);
222222
}
223223

224-
instrinsic_func2
224+
const instrinsic_func2
225225
_PyIntrinsics_BinaryFunctions[] = {
226226
[INTRINSIC_PREP_RERAISE_STAR] = prep_reraise_star,
227227
};

Tools/c-analyzer/c_parser/parser/_common.py

+17-5
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,25 @@
77
)
88

99

10-
def log_match(group, m):
10+
def log_match(group, m, depth_before=None, depth_after=None):
1111
from . import _logger
12-
text = m.group(0)
13-
if text.startswith(('(', ')')) or text.endswith(('(', ')')):
14-
_logger.debug(f'matched <{group}> ({text!r})')
12+
13+
if m is not None:
14+
text = m.group(0)
15+
if text.startswith(('(', ')')) or text.endswith(('(', ')')):
16+
_logger.debug(f'matched <{group}> ({text!r})')
17+
else:
18+
_logger.debug(f'matched <{group}> ({text})')
19+
20+
elif depth_before is not None or depth_after is not None:
21+
if depth_before is None:
22+
depth_before = '???'
23+
elif depth_after is None:
24+
depth_after = '???'
25+
_logger.log(1, f'depth: %s -> %s', depth_before, depth_after)
26+
1527
else:
16-
_logger.debug(f'matched <{group}> ({text})')
28+
raise NotImplementedError('this should not have been hit')
1729

1830

1931
#############################

Tools/c-analyzer/c_parser/parser/_func_body.py

+14-11
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ def parse_function_body(name, text, resolve, source, anon_name, parent):
6565
) = m.groups()
6666

6767
if empty:
68-
log_match('', m)
68+
log_match('', m, depth)
6969
resolve(None, None, None, text)
7070
yield None, text
7171
elif inline_kind:
72-
log_match('', m)
72+
log_match('', m, depth)
7373
kind = inline_kind
7474
name = inline_name or anon_name('inline-')
7575
data = [] # members
@@ -92,7 +92,7 @@ def parse_function_body(name, text, resolve, source, anon_name, parent):
9292
# XXX Should "parent" really be None for inline type decls?
9393
yield resolve(kind, data, name, text, None), text
9494
elif block_close:
95-
log_match('', m)
95+
log_match('', m, depth)
9696
depth -= 1
9797
resolve(None, None, None, text)
9898
# XXX This isn't great. Calling resolve() should have
@@ -101,13 +101,13 @@ def parse_function_body(name, text, resolve, source, anon_name, parent):
101101
# needs to be fixed.
102102
yield None, text
103103
elif compound_bare:
104-
log_match('', m)
104+
log_match('', m, depth)
105105
yield resolve('statement', compound_bare, None, text, parent), text
106106
elif compound_labeled:
107-
log_match('', m)
107+
log_match('', m, depth)
108108
yield resolve('statement', compound_labeled, None, text, parent), text
109109
elif compound_paren:
110-
log_match('', m)
110+
log_match('', m, depth)
111111
try:
112112
pos = match_paren(text)
113113
except ValueError:
@@ -132,7 +132,7 @@ def parse_function_body(name, text, resolve, source, anon_name, parent):
132132
}
133133
yield resolve('statement', data, None, text, parent), text
134134
elif block_open:
135-
log_match('', m)
135+
log_match('', m, depth)
136136
depth += 1
137137
if block_leading:
138138
# An inline block: the last evaluated expression is used
@@ -144,10 +144,10 @@ def parse_function_body(name, text, resolve, source, anon_name, parent):
144144
resolve(None, None, None, text)
145145
yield None, text
146146
elif simple_ending:
147-
log_match('', m)
147+
log_match('', m, depth)
148148
yield resolve('statement', simple_stmt, None, text, parent), text
149149
elif var_ending:
150-
log_match('', m)
150+
log_match('', m, depth)
151151
kind = 'variable'
152152
_, name, vartype = parse_var_decl(decl)
153153
data = {
@@ -220,7 +220,7 @@ def _parse_next_local_static(m, srcinfo, anon_name, func, depth):
220220
remainder = srcinfo.text[m.end():]
221221

222222
if inline_kind:
223-
log_match('func inline', m)
223+
log_match('func inline', m, depth, depth)
224224
kind = inline_kind
225225
name = inline_name or anon_name('inline-')
226226
# Immediately emit a forward declaration.
@@ -249,7 +249,7 @@ def parse_body(source):
249249
yield parse_body, depth
250250

251251
elif static_decl:
252-
log_match('local variable', m)
252+
log_match('local variable', m, depth, depth)
253253
_, name, data = parse_var_decl(static_decl)
254254

255255
yield srcinfo.resolve('variable', data, name, parent=func), depth
@@ -266,10 +266,13 @@ def parse_body(source):
266266
else:
267267
log_match('func other', m)
268268
if block_open:
269+
log_match('func other', None, depth, depth + 1)
269270
depth += 1
270271
elif block_close:
272+
log_match('func other', None, depth, depth - 1)
271273
depth -= 1
272274
elif stmt_end:
275+
log_match('func other', None, depth, depth)
273276
pass
274277
else:
275278
# This should be unreachable.

Tools/c-analyzer/c_parser/preprocessor/gcc.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
[^()]*
3030
)*
3131
) # <args>
32-
( [)] [)] )? # <closed>
32+
( [)] [)] ) # <closed>
3333
''', re.VERBOSE)
3434

3535
POST_ARGS = (
@@ -156,6 +156,7 @@ def _iter_top_include_lines(lines, topfile, cwd,
156156
if name != 'pragma':
157157
raise Exception(line)
158158
else:
159+
line = re.sub(r'__inline__', 'inline', line)
159160
if not raw:
160161
line, partial = _strip_directives(line, partial=partial)
161162
yield _common.SourceLine(

Tools/c-analyzer/cpython/_parser.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,14 @@ def clean_lines(text):
105105
* ./Include
106106
* ./Include/internal
107107
108+
Modules/_decimal/**/*.c Modules/_decimal/libmpdec
109+
Modules/_hacl/*.c Modules/_hacl/include
110+
Modules/_hacl/*.h Modules/_hacl/include
108111
Modules/_tkinter.c /usr/include/tcl8.6
112+
Modules/md5module.c Modules/_hacl/include
113+
Modules/sha1module.c Modules/_hacl/include
114+
Modules/sha2module.c Modules/_hacl/include
109115
Modules/tkappinit.c /usr/include/tcl
110-
Modules/_decimal/**/*.c Modules/_decimal/libmpdec
111116
Objects/stringlib/*.h Objects
112117
113118
# @end=tsv@
@@ -173,6 +178,7 @@ def clean_lines(text):
173178
Modules/_functoolsmodule.c Py_BUILD_CORE 1
174179
Modules/_heapqmodule.c Py_BUILD_CORE 1
175180
Modules/_io/*.c Py_BUILD_CORE 1
181+
Modules/_io/*.h Py_BUILD_CORE 1
176182
Modules/_localemodule.c Py_BUILD_CORE 1
177183
Modules/_operator.c Py_BUILD_CORE 1
178184
Modules/_posixsubprocess.c Py_BUILD_CORE 1
@@ -296,6 +302,7 @@ def clean_lines(text):
296302
# First match wins.
297303
_abs('Modules/_ctypes/ctypes.h'): (5_000, 500),
298304
_abs('Modules/_datetimemodule.c'): (20_000, 300),
305+
_abs('Modules/_hacl/*.c'): (200_000, 500),
299306
_abs('Modules/posixmodule.c'): (20_000, 500),
300307
_abs('Modules/termios.c'): (10_000, 800),
301308
_abs('Modules/_testcapimodule.c'): (20_000, 400),

Tools/c-analyzer/cpython/globals-to-fix.tsv

+1-1
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ Modules/_tkinter.c - command_mutex -
539539
Modules/_tkinter.c - HeadFHCD -
540540
Modules/_tkinter.c - stdin_ready -
541541
Modules/_tkinter.c - event_tstate -
542-
Modules/_xxsubinterpretersmodule.c - _globals -
542+
Modules/_xxinterpchannelsmodule.c - _globals -
543543
Modules/readline.c - completer_word_break_characters -
544544
Modules/readline.c - _history_length -
545545
Modules/readline.c - should_auto_add_history -

Tools/c-analyzer/cpython/ignored.tsv

+28-2
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ Modules/_decimal/_decimal.c - invalid_signals_err -
206206
Modules/_decimal/_decimal.c - signal_map -
207207
Modules/_decimal/_decimal.c - ssize_constants -
208208
Modules/_elementtree.c - ExpatMemoryHandler -
209+
Modules/_hashopenssl.c - py_hashes -
210+
Modules/_hacl/Hacl_Hash_SHA1.c - _h0 -
211+
Modules/_hacl/Hacl_Hash_MD5.c - _h0 -
212+
Modules/_hacl/Hacl_Hash_MD5.c - _t -
209213
Modules/_io/_iomodule.c - static_types -
210214
Modules/_io/textio.c - encodefuncs -
211215
Modules/_io/winconsoleio.c - _PyWindowsConsoleIO_Type -
@@ -218,9 +222,10 @@ Modules/_sre/sre_targets.h - sre_targets -
218222
Modules/_sre.c pattern_repr flag_names -
219223
Modules/_struct.c - bigendian_table -
220224
Modules/_struct.c - lilendian_table -
225+
Modules/_struct.c - native_table -
221226
Modules/_tkinter.c - state_key -
222-
Modules/_xxsubinterpretersmodule.c - _channelid_end_recv -
223-
Modules/_xxsubinterpretersmodule.c - _channelid_end_send -
227+
Modules/_xxinterpchannelsmodule.c - _channelid_end_recv -
228+
Modules/_xxinterpchannelsmodule.c - _channelid_end_send -
224229
Modules/_zoneinfo.c - DAYS_BEFORE_MONTH -
225230
Modules/_zoneinfo.c - DAYS_IN_MONTH -
226231
Modules/arraymodule.c - descriptors -
@@ -332,12 +337,15 @@ Python/frozen.c - _PyImport_FrozenTest -
332337
Python/getopt.c - longopts -
333338
Python/import.c - _PyImport_Inittab -
334339
Python/import.c - _PySys_ImplCacheTag -
340+
Python/intrinsics.c - _PyIntrinsics_UnaryFunctions -
341+
Python/intrinsics.c - _PyIntrinsics_BinaryFunctions -
335342
Python/opcode_targets.h - opcode_targets -
336343
Python/perf_trampoline.c - _Py_perfmap_callbacks -
337344
Python/pyhash.c - PyHash_Func -
338345
Python/pylifecycle.c - _C_LOCALE_WARNING -
339346
Python/pylifecycle.c - _PyOS_mystrnicmp_hack -
340347
Python/pylifecycle.c - _TARGET_LOCALES -
348+
Python/pylifecycle.c - INTERPRETER_TRAMPOLINE_CODEDEF -
341349
Python/pystate.c - initial -
342350
Python/specialize.c - adaptive_opcodes -
343351
Python/specialize.c - cache_requirements -
@@ -392,8 +400,23 @@ Modules/_testbuffer.c ndarray_memoryview_from_buffer strides -
392400
Modules/_testbuffer.c ndarray_memoryview_from_buffer suboffsets -
393401
Modules/_testbuffer.c ndarray_push kwlist -
394402
Modules/_testbuffer.c staticarray_init kwlist -
403+
Modules/_testcapi/code.c get_code_extra_index key -
404+
Modules/_testcapi/datetime.c - test_run_counter -
405+
Modules/_testcapi/exceptions.c - PyRecursingInfinitelyError_Type -
395406
Modules/_testcapi/heaptype.c - _testcapimodule -
407+
Modules/_testcapi/mem.c - FmData -
408+
Modules/_testcapi/mem.c - FmHook -
409+
Modules/_testcapi/structmember.c - test_structmembersType_OldAPI -
396410
Modules/_testcapi/unicode.c - _testcapimodule -
411+
Modules/_testcapi/watchers.c - g_dict_watch_events -
412+
Modules/_testcapi/watchers.c - g_dict_watchers_installed -
413+
Modules/_testcapi/watchers.c - g_type_modified_events -
414+
Modules/_testcapi/watchers.c - g_type_watchers_installed -
415+
Modules/_testcapi/watchers.c - num_code_object_created_events -
416+
Modules/_testcapi/watchers.c - num_code_object_destroyed_events -
417+
Modules/_testcapi/watchers.c - pyfunc_watchers -
418+
Modules/_testcapi/watchers.c - func_watcher_ids -
419+
Modules/_testcapi/watchers.c - func_watcher_callbacks -
397420
Modules/_testcapimodule.c - ContainerNoGC_members -
398421
Modules/_testcapimodule.c - ContainerNoGC_type -
399422
Modules/_testcapimodule.c - FmData -
@@ -460,11 +483,13 @@ Modules/_testcapimodule.c - meth_static_methods -
460483
Modules/_testcapimodule.c - ml -
461484
Modules/_testcapimodule.c - str1 -
462485
Modules/_testcapimodule.c - str2 -
486+
Modules/_testcapimodule.c - test_c_thread -
463487
Modules/_testcapimodule.c - test_members -
464488
Modules/_testcapimodule.c - test_run_counter -
465489
Modules/_testcapimodule.c - test_structmembersType -
466490
Modules/_testcapimodule.c - thread_done -
467491
Modules/_testcapimodule.c - x -
492+
Modules/_testcapimodule.c - wait_done -
468493
Modules/_testcapimodule.c getargs_keyword_only keywords -
469494
Modules/_testcapimodule.c getargs_keywords keywords -
470495
Modules/_testcapimodule.c getargs_positional_only_and_keywords keywords -
@@ -526,6 +551,7 @@ Modules/_testmultiphase.c - slots_exec_unreported_exception -
526551
Modules/_testmultiphase.c - slots_nonmodule_with_exec_slots -
527552
Modules/_testmultiphase.c - testexport_methods -
528553
Modules/_testmultiphase.c - uninitialized_def -
554+
Modules/_testsinglephase.c - global_state -
529555
Modules/_xxtestfuzz/_xxtestfuzz.c - _fuzzmodule -
530556
Modules/_xxtestfuzz/_xxtestfuzz.c - module_methods -
531557
Modules/_xxtestfuzz/fuzzer.c - SRE_FLAG_DEBUG -

0 commit comments

Comments
 (0)