From 6143080a9b72d3e5c17ed0425734b72250834b72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Mon, 3 Jun 2024 15:33:10 +0200 Subject: [PATCH 1/5] use do { ... } while(0) in some macros --- Python/symtable.c | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/Python/symtable.c b/Python/symtable.c index d8240cdd11f7ea..a81658c86ceb22 100644 --- a/Python/symtable.c +++ b/Python/symtable.c @@ -70,6 +70,9 @@ #define DUPLICATE_TYPE_PARAM \ "duplicate type parameter '%U'" +#define DUPLICATE_ARGUMENT \ +"duplicate argument '%U' in function definition" + #define LOCATION(x) \ (x)->lineno, (x)->col_offset, (x)->end_lineno, (x)->end_col_offset @@ -359,9 +362,6 @@ static void dump_symtable(PySTEntryObject* ste) } #endif -#define DUPLICATE_ARGUMENT \ -"duplicate argument '%U' in function definition" - static struct symtable * symtable_new(void) { @@ -601,7 +601,7 @@ error_at_directive(PySTEntryObject *ste, PyObject *name) global: set of all symbol names explicitly declared as global */ -#define SET_SCOPE(DICT, NAME, I) { \ +#define SET_SCOPE(DICT, NAME, I) do { \ PyObject *o = PyLong_FromLong(I); \ if (!o) \ return 0; \ @@ -610,7 +610,7 @@ error_at_directive(PySTEntryObject *ste, PyObject *name) return 0; \ } \ Py_DECREF(o); \ -} +} while(0) /* Decide on scope of name, given flags. @@ -1561,11 +1561,13 @@ symtable_enter_type_param_block(struct symtable *st, identifier name, #define VISIT_QUIT(ST, X) \ return --(ST)->recursion_depth,(X) -#define VISIT(ST, TYPE, V) \ - if (!symtable_visit_ ## TYPE((ST), (V))) \ - VISIT_QUIT((ST), 0); +#define VISIT(ST, TYPE, V) do { \ + if (!symtable_visit_ ## TYPE((ST), (V))) { \ + VISIT_QUIT((ST), 0); \ + } \ +} while(0) -#define VISIT_SEQ(ST, TYPE, SEQ) { \ +#define VISIT_SEQ(ST, TYPE, SEQ) do { \ int i; \ asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ for (i = 0; i < asdl_seq_LEN(seq); i++) { \ @@ -1573,9 +1575,9 @@ symtable_enter_type_param_block(struct symtable *st, identifier name, if (!symtable_visit_ ## TYPE((ST), elt)) \ VISIT_QUIT((ST), 0); \ } \ -} +} while(0) -#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \ +#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) do { \ int i; \ asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ for (i = (START); i < asdl_seq_LEN(seq); i++) { \ @@ -1583,9 +1585,9 @@ symtable_enter_type_param_block(struct symtable *st, identifier name, if (!symtable_visit_ ## TYPE((ST), elt)) \ VISIT_QUIT((ST), 0); \ } \ -} +} while(0) -#define VISIT_SEQ_WITH_NULL(ST, TYPE, SEQ) { \ +#define VISIT_SEQ_WITH_NULL(ST, TYPE, SEQ) do { \ int i = 0; \ asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ for (i = 0; i < asdl_seq_LEN(seq); i++) { \ @@ -1594,7 +1596,7 @@ symtable_enter_type_param_block(struct symtable *st, identifier name, if (!symtable_visit_ ## TYPE((ST), elt)) \ VISIT_QUIT((ST), 0); \ } \ -} +} while(0) static int symtable_record_directive(struct symtable *st, identifier name, int lineno, @@ -2261,11 +2263,11 @@ symtable_visit_expr(struct symtable *st, expr_ty e) break; case Slice_kind: if (e->v.Slice.lower) - VISIT(st, expr, e->v.Slice.lower) + VISIT(st, expr, e->v.Slice.lower); if (e->v.Slice.upper) - VISIT(st, expr, e->v.Slice.upper) + VISIT(st, expr, e->v.Slice.upper); if (e->v.Slice.step) - VISIT(st, expr, e->v.Slice.step) + VISIT(st, expr, e->v.Slice.step); break; case Name_kind: if (!symtable_add_def(st, e->v.Name.id, From 5e200d4ea9645eea64697a177fed146b1958d373 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Mon, 3 Jun 2024 16:53:49 +0200 Subject: [PATCH 2/5] fixup --- Python/symtable.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Python/symtable.c b/Python/symtable.c index a81658c86ceb22..0a6625f1a059ea 100644 --- a/Python/symtable.c +++ b/Python/symtable.c @@ -70,10 +70,6 @@ #define DUPLICATE_TYPE_PARAM \ "duplicate type parameter '%U'" -#define DUPLICATE_ARGUMENT \ -"duplicate argument '%U' in function definition" - - #define LOCATION(x) \ (x)->lineno, (x)->col_offset, (x)->end_lineno, (x)->end_col_offset @@ -362,6 +358,9 @@ static void dump_symtable(PySTEntryObject* ste) } #endif +#define DUPLICATE_ARGUMENT \ +"duplicate argument '%U' in function definition" + static struct symtable * symtable_new(void) { From e003ac3cba87ff3a86ad3a4faecf1adbb60f7d6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Mon, 3 Jun 2024 16:54:08 +0200 Subject: [PATCH 3/5] fixup --- Python/symtable.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Python/symtable.c b/Python/symtable.c index 0a6625f1a059ea..46db070100fae7 100644 --- a/Python/symtable.c +++ b/Python/symtable.c @@ -70,6 +70,7 @@ #define DUPLICATE_TYPE_PARAM \ "duplicate type parameter '%U'" + #define LOCATION(x) \ (x)->lineno, (x)->col_offset, (x)->end_lineno, (x)->end_col_offset From 86db9ba775c77846faac14b609709158ff8491ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Mon, 3 Jun 2024 17:07:05 +0200 Subject: [PATCH 4/5] address review --- Python/symtable.c | 92 ++++++++++++++++++++++++----------------------- 1 file changed, 48 insertions(+), 44 deletions(-) diff --git a/Python/symtable.c b/Python/symtable.c index 46db070100fae7..43c26a866de625 100644 --- a/Python/symtable.c +++ b/Python/symtable.c @@ -602,15 +602,15 @@ error_at_directive(PySTEntryObject *ste, PyObject *name) */ #define SET_SCOPE(DICT, NAME, I) do { \ - PyObject *o = PyLong_FromLong(I); \ - if (!o) \ - return 0; \ - if (PyDict_SetItem((DICT), (NAME), o) < 0) { \ + PyObject *o = PyLong_FromLong(I); \ + if (!o) \ + return 0; \ + if (PyDict_SetItem((DICT), (NAME), o) < 0) { \ + Py_DECREF(o); \ + return 0; \ + } \ Py_DECREF(o); \ - return 0; \ - } \ - Py_DECREF(o); \ -} while(0) + } while(0) /* Decide on scope of name, given flags. @@ -1561,42 +1561,46 @@ symtable_enter_type_param_block(struct symtable *st, identifier name, #define VISIT_QUIT(ST, X) \ return --(ST)->recursion_depth,(X) -#define VISIT(ST, TYPE, V) do { \ - if (!symtable_visit_ ## TYPE((ST), (V))) { \ - VISIT_QUIT((ST), 0); \ - } \ -} while(0) - -#define VISIT_SEQ(ST, 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 (!symtable_visit_ ## TYPE((ST), elt)) \ - VISIT_QUIT((ST), 0); \ - } \ -} while(0) - -#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) do { \ - int i; \ - asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ - for (i = (START); i < asdl_seq_LEN(seq); i++) { \ - TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \ - if (!symtable_visit_ ## TYPE((ST), elt)) \ - VISIT_QUIT((ST), 0); \ - } \ -} while(0) - -#define VISIT_SEQ_WITH_NULL(ST, TYPE, SEQ) do { \ - int i = 0; \ - 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 (!elt) continue; /* can be NULL */ \ - if (!symtable_visit_ ## TYPE((ST), elt)) \ - VISIT_QUIT((ST), 0); \ - } \ -} while(0) +#define VISIT(ST, TYPE, V) \ + do { \ + if (!symtable_visit_ ## TYPE((ST), (V))) { \ + VISIT_QUIT((ST), 0); \ + } \ + } while(0) + +#define VISIT_SEQ(ST, 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 (!symtable_visit_ ## TYPE((ST), elt)) \ + VISIT_QUIT((ST), 0); \ + } \ + } while(0) + +#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) \ + do { \ + int i; \ + asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ + for (i = (START); i < asdl_seq_LEN(seq); i++) { \ + TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \ + if (!symtable_visit_ ## TYPE((ST), elt)) \ + VISIT_QUIT((ST), 0); \ + } \ + } while(0) + +#define VISIT_SEQ_WITH_NULL(ST, TYPE, SEQ) \ + do { \ + int i = 0; \ + 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 (!elt) continue; /* can be NULL */ \ + if (!symtable_visit_ ## TYPE((ST), elt)) \ + VISIT_QUIT((ST), 0); \ + } \ + } while(0) static int symtable_record_directive(struct symtable *st, identifier name, int lineno, From 11296619da871cf7069dc84bc8a3ddc292686423 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Mon, 3 Jun 2024 17:12:16 +0200 Subject: [PATCH 5/5] fixup --- Python/symtable.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Python/symtable.c b/Python/symtable.c index 43c26a866de625..0ee8ca36cf8df0 100644 --- a/Python/symtable.c +++ b/Python/symtable.c @@ -601,7 +601,8 @@ error_at_directive(PySTEntryObject *ste, PyObject *name) global: set of all symbol names explicitly declared as global */ -#define SET_SCOPE(DICT, NAME, I) do { \ +#define SET_SCOPE(DICT, NAME, I) \ + do { \ PyObject *o = PyLong_FromLong(I); \ if (!o) \ return 0; \