Skip to content

Commit 37d0f7d

Browse files
akimdkocsismate
authored andcommitted
Use "%empty" in the parsers, instead of comments
The annotation %empty is properly enforced: warnings when it's missing, and errors when it's inappropriate. Support for %empty was introduced in Bison 3.0. Pass -Wempty-rule to Bison. Closes GH-5134
1 parent b915d68 commit 37d0f7d

File tree

8 files changed

+42
-39
lines changed

8 files changed

+42
-39
lines changed

Zend/Makefile.frag

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ $(srcdir)/zend_language_parser.c: $(srcdir)/zend_language_parser.y
1313
# Tweak zendparse to be exported through ZEND_API. This has to be revisited once
1414
# bison supports foreign skeletons and that bison version is used. Read
1515
# https://git.savannah.gnu.org/cgit/bison.git/tree/data/README.md for more.
16-
@$(YACC) -p zend -v -d $(srcdir)/zend_language_parser.y -o $@
16+
@$(YACC) $(YFLAGS) -p zend -v -d $(srcdir)/zend_language_parser.y -o $@
1717
@$(SED) -e 's,^int zendparse\(.*\),ZEND_API int zendparse\1,g' < $@ \
1818
> $@.tmp && \
1919
mv $@.tmp $@
@@ -27,7 +27,7 @@ $(srcdir)/zend_language_parser.c: $(srcdir)/zend_language_parser.y
2727

2828
$(srcdir)/zend_ini_parser.h: $(srcdir)/zend_ini_parser.c
2929
$(srcdir)/zend_ini_parser.c: $(srcdir)/zend_ini_parser.y
30-
@$(YACC) -p ini_ -v -d $(srcdir)/zend_ini_parser.y -o $@
30+
@$(YACC) $(YFLAGS) -p ini_ -v -d $(srcdir)/zend_ini_parser.y -o $@
3131

3232
$(srcdir)/zend_ini_scanner.c: $(srcdir)/zend_ini_scanner.l
3333
@(cd $(top_srcdir); $(RE2C) $(RE2C_FLAGS) --no-generation-date --case-inverted -cbdFt Zend/zend_ini_scanner_defs.h -oZend/zend_ini_scanner.c Zend/zend_ini_scanner.l)

Zend/zend_ini_parser.y

+4-4
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ static void zval_ini_dtor(zval *zv)
317317
318318
statement_list:
319319
statement_list statement
320-
| /* empty */
320+
| %empty
321321
;
322322
323323
statement:
@@ -351,7 +351,7 @@ statement:
351351
352352
section_string_or_value:
353353
var_string_list_section { $$ = $1; }
354-
| /* empty */ { zend_ini_init_string(&$$); }
354+
| %empty { zend_ini_init_string(&$$); }
355355
;
356356
357357
string_or_value:
@@ -364,13 +364,13 @@ string_or_value:
364364
365365
option_offset:
366366
var_string_list { $$ = $1; }
367-
| /* empty */ { zend_ini_init_string(&$$); }
367+
| %empty { zend_ini_init_string(&$$); }
368368
;
369369
370370
encapsed_list:
371371
encapsed_list cfg_var_ref { zend_ini_add_string(&$$, &$1, &$2); zend_string_free(Z_STR($2)); }
372372
| encapsed_list TC_QUOTED_STRING { zend_ini_add_string(&$$, &$1, &$2); zend_string_free(Z_STR($2)); }
373-
| /* empty */ { zend_ini_init_string(&$$); }
373+
| %empty { zend_ini_init_string(&$$); }
374374
;
375375
376376
var_string_list_section:

Zend/zend_language_parser.y

+27-27
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ identifier:
297297

298298
top_statement_list:
299299
top_statement_list top_statement { $$ = zend_ast_list_add($1, $2); }
300-
| /* empty */ { $$ = zend_ast_create_list(0, ZEND_AST_STMT_LIST); }
300+
| %empty { $$ = zend_ast_create_list(0, ZEND_AST_STMT_LIST); }
301301
;
302302

