~ubuntu-branches/ubuntu/maverick/coccinelle/maverick

« back to all changes in this revision

Viewing changes to parsing_c/cpp_ast_c.ml

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T Chen
  • Date: 2009-08-10 01:01:24 UTC
  • mfrom: (7.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20090810010124-9fn4c8m93ic60fqx
Tags: 0.1.9.deb-1ubuntu1
* Merge from Debian unstable, remaining changes:
  - debian/control: build-depend on python2.6-dev,
    set XB-Python-Version to 2.6
* New upstream (Debian) packaging applied fix for
  LP: #410907 (see changelog entry below)

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
(*****************************************************************************)
19
19
(* Wrappers *)
20
20
(*****************************************************************************)
21
 
let pr2, pr2_once = Common.mk_pr2_wrappers Flag_parsing_c.verbose_cpp_ast
 
21
let pr2, pr2_once = 
 
22
  Common.mk_pr2_wrappers Flag_parsing_c.verbose_cpp_ast
22
23
let pr2_debug,pr2_debug_once = 
23
24
  Common.mk_pr2_wrappers Flag_parsing_c.debug_cpp_ast
24
25
 
99
100
  ))
100
101
 
101
102
(*****************************************************************************)
 
103
(* Debug *)
 
104
(*****************************************************************************)
 
105
let (show_cpp_i_opts: string list -> unit) = fun xs -> 
 
106
  if not (null xs) then begin 
 
107
    pr2 "-I";
 
108
    xs +> List.iter pr2
 
109
  end
 
110
 
 
111
  
 
112
let (show_cpp_d_opts: string list -> unit) = fun xs ->
 
113
  if not (null xs) then begin
 
114
    pr2 "-D";
 
115
    xs +> List.iter pr2
 
116
  end
 
117
 
 
118
(* ---------------------------------------------------------------------- *)
 
119
let trace_cpp_process depth mark inc_file =
 
120
  pr2_debug (spf "%s>%s %s" 
 
121
          (Common.repeat "-" depth +> Common.join "")
 
122
          mark
 
123
          (s_of_inc_file_bis inc_file));
 
124
  ()
 
125
 
 
126
 
 
127
 
 
128
(*****************************************************************************)
102
129
(* Helpers *)
103
130
(*****************************************************************************)
104
131
 
 
132
 
105
133
let _hcandidates = Hashtbl.create 101
106
134
 
107
135
let init_adjust_candidate_header_files dir = 
172
200
 
173
201
 
174
202
(* ---------------------------------------------------------------------- *)
175
 
let trace_cpp_process depth mark inc_file =
176
 
  pr2_debug (spf "%s>%s %s" 
177
 
          (Common.repeat "-" depth +> Common.join "")
178
 
          mark
179
 
          (s_of_inc_file_bis inc_file));
180
 
  ()
181
 
 
182
 
 
183
 
(* ---------------------------------------------------------------------- *)
184
203
let _headers_hash = Hashtbl.create 101 
185
204
 
186
205
(* On freebsd ocaml is trashing, use up to 1.6Go of memory and then
193
212
 * even if the cache is small. That's because huge single 
194
213
 * ast element and probably the ast marshalling fail.
195
214
 *)
196
 
let threshold_cache_nb_files = ref 200
197
 
 
198
 
let parse_c_and_cpp_cache file =
199
 
  if Hashtbl.length _headers_hash > !threshold_cache_nb_files
 
215
let default_threshold_cache_nb_files = 200
 
216
 
 
217
let parse_c_and_cpp_cache 
 
218
  ?(threshold_cache_nb_files= default_threshold_cache_nb_files) file =
 
219
 
 
220
  if Hashtbl.length _headers_hash > threshold_cache_nb_files
200
221
  then Hashtbl.clear _headers_hash;
201
222
 
202
223
  Common.memoized _headers_hash file (fun () -> 
204
225
  )
205
226
 
