Release coccinelle-0.1.2
[bpt/coccinelle.git] / parsing_c / test_parsing_c.ml
CommitLineData
34e49164
C
1open Common
2
3let score_path = "/home/pad/c-yacfe/tmp"
4
5let tmpfile = "/tmp/output.c"
6
7(*****************************************************************************)
8(* Subsystem testing *)
9(*****************************************************************************)
10
11let test_tokens_c file =
12 if not (file =~ ".*\\.c")
13 then pr2 "warning: seems not a .c file";
14
15 Flag_parsing_c.debug_lexer := true;
16 Flag_parsing_c.verbose_lexing := true;
17 Flag_parsing_c.verbose_parsing := true;
18
19 Parse_c.tokens file +> List.iter (fun x -> pr2_gen x);
20 ()
21
22
23
24(* ---------------------------------------------------------------------- *)
25let test_parse_gen xs ext =
26
27 Flag_parsing_c.debug_typedef := true;
28 Flag_parsing_c.debug_cpp := true;
29 Flag_parsing_c.debug_etdt := false;
30 Flag_parsing_c.filter_msg := true;
31
32 let dirname_opt =
33 match xs with
34 | [x] when is_directory x -> Some x
35 | _ -> None
36 in
37
38 (* old:
39 let xs = if !Flag.dir then
40 process_output_to_list ("find " ^ x ^" -name \"*.c\"") else x::xs in
41 *)
485bce71 42 let fullxs = Common.files_of_dir_or_files_no_vcs ext xs in
34e49164
C
43
44 let stat_list = ref [] in
45 let newscore = Common.empty_score () in
46
485bce71 47 Common.check_stack_nbfiles (List.length fullxs);
34e49164
C
48
49 fullxs +> List.iter (fun file ->
50 if not (file =~ (".*\\."^ext))
51 then pr2 ("warning: seems not a ."^ext^" file");
52
53
54 pr2 "";
55 pr2 ("PARSING: " ^ file);
56
57 let (xs, stat) = Parse_c.parse_print_error_heuristic file in
58 xs +> List.iter (fun (ast, (s, toks)) ->
59 Parse_c.print_commentized toks
60 );
61
62 Common.push2 stat stat_list;
63 let s =
64 sprintf "bad = %d, timeout = %B"
485bce71 65 stat.Parsing_stat.bad stat.Parsing_stat.have_timeout
34e49164 66 in
485bce71 67 if stat.Parsing_stat.bad = 0 && not stat.Parsing_stat.have_timeout
34e49164
C
68 then Hashtbl.add newscore file (Common.Ok)
69 else Hashtbl.add newscore file (Common.Pb s)
70 );
71
72 if not (null !stat_list)
485bce71 73 then Parsing_stat.print_parsing_stat_list !stat_list;
34e49164
C
74
75 dirname_opt +> Common.do_option (fun dirname ->
76 pr2 "--------------------------------";
77 pr2 "regression testing information";
78 pr2 "--------------------------------";
79 let str = Str.global_replace (Str.regexp "/") "__" dirname in
80 let def = if !Flag_parsing_c.filter_define_error then "_def_" else "" in
81 let ext = if ext = "c" then "" else ext in
82 Common.regression_testing newscore
83 (Filename.concat score_path
84 ("score_parsing__" ^str ^ def ^ ext ^ ".marshalled"))
85 )
86
87
88let test_parse_c xs =
89 test_parse_gen xs "c"
90let test_parse_h xs =
91 test_parse_gen xs "h"
92let test_parse_ch xs =
93 test_parse_gen xs "[ch]"
94
95
96
97
98
99
100
101
102
103
104
105
106(* ---------------------------------------------------------------------- *)
107(* file can be "foo.c" or "foo.c:main" *)
108let test_cfg file =
34e49164
C
109 let (file, specific_func) =
110 if file =~ "\\(.*\\.c\\):\\(.*\\)"
111 then
112 let (a,b) = matched2 file in
113 a, Some b
114 else
115 file, None
116 in
117
118 if not (file =~ ".*\\.c")
119 then pr2 "warning: seems not a .c file";
120
121 let (program, _stat) = Parse_c.parse_print_error_heuristic file in
122
123 program +> List.iter (fun (e,_) ->
124 let toprocess =
125 match specific_func, e with
126 | None, _ -> true
485bce71
C
127 | Some s, Ast_c.Definition (defbis,_) ->
128 s = defbis.Ast_c.f_name
34e49164
C
129 | _, _ -> false
130 in
131
132 if toprocess
133 then
134 (* old: Flow_to_ast.test !Flag.show_flow def *)
135 (try
136 let flow = Ast_to_flow.ast_to_control_flow e in
137 flow +> do_option (fun flow ->
138 Ast_to_flow.deadcode_detection flow;
139 let flow = Ast_to_flow.annotate_loop_nodes flow in
140
141 let flow' =
142(*
143 if !Flag_cocci.show_before_fixed_flow
144 then flow
145 else Ctlcocci_integration.fix_flow_ctl flow
146*)
147 flow
148 in
485bce71
C
149 let filename = Filename.temp_file "output" ".dot" in
150 Ograph_extended.print_ograph_mutable flow' (filename) true
34e49164
C
151 )
152 with Ast_to_flow.Error (x) -> Ast_to_flow.report_error x
153 )
154 )
155
156
157
34e49164
C
158(* ---------------------------------------------------------------------- *)
159let test_parse_unparse infile =
160 if not (infile =~ ".*\\.c")
161 then pr2 "warning: seems not a .c file";
162
34e49164
C
163 let (program2, _stat) = Parse_c.parse_print_error_heuristic infile in
164 let program2_with_ppmethod =
485bce71 165 program2 +> List.map (fun x -> x, Unparse_c.PPnormal)
34e49164 166 in
485bce71 167 Unparse_c.pp_program program2_with_ppmethod tmpfile;
34e49164
C
168 Common.command2 ("cat " ^ tmpfile);
169 (* if want see diff of space => no -b -B *)
170 Common.command2 (spf "diff -u -p %s %s" infile tmpfile);
171 (* +> Transformation.test_simple_trans1;*)
34e49164
C
172 ()
173
174
175
176
177let test_type_c infile =
178 if not (infile =~ ".*\\.c")
179 then pr2 "warning: seems not a .c file";
180
181 Flag_parsing_c.pretty_print_type_info := true;
182
183 let (program2, _stat) = Parse_c.parse_print_error_heuristic infile in
184 let _program2 =
185 program2
186 +> Common.unzip
187 +> (fun (program, infos) ->
188 Type_annoter_c.annotate_program Type_annoter_c.initial_env true
189 program +> List.map fst,
190 infos
191 )
192 +> Common.uncurry Common.zip
193 in
34e49164 194 let program2_with_ppmethod =
485bce71 195 program2 +> List.map (fun x -> x, Unparse_c.PPnormal)
34e49164 196 in
485bce71 197 Unparse_c.pp_program program2_with_ppmethod tmpfile;
34e49164
C
198 Common.command2 ("cat " ^ tmpfile);
199 ();;
200
201
202(* ---------------------------------------------------------------------- *)
203(* used by generic_makefile now *)
204let test_compare_c file1 file2 =
205 let (correct, diffxs) = Compare_c.compare_default file1 file2 in
206 let res = Compare_c.compare_result_to_bool correct in
207 if res
208 then raise (Common.UnixExit 0)
209 else raise (Common.UnixExit (-1))
210
211
212let test_compare_c_hardcoded () =
213 Compare_c.compare_default
214 "tests/compare1.c"
215 "tests/compare2.c"
216 (*
217 "tests/equal_modulo1.c"
218 "tests/equal_modulo2.c"
219 *)
220 +> Compare_c.compare_result_to_string
221 +> pr2
222
223
224
225(* ---------------------------------------------------------------------- *)
226let test_xxx a =
227 ()
228
229(*
230 ignore(Parse_c.parse_cpp_define_file "standard.h")
231 pr2 "pr2";
232 pr "pr"
233
234 Format.print_newline();
235 Format.printf "@[<v 5>--@,--@,@[<v 5>--@,--@,@]--@,--@,@]";
236 Format.print_newline();
237 Format.printf "@[<v>(---@[<v>(---@[<v>(---@,)@]@,)@]@,)@]"
238*)
239
240
241
242(*****************************************************************************)
243(* Main entry for Arg *)
244(*****************************************************************************)
245
246let actions () = [
247 "-tokens_c", " <file>",
248 Common.mk_action_1_arg test_tokens_c;
249 "-parse_c", " <file or dir>",
250 Common.mk_action_n_arg test_parse_c;
251 "-parse_h", " <file or dir>",
252 Common.mk_action_n_arg test_parse_h;
253 "-parse_ch", " <file or dir>",
254 Common.mk_action_n_arg test_parse_ch;
255
256 "-show_flow", " <file or file:function>",
257 Common.mk_action_1_arg test_cfg;
258 "-control_flow", " <file or file:function>",
259 Common.mk_action_1_arg test_cfg;
260 "-parse_unparse", " <file>",
261 Common.mk_action_1_arg test_parse_unparse;
262 "-type_c", " <file>",
263 Common.mk_action_1_arg test_type_c;
264 "-compare_c", " <file1> <file2>",
265 Common.mk_action_2_arg test_compare_c (* result is in unix code *);
266
267 "-compare_c_hardcoded", " ",
268 Common.mk_action_0_arg test_compare_c_hardcoded;
269
270 "-xxx", " <file1> <>",
271 Common.mk_action_n_arg test_xxx;
272]
273