5 let score_path = "/home/pad/c-yacfe/tmp"
7 let tmpfile = "/tmp/output.c"
10 (*****************************************************************************)
11 (* Subsystem testing *)
12 (*****************************************************************************)
14 let test_tokens_c file
=
15 if not
(file
=~
".*\\.c")
16 then pr2
"warning: seems not a .c file";
18 Flag_parsing_c.debug_lexer
:= true;
19 Flag_parsing_c.verbose_lexing
:= true;
20 Flag_parsing_c.verbose_parsing
:= true;
22 Parse_c.tokens file
+> List.iter
(fun x
-> pr2_gen x
);
27 (* ---------------------------------------------------------------------- *)
28 let test_parse_gen xs ext
=
30 Flag_parsing_c.debug_typedef
:= true;
31 Flag_parsing_c.debug_cpp
:= true;
32 Flag_parsing_c.debug_etdt
:= false;
33 Flag_parsing_c.filter_msg
:= true;
37 | [x
] when is_directory x
-> Some x
42 let xs = if !Flag.dir then
43 process_output_to_list ("find " ^ x ^" -name \"*.c\"") else x::xs in
45 let fullxs = Common.files_of_dir_or_files_no_vcs ext
xs in
47 let stat_list = ref [] in
48 let newscore = Common.empty_score
() in
50 Common.check_stack_nbfiles
(List.length
fullxs);
52 fullxs +> List.iter
(fun file
->
53 if not
(file
=~
(".*\\."^ext
))
54 then pr2
("warning: seems not a ."^ext^
" file");
58 pr2
("PARSING: " ^ file
);
60 let (xs, stat
) = Parse_c.parse_print_error_heuristic file
in
61 xs +> List.iter
(fun (ast
, (s
, toks
)) ->
62 Parse_c.print_commentized toks
65 Common.push2 stat
stat_list;
67 sprintf
"bad = %d, timeout = %B"
68 stat
.Parsing_stat.bad stat
.Parsing_stat.have_timeout
70 if stat
.Parsing_stat.bad
=|= 0 && not stat
.Parsing_stat.have_timeout
71 then Hashtbl.add
newscore file
(Common.Ok
)
72 else Hashtbl.add
newscore file
(Common.Pb
s)
75 if not
(null
!stat_list)
77 Parsing_stat.print_recurring_problematic_tokens
!stat_list;
78 Parsing_stat.print_parsing_stat_list
!stat_list;
81 dirname_opt +> Common.do_option
(fun dirname
->
82 pr2_xxxxxxxxxxxxxxxxx
();
83 pr2
"regression testing information";
84 pr2_xxxxxxxxxxxxxxxxx
();
85 let str = Str.global_replace
(Str.regexp
"/") "__" dirname
in
86 let def = if !Flag_parsing_c.filter_define_error
then "_def_" else "" in
87 let ext = if ext =$
= "c" then "" else ext in
88 Common.regression_testing
newscore
89 (Filename.concat
score_path
90 ("score_parsing__" ^
str ^
def ^
ext ^
".marshalled"))
98 let test_parse_ch xs =
99 test_parse_gen xs "[ch]"
112 (* ---------------------------------------------------------------------- *)
113 (* file can be "foo.c" or "foo.c:main" *)
115 let (file
, specific_func
) =
116 if file
=~
"\\(.*\\.c\\):\\(.*\\)"
118 let (a
,b
) = matched2 file
in
124 if not
(file
=~
".*\\.c")
125 then pr2
"warning: seems not a .c file";
127 let (program
, _stat
) = Parse_c.parse_print_error_heuristic file
in
129 program
+> List.iter
(fun (e
,_
) ->
131 match specific_func
, e
with
133 | Some
s, Ast_c.Definition
(defbis
,_
) ->
134 s =$
= Ast_c.str_of_name
(defbis
.Ast_c.f_name
)
140 (* old: Flow_to_ast.test !Flag.show_flow def *)
142 let flow = Ast_to_flow.ast_to_control_flow e
in
143 flow +> do_option
(fun flow ->
144 Ast_to_flow.deadcode_detection
flow;
145 let flow = Ast_to_flow.annotate_loop_nodes
flow in
149 if !Flag_cocci.show_before_fixed_flow
151 else Ctlcocci_integration.fix_flow_ctl flow
155 let filename = Filename.temp_file
"output" ".dot" in
156 Ograph_extended.print_ograph_mutable
flow'
(filename) true
158 with Ast_to_flow.Error
(x
) -> Ast_to_flow.report_error x
164 let test_cfg_ifdef file
=
165 let (ast2
, _stat
) = Parse_c.parse_print_error_heuristic file
in
166 let ast = Parse_c.program_of_program2 ast2
in
168 let ast = Cpp_ast_c.cpp_ifdef_statementize
ast in
170 ast +> List.iter
(fun e
->
172 let flow = Ast_to_flow.ast_to_control_flow e
in
173 flow +> do_option
(fun flow ->
174 Ast_to_flow.deadcode_detection
flow;
175 let flow = Ast_to_flow.annotate_loop_nodes
flow in
176 Ograph_extended.print_ograph_mutable
flow ("/tmp/output.dot") true
178 with Ast_to_flow.Error
(x
) -> Ast_to_flow.report_error x
182 (* ---------------------------------------------------------------------- *)
183 let test_parse_unparse infile
=
184 if not
(infile
=~
".*\\.c")
185 then pr2
"warning: seems not a .c file";
187 let (program2
, _stat
) = Parse_c.parse_print_error_heuristic infile
in
188 let program2_with_ppmethod =
189 program2
+> List.map
(fun x
-> x
, Unparse_c.PPnormal
)
191 Unparse_c.pp_program
program2_with_ppmethod tmpfile;
192 Common.command2
("cat " ^
tmpfile);
193 (* if want see diff of space => no -b -B *)
194 Common.command2
(spf
"diff -u -p %s %s" infile
tmpfile);
195 (* +> Transformation.test_simple_trans1;*)
201 let test_type_c infile
=
202 if not
(infile
=~
".*\\.c")
203 then pr2
"warning: seems not a .c file";
205 Flag_parsing_c.pretty_print_type_info
:= true;
207 let (program2
, _stat
) = Parse_c.parse_print_error_heuristic infile
in
211 +> (fun (program
, infos
) ->
212 Type_annoter_c.annotate_program
!Type_annoter_c.initial_env
213 program
+> List.map fst
,
216 +> Common.uncurry
Common.zip
218 let program2_with_ppmethod =
219 program2
+> List.map
(fun x
-> x
, Unparse_c.PPnormal
)
221 Unparse_c.pp_program
program2_with_ppmethod tmpfile;
222 Common.command2
("cat " ^
tmpfile);
226 (* ---------------------------------------------------------------------- *)
227 (* ex: demos/platform_ifdef.c *)
228 let test_comment_annotater infile
=
229 let (program2
, _stat
) = Parse_c.parse_print_error_heuristic infile
in
230 let asts = program2
+> List.map
(fun (ast,_
) -> ast) in
231 let toks = program2
+> List.map
(fun (ast, (s, toks)) -> toks) +>
234 Flag_parsing_c.pretty_print_comment_info
:= true;
236 pr2
"pretty print, before comment annotation: --->";
237 Common.adjust_pp_with_indent
(fun () ->
238 asts +> List.iter
(fun ast ->
239 Pretty_print_c.pp_toplevel_simple
ast;
243 let _ = Comment_annotater_c.annotate_program
toks asts in
245 Common.adjust_pp_with_indent
(fun () ->
246 pr2
"pretty print, after comment annotation: --->";
247 asts +> List.iter
(fun ast ->
248 Pretty_print_c.pp_toplevel_simple
ast;
256 (* ---------------------------------------------------------------------- *)
257 (* used by generic_makefile now *)
258 let test_compare_c file1 file2
=
259 let (correct
, diffxs
) = Compare_c.compare_default file1 file2
in
260 let res = Compare_c.compare_result_to_bool correct
in
262 then raise
(Common.UnixExit
0)
263 else raise
(Common.UnixExit
(-1))
266 let test_compare_c_hardcoded () =
267 Compare_c.compare_default
271 "tests/equal_modulo1.c"
272 "tests/equal_modulo2.c"
274 +> Compare_c.compare_result_to_string
279 (* ---------------------------------------------------------------------- *)
280 let test_attributes file
=
281 let (ast2
, _stat
) = Parse_c.parse_c_and_cpp file
in
282 let ast = Parse_c.program_of_program2 ast2
in
284 Visitor_c.vk_program
{ Visitor_c.default_visitor_c
with
285 Visitor_c.kdef
= (fun (k
, bigf
) (defbis
, ii
) ->
286 let sattr = Ast_c.s_of_attr defbis
.f_attr
in
287 pr2
(spf
"%-30s: %s" (Ast_c.str_of_name
(defbis
.f_name
)) sattr);
289 Visitor_c.kdecl
= (fun (k
, bigf
) decl
->
291 | DeclList
(xs, ii
) ->
292 xs +> List.iter
(fun (onedecl
, iicomma
) ->
294 let sattr = Ast_c.s_of_attr onedecl
.v_attr
in
296 match onedecl
.v_namei
with
297 | Some
(name
, ini
) -> Ast_c.str_of_name name
300 pr2
(spf
"%-30s: %s" idname sattr);
309 let cpp_options () = [
310 Cpp_ast_c.I
"/home/yyzhou/pad/linux/include";
312 Cpp_ast_c.cpp_option_of_cmdline
313 (!Flag_parsing_c.cpp_i_opts
,!Flag_parsing_c.cpp_d_opts
)
316 let (ast2
, _stat
) = Parse_c.parse_c_and_cpp file
in
317 let dirname = Filename.dirname file
in
318 let ast = Parse_c.program_of_program2 ast2
in
319 let _ast'
= Cpp_ast_c.cpp_expand_include
(cpp_options()) dirname ast in
326 (* ---------------------------------------------------------------------- *)
331 ignore(Parse_c.parse_cpp_define_file "standard.h")
335 Format.print_newline();
336 Format.printf "@[<v 5>--@,--@,@[<v 5>--@,--@,@]--@,--@,@]";
337 Format.print_newline();
338 Format.printf "@[<v>(---@[<v>(---@[<v>(---@,)@]@,)@]@,)@]"
343 (*****************************************************************************)
344 (* Main entry for Arg *)
345 (*****************************************************************************)
348 "-tokens_c", " <file>",
349 Common.mk_action_1_arg
test_tokens_c;
350 "-parse_c", " <file or dir>",
351 Common.mk_action_n_arg
test_parse_c;
352 "-parse_h", " <file or dir>",
353 Common.mk_action_n_arg
test_parse_h;
354 "-parse_ch", " <file or dir>",
355 Common.mk_action_n_arg
test_parse_ch;
357 "-show_flow", " <file or file:function>",
358 Common.mk_action_1_arg
test_cfg;
359 "-control_flow", " <file or file:function>",
360 Common.mk_action_1_arg
test_cfg;
361 "-test_cfg_ifdef", " <file>",
362 Common.mk_action_1_arg
test_cfg_ifdef;
363 "-parse_unparse", " <file>",
364 Common.mk_action_1_arg
test_parse_unparse;
365 "-type_c", " <file>",
366 Common.mk_action_1_arg
test_type_c;
367 "-compare_c", " <file1> <file2>",
368 Common.mk_action_2_arg
test_compare_c (* result is in unix code *);
369 "-comment_annotater_c", " <file>",
370 Common.mk_action_1_arg
test_comment_annotater;
372 "-compare_c_hardcoded", " ",
373 Common.mk_action_0_arg
test_compare_c_hardcoded;
375 "-test_attributes", " <file>",
376 Common.mk_action_1_arg
test_attributes;
377 "-test_cpp", " <file>",
378 Common.mk_action_1_arg
test_cpp;
382 "-xxx", " <file1> <>",
383 Common.mk_action_n_arg
test_xxx;