206
227
 
207
 
(* ---------------------------------------------------------------------- *)
208
 
let (show_cpp_i_opts: string list -> unit) = fun xs -> 
209
 
  if not (null xs) then begin 
210
 
    pr2 "-I";
211
 
    xs +> List.iter pr2
212
 
  end
213
 
 
214
 
  
215
 
let (show_cpp_d_opts: string list -> unit) = fun xs ->
216
 
  if not (null xs) then begin
217
 
    pr2 "-D";
218
 
    xs +> List.iter pr2
219
 
  end
220
228
 
221
229
(*****************************************************************************)
222
230
(* Main entry *)
225
233
 
226
234
let (cpp_expand_include2: 
227
235
 ?depth_limit:int option ->
 
236
 ?threshold_cache_nb_files:int ->
228
237
 cpp_option list -> Common.dirname -> Ast_c.program -> Ast_c.program) =
229
 
 fun ?(depth_limit=None) iops dirname ast -> 
 
238
 fun ?(depth_limit=None) ?threshold_cache_nb_files iops dirname ast -> 
230
239
 
231
240
  if !Flag_parsing_c.debug_cpp_ast
232
241
  then pr2_xxxxxxxxxxxxxxxxx();
262
271
                  (* CONFIG *)
263
272
                  Flag_parsing_c.verbose_parsing := false; 
264
273
                  Flag_parsing_c.verbose_lexing := false; 
265
 
                  let (ast2, _stat) = parse_c_and_cpp_cache file in
 
274
                  let (ast2, _stat) = 
 
275
                    parse_c_and_cpp_cache ?threshold_cache_nb_files file 
 
276
                  in
266
277
 
267
278
                  let ast = Parse_c.program_of_program2 ast2 in
268
279
                  let dirname' = Filename.dirname file in 
293
304
  aux [] dirname ast
294
305
    
295
306
 
296
 
let cpp_expand_include ?depth_limit a b c = 
 
307
let cpp_expand_include ?depth_limit ?threshold_cache_nb_files a b c = 
297
308
  Common.profile_code "cpp_expand_include"
298
 
   (fun () -> cpp_expand_include2 ?depth_limit a b c)
 
309
   (fun () -> cpp_expand_include2 ?depth_limit ?threshold_cache_nb_files a b c)
299
310
 
300
311
(* 
301
312
let unparse_showing_include_content ?
325
336
 * indice. Or simply count  the number of directives with the same tag and
326
337
 * put this information in the tag. Hence the total_with_this_tag below.
327
338
 *)
328
 
let should_ifdefize tag ifdefs_directives xxs = 
 
339
let should_ifdefize (tag,ii) ifdefs_directives xxs = 
329
340
  let IfdefTag (_tag, total_with_this_tag) = tag in
330
341
  
331
342
  if total_with_this_tag <> List.length ifdefs_directives
332
343
  then begin
333
 
    pr2 "CPPASTC: can not ifdefize, some of its directives were passed";
 
344
    let strloc = Ast_c.strloc_of_info (List.hd ii) in
 
345
    pr2 (spf "CPPASTC: can not ifdefize ifdef at %s" strloc);
 
346
    pr2 "CPPASTC: some of its directives were passed";
334
347
    false 
335
348
  end else 
336
349
    (* todo? put more condition ? dont ifdefize declaration ? *)
373
386
                | IfdefDirective ((Ifdef,tag),ii) -> 
374
387
 
375
388
                    let (restifdefs, xxs, xs') = group_ifdef tag xs in
376
 
                    if should_ifdefize tag (ifdef::restifdefs) xxs 
 
389
                    if should_ifdefize (tag,ii) (ifdef::restifdefs) xxs 
377
390
                    then
378
391
                      let res = IfdefStmt2 (ifdef::restifdefs, xxs) in
379
392
                      Visitor_c.vk_statement_sequencable_s bigf res::aux xs'