[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 5/8] diagnostics: display suggested update after the caret-info
From: |
Akim Demaille |
Subject: |
[PATCH 5/8] diagnostics: display suggested update after the caret-info |
Date: |
Wed, 2 Oct 2019 08:22:16 +0200 |
This commit adds the suggestion in green, on the line below the
caret-and-tildes.
foo.y:1.1-14: warning: deprecated directive: '%error-verbose', use '%define
parse.error verbose' [-Wdeprecated]
1 | %error-verbose
| ^~~~~~~~~~~~~~
| %define parse.error verbose
The current approach, with location_caret_suggestion, is fragile:
there's a protocol of calls to the complain functions which is strict.
We should rather have a richer structure describing the diagnostics,
including with submessages such as the suggestions, passed in the end
to the routines in charge of formatting and printing them.
* src/location.h, src/location.c (location_caret_suggestion): New.
* src/complain.c (deprecated_directive): Use it.
* tests/diagnostics.at, tests/input.at: Adjust expectations.
---
data/bison-default.css | 5 +++++
src/complain.c | 19 +++++++++----------
src/location.c | 13 +++++++++++++
src/location.h | 4 ++++
tests/diagnostics.at | 24 ++++++++++++++++++++++--
tests/input.at | 15 ++++++++++-----
6 files changed, 63 insertions(+), 17 deletions(-)
diff --git a/data/bison-default.css b/data/bison-default.css
index db9d87eb..41304a2b 100644
--- a/data/bison-default.css
+++ b/data/bison-default.css
@@ -17,9 +17,14 @@
/* This is an experimental feature. The class names may change in the
future. */
+/* Diagnostics. */
.warning { color: purple; }
.error { color: red; }
.note { color: cyan; }
+.insertion { color: green; }
+
/* Semantic values in Bison's own parser traces. */
.value { color: green; }
+
+/* "Sections" in traces (--trace). */
.trace0 { color: green; }
diff --git a/src/complain.c b/src/complain.c
index 65a34671..9f7b43b5 100644
--- a/src/complain.c
+++ b/src/complain.c
@@ -546,17 +546,16 @@ bison_directive (location const *loc, char const
*directive)
void
deprecated_directive (location const *loc, char const *old, char const *upd)
{
- if (feature_flag & feature_caret)
- complain (loc, Wdeprecated,
- _("deprecated directive, use %s"),
- quote_n (1, upd));
- else
- complain (loc, Wdeprecated,
- _("deprecated directive: %s, use %s"),
- quote (old), quote_n (1, upd));
- /* Register updates only if -Wdeprecated is enabled. */
if (warning_is_enabled (Wdeprecated))
- fixits_register (loc, upd);
+ {
+ complain (loc, Wdeprecated,
+ _("deprecated directive: %s, use %s"),
+ quote (old), quote_n (1, upd));
+ if (feature_flag & feature_caret)
+ location_caret_suggestion (*loc, upd, stderr);
+ /* Register updates only if -Wdeprecated is enabled. */
+ fixits_register (loc, upd);
+ }
}
void
diff --git a/src/location.c b/src/location.c
index 304d84ac..251ab029 100644
--- a/src/location.c
+++ b/src/location.c
@@ -470,6 +470,19 @@ location_caret (location loc, const char *style, FILE *out)
}
}
+void
+location_caret_suggestion (location loc, const char *s, FILE *out)
+{
+ const char *style = "insertion";
+ fprintf (out, " | %*s",
+ loc.start.column - 1 - caret_info.skip + (caret_info.skip ? 3 : 0),
+ "");
+ begin_use_class (style, out);
+ fputs (s, out);
+ end_use_class (style, out);
+ putc ('\n', out);
+}
+
bool
location_empty (location loc)
{
diff --git a/src/location.h b/src/location.h
index 64e80266..f0041c4e 100644
--- a/src/location.h
+++ b/src/location.h
@@ -125,6 +125,10 @@ void caret_free (void);
with the color STYLE. */
void location_caret (location loc, const char* style, FILE *out);
+/* Display a suggestion of replacement for LOC with S. To call after
+ location_caret. */
+void location_caret_suggestion (location loc, const char *s, FILE *out);
+
/* Return -1, 0, 1, depending whether a is before, equal, or
after b. */
static inline int
diff --git a/tests/diagnostics.at b/tests/diagnostics.at
index 03960d5f..4042a815 100644
--- a/tests/diagnostics.at
+++ b/tests/diagnostics.at
@@ -318,6 +318,7 @@ input.y: <warning>warning:</warning> fix-its can be
applied. Rerun with option
AT_TEST([[Screen width: 200 columns]],
[[%token ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ
+ %error-verbose
%%
exp: ABCDEFGHIJKLMNOPQRSTUVWXYZ
]],
@@ -340,11 +341,17 @@ input.y:9.92-117: <warning>warning:</warning> symbol
ABCDEFGHIJKLMNOPQRSTUVWXYZ
input.y:9.8-33: previous declaration
9 | %token <note>ABCDEFGHIJKLMNOPQRSTUVWXYZ</note>
ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
| <note>^~~~~~~~~~~~~~~~~~~~~~~~~~</note>
+input.y:10.56-69: <warning>warning:</warning> deprecated directive:
'%error-verbose', use '%define parse.error verbose'
[<warning>-Wdeprecated</warning>]
+ 10 |
<warning>%error-verbose</warning>
+ |
<warning>^~~~~~~~~~~~~~</warning>
+ |
<insertion>%define parse.error verbose</insertion>
+input.y: <warning>warning:</warning> fix-its can be applied. Rerun with
option '--update'. [<warning>-Wother</warning>]
]],
[[COLUMNS=200]])
AT_TEST([[Screen width: 80 columns]],
[[%token ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ
+ %error-verbose
%%
exp: ABCDEFGHIJKLMNOPQRSTUVWXYZ
]],
@@ -367,11 +374,17 @@ input.y:9.92-117: <warning>warning:</warning> symbol
ABCDEFGHIJKLMNOPQRSTUVWXYZ
input.y:9.8-33: previous declaration
9 | %token <note>ABCDEFGHIJKLMNOPQRSTUVWXYZ</note>
ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEF...
| <note>^~~~~~~~~~~~~~~~~~~~~~~~~~</note>
+input.y:10.56-69: <warning>warning:</warning> deprecated directive:
'%error-verbose', use '%define parse.error verbose'
[<warning>-Wdeprecated</warning>]
+ 10 |
<warning>%error-verbose</warning>
+ |
<warning>^~~~~~~~~~~~~~</warning>
+ |
<insertion>%define parse.error verbose</insertion>
+input.y: <warning>warning:</warning> fix-its can be applied. Rerun with
option '--update'. [<warning>-Wother</warning>]
]],
[[COLUMNS=80]])
AT_TEST([[Screen width: 60 columns]],
[[%token ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ
+ %error-verbose
%%
exp: ABCDEFGHIJKLMNOPQRSTUVWXYZ
]],
@@ -394,6 +407,11 @@ input.y:9.92-117: <warning>warning:</warning> symbol
ABCDEFGHIJKLMNOPQRSTUVWXYZ
input.y:9.8-33: previous declaration
9 | %token <note>ABCDEFGHIJKLMNOPQRSTUVWXYZ</note> ABCDEFGHIJKLMN...
| <note>^~~~~~~~~~~~~~~~~~~~~~~~~~</note>
+input.y:10.56-69: <warning>warning:</warning> deprecated directive:
'%error-verbose', use '%define parse.error verbose'
[<warning>-Wdeprecated</warning>]
+ 10 | ... <warning>%error-verbose</warning>
+ | <warning>^~~~~~~~~~~~~~</warning>
+ | <insertion>%define parse.error verbose</insertion>
+input.y: <warning>warning:</warning> fix-its can be applied. Rerun with
option '--update'. [<warning>-Wother</warning>]
]],
[[COLUMNS=60]])
@@ -419,12 +437,14 @@ exp : '0'
]])
AT_BISON_CHECK([[-fcaret -Wno-other input.y]], [0], [],
-[[input.y:2.1-12: warning: deprecated directive, use '%define api.pure'
[-Wdeprecated]
+[[input.y:2.1-12: warning: deprecated directive: '%pure-parser', use '%define
api.pure' [-Wdeprecated]
2 | %pure-parser
| ^~~~~~~~~~~~
-input.y:3.1-14: warning: deprecated directive, use '%define parse.error
verbose' [-Wdeprecated]
+ | %define api.pure
+input.y:3.1-14: warning: deprecated directive: '%error-verbose', use '%define
parse.error verbose' [-Wdeprecated]
3 | %error-verbose
| ^~~~~~~~~~~~~~
+ | %define parse.error verbose
]])
AT_CLEANUP
diff --git a/tests/input.at b/tests/input.at
index 8a265c42..7f9dbc1c 100644
--- a/tests/input.at
+++ b/tests/input.at
@@ -2049,21 +2049,26 @@ AT_DATA([[input.y]],
start: %empty;
]])
AT_BISON_CHECK([[-fcaret input.y]], [1], [],
-[[input.y:1.1-26: warning: deprecated directive, use '%define api.push-pull
both' [-Wdeprecated]
+[[input.y:1.1-26: warning: deprecated directive: '%define api.push_pull both',
use '%define api.push-pull both' [-Wdeprecated]
1 | %define api.push_pull both
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
-input.y:2.1-40: warning: deprecated directive, use '%define
lr.keep-unreachable-state maybe' [-Wdeprecated]
+ | %define api.push-pull both
+input.y:2.1-40: warning: deprecated directive: '%define
lr.keep_unreachable_states maybe', use '%define lr.keep-unreachable-state
maybe' [-Wdeprecated]
2 | %define lr.keep_unreachable_states maybe
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-input.y:3.1-23: warning: deprecated directive, use '%define api.namespace
{foo}' [-Wdeprecated]
+ | %define lr.keep-unreachable-state maybe
+input.y:3.1-23: warning: deprecated directive: '%define namespace "foo"', use
'%define api.namespace {foo}' [-Wdeprecated]
3 | %define namespace "foo"
| ^~~~~~~~~~~~~~~~~~~~~~~
-input.y:4.1-15: warning: deprecated directive, use '%define api.value.type
variant' [-Wdeprecated]
+ | %define api.namespace {foo}
+input.y:4.1-15: warning: deprecated directive: '%define variant', use '%define
api.value.type variant' [-Wdeprecated]
4 | %define variant
| ^~~~~~~~~~~~~~~
-input.y:5.1-34: warning: deprecated directive, use '%define api.parser.class
{parser}' [-Wdeprecated]
+ | %define api.value.type variant
+input.y:5.1-34: warning: deprecated directive: '%define parser_class_name
{parser}', use '%define api.parser.class {parser}' [-Wdeprecated]
5 | %define parser_class_name {parser}
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ | %define api.parser.class {parser}
input.y:2.1-40: error: invalid value for %define Boolean variable
'lr.keep-unreachable-state'
2 | %define lr.keep_unreachable_states maybe
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--
2.23.0
- [PATCH 0/8] RFC: diagnostics: add suggestions, Akim Demaille, 2019/10/02
- [PATCH 1/8] TODO: update, Akim Demaille, 2019/10/02
- [PATCH 2/8] tests: use tput to get the number of columns, Akim Demaille, 2019/10/02
- [PATCH 3/8] diagnostics: isolate caret_set_file, Akim Demaille, 2019/10/02
- [PATCH 4/8] diagnostics: isolate caret_set_column, Akim Demaille, 2019/10/02
- [PATCH 5/8] diagnostics: display suggested update after the caret-info,
Akim Demaille <=
- [PATCH 7/8] style: isolate complain_symbol_undeclared, Akim Demaille, 2019/10/02
- [PATCH 6/8] style: simplify the handling of symbol and semantic_type tables, Akim Demaille, 2019/10/02
- [PATCH 8/8] diagnostics: suggest fixes for undeclared symbols, Akim Demaille, 2019/10/02