@@ -32,6 +32,11 @@ type inline_status =
3232 | Noinline (* The atom is declared with the noinline attribute *)
3333 | Inline (* The atom is declared inline *)
3434
35+ type literal_status =
36+ | No_literal
37+ | String_literal
38+ | Wide_string_literal
39+
3540type atom_info =
3641 { a_storage : C .storage ; (* storage class *)
3742 a_defined : bool ; (* defined in the current comp. unit? *)
@@ -41,6 +46,7 @@ type atom_info =
4146 (* 1 section for data, 3 sections (code/lit/jumptbl) for functions *)
4247 a_access : Sections .access_mode ; (* access mode, e.g. small data area *)
4348 a_inline : inline_status ; (* function declared inline? *)
49+ a_literal : literal_status ; (* is this a string literal? *)
4450 a_loc : location (* source location *)
4551}
4652
@@ -138,6 +144,12 @@ let atom_location a =
138144 with Not_found ->
139145 Cutil. no_loc
140146
147+ let atom_literal a =
148+ try
149+ (Hashtbl. find decl_atom a).a_literal
150+ with Not_found ->
151+ No_literal
152+
141153(* * The current environment of composite definitions *)
142154
143155let comp_env : composite_env ref = ref Maps.PTree. empty
@@ -594,6 +606,15 @@ let is_int64 env ty =
594606(* * String literals *)
595607
596608let stringNum = ref 0 (* number of next global for string literals *)
609+
610+ let rec gensym_string_literal () =
611+ incr stringNum;
612+ let name = Printf. sprintf " __stringlit_%d" ! stringNum in
613+ (* Make sure the name is not already used in the source program *)
614+ if Hashtbl. mem atom_of_string name
615+ then gensym_string_literal ()
616+ else name
617+
597618let stringTable : (string, AST.ident) Hashtbl.t = Hashtbl. create 47
598619let wstringTable : (int64 list * ikind, AST.ident) Hashtbl.t = Hashtbl. create 47
599620
@@ -603,8 +624,7 @@ let name_for_string_literal s =
603624 try
604625 Hashtbl. find stringTable s
605626 with Not_found ->
606- incr stringNum;
607- let name = Printf. sprintf " __stringlit_%d" ! stringNum in
627+ let name = gensym_string_literal () in
608628 let id = intern_string name in
609629 let mergeable = if is_C_string s then 1 else 0 in
610630 Hashtbl. add decl_atom id
@@ -615,6 +635,7 @@ let name_for_string_literal s =
615635 a_sections = [Sections. for_stringlit mergeable];
616636 a_access = Sections. Access_default ;
617637 a_inline = No_specifier ;
638+ a_literal = String_literal ;
618639 a_loc = Cutil. no_loc };
619640 Hashtbl. add stringTable s id;
620641 id
@@ -638,8 +659,7 @@ let name_for_wide_string_literal s ik =
638659 try
639660 Hashtbl. find wstringTable (s, ik)
640661 with Not_found ->
641- incr stringNum;
642- let name = Printf. sprintf " __stringlit_%d" ! stringNum in
662+ let name = gensym_string_literal () in
643663 let id = intern_string name in
644664 let wchar_size = Cutil. sizeof_ikind ik in
645665 let mergeable = if is_C_wide_string s then wchar_size else 0 in
@@ -652,6 +672,7 @@ let name_for_wide_string_literal s ik =
652672 a_sections = [Sections. for_stringlit mergeable];
653673 a_access = Sections. Access_default ;
654674 a_inline = No_specifier ;
675+ a_literal = Wide_string_literal ;
655676 a_loc = Cutil. no_loc };
656677 Hashtbl. add wstringTable (s, ik) id;
657678 id
@@ -1185,6 +1206,7 @@ let convertFundef loc env fd =
11851206 a_sections = Sections. for_function env loc id' fd.fd_attrib;
11861207 a_access = Sections. Access_default ;
11871208 a_inline = inline;
1209+ a_literal = No_literal ;
11881210 a_loc = loc };
11891211 (id', AST. Gfun (Ctypes. Internal
11901212 {fn_return = ret;
@@ -1276,6 +1298,7 @@ let convertGlobvar loc env (sto, id, ty, optinit) =
12761298 a_sections = [section];
12771299 a_access = access;
12781300 a_inline = No_specifier ;
1301+ a_literal = No_literal ;
12791302 a_loc = loc };
12801303 let volatile = List. mem C. AVolatile attr in
12811304 let readonly = List. mem C. AConst attr && not volatile in
0 commit comments