Skip to content

Commit d7ba59b

Browse files
committed
allows a typedefed named to immediately follow its typedef
In the following example ``` typedef int T; T foo(void); ``` i.e., when the next token after a typedef is the typedefed name, the T token is already looked ahead, so it is recognized as IDENT not as NAMED_TYPE, e.g., here is the trace, ``` Lookahead token is now IDENT (15-16) <-- 'T' Reducing production typedef -> TYPEDEF typedef_type typedef_defs SEMICOLON adding T <-- debug output from the lexer, indicating that T was added ``` The solution is to factor out the typedef non-terminal and move the semicolon to globals so that we have it reduced earlier, when the next token is SEMICOLON: ``` Reducing production typedef -> TYPEDEF typedef_type typedef_defs adding T <-- typedef reduces before semicolon State 542: Shifting (SEMICOLON) to state 543 State 543: Lookahead token is now NAMED_TYPE (15-16) <-- it is now recognized as type name ``` Fixes #23
1 parent 68adbb7 commit d7ba59b

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

ctoxml/test.t/run.t

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3875,3 +3875,16 @@
38753875
</field>
38763876
</struct>
38773877
</file>
3878+
3879+
$ echo 'typedef int T; T bar();' | ctoxml
3880+
<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
3881+
<file>
3882+
<type id="T" store="auto">
3883+
<long/>
3884+
</type>
3885+
<fundec id="bar" store="auto">
3886+
<type>
3887+
<type ref="T"/>
3888+
</type>
3889+
</fundec>
3890+
</file>

frontc/cparser.mly

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,12 @@ globals:
193193
;
194194

195195
typedef:
196-
| TYPEDEF typedef_type typedef_defs SEMICOLON
197-
{let _ = List.iter (fun (id, _, _, _) -> Clexer.add_type id) $3 in
198-
TYPEDEF (set_name_group (fst $2, snd $2) $3, [])}
199-
| gcc_attribute TYPEDEF typedef_type typedef_defs SEMICOLON
200-
{let _ = List.iter (fun (id, _, _, _) -> Clexer.add_type id) $4 in
196+
| TYPEDEF typedef_type typedef_defs
197+
{
198+
List.iter (fun (id, _, _, _) -> Clexer.add_type id) $3;
199+
TYPEDEF (set_name_group (fst $2, snd $2) $3, [])}
200+
| gcc_attribute TYPEDEF typedef_type typedef_defs
201+
{List.iter (fun (id, _, _, _) -> Clexer.add_type id) $4;
201202
TYPEDEF (set_name_group (fst $3, snd $3) $4, $1)}
202203
;
203204

@@ -226,7 +227,7 @@ global:
226227
{ OLDFUNDEF (set_single $1 $2, List.rev $3, (snd $4)) }
227228
| global_type SEMICOLON
228229
{ONLYTYPEDEF (set_name_group $1 [])}
229-
| typedef {$1}
230+
| typedef SEMICOLON {$1}
230231
;
231232
global_type:
232233
| global_mod_list_opt global_qual
@@ -448,7 +449,7 @@ IDENT
448449

449450
/*** Typedef Definition ***/
450451
typedef_type:
451-
typedef_sub
452+
| typedef_sub
452453
{apply_mods (snd $1) ((fst $1), NO_STORAGE)}
453454
| CONST typedef_sub
454455
{apply_mods (BASE_CONST::(snd $2)) ((fst $2), NO_STORAGE)}

0 commit comments

Comments
 (0)