Convert "misleading reference" messages to warnings.

* src/scan-code.l: New function 'show_sub_messages', more
	factoring.
	* tests/named-ref.at: Adjust tests.
This commit is contained in:
Alex Rozenman
2009-08-08 18:10:23 +03:00
parent 401b73afdf
commit ce268795cc
3 changed files with 129 additions and 100 deletions

View File

@@ -1,3 +1,10 @@
2009-08-08 Alex Rozenman <rozenman@gmail.com>
Convert "misleading reference" messages to warnings.
* src/scan-code.l: New function 'show_sub_messages', more
factoring.
* tests/named-ref.at: Adjust tests.
2009-08-06 Joel E. Denny <jdenny@clemson.edu> 2009-08-06 Joel E. Denny <jdenny@clemson.edu>
maint: run "make update-copyright" maint: run "make update-copyright"

View File

@@ -307,7 +307,7 @@ contains_dot_or_dash (const char* p)
typedef struct typedef struct
{ {
/* Index in symbol list. */ /* Index in symbol list. */
unsigned index; unsigned symbol_index;
/* Matched symbol id and loc. */ /* Matched symbol id and loc. */
uniqstr id; uniqstr id;
@@ -386,7 +386,7 @@ variant_add (uniqstr id, location id_loc, unsigned symbol_index,
(!exact_mode && is_dot_or_dash (*prefix_end)))) (!exact_mode && is_dot_or_dash (*prefix_end))))
{ {
variant *r = variant_table_grow (); variant *r = variant_table_grow ();
r->index = symbol_index; r->symbol_index = symbol_index;
r->id = id; r->id = id;
r->loc = id_loc; r->loc = id_loc;
r->hidden_by = NULL; r->hidden_by = NULL;
@@ -408,12 +408,78 @@ get_at_spec(unsigned symbol_index)
return at_buf; return at_buf;
} }
static void
show_sub_messages (const char* cp, bool exact_mode, int midrule_rhs_index,
char dollar_or_at, bool is_warning)
{
unsigned i;
for (i = 0; i < variant_count; ++i)
{
const variant *var = &variant_table[i];
const char *at_spec = get_at_spec (var->symbol_index);
if (var->err == 0)
{
if (is_warning)
warn_at (var->loc, _(" refers to: %c%s at %s"),
dollar_or_at, var->id, at_spec);
else
complain_at (var->loc, _(" refers to: %c%s at %s"),
dollar_or_at, var->id, at_spec);
}
else
{
static struct obstack msg_buf;
const char *tail = exact_mode ? "" :
cp + strlen (var->id);
const char *id = var->hidden_by ? var->hidden_by->id :
var->id;
location id_loc = var->hidden_by ? var->hidden_by->loc :
var->loc;
/* Create the explanation message. */
obstack_init (&msg_buf);
obstack_fgrow1 (&msg_buf, " possibly meant: %c", dollar_or_at);
if (contains_dot_or_dash (id))
obstack_fgrow1 (&msg_buf, "[%s]", id);
else
obstack_sgrow (&msg_buf, id);
obstack_sgrow (&msg_buf, tail);
if (var->err & VARIANT_HIDDEN)
{
obstack_fgrow1 (&msg_buf, ", hiding %c", dollar_or_at);
if (contains_dot_or_dash (var->id))
obstack_fgrow1 (&msg_buf, "[%s]", var->id);
else
obstack_sgrow (&msg_buf, var->id);
obstack_sgrow (&msg_buf, tail);
}
obstack_fgrow1 (&msg_buf, " at %s", at_spec);
if (var->err & VARIANT_NOT_VISIBLE_FROM_MIDRULE)
obstack_fgrow1 (&msg_buf, ", cannot be accessed from "
"mid-rule action at $%d", midrule_rhs_index);
obstack_1grow (&msg_buf, '\0');
if (is_warning)
warn_at (id_loc, _("%s"), (char *) obstack_finish (&msg_buf));
else
complain_at (id_loc, _("%s"), (char *) obstack_finish (&msg_buf));
obstack_free (&msg_buf, 0);
}
}
}
/* Returned from "parse_ref" when the reference /* Returned from "parse_ref" when the reference
is inappropriate. */ is inappropriate. */
#define INVALID_REF (INT_MIN) #define INVALID_REF (INT_MIN)
/* Returned from "parse_ref" when the reference /* Returned from "parse_ref" when the reference
points to LHS ($$) of the current rule. */ points to LHS ($$) of the current rule or midrule. */
#define LHS_REF (INT_MIN + 1) #define LHS_REF (INT_MIN + 1)
/* Parse named or positional reference. In case of positional /* Parse named or positional reference. In case of positional
@@ -427,9 +493,9 @@ parse_ref (char *cp, symbol_list *rule, int rule_length,
symbol_list *l; symbol_list *l;
char *cp_end; char *cp_end;
bool exact_mode; bool exact_mode;
bool has_error;
bool has_valid;
unsigned i; unsigned i;
unsigned valid_variants = 0;
unsigned valid_variant_index = 0;
if ('$' == *cp) if ('$' == *cp)
return LHS_REF; return LHS_REF;
@@ -497,114 +563,70 @@ parse_ref (char *cp, symbol_list *rule, int rule_length,
} }
/* Check errors. */ /* Check errors. */
has_error = false;
has_valid = false;
for (i = 0; i < variant_count; ++i) for (i = 0; i < variant_count; ++i)
{ {
variant *var = &variant_table[i]; variant *var = &variant_table[i];
unsigned symbol_index = var->index; unsigned symbol_index = var->symbol_index;
/* Check visibility from mid-rule actions. */ /* Check visibility from mid-rule actions. */
if (midrule_rhs_index != 0 if (midrule_rhs_index != 0
&& (symbol_index == 0 || midrule_rhs_index < symbol_index)) && (symbol_index == 0 || midrule_rhs_index < symbol_index))
{ var->err |= VARIANT_NOT_VISIBLE_FROM_MIDRULE;
var->err |= VARIANT_NOT_VISIBLE_FROM_MIDRULE;
has_error = true;
}
/* Check correct bracketing. */ /* Check correct bracketing. */
if (!exact_mode && contains_dot_or_dash (var->id)) if (!exact_mode && contains_dot_or_dash (var->id))
{ var->err |= VARIANT_BAD_BRACKETING;
var->err |= VARIANT_BAD_BRACKETING;
has_error = true;
}
/* Check using of hidden symbols. */ /* Check using of hidden symbols. */
if (var->hidden_by) if (var->hidden_by)
{ var->err |= VARIANT_HIDDEN;
var->err |= VARIANT_HIDDEN;
has_error = true;
}
if (!var->err) if (!var->err)
has_valid = true; {
valid_variant_index = i;
++valid_variants;
}
} }
if (variant_count == 1 && has_valid) switch (valid_variants)
{ {
/* The only "good" case is here. */ case 0:
unsigned symbol_index = variant_table[0].index; if (variant_count == 0)
if (symbol_index == midrule_rhs_index) complain_at (text_loc, _("invalid reference: %s, symbol not found"),
return LHS_REF; quote (text));
else else
return symbol_index; {
} complain_at (text_loc, _("invalid reference: %s"),
quote (text));
/* Start complaining. */ show_sub_messages (cp, exact_mode, midrule_rhs_index,
dollar_or_at, false);
if (variant_count == 0) }
complain_at (text_loc, _("invalid reference: %s, symbol not found"), return INVALID_REF;
quote (text)); case 1:
else if (variant_count > 1 && !has_error) {
complain_at (text_loc, _("ambiguous reference: %s"), if (variant_count > 1)
quote (text)); {
else if (variant_count > 1 && has_valid && has_error) warn_at (text_loc, _("misleading reference: %s"),
complain_at (text_loc, _("misleading reference: %s"), quote (text));
quote (text)); show_sub_messages (cp, exact_mode, midrule_rhs_index,
else dollar_or_at, true);
complain_at (text_loc, _("invalid reference: %s"), }
quote (text)); {
unsigned symbol_index =
for (i = 0; i < variant_count; ++i) variant_table[valid_variant_index].symbol_index;
{ return (symbol_index == midrule_rhs_index) ? LHS_REF : symbol_index;
const variant *var = &variant_table[i]; }
const char *at_spec = get_at_spec (var->index); }
case 2:
if (var->err == 0) default:
complain_at (var->loc, _(" refers to: %c%s at %s"), complain_at (text_loc, _("ambiguous reference: %s"),
dollar_or_at, var->id, at_spec); quote (text));
else show_sub_messages (cp, exact_mode, midrule_rhs_index,
{ dollar_or_at, false);
static struct obstack msg_buf; return INVALID_REF;
const char *tail = exact_mode ? "" :
cp + strlen (var->id);
const char *id = var->hidden_by ? var->hidden_by->id :
var->id;
location id_loc = var->hidden_by ? var->hidden_by->loc :
var->loc;
/* Create the explanation message. */
obstack_init (&msg_buf);
obstack_fgrow1 (&msg_buf, " possibly meant: %c", dollar_or_at);
if (contains_dot_or_dash (id))
obstack_fgrow1 (&msg_buf, "[%s]", id);
else
obstack_sgrow (&msg_buf, id);
obstack_sgrow (&msg_buf, tail);
if (var->err & VARIANT_HIDDEN)
{
obstack_fgrow1 (&msg_buf, ", hiding %c", dollar_or_at);
if (contains_dot_or_dash (var->id))
obstack_fgrow1 (&msg_buf, "[%s]", var->id);
else
obstack_sgrow (&msg_buf, var->id);
obstack_sgrow (&msg_buf, tail);
}
obstack_fgrow1 (&msg_buf, " at %s", at_spec);
if (var->err & VARIANT_NOT_VISIBLE_FROM_MIDRULE)
obstack_fgrow1 (&msg_buf, ", cannot be accessed from "
"mid-rule action at $%d", midrule_rhs_index);
obstack_1grow (&msg_buf, '\0');
complain_at (id_loc, _("%s"), (char *) obstack_finish (&msg_buf));
obstack_free (&msg_buf, 0);
}
} }
/* Not reachable. */
return INVALID_REF; return INVALID_REF;
} }

