From c704824b45731f2400355ebe8ae0c576e19dc072 Mon Sep 17 00:00:00 2001 From: Niklas Haas Date: Mon, 25 Oct 2021 10:49:11 +0200 Subject: [PATCH] osdep: add MP_UNREACHABLE This seems to work on gcc, clang and mingw as-is, but I made it conditional on __GNUC__ just in case, even though I can't figure out which compilers we care about that don't export this define. Also replace all instances of assert(0) in the code by MP_UNREACHABLE(), which is a strict improvement. --- demux/demux.c | 2 +- demux/demux_mkv.c | 2 +- filters/f_lavfi.c | 4 ++-- filters/f_utils.c | 2 +- filters/filter.c | 2 +- misc/thread_pool.c | 2 +- options/m_config_core.c | 2 +- options/parse_configfile.c | 2 +- osdep/compiler.h | 6 ++++++ player/command.c | 2 +- video/mp_image.c | 2 +- video/out/gpu/video.c | 2 +- video/repack.c | 2 +- 13 files changed, 19 insertions(+), 13 deletions(-) diff --git a/demux/demux.c b/demux/demux.c index fdec805d15..7e68a05c2f 100644 --- a/demux/demux.c +++ b/demux/demux.c @@ -1506,7 +1506,7 @@ static void find_backward_restart_pos(struct demux_stream *ds) // Or if preroll is involved, the first preroll packet. while (ds->reader_head != target) { if (!advance_reader_head(ds)) - assert(0); // target must be in list + MP_UNREACHABLE(); // target must be in list } double seek_pts; diff --git a/demux/demux_mkv.c b/demux/demux_mkv.c index b0117f0b6d..ba9c934b7a 100644 --- a/demux/demux_mkv.c +++ b/demux/demux_mkv.c @@ -2872,7 +2872,7 @@ static int read_next_block_into_queue(demuxer_t *demuxer) if (mkv_d->cluster_end != EBML_UINT_INVALID) mkv_d->cluster_end += stream_tell(s); } - assert(0); // unreachable + MP_UNREACHABLE(); add_block: index_block(demuxer, &block); diff --git a/filters/f_lavfi.c b/filters/f_lavfi.c index 9e64215f39..b66bc8584b 100644 --- a/filters/f_lavfi.c +++ b/filters/f_lavfi.c @@ -394,7 +394,7 @@ static bool init_pads(struct lavfi *c) } else if (pad->type == MP_FRAME_VIDEO) { dst_filter = avfilter_get_by_name("buffersink"); } else { - assert(0); + MP_UNREACHABLE(); } if (!dst_filter) @@ -484,7 +484,7 @@ static bool init_pads(struct lavfi *c) params->frame_rate = av_d2q(fmt->nominal_fps, 1000000); filter_name = "buffer"; } else { - assert(0); + MP_UNREACHABLE(); } params->time_base = pad->timebase; diff --git a/filters/f_utils.c b/filters/f_utils.c index 1b5b0e548e..c166e2a4db 100644 --- a/filters/f_utils.c +++ b/filters/f_utils.c @@ -250,7 +250,7 @@ static void fixed_aframe_size_process(struct mp_filter *f) int in_samples = mp_aframe_get_size(p->in); int copy = MPMIN(in_samples, p->samples - p->out_written); if (!mp_aframe_copy_samples(p->out, p->out_written, p->in, 0, copy)) - assert(0); + MP_UNREACHABLE(); mp_aframe_skip_samples(p->in, copy); p->out_written += copy; } diff --git a/filters/filter.c b/filters/filter.c index cddeea0a3f..667925c43c 100644 --- a/filters/filter.c +++ b/filters/filter.c @@ -372,7 +372,7 @@ static struct mp_pin *find_connected_end(struct mp_pin *p) return other; p = other->user_conn; } - assert(0); + MP_UNREACHABLE(); } // With p being part of a connection, create the pin_connection and set all diff --git a/misc/thread_pool.c b/misc/thread_pool.c index 217c990c19..e2c7a238dd 100644 --- a/misc/thread_pool.c +++ b/misc/thread_pool.c @@ -105,7 +105,7 @@ static void *worker_thread(void *arg) return NULL; } } - assert(0); + MP_UNREACHABLE(); } pthread_mutex_unlock(&pool->lock); diff --git a/options/m_config_core.c b/options/m_config_core.c index f82b886670..fd476a0627 100644 --- a/options/m_config_core.c +++ b/options/m_config_core.c @@ -879,7 +879,7 @@ void mp_read_option_raw(struct mpv_global *global, const char *name, } } - assert(0); // not found + MP_UNREACHABLE(); // not found } static const struct m_config_group *find_group(struct mpv_global *global, diff --git a/options/parse_configfile.c b/options/parse_configfile.c index 627c466948..1ef256989f 100644 --- a/options/parse_configfile.c +++ b/options/parse_configfile.c @@ -176,7 +176,7 @@ static bstr read_file(struct mp_log *log, const char *filename) } size += s; } - assert(0); + MP_UNREACHABLE(); } // Load options and profiles from from a config file. diff --git a/osdep/compiler.h b/osdep/compiler.h index 7c9f859f3a..471fc2ffd1 100644 --- a/osdep/compiler.h +++ b/osdep/compiler.h @@ -23,4 +23,10 @@ #define alignof(x) (offsetof(struct {char unalign_; x u;}, u)) #endif +#ifdef __GNUC__ +#define MP_UNREACHABLE() (assert(!"unreachable"), __builtin_unreachable()) +#else +#define MP_UNREACHABLE() (assert(!"unreachable"), abort()) +#endif + #endif diff --git a/player/command.c b/player/command.c index 4f0055513d..6a639fd361 100644 --- a/player/command.c +++ b/player/command.c @@ -155,7 +155,7 @@ static void hook_remove(struct MPContext *mpctx, struct hook_handler *h) return; } } - assert(0); + MP_UNREACHABLE(); } bool mp_hook_test_completion(struct MPContext *mpctx, char *type) diff --git a/video/mp_image.c b/video/mp_image.c index 0e95a59808..e8d433cbfe 100644 --- a/video/mp_image.c +++ b/video/mp_image.c @@ -593,7 +593,7 @@ static bool endian_swap_bytes(void *d, size_t bytes, size_t word_size) AV_WL32(ud + x * 2, AV_RB32(ud + x * 2)); break; default: - assert(0); + MP_UNREACHABLE(); } return true; diff --git a/video/out/gpu/video.c b/video/out/gpu/video.c index 5d6b766d36..43fff511a1 100644 --- a/video/out/gpu/video.c +++ b/video/out/gpu/video.c @@ -4237,7 +4237,7 @@ static void gl_video_dr_free_buffer(void *opaque, uint8_t *data) } } // not found - must not happen - assert(0); + MP_UNREACHABLE(); } struct mp_image *gl_video_get_image(struct gl_video *p, int imgfmt, int w, int h, diff --git a/video/repack.c b/video/repack.c index ed78981b3c..7973208caf 100644 --- a/video/repack.c +++ b/video/repack.c @@ -169,7 +169,7 @@ static void swap_endian(struct mp_image *dst, int dst_x, int dst_y, ((uint32_t *)d)[x] = av_bswap32(((uint32_t *)s)[x]); break; default: - assert(0); + MP_UNREACHABLE(); } } }