diagnostics: improve them for %name-prefix

Currently the diagnostics for %name-prefix are not precise enough.  In
particular, they does not show that braces must be used instead of
quotes.

Before:

    foo.y:3.1-14: warning: deprecated directive, use '%define api.prefix' [-Wdeprecated]
     %name-prefix = "foo"
     ^^^^^^^^^^^^^^

After:

    foo.y:3.1-20: warning: deprecated directive, use '%define api.prefix {foo}' [-Wdeprecated]
     %name-prefix = "foo"
     ^^^^^^^^^^^^^^^^^^^^

To do this we need the value passed to %name-prefix, so move the
warning from the scanner to the parser.

Accuracy will be very important for the forthcoming changes.

* src/parse-gram.y (do_name_prefix): New.
(PERCENT_NAME_PREFIX): Have a semantic value: the raw source, with
possibly underscores, equal sign, and spaces.  This is used to provide
a more accurate message.  It does not take comments into account,
but...
* src/scan-gram.l (%name-prefix): Delegate the warnings to the parser.

* tests/headers.at, tests/input.at: Adjust expectations.
This commit is contained in:
Akim Demaille
2019-01-12 17:00:49 +01:00
parent a88f8117b0
commit 72c488aa62
4 changed files with 41 additions and 11 deletions

View File

@@ -43,8 +43,9 @@
#include "named-ref.h"
#include "quotearg.h"
#include "reader.h"
#include "scan-gram.h"
#include "scan-code.h"
#include "scan-gram.h"
#include "vasnprintf.h"
#include "xmemdup0.h"
static int current_prec = 0;
@@ -81,6 +82,10 @@
string from the scanner (should be CODE). */
static char const *translate_code_braceless (char *code, location loc);
/* Handle a %name-prefix directive. */
static void do_name_prefix (location const *loc,
char const *directive, char const *value);
/* Handle a %require directive. */
static void do_require (location const *loc, char const *version);
@@ -190,7 +195,9 @@
%printer { fputs (quotearg_style (c_quoting_style, $$), yyo); } STRING
%printer { fprintf (yyo, "{\n%s\n}", $$); } <char*>
%type <uniqstr> BRACKETED_ID ID ID_COLON PERCENT_FLAG TAG tag tag.opt variable
%type <uniqstr>
BRACKETED_ID ID ID_COLON PERCENT_FLAG PERCENT_NAME_PREFIX TAG
tag tag.opt variable
%printer { fputs ($$, yyo); } <uniqstr>
%printer { fprintf (yyo, "[%s]", $$); } BRACKETED_ID
%printer { fprintf (yyo, "%s:", $$); } ID_COLON
@@ -307,7 +314,7 @@ prologue_declaration:
code_scanner_last_string_free ();
}
| "%language" STRING { language_argmatch ($2, grammar_prio, @1); }
| "%name-prefix" STRING { spec_name_prefix = $2; }
| "%name-prefix" STRING { do_name_prefix (&@$, $1, $2); }
| "%no-lines" { no_lines_flag = true; }
| "%nondeterministic-parser" { nondeterministic_parser = true; }
| "%output" STRING { spec_outfile = $2; }
@@ -846,6 +853,31 @@ add_param (param_type type, char *decl, location loc)
}
static void
do_name_prefix (location const *loc,
char const *directive, char const *value)
{
spec_name_prefix = value;
char buf1[1024];
size_t len1 = sizeof (buf1);
char *old = asnprintf (buf1, &len1, "%s\"%s\"", directive, value);
if (!old)
xalloc_die ();
char buf2[1024];
size_t len2 = sizeof (buf2);
char *new = asnprintf (buf2, &len2, "%%define api.prefix {%s}", value);
if (!new)
xalloc_die ();
bison_directive (loc, old);
deprecated_directive (loc, old, new);
if (old != buf1)
free (old);
if (new != buf2)
free (new);
}
static void
do_require (location const *loc, char const *version)
{

View File

@@ -144,7 +144,8 @@ splice (\\[ \f\t\v]*\n)*
/* An equal sign, with optional leading whitespaces. This is used in some
deprecated constructs. */
eqopt ([[:space:]]*=)?
sp [[:space:]]*
eqopt ({sp}=)?
%%
%{
@@ -266,10 +267,7 @@ eqopt ([[:space:]]*=)?
/* Deprecated since Bison 2.6 (2012-07-19), but the warning is
issued only since Bison 3.3. */
"%name"[-_]"prefix"{eqopt} {
deprecated_directive (loc, yytext, "%define api.prefix");
return BISON_DIRECTIVE (NAME_PREFIX);
}
"%name"[-_]"prefix"{eqopt}{sp} RETURN_VALUE (PERCENT_NAME_PREFIX, uniqstr_new (yytext));
/* Deprecated since Bison 2.7.90, 2012. */
"%default"[-_]"prec" DEPRECATED ("%default-prec");

View File

@@ -85,7 +85,7 @@ exp: %empty;
]])
AT_BISON_CHECK([--defines -o input.c input.y], [], [],
[[input.y:11.1-12: warning: deprecated directive: '%name-prefix', use '%define api.prefix' [-Wdeprecated]
[[input.y:11.1-18: warning: deprecated directive: '%name-prefix "my_"', use '%define api.prefix {my_}' [-Wdeprecated]
]])
# YYLTYPE should be defined, and MY_LLOC declared.

View File

@@ -2514,7 +2514,7 @@ input.y:13.1-14: warning: deprecated directive: '%file-prefix =', use '%file-pre
input.y:14.1-15.2: warning: deprecated directive: '%file-prefix\n =', use '%file-prefix' [-Wdeprecated]
input.y:17.1-19: warning: deprecated directive: '%fixed-output_files', use '%fixed-output-files' [-Wdeprecated]
input.y:18.1-19: warning: deprecated directive: '%fixed_output-files', use '%fixed-output-files' [-Wdeprecated]
input.y:20.1-13: warning: deprecated directive: '%name-prefix=', use '%define api.prefix' [-Wdeprecated]
input.y:20.1-19: warning: deprecated directive: '%name-prefix= "foo"', use '%define api.prefix {foo}' [-Wdeprecated]
input.y:21.1-16: warning: deprecated directive: '%no-default_prec', use '%no-default-prec' [-Wdeprecated]
input.y:22.1-16: warning: deprecated directive: '%no_default-prec', use '%no-default-prec' [-Wdeprecated]
input.y:23.1-9: warning: deprecated directive: '%no_lines', use '%no-lines' [-Wdeprecated]
@@ -2524,7 +2524,7 @@ input.y:26.1-12: warning: deprecated directive: '%token_table', use '%token-tabl
input.y:27.1-14: warning: deprecated directive: '%error-verbose', use '%define parse.error verbose' [-Wdeprecated]
input.y:27-6: error: %define variable 'parse.error' redefined
input.y:11-6: previous definition
input.y:29.1-12: warning: deprecated directive: '%name-prefix', use '%define api.prefix' [-Wdeprecated]
input.y:29.1-18: warning: deprecated directive: '%name-prefix "bar"', use '%define api.prefix {bar}' [-Wdeprecated]
]])
AT_CLEANUP