diagnostics: modernize the display of submessages

Since Bison 2.7, output was indented four spaces for explanatory
statements.  For example:

    input.y:2.7-13: error: %type redeclaration for exp
    input.y:1.7-11:     previous declaration

Since the introduction of caret-diagnostics, it became less clear.
Remove the indentation and display submessages as in GCC:

    input.y:2.7-13: error: %type redeclaration for exp
        2 | %type <float> exp
          |       ^~~~~~~
    input.y:1.7-11: note: previous declaration
        1 | %type <int> exp
          |       ^~~~~

* src/complain.h (SUB_INDENT): Remove.
(warnings): Add "note" to the enum.
* src/complain.h, src/complain.c (complain_indent): Replace by...
(subcomplain): this.
Adjust all dependencies.
* tests/actions.at, tests/diagnostics.at, tests/glr-regression.at,
* tests/input.at, tests/named-refs.at, tests/regression.at:
Adjust expectations.
This commit is contained in:
Victor Morales Cayuela
2020-02-14 18:41:55 +01:00
committed by Akim Demaille
parent a09d0ae4d1
commit e09a72eeb0
16 changed files with 284 additions and 364 deletions

View File

@@ -424,7 +424,6 @@ warnings_print_categories (warnings warn_flags, FILE *out)
*
* \param loc the location, defaulting to the current file,
* or the program name.
* \param indent optional indentation for the error message.
* \param flags the category for this message.
* \param sever to decide the prefix to put before the message
* (e.g., "warning").
@@ -436,51 +435,46 @@ warnings_print_categories (warnings warn_flags, FILE *out)
*/
static
void
error_message (const location *loc, int *indent, warnings flags,
error_message (const location *loc, warnings flags,
severity sever, const char *message, va_list args)
{
int pos = 0;
const char* style = flags & note ? "note" : severity_style (sever);
if (loc)
pos += location_print (*loc, stderr);
location_print (*loc, stderr);
else
pos += fprintf (stderr, "%s", grammar_file ? grammar_file : program_name);
pos += fprintf (stderr, ": ");
fprintf (stderr, "%s", grammar_file ? grammar_file : program_name);
fprintf (stderr, ": ");
if (indent)
if (sever != severity_disabled)
{
if (*indent)
sever = severity_disabled;
if (!*indent)
*indent = pos;
else if (*indent > pos)
fprintf (stderr, "%*s", *indent - pos, "");
begin_use_class (style, stderr);
fprintf (stderr, "%s:", flags & note ? _("note") : severity_prefix (sever));
end_use_class (style, stderr);
fputc (' ', stderr);
}
severity_print (sever, stderr);
vfprintf (stderr, message, args);
/* Print the type of warning, only if this is not a sub message
(in which case the prefix is null). */
if (! (flags & silent) && sever != severity_disabled)
warnings_print_categories (flags, stderr);
{
size_t l = strlen (message);
if (l < 2 || message[l - 2] != ':' || message[l - 1] != ' ')
{
putc ('\n', stderr);
flush (stderr);
if (loc && !(flags & no_caret))
location_caret (*loc, severity_style (sever), stderr);
}
}
size_t l = strlen (message);
if (l < 2 || message[l - 2] != ':' || message[l - 1] != ' ')
{
putc ('\n', stderr);
flush (stderr);
if (loc && !(flags & no_caret))
location_caret (*loc, style, stderr);
}
flush (stderr);
}
/** Raise a complaint (fatal error, error or just warning). */
static void
complains (const location *loc, int *indent, warnings flags,
complains (const location *loc, warnings flags,
const char *message, va_list args)
{
if ((flags & complaint) && complaint_status < status_complaint)
@@ -491,7 +485,7 @@ complains (const location *loc, int *indent, warnings flags,
{
if (severity_error <= s && ! complaint_status)
complaint_status = status_warning_as_error;
error_message (loc, indent, flags, s, message, args);
error_message (loc, flags, s, message, args);
}
if (flags & fatal)
@@ -503,41 +497,39 @@ complain (location const *loc, warnings flags, const char *message, ...)
{
va_list args;
va_start (args, message);
complains (loc, NULL, flags, message, args);
complains (loc, flags, message, args);
va_end (args);
}
void
complain_indent (location const *loc, warnings flags, int *indent,
const char *message, ...)
subcomplain (location const *loc, warnings flags, const char *message, ...)
{
va_list args;
va_start (args, message);
complains (loc, indent, flags, message, args);
complains (loc, flags | note | silent, message, args);
va_end (args);
}
void
complain_args (location const *loc, warnings w, int *indent,
complain_args (location const *loc, warnings w,
int argc, char *argv[])
{
switch (argc)
{
case 1:
complain_indent (loc, w, indent, "%s", _(argv[0]));
complain (loc, w, "%s", _(argv[0]));
break;
case 2:
complain_indent (loc, w, indent, _(argv[0]), argv[1]);
complain (loc, w, _(argv[0]), argv[1]);
break;
case 3:
complain_indent (loc, w, indent, _(argv[0]), argv[1], argv[2]);
complain (loc, w, _(argv[0]), argv[1], argv[2]);
break;
case 4:
complain_indent (loc, w, indent, _(argv[0]), argv[1], argv[2], argv[3]);
complain (loc, w, _(argv[0]), argv[1], argv[2], argv[3]);
break;
case 5:
complain_indent (loc, w, indent, _(argv[0]), argv[1], argv[2], argv[3],
argv[4]);
complain (loc, w, _(argv[0]), argv[1], argv[2], argv[3], argv[4]);
break;
default:
complain (loc, fatal, "too many arguments for complains");
@@ -571,13 +563,11 @@ void
duplicate_directive (char const *directive,
location first, location second)
{
int i = 0;
if (feature_flag & feature_caret)
complain_indent (&second, Wother, &i, _("duplicate directive"));
complain (&second, Wother, _("duplicate directive"));
else
complain_indent (&second, Wother, &i, _("duplicate directive: %s"), quote (directive));
i += SUB_INDENT;
complain_indent (&first, Wother, &i, _("previous declaration"));
complain (&second, Wother, _("duplicate directive: %s"), quote (directive));
subcomplain (&first, Wother, _("previous declaration"));
fixits_register (&second, "");
}
@@ -585,12 +575,8 @@ void
duplicate_rule_directive (char const *directive,
location first, location second)
{
int i = 0;
complain_indent (&second, complaint, &i,
_("only one %s allowed per rule"), directive);
i += SUB_INDENT;
complain_indent (&first, complaint, &i,
_("previous declaration"));
complain (&second, complaint, _("only one %s allowed per rule"), directive);
subcomplain (&first, complaint, _("previous declaration"));
fixits_register (&second, "");
}

View File

@@ -21,9 +21,6 @@
# include "location.h"
/* Sub-messages indent. */
# define SUB_INDENT (4)
/*---------------.
| Error stream. |
`---------------*/
@@ -119,6 +116,7 @@ typedef enum
fatal = 1 << 12, /**< All fatal errors. */
silent = 1 << 13, /**< Do not display the warning type. */
no_caret = 1 << 14, /**< Do not display caret location. */
note = 1 << 15, /**< Display as a note. */
/**< All above warnings. */
Weverything = ~complaint & ~fatal & ~silent,
@@ -137,13 +135,13 @@ void complain (location const *loc, warnings flags, char const *message, ...)
__attribute__ ((__format__ (__printf__, 3, 4)));
/** Likewise, but with an \a argc/argv interface. */
void complain_args (location const *loc, warnings w, int *indent,
void complain_args (location const *loc, warnings w,
int argc, char *arg[]);
/** Make a complaint with location and some indentation. */
void complain_indent (location const *loc, warnings flags, int *indent,
char const *message, ...)
__attribute__ ((__format__ (__printf__, 4, 5)));
/** Make a subcomplain with location and note. */
void subcomplain (location const *loc, warnings flags,
char const *message, ...)
__attribute__ ((__format__ (__printf__, 3, 4)));
/** GNU Bison extension not valid with POSIX Yacc. */

View File

@@ -525,15 +525,13 @@ muscle_percent_define_insert (char const *var, location variable_loc,
= atoi (muscle_find_const (how_name));
if (how_old == MUSCLE_PERCENT_DEFINE_F)
goto end;
int i = 0;
/* If assigning the same value, make it a warning. */
warnings warn = STREQ (value, current_value) ? Wother : complaint;
complain_indent (&variable_loc, warn, &i,
_("%%define variable %s redefined"),
quote (variable));
i += SUB_INDENT;
complain (&variable_loc, warn,
_("%%define variable %s redefined"),
quote (variable));
location loc = muscle_percent_define_get_loc (variable);
complain_indent (&loc, warn, &i, _("previous definition"));
subcomplain (&loc, warn, _("previous definition"));
fixits_register (&variable_loc, "");
warned = true;
}
@@ -739,14 +737,12 @@ muscle_percent_define_check_values (char const * const *values)
if (!*values)
{
location loc = muscle_percent_define_get_loc (*variablep);
int i = 0;
complain_indent (&loc, complaint, &i,
_("invalid value for %%define variable %s: %s"),
quote (*variablep), quote_n (1, value));
i += SUB_INDENT;
complain (&loc, complaint,
_("invalid value for %%define variable %s: %s"),
quote (*variablep), quote_n (1, value));
for (values = variablep + 1; *values; ++values)
complain_indent (&loc, complaint | no_caret | silent, &i,
_("accepted value: %s"), quote (*values));
subcomplain (&loc, complaint | no_caret | silent,
_("accepted value: %s"), quote (*values));
}
else
while (*values)

View File

@@ -756,14 +756,11 @@ id:
}
if (muscle_percent_define_ifdef (var))
{
int indent = 0;
complain_indent (&@1, complaint, &indent,
_("character literals cannot be used together"
" with %s"), var);
indent += SUB_INDENT;
complain (&@1, complaint,
_("character literals cannot be used together"
" with %s"), var);
location loc = muscle_percent_define_get_loc (var);
complain_indent (&loc, complaint, &indent,
_("definition of %s"), var);
subcomplain (&loc, complaint, _("definition of %s"), var);
}
$$ = symbol_get (char_name ($1), @1);
symbol_class_set ($$, token_sym, @1, false);

View File

@@ -124,16 +124,13 @@ record_merge_function_type (int merger, uniqstr type, location declaration_loc)
aver (merge_function != NULL && merger_find == merger);
if (merge_function->type != NULL && !UNIQSTR_EQ (merge_function->type, type))
{
int indent = 0;
complain_indent (&declaration_loc, complaint, &indent,
_("result type clash on merge function %s: "
"<%s> != <%s>"),
quote (merge_function->name), type,
merge_function->type);
indent += SUB_INDENT;
complain_indent (&merge_function->type_declaration_loc, complaint,
&indent,
_("previous declaration"));
complain (&declaration_loc, complaint,
_("result type clash on merge function %s: "
"<%s> != <%s>"),
quote (merge_function->name), type,
merge_function->type);
subcomplain (&merge_function->type_declaration_loc, complaint,
_("previous declaration"));
}
merge_function->type = uniqstr_new (type);
merge_function->type_declaration_loc = declaration_loc;

View File

@@ -321,14 +321,14 @@ static void
show_sub_message (warnings warning,
const char* cp, bool explicit_bracketing,
int midrule_rhs_index, char dollar_or_at,
int indent, const variant *var)
const variant *var)
{
const char *at_spec = get_at_spec (var->symbol_index);
if (var->err == 0)
complain_indent (&var->loc, warning, &indent,
_("refers to: %c%s at %s"), dollar_or_at,
var->id, at_spec);
subcomplain (&var->loc, warning,
_("refers to: %c%s at %s"), dollar_or_at,
var->id, at_spec);
else
{
const char *id;
@@ -375,8 +375,8 @@ show_sub_message (warnings warning,
_(", cannot be accessed from midrule action at $%d"),
midrule_rhs_index);
complain_indent (&id_loc, warning, &indent, "%s",
obstack_finish0 (&msg_buf));
subcomplain (&id_loc, warning, "%s",
obstack_finish0 (&msg_buf));
obstack_free (&msg_buf, 0);
}
}
@@ -384,14 +384,13 @@ show_sub_message (warnings warning,
static void
show_sub_messages (warnings warning,
const char* cp, bool explicit_bracketing,
int midrule_rhs_index, char dollar_or_at,
int indent)
int midrule_rhs_index, char dollar_or_at)
{
for (int i = 0; i < variant_count; ++i)
show_sub_message (warning | silent,
cp, explicit_bracketing,
midrule_rhs_index, dollar_or_at,
indent, &variant_table[i]);
&variant_table[i]);
}
/* Returned from "parse_ref" when the reference
@@ -492,47 +491,44 @@ parse_ref (char *cp, symbol_list *rule, int rule_length,
{
int len = (explicit_bracketing || !ref_tail_fields) ?
cp_end - cp : ref_tail_fields - cp;
int indent = 0;
complain_indent (text_loc, complaint, &indent,
_("invalid reference: %s"), quote (text));
indent += SUB_INDENT;
complain (text_loc, complaint,
_("invalid reference: %s"), quote (text));
if (len == 0)
{
location sym_loc = *text_loc;
sym_loc.start.column += 1;
sym_loc.end = sym_loc.start;
complain_indent (&sym_loc, complaint, &indent,
_("syntax error after '%c', expecting integer, "
"letter, '_', '[', or '$'"),
dollar_or_at);
subcomplain (&sym_loc, complaint,
_("syntax error after '%c', expecting integer, "
"letter, '_', '[', or '$'"),
dollar_or_at);
}
else if (midrule_rhs_index)
complain_indent (&rule->rhs_loc, complaint, &indent,
_("symbol not found in production before $%d: "
"%.*s"),
midrule_rhs_index, len, cp);
subcomplain (&rule->rhs_loc, complaint,
_("symbol not found in production before $%d: "
"%.*s"),
midrule_rhs_index, len, cp);
else
complain_indent (&rule->rhs_loc, complaint, &indent,
_("symbol not found in production: %.*s"),
len, cp);
subcomplain (&rule->rhs_loc, complaint,
_("symbol not found in production: %.*s"),
len, cp);
if (variant_count > 0)
show_sub_messages (complaint,
cp, explicit_bracketing, midrule_rhs_index,
dollar_or_at, indent);
dollar_or_at);
return INVALID_REF;
}
case 1:
{
int indent = 0;
if (variant_count > 1)
{
complain_indent (text_loc, Wother, &indent,
_("misleading reference: %s"), quote (text));
complain (text_loc, Wother,
_("misleading reference: %s"), quote (text));
show_sub_messages (Wother,
cp, explicit_bracketing, midrule_rhs_index,
dollar_or_at, indent + SUB_INDENT);
dollar_or_at);
}
{
int symbol_index =
@@ -543,12 +539,11 @@ parse_ref (char *cp, symbol_list *rule, int rule_length,
case 2:
default:
{
int indent = 0;
complain_indent (text_loc, complaint, &indent,
_("ambiguous reference: %s"), quote (text));
complain (text_loc, complaint,
_("ambiguous reference: %s"), quote (text));
show_sub_messages (complaint,
cp, explicit_bracketing, midrule_rhs_index,
dollar_or_at, indent + SUB_INDENT);
dollar_or_at);
return INVALID_REF;
}
}

View File

@@ -189,7 +189,7 @@ flag (const char *arg)
else if (STREQ (arg, "fatal"))
return fatal;
else if (STREQ (arg, "note"))
return silent | complaint | no_caret;
return silent | complaint | no_caret | note;
else if (STREQ (arg, "warn"))
return Wother;
else
@@ -225,14 +225,7 @@ at_complain (int argc, char *argv[], char **out_namep, int *out_linenop)
boundary_set_from_string (&loc.end, argv[3]);
locp = &loc;
}
static int indent;
if (w & silent)
indent += SUB_INDENT;
else
indent = 0;
complain_args (locp, w, &indent, argc - 4, argv + 4);
if (w & silent)
indent -= SUB_INDENT;
complain_args (locp, w, argc - 4, argv + 4);
}
static void

View File

@@ -295,42 +295,29 @@ static void
complain_symbol_redeclared (symbol *s, const char *what, location first,
location second)
{
int i = 0;
locations_sort (&first, &second);
complain_indent (&second, complaint, &i,
_("%s redeclaration for %s"), what, s->tag);
i += SUB_INDENT;
complain_indent (&first, complaint, &i,
_("previous declaration"));
complain (&second, complaint, _("%s redeclaration for %s"), what, s->tag);
subcomplain (&first, complaint, _("previous declaration"));
}
static void
complain_semantic_type_redeclared (semantic_type *s, const char *what, location first,
location second)
{
int i = 0;
locations_sort (&first, &second);
complain_indent (&second, complaint, &i,
_("%s redeclaration for <%s>"), what, s->tag);
i += SUB_INDENT;
complain_indent (&first, complaint, &i,
_("previous declaration"));
complain (&second, complaint, _("%s redeclaration for <%s>"), what, s->tag);
subcomplain (&first, complaint, _("previous declaration"));
}
static void
complain_class_redeclared (symbol *sym, symbol_class class, location second)
{
int i = 0;
complain_indent (&second, complaint, &i,
class == token_sym
? _("symbol %s redeclared as a token")
: _("symbol %s redeclared as a nonterminal"), sym->tag);
complain (&second, complaint,
class == token_sym
? _("symbol %s redeclared as a token")
: _("symbol %s redeclared as a nonterminal"), sym->tag);
if (!location_empty (sym->location))
{
i += SUB_INDENT;
complain_indent (&sym->location, complaint, &i,
_("previous definition"));
}
subcomplain (&sym->location, complaint, _("previous definition"));
}
static const symbol *
@@ -546,12 +533,10 @@ symbol_class_set (symbol *sym, symbol_class class, location loc, bool declaring)
{
if (s->status == declared)
{
int i = 0;
complain_indent (&loc, Wother, &i,
_("symbol %s redeclared"), sym->tag);
i += SUB_INDENT;
complain_indent (&sym->location, Wother, &i,
_("previous declaration"));
complain (&loc, Wother,
_("symbol %s redeclared"), sym->tag);
subcomplain (&sym->location, Wother,
_("previous declaration"));
}
else
s->status = declared;
@@ -731,15 +716,13 @@ symbol_pack (symbol *this)
static void
complain_user_token_number_redeclared (int num, symbol *first, symbol *second)
{
int i = 0;
symbols_sort (&first, &second);
complain_indent (&second->location, complaint, &i,
_("user token number %d redeclaration for %s"),
num, second->tag);
i += SUB_INDENT;
complain_indent (&first->location, complaint, &i,
_("previous declaration for %s"),
first->tag);
complain (&second->location, complaint,
_("user token number %d redeclaration for %s"),
num, second->tag);
subcomplain (&first->location, complaint,
_("previous declaration for %s"),
first->tag);
}
/*--------------------------------------------------.