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>
This commit is contained in:
Victor Stinner 2024-06-27 11:58:44 +02:00 committed by GitHub
parent 4999e0bda0
commit 12af8ec864
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
29 changed files with 131 additions and 104 deletions

View file

@ -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;

View file

@ -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);

View file

@ -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:

View file

@ -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

View file

@ -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;

View file

@ -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;

View file

@ -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);

View file

@ -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 */

View file

@ -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;

View file

@ -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;

View file

@ -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));

View file

@ -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);

View file

@ -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 */