2 * Copyright 2005-2010, Ecole des Mines de Nantes, University of Copenhagen
3 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
4 * This file is part of Coccinelle.
6 * Coccinelle is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, according to version 2 of the License.
10 * Coccinelle is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
18 * The authors reserve the right to distribute this or future versions of
19 * Coccinelle under other licenses.
24 * Copyright 2005-2010, Ecole des Mines de Nantes, University of Copenhagen
25 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
26 * This file is part of Coccinelle.
28 * Coccinelle is free software: you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, according to version 2 of the License.
32 * Coccinelle is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
40 * The authors reserve the right to distribute this or future versions of
41 * Coccinelle under other licenses.
49 (* For the moment I do only eq_type and not eq_expr, etc. The reason
50 * for eq_type is related to the typedef and struct isomorphism. Sometimes
51 * one use the typedef and sometimes the structname.
53 * TODO: should use the isomorphisms engine of julia.
54 * Maybe I can transform my ast_c in ast_cocci, and use julia's code ?
55 * Maybe I can add some Disj in my ast_c ?
64 type 'a matcher
= 'a
-> 'a
-> tin
-> 'a tout
68 ('a
-> (tin
-> 'b tout
)) ->
71 val (>&&>) : bool -> (tin
-> 'x tout
) -> (tin
-> 'x tout
)
73 val return
: 'a
-> tin
-> 'a tout
74 val fail
: tin
-> 'a tout
79 functor (X
: PARAM
) ->
82 type 'a matcher
= 'a
-> 'a
-> X.tin
-> 'a
X.tout
89 let (option: 'a matcher
-> ('a
option matcher
)) = fun f t1 t2
->
91 | (Some t1
, Some t2
) ->
95 | (None
, None
) -> return None
99 let same_s saopt sbopt
=
100 match saopt
, sbopt
with
102 | Some namea
, Some nameb
->
103 let sa = Ast_c.str_of_name namea
in
104 let sb = Ast_c.str_of_name nameb
in
109 let rec fullType a b
=
110 let ((qua
,iiqa
), tya
) = a
in
111 let ((qub
,iiqb
), tyb
) = b
in
112 (qua
.const
=:= qub
.const
&& qua
.volatile
=:= qub
.volatile
) >&&>
114 let (qu
,iiq
) = (qua
, iiqa
) in
115 typeC tya tyb
>>= (fun ty
->
116 return ((qu
,iiq
), ty
)
120 let (a
, iia
) = tya
in
121 let (b
, iib
) = tyb
in
126 | BaseType a
, BaseType b
->
127 a
=*= b
>&&> return (BaseType a
, iix)
128 | Pointer a
, Pointer b
->
129 fullType a b
>>= (fun x
-> return (Pointer x
, iix))
131 | StructUnionName
(sua
, sa), StructUnionName
(sub
, sb) ->
132 (sua
=*= sub
&& sa =$
= sb) >&&>
133 return (StructUnionName
(sua
, sa), iix)
135 | TypeName
(namea
, opta
), TypeName
(nameb
, optb
) ->
136 let sa = Ast_c.str_of_name namea
in
137 let sb = Ast_c.str_of_name nameb
in
139 (* assert compatible opta optb ? *)
140 (*option fullType opta optb*)
143 (match opta
, optb
with
152 return (TypeName
(namea
, opt), iix)
155 | Array
(ea
, a
), Array
(eb
,b
) ->
156 let get_option f
= function Some x
-> Some
(f x
) | None
-> None
in
157 let ea = get_option Lib_parsing_c.al_expr
ea in
158 let eb = get_option Lib_parsing_c.al_expr
eb in
159 ea =*= eb >&&> fullType a b
>>= (fun x
-> return (Array
(ea, x
), iix))
161 | FunctionType
(returna
, paramsa
), FunctionType
(returnb
, paramsb
) ->
162 let (tsa
, (ba
,iihas3dotsa
)) = paramsa
in
163 let (tsb
, (bb
,iihas3dotsb
)) = paramsb
in
166 let iihas3dotsx = iihas3dotsa
in
168 (ba
=:= bb
&& List.length tsa
=|= List.length tsb
) >&&>
169 fullType returna returnb
>>= (fun returnx
->
171 Common.zip tsa tsb
+> List.fold_left
172 (fun acc
((parama
,iia
),(paramb
,iib
))->
176 let {p_register
= (ba
,iiba
); p_namei
= saopt
; p_type
= ta
} =
178 let {p_register
= (bb
,iibb
); p_namei
= sbopt
; p_type
= tb
} =
187 (* todo? iso on name or argument ? *)
188 (ba
=:= bb
&& same_s saopt sbopt
) >&&>
189 fullType ta tb
>>= (fun tx
->
190 let paramx = { p_register
= (bx, iibx);
193 return ((paramx,iix)::xs
)
198 let paramsx = (List.rev tsx
, (bx, iihas3dotsx)) in
199 return (FunctionType
(returnx
, paramsx), iix)
202 | Enum
(saopt
, enuma
), Enum
(sbopt
, enumb
) ->
204 List.length enuma
=|= List.length enumb
&&
205 Common.zip enuma enumb
+> List.for_all
(fun
206 (((namesa
,eopta
), iicommaa
), ((namesb
,eoptb
),iicommab
))
208 let sa = str_of_name namesa
in
209 let sb = str_of_name namesb
in
211 (* todo ? eopta and b can have some info so ok to use =*= ? *)
215 return (Enum
(saopt
, enuma
), iix)
217 | EnumName
sa, EnumName
sb -> sa =$
= sb >&&> return (EnumName
sa, iix)
219 | ParenType a
, ParenType b
->
221 fullType a b
>>= (fun x
->
222 return (ParenType x
, iix)
225 | TypeOfExpr
ea, TypeOfExpr
eb ->
226 let ea = Lib_parsing_c.al_expr
ea in
227 let eb = Lib_parsing_c.al_expr
eb in
228 ea =*= eb >&&> return (TypeOfExpr
ea, iix)
230 | TypeOfType a
, TypeOfType b
->
231 fullType a b
>>= (fun x
-> return (TypeOfType x
, iix))
233 (* | TypeOfType a, b ->
238 | StructUnion
(sua
, saopt
, sta
), StructUnion
(sub
, sbopt
, stb
) ->
239 (sua
=*= sub
&& saopt
=*= sbopt
&& List.length sta
=|= List.length stb
)
241 Common.zip sta stb
+> List.fold_left
242 (fun acc
((fielda
), (fieldb
)) ->
244 match fielda
, fieldb
with
245 | EmptyField iia
, EmptyField iib
->
247 return ((EmptyField
iix)::xs
)
249 | DeclarationField
(FieldDeclList
(fa
, iipta
)),
250 DeclarationField
(FieldDeclList
(fb
, iiptb
)) ->
251 let iipt = iipta
in (* TODO ?*)
252 (List.length fa
=|= List.length fb
) >&&>
254 Common.zip fa fb
+> List.fold_left
255 (fun acc2
((fielda
,iia
),(fieldb
,iib
))->
258 match fielda
, fieldb
with
259 | Simple
(nameaopt
, ta
), Simple
(namebopt
, tb
) ->
262 same_s nameaopt namebopt
>&&>
263 fullType ta tb
>>= (fun tx
->
264 return (((Simple
(nameaopt
, tx
)), iix)::xs
)
267 | BitField
(nameopta
, ta
, infoa
, ea),
268 BitField
(nameoptb
, tb
, infob
, eb) ->
270 (same_s nameopta nameoptb
&& ea =*= eb) >&&>
271 fullType ta tb
>>= (fun tx
->
272 return (((BitField
(nameopta
,tx
,infox,ea)), iix)::xs
)
278 return (((DeclarationField
279 (FieldDeclList
(List.rev fx
,iipt))))::xs
)
287 return (StructUnion
(sua
, saopt
, List.rev stx
), iix)
293 * subtil: in the return must put iia, not iix, and in following case
294 * must put iib and not iix, because we want the token corresponding
297 | TypeName
(name
, Some a
), _
->
298 fullType a
(Ast_c.nQ
, tyb
) >>= (fun x
->
299 return (TypeName
(name
, Some x
), iia
)
302 | _
, TypeName
(name
, Some b
) ->
303 fullType b
(Ast_c.nQ
, tya
) >>= (fun x
->
304 return (TypeName
(name
, Some x
), iib
) (* subtil: *)
315 type 'a tout
= 'a
option
317 type 'a matcher
= 'a
-> 'a
-> tin
-> 'a tout
319 let return x
= fun tin
-> Some x
320 let fail = fun tin
-> None
322 let (>>=) m f
= fun tin
->
327 let (>&&>) b m
= fun tin
->
333 module EQ
= C_VS_C
(XEQ
)
336 let eq_type2 a b
= EQ.fullType a b
() <> None
337 let merge_type2 a b
= Common.some
(EQ.fullType a b
())
340 Common.profile_code
"C_vs_c" (fun () -> eq_type2 a b
)
343 Common.profile_code
"C_vs_c" (fun () -> merge_type2 a b
)
346 (* ------------------------------------------------------------------------- *)
348 (* This seemed like a reasonable place to put this, given the file name,
349 but not sure that it is the case... This has to be compatible with the
350 function equal_inh_metavarval. It is indeed not so clear why that is
351 defined in cocci_vs_c.ml, and not here, since it is comparing C code to C
354 let subexpression_of_expression small_exp big_exp
=
355 let res = ref false in (* because no appropriate functional visitor... *)
356 let expr (k
,bigf
) big_exp
=
357 (* comparison used in Cocci_vs_c.equal_inh_metavarval *)
358 (* have to strip each subexp, because stripping puts some offsets in the
359 term rather than setting everything to 0. No idea why... *)
360 if small_exp
=*= Lib_parsing_c.al_inh_expr big_exp
363 let bigf = { Visitor_c.default_visitor_c
with Visitor_c.kexpr
= expr } in
364 Visitor_c.vk_expr
bigf big_exp
;
365 (*Printf.printf "comparison gives %b\n" !res;
366 Pretty_print_c.pp_expression_simple small_exp;
367 Format.print_newline();
368 Pretty_print_c.pp_expression_simple big_exp;
369 Format.print_newline();
370 Printf.printf "--------------------------------\n";*)