3 * Copyright (C) 2008, 2009 University of Urbana Champaign
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License (GPL)
7 * version 2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * file license.txt for more details.
18 (*****************************************************************************)
20 (*****************************************************************************)
21 let pr2, pr2_once
= Common.mk_pr2_wrappers
Flag_parsing_c.verbose_cpp_ast
22 let pr2_debug,pr2_debug_once
=
23 Common.mk_pr2_wrappers
Flag_parsing_c.debug_cpp_ast
25 (*****************************************************************************)
26 (* Cpp Ast Manipulations *)
27 (*****************************************************************************)
30 * cpp-include-expander-builtin.
32 * alternative1: parse and call cpp tour a tour. So let cpp work at
33 * the token level. That's what most tools do.
34 * alternative2: apply cpp at the very end. Process that go through ast
35 * and do the stuff such as #include, macro expand,
36 * ifdef but on the ast!
38 * But need keep those info in ast at least, even bad
39 * macro for instance, and for parse error region ? maybe can
40 * get another chance ?
41 * I think it's better to do the cpp-include-expander in a different step
42 * rather than embedding it in the parser. The parser is already too complex.
43 * Also keep with the tradition to try to parse as-is.
45 * todo? but maybe could discover new info that could help reparse
46 * the ParseError in original file. Try again parsing it by
47 * putting it in a minifile ?
50 * todo? maybe can do some pass that work at the ifdef level and for instance
51 * try to paren them, so have in Ast some stuff that are not
52 * present at parsing time but that can then be constructed after
53 * some processing (a little bit like my type for expression filler,
54 * or position info filler, or include relative position filler).
56 * ??add such info about what was done somewhere ? could build new
57 * ??ast each time but too tedious (maybe need delta-programming!)
59 * todo? maybe change cpp_ast_c to go deeper on local "" ?
67 * cpp_ifdef_statementize: again better to separate concern and in parser
68 * just add the directives in a flat way (IfdefStmt) and later do more
69 * processing and transform them in a tree with some IfdefStmt2.
74 (*****************************************************************************)
76 (*****************************************************************************)
80 | D
of string * string option
84 let i_of_cpp_options xs
=
85 xs
+> Common.map_filter
(function
90 let cpp_option_of_cmdline (xs
, ys
) =
91 (xs
+> List.map
(fun s
-> I s
)) ++
92 (ys
+> List.map
(fun s
->
93 if s
=~
"\\([A-Z][A-Z0-9_]*\\)=\\(.*\\)"
95 let (def
, value) = matched2 s
in
101 (*****************************************************************************)
103 (*****************************************************************************)
105 let _hcandidates = Hashtbl.create
101
107 let init_adjust_candidate_header_files dir
=
109 let files = Common.files_of_dir_or_files
ext [dir
] in
111 files +> List.iter
(fun file
->
112 let base = Filename.basename file
in
114 Hashtbl.add
_hcandidates base file
;
120 (* may return a list of match ? *)
121 let find_header_file1 cppopts dirname inc_file
=
125 Filename.concat dirname
(Ast_c.s_of_inc_file inc_file
) in
126 if Sys.file_exists
finalfile
130 i_of_cpp_options cppopts
+> Common.map_filter
(fun dirname
->
132 Filename.concat dirname
(Ast_c.s_of_inc_file inc_file
) in
133 if Sys.file_exists
finalfile
138 pr2 ("CPPAST: weird include not handled:" ^ s
);
141 (* todo? can try find most precise ? first just use basename but
142 * then maybe look if have also some dir in common ?
144 let find_header_file2 inc_file
=
148 let s = (Ast_c.s_of_inc_file inc_file
) in
149 let base = Filename.basename
s in
151 let res = Hashtbl.find_all
_hcandidates base in
154 pr2_debug ("CPPAST: find header in other dir: " ^ file
);
164 let find_header_file cppopts dirname inc_file
=
165 let res1 = find_header_file1 cppopts dirname inc_file
in
168 | [] -> find_header_file2 inc_file
174 (* ---------------------------------------------------------------------- *)
175 let trace_cpp_process depth mark inc_file
=
176 pr2_debug (spf
"%s>%s %s"
177 (Common.repeat
"-" depth
+> Common.join
"")
179 (s_of_inc_file_bis inc_file
));
183 (* ---------------------------------------------------------------------- *)
184 let _headers_hash = Hashtbl.create
101
186 (* On freebsd ocaml is trashing, use up to 1.6Go of memory and then
187 * building the database_c takes ages.
189 * So just limit with following threshold to avoid this trashing, simple.
191 * On netbsd, got a Out_of_memory exn on this file;
192 * /home/pad/software-os-src2/netbsd/dev/microcode/cyclades-z/
193 * even if the cache is small. That's because huge single
194 * ast element and probably the ast marshalling fail.
196 let threshold_cache_nb_files = ref 200
198 let parse_c_and_cpp_cache file
=
199 if Hashtbl.length
_headers_hash > !threshold_cache_nb_files
200 then Hashtbl.clear
_headers_hash;
202 Common.memoized
_headers_hash file
(fun () ->
203 Parse_c.parse_c_and_cpp file
207 (* ---------------------------------------------------------------------- *)
208 let (show_cpp_i_opts
: string list
-> unit) = fun xs
->
209 if not
(null xs
) then begin
215 let (show_cpp_d_opts
: string list
-> unit) = fun xs
->
216 if not
(null xs
) then begin
221 (*****************************************************************************)
223 (*****************************************************************************)
226 let (cpp_expand_include2
:
227 ?depth_limit
:int option ->
228 cpp_option list
-> Common.dirname
-> Ast_c.program
-> Ast_c.program
) =
229 fun ?
(depth_limit
=None
) iops dirname ast
->
231 if !Flag_parsing_c.debug_cpp_ast
232 then pr2_xxxxxxxxxxxxxxxxx
();
234 let already_included = ref [] in
236 let rec aux stack dirname ast
=
237 let depth = List.length stack
in
239 ast
+> Visitor_c.vk_program_s
{ Visitor_c.default_visitor_c_s
with
240 Visitor_c.kcppdirective_s
= (fun (k
, bigf
) cpp ->
242 | Include
{i_include
= (inc_file
, ii
);
243 i_rel_pos
= h_rel_pos
;
248 (match depth_limit
with
249 | Some limit
when depth >= limit
-> cpp
252 (match find_header_file iops dirname inc_file
with
254 if List.mem file
!already_included
256 (* pr2 ("already included: " ^ file); *)
257 trace_cpp_process depth "*" inc_file
;
260 trace_cpp_process depth "" inc_file
;
261 Common.push2 file
already_included;
263 Flag_parsing_c.verbose_parsing
:= false;
264 Flag_parsing_c.verbose_lexing
:= false;
265 let (ast2
, _stat
) = parse_c_and_cpp_cache file
in
267 let ast = Parse_c.program_of_program2 ast2
in
268 let dirname'
= Filename.dirname file
in
271 let ast'
= aux (file
::stack
) dirname'
ast in
273 Include
{i_include
= (inc_file
, ii
);
274 i_rel_pos
= h_rel_pos
;
276 i_content
= Some
(file
, ast'
);
280 trace_cpp_process depth "!!" inc_file
;
281 pr2 "CPPAST: file not found";
284 trace_cpp_process depth "!!" inc_file
;
285 pr2 "CPPAST: too much candidates";
296 let cpp_expand_include ?depth_limit a b c
=
297 Common.profile_code
"cpp_expand_include"
298 (fun () -> cpp_expand_include2 ?depth_limit a b c
)
301 let unparse_showing_include_content ?
305 (*****************************************************************************)
306 (* Ifdef-statementize *)
307 (*****************************************************************************)
310 let is_ifdef_and_same_tag tag x
=
312 | IfdefStmt
(IfdefDirective
((_
, tag2
),_
)) ->
314 | StmtElem _
| CppDirectiveStmt _
-> false
315 | IfdefStmt2 _
-> raise Impossible
319 (* What if I skipped in the parser only some of the ifdef elements
320 * of the same tag. Once I passed one, I should pass all of them and so
321 * at least should detect here that one tag is not "valid". Maybe in the parser
322 * can return or marked some tags as "partially_passed_ifdef_tag".
323 * Maybe could do in ast_c a MatchingTag of int * bool ref (* one_was_passed *)
324 * where the
ref will be shared by the ifdefs
with the same matching tag
325 * indice
. Or simply count the number
of directives
with the same tag
and
326 * put this information
in the tag
. Hence the total_with_this_tag below
.
328 let should_ifdefize tag ifdefs_directives xxs
=
329 let IfdefTag (_tag
, total_with_this_tag
) = tag
in
331 if total_with_this_tag
<> List.length ifdefs_directives
333 pr2 "CPPASTC: can not ifdefize, some of its directives were passed";
336 (* todo? put more condition ? dont ifdefize declaration ? *)
343 (* return a triple, (ifdefs directive * grouped xs * remaining sequencable)
344 * XXX1 XXX2 elsif YYY1 else ZZZ1 endif WWW1 WWW2
345 * => [elsif, else, endif], [XXX1 XXX2; YYY1; ZZZ1], [WWW1 WWW2]
347 let group_ifdef tag xs
=
348 let (xxs
, xs
) = group_by_post
(is_ifdef_and_same_tag tag
) xs
in
350 xxs
+> List.map snd
+> List.map
(fun x
->
353 | StmtElem _
| CppDirectiveStmt _
| IfdefStmt2 _
-> raise Impossible
359 let rec cpp_ifdef_statementize ast =
360 Visitor_c.vk_program_s
{ Visitor_c.default_visitor_c_s
with
361 Visitor_c.kstatementseq_list_s
= (fun (k
, bigf
) xs
->
368 Visitor_c.vk_statement_sequencable_s bigf stseq
::aux xs
369 | CppDirectiveStmt directive
->
370 Visitor_c.vk_statement_sequencable_s bigf stseq
::aux xs
373 | IfdefDirective
((Ifdef
,tag
),ii
) ->
375 let (restifdefs
, xxs
, xs'
) = group_ifdef tag xs
in
376 if should_ifdefize tag
(ifdef
::restifdefs
) xxs
378 let res = IfdefStmt2
(ifdef
::restifdefs
, xxs
) in
379 Visitor_c.vk_statement_sequencable_s bigf
res::aux xs'
381 Visitor_c.vk_statement_sequencable_s bigf stseq
::aux xs
383 | IfdefDirective
(((IfdefElseif
|IfdefElse
|IfdefEndif
),b
),ii
) ->
384 pr2 "weird: first directive is not a ifdef";
385 (* maybe not weird, just that should_ifdefize
387 Visitor_c.vk_statement_sequencable_s bigf stseq
::aux xs
390 | IfdefStmt2
(ifdef
, xxs
) ->
391 failwith
"already applied cpp_ifdef_statementize"
399 (*****************************************************************************)
401 (*****************************************************************************)
403 let (cpp_expand_macro_expr
:
404 Ast_c.define_kind
-> Ast_c.argument
Ast_c.wrap2 list
->
405 Ast_c.expression
option) =