View File

@@ -256,10 +256,10 @@ exp:
AT_BISON_CHECK([-o test.c test.y], 1, [], AT_BISON_CHECK([-o test.c test.y], 1, [],
[[test.y:50.51-60: invalid reference: `$<ival>lo9', symbol not found [[test.y:50.51-60: invalid reference: `$<ival>lo9', symbol not found
test.y:51.51-60: misleading reference: `$<ival>exp' test.y:51.51-60: warning: misleading reference: `$<ival>exp'
test.y:42.1-3: refers to: $exp at $$ test.y:42.1-3: warning: refers to: $exp at $$
test.y:51.7: possibly meant: $x, hiding $exp at $1 test.y:51.7: warning: possibly meant: $x, hiding $exp at $1
test.y:51.41: possibly meant: $r, hiding $exp at $4 test.y:51.41: warning: possibly meant: $r, hiding $exp at $4
test.y:52.51-52: $l of `exp' has no declared type test.y:52.51-52: $l of `exp' has no declared type
test.y:55.46-49: invalid reference: `$r12', symbol not found test.y:55.46-49: invalid reference: `$r12', symbol not found
test.y:56.29-33: invalid reference: `$expo', symbol not found test.y:56.29-33: invalid reference: `$expo', symbol not found
@@ -276,10 +276,10 @@ start: foo foo.bar { $foo.bar; }
foo: '1' foo: '1'
foo.bar: '2' foo.bar: '2'
]]) ]])
AT_BISON_CHECK([-o test.c test.y], 1, [], AT_BISON_CHECK([-o test.c test.y], 0, [],
[[test.y:11.22-29: misleading reference: `$foo.bar' [[test.y:11.22-29: warning: misleading reference: `$foo.bar'
test.y:11.8-10: refers to: $foo at $1 test.y:11.8-10: warning: refers to: $foo at $1
test.y:11.12-18: possibly meant: $[foo.bar] at $2 test.y:11.12-18: warning: possibly meant: $[foo.bar] at $2
]]) ]])
AT_CLEANUP AT_CLEANUP