Skip to content

Commit 160543c

Browse files
Fix add_identifier timing: register each declarator immediately after its declarator via helper rules
Co-authored-by: michael-schwarz <13812333+michael-schwarz@users.noreply.github.com>
1 parent d8822cc commit 160543c

1 file changed

Lines changed: 14 additions & 12 deletions

File tree

src/frontc/cparser.mly

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,8 @@ let doDeclaration (loc: cabsloc) (specs: spec_elem list) (nl: init_name list) :
123123
if nl = [] then
124124
ONLYTYPEDEF (specs, loc)
125125
else begin
126-
(* Tell the lexer about the new variable names.
127-
For initialized declarations, add_identifier was already called in
128-
declarator_init_start before the initializer was parsed (C11 6.2.1.7).
129-
Only register names that have no initializer here. *)
130-
List.iter (fun ((n, _, _, _), init) ->
131-
match init with
132-
| NO_INIT -> !Lexerhack.add_identifier n
133-
| _ -> ()
134-
) nl;
126+
(* add_identifier is called in declarator_no_init / declarator_init_start as each
127+
declarator is parsed (C11 6.2.1.7), so nothing to do here. *)
135128
DECDEF ((specs, nl), loc)
136129
end
137130

@@ -395,7 +388,7 @@ let transformOffsetOf (speclist, dtype) member =
395388

396389
%type <Cabs.init_name> init_declarator
397390
%type <Cabs.init_name list> init_declarator_list
398-
%type <Cabs.name> declarator declarator_init_start
391+
%type <Cabs.name> declarator declarator_no_init declarator_init_start
399392
%type <Cabs.name * expression option> field_decl
400393
%type <(Cabs.name * expression option) list> field_decl_list
401394
%type <string * Cabs.decl_type> direct_decl
@@ -1043,14 +1036,23 @@ init_declarator_attr:
10431036

10441037
;
10451038
init_declarator: /* ISO 6.7 */
1046-
declarator { ($1, NO_INIT) }
1039+
declarator_no_init { ($1, NO_INIT) }
10471040
| declarator_init_start init_expression location
10481041
{ let (n, d, a, l) = $1 in ((n, d, a, joinLoc l $3), $2) }
10491042
;
10501043

1044+
/* (* Parses "declarator" (without initializer) and immediately adds the declared name as a
1045+
variable identifier in the lexer hack, so that subsequent declarators in the same
1046+
declaration see the name as an identifier, not as a type
1047+
(C11 6.2.1.7: scope begins just after the completion of the declarator). *) */
1048+
declarator_no_init:
1049+
declarator { let (n, _, _, _) = $1 in !Lexerhack.add_identifier n; $1 }
1050+
;
1051+
10511052
/* (* Parses "declarator =" and adds the declared name as a variable identifier
10521053
in the lexer hack, so that in the initializer the name shadows any typedef
1053-
with the same name (C11 6.2.1.7: scope begins after completion of declarator). *) */
1054+
with the same name (C11 6.2.1.7: scope begins just after the completion of the
1055+
declarator). *) */
10541056
declarator_init_start:
10551057
declarator EQ { let (n, _, _, _) = $1 in !Lexerhack.add_identifier n; $1 }
10561058
;

0 commit comments

Comments
 (0)