From 42d101da8dff74111b409fb92bd2b0853067d857 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Sat, 16 Feb 2013 09:23:48 +0100 Subject: [PATCH] diagnostics: %empty enables -Wempty-rule * src/complain.h, src/complain.c (warning_is_unset): New. * src/reader.c (grammar_current_rule_empty_set): If enabled -Wempty-rule, if not disabled. * tests/actions.at (Implicitly empty rule): Check this feature. Also check that -Wno-empty-rule does disable this warning. --- src/complain.c | 9 +++++++++ src/complain.h | 3 +++ src/reader.c | 4 ++++ tests/actions.at | 19 +++++++++++++++++++ 4 files changed, 35 insertions(+) diff --git a/src/complain.c b/src/complain.c index cdc88030..44e1b1ea 100644 --- a/src/complain.c +++ b/src/complain.c @@ -196,6 +196,15 @@ warning_severity (warnings flags) } } +bool +warning_is_unset (warnings flags) +{ + size_t b; + for (b = 0; b < warnings_size; ++b) + if (flags & 1 << b && warnings_flag[b] != severity_unset) + return false; + return true; +} /** Display a "[-Wyacc]" like message on \a f. */ diff --git a/src/complain.h b/src/complain.h index 422c48f4..a4331848 100644 --- a/src/complain.h +++ b/src/complain.h @@ -101,6 +101,9 @@ typedef enum Wall = ~complaint & ~fatal & ~silent } warnings; +/** Whether the warnings of \a flags are all unset. + (Never enabled, never disabled). */ +bool warning_is_unset (warnings flags); /** Make a complaint, with maybe a location. */ void complain (location const *loc, warnings flags, char const *message, ...) diff --git a/src/reader.c b/src/reader.c index 0422d204..c83fc52a 100644 --- a/src/reader.c +++ b/src/reader.c @@ -451,6 +451,10 @@ grammar_current_rule_prec_set (symbol *precsym, location loc) void grammar_current_rule_empty_set (location loc) { + /* If %empty is used and -Wno-empty-rule is not, then enable + -Wempty-rule. */ + if (warning_is_unset (Wempty_rule)) + warning_argmatch ("empty-rule", 0, 0); if (current_rule->percent_empty_loc.start.file) complain (&loc, complaint, _("only one %s allowed per rule"), "%empty"); else diff --git a/tests/actions.at b/tests/actions.at index 1f6630a9..d91fc17f 100644 --- a/tests/actions.at +++ b/tests/actions.at @@ -84,6 +84,25 @@ AT_BISON_CHECK([-fcaret -Wempty-rule 1.y], [0], [], ^^ ]]) +AT_DATA_GRAMMAR([[2.y]], +[[%% +exp: a b c; +a: /* empty. */ {}; +b: %empty {}; +c: /* empty. */ {}; +]]) + +AT_BISON_CHECK([-fcaret 2.y], [0], [], +[[2.y:11.17-18: warning: empty rule without %empty [-Wempty-rule] + a: /* empty. */ {}; + ^^ +2.y:13.17-18: warning: empty rule without %empty [-Wempty-rule] + c: /* empty. */ {}; + ^^ +]]) + +AT_BISON_CHECK([-fcaret -Wno-empty-rule 2.y], [0]) + AT_CLEANUP ## ------------------------ ## -- 2.45.2