e82e71700301965220b989c9f639960d0c71b070
2 * Copyright 2012, INRIA
3 * Julia Lawall, Gilles Muller
4 * Copyright 2010-2011, INRIA, University of Copenhagen
5 * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
6 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
7 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
8 * This file is part of Coccinelle.
10 * Coccinelle is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, according to version 2 of the License.
14 * Coccinelle is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
22 * The authors reserve the right to distribute this or future versions of
23 * Coccinelle under other licenses.
31 (* For the moment I do only eq_type and not eq_expr, etc. The reason
32 * for eq_type is related to the typedef and struct isomorphism. Sometimes
33 * one use the typedef and sometimes the structname.
35 * TODO: should use the isomorphisms engine of julia.
36 * Maybe I can transform my ast_c in ast_cocci, and use julia's code ?
37 * Maybe I can add some Disj in my ast_c ?
46 type 'a matcher
= 'a
-> 'a
-> tin
-> 'a tout
50 ('a
-> (tin
-> 'b tout
)) ->
53 val (>&&>) : bool -> (tin
-> 'x tout
) -> (tin
-> 'x tout
)
55 val return
: 'a
-> tin
-> 'a tout
56 val fail
: tin
-> 'a tout
61 functor (X
: PARAM
) ->
64 type 'a matcher
= 'a
-> 'a
-> X.tin
-> 'a
X.tout
71 let (option: 'a matcher
-> ('a
option matcher
)) = fun f t1 t2
->
73 | (Some t1
, Some t2
) ->
77 | (None
, None
) -> return None
81 let same_s saopt sbopt
=
82 match saopt
, sbopt
with
84 | Some namea
, Some nameb
->
85 let sa = Ast_c.str_of_name namea
in
86 let sb = Ast_c.str_of_name nameb
in
91 let rec fullType a b
=
92 let ((qua
,iiqa
), tya
) = a
in
93 let ((qub
,iiqb
), tyb
) = b
in
94 (qua
.const
=:= qub
.const
&& qua
.volatile
=:= qub
.volatile
) >&&>
96 let (qu
,iiq
) = (qua
, iiqa
) in
97 typeC tya tyb
>>= (fun ty
->
102 let (a
, iia
) = tya
in
103 let (b
, iib
) = tyb
in
108 | BaseType a
, BaseType b
->
109 a
=*= b
>&&> return (BaseType a
, iix)
110 | Pointer a
, Pointer b
->
111 fullType a b
>>= (fun x
-> return (Pointer x
, iix))
113 | StructUnionName
(sua
, sa), StructUnionName
(sub
, sb) ->
114 (sua
=*= sub
&& sa =$
= sb) >&&>
115 return (StructUnionName
(sua
, sa), iix)
117 | TypeName
(namea
, opta
), TypeName
(nameb
, optb
) ->
118 let sa = Ast_c.str_of_name namea
in
119 let sb = Ast_c.str_of_name nameb
in
121 (* assert compatible opta optb ? *)
122 (*option fullType opta optb*)
125 (match opta
, optb
with
134 return (TypeName
(namea
, opt), iix)
137 | Array
(ea
, a
), Array
(eb
,b
) ->
138 let get_option f
= function Some x
-> Some
(f x
) | None
-> None
in
139 let ea = get_option Lib_parsing_c.al_expr
ea in
140 let eb = get_option Lib_parsing_c.al_expr
eb in
141 ea =*= eb >&&> fullType a b
>>= (fun x
-> return (Array
(ea, x
), iix))
143 | FunctionType
(returna
, paramsa
), FunctionType
(returnb
, paramsb
) ->
144 let (tsa
, (ba
,iihas3dotsa
)) = paramsa
in
145 let (tsb
, (bb
,iihas3dotsb
)) = paramsb
in
148 let iihas3dotsx = iihas3dotsa
in
150 (ba
=:= bb
&& List.length tsa
=|= List.length tsb
) >&&>
151 fullType returna returnb
>>= (fun returnx
->
153 Common.zip tsa tsb
+> List.fold_left
154 (fun acc
((parama
,iia
),(paramb
,iib
))->
158 let {p_register
= (ba
,iiba
); p_namei
= saopt
; p_type
= ta
} =
160 let {p_register
= (bb
,iibb
); p_namei
= sbopt
; p_type
= tb
} =
169 (* todo? iso on name or argument ? *)
170 (ba
=:= bb
&& same_s saopt sbopt
) >&&>
171 fullType ta tb
>>= (fun tx
->
172 let paramx = { p_register
= (bx, iibx);
175 return ((paramx,iix)::xs
)
180 let paramsx = (List.rev tsx
, (bx, iihas3dotsx)) in
181 return (FunctionType
(returnx
, paramsx), iix)
184 | Enum
(saopt
, enuma
), Enum
(sbopt
, enumb
) ->
186 List.length enuma
=|= List.length enumb
&&
187 Common.zip enuma enumb
+> List.for_all
(fun
188 (((namesa
,eopta
), iicommaa
), ((namesb
,eoptb
),iicommab
))
190 let sa = str_of_name namesa
in
191 let sb = str_of_name namesb
in
193 (* todo ? eopta and b can have some info so ok to use =*= ? *)
197 return (Enum
(saopt
, enuma
), iix)
199 | EnumName
sa, EnumName
sb -> sa =$
= sb >&&> return (EnumName
sa, iix)
201 | ParenType a
, ParenType b
->
203 fullType a b
>>= (fun x
->
204 return (ParenType x
, iix)
207 | TypeOfExpr
ea, TypeOfExpr
eb ->
208 let ea = Lib_parsing_c.al_expr
ea in
209 let eb = Lib_parsing_c.al_expr
eb in
210 ea =*= eb >&&> return (TypeOfExpr
ea, iix)
212 | TypeOfType a
, TypeOfType b
->
213 fullType a b
>>= (fun x
-> return (TypeOfType x
, iix))
215 (* | TypeOfType a, b ->
220 | StructUnion
(sua
, saopt
, sta
), StructUnion
(sub
, sbopt
, stb
) ->
221 (sua
=*= sub
&& saopt
=*= sbopt
&& List.length sta
=|= List.length stb
)
224 (* zip is only safe if the above succeeds *)
225 (Common.zip sta stb
+> List.fold_left
226 (fun acc
((fielda
), (fieldb
)) ->
228 match fielda
, fieldb
with
229 | EmptyField iia
, EmptyField iib
->
231 return ((EmptyField
iix)::xs
)
233 | DeclarationField
(FieldDeclList
(fa
, iipta
)),
234 DeclarationField
(FieldDeclList
(fb
, iiptb
)) ->
235 let iipt = iipta
in (* TODO ?*)
236 (List.length fa
=|= List.length fb
) >&&>
238 (* only executable if the length is correct *)
239 (Common.zip fa fb
+> List.fold_left
240 (fun acc2
((fielda
,iia
),(fieldb
,iib
))->
243 match fielda
, fieldb
with
244 | Simple
(nameaopt
, ta
), Simple
(namebopt
, tb
) ->
247 same_s nameaopt namebopt
>&&>
248 fullType ta tb
>>= (fun tx
->
249 return (((Simple
(nameaopt
, tx
)), iix)::xs
)
252 | BitField
(nameopta
, ta
, infoa
, ea),
253 BitField
(nameoptb
, tb
, infob
, eb) ->
255 (same_s nameopta nameoptb
&& ea =*= eb) >&&>
256 fullType ta tb
>>= (fun tx
->
257 return (((BitField
(nameopta
,tx
,infox,ea)), iix)::xs
)
263 return (((DeclarationField
264 (FieldDeclList
(List.rev fx
,iipt))))::xs
)
272 return (StructUnion
(sua
, saopt
, List.rev stx
), iix)
278 * subtil: in the return must put iia, not iix, and in following case
279 * must put iib and not iix, because we want the token corresponding
282 | TypeName
(name
, Some a
), _
->
283 fullType a
(Ast_c.nQ
, tyb
) >>= (fun x
->
284 return (TypeName
(name
, Some x
), iia
)
287 | _
, TypeName
(name
, Some b
) ->
288 fullType b
(Ast_c.nQ
, tya
) >>= (fun x
->
289 return (TypeName
(name
, Some x
), iib
) (* subtil: *)
300 type 'a tout
= 'a
option
302 type 'a matcher
= 'a
-> 'a
-> tin
-> 'a tout
304 let return x
= fun tin
-> Some x
305 let fail = fun tin
-> None
307 let (>>=) m f
= fun tin
->
312 let (>&&>) b m
= fun tin
->
318 module EQ
= C_VS_C
(XEQ
)
321 let eq_type2 a b
= EQ.fullType a b
() <> None
322 let merge_type2 a b
= Common.some
(EQ.fullType a b
())
325 Common.profile_code
"C_vs_c" (fun () -> eq_type2 a b
)
328 Common.profile_code
"C_vs_c" (fun () -> merge_type2 a b
)
331 (* ------------------------------------------------------------------------- *)
333 (* This seemed like a reasonable place to put this, given the file name,
334 but not sure that it is the case... This has to be compatible with the
335 function equal_inh_metavarval. It is indeed not so clear why that is
336 defined in cocci_vs_c.ml, and not here, since it is comparing C code to C
339 let subexpression_of_expression small_exp big_exp
=
340 let res = ref false in (* because no appropriate functional visitor... *)
341 let expr (k
,bigf
) big_exp
=
342 (* comparison used in Cocci_vs_c.equal_inh_metavarval *)
343 (* have to strip each subexp, because stripping puts some offsets in the
344 term rather than setting everything to 0. No idea why... *)
345 if small_exp
=*= Lib_parsing_c.al_inh_expr big_exp
348 let bigf = { Visitor_c.default_visitor_c
with Visitor_c.kexpr
= expr } in
349 Visitor_c.vk_expr
bigf big_exp
;
350 (*Printf.printf "comparison gives %b\n" !res;
351 Pretty_print_c.pp_expression_simple small_exp;
352 Format.print_newline();
353 Pretty_print_c.pp_expression_simple big_exp;
354 Format.print_newline();
355 Printf.printf "--------------------------------\n";*)