GH-125521: Remove `if (true)` from generated output to reduce C compiler warnings (GH-125700)

This commit is contained in:
Mark Shannon 2024-10-22 10:11:29 +01:00 committed by GitHub
parent c1bdbe84c8
commit 57e3c59bb6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 75 additions and 40 deletions

View File

@ -1270,6 +1270,33 @@ class TestGeneratedCases(unittest.TestCase):
""" """
self.run_cases_test(input, output) self.run_cases_test(input, output)
def test_error_if_true(self):
input = """
inst(OP1, ( --)) {
ERROR_IF(true, here);
}
inst(OP2, ( --)) {
ERROR_IF(1, there);
}
"""
output = """
TARGET(OP1) {
frame->instr_ptr = next_instr;
next_instr += 1;
INSTRUCTION_STATS(OP1);
goto here;
}
TARGET(OP2) {
frame->instr_ptr = next_instr;
next_instr += 1;
INSTRUCTION_STATS(OP2);
goto there;
}
"""
self.run_cases_test(input, output)
def test_scalar_array_inconsistency(self): def test_scalar_array_inconsistency(self):
input = """ input = """

View File

@ -689,7 +689,7 @@
for (int _i = oparg*2; --_i >= 0;) { for (int _i = oparg*2; --_i >= 0;) {
PyStackRef_CLOSE(values[_i]); PyStackRef_CLOSE(values[_i]);
} }
if (true) { {
stack_pointer += -oparg*2; stack_pointer += -oparg*2;
assert(WITHIN_STACK_BOUNDS()); assert(WITHIN_STACK_BOUNDS());
goto error; goto error;
@ -731,7 +731,7 @@
for (int _i = oparg; --_i >= 0;) { for (int _i = oparg; --_i >= 0;) {
PyStackRef_CLOSE(values[_i]); PyStackRef_CLOSE(values[_i]);
} }
if (true) { {
stack_pointer += -oparg; stack_pointer += -oparg;
assert(WITHIN_STACK_BOUNDS()); assert(WITHIN_STACK_BOUNDS());
goto error; goto error;
@ -748,7 +748,7 @@
} }
if (err != 0) { if (err != 0) {
Py_DECREF(set_o); Py_DECREF(set_o);
if (true) { {
stack_pointer += -oparg; stack_pointer += -oparg;
assert(WITHIN_STACK_BOUNDS()); assert(WITHIN_STACK_BOUNDS());
goto error; goto error;
@ -803,7 +803,7 @@
for (int _i = oparg; --_i >= 0;) { for (int _i = oparg; --_i >= 0;) {
PyStackRef_CLOSE(pieces[_i]); PyStackRef_CLOSE(pieces[_i]);
} }
if (true) { {
stack_pointer += -oparg; stack_pointer += -oparg;
assert(WITHIN_STACK_BOUNDS()); assert(WITHIN_STACK_BOUNDS());
goto error; goto error;
@ -945,7 +945,7 @@
for (int i = 0; i < total_args; i++) { for (int i = 0; i < total_args; i++) {
PyStackRef_CLOSE(args[i]); PyStackRef_CLOSE(args[i]);
} }
if (true) { {
stack_pointer += -2 - oparg; stack_pointer += -2 - oparg;
assert(WITHIN_STACK_BOUNDS()); assert(WITHIN_STACK_BOUNDS());
goto error; goto error;
@ -1343,7 +1343,7 @@
for (int _i = oparg; --_i >= 0;) { for (int _i = oparg; --_i >= 0;) {
PyStackRef_CLOSE(args[_i]); PyStackRef_CLOSE(args[_i]);
} }
if (true) { {
stack_pointer += -2 - oparg; stack_pointer += -2 - oparg;
assert(WITHIN_STACK_BOUNDS()); assert(WITHIN_STACK_BOUNDS());
goto error; goto error;
@ -1422,7 +1422,7 @@
for (int _i = oparg; --_i >= 0;) { for (int _i = oparg; --_i >= 0;) {
PyStackRef_CLOSE(args[_i]); PyStackRef_CLOSE(args[_i]);
} }
if (true) { {
stack_pointer += -2 - oparg; stack_pointer += -2 - oparg;
assert(WITHIN_STACK_BOUNDS()); assert(WITHIN_STACK_BOUNDS());
goto error; goto error;
@ -1509,7 +1509,7 @@
for (int _i = oparg; --_i >= 0;) { for (int _i = oparg; --_i >= 0;) {
PyStackRef_CLOSE(args[_i]); PyStackRef_CLOSE(args[_i]);
} }
if (true) { {
stack_pointer += -2 - oparg; stack_pointer += -2 - oparg;
assert(WITHIN_STACK_BOUNDS()); assert(WITHIN_STACK_BOUNDS());
goto error; goto error;
@ -1971,7 +1971,7 @@
PyStackRef_CLOSE(args[_i]); PyStackRef_CLOSE(args[_i]);
} }
PyStackRef_CLOSE(kwnames); PyStackRef_CLOSE(kwnames);
if (true) { {
stack_pointer += -3 - oparg; stack_pointer += -3 - oparg;
assert(WITHIN_STACK_BOUNDS()); assert(WITHIN_STACK_BOUNDS());
goto error; goto error;
@ -2174,7 +2174,7 @@
PyStackRef_CLOSE(args[_i]); PyStackRef_CLOSE(args[_i]);
} }
PyStackRef_CLOSE(kwnames); PyStackRef_CLOSE(kwnames);
if (true) { {
stack_pointer += -3 - oparg; stack_pointer += -3 - oparg;
assert(WITHIN_STACK_BOUNDS()); assert(WITHIN_STACK_BOUNDS());
goto error; goto error;
@ -2431,7 +2431,7 @@
for (int _i = oparg; --_i >= 0;) { for (int _i = oparg; --_i >= 0;) {
PyStackRef_CLOSE(args[_i]); PyStackRef_CLOSE(args[_i]);
} }
if (true) { {
stack_pointer += -2 - oparg; stack_pointer += -2 - oparg;
assert(WITHIN_STACK_BOUNDS()); assert(WITHIN_STACK_BOUNDS());
goto error; goto error;
@ -2516,7 +2516,7 @@
for (int _i = oparg; --_i >= 0;) { for (int _i = oparg; --_i >= 0;) {
PyStackRef_CLOSE(args[_i]); PyStackRef_CLOSE(args[_i]);
} }
if (true) { {
stack_pointer += -2 - oparg; stack_pointer += -2 - oparg;
assert(WITHIN_STACK_BOUNDS()); assert(WITHIN_STACK_BOUNDS());
goto error; goto error;
@ -2749,7 +2749,7 @@
for (int _i = oparg; --_i >= 0;) { for (int _i = oparg; --_i >= 0;) {
PyStackRef_CLOSE(args[_i]); PyStackRef_CLOSE(args[_i]);
} }
if (true) { {
stack_pointer += -2 - oparg; stack_pointer += -2 - oparg;
assert(WITHIN_STACK_BOUNDS()); assert(WITHIN_STACK_BOUNDS());
goto error; goto error;
@ -3103,7 +3103,7 @@
if (err < 0) { if (err < 0) {
PyStackRef_CLOSE(exc_value_st); PyStackRef_CLOSE(exc_value_st);
PyStackRef_CLOSE(match_type_st); PyStackRef_CLOSE(match_type_st);
if (true) goto pop_2_error; goto pop_2_error;
} }
PyObject *match_o = NULL; PyObject *match_o = NULL;
PyObject *rest_o = NULL; PyObject *rest_o = NULL;
@ -3149,7 +3149,7 @@
stack_pointer = _PyFrame_GetStackPointer(frame); stack_pointer = _PyFrame_GetStackPointer(frame);
if (err < 0) { if (err < 0) {
PyStackRef_CLOSE(right); PyStackRef_CLOSE(right);
if (true) goto pop_1_error; goto pop_1_error;
} }
_PyFrame_SetStackPointer(frame, stack_pointer); _PyFrame_SetStackPointer(frame, stack_pointer);
int res = PyErr_GivenExceptionMatches(left_o, right_o); int res = PyErr_GivenExceptionMatches(left_o, right_o);
@ -3583,7 +3583,7 @@
PyTuple_GetItem(_PyFrame_GetCode(frame)->co_localsplusnames, oparg) PyTuple_GetItem(_PyFrame_GetCode(frame)->co_localsplusnames, oparg)
); );
stack_pointer = _PyFrame_GetStackPointer(frame); stack_pointer = _PyFrame_GetStackPointer(frame);
if (1) goto error; goto error;
} }
SETLOCAL(oparg, PyStackRef_NULL); SETLOCAL(oparg, PyStackRef_NULL);
DISPATCH(); DISPATCH();
@ -3682,7 +3682,7 @@
_PyEval_FormatKwargsError(tstate, callable_o, update_o); _PyEval_FormatKwargsError(tstate, callable_o, update_o);
stack_pointer = _PyFrame_GetStackPointer(frame); stack_pointer = _PyFrame_GetStackPointer(frame);
PyStackRef_CLOSE(update); PyStackRef_CLOSE(update);
if (true) goto pop_1_error; goto pop_1_error;
} }
PyStackRef_CLOSE(update); PyStackRef_CLOSE(update);
stack_pointer += -1; stack_pointer += -1;
@ -3715,7 +3715,7 @@
stack_pointer = _PyFrame_GetStackPointer(frame); stack_pointer = _PyFrame_GetStackPointer(frame);
} }
PyStackRef_CLOSE(update); PyStackRef_CLOSE(update);
if (true) goto pop_1_error; goto pop_1_error;
} }
PyStackRef_CLOSE(update); PyStackRef_CLOSE(update);
stack_pointer += -1; stack_pointer += -1;
@ -4173,7 +4173,7 @@
type->tp_name); type->tp_name);
stack_pointer = _PyFrame_GetStackPointer(frame); stack_pointer = _PyFrame_GetStackPointer(frame);
PyStackRef_CLOSE(obj); PyStackRef_CLOSE(obj);
if (true) goto pop_1_error; goto pop_1_error;
} }
_PyFrame_SetStackPointer(frame, stack_pointer); _PyFrame_SetStackPointer(frame, stack_pointer);
iter_o = (*getter)(obj_o); iter_o = (*getter)(obj_o);
@ -4191,7 +4191,7 @@
Py_TYPE(iter_o)->tp_name); Py_TYPE(iter_o)->tp_name);
stack_pointer = _PyFrame_GetStackPointer(frame); stack_pointer = _PyFrame_GetStackPointer(frame);
Py_DECREF(iter_o); Py_DECREF(iter_o);
if (true) goto error; goto error;
} }
iter = PyStackRef_FromPyObjectSteal(iter_o); iter = PyStackRef_FromPyObjectSteal(iter_o);
stack_pointer[-1] = iter; stack_pointer[-1] = iter;
@ -4458,7 +4458,7 @@
for (int i = 0; i < total_args; i++) { for (int i = 0; i < total_args; i++) {
PyStackRef_CLOSE(args[i]); PyStackRef_CLOSE(args[i]);
} }
if (true) { {
stack_pointer += -2 - oparg; stack_pointer += -2 - oparg;
assert(WITHIN_STACK_BOUNDS()); assert(WITHIN_STACK_BOUNDS());
goto error; goto error;
@ -5210,7 +5210,7 @@
stack_pointer = _PyFrame_GetStackPointer(frame); stack_pointer = _PyFrame_GetStackPointer(frame);
} }
PyStackRef_CLOSE(iterable_st); PyStackRef_CLOSE(iterable_st);
if (true) goto pop_1_error; goto pop_1_error;
} }
assert(Py_IsNone(none_val)); assert(Py_IsNone(none_val));
PyStackRef_CLOSE(iterable_st); PyStackRef_CLOSE(iterable_st);
@ -5866,7 +5866,7 @@
_PyErr_SetString(tstate, PyExc_NameError, _PyErr_SetString(tstate, PyExc_NameError,
"__build_class__ not found"); "__build_class__ not found");
stack_pointer = _PyFrame_GetStackPointer(frame); stack_pointer = _PyFrame_GetStackPointer(frame);
if (true) goto error; goto error;
} }
bc = PyStackRef_FromPyObjectSteal(bc_o); bc = PyStackRef_FromPyObjectSteal(bc_o);
stack_pointer[0] = bc; stack_pointer[0] = bc;
@ -5920,7 +5920,7 @@
_PyFrame_SetStackPointer(frame, stack_pointer); _PyFrame_SetStackPointer(frame, stack_pointer);
_PyEval_FormatExcUnbound(tstate, _PyFrame_GetCode(frame), oparg); _PyEval_FormatExcUnbound(tstate, _PyFrame_GetCode(frame), oparg);
stack_pointer = _PyFrame_GetStackPointer(frame); stack_pointer = _PyFrame_GetStackPointer(frame);
if (true) goto error; goto error;
} }
value = PyStackRef_FromPyObjectSteal(value_o); value = PyStackRef_FromPyObjectSteal(value_o);
stack_pointer[0] = value; stack_pointer[0] = value;
@ -5969,7 +5969,7 @@
PyTuple_GetItem(_PyFrame_GetCode(frame)->co_localsplusnames, oparg) PyTuple_GetItem(_PyFrame_GetCode(frame)->co_localsplusnames, oparg)
); );
stack_pointer = _PyFrame_GetStackPointer(frame); stack_pointer = _PyFrame_GetStackPointer(frame);
if (1) goto error; goto error;
} }
value = PyStackRef_DUP(value_s); value = PyStackRef_DUP(value_s);
stack_pointer[0] = value; stack_pointer[0] = value;
@ -6088,7 +6088,7 @@
tstate, PyExc_NameError, tstate, PyExc_NameError,
NAME_ERROR_MSG, name); NAME_ERROR_MSG, name);
stack_pointer = _PyFrame_GetStackPointer(frame); stack_pointer = _PyFrame_GetStackPointer(frame);
if (true) goto error; goto error;
} }
} }
} }
@ -6237,7 +6237,7 @@
_PyErr_SetString(tstate, PyExc_SystemError, _PyErr_SetString(tstate, PyExc_SystemError,
"no locals found"); "no locals found");
stack_pointer = _PyFrame_GetStackPointer(frame); stack_pointer = _PyFrame_GetStackPointer(frame);
if (true) goto error; goto error;
} }
locals = PyStackRef_FromPyObjectNew(l); locals = PyStackRef_FromPyObjectNew(l);
stack_pointer[0] = locals; stack_pointer[0] = locals;
@ -6288,7 +6288,7 @@
Py_TYPE(owner_o)->tp_name); Py_TYPE(owner_o)->tp_name);
stack_pointer = _PyFrame_GetStackPointer(frame); stack_pointer = _PyFrame_GetStackPointer(frame);
} }
if (true) goto error; goto error;
} }
attr = PyStackRef_FromPyObjectSteal(attr_o); attr = PyStackRef_FromPyObjectSteal(attr_o);
self_or_null = self_or_null_o == NULL ? self_or_null = self_or_null_o == NULL ?
@ -6348,7 +6348,7 @@
PyStackRef_CLOSE(global_super_st); PyStackRef_CLOSE(global_super_st);
PyStackRef_CLOSE(class_st); PyStackRef_CLOSE(class_st);
PyStackRef_CLOSE(self_st); PyStackRef_CLOSE(self_st);
if (true) goto pop_3_error; goto pop_3_error;
} }
} }
// we make no attempt to optimize here; specializations should // we make no attempt to optimize here; specializations should
@ -6466,7 +6466,7 @@
PyStackRef_CLOSE(class_st); PyStackRef_CLOSE(class_st);
if (attr_o == NULL) { if (attr_o == NULL) {
PyStackRef_CLOSE(self_st); PyStackRef_CLOSE(self_st);
if (true) goto pop_3_error; goto pop_3_error;
} }
if (method_found) { if (method_found) {
self_or_null = self_st; // transfer ownership self_or_null = self_st; // transfer ownership
@ -6838,7 +6838,7 @@
stack_pointer = _PyFrame_GetStackPointer(frame); stack_pointer = _PyFrame_GetStackPointer(frame);
goto exception_unwind; goto exception_unwind;
} }
if (true) goto error; goto error;
} }
TARGET(RERAISE) { TARGET(RERAISE) {
@ -7130,7 +7130,7 @@
} }
else { else {
PyStackRef_CLOSE(v); PyStackRef_CLOSE(v);
if (true) goto pop_1_error; goto pop_1_error;
} }
} }
PyStackRef_CLOSE(v); PyStackRef_CLOSE(v);
@ -7202,7 +7202,7 @@
_PyErr_Format(tstate, PyExc_SystemError, _PyErr_Format(tstate, PyExc_SystemError,
"no locals found when setting up annotations"); "no locals found when setting up annotations");
stack_pointer = _PyFrame_GetStackPointer(frame); stack_pointer = _PyFrame_GetStackPointer(frame);
if (true) goto error; goto error;
} }
/* check if __annotations__ in locals()... */ /* check if __annotations__ in locals()... */
_PyFrame_SetStackPointer(frame, stack_pointer); _PyFrame_SetStackPointer(frame, stack_pointer);
@ -7559,7 +7559,7 @@
"no locals found when storing %R", name); "no locals found when storing %R", name);
stack_pointer = _PyFrame_GetStackPointer(frame); stack_pointer = _PyFrame_GetStackPointer(frame);
PyStackRef_CLOSE(v); PyStackRef_CLOSE(v);
if (true) goto pop_1_error; goto pop_1_error;
} }
if (PyDict_CheckExact(ns)) { if (PyDict_CheckExact(ns)) {
_PyFrame_SetStackPointer(frame, stack_pointer); _PyFrame_SetStackPointer(frame, stack_pointer);

View File

@ -744,7 +744,7 @@ def always_exits(op: parser.InstDef) -> bool:
if tkn.text == "DEOPT_IF" or tkn.text == "ERROR_IF": if tkn.text == "DEOPT_IF" or tkn.text == "ERROR_IF":
next(tkn_iter) # '(' next(tkn_iter) # '('
t = next(tkn_iter) t = next(tkn_iter)
if t.text == "true": if t.text in ("true", "1"):
return True return True
return False return False

View File

@ -165,16 +165,24 @@ class Emitter:
storage: Storage, storage: Storage,
inst: Instruction | None, inst: Instruction | None,
) -> bool: ) -> bool:
self.out.emit_at("if ", tkn)
lparen = next(tkn_iter) lparen = next(tkn_iter)
self.emit(lparen)
assert lparen.kind == "LPAREN" assert lparen.kind == "LPAREN"
first_tkn = tkn_iter.peek() first_tkn = tkn_iter.peek()
emit_to(self.out, tkn_iter, "COMMA") unconditional = always_true(first_tkn)
if unconditional:
next(tkn_iter)
comma = next(tkn_iter)
if comma.kind != "COMMA":
raise analysis_error(f"Expected comma, got '{comma.text}'", comma)
self.out.start_line()
else:
self.out.emit_at("if ", tkn)
self.emit(lparen)
emit_to(self.out, tkn_iter, "COMMA")
self.out.emit(") ")
label = next(tkn_iter).text label = next(tkn_iter).text
next(tkn_iter) # RPAREN next(tkn_iter) # RPAREN
next(tkn_iter) # Semi colon next(tkn_iter) # Semi colon
self.out.emit(") ")
storage.clear_inputs("at ERROR_IF") storage.clear_inputs("at ERROR_IF")
c_offset = storage.stack.peek_offset() c_offset = storage.stack.peek_offset()
try: try:
@ -196,7 +204,7 @@ class Emitter:
self.out.emit(label) self.out.emit(label)
self.out.emit(";\n") self.out.emit(";\n")
self.out.emit("}\n") self.out.emit("}\n")
return not always_true(first_tkn) return not unconditional
def error_no_pop( def error_no_pop(
self, self,