Skip to content

Commit 92c4b06

Browse files
committed
Use ZEND_UNREACHABLE() instead of ZEND_ASSERT(0)
Instead of marking unreachable code with `ZEND_ASSERT(0)`, we introduce `ZEND_UNREACHABLE()`, so that MSVC which does not consider `assert(0)` to mark unreachable code does no longer trigger C4715[1] warnings in debug builds. This may be useful for other compilers as well. [1] <https://docs.microsoft.com/de-de/cpp/error-messages/compiler-warnings/compiler-warning-level-1-c4715?view=vs-2019>
1 parent 9ff3230 commit 92c4b06

26 files changed

+122
-120
lines changed

Zend/zend.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ static void zend_get_windows_version_info(OSVERSIONINFOEX *osvi) /* {{{ */
529529
ZeroMemory(osvi, sizeof(OSVERSIONINFOEX));
530530
osvi->dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
531531
if(!GetVersionEx((OSVERSIONINFO *) osvi)) {
532-
ZEND_ASSERT(0); /* Should not happen as sizeof is used. */
532+
ZEND_UNREACHABLE(); /* Should not happen as sizeof is used. */
533533
}
534534
}
535535
/* }}} */
@@ -1718,7 +1718,7 @@ ZEND_API void *zend_map_ptr_new(void)
17181718
if (CG(map_ptr_last) >= CG(map_ptr_size)) {
17191719
#if ZEND_MAP_PTR_KIND == ZEND_MAP_PTR_KIND_PTR
17201720
// TODO: error ???
1721-
ZEND_ASSERT(0);
1721+
ZEND_UNREACHABLE();
17221722
#elif ZEND_MAP_PTR_KIND == ZEND_MAP_PTR_KIND_PTR_OR_OFFSET
17231723
/* Grow map_ptr table */
17241724
CG(map_ptr_size) = ZEND_MM_ALIGNED_SIZE_EX(CG(map_ptr_last) + 1, 4096);
@@ -1747,7 +1747,7 @@ ZEND_API void zend_map_ptr_extend(size_t last)
17471747
if (last >= CG(map_ptr_size)) {
17481748
#if ZEND_MAP_PTR_KIND == ZEND_MAP_PTR_KIND_PTR
17491749
/* This may never happen */
1750-
ZEND_ASSERT(0);
1750+
ZEND_UNREACHABLE();
17511751
#elif ZEND_MAP_PTR_KIND == ZEND_MAP_PTR_KIND_PTR_OR_OFFSET
17521752
/* Grow map_ptr table */
17531753
CG(map_ptr_size) = ZEND_MM_ALIGNED_SIZE_EX(last, 4096);

Zend/zend_ast.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1465,7 +1465,7 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
14651465
break;
14661466
case ZEND_AST_ZNODE:
14671467
/* This AST kind is only used for temporary nodes during compilation */
1468-
ZEND_ASSERT(0);
1468+
ZEND_UNREACHABLE();
14691469
break;
14701470

14711471
/* declaration nodes */

Zend/zend_compile.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1903,7 +1903,7 @@ zend_ast *zend_negate_num_string(zend_ast *ast) /* {{{ */
19031903
memmove(Z_STRVAL_P(zv) + 1, Z_STRVAL_P(zv), orig_len + 1);
19041904
Z_STRVAL_P(zv)[0] = '-';
19051905
} else {
1906-
ZEND_ASSERT(0);
1906+
ZEND_UNREACHABLE();
19071907
}
19081908
return ast;
19091909
}
@@ -2258,7 +2258,7 @@ static void zend_compile_memoized_expr(znode *result, zend_ast *expr) /* {{{ */
22582258
Z_TRY_ADDREF(result->u.constant);
22592259
}
22602260
} else {
2261-
ZEND_ASSERT(0);
2261+
ZEND_UNREACHABLE();
22622262
}
22632263
}
22642264
/* }}} */

