Release coccinelle-0.2.0rc1
[bpt/coccinelle.git] / engine / pretty_print_engine.ml
CommitLineData
34e49164
C
1open Common.Infix
2
3open Lib_engine
4
5
6let pp = Common.pp
7
8let pp_meta (_,x) = pp x
9
10let rec pp_binding_kind = function
11 | Ast_c.MetaIdVal s -> pp ("id " ^ s)
12 | Ast_c.MetaFuncVal s -> pp ("func " ^ s)
13 | Ast_c.MetaLocalFuncVal s -> pp ("localfunc " ^ s)
978fd7e5 14 | Ast_c.MetaExprVal expr -> Pretty_print_c.pp_expression_simple expr
34e49164 15 | Ast_c.MetaExprListVal expr_list -> pp "<<exprlist>>"
113803cf
C
16 | Ast_c.MetaInitVal ini ->
17 Pretty_print_c.pp_init_simple ini
34e49164
C
18 | Ast_c.MetaTypeVal typ ->
19 Pretty_print_c.pp_type_simple typ
20 | Ast_c.MetaStmtVal statement ->
21 Pretty_print_c.pp_statement_simple statement
22 | Ast_c.MetaParamVal params -> pp "<<param>>"
23 | Ast_c.MetaParamListVal params -> pp "<<paramlist>>"
24 | Ast_c.MetaListlenVal n -> pp (string_of_int n)
25 | Ast_c.MetaPosVal (pos1, pos2) ->
26 let print_pos = function
27 Ast_cocci.Real x -> string_of_int x
28 | Ast_cocci.Virt(x,off) -> Printf.sprintf "%d+%d" x off in
29 pp (Common.sprintf ("pos(%s,%s)") (print_pos pos1) (print_pos pos2))
30 | Ast_c.MetaPosValList l ->
31 pp (Common.sprintf ("poss[%s]")
32 (String.concat ", "
33 (List.map
485bce71
C
34 (function (fl,ce,(minl,minc),(maxl,maxc)) ->
35 Printf.sprintf "(%s,%s,(%d,%d),(%d,%d))"
36 fl ce minl minc maxl maxc)
34e49164
C
37 l)))
38
39and pp_binding subst =
40 begin
41 pp "[";
42 Common.print_between (fun () -> pp ";"; Format.print_cut() )
43 (fun ((_,s), kind) -> pp s; pp " --> "; pp_binding_kind kind)
44 subst;
45 pp "]";
46 end
47
48
49let pp_binding_kind2 = function
50 | ParenVal s -> pp "pv("; pp_meta s; pp ")"
51 | NormalMetaVal x -> pp_binding_kind x
7f004419 52 | LabelVal (Absolute xs) ->
34e49164
C
53 begin
54 pp "labelval";
55 pp "(";
56 Common.print_between (fun () -> pp ",") Format.print_int xs;
57 pp ")";
58 end
7f004419
C
59 | LabelVal (Prefix xs) ->
60 begin
61 pp "prefixlabelval";
62 pp "(";
63 Common.print_between (fun () -> pp ",") Format.print_int xs;
64 pp ")";
65 end
34e49164
C
66 | GoodVal -> pp "goodval"
67 | BadVal -> pp "badval"
68
69
70let rec pp_predicate = function
71 | InLoop -> pp "InLoop"
72 | TrueBranch -> pp "TrueBranch"
73 | FalseBranch -> pp "FalseBranch"
74 | After -> pp "After"
75 | FallThrough -> pp "FallThrough"
951c7801 76 | LoopFallThrough -> pp "LoopFallThrough"
34e49164
C
77 | Return -> pp "Return"
78 | FunHeader -> pp "FunHeader"
79 | Top -> pp "Top"
80 | ErrorExit -> pp "ErrorExit"
81 | Exit -> pp "Exit"
82 | Goto -> pp "Goto"
83 | Paren s -> pp "Paren("; pp_meta s; pp ")"
84 | Match (re) -> Pretty_print_cocci.print_rule_elem re
85 | Label s -> pp "Label("; pp_meta s; pp ")"
86 | BCLabel s -> pp "BreakContinueLabel("; pp_meta s; pp ")"
87 | PrefixLabel s -> pp "PrefixLabel("; pp_meta s; pp ")"
88 | BindGood s -> pp "BindGood("; pp_meta s; pp ")"
89 | BindBad s -> pp "BindBad("; pp_meta s; pp ")"
90 | FakeBrace -> pp "FakeBrace"
91
92and pp_binding2 subst =
93 begin
94 pp "[";
95 Common.print_between (fun () -> pp ";";Format.print_cut(); )
96 (fun (s, kind) -> pp s; pp " --> "; pp_binding_kind2 kind)
97 subst;
98 pp "]";
99 end
100
101and pp_binding2_ctlsubst subst =
102 begin
103 pp "[";
104 Common.print_between (fun () -> pp ";"; Format.print_cut(); )
105 (function
106 Ast_ctl.Subst (s, kind) ->
107 pp_meta s; pp " --> "; pp_binding_kind2 kind;
108 | Ast_ctl.NegSubst (s, kind) ->
109 pp_meta s; pp " -/-> "; pp_binding_kind2 kind;
110 )
111 subst;
112 pp "]";
113 end
114
115let predicate_to_string pred =
116 Common.format_to_string (function _ -> pp_predicate pred)
117
118
119let pp_pred_smodif = fun (pred, smodif) ->
120 begin
121 pp_predicate pred;
122(*
123 (match smodif with
124 | Ast_ctl.Modif x | Ast_ctl.UnModif x -> pp " with <modifTODO>"
125 | Ast_ctl.Control -> ()
126 )
127*)
128 end
129
130
131let pp_ctlcocci show_plus inline_let_def ctl =
132 begin
133 if show_plus
134 then begin
135 Pretty_print_cocci.print_plus_flag := true;
136 Pretty_print_cocci.print_minus_flag := true;
137 end
138 else begin
139 Pretty_print_cocci.print_plus_flag := false;
140 Pretty_print_cocci.print_minus_flag := false;
141 end;
142 Common.pp_do_in_box (fun () ->
143 Pretty_print_ctl.pp_ctl (pp_pred_smodif,(fun s -> pp_meta s))
144 inline_let_def ctl;
145 );
146 end
147
148