Coccinelle release 1.0.0-rc4
[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.MetaInitListVal ini -> pp "<<initlist>>"
43 | Ast_c.MetaTypeVal typ ->
44 Pretty_print_c.pp_type_simple typ
45 | Ast_c.MetaDeclVal decl ->
46 Pretty_print_c.pp_decl_simple decl
47 | Ast_c.MetaFieldVal decl ->
48 Pretty_print_c.pp_field_simple decl
49 | Ast_c.MetaFieldListVal decls ->
50 List.iter Pretty_print_c.pp_field_simple decls
51 | Ast_c.MetaStmtVal statement ->
52 Pretty_print_c.pp_statement_simple statement
53 | Ast_c.MetaParamVal params -> pp "<<param>>"
54 | Ast_c.MetaParamListVal params -> pp "<<paramlist>>"
55 | Ast_c.MetaListlenVal n -> pp (string_of_int n)
56 | Ast_c.MetaPosVal (pos1, pos2) ->
57 let print_pos = function
58 Ast_cocci.Real x -> string_of_int x
59 | Ast_cocci.Virt(x,off) -> Printf.sprintf "%d+%d" x off in
60 pp (Common.sprintf ("pos(%s,%s)") (print_pos pos1) (print_pos pos2))
61 | Ast_c.MetaPosValList l ->
62 pp (Common.sprintf ("poss[%s]")
63 (String.concat ", "
64 (List.map
65 (function (fl,ce,(minl,minc),(maxl,maxc)) ->
66 Printf.sprintf "(%s,%s,(%d,%d),(%d,%d))"
67 fl ce minl minc maxl maxc)
68 l)))
69
70 and pp_binding subst =
71 begin
72 pp "[";
73 Common.print_between (fun () -> pp ";"; Format.print_cut() )
74 (fun ((r,s), kind) ->
75 pp r; pp "."; pp s; pp " --> "; pp_binding_kind kind)
76 subst;
77 pp "]";
78 end
79
80
81 let pp_binding_kind2 = function
82 | ParenVal s -> pp "pv("; pp_meta s; pp ")"
83 | NormalMetaVal x -> pp_binding_kind x
84 | LabelVal (Absolute xs) ->
85 begin
86 pp "labelval";
87 pp "(";
88 Common.print_between (fun () -> pp ",") Format.print_int xs;
89 pp ")";
90 end
91 | LabelVal (Prefix xs) ->
92 begin
93 pp "prefixlabelval";
94 pp "(";
95 Common.print_between (fun () -> pp ",") Format.print_int xs;
96 pp ")";
97 end
98 | GoodVal -> pp "goodval"
99 | BadVal -> pp "badval"
100
101
102 let rec pp_predicate = function
103 | InLoop -> pp "InLoop"
104 | TrueBranch -> pp "TrueBranch"
105 | FalseBranch -> pp "FalseBranch"
106 | After -> pp "After"
107 | FallThrough -> pp "FallThrough"
108 | LoopFallThrough -> pp "LoopFallThrough"
109 | Return -> pp "Return"
110 | UnsafeBrace -> pp "UnsafeBrace"
111 | FunHeader -> pp "FunHeader"
112 | Top -> pp "Top"
113 | ErrorExit -> pp "ErrorExit"
114 | Exit -> pp "Exit"
115 | Goto -> pp "Goto"
116 | Paren s -> pp "Paren("; pp_meta s; pp ")"
117 | Match (re) -> Pretty_print_cocci.print_rule_elem re
118 | Label s -> pp "Label("; pp_meta s; pp ")"
119 | BCLabel s -> pp "BreakContinueLabel("; pp_meta s; pp ")"
120 | PrefixLabel s -> pp "PrefixLabel("; pp_meta s; pp ")"
121 | BindGood s -> pp "BindGood("; pp_meta s; pp ")"
122 | BindBad s -> pp "BindBad("; pp_meta s; pp ")"
123 | FakeBrace -> pp "FakeBrace"
124
125 and pp_binding2 subst =
126 begin
127 pp "[";
128 Common.print_between (fun () -> pp ";";Format.print_cut(); )
129 (fun (s, kind) -> pp s; pp " --> "; pp_binding_kind2 kind)
130 subst;
131 pp "]";
132 end
133
134 and pp_binding2_ctlsubst subst =
135 begin
136 pp "[";
137 Common.print_between (fun () -> pp ";"; Format.print_cut(); )
138 (function
139 Ast_ctl.Subst (s, kind) ->
140 pp_meta s; pp " --> "; pp_binding_kind2 kind;
141 | Ast_ctl.NegSubst (s, kind) ->
142 pp_meta s; pp " -/-> "; pp_binding_kind2 kind;
143 )
144 subst;
145 pp "]";
146 end
147
148 let predicate_to_string pred =
149 Common.format_to_string (function _ -> pp_predicate pred)
150
151
152 let pp_pred_smodif = fun (pred, smodif) ->
153 begin
154 pp_predicate pred;
155 (*
156 (match smodif with
157 | Ast_ctl.Modif x | Ast_ctl.UnModif x -> pp " with <modifTODO>"
158 | Ast_ctl.Control -> ()
159 )
160 *)
161 end
162
163
164 let pp_ctlcocci show_plus inline_let_def ctl =
165 begin
166 if show_plus
167 then begin
168 Pretty_print_cocci.print_plus_flag := true;
169 Pretty_print_cocci.print_minus_flag := true;
170 end
171 else begin
172 Pretty_print_cocci.print_plus_flag := false;
173 Pretty_print_cocci.print_minus_flag := false;
174 end;
175 Common.pp_do_in_box (fun () ->
176 Pretty_print_ctl.pp_ctl (pp_pred_smodif,(fun s -> pp_meta s))
177 inline_let_def ctl;
178 );
179 end
180
181