Skip to content

Commit bf45bca

Browse files
committed
CHB: add printing support for gcc attributes
1 parent a918c00 commit bf45bca

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

CodeHawk/CHB/bchlib/bCHBCTypePretty.ml

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,87 @@ let constant_to_string (c: bconstant_t) =
165165
| _ -> cil_constant_to_string c
166166

167167

168-
let attributes_to_string attrs =
168+
let rec attributes_to_string (attrs: b_attributes_t) =
169169
match attrs with
170170
| [] -> ""
171-
| l ->
171+
| _ ->
172172
(String.concat
173-
"," (List.map (fun attr -> match attr with Attr (s, _) -> s) l)) ^ " "
173+
"\n"
174+
(List.map
175+
(fun attr ->
176+
match attr with
177+
| Attr (s, params) ->
178+
"__attribute__((__" ^ s ^ "__,"
179+
^ (String.concat
180+
", "
181+
(List.map b_attrparam_to_string params))
182+
^ "__))") attrs))
183+
184+
and b_attrparam_to_string (param: b_attrparam_t) =
185+
match param with
186+
| AInt i -> string_of_int i
187+
| AStr s -> s
188+
| ACons (s, []) -> "__" ^ s ^ "__"
189+
| ACons (s, params) ->
190+
"__"
191+
^ s
192+
^ "__("
193+
^ (String.concat ", " (List.map b_attrparam_to_string params)) ^ ")"
194+
| ASizeOf t -> "__sizeof__(" ^ (typ_to_string t) ^ ")"
195+
| ASizeOfE p -> "__sizeofE__(" ^ (b_attrparam_to_string p) ^ ")"
196+
| ASizeOfS ts -> "__sizeofS__(" ^ (btypsig_to_string ts) ^ ")"
197+
| AAlignOf t -> "__alignof__(" ^ (typ_to_string t) ^ ")"
198+
| AAlignOfE p -> "__alignofE__(" ^ (b_attrparam_to_string p) ^ ")"
199+
| AAlignOfS ts -> "__alignofS__(" ^ (btypsig_to_string ts) ^ ")"
200+
| AUnOp (unop, p) ->
201+
(unop_to_print_string unop) ^ " " ^ (b_attrparam_to_string p)
202+
| ABinOp (binop, p1, p2) ->
203+
(b_attrparam_to_string p1)
204+
^ " "
205+
^ (binop_to_print_string binop)
206+
^ " "
207+
^ (b_attrparam_to_string p2)
208+
| ADot (p, s) -> (b_attrparam_to_string p) ^ "." ^ s
209+
| AStar p -> "*" ^ (b_attrparam_to_string p)
210+
| AAddrOf p -> "&" ^ (b_attrparam_to_string p)
211+
| AIndex (p1, p2) ->
212+
(b_attrparam_to_string p1) ^ "[" ^ (b_attrparam_to_string p2) ^ "]"
213+
| AQuestion (p1, p2, p3) ->
214+
(b_attrparam_to_string p1)
215+
^ " ? "
216+
^ (b_attrparam_to_string p2)
217+
^ " : "
218+
^ (b_attrparam_to_string p3)
219+
| AAssign (p1, p2) ->
220+
(b_attrparam_to_string p1) ^ " = " ^ (b_attrparam_to_string p2)
221+
222+
223+
and btypsig_to_string (ts: btypsig_t) =
224+
let s_attrs (attrs: b_attributes_t) =
225+
match attrs with
226+
| [] -> ""
227+
| _ -> " " ^ (attributes_to_string attrs) in
228+
match ts with
229+
| TSArray (tts, Some i64, attrs) ->
230+
(btypsig_to_string tts) ^ "[" ^ (Int64.to_string i64) ^ "]" ^ (s_attrs attrs)
231+
| TSArray (tts, None, attrs) ->
232+
(btypsig_to_string tts) ^ "[]" ^ (s_attrs attrs)
233+
| TSPtr (tts, attrs) ->
234+
"( " ^ (btypsig_to_string tts) ^ " *)" ^ (s_attrs attrs)
235+
| TSComp (is_struct, name, attrs) ->
236+
(if is_struct then "struct " else "union ") ^ name ^ (s_attrs attrs)
237+
| TSFun (rts, Some tslist, _is_vararg, attrs) ->
238+
(btypsig_to_string rts)
239+
^ " ("
240+
^ (String.concat ", " (List.map btypsig_to_string tslist))
241+
^ (s_attrs attrs)
242+
| TSFun (rts, None, _is_vararg, attrs) ->
243+
(btypsig_to_string rts) ^ "(?)" ^ (s_attrs attrs)
244+
| TSEnum (name, attrs) -> "enum " ^ name ^ (s_attrs attrs)
245+
| TSBase t -> typ_to_string t
174246

175247

176-
let rec typ_to_string (t: btype_t) =
248+
and typ_to_string (t: btype_t) =
177249
let ns_to_string ns =
178250
String.concat "" (List.map (fun n -> (tname_to_string n) ^ "::") ns) in
179251
let a = attributes_to_string in

CodeHawk/CHB/bchlib/bCHBCTypePretty.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ open BCHBCTypes
3636

3737
val attributes_to_string: b_attributes_t -> string
3838

39+
val b_attrparam_to_string: b_attrparam_t -> string
40+
3941
val location_line_to_string: b_location_t -> string
4042

4143
val storage_to_string: bstorage_t -> string

0 commit comments

Comments
 (0)