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