303303
namespace_name:
@@ -357,7 +357,7 @@ mixed_group_use_declaration:
357357
;
358358

359359
possible_comma:
360-
/* empty */
360+
%empty
361361
| ','
362362
;
363363

@@ -407,7 +407,7 @@ const_list:
407407
inner_statement_list:
408408
inner_statement_list inner_statement
409409
{ $$ = zend_ast_list_add($1, $2); }
410-
| /* empty */
410+
| %empty
411411
{ $$ = zend_ast_create_list(0, ZEND_AST_STMT_LIST); }
412412
;
413413

@@ -463,7 +463,7 @@ statement:
463463
;
464464

465465
catch_list:
466-
/* empty */
466+
%empty
467467
{ $$ = zend_ast_create_list(0, ZEND_AST_CATCH_LIST); }
468468
| catch_list T_CATCH '(' catch_name_list T_VARIABLE ')' '{' inner_statement_list '}'
469469
{ $$ = zend_ast_list_add($1, zend_ast_create(ZEND_AST_CATCH, $4, $5, $8)); }
@@ -475,7 +475,7 @@ catch_name_list:
475475
;
476476

477477
finally_statement:
478-
/* empty */ { $$ = NULL; }
478+
%empty { $$ = NULL; }
479479
| T_FINALLY '{' inner_statement_list '}' { $$ = $3; }
480480
;
481481

@@ -496,12 +496,12 @@ function_declaration_statement:
496496
;
497497

498498
is_reference:
499-
/* empty */ { $$ = 0; }
499+
%empty { $$ = 0; }
500500
| '&' { $$ = ZEND_PARAM_REF; }
501501
;
502502

503503
is_variadic:
504-
/* empty */ { $$ = 0; }
504+
%empty { $$ = 0; }
505505
| T_ELLIPSIS { $$ = ZEND_PARAM_VARIADIC; }
506506
;
507507

@@ -538,17 +538,17 @@ interface_declaration_statement:
538538
;
539539

540540
extends_from:
541-
/* empty */ { $$ = NULL; }
541+
%empty { $$ = NULL; }
542542
| T_EXTENDS class_name { $$ = $2; }
543543
;
544544

545545
interface_extends_list:
546-
/* empty */ { $$ = NULL; }
546+
%empty { $$ = NULL; }
547547
| T_EXTENDS class_name_list { $$ = $2; }
548548
;
549549

550550
implements_list:
551-
/* empty */ { $$ = NULL; }
551+
%empty { $$ = NULL; }
552552
| T_IMPLEMENTS class_name_list { $$ = $2; }
553553
;
554554

@@ -582,7 +582,7 @@ switch_case_list:
582582
;
583583

584584
case_list:
585-
/* empty */ { $$ = zend_ast_create_list(0, ZEND_AST_SWITCH_LIST); }
585+
%empty { $$ = zend_ast_create_list(0, ZEND_AST_SWITCH_LIST); }
586586
| case_list T_CASE expr case_separator inner_statement_list
587587
{ $$ = zend_ast_list_add($1, zend_ast_create(ZEND_AST_SWITCH_CASE, $3, $5)); }
588588
| case_list T_DEFAULT case_separator inner_statement_list
@@ -634,7 +634,7 @@ alt_if_stmt:
634634

635635
parameter_list:
636636
non_empty_parameter_list { $$ = $1; }
637-
| /* empty */ { $$ = zend_ast_create_list(0, ZEND_AST_PARAM_LIST); }
637+
| %empty { $$ = zend_ast_create_list(0, ZEND_AST_PARAM_LIST); }
638638
;
639639

640640

@@ -654,7 +654,7 @@ parameter:
654654

655655

