diff options
author | Victor Stinner <vstinner@python.org> | 2024-06-27 09:58:44 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-27 09:58:44 (GMT) |
commit | 12af8ec864225248c3d2916cb142a5e7ee36cbe2 (patch) | |
tree | d29a5c87668a1cd1e67ceee0b9074ce7f2c74392 /Python | |
parent | 4999e0bda091826fcdf303dd439364e1d303a5ce (diff) | |
download | cpython-12af8ec864225248c3d2916cb142a5e7ee36cbe2.zip cpython-12af8ec864225248c3d2916cb142a5e7ee36cbe2.tar.gz cpython-12af8ec864225248c3d2916cb142a5e7ee36cbe2.tar.bz2 |
gh-121040: Use __attribute__((fallthrough)) (#121044)
Fix warnings when using -Wimplicit-fallthrough compiler flag.
Annotate explicitly "fall through" switch cases with a new
_Py_FALLTHROUGH macro which uses __attribute__((fallthrough)) if
available. Replace "fall through" comments with _Py_FALLTHROUGH.
Add _Py__has_attribute() macro. No longer define __has_attribute()
macro if it's not defined. Move also _Py__has_builtin() at the top
of pyport.h.
Co-Authored-By: Nikita Sobolev <mail@sobolevn.me>
Diffstat (limited to 'Python')
-rw-r--r-- | Python/assemble.c | 6 | ||||
-rw-r--r-- | Python/bytecodes.c | 4 | ||||
-rw-r--r-- | Python/compile.c | 4 | ||||
-rw-r--r-- | Python/crossinterp.c | 2 | ||||
-rw-r--r-- | Python/dtoa.c | 8 | ||||
-rw-r--r-- | Python/formatter_unicode.c | 2 | ||||
-rw-r--r-- | Python/generated_cases.c.h | 4 | ||||
-rw-r--r-- | Python/getargs.c | 2 | ||||
-rw-r--r-- | Python/importdl.c | 30 | ||||
-rw-r--r-- | Python/marshal.c | 6 | ||||
-rw-r--r-- | Python/modsupport.c | 1 | ||||
-rw-r--r-- | Python/pyhash.c | 36 | ||||
-rw-r--r-- | Python/pystrtod.c | 6 |
13 files changed, 56 insertions, 55 deletions
diff --git a/Python/assemble.c b/Python/assemble.c index 945c8ac..f7b88b5 100644 --- a/Python/assemble.c +++ b/Python/assemble.c @@ -369,17 +369,17 @@ write_instr(_Py_CODEUNIT *codestr, instruction *instr, int ilen) codestr->op.code = EXTENDED_ARG; codestr->op.arg = (oparg >> 24) & 0xFF; codestr++; - /* fall through */ + _Py_FALLTHROUGH; case 3: codestr->op.code = EXTENDED_ARG; codestr->op.arg = (oparg >> 16) & 0xFF; codestr++; - /* fall through */ + _Py_FALLTHROUGH; case 2: codestr->op.code = EXTENDED_ARG; codestr->op.arg = (oparg >> 8) & 0xFF; codestr++; - /* fall through */ + _Py_FALLTHROUGH; case 1: codestr->op.code = opcode; codestr->op.arg = oparg & 0xFF; diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 67061ac..8dfce77 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -885,10 +885,10 @@ dummy_func( switch (oparg) { case 2: cause = PyStackRef_AsPyObjectSteal(args[1]); - /* fall through */ + _Py_FALLTHROUGH; case 1: exc = PyStackRef_AsPyObjectSteal(args[0]); - /* fall through */ + _Py_FALLTHROUGH; case 0: if (do_raise(tstate, exc, cause)) { assert(oparg == 0); diff --git a/Python/compile.c b/Python/compile.c index cdac438..69de0ec 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -4681,7 +4681,7 @@ check_subscripter(struct compiler *c, expr_ty e) { return SUCCESS; } - /* fall through */ + _Py_FALLTHROUGH; case Set_kind: case SetComp_kind: case GeneratorExp_kind: @@ -4714,7 +4714,7 @@ check_index(struct compiler *c, expr_ty e, expr_ty s) if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) { return SUCCESS; } - /* fall through */ + _Py_FALLTHROUGH; case Tuple_kind: case List_kind: case ListComp_kind: diff --git a/Python/crossinterp.c b/Python/crossinterp.c index a03456a..acb372a 100644 --- a/Python/crossinterp.c +++ b/Python/crossinterp.c @@ -969,7 +969,7 @@ _PyXI_ApplyErrorCode(_PyXI_errcode code, PyInterpreterState *interp) { assert(!PyErr_Occurred()); switch (code) { - case _PyXI_ERR_NO_ERROR: // fall through + case _PyXI_ERR_NO_ERROR: _Py_FALLTHROUGH; case _PyXI_ERR_UNCAUGHT_EXCEPTION: // There is nothing to apply. #ifdef Py_DEBUG diff --git a/Python/dtoa.c b/Python/dtoa.c index 8bba06d..d0c89b2 100644 --- a/Python/dtoa.c +++ b/Python/dtoa.c @@ -1405,7 +1405,7 @@ _Py_dg_strtod(const char *s00, char **se) switch (c) { case '-': sign = 1; - /* fall through */ + _Py_FALLTHROUGH; case '+': c = *++s; } @@ -1474,7 +1474,7 @@ _Py_dg_strtod(const char *s00, char **se) switch (c) { case '-': esign = 1; - /* fall through */ + _Py_FALLTHROUGH; case '+': c = *++s; } @@ -2362,7 +2362,7 @@ _Py_dg_dtoa(double dd, int mode, int ndigits, break; case 2: leftright = 0; - /* fall through */ + _Py_FALLTHROUGH; case 4: if (ndigits <= 0) ndigits = 1; @@ -2370,7 +2370,7 @@ _Py_dg_dtoa(double dd, int mode, int ndigits, break; case 3: leftright = 0; - /* fall through */ + _Py_FALLTHROUGH; case 5: i = ndigits + k + 1; ilim = i; diff --git a/Python/formatter_unicode.c b/Python/formatter_unicode.c index 6af589f..ebd6721 100644 --- a/Python/formatter_unicode.c +++ b/Python/formatter_unicode.c @@ -320,7 +320,7 @@ parse_internal_render_format_spec(PyObject *obj, format->thousands_separators = LT_UNDER_FOUR_LOCALE; break; } - /* fall through */ + _Py_FALLTHROUGH; default: invalid_thousands_separator_type(format->thousands_separators, format->type); return 0; diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 4a54680..14959fb 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -5532,10 +5532,10 @@ switch (oparg) { case 2: cause = PyStackRef_AsPyObjectSteal(args[1]); - /* fall through */ + _Py_FALLTHROUGH; case 1: exc = PyStackRef_AsPyObjectSteal(args[0]); - /* fall through */ + _Py_FALLTHROUGH; case 0: if (do_raise(tstate, exc, cause)) { assert(oparg == 0); diff --git a/Python/getargs.c b/Python/getargs.c index 3e38280..b96ce3a 100644 --- a/Python/getargs.c +++ b/Python/getargs.c @@ -2676,7 +2676,7 @@ skipitem(const char **p_format, va_list *p_va, int flags) goto err; format++; } - /* fall through */ + _Py_FALLTHROUGH; case 's': /* string */ case 'z': /* string or None */ diff --git a/Python/importdl.c b/Python/importdl.c index 7c42d37..9646483 100644 --- a/Python/importdl.c +++ b/Python/importdl.c @@ -248,14 +248,14 @@ _Py_ext_module_loader_result_set_error( { #ifndef NDEBUG switch (kind) { - case _Py_ext_module_loader_result_EXCEPTION: /* fall through */ + case _Py_ext_module_loader_result_EXCEPTION: _Py_FALLTHROUGH; case _Py_ext_module_loader_result_ERR_UNREPORTED_EXC: assert(PyErr_Occurred()); break; - case _Py_ext_module_loader_result_ERR_MISSING: /* fall through */ - case _Py_ext_module_loader_result_ERR_UNINITIALIZED: /* fall through */ - case _Py_ext_module_loader_result_ERR_NONASCII_NOT_MULTIPHASE: /* fall through */ - case _Py_ext_module_loader_result_ERR_NOT_MODULE: /* fall through */ + case _Py_ext_module_loader_result_ERR_MISSING: _Py_FALLTHROUGH; + case _Py_ext_module_loader_result_ERR_UNINITIALIZED: _Py_FALLTHROUGH; + case _Py_ext_module_loader_result_ERR_NONASCII_NOT_MULTIPHASE: _Py_FALLTHROUGH; + case _Py_ext_module_loader_result_ERR_NOT_MODULE: _Py_FALLTHROUGH; case _Py_ext_module_loader_result_ERR_MISSING_DEF: assert(!PyErr_Occurred()); break; @@ -279,11 +279,11 @@ _Py_ext_module_loader_result_set_error( res->kind = _Py_ext_module_kind_INVALID; break; /* None of the rest affect the result kind. */ - case _Py_ext_module_loader_result_EXCEPTION: /* fall through */ - case _Py_ext_module_loader_result_ERR_MISSING: /* fall through */ - case _Py_ext_module_loader_result_ERR_UNREPORTED_EXC: /* fall through */ - case _Py_ext_module_loader_result_ERR_NONASCII_NOT_MULTIPHASE: /* fall through */ - case _Py_ext_module_loader_result_ERR_NOT_MODULE: /* fall through */ + case _Py_ext_module_loader_result_EXCEPTION: _Py_FALLTHROUGH; + case _Py_ext_module_loader_result_ERR_MISSING: _Py_FALLTHROUGH; + case _Py_ext_module_loader_result_ERR_UNREPORTED_EXC: _Py_FALLTHROUGH; + case _Py_ext_module_loader_result_ERR_NONASCII_NOT_MULTIPHASE: _Py_FALLTHROUGH; + case _Py_ext_module_loader_result_ERR_NOT_MODULE: _Py_FALLTHROUGH; case _Py_ext_module_loader_result_ERR_MISSING_DEF: break; default: @@ -307,14 +307,14 @@ _Py_ext_module_loader_result_apply_error( #ifndef NDEBUG switch (err.kind) { - case _Py_ext_module_loader_result_EXCEPTION: /* fall through */ + case _Py_ext_module_loader_result_EXCEPTION: _Py_FALLTHROUGH; case _Py_ext_module_loader_result_ERR_UNREPORTED_EXC: assert(err.exc != NULL); break; - case _Py_ext_module_loader_result_ERR_MISSING: /* fall through */ - case _Py_ext_module_loader_result_ERR_UNINITIALIZED: /* fall through */ - case _Py_ext_module_loader_result_ERR_NONASCII_NOT_MULTIPHASE: /* fall through */ - case _Py_ext_module_loader_result_ERR_NOT_MODULE: /* fall through */ + case _Py_ext_module_loader_result_ERR_MISSING: _Py_FALLTHROUGH; + case _Py_ext_module_loader_result_ERR_UNINITIALIZED: _Py_FALLTHROUGH; + case _Py_ext_module_loader_result_ERR_NONASCII_NOT_MULTIPHASE: _Py_FALLTHROUGH; + case _Py_ext_module_loader_result_ERR_NOT_MODULE: _Py_FALLTHROUGH; case _Py_ext_module_loader_result_ERR_MISSING_DEF: assert(err.exc == NULL); break; diff --git a/Python/marshal.c b/Python/marshal.c index a46fc0c..fe97ccd 100644 --- a/Python/marshal.c +++ b/Python/marshal.c @@ -1156,7 +1156,7 @@ r_object(RFILE *p) case TYPE_ASCII_INTERNED: is_interned = 1; - /* fall through */ + _Py_FALLTHROUGH; case TYPE_ASCII: n = r_long(p); if (n < 0 || n > SIZE32_MAX) { @@ -1170,7 +1170,7 @@ r_object(RFILE *p) case TYPE_SHORT_ASCII_INTERNED: is_interned = 1; - /* fall through */ + _Py_FALLTHROUGH; case TYPE_SHORT_ASCII: n = r_byte(p); if (n == EOF) { @@ -1198,7 +1198,7 @@ r_object(RFILE *p) case TYPE_INTERNED: is_interned = 1; - /* fall through */ + _Py_FALLTHROUGH; case TYPE_UNICODE: { const char *buffer; diff --git a/Python/modsupport.c b/Python/modsupport.c index e9abf30..0fb7783 100644 --- a/Python/modsupport.c +++ b/Python/modsupport.c @@ -306,6 +306,7 @@ do_mkvalue(const char **p_format, va_list *p_va) return PyLong_FromSsize_t(va_arg(*p_va, Py_ssize_t)); #endif /* Fall through from 'n' to 'l' if Py_ssize_t is long */ + _Py_FALLTHROUGH; case 'l': return PyLong_FromLong(va_arg(*p_va, long)); diff --git a/Python/pyhash.c b/Python/pyhash.c index 4145d9e..1504fa2 100644 --- a/Python/pyhash.c +++ b/Python/pyhash.c @@ -170,12 +170,12 @@ _Py_HashBytes(const void *src, Py_ssize_t len) switch(len) { /* ((hash << 5) + hash) + *p == hash * 33 + *p */ - case 7: hash = ((hash << 5) + hash) + *p++; /* fallthrough */ - case 6: hash = ((hash << 5) + hash) + *p++; /* fallthrough */ - case 5: hash = ((hash << 5) + hash) + *p++; /* fallthrough */ - case 4: hash = ((hash << 5) + hash) + *p++; /* fallthrough */ - case 3: hash = ((hash << 5) + hash) + *p++; /* fallthrough */ - case 2: hash = ((hash << 5) + hash) + *p++; /* fallthrough */ + case 7: hash = ((hash << 5) + hash) + *p++; _Py_FALLTHROUGH; + case 6: hash = ((hash << 5) + hash) + *p++; _Py_FALLTHROUGH; + case 5: hash = ((hash << 5) + hash) + *p++; _Py_FALLTHROUGH; + case 4: hash = ((hash << 5) + hash) + *p++; _Py_FALLTHROUGH; + case 3: hash = ((hash << 5) + hash) + *p++; _Py_FALLTHROUGH; + case 2: hash = ((hash << 5) + hash) + *p++; _Py_FALLTHROUGH; case 1: hash = ((hash << 5) + hash) + *p++; break; default: Py_UNREACHABLE(); @@ -391,13 +391,13 @@ siphash13(uint64_t k0, uint64_t k1, const void *src, Py_ssize_t src_sz) { t = 0; pt = (uint8_t *)&t; switch (src_sz) { - case 7: pt[6] = in[6]; /* fall through */ - case 6: pt[5] = in[5]; /* fall through */ - case 5: pt[4] = in[4]; /* fall through */ + case 7: pt[6] = in[6]; _Py_FALLTHROUGH; + case 6: pt[5] = in[5]; _Py_FALLTHROUGH; + case 5: pt[4] = in[4]; _Py_FALLTHROUGH; case 4: memcpy(pt, in, sizeof(uint32_t)); break; - case 3: pt[2] = in[2]; /* fall through */ - case 2: pt[1] = in[1]; /* fall through */ - case 1: pt[0] = in[0]; /* fall through */ + case 3: pt[2] = in[2]; _Py_FALLTHROUGH; + case 2: pt[1] = in[1]; _Py_FALLTHROUGH; + case 1: pt[0] = in[0]; break; } b |= _le64toh(t); @@ -442,13 +442,13 @@ siphash24(uint64_t k0, uint64_t k1, const void *src, Py_ssize_t src_sz) { t = 0; pt = (uint8_t *)&t; switch (src_sz) { - case 7: pt[6] = in[6]; /* fall through */ - case 6: pt[5] = in[5]; /* fall through */ - case 5: pt[4] = in[4]; /* fall through */ + case 7: pt[6] = in[6]; _Py_FALLTHROUGH; + case 6: pt[5] = in[5]; _Py_FALLTHROUGH; + case 5: pt[4] = in[4]; _Py_FALLTHROUGH; case 4: memcpy(pt, in, sizeof(uint32_t)); break; - case 3: pt[2] = in[2]; /* fall through */ - case 2: pt[1] = in[1]; /* fall through */ - case 1: pt[0] = in[0]; /* fall through */ + case 3: pt[2] = in[2]; _Py_FALLTHROUGH; + case 2: pt[1] = in[1]; _Py_FALLTHROUGH; + case 1: pt[0] = in[0]; break; } b |= _le64toh(t); diff --git a/Python/pystrtod.c b/Python/pystrtod.c index 5c8be04..2f2b588 100644 --- a/Python/pystrtod.c +++ b/Python/pystrtod.c @@ -1234,7 +1234,7 @@ char * PyOS_double_to_string(double val, case 'E': float_strings = uc_float_strings; format_code = 'e'; - /* Fall through. */ + _Py_FALLTHROUGH; case 'e': mode = 2; precision++; @@ -1244,7 +1244,7 @@ char * PyOS_double_to_string(double val, case 'F': float_strings = uc_float_strings; format_code = 'f'; - /* Fall through. */ + _Py_FALLTHROUGH; case 'f': mode = 3; break; @@ -1253,7 +1253,7 @@ char * PyOS_double_to_string(double val, case 'G': float_strings = uc_float_strings; format_code = 'g'; - /* Fall through. */ + _Py_FALLTHROUGH; case 'g': mode = 2; /* precision 0 makes no sense for 'g' format; interpret as 1 */ |