Merge branch 'wip/otte/fallthrough' into 'master'

Add G_GNUC_FALLTHROUGH for __attribute__(fallthrough))

See merge request GNOME/glib!296
This commit is contained in:
Philip Withnall 2018-09-04 22:06:26 +00:00
commit 98f326a020
12 changed files with 45 additions and 7 deletions

View File

@ -438,6 +438,7 @@ G_GNUC_DEPRECATED_FOR
G_GNUC_BEGIN_IGNORE_DEPRECATIONS G_GNUC_BEGIN_IGNORE_DEPRECATIONS
G_GNUC_END_IGNORE_DEPRECATIONS G_GNUC_END_IGNORE_DEPRECATIONS
G_GNUC_NORETURN G_GNUC_NORETURN
G_GNUC_FALLTHROUGH
G_GNUC_UNUSED G_GNUC_UNUSED
G_GNUC_PRINTF G_GNUC_PRINTF
G_GNUC_SCANF G_GNUC_SCANF

View File

@ -166,9 +166,11 @@ test_data_array (guchar *buffer, gsize len,
case TEST_DATA_INT16: case TEST_DATA_INT16:
case TEST_DATA_UINT16: case TEST_DATA_UINT16:
g_assert_cmpint (len % 2, ==, 0); g_assert_cmpint (len % 2, ==, 0);
G_GNUC_FALLTHROUGH;
case TEST_DATA_INT32: case TEST_DATA_INT32:
case TEST_DATA_UINT32: case TEST_DATA_UINT32:
g_assert_cmpint (len % 4, ==, 0); g_assert_cmpint (len % 4, ==, 0);
G_GNUC_FALLTHROUGH;
case TEST_DATA_INT64: case TEST_DATA_INT64:
case TEST_DATA_UINT64: case TEST_DATA_UINT64:
g_assert_cmpint (len % 8, ==, 0); g_assert_cmpint (len % 8, ==, 0);

View File

@ -33,12 +33,14 @@ cook_piece (void)
{ {
case 26: case 26:
buffer[i++] = '\n'; buffer[i++] = '\n';
G_GNUC_FALLTHROUGH;
case 27: case 27:
buffer[i++] = '\r'; buffer[i++] = '\r';
break; break;
case 28: case 28:
buffer[i++] = '\r'; buffer[i++] = '\r';
G_GNUC_FALLTHROUGH;
case 29: case 29:
buffer[i++] = '\n'; buffer[i++] = '\n';
break; break;

View File

@ -2280,6 +2280,22 @@
* See the GNU C documentation for more details. * See the GNU C documentation for more details.
*/ */
/**
* G_GNUC_FALLTHROUGH:
*
* Expands to the GNU C fallthrough statement attribute if the compiler is gcc.
* This allows declaring case statement to explicitly fall through in switch
* statements. To enable this feature, use -Wimplicit-fallthrough during
* compilation.
*
* Put the attribute right before the case statement you want to fall through
* to.
*
* See the GNU C documentation for more details.
*
* Since: 2.60
*/
/** /**
* G_GNUC_UNUSED: * G_GNUC_UNUSED:
* *

View File

@ -170,8 +170,11 @@ g_base64_encode_step (const guchar *in,
/* len can only be 0 1 or 2 */ /* len can only be 0 1 or 2 */
switch(len) switch(len)
{ {
case 2: *saveout++ = *inptr++; case 2:
case 1: *saveout++ = *inptr++; *saveout++ = *inptr++;
G_GNUC_FALLTHROUGH;
case 1:
*saveout++ = *inptr++;
} }
((char *)save)[0] += len; ((char *)save)[0] += len;
} }

View File

@ -788,7 +788,8 @@ g_convert_with_fallback (const gchar *str,
inbytes_remaining = strlen (p); inbytes_remaining = strlen (p);
break; break;
} }
/* fall thru if p is NULL */ /* if p is null */
G_GNUC_FALLTHROUGH;
default: default:
{ {
int errsv = errno; int errsv = errno;

View File

@ -191,6 +191,12 @@
#define G_GNUC_NO_INSTRUMENT #define G_GNUC_NO_INSTRUMENT
#endif /* !__GNUC__ */ #endif /* !__GNUC__ */
#if __GNUC__ > 6
#define G_GNUC_FALLTHROUGH __attribute__((fallthrough))
#else
#define G_GNUC_FALLTHROUGH
#endif /* __GNUC__ */
#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1) #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
#define G_GNUC_DEPRECATED __attribute__((__deprecated__)) #define G_GNUC_DEPRECATED __attribute__((__deprecated__))
#else #else

View File

@ -2872,6 +2872,8 @@ failure:
case G_MARKUP_COLLECT_STRDUP: case G_MARKUP_COLLECT_STRDUP:
if (written) if (written)
g_free (*(char **) ptr); g_free (*(char **) ptr);
*(char **) ptr = NULL;
break;
case G_MARKUP_COLLECT_STRING: case G_MARKUP_COLLECT_STRING:
*(char **) ptr = NULL; *(char **) ptr = NULL;

View File

@ -2537,6 +2537,7 @@ expand_escape (const gchar *replacement,
base = 8; base = 8;
p = g_utf8_next_char (p); p = g_utf8_next_char (p);
} }
G_GNUC_FALLTHROUGH;
case '1': case '1':
case '2': case '2':
case '3': case '3':

View File

@ -372,6 +372,8 @@ parse_constant_offset (const gchar *name,
*offset = -*offset; *offset = -*offset;
return TRUE; return TRUE;
} }
else
return FALSE;
default: default:
return FALSE; return FALSE;

View File

@ -210,10 +210,7 @@ token_stream_prepare (TokenStream *stream)
break; break;
} }
else G_GNUC_FALLTHROUGH;
{
/* ↓↓↓ */
}
case 'a': /* 'b' */ case 'c': case 'd': case 'e': case 'f': case 'a': /* 'b' */ case 'c': case 'd': case 'e': case 'f':
case 'g': case 'h': case 'i': case 'j': case 'k': case 'l': case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
@ -1646,6 +1643,8 @@ string_parse (TokenStream *stream,
case '\n': i++; continue; case '\n': i++; continue;
} }
G_GNUC_FALLTHROUGH;
default: default:
str[j++] = token[i++]; str[j++] = token[i++];
} }
@ -1773,6 +1772,8 @@ bytestring_parse (TokenStream *stream,
case '\n': i++; continue; case '\n': i++; continue;
} }
G_GNUC_FALLTHROUGH;
default: default:
str[j++] = token[i++]; str[j++] = token[i++];
} }

View File

@ -344,6 +344,7 @@ if cc.get_id() == 'gcc' or cc.get_id() == 'clang'
test_c_args = [ test_c_args = [
'-Wall', '-Wall',
'-Wduplicated-branches', '-Wduplicated-branches',
'-Wimplicit-fallthrough',
'-Wmisleading-indentation', '-Wmisleading-indentation',
'-Wstrict-prototypes', '-Wstrict-prototypes',
'-Wunused', '-Wunused',