8000 gh-120017: use 'do-while(0)' in some `{codegen,compile}.c` multi-line macros by picnixz · Pull Request #120018 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-120017: use 'do-while(0)' in some {codegen,compile}.c multi-line macros #120018

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Nov 7, 2024
150 changes: 84 additions & 66 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@
#define ERROR -1

#define RETURN_IF_ERROR(X) \
if ((X) == -1) { \
return ERROR; \
}
do { \
if ((X) == -1) { \
return ERROR; \
} \
} while (0)

#define IS_TOP_LEVEL_AWAIT(C) ( \
((C)->c_flags.cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \
Expand Down Expand Up @@ -928,42 +930,47 @@ codegen_addop_j(instr_sequence *seq, location loc,
return _PyInstructionSequence_Addop(seq, opcode, target.id, loc);
}

#define RETURN_IF_ERROR_IN_SCOPE(C, CALL) { \
if ((CALL) < 0) { \
compiler_exit_scope((C)); \
return ERROR; \
} \
}
#define RETURN_IF_ERROR_IN_SCOPE(C, CALL) \
do { \
if ((CALL) < 0) { \
compiler_exit_scope((C)); \
return ERROR; \
} \
} while (0)

#define ADDOP(C, LOC, OP) \
RETURN_IF_ERROR(codegen_addop_noarg(INSTR_SEQUENCE(C), (OP), (LOC)))

#define ADDOP_IN_SCOPE(C, LOC, OP) RETURN_IF_ERROR_IN_SCOPE((C), codegen_addop_noarg(INSTR_SEQUENCE(C), (OP), (LOC)))
#define ADDOP_IN_SCOPE(C, LOC, OP) \
RETURN_IF_ERROR_IN_SCOPE((C), codegen_addop_noarg(INSTR_SEQUENCE(C), (OP), (LOC)))

#define ADDOP_LOAD_CONST(C, LOC, O) \
RETURN_IF_ERROR(compiler_addop_load_const((C), (LOC), (O)))

/* Same as ADDOP_LOAD_CONST, but steals a reference. */
#define ADDOP_LOAD_CONST_NEW(C, LOC, O) { \
PyObject *__new_const = (O); \
if (__new_const == NULL) { \
return ERROR; \
} \
if (compiler_addop_load_const((C), (LOC), __new_const) < 0) { \
Py_DECREF(__new_const); \
return ERROR; \
} \
Py_DECREF(__new_const); \
}

#define ADDOP_N(C, LOC, OP, O, TYPE) { \
assert(!OPCODE_HAS_CONST(OP)); /* use ADDOP_LOAD_CONST_NEW */ \
if (compiler_addop_o((C), (LOC), (OP), (C)->u->u_metadata.u_ ## TYPE, (O)) < 0) { \
Py_DECREF((O)); \
return ERROR; \
} \
Py_DECREF((O)); \
}
#define ADDOP_LOAD_CONST_NEW(C, LOC, O) \
do { \
PyObject *__new_const = (O); \
if (__new_const == NULL) { \
return ERROR; \
} \
if (compiler_addop_load_const((C), (LOC), __new_const) < 0) { \
Py_DECREF(__new_const); \
return ERROR; \
} \
Py_DECREF(__new_const); \
} while (0)

