* Noteworthy changes in release ?.? (????-??-??) [?]
+** Warnings about useless semantic types
+
+ Bison now warns about useless (uninhabited) semantic types. Since
+ semantic types are not declared to Bison (they are defined in the opaque
+ %union structure), it is %printer/%destructor directives about useless
+ types that trigger the warning:
+
+ %token <type1> term
+ %type <type2> nterm
+ %printer {} <type1> <type3>
+ %destructor {} <type2> <type4>
+ %%
+ nterm: term { $$ = $1; };
+
+ 3.28-34: warning: type <type3> is used, but is not associated to any symbol
+ 4.28-34: warning: type <type4> is used, but is not associated to any symbol
+
** Warnings about undeclared symbols
Bison used to raise an error for %printer and %destructor directives for
symbol_list *res = xmalloc (sizeof *res);
res->content_type = SYMLIST_TYPE;
- res->content.type_name = type_name;
+ res->content.sem_type = xmalloc (sizeof (semantic_type));
+ res->content.sem_type->tag = type_name;
+ res->content.sem_type->location = loc;
+ res->content.sem_type->status = undeclared;
+
res->location = res->sym_loc = loc;
res->named_ref = NULL;
res->next = NULL;
break;
case SYMLIST_TYPE:
semantic_type_code_props_set
- (semantic_type_get (node->content.type_name),
+ (semantic_type_get (node->content.sem_type->tag,
+ &node->content.sem_type->location),
kind, &cprops);
+ if (node->content.sem_type->status == undeclared)
+ node->content.sem_type->status = used;
break;
case SYMLIST_DEFAULT_TAGGED:
default_tagged_code_props_set (kind, &cprops);
/**
* The semantic type iff <tt>symbol_list::content_type = SYMLIST_TYPE</tt>.
*/
- uniqstr type_name;
+ semantic_type *sem_type;
} content;
location location;
`-------------------------------------------------------------------*/
static symbol **symbols_sorted = NULL;
+static symbol **semantic_types_sorted = NULL;
/*------------------------.
| Distinguished symbols. |
`----------------------------------------*/
static semantic_type *
-semantic_type_new (uniqstr tag)
+semantic_type_new (uniqstr tag, const location *loc)
{
semantic_type *res = xmalloc (sizeof *res);
uniqstr_assert (tag);
res->tag = tag;
+ if (loc)
+ res->location = *loc;
for (int i = 0; i < CODE_PROPS_SIZE; ++i)
code_props_none_init (&res->props[i]);
if (sym->type_name)
{
code_props const *code =
- &semantic_type_get (sym->type_name)->props[kind];
+ &semantic_type_get (sym->type_name, NULL)->props[kind];
if (code->code)
return code;
}
sym->number = nvars++;
}
+ /* Set the semantic type status associated to the current symbol to
+ 'declared' so that we could check semantic types unnecessary uses. */
+ if (sym->type_name)
+ {
+ semantic_type *sem_type = semantic_type_get (sym->type_name, NULL);
+ if (sem_type)
+ sem_type->status = declared;
+ }
+
+ return true;
+}
+
+static inline bool
+semantic_type_check_defined (semantic_type *sem_type)
+{
+ if (sem_type->status != declared)
+ warn_at (sem_type->location,
+ _("type <%s> is used, but is not associated to any symbol"),
+ sem_type->tag);
+
return true;
}
return symbol_check_defined (sym);
}
+static bool
+semantic_type_check_defined_processor (void *sem_type,
+ void *null ATTRIBUTE_UNUSED)
+{
+ return semantic_type_check_defined (sem_type);
+}
+
void
symbol_make_alias (symbol *sym, symbol *str, location loc)
`-----------------------------------------------------------------------*/
semantic_type *
-semantic_type_from_uniqstr (const uniqstr key)
+semantic_type_from_uniqstr (const uniqstr key, const location *loc)
{
semantic_type probe;
semantic_type *entry;
if (!entry)
{
/* First insertion in the hash. */
- entry = semantic_type_new (key);
+ entry = semantic_type_new (key, loc);
if (!hash_insert (semantic_type_table, entry))
xalloc_die ();
}
`-----------------------------------------------------------------------*/
semantic_type *
-semantic_type_get (const char *key)
+semantic_type_get (const char *key, const location *loc)
{
- return semantic_type_from_uniqstr (uniqstr_new (key));
+ return semantic_type_from_uniqstr (uniqstr_new (key), loc);
}
{
symbols_do (symbol_check_defined_processor, NULL,
symbol_table, symbols_sorted);
+ symbols_do (semantic_type_check_defined_processor, NULL,
+ semantic_type_table, semantic_types_sorted);
}
/*------------------------------------------------------------------.
/** The key, name of the semantic type. */
uniqstr tag;
+ /** The location of its first occurence. */
+ location location;
+
+ /** Its status : "undeclared", "used" or "declared".
+ It cannot be "needed". */
+ status status;
+
/** Any \c %destructor and %printer declared for this
semantic type. */
code_props props[CODE_PROPS_SIZE];
} semantic_type;
/** Fetch (or create) the semantic type associated to KEY. */
-semantic_type *semantic_type_from_uniqstr (const uniqstr key);
+semantic_type *semantic_type_from_uniqstr (const uniqstr key,
+ const location *loc);
/** Fetch (or create) the semantic type associated to KEY. */
-semantic_type *semantic_type_get (const char *key);
+semantic_type *semantic_type_get (const char *key, const location *loc);
/** Set the \c destructor or \c printer associated with \c type. */
void semantic_type_code_props_set (semantic_type *type,
%type <::std::list<std::string>> list result;
%printer { debug_stream() << $][$; }
- <int> <::std::string> <::std::list<::std::string>>;
+ <int> <::std::string> <::std::list<std::string>>;
%%
result:
AT_CLEANUP
+## ----------------------------------------------------- ##
+## Unassociated types used for a printer or destructor. ##
+## ----------------------------------------------------- ##
+
+AT_SETUP([Unassociated types used for a printer or destructor])
+
+AT_DATA([[input.y]],
+[[%token <type1> tag1
+%type <type2> tag2
+
+%printer { } <type1> <type3>
+%destructor { } <type2> <type4>
+
+%%
+
+exp: tag1 { $1; }
+ | tag2 { $1; }
+
+tag2: "a" { $$; }
+]])
+
+AT_BISON_CHECK([input.y], [0], [],
+[[input.y:4.22-28: warning: type <type3> is used, but is not associated to any symbol
+input.y:5.25-31: warning: type <type4> is used, but is not associated to any symbol
+]])
+
+AT_CLEANUP
+
+
## ---------------------------------------- ##
## Unused values with default %destructor. ##
## ---------------------------------------- ##