Zend/zend_hash.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ static void _zend_is_inconsistent(const HashTable *ht, const char *file, int lin
6868
zend_output_debug_string(1, "%s(%d) : ht=%p is inconsistent", file, line, ht);
6969
break;
7070
}
71-
ZEND_ASSERT(0);
71+
ZEND_UNREACHABLE();
7272
}
7373
#define IS_CONSISTENT(a) _zend_is_inconsistent(a, __FILE__, __LINE__);
7474
#define SET_INCONSISTENT(n) do { \

Zend/zend_inheritance.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1280,7 +1280,7 @@ ZEND_API void zend_do_inheritance_ex(zend_class_entry *ce, zend_class_entry *par
12801280
zend_class_init_statics(parent_ce);
12811281
}
12821282
if (UNEXPECTED(zend_update_class_constants(parent_ce) != SUCCESS)) {
1283-
ZEND_ASSERT(0);
1283+
ZEND_UNREACHABLE();
12841284
}
12851285
src = CE_STATIC_MEMBERS(parent_ce) + parent_ce->default_static_members_count;
12861286
do {

Zend/zend_object_handlers.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1757,7 +1757,7 @@ ZEND_API HashTable *zend_std_get_properties_for(zend_object *obj, zend_prop_purp
17571757
}
17581758
return ht;
17591759
default:
1760-
ZEND_ASSERT(0);
1760+
ZEND_UNREACHABLE();
17611761
return NULL;
17621762
}
17631763
}

Zend/zend_opcode.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ static void emit_live_range(
631631
case ZEND_ADD_ARRAY_ELEMENT:
632632
case ZEND_ADD_ARRAY_UNPACK:
633633
case ZEND_ROPE_ADD:
634-
ZEND_ASSERT(0);
634+
ZEND_UNREACHABLE();
635635
return;
636636
/* Result is boolean, it doesn't have to be destroyed. */
637637
case ZEND_JMPZ_EX:
@@ -1131,7 +1131,7 @@ ZEND_API binary_op_type get_binary_op(int opcode)
11311131
case ZEND_BOOL_XOR:
11321132
return (binary_op_type) boolean_xor_function;
11331133
default:
1134-
ZEND_ASSERT(0);
1134+
ZEND_UNREACHABLE();
11351135
return (binary_op_type) NULL;
11361136
}
11371137
}

Zend/zend_operators.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2089,7 +2089,7 @@ ZEND_API int ZEND_FASTCALL zend_compare(zval *op1, zval *op2) /* {{{ */
20892089
} else if (Z_TYPE_P(op2)==IS_ARRAY) {
20902090
return -1;
20912091
} else {
2092-
ZEND_ASSERT(0);
2092+
ZEND_UNREACHABLE();
20932093
zend_throw_error(NULL, "Unsupported operand types");
20942094
return 1;
20952095
}

Zend/zend_portability.h

+6-4
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,16 @@
107107
# define ZEND_ASSERT(c) ZEND_ASSUME(c)
108108
#endif
109109

110-
/* Only use this macro if you know for sure that all of the switches values
111-
are covered by its case statements */
112110
#if ZEND_DEBUG
113-
# define EMPTY_SWITCH_DEFAULT_CASE() default: ZEND_ASSERT(0); break;
111+
# define ZEND_UNREACHABLE() do {ZEND_ASSERT(0); ZEND_ASSUME(0);} while (0)
114112
#else
115-
# define EMPTY_SWITCH_DEFAULT_CASE() default: ZEND_ASSUME(0); break;
113+
# define ZEND_UNREACHABLE() ZEND_ASSUME(0)
116114
#endif
117115

116+
/* Only use this macro if you know for sure that all of the switches values
117+
are covered by its case statements */
118+
#define EMPTY_SWITCH_DEFAULT_CASE() default: ZEND_UNREACHABLE(); break;
119+
118120
#if defined(__GNUC__) && __GNUC__ >= 4
119121
# define ZEND_IGNORE_VALUE(x) (({ __typeof__ (x) __x = (x); (void) __x; }))
120122
#else

