From 365b4d95a49dabdc08d271a31ca3eb9c6591ba91 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Fri, 3 May 2019 08:30:02 +0200 Subject: [PATCH] style: clarify the use of symbol_lists' locations symbol_list features a 'location' and a 'sym_loc' member. The former is expected to be set only for symbol_lists that denote a symbol (not a type name), and the latter should only denote the location of the symbol/type name. Yet both are set, and the name "location" is too unprecise. * src/symlist.h, src/symlist.c (symbol_list::location): Rename as rhs_loc for clarity. Move it to the "section" of data valid only for rules. * src/reader.c, src/scan-code.l: Adjust. --- src/reader.c | 28 ++++++++++++++-------------- src/scan-code.l | 4 ++-- src/symlist.c | 5 +++-- src/symlist.h | 4 +++- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/reader.c b/src/reader.c index 9d4d049d..fecb999e 100644 --- a/src/reader.c +++ b/src/reader.c @@ -295,7 +295,7 @@ grammar_rule_check_and_complete (symbol_list *r) char const *rhs_type = first_rhs->content->type_name ? first_rhs->content->type_name : ""; if (!UNIQSTR_EQ (lhs_type, rhs_type)) - complain (&r->location, Wother, + complain (&r->rhs_loc, Wother, _("type clash on default action: <%s> != <%s>"), lhs_type, rhs_type); else @@ -308,7 +308,7 @@ grammar_rule_check_and_complete (symbol_list *r) if (is_cxx) { code_props_rule_action_init (&r->action_props, "{ $$ = $1; }", - r->location, r, + r->rhs_loc, r, /* name */ NULL, /* type */ NULL, /* is_predicate */ false); @@ -318,7 +318,7 @@ grammar_rule_check_and_complete (symbol_list *r) } /* Warn if there is no default for $$ but we need one. */ else - complain (&r->location, Wother, + complain (&r->rhs_loc, Wother, _("empty rule for typed nonterminal, and no action")); } @@ -335,9 +335,9 @@ grammar_rule_check_and_complete (symbol_list *r) { warnings warn_flag = midrule_warning ? Wmidrule_values : Wother; if (n) - complain (&l->location, warn_flag, _("unused value: $%d"), n); + complain (&l->sym_loc, warn_flag, _("unused value: $%d"), n); else - complain (&l->location, warn_flag, _("unset value: $$")); + complain (&l->rhs_loc, warn_flag, _("unset value: $$")); } } } @@ -357,9 +357,9 @@ grammar_rule_check_and_complete (symbol_list *r) && !r->percent_empty_loc.start.file && warning_is_enabled (Wempty_rule)) { - complain (&r->location, Wempty_rule, + complain (&r->rhs_loc, Wempty_rule, _("empty rule without %%empty")); - fixits_register (&r->location, " %empty "); + fixits_register (&r->rhs_loc, " %empty "); } /* See comments in grammar_current_rule_prec_set for how POSIX @@ -369,12 +369,12 @@ grammar_rule_check_and_complete (symbol_list *r) && r->ruleprec->tag[0] != '\'' && r->ruleprec->tag[0] != '"' && r->ruleprec->content->status != declared && !r->ruleprec->content->prec) - complain (&r->location, Wother, + complain (&r->rhs_loc, Wother, _("token for %%prec is not defined: %s"), r->ruleprec->tag); /* Check that the (main) action was not typed. */ if (r->action_props.type) - complain (&r->location, Wother, + complain (&r->rhs_loc, Wother, _("only midrule actions can be typed: %s"), r->action_props.type); } @@ -387,8 +387,8 @@ void grammar_current_rule_end (location loc) { /* Put an empty link in the list to mark the end of this rule */ - grammar_symbol_append (NULL, grammar_end->location); - current_rule->location = loc; + grammar_symbol_append (NULL, grammar_end->rhs_loc); + current_rule->rhs_loc = loc; } @@ -422,7 +422,7 @@ grammar_midrule_action (void) ++nrules; ++nritems; /* Attach its location and actions to that of the DUMMY. */ - midrule->location = dummy_location; + midrule->rhs_loc = dummy_location; code_props_rule_action_init (&midrule->action_props, current_rule->action_props.code, current_rule->action_props.location, @@ -647,7 +647,7 @@ packgram (void) rules[ruleno].dprec = lhs->dprec; rules[ruleno].merger = lhs->merger; rules[ruleno].precsym = NULL; - rules[ruleno].location = lhs->location; + rules[ruleno].location = lhs->rhs_loc; rules[ruleno].useful = true; rules[ruleno].action = lhs->action_props.code; rules[ruleno].action_location = lhs->action_props.location; @@ -824,7 +824,7 @@ check_and_convert_grammar (void) $accept: %start $end. */ { symbol_list *p = symbol_list_sym_new (accept, empty_location); - p->location = grammar->location; + p->rhs_loc = grammar->rhs_loc; p->next = symbol_list_sym_new (startsymbol, empty_location); p->next->next = symbol_list_sym_new (endtoken, empty_location); p->next->next->next = symbol_list_sym_new (NULL, empty_location); diff --git a/src/scan-code.l b/src/scan-code.l index 894cc542..0974905a 100644 --- a/src/scan-code.l +++ b/src/scan-code.l @@ -507,12 +507,12 @@ parse_ref (char *cp, symbol_list *rule, int rule_length, dollar_or_at); } else if (midrule_rhs_index) - complain_indent (&rule->location, complaint, &indent, + complain_indent (&rule->rhs_loc, complaint, &indent, _("symbol not found in production before $%d: " "%.*s"), midrule_rhs_index, len, cp); else - complain_indent (&rule->location, complaint, &indent, + complain_indent (&rule->rhs_loc, complaint, &indent, _("symbol not found in production: %.*s"), len, cp); diff --git a/src/symlist.c b/src/symlist.c index 79277e27..d594ddbe 100644 --- a/src/symlist.c +++ b/src/symlist.c @@ -34,7 +34,7 @@ symbol_list_sym_new (symbol *sym, location loc) res->content_type = SYMLIST_SYMBOL; res->content.sym = sym; - res->location = res->sym_loc = loc; + res->sym_loc = loc; res->named_ref = NULL; res->midrule = NULL; @@ -42,6 +42,7 @@ symbol_list_sym_new (symbol *sym, location loc) res->midrule_parent_rhs_index = 0; /* Members used for LHS only. */ + res->rhs_loc = empty_location; res->ruleprec = NULL; res->percent_empty_loc = empty_location; code_props_none_init (&res->action_props); @@ -73,7 +74,7 @@ symbol_list_type_new (uniqstr type_name, location loc) res->content.sem_type->location = loc; res->content.sem_type->status = undeclared; - res->location = res->sym_loc = loc; + res->sym_loc = loc; res->named_ref = NULL; res->next = NULL; diff --git a/src/symlist.h b/src/symlist.h index ee40a851..880e8544 100644 --- a/src/symlist.h +++ b/src/symlist.h @@ -50,7 +50,6 @@ typedef struct symbol_list */ semantic_type *sem_type; } content; - location location; /* Named reference. */ named_ref *named_ref; @@ -74,6 +73,9 @@ typedef struct symbol_list | when several RHSs are bound to a single lhs via "|"). | `--------------------------------------------------------------*/ + /* Location of the RHS. */ + location rhs_loc; + /* Precedence/associativity. */ symbol *ruleprec;