diagnostics: complain about undeclared string tokens

String literals, which allow for better error messages, are (too)
liberally accepted by Bison, which might result in silent errors.  For
instance

    %type <exVal> cond "condition"

does not define “condition” as a string alias to 'cond' (nonterminal
symbols do not have string aliases).  It is rather equivalent to

    %nterm <exVal> cond
    %token <exVal> "condition"

i.e., it gives the type 'exVal' to the "condition" token, which was
clearly not the intention.

Introduce -Wdangling-alias to catch this.

* src/complain.h, src/complain.c: Add support for -Wdangling-alias.
(argmatch_warning_args): Sort.
* src/symtab.c (symbol_check_defined): Complain about dangling
aliases.
* doc/bison.texi: Document it.
* tests/input.at (Dangling aliases): New test.
This commit is contained in:
Akim Demaille
2019-11-12 08:28:51 +01:00
parent 28d1ca8f48
commit 8a910107b3
6 changed files with 134 additions and 13 deletions

View File

@@ -298,6 +298,31 @@ input.y:8.14: error: syntax error, unexpected integer
AT_CLEANUP
## ------------------ ##
## Dangling aliases. ##
## ------------------ ##
AT_SETUP([Dangling aliases])
AT_DATA([input.y],
[[%token FOO "foo"
%type <val> "bar"
%%
expr: "foo" "bar" "baz"
]])
AT_BISON_CHECK([-fcaret -Wdangling input.y], [0], [],
[[input.y:2.13-17: warning: string literal "bar" not attached to a symbol [-Wdangling-alias]
2 | %type <val> "bar"
| ^~~~~
input.y:4.19-23: warning: string literal "baz" not attached to a symbol [-Wdangling-alias]
4 | expr: "foo" "bar" "baz"
| ^~~~~
]])
AT_CLEANUP
## --------------------- ##
## Symbol declarations. ##
## --------------------- ##