Skip to content

Commit aa40855

Browse files
Move add_type to declarator_no_init: all lexer registration per-declarator, none in doDeclaration
Co-authored-by: michael-schwarz <13812333+michael-schwarz@users.noreply.github.com> Agent-Logs-Url: https://github.com/goblint/cil/sessions/459679d3-44f6-4f0b-8a30-92252ab7fa0d
1 parent 5960536 commit aa40855

1 file changed

Lines changed: 19 additions & 16 deletions

File tree

src/frontc/cparser.mly

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ let smooth_expression lst =
7979
let currentFunctionName = ref "<outside any function>"
8080

8181
(* Set to true while parsing a typedef declaration's declarator list, so that
82-
declarator_no_init knows not to call add_identifier for typedef names. *)
82+
declarator_no_init calls add_type (not add_identifier) for typedef names. *)
8383
let is_typedef_decl = ref false
8484

8585
let announceFunctionName ((n, decl, _, _):name) =
@@ -120,18 +120,17 @@ let applyPointer (ptspecs: attribute list list) (dt: decl_type)
120120

121121
let doDeclaration (loc: cabsloc) (specs: spec_elem list) (nl: init_name list) : definition =
122122
is_typedef_decl := false;
123-
if isTypedef specs then begin
124-
(* Tell the lexer about the new type names *)
125-
List.iter (fun ((n, _, _, _), _) -> !Lexerhack.add_type n) nl;
123+
(* Lexer registrations (add_type / add_identifier) are done per-declarator in
124+
declarator_no_init / declarator_init_start as each declarator is parsed
125+
(C11 6.2.1.7: scope begins just after the completion of the declarator), so
126+
nothing to do here. *)
127+
if isTypedef specs then
126128
TYPEDEF ((specs, List.map (fun (n, _) -> n) nl), loc)
127-
end else
129+
else
128130
if nl = [] then
129131
ONLYTYPEDEF (specs, loc)
130-
else begin
131-
(* add_identifier is called in declarator_no_init / declarator_init_start as each
132-
declarator is parsed (C11 6.2.1.7), so nothing to do here. *)
132+
else
133133
DECDEF ((specs, nl), loc)
134-
end
135134

136135

137136
let doFunctionDef (loc: cabsloc)
@@ -1046,15 +1045,19 @@ init_declarator: /* ISO 6.7 */
10461045
{ let (n, d, a, l) = $1 in ((n, d, a, joinLoc l $3), $2) }
10471046
;
10481047

1049-
/* (* Parses "declarator" (without initializer) and immediately adds the declared name
1050-
as a variable identifier in the lexer hack, for non-typedef declarations only,
1051-
so that subsequent declarators in the same declaration see the name as an
1052-
identifier, not as a type (C11 6.2.1.7: scope begins just after the completion
1053-
of its declarator). For typedef declarations (is_typedef_decl = true), the name
1054-
is registered later via add_type in doDeclaration. *) */
1048+
/* (* Parses "declarator" (without initializer) and immediately registers the
1049+
declared name in the lexer hack, per C11 6.2.1.7 (scope begins just after
1050+
the completion of its declarator).
1051+
- For non-typedef declarations: calls add_identifier so subsequent
1052+
declarators in the same list see the name as a variable (not a type).
1053+
- For typedef declarations (is_typedef_decl = true): calls add_type so
1054+
subsequent declarators see the name as a type.
1055+
In both cases every declarator is registered at the right time without
1056+
going through doDeclaration. *) */
10551057
declarator_no_init:
10561058
declarator { let (n, _, _, _) = $1 in
1057-
if not !is_typedef_decl then !Lexerhack.add_identifier n;
1059+
if !is_typedef_decl then !Lexerhack.add_type n
1060+
else !Lexerhack.add_identifier n;
10581061
$1 }
10591062
;
10601063

0 commit comments

Comments
 (0)