[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 02/12] diagnostics: improve them for %name-prefix
From: |
Akim Demaille |
Subject: |
[PATCH 02/12] diagnostics: improve them for %name-prefix |
Date: |
Sun, 13 Jan 2019 15:23:58 +0100 |
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.
---
src/parse-gram.y | 38 +++++++++++++++++++++++++++++++++++---
src/scan-gram.l | 8 +++-----
tests/headers.at | 2 +-
tests/input.at | 4 ++--
4 files changed, 41 insertions(+), 11 deletions(-)
diff --git a/src/parse-gram.y b/src/parse-gram.y
index b0f89eb0..fe0afce7 100644
--- a/src/parse-gram.y
+++ b/src/parse-gram.y
@@ -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)
{
diff --git a/src/scan-gram.l b/src/scan-gram.l
index 571962e9..86097b07 100644
--- a/src/scan-gram.l
+++ b/src/scan-gram.l
@@ -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");
diff --git a/tests/headers.at b/tests/headers.at
index bdca60d5..32c8d856 100644
--- a/tests/headers.at
+++ b/tests/headers.at
@@ -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.
diff --git a/tests/input.at b/tests/input.at
index 7fdba82f..5d7ea7f6 100644
--- a/tests/input.at
+++ b/tests/input.at
@@ -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
--
2.20.1
- [PATCH 00/12] RFC: fixits and --update, Akim Demaille, 2019/01/13
- [PATCH 12/12] diagnostics: add fixit support for duplicate removal, Akim Demaille, 2019/01/13
- [PATCH 01/12] diagnostics: style: avoid allocating memory when not needed, Akim Demaille, 2019/01/13
- [PATCH 02/12] diagnostics: improve them for %name-prefix,
Akim Demaille <=
- [PATCH 09/12] diagnostics: avoid duplicate warnings for deprecated directives, Akim Demaille, 2019/01/13
- [PATCH 10/12] diagnostics: improve the accuracy for %error-verbose, Akim Demaille, 2019/01/13
- [PATCH 06/12] diagnostics: keep the fixits, Akim Demaille, 2019/01/13
- [PATCH 04/12] diagnostics: prefer ^~~~ to ^^^^ to underline code, Akim Demaille, 2019/01/13
- [PATCH 05/12] diagnostics: add -ffixit support for deprecated features, Akim Demaille, 2019/01/13
- [PATCH 08/12] diagnostics: update the grammar file, Akim Demaille, 2019/01/13
- [PATCH 07/12] diagnostics: improve accuracy for deprecated %define variables, Akim Demaille, 2019/01/13