#define ADDOP_N(C, LOC, OP, O, TYPE) \
do { \
assert(!OPCODE_HAS_CONST(OP)); /* use ADDOP_LOAD_CONST_NEW */ \
if (compiler_addop_o((C), (LOC), (OP), \
(C)->u->u_metadata.u_ ## TYPE, (O)) < 0) { \
Py_DECREF((O)); \
return ERROR; \
} \
Py_DECREF((O)); \
} while (0)

#define ADDOP_NAME(C, LOC, OP, O, TYPE) \
RETURN_IF_ERROR(compiler_addop_name((C), (LOC), (OP), (C)->u->u_metadata.u_ ## TYPE, (O)))
Expand Down Expand Up @@ -997,31 +1004,33 @@ codegen_addop_j(instr_sequence *seq, location loc,
*/

#define VISIT(C, TYPE, V) \
RETURN_IF_ERROR(compiler_visit_ ## TYPE((C), (V)));
RETURN_IF_ERROR(compiler_visit_ ## TYPE((C), (V)))

#define VISIT_IN_SCOPE(C, TYPE, V) \
RETURN_IF_ERROR_IN_SCOPE((C), compiler_visit_ ## TYPE((C), (V)))

#define VISIT_SEQ(C, TYPE, SEQ) { \
int _i; \
asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
RETURN_IF_ERROR(compiler_visit_ ## TYPE((C), elt)); \
} \
}

#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
int _i; \
asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
if (compiler_visit_ ## TYPE((C), elt) < 0) { \
compiler_exit_scope(C); \
return ERROR; \
} \
} \
}
#define VISIT_SEQ(C, TYPE, SEQ) \
do { \
int _i; \
asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
RETURN_IF_ERROR(compiler_visit_ ## TYPE((C), elt)); \
} \
} while (0)

#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) \
do { \
int _i; \
asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
if (compiler_visit_ ## TYPE((C), elt) < 0) { \
compiler_exit_scope(C); \
return ERROR; \
} \
} \
} while (0)


static int
Expand Down Expand Up @@ -2560,7 +2569,7 @@ compiler_class(struct compiler *c, stmt_ty s)
RETURN_IF_ERROR_IN_SCOPE(c, compiler_nameop(c, loc, &_Py_STR(type_params), Load));
RETURN_IF_ERROR_IN_SCOPE(
c, codegen_addop_i(INSTR_SEQUENCE(c), CALL_INTRINSIC_1, INTRINSIC_SUBSCRIPT_GENERIC, loc)
)
);
RETURN_IF_ERROR_IN_SCOPE(c, compiler_nameop(c, loc, &_Py_STR(generic_base), Store));

Py_ssize_t original_len = asdl_seq_LEN(s->v.ClassDef.bases);
Expand Down Expand Up @@ -3908,7 +3917,7 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s)
case Return_kind:
return compiler_return(c, s);
case Delete_kind:
VISIT_SEQ(c, expr, s->v.Delete.targets)
VISIT_SEQ(c, expr, s->v.Delete.targets);
break;
case Assign_kind:
{
Expand Down Expand Up @@ -5930,7 +5939,7 @@ compiler_async_with(struct compiler *c, stmt_ty s, int pos)
pos++;
if (pos == asdl_seq_LEN(s->v.AsyncWith.items)) {
/* BLOCK code */
VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
VISIT_SEQ(c, stmt, s->v.AsyncWith.body);
}
else {
RETURN_IF_ERROR(compiler_async_with(c, s, pos));
Expand Down Expand Up @@ -6029,7 +6038,7 @@ compiler_with(struct compiler *c, stmt_ty s, int pos)
pos++;
if (pos == asdl_seq_LEN(s->v.With.items)) {
/* BLOCK code */
VISIT_SEQ(c, stmt, s->v.With.body)
VISIT_SEQ(c, stmt, s->v.With.body);
}
else {
RETURN_IF_ERROR(compiler_with(c, s, pos));
Expand Down Expand Up @@ -7728,10 +7737,14 @@ _PyCompile_CodeGen(PyObject *ast, PyObject *filename, PyCompilerFlags *pflags,

_PyCompile_CodeUnitMetadata *umd = &c->u->u_metadata;

#define SET_MATADATA_ITEM(key, value) \
if (value != NULL) { \
if (PyDict_SetItemString(metadata, key, value) < 0) goto finally; \
}
#define SET_MATADATA_ITEM(key, value) \
do { \
if (value != NULL) { \
if (PyDict_SetItemString(metadata, key, value) < 0) { \
goto finally; \
} \
} \
} while (0)

SET_MATADATA_ITEM("name", umd->u_name);
SET_MATADATA_ITEM("qualname", umd->u_qualname);
Expand All @@ -7742,13 +7755,18 @@ _PyCompile_CodeGen(PyObject *ast, PyObject *filename, PyCompilerFlags *pflags,
SET_MATADATA_ITEM("freevars", umd->u_freevars);
#undef SET_MATADATA_ITEM

#define SET_MATADATA_INT(key, value) do { \
PyObject *v = PyLong_FromLong((long)value); \
if (v == NULL) goto finally; \
int res = PyDict_SetItemString(metadata, key, v); \
Py_XDECREF(v); \
if (res < 0) goto finally; \
} while (0);
#define SET_MATADATA_INT(key, value) \
do { \
PyObject *v = PyLong_FromLong((long)value); \
if (v == NULL) { \
goto finally; \
} \
int res = PyDict_SetItemString(metadata, key, v); \
Py_XDECREF(v); \
if (res < 0) { \
goto finally; \
} \
} while (0)

SET_MATADATA_INT("argcount", umd->u_argcount);
SET_MATADATA_INT("posonlyargcount", umd->u_posonlyargcount);
Expand Down
Loading
0