Skip to content

Commit c222441

Browse files
authored
gh-120017: use 'do-while(0)' in some {codegen,compile}.c multi-line macros (#120018)
1 parent bbe9b21 commit c222441

File tree

2 files changed

+72
-62
lines changed

2 files changed

+72
-62
lines changed

Diff for: Python/codegen.c

+67-59
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,19 @@
5151
#define ERROR -1
5252

5353
#define RETURN_IF_ERROR(X) \
54-
if ((X) == -1) { \
55-
return ERROR; \
56-
}
57-
58-
#define RETURN_IF_ERROR_IN_SCOPE(C, CALL) { \
59-
if ((CALL) < 0) { \
60-
_PyCompile_ExitScope((C)); \
61-
return ERROR; \
62-
} \
63-
}
54+
do { \
55+
if ((X) == -1) { \
56+
return ERROR; \
57+
} \
58+
} while (0)
59+
60+
#define RETURN_IF_ERROR_IN_SCOPE(C, CALL) \
61+
do { \
62+
if ((CALL) < 0) { \
63+
_PyCompile_ExitScope((C)); \
64+
return ERROR; \
65+
} \
66+
} while (0)
6467

6568
struct _PyCompiler;
6669
typedef struct _PyCompiler compiler;
@@ -261,7 +264,7 @@ codegen_addop_i(instr_sequence *seq, int opcode, Py_ssize_t oparg, location loc)
261264
RETURN_IF_ERROR(codegen_addop_i(INSTR_SEQUENCE(C), (OP), (O), (LOC)))
262265

263266
#define ADDOP_I_IN_SCOPE(C, LOC, OP, O) \
264-
RETURN_IF_ERROR_IN_SCOPE(C, codegen_addop_i(INSTR_SEQUENCE(C), (OP), (O), (LOC)));
267+
RETURN_IF_ERROR_IN_SCOPE(C, codegen_addop_i(INSTR_SEQUENCE(C), (OP), (O), (LOC)))
265268

266269
static int
267270
codegen_addop_noarg(instr_sequence *seq, int opcode, location loc)
@@ -303,17 +306,18 @@ codegen_addop_load_const(compiler *c, location loc, PyObject *o)
303306
RETURN_IF_ERROR_IN_SCOPE((C), codegen_addop_load_const((C), (LOC), (O)))
304307

305308
/* Same as ADDOP_LOAD_CONST, but steals a reference. */
306-
#define ADDOP_LOAD_CONST_NEW(C, LOC, O) { \
307-
PyObject *__new_const = (O); \
308-
if (__new_const == NULL) { \
309-
return ERROR; \
310-
} \
311-
if (codegen_addop_load_const((C), (LOC), __new_const) < 0) { \
312-
Py_DECREF(__new_const); \
313-
return ERROR; \
314-
} \
315-
Py_DECREF(__new_const); \
316-
}
309+
#define ADDOP_LOAD_CONST_NEW(C, LOC, O) \
310+
do { \
311+
PyObject *__new_const = (O); \
312+
if (__new_const == NULL) { \
313+
return ERROR; \
314+
} \
315+
if (codegen_addop_load_const((C), (LOC), __new_const) < 0) { \
316+
Py_DECREF(__new_const); \
317+
return ERROR; \
318+
} \
319+
Py_DECREF(__new_const); \
320+
} while (0)
317321

