5 let score_path = "/home/pad/c-yacfe/tmp"
7 let tmpfile = "/tmp/output.c"
9 module Ast_to_flow
= Control_flow_c_build
11 (*****************************************************************************)
12 (* Subsystem testing *)
13 (*****************************************************************************)
15 let test_tokens_c file
=
16 if not
(file
=~
".*\\.c")
17 then pr2
"warning: seems not a .c file";
19 Flag_parsing_c.debug_lexer
:= true;
20 Flag_parsing_c.verbose_lexing
:= true;
21 Flag_parsing_c.verbose_parsing
:= true;
23 Parse_c.tokens file
+> List.iter
(fun x
-> pr2_gen x
);
28 (* ---------------------------------------------------------------------- *)
29 let test_parse_gen xs ext
=
31 Flag_parsing_c.debug_typedef
:= true;
32 Flag_parsing_c.debug_cpp
:= true;
33 Flag_parsing_c.debug_etdt
:= false;
34 Flag_parsing_c.filter_msg
:= true;
38 | [x
] when is_directory x
-> Some x
43 let xs = if !Flag.dir then
44 process_output_to_list ("find " ^ x ^" -name \"*.c\"") else x::xs in
46 let fullxs = Common.files_of_dir_or_files_no_vcs ext
xs in
48 let stat_list = ref [] in
49 let newscore = Common.empty_score
() in
51 Common.check_stack_nbfiles
(List.length
fullxs);
53 fullxs +> List.iter
(fun file
->
54 if not
(file
=~
(".*\\."^ext
))
55 then pr2
("warning: seems not a ."^ext^
" file");
59 pr2
("PARSING: " ^ file
);
61 let (xs, stat
) = Parse_c.parse_c_and_cpp file
in
62 xs +> List.iter
(fun (ast
, (s
, toks
)) ->
63 Parse_c.print_commentized toks
66 Common.push2 stat
stat_list;
68 sprintf
"bad = %d, timeout = %B"
69 stat
.Parsing_stat.bad stat
.Parsing_stat.have_timeout
71 if stat
.Parsing_stat.bad
=|= 0 && not stat
.Parsing_stat.have_timeout
72 then Hashtbl.add
newscore file
(Common.Ok
)
73 else Hashtbl.add
newscore file
(Common.Pb
s)
76 dirname_opt +> Common.do_option
(fun dirname
->
77 pr2_xxxxxxxxxxxxxxxxx
();
78 pr2
"regression testing information";
79 pr2_xxxxxxxxxxxxxxxxx
();
80 let str = Str.global_replace
(Str.regexp
"/") "__" dirname
in
81 let def = if !Flag_parsing_c.filter_define_error
then "_def_" else "" in
82 let ext = if ext =$
= "c" then "" else ext in
83 Common.regression_testing
newscore
84 (Filename.concat
score_path
85 ("score_parsing__" ^
str ^
def ^
ext ^
".marshalled"))
88 if not
(null
!stat_list)
90 Parsing_stat.print_recurring_problematic_tokens
!stat_list;
91 Parsing_stat.print_parsing_stat_list
!stat_list;
100 let test_parse_ch xs =
101 test_parse_gen xs "[ch]"
103 (* could use a simpler parser than heavy parse_c_and_cpp here as there
104 * is no more cpp stuff in the .i files
106 let test_parse_i xs =
107 test_parse_gen xs "i"
115 (* ---------------------------------------------------------------------- *)
116 (* file can be "foo.c" or "foo.c:main" *)
118 let (file
, specific_func
) =
119 if file
=~
"\\(.*\\.c\\):\\(.*\\)"
121 let (a
,b
) = matched2 file
in
127 if not
(file
=~
".*\\.c")
128 then pr2
"warning: seems not a .c file";
130 let (program
, _stat
) = Parse_c.parse_c_and_cpp file
in
132 program
+> List.iter
(fun (e
,_
) ->
134 match specific_func
, e
with
136 | Some
s, Ast_c.Definition
(defbis
,_
) ->
137 s =$
= Ast_c.str_of_name
(defbis
.Ast_c.f_name
)
143 (* old: Flow_to_ast.test !Flag.show_flow def *)
145 let flow = Ast_to_flow.ast_to_control_flow e
in
146 flow +> do_option
(fun flow ->
147 Ast_to_flow.deadcode_detection
flow;
148 let flow = Ast_to_flow.annotate_loop_nodes
flow in
152 if !Flag_cocci.show_before_fixed_flow
154 else Ctlcocci_integration.fix_flow_ctl flow
158 let filename = Filename.temp_file
"output" ".dot" in
159 Ograph_extended.print_ograph_mutable
flow'
(filename) true
161 with Ast_to_flow.Error
(x
) -> Ast_to_flow.report_error x
167 let test_cfg_ifdef file
=
168 let (ast2
, _stat
) = Parse_c.parse_c_and_cpp file
in
169 let ast = Parse_c.program_of_program2 ast2
in
171 let ast = Cpp_ast_c.cpp_ifdef_statementize
ast in
173 ast +> List.iter
(fun e
->
175 let flow = Ast_to_flow.ast_to_control_flow e
in
176 flow +> do_option
(fun flow ->
177 Ast_to_flow.deadcode_detection
flow;
178 let flow = Ast_to_flow.annotate_loop_nodes
flow in
179 Ograph_extended.print_ograph_mutable
flow ("/tmp/output.dot") true
181 with Ast_to_flow.Error
(x
) -> Ast_to_flow.report_error x
185 (* ---------------------------------------------------------------------- *)
186 let test_parse_unparse infile
=
187 if not
(infile
=~
".*\\.c")
188 then pr2
"warning: seems not a .c file";
190 let (program2
, _stat
) = Parse_c.parse_c_and_cpp infile
in
191 let program2_with_ppmethod =
192 program2
+> List.map
(fun x
-> x
, Unparse_c.PPnormal
)
194 Unparse_c.pp_program
program2_with_ppmethod tmpfile;
195 Common.command2
("cat " ^
tmpfile);
196 (* if want see diff of space => no -b -B *)
197 Common.command2
(spf
"diff -u -p %s %s" infile
tmpfile);
198 (* +> Transformation.test_simple_trans1;*)
202 let parse_and_print_sexp file
=
203 let (ast2
,_stat
) = Parse_c.parse_c_and_cpp file
in
204 let ast = Parse_c.program_of_program2 ast2
in
206 Type_annoter_c.annotate_program
!Type_annoter_c.initial_env
ast
210 let sexp = Sexp_ast_c.sexp_of_program ast in
211 let s = Sexp.to_string_hum sexp in
213 Sexp_ast_c.show_info
:= false;
214 let s = Sexp_ast_c.string_of_program
ast in
220 let test_type_c infile
=
221 if not
(infile
=~
".*\\.c")
222 then pr2
"warning: seems not a .c file";
224 Flag_parsing_c.pretty_print_type_info
:= true;
226 let (program2
, _stat
) = Parse_c.parse_c_and_cpp infile
in
230 +> (fun (program
, infos
) ->
231 Type_annoter_c.annotate_program
!Type_annoter_c.initial_env
232 program
+> List.map fst
,
235 +> Common.uncurry
Common.zip
237 let program2_with_ppmethod =
238 program2
+> List.map
(fun x
-> x
, Unparse_c.PPnormal
)
240 Unparse_c.pp_program
program2_with_ppmethod tmpfile;
241 Common.command2
("cat " ^
tmpfile);
245 (* ---------------------------------------------------------------------- *)
246 (* ex: demos/platform_ifdef.c *)
247 let test_comment_annotater infile
=
248 let (program2
, _stat
) = Parse_c.parse_c_and_cpp infile
in
249 let asts = program2
+> List.map
(fun (ast,_
) -> ast) in
250 let toks = program2
+> List.map
(fun (ast, (s, toks)) -> toks) +>
253 Flag_parsing_c.pretty_print_comment_info
:= true;
255 pr2
"pretty print, before comment annotation: --->";
256 Common.adjust_pp_with_indent
(fun () ->
257 asts +> List.iter
(fun ast ->
258 Pretty_print_c.pp_toplevel_simple
ast;
262 let _ = Comment_annotater_c.annotate_program
toks asts in
264 Common.adjust_pp_with_indent
(fun () ->
265 pr2
"pretty print, after comment annotation: --->";
266 asts +> List.iter
(fun ast ->
267 Pretty_print_c.pp_toplevel_simple
ast;
275 (* ---------------------------------------------------------------------- *)
276 (* used by generic_makefile now *)
277 let test_compare_c file1 file2
=
278 let (correct
, diffxs
) = Compare_c.compare_default file1 file2
in
279 let res = Compare_c.compare_result_to_bool correct
in
281 then raise
(Common.UnixExit
0)
282 else raise
(Common.UnixExit
(-1))
285 let test_compare_c_hardcoded () =
286 Compare_c.compare_default
290 "tests/equal_modulo1.c"
291 "tests/equal_modulo2.c"
293 +> Compare_c.compare_result_to_string
298 (* ---------------------------------------------------------------------- *)
299 let test_attributes file
=
300 let (ast2
, _stat
) = Parse_c.parse_c_and_cpp file
in
301 let ast = Parse_c.program_of_program2 ast2
in
303 Visitor_c.vk_program
{ Visitor_c.default_visitor_c
with
304 Visitor_c.kdef
= (fun (k
, bigf
) (defbis
, ii
) ->
305 let sattr = Ast_c.s_of_attr defbis
.f_attr
in
306 pr2
(spf
"%-30s: %s" (Ast_c.str_of_name
(defbis
.f_name
)) sattr);
308 Visitor_c.kdecl
= (fun (k
, bigf
) decl
->
310 | DeclList
(xs, ii
) ->
311 xs +> List.iter
(fun (onedecl
, iicomma
) ->
313 let sattr = Ast_c.s_of_attr onedecl
.v_attr
in
315 match onedecl
.v_namei
with
316 | Some
(name
, ini
) -> Ast_c.str_of_name name
319 pr2
(spf
"%-30s: %s" idname sattr);
328 let cpp_options () = [
329 Cpp_ast_c.I
"/home/yyzhou/pad/linux/include";
331 Cpp_ast_c.cpp_option_of_cmdline
332 (!Flag_parsing_c.cpp_i_opts
,!Flag_parsing_c.cpp_d_opts
)
335 let (ast2
, _stat
) = Parse_c.parse_c_and_cpp file
in
336 let dirname = Filename.dirname file
in
337 let ast = Parse_c.program_of_program2 ast2
in
338 let ast = Cpp_ast_c.cpp_expand_include
(cpp_options()) dirname ast in
339 let _ast = Cpp_ast_c.cpp_ifdef_statementize
ast in
346 (* CONFIG [ch] ? also do for .c ? maybe less needed now that I
349 let extract_macros ~selection dir
=
351 let fullxs = Common.files_of_dir_or_files_no_vcs
ext [dir
] in
352 let macros_and_filename =
353 fullxs +> List.map
(fun file
->
354 pr2
(spf
"processing: %s" file
);
355 let xs = Parse_c.extract_macros file
in
362 then Cpp_analysis_c.extract_dangerous_macros
macros_and_filename
363 else macros_and_filename
365 macros +> List.iter
(fun (file
, defs
) ->
366 pr
("/* PARSING: " ^ file ^
" */");
367 defs
+> List.iter
(fun (s, def) ->
368 let str = Cpp_token_c.string_of_define_def
def in
377 Flag_parsing_c.filter_msg_define_error
:= true;
378 Flag_parsing_c.filter_define_error
:= true;
379 Flag_parsing_c.verbose_lexing
:= false;
380 Flag_parsing_c.verbose_parsing
:= false;
384 | [x
] when is_directory x
-> Some x
387 dirname_opt +> Common.do_option
(fun dir
->
390 let fullxs = Common.files_of_dir_or_files_no_vcs
ext [dir
] in
392 let macros_and_filename =
393 fullxs +> List.map
(fun file
->
394 pr2
(spf
"processing: %s" file
);
395 let xs = Parse_c.extract_macros file
in
400 Cpp_analysis_c.extract_dangerous_macros
macros_and_filename
402 macros +> List.iter
(fun (file
, xs) ->
403 xs +> List.iter
(fun (x
, def) ->
404 let (s, params
, body
) = def in
405 let str = Cpp_token_c.string_of_define_def
def in
408 Hashtbl.replace
!Parse_c._defs_builtins
s (s, params
, body
);
415 let fullxs = Common.files_of_dir_or_files_no_vcs
ext xs in
417 let stat_list = ref [] in
418 Common.check_stack_nbfiles
(List.length
fullxs);
420 fullxs +> List.iter
(fun file
->
421 if not
(file
=~
(".*\\."^
ext))
422 then pr2
("warning: seems not a ."^
ext^
" file");
425 pr2
("PARSING: " ^ file
);
427 let (xs, stat
) = Parse_c.parse_c_and_cpp file
in
428 xs +> List.iter
(fun (ast, (s, toks)) ->
429 Parse_c.print_commentized
toks
432 Common.push2 stat
stat_list;
435 if not
(null
!stat_list)
437 Parsing_stat.print_recurring_problematic_tokens
!stat_list;
438 Parsing_stat.print_parsing_stat_list
!stat_list;
449 (* ---------------------------------------------------------------------- *)
454 ignore(Parse_c.parse_cpp_define_file "standard.h")
458 Format.print_newline();
459 Format.printf "@[<v 5>--@,--@,@[<v 5>--@,--@,@]--@,--@,@]";
460 Format.print_newline();
461 Format.printf "@[<v>(---@[<v>(---@[<v>(---@,)@]@,)@]@,)@]"
466 (*****************************************************************************)
467 (* Main entry for Arg *)
468 (*****************************************************************************)
471 "-tokens_c", " <file>",
472 Common.mk_action_1_arg
test_tokens_c;
473 "-parse_c", " <file or dir>",
474 Common.mk_action_n_arg
test_parse_c;
475 "-parse_h", " <file or dir>",
476 Common.mk_action_n_arg
test_parse_h;
477 "-parse_ch", " <file or dir>",
478 Common.mk_action_n_arg
test_parse_ch;
479 "-parse_i", " <file or dir>",
480 Common.mk_action_n_arg
test_parse_i;
482 "-parse", " <file or dir>",
483 Common.mk_action_n_arg
test_parse;
485 "-show_flow", " <file or file:function>",
486 Common.mk_action_1_arg
test_cfg;
487 "-control_flow", " <file or file:function>",
488 Common.mk_action_1_arg
test_cfg;
489 "-test_cfg_ifdef", " <file>",
490 Common.mk_action_1_arg
test_cfg_ifdef;
491 "-parse_unparse", " <file>",
492 Common.mk_action_1_arg
test_parse_unparse;
493 "-parse_and_print_sexp", " <file>",
494 Common.mk_action_1_arg
parse_and_print_sexp;
495 "-type_c", " <file>",
496 Common.mk_action_1_arg
test_type_c;
497 "-compare_c", " <file1> <file2>",
498 Common.mk_action_2_arg
test_compare_c (* result is in unix code *);
499 "-comment_annotater_c", " <file>",
500 Common.mk_action_1_arg
test_comment_annotater;
502 "-compare_c_hardcoded", " ",
503 Common.mk_action_0_arg
test_compare_c_hardcoded;
505 "-test_attributes", " <file>",
506 Common.mk_action_1_arg
test_attributes;
507 "-test_cpp", " <file>",
508 Common.mk_action_1_arg
test_cpp;
510 "-extract_macros", " <file or dir>",
511 Common.mk_action_1_arg
(extract_macros ~selection
:false) ;
513 "-extract_macros_select", " <file or dir>",
514 Common.mk_action_1_arg
(extract_macros ~selection
:true);
517 "-xxx", " <file1> <>",
518 Common.mk_action_n_arg
test_xxx;