ext/ffi/ffi.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -1504,7 +1504,7 @@ static int zend_ffi_ctype_name(zend_ffi_ctype_name_buf *buf, const zend_ffi_type
15041504
}
15051505
break;
15061506
default:
1507-
ZEND_ASSERT(0);
1507+
ZEND_UNREACHABLE();
15081508
}
15091509
if (name) {
15101510
break;
@@ -1992,7 +1992,7 @@ static HashTable *zend_ffi_cdata_get_debug_info(zend_object *obj, int *is_temp)
19921992
return ht;
19931993
break;
19941994
default:
1995-
ZEND_ASSERT(0);
1995+
ZEND_UNREACHABLE();
19961996
break;
19971997
}
19981998
return NULL;
@@ -5364,7 +5364,7 @@ void zend_ffi_resolve_const(const char *name, size_t name_len, zend_ffi_val *val
53645364
val->kind = ZEND_FFI_VAL_UINT64;
53655365
break;
53665366
default:
5367-
ZEND_ASSERT(0);
5367+
ZEND_UNREACHABLE();
53685368
}
53695369
return;
53705370
}
@@ -6185,7 +6185,7 @@ void zend_ffi_declare_tag(const char *name, size_t name_len, zend_ffi_dcl *dcl,
61856185
return;
61866186
}
61876187
} else {
6188-
ZEND_ASSERT(0);
6188+
ZEND_UNREACHABLE();
61896189
return;
61906190
}
61916191
dcl->type = type;
@@ -6212,7 +6212,7 @@ void zend_ffi_declare_tag(const char *name, size_t name_len, zend_ffi_dcl *dcl,
62126212
type = ZEND_FFI_TYPE(dcl->type);
62136213
type->enumeration.tag_name = zend_string_copy(tag_name);
62146214
} else {
6215-
ZEND_ASSERT(0);
6215+
ZEND_UNREACHABLE();
62166216
}
62176217
tag->type = ZEND_FFI_TYPE_MAKE_OWNED(dcl->type);
62186218
dcl->type = ZEND_FFI_TYPE(dcl->type);
@@ -6511,7 +6511,7 @@ static int zend_ffi_nested_type(zend_ffi_type *type, zend_ffi_type *nested_type)
65116511
}
65126512
break;
65136513
default:
6514-
ZEND_ASSERT(0);
6514+
ZEND_UNREACHABLE();
65156515
}
65166516
}
65176517
/* }}} */

ext/gd/gd.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1872,7 +1872,7 @@ static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, char
18721872
(*func_p)(im, fp, q, t);
18731873
break;
18741874
default:
1875-
ZEND_ASSERT(0);
1875+
ZEND_UNREACHABLE();
18761876
}
18771877
fflush(fp);
18781878
fclose(fp);
@@ -1899,7 +1899,7 @@ static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, char
18991899
(*func_p)(im, tmp, q, t);
19001900
break;
19011901
default:
1902-
ZEND_ASSERT(0);
1902+
ZEND_UNREACHABLE();
19031903
}
19041904

19051905
fseek(tmp, 0, SEEK_SET);

ext/opcache/Optimizer/optimize_func_calls.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ void zend_optimize_func_calls(zend_op_array *op_array, zend_optimizer_ctx *ctx)
214214
|| fcall->opcode == ZEND_NEW) {
215215
/* We don't have specialized opcodes for this, do nothing */
216216
} else {
217-
ZEND_ASSERT(0);
217+
ZEND_UNREACHABLE();
218218
}
219219

220220
if ((ZEND_OPTIMIZER_PASS_16 & ctx->optimization_level)

ext/opcache/Optimizer/sccp.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ static zend_bool can_replace_op1(
267267
(opline - 1)->opcode != ZEND_ASSIGN_STATIC_PROP_REF;
268268
default:
269269
if (ssa_op->op1_def != -1) {
270-
ZEND_ASSERT(0);
270+
ZEND_UNREACHABLE();
271271
return 0;
272272
}
273273
}

ext/opcache/Optimizer/scdf.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ static inline uint32_t scdf_edge(zend_cfg *cfg, int from, int to) {
8686
return edge;
8787
}
8888
}
89-
ZEND_ASSERT(0);
89+
ZEND_UNREACHABLE();
9090
}
9191