318322
static int
319323
codegen_addop_o(compiler *c, location loc,
@@ -325,19 +329,23 @@ codegen_addop_o(compiler *c, location loc,
325329
return SUCCESS;
326330
}
327331

328-
#define ADDOP_N(C, LOC, OP, O, TYPE) { \
329-
assert(!OPCODE_HAS_CONST(OP)); /* use ADDOP_LOAD_CONST_NEW */ \
330-
int ret = codegen_addop_o((C), (LOC), (OP), METADATA(C)->u_ ## TYPE, (O)); \
331-
Py_DECREF((O)); \
332-
RETURN_IF_ERROR(ret); \
333-
}
334-
335-
#define ADDOP_N_IN_SCOPE(C, LOC, OP, O, TYPE) { \
336-
assert(!OPCODE_HAS_CONST(OP)); /* use ADDOP_LOAD_CONST_NEW */ \
337-
int ret = codegen_addop_o((C), (LOC), (OP), METADATA(C)->u_ ## TYPE, (O)); \
338-
Py_DECREF((O)); \
339-
RETURN_IF_ERROR_IN_SCOPE((C), ret); \
340-
}
332+
#define ADDOP_N(C, LOC, OP, O, TYPE) \
333+
do { \
334+
assert(!OPCODE_HAS_CONST(OP)); /* use ADDOP_LOAD_CONST_NEW */ \
335+
int ret = codegen_addop_o((C), (LOC), (OP), \
336+
METADATA(C)->u_ ## TYPE, (O)); \
337+
Py_DECREF((O)); \
338+
RETURN_IF_ERROR(ret); \
339+
} while (0)
340+
341+
#define ADDOP_N_IN_SCOPE(C, LOC, OP, O, TYPE) \
342+
do { \
343+
assert(!OPCODE_HAS_CONST(OP)); /* use ADDOP_LOAD_CONST_NEW */ \
344+
int ret = codegen_addop_o((C), (LOC), (OP), \
345+
METADATA(C)->u_ ## TYPE, (O)); \
346+
Py_DECREF((O)); \
347+
RETURN_IF_ERROR_IN_SCOPE((C), ret); \
348+
} while (0)
341349

342350
#define LOAD_METHOD -1
343351
#define LOAD_SUPER_METHOD -2
@@ -426,31 +434,31 @@ codegen_addop_j(instr_sequence *seq, location loc,
426434
*/
427435

428436
#define VISIT(C, TYPE, V) \
429-
RETURN_IF_ERROR(codegen_visit_ ## TYPE((C), (V)));
437+
RETURN_IF_ERROR(codegen_visit_ ## TYPE((C), (V)))
430438

431439
#define VISIT_IN_SCOPE(C, TYPE, V) \
432440
RETURN_IF_ERROR_IN_SCOPE((C), codegen_visit_ ## TYPE((C), (V)))
433441

434-
#define VISIT_SEQ(C, TYPE, SEQ) { \
435-
int _i; \
436-
asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
437-
for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
438-
TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
439-
RETURN_IF_ERROR(codegen_visit_ ## TYPE((C), elt)); \
440-
} \
441-
}
442-
443-
#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
444-
int _i; \
445-
asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
446-
for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
447-
TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
448-
if (codegen_visit_ ## TYPE((C), elt) < 0) { \
449-
_PyCompile_ExitScope(C); \
450-
return ERROR; \
451-
} \
452-
} \
453-
}
442+
#define VISIT_SEQ(C, TYPE, SEQ) \
443+
do { \
444+
asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
445+
for (int _i = 0; _i < asdl_seq_LEN(seq); _i++) { \
446+
TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
447+
RETURN_IF_ERROR(codegen_visit_ ## TYPE((C), elt)); \
448+
} \
449+
} while (0)
450+
451+
#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) \
452+
do { \
453+
asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
454+
for (int _i = 0; _i < asdl_seq_LEN(seq); _i++) { \
455+
TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
456+
if (codegen_visit_ ## TYPE((C), elt) < 0) { \
457+
_PyCompile_ExitScope(C); \
458+
return ERROR; \
459+
} \
460+
} \
461+
} while (0)
454462

455463
static int
456464
codegen_call_exit_with_nones(compiler *c, location loc)
@@ -2866,7 +2874,7 @@ codegen_visit_stmt(compiler *c, stmt_ty s)
28662874
case Return_kind:
28672875
return codegen_return(c, s);
28682876
case Delete_kind:
2869-
VISIT_SEQ(c, expr, s->v.Delete.targets)
2877+
VISIT_SEQ(c, expr, s->v.Delete.targets);
28702878
break;
28712879
case Assign_kind:
28722880
{
@@ -4759,7 +4767,7 @@ codegen_async_with(compiler *c, stmt_ty s, int pos)
47594767
pos++;
47604768
if (pos == asdl_seq_LEN(s->v.AsyncWith.items)) {
47614769
/* BLOCK code */
4762-
VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
4770+
VISIT_SEQ(c, stmt, s->v.AsyncWith.body);
47634771
}
47644772
else {
47654773
RETURN_IF_ERROR(codegen_async_with(c, s, pos));
@@ -4858,7 +4866,7 @@ codegen_with(compiler *c, stmt_ty s, int pos)
48584866
pos++;
48594867
if (pos == asdl_seq_LEN(s->v.With.items)) {
48604868
/* BLOCK code */
4861-
VISIT_SEQ(c, stmt, s->v.With.body)
4869+
VISIT_SEQ(c, stmt, s->v.With.body);
48624870
}
48634871
else {
48644872
RETURN_IF_ERROR(codegen_with(c, s, pos));

Diff for: Python/compile.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@
3131
#define ERROR -1
3232

3333
#define RETURN_IF_ERROR(X) \
34-
if ((X) == -1) { \
35-
return ERROR; \
36-
}
34+
do { \
35+
if ((X) == -1) { \
36+
return ERROR; \
37+
} \
38+
} while (0)
3739

3840
typedef _Py_SourceLocation location;
3941
typedef _PyJumpTargetLabel jump_target_label;

0 commit comments

Comments
 (0)