Skip to content

Commit 57e3c59

Browse files
authored
GH-125521: Remove if (true) from generated output to reduce C compiler warnings (GH-125700)
1 parent c1bdbe8 commit 57e3c59

File tree

4 files changed

+75
-40
lines changed

4 files changed

+75
-40
lines changed

Lib/test/test_generated_cases.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,33 @@ def test_push_then_error(self):
12701270
"""
12711271
self.run_cases_test(input, output)
12721272

1273+
def test_error_if_true(self):
1274+
1275+
input = """
1276+
inst(OP1, ( --)) {
1277+
ERROR_IF(true, here);
1278+
}
1279+
inst(OP2, ( --)) {
1280+
ERROR_IF(1, there);
1281+
}
1282+
"""
1283+
output = """
1284+
TARGET(OP1) {
1285+
frame->instr_ptr = next_instr;
1286+
next_instr += 1;
1287+
INSTRUCTION_STATS(OP1);
1288+
goto here;
1289+
}
1290+
1291+
TARGET(OP2) {
1292+
frame->instr_ptr = next_instr;
1293+
next_instr += 1;
1294+
INSTRUCTION_STATS(OP2);
1295+
goto there;
1296+
}
1297+
"""
1298+
self.run_cases_test(input, output)
1299+
12731300
def test_scalar_array_inconsistency(self):
12741301

12751302
input = """

Python/generated_cases.c.h

Lines changed: 34 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tools/cases_generator/analyzer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ def always_exits(op: parser.InstDef) -> bool:
744744
if tkn.text == "DEOPT_IF" or tkn.text == "ERROR_IF":
745745
next(tkn_iter) # '('
746746
t = next(tkn_iter)
747-
if t.text == "true":
747+
if t.text in ("true", "1"):
748748
return True
749749
return False
750750

Tools/cases_generator/generators_common.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,16 +165,24 @@ def error_if(
165165
storage: Storage,
166166
inst: Instruction | None,
167167
) -> bool:
168-
self.out.emit_at("if ", tkn)
169168
lparen = next(tkn_iter)
170-
self.emit(lparen)
171169
assert lparen.kind == "LPAREN"
172170
first_tkn = tkn_iter.peek()
173-
emit_to(self.out, tkn_iter, "COMMA")
171+
unconditional = always_true(first_tkn)
172+
if unconditional:
173+
next(tkn_iter)
174+
comma = next(tkn_iter)
175+
if comma.kind != "COMMA":
176+
raise analysis_error(f"Expected comma, got '{comma.text}'", comma)
177+
self.out.start_line()
178+
else:
179+
self.out.emit_at("if ", tkn)
180+
self.emit(lparen)
181+
emit_to(self.out, tkn_iter, "COMMA")
182+
self.out.emit(") ")
174183
label = next(tkn_iter).text
175184
next(tkn_iter) # RPAREN
176185
next(tkn_iter) # Semi colon
177-
self.out.emit(") ")
178186
storage.clear_inputs("at ERROR_IF")
179187
c_offset = storage.stack.peek_offset()
180188
try:
@@ -196,7 +204,7 @@ def error_if(
196204
self.out.emit(label)
197205
self.out.emit(";\n")
198206
self.out.emit("}\n")
199-
return not always_true(first_tkn)
207+
return not unconditional
200208

201209
def error_no_pop(
202210
self,

0 commit comments

Comments
 (0)