Skip to content

Commit 66f8d59

Browse files
Address sim642 comments: move is_typedef_decl to Lexerhack, consolidate tests, remove noinit file
Agent-Logs-Url: https://github.com/goblint/cil/sessions/e317c474-30b0-43d6-aec0-4a7a9377d477 Co-authored-by: michael-schwarz <13812333+michael-schwarz@users.noreply.github.com>
1 parent aa40855 commit 66f8d59

5 files changed

Lines changed: 20 additions & 58 deletions

File tree

src/frontc/cparser.mly

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,6 @@ let smooth_expression lst =
7878

7979
let currentFunctionName = ref "<outside any function>"
8080

81-
(* Set to true while parsing a typedef declaration's declarator list, so that
82-
declarator_no_init calls add_type (not add_identifier) for typedef names. *)
83-
let is_typedef_decl = ref false
84-
8581
let announceFunctionName ((n, decl, _, _):name) =
8682
!Lexerhack.add_identifier n;
8783
(* Start a context that includes the parameter names and the whole body.
@@ -119,7 +115,7 @@ let applyPointer (ptspecs: attribute list list) (dt: decl_type)
119115
loop ptspecs
120116

121117
let doDeclaration (loc: cabsloc) (specs: spec_elem list) (nl: init_name list) : definition =
122-
is_typedef_decl := false;
118+
Lexerhack.is_typedef_decl := false;
123119
(* Lexer registrations (add_type / add_identifier) are done per-declarator in
124120
declarator_no_init / declarator_init_start as each declarator is parsed
125121
(C11 6.2.1.7: scope begins just after the completion of the declarator), so
@@ -1050,13 +1046,13 @@ init_declarator: /* ISO 6.7 */
10501046
the completion of its declarator).
10511047
- For non-typedef declarations: calls add_identifier so subsequent
10521048
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
1049+
- For typedef declarations (Lexerhack.is_typedef_decl = true): calls add_type so
10541050
subsequent declarators see the name as a type.
10551051
In both cases every declarator is registered at the right time without
10561052
going through doDeclaration. *) */
10571053
declarator_no_init:
10581054
declarator { let (n, _, _, _) = $1 in
1059-
if !is_typedef_decl then !Lexerhack.add_type n
1055+
if !Lexerhack.is_typedef_decl then !Lexerhack.add_type n
10601056
else !Lexerhack.add_identifier n;
10611057
$1 }
10621058
;
@@ -1071,7 +1067,7 @@ declarator_init_start:
10711067

10721068
decl_spec_list_common: /* ISO 6.7 */
10731069
/* ISO 6.7.1 */
1074-
| TYPEDEF decl_spec_list_opt { is_typedef_decl := true; SpecTypedef :: $2, $1 }
1070+
| TYPEDEF decl_spec_list_opt { Lexerhack.is_typedef_decl := true; SpecTypedef :: $2, $1 }
10751071
| EXTERN decl_spec_list_opt { SpecStorage EXTERN :: $2, $1 }
10761072
| STATIC decl_spec_list_opt { SpecStorage STATIC :: $2, $1 }
10771073
| AUTO decl_spec_list_opt { SpecStorage AUTO :: $2, $1 }

src/frontc/lexerhack.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ let push_context: (unit -> unit) ref =
1414
ref (fun _ -> E.s (E.bug "You called an uninitialized push_context"))
1515

1616
let pop_context: (unit -> unit) ref =
17-
ref (fun _ -> E.s (E.bug "You called an uninitialized pop_context"))
17+
ref (fun _ -> E.s (E.bug "You called an uninitialized pop_context"))
18+
19+
(* Set to true while parsing a typedef declaration's declarator list, so that
20+
declarator_no_init calls add_type (not add_identifier) for typedef names. *)
21+
let is_typedef_decl : bool ref = ref false
1822

1923

2024
(* Keep here the current pattern for formatparse *)

test/small1/typedef_varname.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ typedef struct rax {
2121
} rax;
2222

2323
typedef int mytype;
24+
typedef int T;
2425

2526
int main() {
2627
rax *rax = malloc(sizeof(*rax)); /* variable rax shadows typedef rax in initializer */
@@ -39,5 +40,15 @@ int main() {
3940
if (b != sizeof(int)) E(4);
4041
}
4142

43+
/* Chain initialization: after "T T = 1", T is registered as a variable by the
44+
lexer hack, so the subsequent initializer "U = T + 1" sees T as a variable
45+
(value 1), not as a type name. This fails to parse without the fix because
46+
"T + 1" would be a syntax error when T is a NAMED_TYPE. */
47+
{
48+
T T = 1, U = T + 1;
49+
if (T != 1) E(5);
50+
if (U != 2) E(6);
51+
}
52+
4253
SUCCESS;
4354
}

test/small1/typedef_varname_noinit.c

Lines changed: 0 additions & 48 deletions
This file was deleted.

test/testcil.pl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,6 @@ sub addToGroup {
472472
addTest("testrun/void _GNUCC=1");
473473
addTest("test/voidtypedef ");
474474
addTest("testrun/typedef_varname ");
475-
addTest("testrun/typedef_varname_noinit ");
476475
addTest("testrun/wrongnumargs ");
477476
addBadComment("testrun/wrongnumargs",
478477
"Notbug. Should fail since we don't pad argument lists");

0 commit comments

Comments
 (0)