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 (* --------------------------------------------------------------------- *)
245 match Ast.unwrap i
with
246 Ast.Id
(name) -> mcode print_string name
247 | Ast.MetaId
(name,_,_,_) ->
248 handle_metavar name (function
249 | (Ast_c.MetaIdVal
(id
,_)) -> print_text id
250 | _ -> raise Impossible
252 | Ast.MetaFunc
(name,_,_,_) ->
253 handle_metavar name (function
254 | (Ast_c.MetaFuncVal id
) -> print_text id
255 | _ -> raise Impossible
257 | Ast.MetaLocalFunc
(name,_,_,_) ->
258 handle_metavar name (function
259 | (Ast_c.MetaLocalFuncVal id
) -> print_text id
260 | _ -> raise Impossible
263 | Ast.OptIdent
(_) | Ast.UniqueIdent
(_) ->
268 (* --------------------------------------------------------------------- *)
271 let print_disj_list fn l
=
273 print_between (function _ -> print_text "\n|\n") fn l
;
274 print_text "\n)\n" in
276 let rec expression e
=
277 match Ast.unwrap e
with
278 Ast.Ident
(id
) -> ident id
279 | Ast.Constant
(const
) -> mcode constant const
280 | Ast.FunCall
(fn,lp
,args
,rp
) ->
281 expression fn; mcode (print_string_with_hint StartBox
) lp
;
282 dots (function _ -> ()) arg_expression args
;
283 mcode (print_string_with_hint EndBox
) rp
284 | Ast.Assignment
(left
,op
,right
,_) ->
285 expression left
; pr_space
(); mcode assignOp op
;
286 pr_space
(); expression right
287 | Ast.CondExpr
(exp1
,why
,exp2
,colon
,exp3
) ->
288 expression exp1
; pr_space
(); mcode print_string why
;
289 print_option (function e
-> pr_space
(); expression e
) exp2
;
290 pr_space
(); mcode print_string colon
; pr_space
(); expression exp3
291 | Ast.Postfix
(exp
,op
) -> expression exp
; mcode fixOp op
292 | Ast.Infix
(exp
,op
) -> mcode fixOp op
; expression exp
293 | Ast.Unary
(exp
,op
) -> mcode unaryOp op
; expression exp
294 | Ast.Binary
(left
,op
,right
) ->
295 expression left
; pr_space
(); mcode binaryOp op
; pr_space
();
297 | Ast.Nested
(left
,op
,right
) -> failwith
"nested only in minus code"
298 | Ast.Paren
(lp
,exp
,rp
) ->
299 mcode print_string_box lp
; expression exp
; close_box();
300 mcode print_string rp
301 | Ast.ArrayAccess
(exp1
,lb
,exp2
,rb
) ->
302 expression exp1
; mcode print_string_box lb
; expression exp2
; close_box();
303 mcode print_string rb
304 | Ast.RecordAccess
(exp
,pt
,field
) ->
305 expression exp
; mcode print_string pt
; ident field
306 | Ast.RecordPtAccess
(exp
,ar
,field
) ->
307 expression exp
; mcode print_string ar
; ident field
308 | Ast.Cast
(lp
,ty
,rp
,exp
) ->
309 mcode print_string_box lp
; fullType ty
; close_box();
310 mcode print_string rp
; expression exp
311 | Ast.SizeOfExpr
(sizeof
,exp
) ->
312 mcode print_string sizeof
; expression exp
313 | Ast.SizeOfType
(sizeof
,lp
,ty
,rp
) ->
314 mcode print_string sizeof
;
315 mcode print_string_box lp
; fullType ty
; close_box();
316 mcode print_string rp
317 | Ast.TypeExp
(ty
) -> fullType ty
319 | Ast.MetaErr
(name,_,_,_) ->
320 failwith
"metaErr not handled"
322 | Ast.MetaExpr
(name,_,_,_typedontcare
,_formdontcare
,_) ->
323 handle_metavar name (function
324 | Ast_c.MetaExprVal
(exp
,_) ->
325 pretty_print_c.Pretty_print_c.expression exp
326 | _ -> raise Impossible
329 | Ast.MetaExprList
(name,_,_,_) ->
330 handle_metavar name (function
331 | Ast_c.MetaExprListVal args
->
332 pretty_print_c.Pretty_print_c.arg_list args
333 | Ast_c.MetaParamListVal
_ ->
334 failwith
"have meta param list matching meta exp list\n";
335 | _ -> raise Impossible
338 | Ast.EComma
(cm
) -> mcode print_string cm
340 | Ast.DisjExpr
(exp_list
) ->
342 then print_disj_list expression exp_list
343 else raise CantBeInPlus
344 | Ast.NestExpr
(starter
,expr_dots
,ender
,Some whencode
,multi
)
346 nest_dots starter ender
expression
347 (function _ -> print_text " when != "; expression whencode
)
349 | Ast.NestExpr
(starter
,expr_dots
,ender
,None
,multi
) when generating
->
350 nest_dots starter ender
expression (function _ -> ()) expr_dots
351 | Ast.NestExpr
_ -> raise CantBeInPlus
352 | Ast.Edots
(dots,Some whencode
)
353 | Ast.Ecircles
(dots,Some whencode
)
354 | Ast.Estars
(dots,Some whencode
) ->
357 (mcode print_string dots;
358 print_text " when != ";
360 else raise CantBeInPlus
361 | Ast.Edots
(dots,None
)
362 | Ast.Ecircles
(dots,None
)
363 | Ast.Estars
(dots,None
) ->
365 then mcode print_string dots
366 else raise CantBeInPlus
368 | Ast.OptExp
(exp
) | Ast.UniqueExp
(exp
) ->
371 and arg_expression e
=
372 match Ast.unwrap e
with
374 (* space is only used by add_newline, and only if not using SMPL
375 spacing. pr_cspace uses a " " in unparse_c.ml. Not so nice... *)
376 mcode (print_string_with_hint (SpaceOrNewline
(ref " "))) cm
379 and unaryOp
= function
380 Ast.GetRef
-> print_string "&"
381 | Ast.DeRef
-> print_string "*"
382 | Ast.UnPlus
-> print_string "+"
383 | Ast.UnMinus
-> print_string "-"
384 | Ast.Tilde
-> print_string "~"
385 | Ast.Not
-> print_string "!"
387 and assignOp
= function
388 Ast.SimpleAssign
-> print_string "="
389 | Ast.OpAssign
(aop
) ->
390 (function line -> function lcol ->
391 arithOp aop
line lcol; print_string "=" line lcol)
394 Ast.Dec
-> print_string "--"
395 | Ast.Inc
-> print_string "++"
397 and binaryOp
= function
398 Ast.Arith
(aop
) -> arithOp aop
399 | Ast.Logical
(lop
) -> logicalOp lop
401 and arithOp
= function
402 Ast.Plus
-> print_string "+"
403 | Ast.Minus
-> print_string "-"
404 | Ast.Mul
-> print_string "*"
405 | Ast.Div
-> print_string "/"
406 | Ast.Mod
-> print_string "%"
407 | Ast.DecLeft
-> print_string "<<"
408 | Ast.DecRight
-> print_string ">>"
409 | Ast.And
-> print_string "&"
410 | Ast.Or
-> print_string "|"
411 | Ast.Xor
-> print_string "^"
413 and logicalOp
= function
414 Ast.Inf
-> print_string "<"
415 | Ast.Sup
-> print_string ">"
416 | Ast.InfEq
-> print_string "<="
417 | Ast.SupEq
-> print_string ">="
418 | Ast.Eq
-> print_string "=="
419 | Ast.NotEq
-> print_string "!="
420 | Ast.AndLog
-> print_string "&&"
421 | Ast.OrLog
-> print_string "||"
423 and constant
= function
424 Ast.String
(s
) -> print_string ("\""^s^
"\"")
425 | Ast.Char
(s
) -> print_string ("\'"^s^
"\'")
426 | Ast.Int
(s
) -> print_string s
427 | Ast.Float
(s
) -> print_string s
429 (* --------------------------------------------------------------------- *)
434 match Ast.unwrap ft
with
435 Ast.Type
(cv
,ty
) -> print_option_space (mcode const_vol
) cv
; typeC ty
436 | Ast.DisjType
_ -> failwith
"can't be in plus"
437 | Ast.OptType
(_) | Ast.UniqueType
(_) ->
440 and print_function_pointer
(ty
,lp1
,star
,rp1
,lp2
,params
,rp2
) fn =
441 fullType ty
; mcode print_string lp1
; mcode print_string star
; fn();
442 mcode print_string rp1
; mcode print_string lp1
;
443 parameter_list params
; mcode print_string rp2
445 and print_function_type
(ty
,lp1
,params
,rp1
) fn =
446 print_option fullType ty
; fn(); mcode print_string lp1
;
447 parameter_list params
; mcode print_string rp1
450 match Ast.unwrap ty
with
451 Ast.BaseType
(ty
,strings
) ->
452 print_between pr_space
(mcode print_string) strings
453 | Ast.SignedT
(sgn
,ty
) -> mcode sign sgn
; print_option_prespace typeC ty
454 | Ast.Pointer
(ty
,star
) -> fullType ty
; ft_space ty
; mcode print_string star
455 | Ast.FunctionPointer
(ty
,lp1
,star
,rp1
,lp2
,params
,rp2
) ->
456 print_function_pointer
(ty
,lp1
,star
,rp1
,lp2
,params
,rp2
)
458 | Ast.FunctionType
(am
,ty
,lp1
,params
,rp1
) ->
459 print_function_type
(ty
,lp1
,params
,rp1
) (function _ -> ())
460 | Ast.Array
(ty
,lb
,size
,rb
) ->
461 fullType ty
; mcode print_string lb
; print_option expression size
;
462 mcode print_string rb
463 | Ast.EnumName
(kind
,name) -> mcode print_string kind
; pr_space
();
465 | Ast.StructUnionName
(kind
,name) ->
466 mcode structUnion kind
; print_option_prespace ident name
467 | Ast.StructUnionDef
(ty
,lb
,decls
,rb
) ->
468 fullType ty
; ft_space ty
;
469 mcode print_string lb
;
470 dots force_newline declaration decls
;
471 mcode print_string rb
472 | Ast.TypeName
(name)-> mcode print_string name
473 | Ast.MetaType
(name,_,_) ->
474 handle_metavar name (function
475 Ast_c.MetaTypeVal exp
->
476 pretty_print_c.Pretty_print_c.ty exp
477 | _ -> raise Impossible
)
479 and baseType
= function
480 Ast.VoidType
-> print_string "void"
481 | Ast.CharType
-> print_string "char"
482 | Ast.ShortType
-> print_string "short"
483 | Ast.IntType
-> print_string "int"
484 | Ast.DoubleType
-> print_string "double"
485 | Ast.FloatType
-> print_string "float"
486 | Ast.LongType
-> print_string "long"
487 | Ast.LongLongType
-> print_string "long long"
489 and structUnion
= function
490 Ast.Struct
-> print_string "struct"
491 | Ast.Union
-> print_string "union"
494 Ast.Signed
-> print_string "signed"
495 | Ast.Unsigned
-> print_string "unsigned"
498 and const_vol
= function
499 Ast.Const
-> print_string "const"
500 | Ast.Volatile
-> print_string "volatile"
502 (* --------------------------------------------------------------------- *)
503 (* Function declaration *)
505 and storage
= function
506 Ast.Static
-> print_string "static"
507 | Ast.Auto
-> print_string "auto"
508 | Ast.Register
-> print_string "register"
509 | Ast.Extern
-> print_string "extern"
511 (* --------------------------------------------------------------------- *)
512 (* Variable declaration *)
514 and print_named_type ty id
=
515 match Ast.unwrap ty
with
516 Ast.Type
(None
,ty1
) ->
517 (match Ast.unwrap ty1
with
518 Ast.FunctionPointer
(ty
,lp1
,star
,rp1
,lp2
,params
,rp2
) ->
519 print_function_pointer
(ty
,lp1
,star
,rp1
,lp2
,params
,rp2
)
520 (function _ -> pr_space
(); ident id
)
521 | Ast.FunctionType
(am
,ty
,lp1
,params
,rp1
) ->
522 print_function_type
(ty
,lp1
,params
,rp1
)
523 (function _ -> pr_space
(); ident id
)
524 | Ast.Array
(_,_,_,_) ->
526 match Ast.unwrap ty
with
527 Ast.Array
(ty
,lb
,size
,rb
) ->
528 (match Ast.unwrap ty
with
533 mcode print_string lb
;
534 print_option expression size
;
535 mcode print_string rb
)
536 | _ -> failwith
"complex array types not supported")
537 | _ -> typeC ty
; ty_space ty
; ident id
; k
() in
538 loop ty1
(function _ -> ())
539 (*| should have a case here for pointer to array or function type
540 that would put ( * ) around the variable. This makes one wonder
541 why we really need a special case for function pointer *)
542 | _ -> fullType ty
; ft_space ty
; ident id
)
543 | _ -> fullType ty
; ft_space ty
; ident id
546 match Ast.unwrap ty
with
547 Ast.Pointer
(_,_) -> ()
551 match Ast.unwrap ty
with
553 (match Ast.unwrap ty
with
554 Ast.Pointer
(_,_) -> ()
555 | Ast.MetaType
(name,_,_) ->
556 (match List.assoc
(Ast.unwrap_mcode
name) env
with
557 Ast_c.MetaTypeVal
(tq
,ty
) ->
558 (match Ast_c.unwrap ty
with
559 Ast_c.Pointer
(_,_) -> ()
566 match Ast.unwrap d
with
567 Ast.Init
(stg
,ty
,id
,eq
,ini
,sem
) ->
568 print_option (mcode storage
) stg
;
569 print_option (function _ -> pr_space
()) stg
;
570 print_named_type ty id
;
571 pr_space
(); mcode print_string eq
;
572 pr_space
(); initialiser
true ini
; mcode print_string sem
573 | Ast.UnInit
(stg
,ty
,id
,sem
) ->
574 print_option (mcode storage
) stg
;
575 print_option (function _ -> pr_space
()) stg
;
576 print_named_type ty id
;
577 mcode print_string sem
578 | Ast.MacroDecl
(name,lp
,args
,rp
,sem
) ->
579 ident name; mcode print_string_box lp
;
580 dots (function _ -> ()) expression args
;
581 close_box(); mcode print_string rp
; mcode print_string sem
582 | Ast.TyDecl
(ty
,sem
) -> fullType ty
; mcode print_string sem
583 | Ast.Typedef
(stg
,ty
,id
,sem
) ->
584 mcode print_string stg
;
585 fullType ty
; typeC id
;
586 mcode print_string sem
587 | Ast.DisjDecl
(_) | Ast.MetaDecl
(_,_,_) -> raise CantBeInPlus
588 | Ast.Ddots
(_,_) -> raise CantBeInPlus
589 | Ast.OptDecl
(decl
) | Ast.UniqueDecl
(decl
) ->
592 (* --------------------------------------------------------------------- *)
595 and initialiser nlcomma i
=
596 match Ast.unwrap i
with
597 Ast.MetaInit
(name,_,_) ->
598 handle_metavar name (function
599 Ast_c.MetaInitVal ini
->
600 pretty_print_c.Pretty_print_c.init ini
601 | _ -> raise Impossible
)
602 | Ast.InitExpr
(exp
) -> expression exp
603 | Ast.InitList
(_,lb
,initlist
,rb
,[]) ->
604 mcode print_string lb
; start_block();
605 (* awkward, because the comma is separate from the initialiser *)
606 let rec loop = function
608 | [x
] -> initialiser
false x
609 | x
::xs
-> initialiser nlcomma x
; loop xs
in
611 end_block(); mcode print_string rb
612 | Ast.InitList
(_,lb
,initlist
,rb
,_) -> failwith
"unexpected whencode in plus"
613 | Ast.InitGccExt
(designators
,eq
,ini
) ->
614 List.iter designator designators
; pr_space
();
615 mcode print_string eq
; pr_space
(); initialiser nlcomma ini
616 | Ast.InitGccName
(name,eq
,ini
) ->
617 ident name; mcode print_string eq
; initialiser nlcomma ini
618 | Ast.IComma
(comma
) ->
619 mcode print_string comma
;
620 if nlcomma
then force_newline()
621 | Ast.OptIni
(ini
) | Ast.UniqueIni
(ini
) ->
624 and designator
= function
625 Ast.DesignatorField
(dot
,id
) -> mcode print_string dot
; ident id
626 | Ast.DesignatorIndex
(lb
,exp
,rb
) ->
627 mcode print_string lb
; expression exp
; mcode print_string rb
628 | Ast.DesignatorRange
(lb
,min
,dots,max
,rb
) ->
629 mcode print_string lb
; expression min
; mcode print_string dots;
630 expression max
; mcode print_string rb
632 (* --------------------------------------------------------------------- *)
635 and parameterTypeDef p
=
636 match Ast.unwrap p
with
637 Ast.VoidParam
(ty
) -> fullType ty
638 | Ast.Param
(ty
,Some id
) -> print_named_type ty id
639 | Ast.Param
(ty
,None
) -> fullType ty
641 | Ast.MetaParam
(name,_,_) ->
644 Ast_c.MetaParamVal p
->
645 pretty_print_c.Pretty_print_c.param p
646 | _ -> raise Impossible
)
647 | Ast.MetaParamList
(name,_,_,_) ->
648 failwith
"not handling MetaParamList"
650 | Ast.PComma
(cm
) -> mcode print_string cm
651 | Ast.Pdots
(dots) | Ast.Pcircles
(dots) when generating
->
652 mcode print_string dots
653 | Ast.Pdots
(dots) | Ast.Pcircles
(dots) -> raise CantBeInPlus
654 | Ast.OptParam
(param
) | Ast.UniqueParam
(param
) -> raise CantBeInPlus
656 and parameter_list l
=
659 match Ast.unwrap p
with
660 Ast.PComma
(cm
) -> pr_space
()
662 dots (function _ -> ()) comma l
666 (* --------------------------------------------------------------------- *)
669 let rec inc_file = function
671 print_string ("\""^
(String.concat
"/" (List.map inc_elem elems
))^
"\"")
672 | Ast.NonLocal
(elems
) ->
673 print_string ("<"^
(String.concat
"/" (List.map inc_elem elems
))^
">")
675 and inc_elem
= function
677 | Ast.IncDots
-> "..."
679 (* --------------------------------------------------------------------- *)
682 and rule_elem arity re
=
683 match Ast.unwrap re
with
684 Ast.FunHeader
(_,_,fninfo
,name,lp
,params
,rp
) ->
685 pr_arity arity
; List.iter print_fninfo fninfo
;
686 ident name; mcode print_string_box lp
;
687 parameter_list params
; close_box(); mcode print_string rp
;
689 | Ast.Decl
(_,_,decl
) -> pr_arity arity
; declaration decl
691 | Ast.SeqStart
(brace
) ->
692 pr_arity arity
; mcode print_string brace
; start_block()
693 | Ast.SeqEnd
(brace
) ->
694 end_block(); pr_arity arity
; mcode print_string brace
696 | Ast.ExprStatement
(exp
,sem
) ->
697 pr_arity arity
; expression exp
; mcode print_string sem
699 | Ast.IfHeader
(iff
,lp
,exp
,rp
) ->
701 mcode print_string iff
; pr_space
(); mcode print_string_box lp
;
702 expression exp
; close_box(); mcode print_string rp
704 pr_arity arity
; mcode print_string els
706 | Ast.WhileHeader
(whl
,lp
,exp
,rp
) ->
708 mcode print_string whl
; pr_space
(); mcode print_string_box lp
;
709 expression exp
; close_box(); mcode print_string rp
711 pr_arity arity
; mcode print_string d
712 | Ast.WhileTail
(whl
,lp
,exp
,rp
,sem
) ->
714 mcode print_string whl
; pr_space
(); mcode print_string_box lp
;
715 expression exp
; close_box(); mcode print_string rp
;
716 mcode print_string sem
717 | Ast.ForHeader
(fr
,lp
,e1
,sem1
,e2
,sem2
,e3
,rp
) ->
719 mcode print_string fr
; mcode print_string_box lp
;
720 print_option expression e1
; mcode print_string sem1
;
721 print_option expression e2
; mcode print_string sem2
;
722 print_option expression e3
; close_box();
723 mcode print_string rp
724 | Ast.IteratorHeader
(nm
,lp
,args
,rp
) ->
726 ident nm
; pr_space
(); mcode print_string_box lp
;
727 dots (function _ -> ()) expression args
; close_box();
728 mcode print_string rp
730 | Ast.SwitchHeader
(switch
,lp
,exp
,rp
) ->
732 mcode print_string switch
; pr_space
(); mcode print_string_box lp
;
733 expression exp
; close_box(); mcode print_string rp
735 | Ast.Break
(br
,sem
) ->
736 pr_arity arity
; mcode print_string br
; mcode print_string sem
737 | Ast.Continue
(cont
,sem
) ->
738 pr_arity arity
; mcode print_string cont
; mcode print_string sem
739 | Ast.Label
(l
,dd
) -> ident l
; mcode print_string dd
740 | Ast.Goto
(goto
,l
,sem
) ->
741 mcode print_string goto
; ident l
; mcode print_string sem
742 | Ast.Return
(ret
,sem
) ->
743 pr_arity arity
; mcode print_string ret
;
744 mcode print_string sem
745 | Ast.ReturnExpr
(ret
,exp
,sem
) ->
746 pr_arity arity
; mcode print_string ret
; pr_space
();
747 expression exp
; mcode print_string sem
749 | Ast.Exp
(exp
) -> pr_arity arity
; expression exp
750 | Ast.TopExp
(exp
) -> pr_arity arity
; expression exp
751 | Ast.Ty
(ty
) -> pr_arity arity
; fullType ty
752 | Ast.TopInit
(init
) -> initialiser
false init
753 | Ast.Include
(inc
,s
) ->
754 mcode print_string inc
; print_text " "; mcode inc_file s
755 | Ast.DefineHeader
(def
,id
,params
) ->
756 mcode print_string def
; pr_space
(); ident id
;
757 print_define_parameters params
758 | Ast.Default
(def
,colon
) ->
759 mcode print_string def
; mcode print_string colon
; pr_space
()
760 | Ast.Case
(case
,exp
,colon
) ->
761 mcode print_string case
; pr_space
(); expression exp
;
762 mcode print_string colon
; pr_space
()
763 | Ast.DisjRuleElem
(res
) ->
766 (pr_arity arity
; print_text "\n(\n";
767 print_between (function _ -> print_text "\n|\n") (rule_elem arity
)
770 else raise CantBeInPlus
772 | Ast.MetaRuleElem
(name,_,_) ->
775 | Ast.MetaStmt
(name,_,_,_) ->
776 handle_metavar name (function
777 | Ast_c.MetaStmtVal stm
->
778 pretty_print_c.Pretty_print_c.statement stm
779 | _ -> raise Impossible
781 | Ast.MetaStmtList
(name,_,_) ->
783 "MetaStmtList not supported (not even in ast_c metavars binding)"
785 and print_define_parameters params
=
786 match Ast.unwrap params
with
788 | Ast.DParams
(lp
,params
,rp
) ->
789 mcode print_string lp
;
790 dots (function _ -> ()) print_define_param params
; mcode print_string rp
792 and print_define_param param
=
793 match Ast.unwrap param
with
794 Ast.DParam
(id
) -> ident id
795 | Ast.DPComma
(comma) -> mcode print_string comma
796 | Ast.DPdots
(dots) -> mcode print_string dots
797 | Ast.DPcircles
(circles
) -> mcode print_string circles
798 | Ast.OptDParam
(dp
) -> print_text "?"; print_define_param dp
799 | Ast.UniqueDParam
(dp
) -> print_text "!"; print_define_param dp
801 and print_fninfo
= function
802 Ast.FStorage
(stg
) -> mcode storage stg
803 | Ast.FType
(ty
) -> fullType ty
804 | Ast.FInline
(inline
) -> mcode print_string inline
; pr_space
()
805 | Ast.FAttr
(attr
) -> mcode print_string attr
; pr_space
() in
807 let indent_if_needed s f
=
808 match Ast.unwrap s
with
809 Ast.Seq
(lbrace
,body
,rbrace
) -> pr_space
(); f
()
811 (*no newline at the end - someone else will do that*)
812 start_block(); f
(); unindent
true in
814 let rec statement arity s
=
815 match Ast.unwrap s
with
816 Ast.Seq
(lbrace
,body
,rbrace
) ->
817 rule_elem arity lbrace
;
818 dots force_newline (statement arity
) body
;
819 rule_elem arity rbrace
821 | Ast.IfThen
(header
,branch
,_) ->
822 rule_elem arity header
;
823 indent_if_needed branch
(function _ -> statement arity branch
)
824 | Ast.IfThenElse
(header
,branch1
,els
,branch2
,_) ->
825 rule_elem arity header
;
826 indent_if_needed branch1
(function _ -> statement arity branch1
);
829 indent_if_needed branch2
(function _ -> statement arity branch2
)
830 | Ast.While
(header
,body
,_) ->
831 rule_elem arity header
;
832 indent_if_needed body
(function _ -> statement arity body
)
833 | Ast.Do
(header
,body
,tail
) ->
834 rule_elem arity header
;
835 indent_if_needed body
(function _ -> statement arity body
);
837 | Ast.For
(header
,body
,_) ->
838 rule_elem arity header
;
839 indent_if_needed body
(function _ -> statement arity body
)
840 | Ast.Iterator
(header
,body
,(_,_,_,aft
)) ->
841 rule_elem arity header
;
842 indent_if_needed body
(function _ -> statement arity body
);
843 mcode (fun _ _ _ -> ()) ((),Ast.no_info
,aft
,Ast.NoMetaPos
)
845 | Ast.Switch
(header
,lb
,decls
,cases
,rb
) ->
846 rule_elem arity header
; pr_space
(); rule_elem arity lb
;
847 dots force_newline (statement arity
) decls
;
848 List.iter
(function x
-> case_line arity x
; force_newline()) cases
;
851 | Ast.Atomic
(re
) -> rule_elem arity re
853 | Ast.FunDecl
(header
,lbrace
,body
,rbrace
) ->
854 rule_elem arity header
; rule_elem arity lbrace
;
855 dots force_newline (statement arity
) body
; rule_elem arity rbrace
857 | Ast.Define
(header
,body
) ->
858 rule_elem arity header
; pr_space
();
859 dots force_newline (statement arity
) body
861 | Ast.Disj
([stmt_dots
]) ->
865 dots force_newline (statement arity
) stmt_dots
)
866 else raise CantBeInPlus
867 | Ast.Disj
(stmt_dots_list
) -> (* ignores newline directive for readability *)
870 (pr_arity arity
; print_text "\n(\n";
871 print_between (function _ -> print_text "\n|\n")
872 (dots force_newline (statement arity
))
875 else raise CantBeInPlus
876 | Ast.Nest
(starter
,stmt_dots
,ender
,whn
,multi
,_,_) when generating
->
878 nest_dots starter ender
(statement arity
)
880 print_between force_newline
881 (whencode
(dots force_newline (statement "")) (statement "")) whn
;
884 | Ast.Nest
(_) -> raise CantBeInPlus
885 | Ast.Dots
(d
,whn
,_,_) | Ast.Circles
(d
,whn
,_,_) | Ast.Stars
(d
,whn
,_,_) ->
888 (pr_arity arity
; mcode print_string d
;
889 print_between force_newline
890 (whencode
(dots force_newline (statement "")) (statement "")) whn
;
892 else raise CantBeInPlus
894 | Ast.OptStm
(s
) | Ast.UniqueStm
(s
) ->
897 and whencode notfn alwaysfn
= function
899 print_text " WHEN != "; notfn a
900 | Ast.WhenAlways a
->
901 print_text " WHEN = "; alwaysfn a
902 | Ast.WhenModifier x
-> print_text " WHEN "; print_when_modif x
903 | Ast.WhenNotTrue a
->
904 print_text " WHEN != TRUE "; rule_elem
"" a
905 | Ast.WhenNotFalse a
->
906 print_text " WHEN != FALSE "; rule_elem
"" a
908 and print_when_modif
= function
909 | Ast.WhenAny
-> print_text "ANY"
910 | Ast.WhenStrict
-> print_text "STRICT"
911 | Ast.WhenForall
-> print_text "FORALL"
912 | Ast.WhenExists
-> print_text "EXISTS"
914 and case_line arity c
=
915 match Ast.unwrap c
with
916 Ast.CaseLine
(header
,code
) ->
917 rule_elem arity header
; pr_space
();
918 dots force_newline (statement arity
) code
919 | Ast.OptCase
(case
) -> raise CantBeInPlus
in
922 match Ast.unwrap t
with
923 Ast.FILEINFO
(old_file
,new_file
) -> raise CantBeInPlus
924 | Ast.DECL
(stmt
) -> statement "" stmt
925 | Ast.CODE
(stmt_dots
) -> dots force_newline (statement "") stmt_dots
926 | Ast.ERRORWORDS
(exps
) -> raise CantBeInPlus
931 print_between (function _ -> force_newline(); force_newline()) top_level
935 let if_open_brace = function "{" -> true | _ -> false in
937 (* boolean result indicates whether an indent is needed *)
938 let rec pp_any = function
939 (* assert: normally there is only CONTEXT NOTHING tokens in any *)
940 Ast.FullTypeTag
(x
) -> fullType x
; false
941 | Ast.BaseTypeTag
(x
) -> baseType x
unknown unknown; false
942 | Ast.StructUnionTag
(x
) -> structUnion x
unknown unknown; false
943 | Ast.SignTag
(x
) -> sign x
unknown unknown; false
945 | Ast.IdentTag
(x
) -> ident x
; false
947 | Ast.ExpressionTag
(x
) -> expression x
; false
949 | Ast.ConstantTag
(x
) -> constant x
unknown unknown; false
950 | Ast.UnaryOpTag
(x
) -> unaryOp x
unknown unknown; false
951 | Ast.AssignOpTag
(x
) -> assignOp x
unknown unknown; false
952 | Ast.FixOpTag
(x
) -> fixOp x
unknown unknown; false
953 | Ast.BinaryOpTag
(x
) -> binaryOp x
unknown unknown; false
954 | Ast.ArithOpTag
(x
) -> arithOp x
unknown unknown; false
955 | Ast.LogicalOpTag
(x
) -> logicalOp x
unknown unknown; false
957 | Ast.InitTag
(x
) -> initialiser
false x
; false
958 | Ast.DeclarationTag
(x
) -> declaration x
; false
960 | Ast.StorageTag
(x
) -> storage x
unknown unknown; false
961 | Ast.IncFileTag
(x
) -> inc_file x
unknown unknown; false
963 | Ast.Rule_elemTag
(x
) -> rule_elem
"" x
; false
964 | Ast.StatementTag
(x
) -> statement "" x
; false
965 | Ast.CaseLineTag
(x
) -> case_line
"" x
; false
967 | Ast.ConstVolTag
(x
) -> const_vol x
unknown unknown; false
969 let print = function Ast.Noindent s
| Ast.Indent s
-> print_text s
in
970 print_between force_newline print xs
; false
971 | Ast.Token
(x
,None
) -> print_text x
; if_open_brace x
972 | Ast.Token
(x
,Some info
) ->
976 "else" -> force_newline()
978 print_string x
line lcol)
979 (let nomcodekind = Ast.CONTEXT
(Ast.DontCarePos
,Ast.NOTHING
) in
980 (x
,info
,nomcodekind,Ast.NoMetaPos
));
983 | Ast.Code
(x
) -> let _ = top_level x
in false
985 (* this is not '...', but a list of expr/statement/params, and
986 normally there should be no '...' inside them *)
987 | Ast.ExprDotsTag
(x
) -> dots (function _ -> ()) expression x
; false
988 | Ast.ParamDotsTag
(x
) -> parameter_list x
; false
989 | Ast.StmtDotsTag
(x
) -> dots force_newline (statement "") x
; false
990 | Ast.DeclDotsTag
(x
) -> dots force_newline declaration x
; false
992 | Ast.TypeCTag
(x
) -> typeC x
; false
993 | Ast.ParamTag
(x
) -> parameterTypeDef x
; false
994 | Ast.SgrepStartTag
(x
) -> failwith
"unexpected start tag"
995 | Ast.SgrepEndTag
(x
) -> failwith
"unexpected end tag"
998 anything := (function x
-> let _ = pp_any x
in ());
1000 (* todo? imitate what is in pretty_print_cocci ? *)
1004 (* for many tags, we must not do a newline before the first '+' *)
1006 match Ast.unwrap s
with Ast.FunDecl
_ -> true | _ -> false in
1007 let unindent_before = function
1008 (* need to get unindent before newline for } *)
1009 (Ast.Token
("}",_)::_) -> true
1012 (if unindent_before x
then unindent
true);
1014 let newline_before _ =
1017 let hd = List.hd xxs
in
1019 (Ast.StatementTag s
::_) when isfn s
->
1020 force_newline(); force_newline()
1022 | (Ast.Rule_elemTag
_::_) | (Ast.StatementTag
_::_)
1023 | (Ast.InitTag
_::_)
1024 | (Ast.DeclarationTag
_::_) | (Ast.Token
("}",_)::_) -> prnl hd
1026 let newline_after _ =
1027 if before
=*= Before
1029 match List.rev
(List.hd(List.rev xxs
)) with
1030 (Ast.StatementTag s
::_) ->
1031 (if isfn s
then force_newline());
1034 | (Ast.Rule_elemTag
_::_) | (Ast.InitTag
_::_)
1035 | (Ast.DeclarationTag
_::_) | (Ast.Token
("{",_)::_) ->
1038 (* print a newline at the beginning, if needed *)
1040 (* print a newline before each of the rest *)
1041 let rec loop leading_newline indent_needed
= function
1046 match (indent_needed
,unindent_before x
) with
1047 (true,true) -> force_newline()
1048 | (true,false) -> force_newline(); indent
()
1049 | (false,true) -> unindent
true; force_newline()
1050 | (false,false) -> force_newline());
1051 let space_needed_before = function
1053 (match Ast.unwrap x
with
1054 Ast.PComma
_ -> false
1056 | Ast.ExpressionTag
(x
) ->
1057 (match Ast.unwrap x
with
1058 Ast.EComma
_ -> false
1061 (match Ast.unwrap x
with
1062 Ast.IComma
_ -> false
1064 | Ast.Token
(t
,_) when List.mem t
[",";";";"(";")"] -> false
1066 let space_needed_after = function
1067 Ast.Token
(t
,_) when List.mem t
["("] -> (*never needed*) false
1068 | Ast.Token
(t
,_) when List.mem t
["if";"for";"while";"do"] ->
1069 (* space always needed *)
1071 | Ast.ExpressionTag
(e
) ->
1072 (match Ast.unwrap e
with
1074 (* space always needed *)
1079 let rec loop space_after
indent_needed = function
1082 (if space_after
&& space_needed_before x
1084 let indent_needed = pp_any x
in
1085 let space_after = space_needed_after x
in
1086 loop space_after indent_needed xs
in
1087 loop false false x
in
1088 loop true indent_needed xs
in
1089 loop false false (x
::xs
);
1090 (* print a newline at the end, if needed *)
1093 let rec pp_list_list_any (envs
, pr
, pr_celem
, pr_cspace
, pr_space
, pr_arity
,
1094 pr_barrier
, indent
, unindent
)
1095 generating xxs before
=
1098 do_all (env
, pr
, pr_celem
, pr_cspace
, pr_space
, pr_arity
, pr_barrier
,
1100 generating xxs before
)