9292
static inline zend_bool scdf_is_edge_feasible(scdf_ctx *scdf, int from, int to) {

ext/opcache/Optimizer/zend_inference.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -3510,7 +3510,7 @@ static zend_always_inline int _zend_update_type_info(
35103510
case ZEND_CATCH:
35113511
case ZEND_INCLUDE_OR_EVAL:
35123512
/* Forbidden opcodes */
3513-
ZEND_ASSERT(0);
3513+
ZEND_UNREACHABLE();
35143514
break;
35153515
default:
35163516
unknown_opcode:

ext/opcache/Optimizer/zend_optimizer.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ int zend_optimizer_replace_by_const(zend_op_array *op_array,
615615
ZEND_ASSERT(opline->extended_value == ZEND_FREE_ON_RETURN);
616616
MAKE_NOP(opline);
617617
} else {
618-
ZEND_ASSERT(0);
618+
ZEND_UNREACHABLE();
619619
}
620620
}
621621
opline++;

ext/opcache/Optimizer/zend_ssa.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ int zend_ssa_unlink_use_chain(zend_ssa *ssa, int op, int var) /* {{{ */
11891189
}
11901190
}
11911191
/* something wrong */
1192-
ZEND_ASSERT(0);
1192+
ZEND_UNREACHABLE();
11931193
return 0;
11941194
}
11951195
}
@@ -1238,7 +1238,7 @@ static inline zend_ssa_phi **zend_ssa_next_use_phi_ptr(zend_ssa *ssa, int var, z
12381238
}
12391239
}
12401240
}
1241-
ZEND_ASSERT(0);
1241+
ZEND_UNREACHABLE();
12421242
return NULL;
12431243
}
12441244
/* }}} */

ext/opcache/jit/zend_jit.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -3373,7 +3373,7 @@ ZEND_EXT_API int zend_jit_op_array(zend_op_array *op_array, zend_script *script)
33733373
return SUCCESS;
33743374
}
33753375
} else {
3376-
ZEND_ASSERT(0);
3376+
ZEND_UNREACHABLE();
33773377
}
33783378
}
33793379

@@ -3485,7 +3485,7 @@ ZEND_EXT_API int zend_jit_script(zend_script *script)
34853485
ZEND_SET_FUNC_INFO(call_graph.op_arrays[i], NULL);
34863486
}
34873487
} else {
3488-
ZEND_ASSERT(0);
3488+
ZEND_UNREACHABLE();
34893489
}
34903490

34913491
zend_arena_release(&CG(arena), checkpoint);

ext/opcache/jit/zend_jit_internal.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -451,10 +451,10 @@ static zend_always_inline const zend_op* zend_jit_trace_get_exit_opline(zend_jit
451451
*exit_if_true = opline->opcode == ZEND_JMPZ;
452452
return opline + 1;
453453
} else {
454-
ZEND_ASSERT(0);
454+
ZEND_UNREACHABLE();
455455
}
456456
} else {
457-
ZEND_ASSERT(0);
457+
ZEND_UNREACHABLE();
458458
}
459459
*exit_if_true = 0;
460460
return NULL;
@@ -476,7 +476,7 @@ static zend_always_inline zend_bool zend_jit_may_be_polymorphic_call(const zend_
476476
} else if (opline->opcode == ZEND_NEW) {
477477
return (opline->op1_type != IS_CONST);
478478
} else {
479-
ZEND_ASSERT(0);
479+
ZEND_UNREACHABLE();
480480
return 0;
481481
}
482482
}

ext/opcache/jit/zend_jit_trace.c

+10-10
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static zend_always_inline const char *zend_jit_trace_star_desc(uint8_t trace_fla
4141
} else if (trace_flags & ZEND_JIT_TRACE_START_RETURN) {
4242
return "return";
4343
} else {
44-
ZEND_ASSERT(0);
44+
ZEND_UNREACHABLE();
4545
return "???";
4646
}
4747
}
@@ -212,7 +212,7 @@ static uint32_t zend_jit_find_trace(const void *addr)
212212
return i;
213213
}
214214
}
215-
ZEND_ASSERT(0);
215+
ZEND_UNREACHABLE();
216216
return 0;
217217
}
218218

