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