6f7e64cfaef6d552d836211aed6cc6ad6ef13cfd
[bpt/coccinelle.git] / engine / pretty_print_engine.ml
1 (*
2 * Copyright 2010, INRIA, University of Copenhagen
3 * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
4 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
5 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
6 * This file is part of Coccinelle.
7 *
8 * Coccinelle is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, according to version 2 of the License.
11 *
12 * Coccinelle is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * The authors reserve the right to distribute this or future versions of
21 * Coccinelle under other licenses.
22 *)
23
24
25 open Common.Infix
26
27 open Lib_engine
28
29
30 let pp = Common.pp
31
32 let pp_meta (_,x) = pp x
33
34 let rec pp_binding_kind = function
35 | Ast_c.MetaIdVal (s,_) -> pp ("id " ^ s)
36 | Ast_c.MetaFuncVal s -> pp ("func " ^ s)
37 | Ast_c.MetaLocalFuncVal s -> pp ("localfunc " ^ s)
38 | Ast_c.MetaExprVal (expr,_) -> Pretty_print_c.pp_expression_simple expr
39 | Ast_c.MetaExprListVal expr_list -> pp "<<exprlist>>"
40 | Ast_c.MetaInitVal ini ->
41 Pretty_print_c.pp_init_simple ini
42 | Ast_c.MetaTypeVal typ ->
43 Pretty_print_c.pp_type_simple typ
44 | Ast_c.MetaDeclVal decl ->
45 Pretty_print_c.pp_decl_simple decl
46 | Ast_c.MetaFieldVal decl ->
47 Pretty_print_c.pp_field_simple decl
48 | Ast_c.MetaFieldListVal decls ->
49 List.iter Pretty_print_c.pp_field_simple decls
50 | Ast_c.MetaStmtVal statement ->
51 Pretty_print_c.pp_statement_simple statement
52 | Ast_c.MetaParamVal params -> pp "<<param>>"
53 | Ast_c.MetaParamListVal params -> pp "<<paramlist>>"
54 | Ast_c.MetaListlenVal n -> pp (string_of_int n)
55 | Ast_c.MetaPosVal (pos1, pos2) ->
56 let print_pos = function
57 Ast_cocci.Real x -> string_of_int x
58 | Ast_cocci.Virt(x,off) -> Printf.sprintf "%d+%d" x off in
59 pp (Common.sprintf ("pos(%s,%s)") (print_pos pos1) (print_pos pos2))
60 | Ast_c.MetaPosValList l ->
61 pp (Common.sprintf ("poss[%s]")
62 (String.concat ", "
63 (List.map
64 (function (fl,ce,(minl,minc),(maxl,maxc)) ->
65 Printf.sprintf "(%s,%s,(%d,%d),(%d,%d))"
66 fl ce minl minc maxl maxc)
67 l)))
68
69 and pp_binding subst =
70 begin
71 pp "[";
72 Common.print_between (fun () -> pp ";"; Format.print_cut() )
73 (fun ((r,s), kind) ->
74 pp r; pp "."; pp s; pp " --> "; pp_binding_kind kind)
75 subst;
76 pp "]";
77 end
78
79
80 let pp_binding_kind2 = function
81 | ParenVal s -> pp "pv("; pp_meta s; pp ")"
82 | NormalMetaVal x -> pp_binding_kind x
83 | LabelVal (Absolute xs) ->
84 begin
85 pp "labelval";
86 pp "(";
87 Common.print_between (fun () -> pp ",") Format.print_int xs;
88 pp ")";
89 end
90 | LabelVal (Prefix xs) ->
91 begin
92 pp "prefixlabelval";
93 pp "(";
94 Common.print_between (fun () -> pp ",") Format.print_int xs;
95 pp ")";
96 end
97 | GoodVal -> pp "goodval"
98 | BadVal -> pp "badval"
99
100
101 let rec pp_predicate = function
102 | InLoop -> pp "InLoop"
103 | TrueBranch -> pp "TrueBranch"
104 | FalseBranch -> pp "FalseBranch"
105 | After -> pp "After"
106 | FallThrough -> pp "FallThrough"
107 | LoopFallThrough -> pp "LoopFallThrough"
108 | Return -> pp "Return"
109 | FunHeader -> pp "FunHeader"
110 | Top -> pp "Top"
111 | ErrorExit -> pp "ErrorExit"
112 | Exit -> pp "Exit"
113 | Goto -> pp "Goto"
114 | Paren s -> pp "Paren("; pp_meta s; pp ")"
115 | Match (re) -> Pretty_print_cocci.print_rule_elem re
116 | Label s -> pp "Label("; pp_meta s; pp ")"
117 | BCLabel s -> pp "BreakContinueLabel("; pp_meta s; pp ")"
118 | PrefixLabel s -> pp "PrefixLabel("; pp_meta s; pp ")"
119 | BindGood s -> pp "BindGood("; pp_meta s; pp ")"
120 | BindBad s -> pp "BindBad("; pp_meta s; pp ")"
121 | FakeBrace -> pp "FakeBrace"
122
123 and pp_binding2 subst =
124 begin
125 pp "[";
126 Common.print_between (fun () -> pp ";";Format.print_cut(); )
127 (fun (s, kind) -> pp s; pp " --> "; pp_binding_kind2 kind)
128 subst;
129 pp "]";
130 end
131
132 and pp_binding2_ctlsubst subst =
133 begin
134 pp "[";
135 Common.print_between (fun () -> pp ";"; Format.print_cut(); )
136 (function
137 Ast_ctl.Subst (s, kind) ->
138 pp_meta s; pp " --> "; pp_binding_kind2 kind;
139 | Ast_ctl.NegSubst (s, kind) ->
140 pp_meta s; pp " -/-> "; pp_binding_kind2 kind;
141 )
142 subst;
143 pp "]";
144 end
145
146 let predicate_to_string pred =
147 Common.format_to_string (function _ -> pp_predicate pred)
148
149
150 let pp_pred_smodif = fun (pred, smodif) ->
151 begin
152 pp_predicate pred;
153 (*
154 (match smodif with
155 | Ast_ctl.Modif x | Ast_ctl.UnModif x -> pp " with <modifTODO>"
156 | Ast_ctl.Control -> ()
157 )
158 *)
159 end
160
161
162 let pp_ctlcocci show_plus inline_let_def ctl =
163 begin
164 if show_plus
165 then begin
166 Pretty_print_cocci.print_plus_flag := true;
167 Pretty_print_cocci.print_minus_flag := true;
168 end
169 else begin
170 Pretty_print_cocci.print_plus_flag := false;
171 Pretty_print_cocci.print_minus_flag := false;
172 end;
173 Common.pp_do_in_box (fun () ->
174 Pretty_print_ctl.pp_ctl (pp_pred_smodif,(fun s -> pp_meta s))
175 inline_let_def ctl;
176 );
177 end
178
179