@@ -2711,7 +2711,7 @@ static const void *zend_jit_trace(zend_jit_trace_rec *trace_buffer, uint32_t par
27112711
} else if (i < parent_vars_count
27122712
&& STACK_TYPE(parent_stack, i) != IS_UNKNOWN) {
27132713
/* This must be already handled by trace type inference */
2714-
ZEND_ASSERT(0);
2714+
ZEND_UNREACHABLE();
27152715
SET_STACK_TYPE(stack, i, STACK_TYPE(parent_stack, i));
27162716
} else if ((info & MAY_BE_GUARD) != 0
27172717
&& trace_buffer->stop == ZEND_JIT_TRACE_STOP_LOOP
@@ -2729,7 +2729,7 @@ static const void *zend_jit_trace(zend_jit_trace_rec *trace_buffer, uint32_t par
27292729
&& op_array->function_name
27302730
&& i >= op_array->num_args) {
27312731
/* This must be already handled by trace type inference */
2732-
ZEND_ASSERT(0);
2732+
ZEND_UNREACHABLE();
27332733
SET_STACK_TYPE(stack, i, IS_UNDEF);
27342734
} else {
27352735
SET_STACK_TYPE(stack, i, IS_UNKNOWN);
@@ -3574,7 +3574,7 @@ static const void *zend_jit_trace(zend_jit_trace_rec *trace_buffer, uint32_t par
35743574
smart_branch_opcode = opline->opcode;
35753575
exit_opline = OP_JMP_ADDR(opline, opline->op2);
35763576
} else {
3577-
ZEND_ASSERT(0);
3577+
ZEND_UNREACHABLE();
35783578
}
35793579
if (ra) {
35803580
zend_jit_trace_clenup_stack(stack, opline, ssa_op, ssa, ra);
@@ -3585,7 +3585,7 @@ static const void *zend_jit_trace(zend_jit_trace_rec *trace_buffer, uint32_t par
35853585
goto jit_failure;
35863586
}
35873587
} else {
3588-
ZEND_ASSERT(0);
3588+
ZEND_UNREACHABLE();
35893589
}
35903590
if (opline->result_type == IS_UNDEF) {
35913591
res_addr = 0;
@@ -4234,7 +4234,7 @@ static const void *zend_jit_trace(zend_jit_trace_rec *trace_buffer, uint32_t par
42344234
frame->call = call->prev;
42354235
}
42364236
} else {
4237-
ZEND_ASSERT(0);
4237+
ZEND_UNREACHABLE();
42384238
}
42394239
}
42404240

@@ -5278,7 +5278,7 @@ int ZEND_FASTCALL zend_jit_trace_exit(uint32_t exit_num, zend_jit_registers_buf
52785278
} else if (STACK_REG(stack, i) == ZREG_LONG_MAX) {
52795279
val = ZEND_LONG_MAX;
52805280
} else {
5281-
ZEND_ASSERT(0);
5281+
ZEND_UNREACHABLE();
52825282
}
52835283
ZVAL_LONG(EX_VAR_NUM(i), val);
52845284
} else if (STACK_TYPE(stack, i) == IS_DOUBLE) {
@@ -5291,11 +5291,11 @@ int ZEND_FASTCALL zend_jit_trace_exit(uint32_t exit_num, zend_jit_registers_buf
52915291
} else if (STACK_REG(stack, i) == ZREG_LONG_MAX_PLUS_1) {
52925292
val = (double)ZEND_LONG_MAX + 1.0;
52935293
} else {
5294-
ZEND_ASSERT(0);
5294+
ZEND_UNREACHABLE();
52955295
}
52965296
ZVAL_DOUBLE(EX_VAR_NUM(i), val);
52975297
} else {
5298-
ZEND_ASSERT(0);
5298+
ZEND_UNREACHABLE();
52995299
}
53005300
}
53015301
}

0 commit comments

Comments
 (0)