2 * Copyright (C) 2010, University of Copenhagen DIKU and INRIA.
3 * Copyright (C) 2006, 2007 Julia Lawall
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License (GPL)
7 * version 2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * file license.txt for more details.
14 * This file was part of Coccinelle.
18 (*****************************************************************************)
19 (* mostly a copy paste of parsing_cocci/pretty_print_cocci.ml
20 * todo?: try to factorize ?
22 (*****************************************************************************)
24 module Ast
= Ast_cocci
26 let term s
= Ast.unwrap_mcode s
28 (* or perhaps can have in plus, for instance a Disj, but those Disj must be
29 * handled by interactive tool (by proposing alternatives)
31 exception CantBeInPlus
33 (*****************************************************************************)
35 type pos
= Before
| After
| InPlace
36 type nlhint
= StartBox
| EndBox
| SpaceOrNewline
of string ref
41 (env
, pr
, pr_celem
, pr_cspace
, pr_space
, pr_arity
, pr_barrier
,
43 generating xxs before
=
45 (* Just to be able to copy paste the code from pretty_print_cocci.ml. *)
46 let print_string s line lcol
=
47 let rcol = if lcol
= unknown then unknown else lcol
+ (String.length s
) in
48 pr s line lcol
rcol None
in
49 let print_string_with_hint hint s line lcol
=
50 let rcol = if lcol
= unknown then unknown else lcol
+ (String.length s
) in
51 pr s line lcol
rcol (Some hint
) in
52 let print_text s
= pr s
unknown unknown unknown None
in
53 let close_box _
= () in
54 let force_newline _
= print_text "\n" in
56 let start_block () = force_newline(); indent
() in
57 let end_block () = unindent
true; force_newline () in
58 let print_string_box s
= print_string s
in
60 let print_option = Common.do_option
in
61 let print_option_prespace fn
= function
63 | Some x
-> pr_space
(); fn x
in
64 let print_option_space fn
= function
66 | Some x
-> fn x
; pr_space
() in
67 let print_between = Common.print_between in
69 let outdent _
= () (* should go to leftmost col, does nothing now *) in
72 Pretty_print_c.mk_pretty_printers pr_celem pr_cspace
73 force_newline indent
outdent (function _
-> unindent
true) in
75 (* --------------------------------------------------------------------- *)
76 (* Only for make_hrule, print plus code, unbound metavariables *)
78 (* avoid polyvariance problems *)
79 let anything : (Ast.anything -> unit) ref = ref (function _
-> ()) in
81 let rec print_anything = function
85 print_between force_newline print_anything_list stream
;
88 and print_anything_list
= function
91 | bef
::((aft
::_
) as rest
) ->
95 Ast.Rule_elemTag
(_
) | Ast.AssignOpTag
(_
) | Ast.BinaryOpTag
(_
)
96 | Ast.ArithOpTag
(_
) | Ast.LogicalOpTag
(_
)
97 | Ast.Token
("if",_
) | Ast.Token
("while",_
) -> true | _
-> false) or
99 Ast.Rule_elemTag
(_
) | Ast.AssignOpTag
(_
) | Ast.BinaryOpTag
(_
)
100 | Ast.ArithOpTag
(_
) | Ast.LogicalOpTag
(_
) | Ast.Token
("{",_
) -> true
102 if space then pr_space
();
103 print_anything_list rest
in
105 let print_around printer
term = function
106 Ast.NOTHING
-> printer
term
107 | Ast.BEFORE
(bef
,_
) -> print_anything bef
; printer
term
108 | Ast.AFTER
(aft
,_
) -> printer
term; print_anything aft
109 | Ast.BEFOREAFTER
(bef
,aft
,_
) ->
110 print_anything bef
; printer
term; print_anything aft
in
112 let print_string_befaft fn fn1 x info
=
114 function Ast.Noindent s
| Ast.Indent s
-> print_string s ln col
in
116 (function (s
,ln
,col
) -> fn1
(); print ln col s
; force_newline())
120 (function (s
,ln
,col
) -> force_newline(); fn1
(); print ln col s
)
122 let print_meta (r
,x
) = print_text x
in
124 let print_pos = function
125 Ast.MetaPos
(name
,_
,_
,_
,_
) ->
126 let name = Ast.unwrap_mcode
name in
127 print_text "@"; print_meta name
130 (* --------------------------------------------------------------------- *)
132 let mcode fn
(s
,info
,mc
,pos
) =
133 let line = info
.Ast.line in
134 let lcol = info
.Ast.column
in
135 match (generating
,mc
) with
137 (* printing for transformation *)
138 (* Here we don't care about the annotation on s. *)
139 let print_comments lb comments
=
141 (function line_before
->
142 function (str
,line,col
) ->
143 match line_before
with
147 Ast.Noindent s
-> unindent
false; s
148 | Ast.Indent s
-> s
in
149 print_string str line col
; Some
line
150 | Some lb
when line =|= lb
->
151 let str = match str with Ast.Noindent s
| Ast.Indent s
-> s
in
152 print_string str line col
; Some
line
155 (* not super elegant to put side-effecting unindent in a let
159 Ast.Noindent s
-> unindent
false; s
160 | Ast.Indent s
-> s
in
161 print_string str line col
; Some
line)
163 let line_before = print_comments None info
.Ast.strbef
in
164 (match line_before with
166 | Some lb
when lb
=|= info
.Ast.line -> ()
167 | _
-> force_newline());
169 let _ = print_comments (Some info
.Ast.line) info
.Ast.straft
in
170 (* newline after a pragma
171 should really store parsed versions of the strings, but make a cheap
173 print_comments takes care of interior newlines *)
175 (* printing for rule generation *)
176 | (true, Ast.MINUS
(_,_,_,plus_stream
)) ->
179 fn s
line lcol; print_pos pos
;
180 print_anything plus_stream
181 | (true, Ast.CONTEXT
(_,plus_streams
)) ->
182 let fn s
= force_newline(); fn s
line lcol; print_pos pos
in
183 print_around fn s plus_streams
184 | (true,Ast.PLUS
Ast.ONE
) ->
186 force_newline(); print_text "+ "; fn s
line lcol; print_pos pos
in
187 print_string_befaft fn (function _ -> print_text "+ ") s info
188 | (true,Ast.PLUS
Ast.MANY
) ->
190 force_newline(); print_text "++ "; fn s
line lcol; print_pos pos
in
191 print_string_befaft fn (function _ -> print_text "++ ") s info
195 (* --------------------------------------------------------------------- *)
197 let handle_metavar name fn =
198 let ((_,b
) as s
,info
,mc
,pos
) = name in
199 let line = info
.Ast.line in
200 let lcol = info
.Ast.column
in
201 match Common.optionise
(fun () -> List.assoc s env
) with
203 let name_string (_,s
) = s
in
206 mcode (function _ -> print_string (name_string s
)) name
209 (Printf.sprintf
"SP line %d: Not found a value in env for: %s"
210 line (name_string s
))
212 pr_barrier
line lcol;
215 (* call mcode to preserve the -+ annotation *)
216 mcode (fun _ _ _ -> fn e
) name
219 if lcol = unknown then unknown else lcol + (String.length b
) in
222 (* --------------------------------------------------------------------- *)
223 let dots between
fn d
=
224 match Ast.unwrap d
with
225 Ast.DOTS
(l
) -> print_between between
fn l
226 | Ast.CIRCLES
(l
) -> print_between between
fn l
227 | Ast.STARS
(l
) -> print_between between
fn l
230 let nest_dots starter ender
fn f d
=
231 mcode print_string starter
;
233 (match Ast.unwrap d
with
234 Ast.DOTS
(l
) -> print_between force_newline fn l
235 | Ast.CIRCLES
(l
) -> print_between force_newline fn l
236 | Ast.STARS
(l
) -> print_between force_newline fn l
);
238 mcode print_string ender
241 let print_disj_list fn l
=
243 print_between (function _ -> print_text "\n|\n") fn l
;
244 print_text "\n)\n" in
246 (* --------------------------------------------------------------------- *)
250 match Ast.unwrap i
with
251 Ast.Id
(name) -> mcode print_string name
252 | Ast.MetaId
(name,_,_,_) ->
253 handle_metavar name (function
254 | (Ast_c.MetaIdVal
(id
,_)) -> print_text id
255 | _ -> raise Impossible
257 | Ast.MetaFunc
(name,_,_,_) ->
258 handle_metavar name (function
259 | (Ast_c.MetaFuncVal id
) -> print_text id
260 | _ -> raise Impossible
262 | Ast.MetaLocalFunc
(name,_,_,_) ->
263 handle_metavar name (function
264 | (Ast_c.MetaLocalFuncVal id
) -> print_text id
265 | _ -> raise Impossible
268 | Ast.DisjId
(id_list
) ->
270 then print_disj_list ident id_list
271 else raise CantBeInPlus
272 | Ast.OptIdent
(_) | Ast.UniqueIdent
(_) ->
277 (* --------------------------------------------------------------------- *)
280 let rec expression e
=
281 match Ast.unwrap e
with
282 Ast.Ident
(id
) -> ident id
283 | Ast.Constant
(const
) -> mcode constant const
284 | Ast.FunCall
(fn,lp
,args
,rp
) ->
285 expression fn; mcode (print_string_with_hint StartBox
) lp
;
286 dots (function _ -> ()) arg_expression args
;
287 mcode (print_string_with_hint EndBox
) rp
288 | Ast.Assignment
(left
,op
,right
,_) ->
289 expression left
; pr_space
(); mcode assignOp op
;
290 pr_space
(); expression right
291 | Ast.CondExpr
(exp1
,why
,exp2
,colon
,exp3
) ->
292 expression exp1
; pr_space
(); mcode print_string why
;
293 print_option (function e
-> pr_space
(); expression e
) exp2
;
294 pr_space
(); mcode print_string colon
; pr_space
(); expression exp3
295 | Ast.Postfix
(exp
,op
) -> expression exp
; mcode fixOp op
296 | Ast.Infix
(exp
,op
) -> mcode fixOp op
; expression exp
297 | Ast.Unary
(exp
,op
) -> mcode unaryOp op
; expression exp
298 | Ast.Binary
(left
,op
,right
) ->
299 expression left
; pr_space
(); mcode binaryOp op
; pr_space
();
301 | Ast.Nested
(left
,op
,right
) -> failwith
"nested only in minus code"
302 | Ast.Paren
(lp
,exp
,rp
) ->
303 mcode print_string_box lp
; expression exp
; close_box();
304 mcode print_string rp
305 | Ast.ArrayAccess
(exp1
,lb
,exp2
,rb
) ->
306 expression exp1
; mcode print_string_box lb
; expression exp2
; close_box();
307 mcode print_string rb
308 | Ast.RecordAccess
(exp
,pt
,field
) ->
309 expression exp
; mcode print_string pt
; ident field
310 | Ast.RecordPtAccess
(exp
,ar
,field
) ->
311 expression exp
; mcode print_string ar
; ident field
312 | Ast.Cast
(lp
,ty
,rp
,exp
) ->
313 mcode print_string_box lp
; fullType ty
; close_box();
314 mcode print_string rp
; expression exp
315 | Ast.SizeOfExpr
(sizeof
,exp
) ->
316 mcode print_string sizeof
; expression exp
317 | Ast.SizeOfType
(sizeof
,lp
,ty
,rp
) ->
318 mcode print_string sizeof
;
319 mcode print_string_box lp
; fullType ty
; close_box();
320 mcode print_string rp
321 | Ast.TypeExp
(ty
) -> fullType ty
323 | Ast.MetaErr
(name,_,_,_) ->
324 failwith
"metaErr not handled"
326 | Ast.MetaExpr
(name,_,_,_typedontcare
,_formdontcare
,_) ->
327 handle_metavar name (function
328 | Ast_c.MetaExprVal
(exp
,_) ->
329 pretty_print_c.Pretty_print_c.expression exp
330 | _ -> raise Impossible
333 | Ast.MetaExprList
(name,_,_,_) ->
334 handle_metavar name (function
335 | Ast_c.MetaExprListVal args
->
336 pretty_print_c.Pretty_print_c.arg_list args
337 | Ast_c.MetaParamListVal
_ ->
338 failwith
"have meta param list matching meta exp list\n";
339 | _ -> raise Impossible
342 | Ast.EComma
(cm
) -> mcode print_string cm
344 | Ast.DisjExpr
(exp_list
) ->
346 then print_disj_list expression exp_list
347 else raise CantBeInPlus
348 | Ast.NestExpr
(starter
,expr_dots
,ender
,Some whencode
,multi
)
350 nest_dots starter ender
expression
351 (function _ -> print_text " when != "; expression whencode
)
353 | Ast.NestExpr
(starter
,expr_dots
,ender
,None
,multi
) when generating
->
354 nest_dots starter ender
expression (function _ -> ()) expr_dots
355 | Ast.NestExpr
_ -> raise CantBeInPlus
356 | Ast.Edots
(dots,Some whencode
)
357 | Ast.Ecircles
(dots,Some whencode
)
358 | Ast.Estars
(dots,Some whencode
) ->
361 (mcode print_string dots;
362 print_text " when != ";
364 else raise CantBeInPlus
365 | Ast.Edots
(dots,None
)
366 | Ast.Ecircles
(dots,None
)
367 | Ast.Estars
(dots,None
) ->
369 then mcode print_string dots
370 else raise CantBeInPlus
372 | Ast.OptExp
(exp
) | Ast.UniqueExp
(exp
) ->
375 and arg_expression e
=
376 match Ast.unwrap e
with
378 (* space is only used by add_newline, and only if not using SMPL
379 spacing. pr_cspace uses a " " in unparse_c.ml. Not so nice... *)
380 mcode (print_string_with_hint (SpaceOrNewline
(ref " "))) cm
383 and unaryOp
= function
384 Ast.GetRef
-> print_string "&"
385 | Ast.DeRef
-> print_string "*"
386 | Ast.UnPlus
-> print_string "+"
387 | Ast.UnMinus
-> print_string "-"
388 | Ast.Tilde
-> print_string "~"
389 | Ast.Not
-> print_string "!"
391 and assignOp
= function
392 Ast.SimpleAssign
-> print_string "="
393 | Ast.OpAssign
(aop
) ->
394 (function line -> function lcol ->
395 arithOp aop
line lcol; print_string "=" line lcol)
398 Ast.Dec
-> print_string "--"
399 | Ast.Inc
-> print_string "++"
401 and binaryOp
= function
402 Ast.Arith
(aop
) -> arithOp aop
403 | Ast.Logical
(lop
) -> logicalOp lop
405 and arithOp
= function
406 Ast.Plus
-> print_string "+"
407 | Ast.Minus
-> print_string "-"
408 | Ast.Mul
-> print_string "*"
409 | Ast.Div
-> print_string "/"
410 | Ast.Mod
-> print_string "%"
411 | Ast.DecLeft
-> print_string "<<"
412 | Ast.DecRight
-> print_string ">>"
413 | Ast.And
-> print_string "&"
414 | Ast.Or
-> print_string "|"
415 | Ast.Xor
-> print_string "^"
417 and logicalOp
= function
418 Ast.Inf
-> print_string "<"
419 | Ast.Sup
-> print_string ">"
420 | Ast.InfEq
-> print_string "<="
421 | Ast.SupEq
-> print_string ">="
422 | Ast.Eq
-> print_string "=="
423 | Ast.NotEq
-> print_string "!="
424 | Ast.AndLog
-> print_string "&&"
425 | Ast.OrLog
-> print_string "||"
427 and constant
= function
428 Ast.String
(s
) -> print_string ("\""^s^
"\"")
429 | Ast.Char
(s
) -> print_string ("\'"^s^
"\'")
430 | Ast.Int
(s
) -> print_string s
431 | Ast.Float
(s
) -> print_string s
433 (* --------------------------------------------------------------------- *)
438 match Ast.unwrap ft
with
439 Ast.Type
(cv
,ty
) -> print_option_space (mcode const_vol
) cv
; typeC ty
440 | Ast.DisjType
_ -> failwith
"can't be in plus"
441 | Ast.OptType
(_) | Ast.UniqueType
(_) ->
444 and print_function_pointer
(ty
,lp1
,star
,rp1
,lp2
,params
,rp2
) fn =
445 fullType ty
; mcode print_string lp1
; mcode print_string star
; fn();
446 mcode print_string rp1
; mcode print_string lp1
;
447 parameter_list params
; mcode print_string rp2
449 and print_function_type
(ty
,lp1
,params
,rp1
) fn =
450 print_option fullType ty
; fn(); mcode print_string lp1
;
451 parameter_list params
; mcode print_string rp1
454 match Ast.unwrap ty
with
455 Ast.BaseType
(ty
,strings
) ->
456 print_between pr_space
(mcode print_string) strings
457 | Ast.SignedT
(sgn
,ty
) -> mcode sign sgn
; print_option_prespace typeC ty
458 | Ast.Pointer
(ty
,star
) -> fullType ty
; ft_space ty
; mcode print_string star
459 | Ast.FunctionPointer
(ty
,lp1
,star
,rp1
,lp2
,params
,rp2
) ->
460 print_function_pointer
(ty
,lp1
,star
,rp1
,lp2
,params
,rp2
)
462 | Ast.FunctionType
(am
,ty
,lp1
,params
,rp1
) ->
463 print_function_type
(ty
,lp1
,params
,rp1
) (function _ -> ())
464 | Ast.Array
(ty
,lb
,size
,rb
) ->
465 fullType ty
; mcode print_string lb
; print_option expression size
;
466 mcode print_string rb
467 | Ast.EnumName
(kind
,name) ->
468 mcode print_string kind
;
469 print_option_prespace ident name
470 | Ast.EnumDef
(ty
,lb
,ids
,rb
) ->
471 fullType ty
; ft_space ty
;
472 mcode print_string lb
;
473 dots force_newline expression ids
;
474 mcode print_string rb
475 | Ast.StructUnionName
(kind
,name) ->
476 mcode structUnion kind
; print_option_prespace ident name
477 | Ast.StructUnionDef
(ty
,lb
,decls
,rb
) ->
478 fullType ty
; ft_space ty
;
479 mcode print_string lb
;
480 dots force_newline declaration decls
;
481 mcode print_string rb
482 | Ast.TypeName
(name)-> mcode print_string name
483 | Ast.MetaType
(name,_,_) ->
484 handle_metavar name (function
485 Ast_c.MetaTypeVal exp
->
486 pretty_print_c.Pretty_print_c.ty exp
487 | _ -> raise Impossible
)
489 and baseType
= function
490 Ast.VoidType
-> print_string "void"
491 | Ast.CharType
-> print_string "char"
492 | Ast.ShortType
-> print_string "short"
493 | Ast.IntType
-> print_string "int"
494 | Ast.DoubleType
-> print_string "double"
495 | Ast.FloatType
-> print_string "float"
496 | Ast.LongType
-> print_string "long"
497 | Ast.LongLongType
-> print_string "long long"
498 | Ast.SizeType
-> print_string "size_t "
499 | Ast.SSizeType
-> print_string "ssize_t "
500 | Ast.PtrDiffType
-> print_string "ptrdiff_t "
502 and structUnion
= function
503 Ast.Struct
-> print_string "struct"
504 | Ast.Union
-> print_string "union"
507 Ast.Signed
-> print_string "signed"
508 | Ast.Unsigned
-> print_string "unsigned"
511 and const_vol
= function
512 Ast.Const
-> print_string "const"
513 | Ast.Volatile
-> print_string "volatile"
515 (* --------------------------------------------------------------------- *)
516 (* Function declaration *)
518 and storage
= function
519 Ast.Static
-> print_string "static"
520 | Ast.Auto
-> print_string "auto"
521 | Ast.Register
-> print_string "register"
522 | Ast.Extern
-> print_string "extern"
524 (* --------------------------------------------------------------------- *)
525 (* Variable declaration *)
527 and print_named_type ty id
=
528 match Ast.unwrap ty
with
529 Ast.Type
(None
,ty1
) ->
530 (match Ast.unwrap ty1
with
531 Ast.FunctionPointer
(ty
,lp1
,star
,rp1
,lp2
,params
,rp2
) ->
532 print_function_pointer
(ty
,lp1
,star
,rp1
,lp2
,params
,rp2
)
533 (function _ -> pr_space
(); ident id
)
534 | Ast.FunctionType
(am
,ty
,lp1
,params
,rp1
) ->
535 print_function_type
(ty
,lp1
,params
,rp1
)
536 (function _ -> pr_space
(); ident id
)
537 | Ast.Array
(_,_,_,_) ->
539 match Ast.unwrap ty
with
540 Ast.Array
(ty
,lb
,size
,rb
) ->
541 (match Ast.unwrap ty
with
546 mcode print_string lb
;
547 print_option expression size
;
548 mcode print_string rb
)
549 | _ -> failwith
"complex array types not supported")
550 | _ -> typeC ty
; ty_space ty
; ident id
; k
() in
551 loop ty1
(function _ -> ())
552 (*| should have a case here for pointer to array or function type
553 that would put ( * ) around the variable. This makes one wonder
554 why we really need a special case for function pointer *)
555 | _ -> fullType ty
; ft_space ty
; ident id
)
556 | _ -> fullType ty
; ft_space ty
; ident id
559 match Ast.unwrap ty
with
560 Ast.Pointer
(_,_) -> ()
564 match Ast.unwrap ty
with
566 (match Ast.unwrap ty
with
567 Ast.Pointer
(_,_) -> ()
568 | Ast.MetaType
(name,_,_) ->
569 (match List.assoc
(Ast.unwrap_mcode
name) env
with
570 Ast_c.MetaTypeVal
(tq
,ty
) ->
571 (match Ast_c.unwrap ty
with
572 Ast_c.Pointer
(_,_) -> ()
579 match Ast.unwrap d
with
580 Ast.MetaDecl
(name,_,_) ->
583 Ast_c.MetaDeclVal d
->
584 pretty_print_c.Pretty_print_c.decl d
585 | _ -> raise Impossible
)
586 | Ast.MetaField
(name,_,_) ->
589 Ast_c.MetaFieldVal d
->
590 pretty_print_c.Pretty_print_c.field d
591 | _ -> raise Impossible
)
592 | Ast.Init
(stg
,ty
,id
,eq
,ini
,sem
) ->
593 print_option (mcode storage
) stg
;
594 print_option (function _ -> pr_space
()) stg
;
595 print_named_type ty id
;
596 pr_space
(); mcode print_string eq
;
597 pr_space
(); initialiser
true ini
; mcode print_string sem
598 | Ast.UnInit
(stg
,ty
,id
,sem
) ->
599 print_option (mcode storage
) stg
;
600 print_option (function _ -> pr_space
()) stg
;
601 print_named_type ty id
;
602 mcode print_string sem
603 | Ast.MacroDecl
(name,lp
,args
,rp
,sem
) ->
604 ident name; mcode print_string_box lp
;
605 dots (function _ -> ()) expression args
;
606 close_box(); mcode print_string rp
; mcode print_string sem
607 | Ast.TyDecl
(ty
,sem
) -> fullType ty
; mcode print_string sem
608 | Ast.Typedef
(stg
,ty
,id
,sem
) ->
609 mcode print_string stg
;
610 fullType ty
; typeC id
;
611 mcode print_string sem
612 | Ast.DisjDecl
(_) -> raise CantBeInPlus
613 | Ast.Ddots
(_,_) -> raise CantBeInPlus
614 | Ast.OptDecl
(decl
) | Ast.UniqueDecl
(decl
) ->
617 (* --------------------------------------------------------------------- *)
620 and initialiser nlcomma i
=
621 match Ast.unwrap i
with
622 Ast.MetaInit
(name,_,_) ->
623 handle_metavar name (function
624 Ast_c.MetaInitVal ini
->
625 pretty_print_c.Pretty_print_c.init ini
626 | _ -> raise Impossible
)
627 | Ast.InitExpr
(exp
) -> expression exp
628 | Ast.ArInitList
(lb
,initlist
,rb
) ->
629 (match Ast.undots initlist
with
630 [] -> mcode print_string lb
; mcode print_string rb
632 mcode print_string lb
; start_block();
633 dots force_newline (initialiser
false) initlist
;
634 end_block(); mcode print_string rb
)
635 | Ast.StrInitList
(_,lb
,[],rb
,[]) ->
636 mcode print_string lb
; mcode print_string rb
637 | Ast.StrInitList
(_,lb
,initlist
,rb
,[]) ->
638 mcode print_string lb
; start_block();
639 (* awkward, because the comma is separate from the initialiser *)
640 let rec loop = function
642 | [x
] -> initialiser
false x
643 | x
::xs
-> initialiser nlcomma x
; loop xs
in
645 end_block(); mcode print_string rb
646 | Ast.StrInitList
(_,lb
,initlist
,rb
,_) ->
647 failwith
"unexpected whencode in plus"
648 | Ast.InitGccExt
(designators
,eq
,ini
) ->
649 List.iter designator designators
; pr_space
();
650 mcode print_string eq
; pr_space
(); initialiser nlcomma ini
651 | Ast.InitGccName
(name,eq
,ini
) ->
652 ident name; mcode print_string eq
; initialiser nlcomma ini
653 | Ast.IComma
(comma
) ->
654 mcode print_string comma
;
655 if nlcomma
then force_newline()
656 | Ast.Idots
(dots,Some whencode
) ->
659 (mcode print_string dots;
660 print_text " when != ";
661 initialiser nlcomma whencode
)
662 else raise CantBeInPlus
663 | Ast.Idots
(dots,None
) ->
665 then mcode print_string dots
666 else raise CantBeInPlus
667 | Ast.OptIni
(ini
) | Ast.UniqueIni
(ini
) ->
670 and designator
= function
671 Ast.DesignatorField
(dot
,id
) -> mcode print_string dot
; ident id
672 | Ast.DesignatorIndex
(lb
,exp
,rb
) ->
673 mcode print_string lb
; expression exp
; mcode print_string rb
674 | Ast.DesignatorRange
(lb
,min
,dots,max
,rb
) ->
675 mcode print_string lb
; expression min
; mcode print_string dots;
676 expression max
; mcode print_string rb
678 (* --------------------------------------------------------------------- *)
681 and parameterTypeDef p
=
682 match Ast.unwrap p
with
683 Ast.VoidParam
(ty
) -> fullType ty
684 | Ast.Param
(ty
,Some id
) -> print_named_type ty id
685 | Ast.Param
(ty
,None
) -> fullType ty
687 | Ast.MetaParam
(name,_,_) ->
690 Ast_c.MetaParamVal p
->
691 pretty_print_c.Pretty_print_c.param p
692 | _ -> raise Impossible
)
693 | Ast.MetaParamList
(name,_,_,_) ->
694 failwith
"not handling MetaParamList"
696 | Ast.PComma
(cm
) -> mcode print_string cm
697 | Ast.Pdots
(dots) | Ast.Pcircles
(dots) when generating
->
698 mcode print_string dots
699 | Ast.Pdots
(dots) | Ast.Pcircles
(dots) -> raise CantBeInPlus
700 | Ast.OptParam
(param
) | Ast.UniqueParam
(param
) -> raise CantBeInPlus
702 and parameter_list l
=
705 match Ast.unwrap p
with
706 Ast.PComma
(cm
) -> pr_space
()
708 dots (function _ -> ()) comma l
712 (* --------------------------------------------------------------------- *)
715 let rec inc_file = function
717 print_string ("\""^
(String.concat
"/" (List.map inc_elem elems
))^
"\"")
718 | Ast.NonLocal
(elems
) ->
719 print_string ("<"^
(String.concat
"/" (List.map inc_elem elems
))^
">")
721 and inc_elem
= function
723 | Ast.IncDots
-> "..."
725 (* --------------------------------------------------------------------- *)
728 and rule_elem arity re
=
729 match Ast.unwrap re
with
730 Ast.FunHeader
(_,_,fninfo
,name,lp
,params
,rp
) ->
731 pr_arity arity
; List.iter print_fninfo fninfo
;
732 ident name; mcode print_string_box lp
;
733 parameter_list params
; close_box(); mcode print_string rp
;
735 | Ast.Decl
(_,_,decl
) -> pr_arity arity
; declaration decl
737 | Ast.SeqStart
(brace
) ->
738 pr_arity arity
; mcode print_string brace
; start_block()
739 | Ast.SeqEnd
(brace
) ->
740 end_block(); pr_arity arity
; mcode print_string brace
742 | Ast.ExprStatement
(exp
,sem
) ->
743 pr_arity arity
; expression exp
; mcode print_string sem
745 | Ast.IfHeader
(iff
,lp
,exp
,rp
) ->
747 mcode print_string iff
; pr_space
(); mcode print_string_box lp
;
748 expression exp
; close_box(); mcode print_string rp
750 pr_arity arity
; mcode print_string els
752 | Ast.WhileHeader
(whl
,lp
,exp
,rp
) ->
754 mcode print_string whl
; pr_space
(); mcode print_string_box lp
;
755 expression exp
; close_box(); mcode print_string rp
757 pr_arity arity
; mcode print_string d
758 | Ast.WhileTail
(whl
,lp
,exp
,rp
,sem
) ->
760 mcode print_string whl
; pr_space
(); mcode print_string_box lp
;
761 expression exp
; close_box(); mcode print_string rp
;
762 mcode print_string sem
763 | Ast.ForHeader
(fr
,lp
,e1
,sem1
,e2
,sem2
,e3
,rp
) ->
765 mcode print_string fr
; mcode print_string_box lp
;
766 print_option expression e1
; mcode print_string sem1
;
767 print_option expression e2
; mcode print_string sem2
;
768 print_option expression e3
; close_box();
769 mcode print_string rp
770 | Ast.IteratorHeader
(nm
,lp
,args
,rp
) ->
772 ident nm
; pr_space
(); mcode print_string_box lp
;
773 dots (function _ -> ()) expression args
; close_box();
774 mcode print_string rp
776 | Ast.SwitchHeader
(switch
,lp
,exp
,rp
) ->
778 mcode print_string switch
; pr_space
(); mcode print_string_box lp
;
779 expression exp
; close_box(); mcode print_string rp
781 | Ast.Break
(br
,sem
) ->
782 pr_arity arity
; mcode print_string br
; mcode print_string sem
783 | Ast.Continue
(cont
,sem
) ->
784 pr_arity arity
; mcode print_string cont
; mcode print_string sem
785 | Ast.Label
(l
,dd
) -> ident l
; mcode print_string dd
786 | Ast.Goto
(goto
,l
,sem
) ->
787 mcode print_string goto
; ident l
; mcode print_string sem
788 | Ast.Return
(ret
,sem
) ->
789 pr_arity arity
; mcode print_string ret
;
790 mcode print_string sem
791 | Ast.ReturnExpr
(ret
,exp
,sem
) ->
792 pr_arity arity
; mcode print_string ret
; pr_space
();
793 expression exp
; mcode print_string sem
795 | Ast.Exp
(exp
) -> pr_arity arity
; expression exp
796 | Ast.TopExp
(exp
) -> pr_arity arity
; expression exp
797 | Ast.Ty
(ty
) -> pr_arity arity
; fullType ty
798 | Ast.TopInit
(init
) -> initialiser
false init
799 | Ast.Include
(inc
,s
) ->
800 mcode print_string inc
; print_text " "; mcode inc_file s
801 | Ast.Undef
(def
,id
) ->
802 mcode print_string def
; pr_space
(); ident id
803 | Ast.DefineHeader
(def
,id
,params
) ->
804 mcode print_string def
; pr_space
(); ident id
;
805 print_define_parameters params
806 | Ast.Default
(def
,colon
) ->
807 mcode print_string def
; mcode print_string colon
; pr_space
()
808 | Ast.Case
(case
,exp
,colon
) ->
809 mcode print_string case
; pr_space
(); expression exp
;
810 mcode print_string colon
; pr_space
()
811 | Ast.DisjRuleElem
(res
) ->
814 (pr_arity arity
; print_text "\n(\n";
815 print_between (function _ -> print_text "\n|\n") (rule_elem arity
)
818 else raise CantBeInPlus
820 | Ast.MetaRuleElem
(name,_,_) ->
823 | Ast.MetaStmt
(name,_,_,_) ->
824 handle_metavar name (function
825 | Ast_c.MetaStmtVal stm
->
826 pretty_print_c.Pretty_print_c.statement stm
827 | _ -> raise Impossible
829 | Ast.MetaStmtList
(name,_,_) ->
831 "MetaStmtList not supported (not even in ast_c metavars binding)"
833 and print_define_parameters params
=
834 match Ast.unwrap params
with
836 | Ast.DParams
(lp
,params
,rp
) ->
837 mcode print_string lp
;
838 dots (function _ -> ()) print_define_param params
; mcode print_string rp
840 and print_define_param param
=
841 match Ast.unwrap param
with
842 Ast.DParam
(id
) -> ident id
843 | Ast.DPComma
(comma) -> mcode print_string comma
844 | Ast.DPdots
(dots) -> mcode print_string dots
845 | Ast.DPcircles
(circles
) -> mcode print_string circles
846 | Ast.OptDParam
(dp
) -> print_text "?"; print_define_param dp
847 | Ast.UniqueDParam
(dp
) -> print_text "!"; print_define_param dp
849 and print_fninfo
= function
850 Ast.FStorage
(stg
) -> mcode storage stg
851 | Ast.FType
(ty
) -> fullType ty
852 | Ast.FInline
(inline
) -> mcode print_string inline
; pr_space
()
853 | Ast.FAttr
(attr
) -> mcode print_string attr
; pr_space
() in
855 let indent_if_needed s f
=
856 match Ast.unwrap s
with
857 Ast.Seq
(lbrace
,body
,rbrace
) -> pr_space
(); f
()
859 (*no newline at the end - someone else will do that*)
860 start_block(); f
(); unindent
true in
862 let rec statement arity s
=
863 match Ast.unwrap s
with
864 Ast.Seq
(lbrace
,body
,rbrace
) ->
865 rule_elem arity lbrace
;
866 dots force_newline (statement arity
) body
;
867 rule_elem arity rbrace
869 | Ast.IfThen
(header
,branch
,_) ->
870 rule_elem arity header
;
871 indent_if_needed branch
(function _ -> statement arity branch
)
872 | Ast.IfThenElse
(header
,branch1
,els
,branch2
,_) ->
873 rule_elem arity header
;
874 indent_if_needed branch1
(function _ -> statement arity branch1
);
877 indent_if_needed branch2
(function _ -> statement arity branch2
)
878 | Ast.While
(header
,body
,_) ->
879 rule_elem arity header
;
880 indent_if_needed body
(function _ -> statement arity body
)
881 | Ast.Do
(header
,body
,tail
) ->
882 rule_elem arity header
;
883 indent_if_needed body
(function _ -> statement arity body
);
885 | Ast.For
(header
,body
,_) ->
886 rule_elem arity header
;
887 indent_if_needed body
(function _ -> statement arity body
)
888 | Ast.Iterator
(header
,body
,(_,_,_,aft
)) ->
889 rule_elem arity header
;
890 indent_if_needed body
(function _ -> statement arity body
);
891 mcode (fun _ _ _ -> ()) ((),Ast.no_info
,aft
,Ast.NoMetaPos
)
893 | Ast.Switch
(header
,lb
,decls
,cases
,rb
) ->
894 rule_elem arity header
; pr_space
(); rule_elem arity lb
;
895 dots force_newline (statement arity
) decls
;
896 List.iter
(function x
-> case_line arity x
; force_newline()) cases
;
899 | Ast.Atomic
(re
) -> rule_elem arity re
901 | Ast.FunDecl
(header
,lbrace
,body
,rbrace
) ->
902 rule_elem arity header
; rule_elem arity lbrace
;
903 dots force_newline (statement arity
) body
; rule_elem arity rbrace
905 | Ast.Define
(header
,body
) ->
906 rule_elem arity header
; pr_space
();
907 dots force_newline (statement arity
) body
909 | Ast.Disj
([stmt_dots
]) ->
913 dots force_newline (statement arity
) stmt_dots
)
914 else raise CantBeInPlus
915 | Ast.Disj
(stmt_dots_list
) -> (* ignores newline directive for readability *)
918 (pr_arity arity
; print_text "\n(\n";
919 print_between (function _ -> print_text "\n|\n")
920 (dots force_newline (statement arity
))
923 else raise CantBeInPlus
924 | Ast.Nest
(starter
,stmt_dots
,ender
,whn
,multi
,_,_) when generating
->
926 nest_dots starter ender
(statement arity
)
928 print_between force_newline
929 (whencode
(dots force_newline (statement "")) (statement "")) whn
;
932 | Ast.Nest
(_) -> raise CantBeInPlus
933 | Ast.Dots
(d
,whn
,_,_) | Ast.Circles
(d
,whn
,_,_) | Ast.Stars
(d
,whn
,_,_) ->
936 (pr_arity arity
; mcode print_string d
;
937 print_between force_newline
938 (whencode
(dots force_newline (statement "")) (statement "")) whn
;
940 else raise CantBeInPlus
942 | Ast.OptStm
(s
) | Ast.UniqueStm
(s
) ->
945 and whencode notfn alwaysfn
= function
947 print_text " WHEN != "; notfn a
948 | Ast.WhenAlways a
->
949 print_text " WHEN = "; alwaysfn a
950 | Ast.WhenModifier x
-> print_text " WHEN "; print_when_modif x
951 | Ast.WhenNotTrue a
->
952 print_text " WHEN != TRUE "; rule_elem
"" a
953 | Ast.WhenNotFalse a
->
954 print_text " WHEN != FALSE "; rule_elem
"" a
956 and print_when_modif
= function
957 | Ast.WhenAny
-> print_text "ANY"
958 | Ast.WhenStrict
-> print_text "STRICT"
959 | Ast.WhenForall
-> print_text "FORALL"
960 | Ast.WhenExists
-> print_text "EXISTS"
962 and case_line arity c
=
963 match Ast.unwrap c
with
964 Ast.CaseLine
(header
,code
) ->
965 rule_elem arity header
; pr_space
();
966 dots force_newline (statement arity
) code
967 | Ast.OptCase
(case
) -> raise CantBeInPlus
in
970 match Ast.unwrap t
with
971 Ast.FILEINFO
(old_file
,new_file
) -> raise CantBeInPlus
972 | Ast.DECL
(stmt
) -> statement "" stmt
973 | Ast.CODE
(stmt_dots
) -> dots force_newline (statement "") stmt_dots
974 | Ast.ERRORWORDS
(exps
) -> raise CantBeInPlus
979 print_between (function _ -> force_newline(); force_newline()) top_level
983 let if_open_brace = function "{" -> true | _ -> false in
985 (* boolean result indicates whether an indent is needed *)
986 let rec pp_any = function
987 (* assert: normally there is only CONTEXT NOTHING tokens in any *)
988 Ast.FullTypeTag
(x
) -> fullType x
; false
989 | Ast.BaseTypeTag
(x
) -> baseType x
unknown unknown; false
990 | Ast.StructUnionTag
(x
) -> structUnion x
unknown unknown; false
991 | Ast.SignTag
(x
) -> sign x
unknown unknown; false
993 | Ast.IdentTag
(x
) -> ident x
; false
995 | Ast.ExpressionTag
(x
) -> expression x
; false
997 | Ast.ConstantTag
(x
) -> constant x
unknown unknown; false
998 | Ast.UnaryOpTag
(x
) -> unaryOp x
unknown unknown; false
999 | Ast.AssignOpTag
(x
) -> assignOp x
unknown unknown; false
1000 | Ast.FixOpTag
(x
) -> fixOp x
unknown unknown; false
1001 | Ast.BinaryOpTag
(x
) -> binaryOp x
unknown unknown; false
1002 | Ast.ArithOpTag
(x
) -> arithOp x
unknown unknown; false
1003 | Ast.LogicalOpTag
(x
) -> logicalOp x
unknown unknown; false
1005 | Ast.InitTag
(x
) -> initialiser
false x
; false
1006 | Ast.DeclarationTag
(x
) -> declaration x
; false
1008 | Ast.StorageTag
(x
) -> storage x
unknown unknown; false
1009 | Ast.IncFileTag
(x
) -> inc_file x
unknown unknown; false
1011 | Ast.Rule_elemTag
(x
) -> rule_elem
"" x
; false
1012 | Ast.StatementTag
(x
) -> statement "" x
; false
1013 | Ast.CaseLineTag
(x
) -> case_line
"" x
; false
1015 | Ast.ConstVolTag
(x
) -> const_vol x
unknown unknown; false
1017 let print = function Ast.Noindent s
| Ast.Indent s
-> print_text s
in
1018 print_between force_newline print xs
; false
1019 | Ast.Token
(x
,None
) -> print_text x
; if_open_brace x
1020 | Ast.Token
(x
,Some info
) ->
1024 "else" -> force_newline()
1026 print_string x
line lcol)
1027 (let nomcodekind = Ast.CONTEXT
(Ast.DontCarePos
,Ast.NOTHING
) in
1028 (x
,info
,nomcodekind,Ast.NoMetaPos
));
1031 | Ast.Code
(x
) -> let _ = top_level x
in false
1033 (* this is not '...', but a list of expr/statement/params, and
1034 normally there should be no '...' inside them *)
1035 | Ast.ExprDotsTag
(x
) -> dots (function _ -> ()) expression x
; false
1036 | Ast.ParamDotsTag
(x
) -> parameter_list x
; false
1037 | Ast.StmtDotsTag
(x
) -> dots force_newline (statement "") x
; false
1038 | Ast.DeclDotsTag
(x
) -> dots force_newline declaration x
; false
1040 | Ast.TypeCTag
(x
) -> typeC x
; false
1041 | Ast.ParamTag
(x
) -> parameterTypeDef x
; false
1042 | Ast.SgrepStartTag
(x
) -> failwith
"unexpected start tag"
1043 | Ast.SgrepEndTag
(x
) -> failwith
"unexpected end tag"
1046 anything := (function x
-> let _ = pp_any x
in ());
1048 (* todo? imitate what is in pretty_print_cocci ? *)
1052 (* for many tags, we must not do a newline before the first '+' *)
1054 match Ast.unwrap s
with Ast.FunDecl
_ -> true | _ -> false in
1055 let unindent_before = function
1056 (* need to get unindent before newline for } *)
1057 (Ast.Token
("}",_)::_) -> true
1060 (if unindent_before x
then unindent
true);
1062 let newline_before _ =
1065 let hd = List.hd xxs
in
1067 (Ast.StatementTag s
::_) when isfn s
->
1068 force_newline(); force_newline()
1070 | (Ast.Rule_elemTag
_::_) | (Ast.StatementTag
_::_)
1071 | (Ast.InitTag
_::_)
1072 | (Ast.DeclarationTag
_::_) | (Ast.Token
("}",_)::_) -> prnl hd
1074 let newline_after _ =
1075 if before
=*= Before
1077 match List.rev
(List.hd(List.rev xxs
)) with
1078 (Ast.StatementTag s
::_) ->
1079 (if isfn s
then force_newline());
1082 | (Ast.Rule_elemTag
_::_) | (Ast.InitTag
_::_)
1083 | (Ast.DeclarationTag
_::_) | (Ast.Token
("{",_)::_) ->
1086 (* print a newline at the beginning, if needed *)
1088 (* print a newline before each of the rest *)
1089 let rec loop leading_newline indent_needed
= function
1094 match (indent_needed
,unindent_before x
) with
1095 (true,true) -> force_newline()
1096 | (true,false) -> force_newline(); indent
()
1097 | (false,true) -> unindent
true; force_newline()
1098 | (false,false) -> force_newline());
1099 let space_needed_before = function
1101 (match Ast.unwrap x
with
1102 Ast.PComma
_ -> false
1104 | Ast.ExpressionTag
(x
) ->
1105 (match Ast.unwrap x
with
1106 Ast.EComma
_ -> false
1109 (match Ast.unwrap x
with
1110 Ast.IComma
_ -> false
1112 | Ast.Token
(t
,_) when List.mem t
[",";";";"(";")"] -> false
1114 let space_needed_after = function
1115 Ast.Token
(t
,_) when List.mem t
["("] -> (*never needed*) false
1116 | Ast.Token
(t
,_) when List.mem t
["if";"for";"while";"do"] ->
1117 (* space always needed *)
1119 | Ast.ExpressionTag
(e
) ->
1120 (match Ast.unwrap e
with
1122 (* space always needed *)
1127 let rec loop space_after
indent_needed = function
1130 (if space_after
&& space_needed_before x
1132 let indent_needed = pp_any x
in
1133 let space_after = space_needed_after x
in
1134 loop space_after indent_needed xs
in
1135 loop false false x
in
1136 loop true indent_needed xs
in
1137 loop false false (x
::xs
);
1138 (* print a newline at the end, if needed *)
1141 let rec pp_list_list_any (envs
, pr
, pr_celem
, pr_cspace
, pr_space
, pr_arity
,
1142 pr_barrier
, indent
, unindent
)
1143 generating xxs before
=
1146 do_all (env
, pr
, pr_celem
, pr_cspace
, pr_space
, pr_arity
, pr_barrier
,
1148 generating xxs before
)