656656
optional_type:
657-
/* empty */ { $$ = NULL; }
657+
%empty { $$ = NULL; }
658658
| type_expr { $$ = $1; }
659659
;
660660

@@ -676,7 +676,7 @@ union_type:
676676
;
677677

678678
return_type:
679-
/* empty */ { $$ = NULL; }
679+
%empty { $$ = NULL; }
680680
| ':' type_expr { $$ = $2; }
681681
;
682682

@@ -722,7 +722,7 @@ static_var:
722722
class_statement_list:
723723
class_statement_list class_statement
724724
{ $$ = zend_ast_list_add($1, $2); }
725-
| /* empty */
725+
| %empty
726726
{ $$ = zend_ast_create_list(0, ZEND_AST_STMT_LIST); }
727727
;
728728

@@ -802,7 +802,7 @@ variable_modifiers:
802802
;
803803

804804
method_modifiers:
805-
/* empty */ { $$ = ZEND_ACC_PUBLIC; }
805+
%empty { $$ = ZEND_ACC_PUBLIC; }
806806
| non_empty_member_modifiers
807807
{ $$ = $1; if (!($$ & ZEND_ACC_PPP_MASK)) { $$ |= ZEND_ACC_PUBLIC; } }
808808
;
@@ -856,7 +856,7 @@ echo_expr:
856856
;
857857

858858
for_exprs:
859-
/* empty */ { $$ = NULL; }
859+
%empty { $$ = NULL; }
860860
| non_empty_for_exprs { $$ = $1; }
861861
;
862862

@@ -1026,24 +1026,24 @@ function:
10261026
;
10271027

10281028
backup_doc_comment:
1029-
/* empty */ { $$ = CG(doc_comment); CG(doc_comment) = NULL; }
1029+
%empty { $$ = CG(doc_comment); CG(doc_comment) = NULL; }
10301030
;
10311031

10321032
backup_fn_flags:
1033-
%prec PREC_ARROW_FUNCTION /* empty */ { $$ = CG(extra_fn_flags); CG(extra_fn_flags) = 0; }
1033+
%prec PREC_ARROW_FUNCTION %empty { $$ = CG(extra_fn_flags); CG(extra_fn_flags) = 0; }
10341034
;
10351035

10361036
backup_lex_pos:
1037-
/* empty */ { $$ = LANG_SCNG(yy_text); }
1037+
%empty { $$ = LANG_SCNG(yy_text); }
10381038
;
10391039

10401040
returns_ref:
1041-
/* empty */ { $$ = 0; }
1041+
%empty { $$ = 0; }
10421042
| '&' { $$ = ZEND_ACC_RETURN_REFERENCE; }
10431043
;
10441044

10451045
lexical_vars:
1046-
/* empty */ { $$ = NULL; }
1046+
%empty { $$ = NULL; }
10471047
| T_USE '(' lexical_var_list ')' { $$ = $3; }
10481048
;
10491049

@@ -1081,20 +1081,20 @@ class_name_reference:
10811081
;
10821082

10831083
exit_expr:
1084-
/* empty */ { $$ = NULL; }
1084+
%empty { $$ = NULL; }
10851085
| '(' optional_expr ')' { $$ = $2; }
10861086
;
10871087

10881088
backticks_expr:
1089-
/* empty */
1089+
%empty
10901090
{ $$ = zend_ast_create_zval_from_str(ZSTR_EMPTY_ALLOC()); }
10911091
| T_ENCAPSED_AND_WHITESPACE { $$ = $1; }
10921092
| encaps_list { $$ = $1; }
10931093
;
10941094

10951095

10961096
ctor_arguments:
1097-
/* empty */ { $$ = zend_ast_create_list(0, ZEND_AST_ARG_LIST); }
1097+
%empty { $$ = zend_ast_create_list(0, ZEND_AST_ARG_LIST); }
10981098
| argument_list { $$ = $1; }
10991099
;
11001100

@@ -1134,7 +1134,7 @@ constant:
11341134
;
11351135

11361136
optional_expr:
1137-
/* empty */ { $$ = NULL; }
1137+
%empty { $$ = NULL; }
11381138
| expr { $$ = $1; }
11391139
;
11401140

@@ -1223,7 +1223,7 @@ array_pair_list:
12231223
;
12241224

12251225
possible_array_pair:
1226-
/* empty */ { $$ = NULL; }
1226+
%empty { $$ = NULL; }
12271227
| array_pair { $$ = $1; }
12281228
;
12291229

build/php.m4

+1
Original file line numberDiff line numberDiff line change
@@ -1815,6 +1815,7 @@ AC_DEFUN([PHP_PROG_BISON], [
18151815
done
18161816
18171817
if test "$php_bison_check" != "invalid"; then
1818+
AC_SUBST([YFLAGS], [-Wempty-rule])
18181819
AC_MSG_RESULT([$php_bison_version (ok)])
18191820
else
18201821
AC_MSG_RESULT([$php_bison_version])

ext/json/Makefile.frag

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ $(srcdir)/json_scanner.c: $(srcdir)/json_scanner.re
22
@$(RE2C) $(RE2C_FLAGS) -t $(srcdir)/php_json_scanner_defs.h --no-generation-date -bci -o $@ $(srcdir)/json_scanner.re
33

44
$(srcdir)/json_parser.tab.c: $(srcdir)/json_parser.y
5-
@$(YACC) --defines -l $(srcdir)/json_parser.y -o $@
5+
@$(YACC) $(YFLAGS) --defines -l $(srcdir)/json_parser.y -o $@

ext/json/json_parser.y

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
%require "3.0"
12
%code top {
23
/*
34
+----------------------------------------------------------------------+
@@ -118,7 +119,7 @@ object_end:
118119
;
119120

120121
members:
121-
/* empty */
122+
%empty
122123
{
123124
if ((parser->scanner.options & PHP_JSON_OBJECT_AS_ARRAY) && parser->methods.object_create == php_json_parser_object_create) {
124125
ZVAL_EMPTY_ARRAY(&$$);
@@ -182,7 +183,7 @@ array_end:
182183
;
183184

184185
elements:
185-
/* empty */
186+
%empty
186187
{
187188
if (parser->methods.array_create == php_json_parser_array_create) {
188189
ZVAL_EMPTY_ARRAY(&$$);

sapi/phpdbg/Makefile.frag

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ $(srcdir)/phpdbg_lexer.c: $(srcdir)/phpdbg_lexer.l
1818

1919
$(srcdir)/phpdbg_parser.h: $(srcdir)/phpdbg_parser.c
2020
$(srcdir)/phpdbg_parser.c: $(srcdir)/phpdbg_parser.y
21-
@$(YACC) -p phpdbg_ -v -d $(srcdir)/phpdbg_parser.y -o $@
21+
@$(YACC) $(YFLAGS) -p phpdbg_ -v -d $(srcdir)/phpdbg_parser.y -o $@
2222

2323
install-phpdbg: $(BUILD_BINARY)
2424
@echo "Installing phpdbg binary: $(INSTALL_ROOT)$(bindir)/"

sapi/phpdbg/phpdbg_parser.y

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
%require "3.0"
12
%{
23

34
/*
@@ -65,7 +66,7 @@ typedef void* yyscan_t;
6566
input
6667
: command { $$ = $1; }
6768
| input T_SEPARATOR command { phpdbg_stack_separate($1.top); $$ = $3; }
68-
| /* empty */
69+
| %empty
6970
;
7071

7172
command
@@ -143,7 +144,7 @@ parameter
143144

144145
req_id
145146
: T_REQ_ID { PHPDBG_G(req_id) = $1.num; }
146-
| /* empty */
147+
| %empty
147148
;
148149

149150
full_expression

0 commit comments

Comments
 (0)