This repository has been archived by the owner on Jun 4, 2019. It is now read-only.
/
parsing_hacks.ml
3129 lines (2517 loc) · 89.6 KB
/
parsing_hacks.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(* Yoann Padioleau
*
* Copyright (C) 2002-2008 Yoann Padioleau
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License (GPL)
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* file license.txt for more details.
*)
open Common
module TH = Token_helpers_cpp
module LP = Lexer_parser_cpp
open Parser_cpp
module Flag = Flag_parsing_cpp
module Parser = Parser_cpp
module Ast = Ast_cpp
module Stat = Parsing_stat_cpp
(*****************************************************************************)
(* Some debugging functions *)
(*****************************************************************************)
let pr2 s =
if !Flag.verbose_parsing
then Common.pr2 s
let pr2_pp s =
if !Flag.debug_pp
then Common.pr2_once ("PP-" ^ s)
let pr2_cplusplus s =
if !Flag.debug_cplusplus
then Common.pr2_once ("C++-" ^ s)
(* In the following, there are some harcoded names of types or macros
* but they are not used by our heuristics! They are just here to
* enable to detect false positive by printing only the typedef/macros
* that we don't know yet. If we print everything, then we can easily
* get lost with too much verbose tracing information. So those
* functions "filter" some messages. So our heuristics are still good,
* there is no more (or not that much) hardcoded linux stuff.
*)
let msg_gen cond is_known printer s =
if cond
then
if not (!Flag.filter_msg)
then printer s
else
if not (is_known s)
then printer s
(* note: cant use partial application with let msg_typedef =
* because it would compute msg_typedef at compile time when
* the flag debug_typedef is always false
*)
let msg_typedef (s,ii) =
msg_gen (!Flag.debug_typedef)
(fun s ->
(match s with
| "u_char" | "u_short" | "u_int" | "u_long"
| "u8" | "u16" | "u32" | "u64"
| "s8" | "s16" | "s32" | "s64"
| "__u8" | "__u16" | "__u32" | "__u64"
-> true
| "acpi_handle"
| "acpi_status"
-> true
| "FILE"
| "DIR"
-> true
| s when s =~ ".*_t$" -> true
| _ -> false
)
)
(fun s ->
pr2_pp ("TYPEDEF: promoting: " ^ s);
if !Flag.debug_typedef_location
then
pr2_pp ("loc: " ^
Common.string_of_parse_info (Ast.parse_info_of_info ii))
)
s
let msg_declare_macro s =
msg_gen (!Flag.debug_pp)
(fun s ->
(match s with
| "DECLARE_MUTEX" | "DECLARE_COMPLETION" | "DECLARE_RWSEM"
| "DECLARE_WAITQUEUE" | "DECLARE_WAIT_QUEUE_HEAD"
| "DEFINE_SPINLOCK" | "DEFINE_TIMER"
| "DEVICE_ATTR" | "CLASS_DEVICE_ATTR" | "DRIVER_ATTR"
| "SENSOR_DEVICE_ATTR"
| "LIST_HEAD"
| "DECLARE_WORK" | "DECLARE_TASKLET"
| "PORT_ATTR_RO" | "PORT_PMA_ATTR"
| "DECLARE_BITMAP"
-> true
(*
| s when s =~ "^DECLARE_.*" -> true
| s when s =~ ".*_ATTR$" -> true
| s when s =~ "^DEFINE_.*" -> true
| s when s =~ "NS_DECL.*" -> true
*)
| _ -> false
)
)
(fun s -> pr2_pp ("MACRO: found declare-macro: " ^ s))
s
let msg_foreach s =
pr2_pp ("MACRO: found foreach: " ^ s)
let msg_debug_macro s =
pr2_pp ("MACRO: found debug-macro: " ^ s)
let msg_macro_noptvirg s =
pr2_pp ("MACRO: found macro with param noptvirg: " ^ s)
let msg_macro_toplevel_noptvirg s =
pr2_pp ("MACRO: found toplevel macro noptvirg: " ^ s)
let msg_macro_noptvirg_single s =
pr2_pp ("MACRO: found single-macro noptvirg: " ^ s)
let msg_macro_higher_order s =
msg_gen (!Flag.debug_pp)
(fun s ->
(match s with
| "DBGINFO"
| "DBGPX"
| "DFLOW"
-> true
| _ -> false
)
)
(fun s -> pr2_pp ("MACRO: found higher ordre macro : " ^ s))
s
let msg_stringification s =
msg_gen (!Flag.debug_pp)
(fun s ->
(match s with
| "REVISION"
| "UTS_RELEASE"
| "SIZE_STR"
| "DMA_STR"
-> true
(* s when s =~ ".*STR.*" -> true *)
| _ -> false
)
)
(fun s -> pr2_pp ("MACRO: found string-macro " ^ s))
s
let msg_classname s =
pr2_cplusplus ("CLASSNAME: found " ^ s)
let msg_templatename s =
pr2_cplusplus ("TEMPLATENAME: found " ^ s)
let msg_constructorname s =
pr2_cplusplus ("CONSTRUCTORNAME: found " ^ s)
(*****************************************************************************)
(* The regexp and basic view definitions *)
(*****************************************************************************)
(* opti: better to built then once and for all, especially regexp_foreach *)
let regexp_macro = Str.regexp
"^[A-Z_][A-Z_0-9]*$"
(* linuxext: *)
let regexp_annot = Str.regexp
"^__.*$"
(* linuxext: *)
let regexp_declare = Str.regexp
".*DECLARE.*"
(* linuxext: *)
let regexp_foreach = Str.regexp_case_fold
".*\\(for_?each\\|for_?all\\|iterate\\|loop\\|walk\\|scan\\|each\\|for\\)"
(* firefoxext: *)
let regexp_ns_decl_like = Str.regexp
("\\(" ^
"NS_DECL_\\|NS_DECLARE_\\|NS_IMPL_\\|" ^
"NS_IMPLEMENT_\\|NS_INTERFACE_\\|NS_FORWARD_\\|NS_HTML_\\|" ^
"NS_DISPLAY_\\|NS_IMPL_\\|" ^
"TX_DECL_\\|DOM_CLASSINFO_\\|NS_CLASSINFO_\\|IMPL_INTERNAL_\\|" ^
"ON_\\|EVT_\\|NS_UCONV_\\|NS_GENERIC_\\|NS_COM_" ^
"\\).*")
let regexp_typedef = Str.regexp
".*_t$"
let false_typedef = [
"printk";
]
let ok_typedef s = not (List.mem s false_typedef)
let not_annot s =
not (s ==~ regexp_annot)
type define_body = (unit,string list) either * Parser.token list
let (_defs : (string, define_body) Hashtbl.t ref) =
ref (Hashtbl.create 101)
(* ------------------------------------------------------------------------- *)
(* fuzzy parsing, different "views" over the same program *)
(* ------------------------------------------------------------------------- *)
(* Normally I should not use ref/mutable in the token_extended type
* and I should have a set of functions taking a list of tokens and
* returning a list of tokens. The problem is that to make easier some
* functions, it is better to work on better representation, on "views"
* over this list of tokens. But then modifying those views and get
* back from those views to the original simple list of tokens is
* tedious. One way is to maintain next to the view a list of "actions"
* (I was using a hash storing the charpos of the token and associating
* the action) but it is tedious too. Simpler to use mutable/ref. We
* use the same idea that we use when working on the Ast_c. *)
(* old: when I was using the list of "actions" next to the views, the hash
* indexed by the charpos, there could have been some problems:
* how my fake_pos interact with the way I tag and adjust token ?
* because I base my tagging on the position of the token ! so sometimes
* could tag another fakeInfo that should not be tagged ?
* fortunately I don't use anymore this technique.
*)
(* update: quite close to the Place_c.Inxxx.
* update: now that can have complex nesting of class/func in c++,
* this type is also present in duplicated form in lexer_parser2.ml
*
* The strategy to set the tag is simply to look at the token
* before the '{'.
*)
type context =
InFunction | InEnum | InStruct | InInitializer | NoContext
type token_extended = {
mutable tok: Parser.token;
mutable where: context;
(* less: need also a after ? *)
mutable new_tokens_before : Parser.token list;
(* line x col cache, more easily accessible, of the info in the token *)
line: int;
col : int;
}
let set_as_comment cppkind x =
(* normally the caller have first filtered the set of tokens to have
* a clearer "view" to work on
*)
assert(not (TH.is_real_comment x.tok));
if TH.is_eof x.tok
then () (* otherwise parse_c will be lost if don't find a EOF token *)
else
x.tok <- TCommentCpp (cppkind, TH.info_of_tok x.tok)
let set_as_opar_cplusplus xs =
match xs with
| ({tok = TOPar ii} as tok1)::xs ->
pr2_cplusplus "TOParCplusplusInit";
tok1.tok <- TOParCplusplusInit ii;
| _ -> raise Impossible
let mk_token_extended x =
let (line, col) = TH.linecol_of_tok x in
{ tok = x;
line = line; col = col;
where = NoContext;
new_tokens_before = [];
}
(* x list list, because x list separated by ',' *)
type paren_grouped =
| Parenthised of paren_grouped list list * token_extended list
| PToken of token_extended
type brace_grouped =
| Braceised of
brace_grouped list list * token_extended * token_extended option
| BToken of token_extended
(* Far better data structure than doing hacks in the lexer or parser
* because in lexer we don't know to which ifdef a endif is related
* and so when we want to comment a ifdef, we don't know which endif
* we must also comment. Especially true for the #if 0 which sometimes
* have a #else part.
*
* x list list, because x list separated by #else or #elif
*)
type ifdef_grouped =
| Ifdef of ifdef_grouped list list * token_extended list
| Ifdefbool of bool * ifdef_grouped list list * token_extended list
| NotIfdefLine of token_extended list
type 'a line_grouped =
Line of 'a list
type body_function_grouped =
| BodyFunction of token_extended list
| NotBodyLine of token_extended list
(* ------------------------------------------------------------------------- *)
(* view builders *)
(* ------------------------------------------------------------------------- *)
(* todo: synchro ! use more indentation
* if paren not closed and same indentation level, certainly because
* part of a mid-ifdef-expression.
*)
let rec mk_parenthised xs =
match xs with
| [] -> []
| x::xs ->
(match x.tok with
| xx when TH.is_opar xx ->
let body, extras, xs = mk_parameters [x] [] xs in
Parenthised (body,extras)::mk_parenthised xs
| _ ->
PToken x::mk_parenthised xs
)
(* return the body of the parenthised expression and the rest of the tokens *)
and mk_parameters extras acc_before_sep xs =
match xs with
| [] ->
(* maybe because of #ifdef which "opens" '(' in 2 branches *)
pr2 "PB: not found closing paren in fuzzy parsing";
[List.rev acc_before_sep], List.rev extras, []
| x::xs ->
(match x.tok with
(* synchro *)
| xx when TH.is_obrace xx && x.col = 0 ->
pr2 "PB: found synchro point } in paren";
[List.rev acc_before_sep], List.rev (extras), (x::xs)
| xx when TH.is_cpar xx ->
[List.rev acc_before_sep], List.rev (x::extras), xs
| xx when TH.is_opar xx ->
let body, extrasnest, xs = mk_parameters [x] [] xs in
mk_parameters extras
(Parenthised (body,extrasnest)::acc_before_sep)
xs
| TComma _ ->
let body, extras, xs = mk_parameters (x::extras) [] xs in
(List.rev acc_before_sep)::body, extras, xs
| _ ->
mk_parameters extras (PToken x::acc_before_sep) xs
)
let rec mk_braceised xs =
match xs with
| [] -> []
| x::xs ->
(match x.tok with
| xx when TH.is_obrace xx ->
let body, endbrace, xs = mk_braceised_aux [] xs in
Braceised (body, x, endbrace)::mk_braceised xs
| xx when TH.is_cbrace xx ->
pr2 "PB: found closing brace alone in fuzzy parsing";
BToken x::mk_braceised xs
| _ ->
BToken x::mk_braceised xs
)
(* return the body of the parenthised expression and the rest of the tokens *)
and mk_braceised_aux acc xs =
match xs with
| [] ->
(* maybe because of #ifdef which "opens" '(' in 2 branches *)
pr2 "PB: not found closing brace in fuzzy parsing";
[List.rev acc], None, []
| x::xs ->
(match x.tok with
| xx when TH.is_cbrace xx -> [List.rev acc], Some x, xs
| xx when TH.is_obrace xx ->
let body, endbrace, xs = mk_braceised_aux [] xs in
mk_braceised_aux (Braceised (body,x, endbrace)::acc) xs
| _ ->
mk_braceised_aux (BToken x::acc) xs
)
let rec mk_ifdef xs =
match xs with
| [] -> []
| x::xs ->
(match x.tok with
| TIfdef _ ->
let body, extra, xs = mk_ifdef_parameters [x] [] xs in
Ifdef (body, extra)::mk_ifdef xs
| TIfdefBool (b,_) ->
let body, extra, xs = mk_ifdef_parameters [x] [] xs in
(* if not passing, then consider a #if 0 as an ordinary #ifdef *)
if !Flag.if0_passing
then Ifdefbool (b, body, extra)::mk_ifdef xs
else Ifdef(body, extra)::mk_ifdef xs
| TIfdefMisc (b,_) | TIfdefVersion (b,_) ->
let body, extra, xs = mk_ifdef_parameters [x] [] xs in
Ifdefbool (b, body, extra)::mk_ifdef xs
| _ ->
(* todo? can have some Ifdef in the line ? *)
let line, xs = Common.span (fun y -> y.line = x.line) (x::xs) in
NotIfdefLine line::mk_ifdef xs
)
and mk_ifdef_parameters extras acc_before_sep xs =
match xs with
| [] ->
(* Note that mk_ifdef is assuming that CPP instruction are alone
* on their line. Because I do a span (fun x -> is_same_line ...)
* I might take with me a #endif if this one is mixed on a line
* with some "normal" tokens.
*)
pr2 "PB: not found closing ifdef in fuzzy parsing";
[List.rev acc_before_sep], List.rev extras, []
| x::xs ->
(match x.tok with
| TEndif _ ->
[List.rev acc_before_sep], List.rev (x::extras), xs
| TIfdef _ ->
let body, extrasnest, xs = mk_ifdef_parameters [x] [] xs in
mk_ifdef_parameters
extras (Ifdef (body, extrasnest)::acc_before_sep) xs
| TIfdefBool (b,_) ->
let body, extrasnest, xs = mk_ifdef_parameters [x] [] xs in
if !Flag.if0_passing
then
mk_ifdef_parameters
extras (Ifdefbool (b, body, extrasnest)::acc_before_sep) xs
else
mk_ifdef_parameters
extras (Ifdef (body, extrasnest)::acc_before_sep) xs
| TIfdefMisc (b,_) | TIfdefVersion (b,_) ->
let body, extrasnest, xs = mk_ifdef_parameters [x] [] xs in
mk_ifdef_parameters
extras (Ifdefbool (b, body, extrasnest)::acc_before_sep) xs
| TIfdefelse _
| TIfdefelif _ ->
let body, extras, xs = mk_ifdef_parameters (x::extras) [] xs in
(List.rev acc_before_sep)::body, extras, xs
| _ ->
let line, xs = Common.span (fun y -> y.line = x.line) (x::xs) in
mk_ifdef_parameters extras (NotIfdefLine line::acc_before_sep) xs
)
(* --------------------------------------- *)
let line_of_paren = function
| PToken x -> x.line
| Parenthised (xxs, info_parens) ->
(match info_parens with
| [] -> raise Impossible
| x::xs -> x.line
)
let rec span_line_paren line = function
| [] -> [],[]
| x::xs ->
(match x with
| PToken tok when TH.is_eof tok.tok ->
[], x::xs
| _ ->
if line_of_paren x = line
then
let (l1, l2) = span_line_paren line xs in
(x::l1, l2)
else ([], x::xs)
)
(* old
let rec mk_line_parenthised xs =
match xs with
| [] -> []
| x::xs ->
let line_no = line_of_paren x in
let line, xs = span_line_paren line_no xs in
Line (x::line)::mk_line_parenthised xs
*)
(* --------------------------------------- *)
let line_range_of_paren = function
| PToken x -> x.line, x.line
| Parenthised (xxs, info_parens) ->
(match info_parens with
| [] -> raise Impossible
| x::xs ->
let lines_no = (x::xs) +> List.map (fun x -> x.line) in
Common.minimum lines_no, Common.maximum lines_no
)
let rec span_line_paren_range (imin, imax) = function
| [] -> [],[]
| x::xs ->
(match x with
| PToken tok when TH.is_eof tok.tok ->
[], x::xs
| _ ->
if line_of_paren x >= imin && line_of_paren x <= imax
then
(* may need to extend *)
let (imin', imax') = line_range_of_paren x in
let (l1, l2) = span_line_paren_range (imin, max imax imax') xs in
(x::l1, l2)
else ([], x::xs)
)
let rec mk_line_parenthised xs =
match xs with
| [] -> []
| x::xs ->
let line_range = line_range_of_paren x in
let line, xs = span_line_paren_range line_range xs in
Line (x::line)::mk_line_parenthised xs
(* --------------------------------------- *)
let rec mk_body_function_grouped xs =
match xs with
| [] -> []
| x::xs ->
(match x with
| {tok = TOBrace _; col = 0} ->
let is_closing_brace = function
| {tok = TCBrace _; col = 0 } -> true
| _ -> false
in
let body, xs = Common.span (fun x -> not (is_closing_brace x)) xs in
(match xs with
| ({tok = TCBrace _; col = 0 })::xs ->
BodyFunction body::mk_body_function_grouped xs
| [] ->
pr2 "PB:not found closing brace in fuzzy parsing";
[NotBodyLine body]
| _ -> raise Impossible
)
| _ ->
let line, xs = Common.span (fun y -> y.line = x.line) (x::xs) in
NotBodyLine line::mk_body_function_grouped xs
)
(* --------------------------------------- *)
let is_braceised = function
| Braceised _ -> true
| BToken _ -> false
(* ------------------------------------------------------------------------- *)
(* view iterators *)
(* ------------------------------------------------------------------------- *)
let rec iter_token_paren f xs =
xs +> List.iter (function
| PToken tok -> f tok;
| Parenthised (xxs, info_parens) ->
info_parens +> List.iter f;
xxs +> List.iter (fun xs -> iter_token_paren f xs)
)
let rec iter_token_brace f xs =
xs +> List.iter (function
| BToken tok -> f tok;
| Braceised (xxs, tok1, tok2opt) ->
f tok1; do_option f tok2opt;
xxs +> List.iter (fun xs -> iter_token_brace f xs)
)
let rec iter_token_ifdef f xs =
xs +> List.iter (function
| NotIfdefLine xs -> xs +> List.iter f;
| Ifdefbool (_, xxs, info_ifdef)
| Ifdef (xxs, info_ifdef) ->
info_ifdef +> List.iter f;
xxs +> List.iter (iter_token_ifdef f)
)
let tokens_of_paren xs =
let g = ref [] in
xs +> iter_token_paren (fun tok -> push2 tok g);
List.rev !g
let tokens_of_paren_ordered xs =
let g = ref [] in
let rec aux_tokens_ordered = function
| PToken tok -> push2 tok g;
| Parenthised (xxs, info_parens) ->
let (opar, cpar, commas) =
match info_parens with
| opar::xs ->
(match List.rev xs with
| cpar::xs ->
opar, cpar, List.rev xs
| _ -> raise Impossible
)
| _ -> raise Impossible
in
push2 opar g;
aux_args (xxs,commas);
push2 cpar g;
and aux_args (xxs, commas) =
match xxs, commas with
| [], [] -> ()
| [xs], [] -> xs +> List.iter aux_tokens_ordered
| xs::ys::xxs, comma::commas ->
xs +> List.iter aux_tokens_ordered;
push2 comma g;
aux_args (ys::xxs, commas)
| _ -> raise Impossible
in
xs +> List.iter aux_tokens_ordered;
List.rev !g
(* ------------------------------------------------------------------------- *)
(* set the context info in token *)
(* ------------------------------------------------------------------------- *)
(* All important context are introduced via some '{' '}'. To make the
* difference is it often enough to just look at the few tokens before the
* '{'.
*
* TODO harder now that have c++, can have function inside struct so need
* handle all together.
*
* TODO So change token but do not recurse in
* nested Braceised. maybe do via accumulator, don't use iter_token_brace.
*
* TODO Also need remove the qualifier as they make
* the sequence pattern matching more difficult.
*)
let rec set_in_function_tag xs =
(* could try: ) { } but it can be the ) of a if or while, so
* better to base the heuristic on the position in column zero.
* Note that some struct or enum or init put also their { in first column
* but set_in_other will overwrite the previous InFunction tag.
*)
let rec aux xs =
match xs with
| [] -> ()
(*TODOC++ext: now can have some const or throw between
=> do a view that filter them first ?
*)
(* ) { and the closing } is in column zero, then certainly a function *)
(*TODO1 col 0 not valid anymore with c++ nestedness of method *)
| BToken ({tok = TCPar _ })::(Braceised (body, tok1, Some tok2))::xs
when tok1.col <> 0 && tok2.col = 0 ->
body +> List.iter (iter_token_brace (fun tok ->
tok.where <- InFunction
));
aux xs
| (BToken x)::xs -> aux xs
(*TODO1 not valid anymore with c++ nestedness of method *)
| (Braceised (body, tok1, Some tok2))::xs
when tok1.col = 0 && tok2.col = 0 ->
body +> List.iter (iter_token_brace (fun tok ->
tok.where <- InFunction
));
aux xs
| Braceised (body, tok1, tok2)::xs ->
aux xs
in
aux xs
let rec set_in_other xs =
match xs with
| [] -> ()
(* enum x { } *)
| BToken ({tok = Tenum _})::BToken ({tok = TIdent _})
::Braceised(body, tok1, tok2)::xs
| BToken ({tok = Tenum _})
::Braceised(body, tok1, tok2)::xs
->
body +> List.iter (iter_token_brace (fun tok ->
tok.where <- InEnum;
));
set_in_other xs
(* struct/union/class x { } *)
| BToken ({tok = tokstruct})::BToken ({tok = TIdent _})
::Braceised(body, tok1, tok2)::xs when TH.is_classkey_keyword tokstruct ->
body +> List.iter (iter_token_brace (fun tok ->
tok.where <- InStruct;
));
set_in_other xs
(* struct/union/class x : ... { } *)
| BToken ({tok = tokstruct})::BToken ({tok = TIdent _})
::BToken ({tok = TCol _})::xs when TH.is_classkey_keyword tokstruct ->
let (before, elem, after) = Common.split_when is_braceised xs in
(match elem with
| Braceised(body, tok1, tok2) ->
body +> List.iter (iter_token_brace (fun tok ->
tok.where <- InInitializer;
));
set_in_other after
| _ -> raise Impossible
)
(* = { } *)
| BToken ({tok = TEq _})
::Braceised(body, tok1, tok2)::xs ->
body +> List.iter (iter_token_brace (fun tok ->
tok.where <- InInitializer;
));
set_in_other xs
(* recurse *)
| BToken _::xs -> set_in_other xs
| Braceised(body, tok1, tok2)::xs ->
body +> List.iter set_in_other;
set_in_other xs
let set_context_tag xs =
begin
(* order is important *)
set_in_function_tag xs;
set_in_other xs;
end
(*****************************************************************************)
(* Helpers *)
(*****************************************************************************)
(* Thanks to this function many stuff are not anymore hardcoded in ocaml code
* (but they are now hardcoded in standard.h ...)
*)
let rec (cpp_engine: (string , Parser.token list) assoc ->
Parser.token list -> Parser.token list) = fun env xs ->
xs +> List.map (fun tok ->
match tok with
| TIdent (s,i1) when List.mem_assoc s env -> Common.assoc s env
| x -> [x]
)
+> List.flatten
(* ------------------------------------------------------------------------- *)
(* the pair is the status of '()' and '{}', ex: (-1,0)
* if too much ')' and good '{}'
* could do for [] too ?
* could do for ',' if encounter ',' at "toplevel", not inside () or {}
* then if have ifdef, then certainly can lead to a problem.
*)
let (count_open_close_stuff_ifdef_clause: ifdef_grouped list -> (int * int)) =
fun xs ->
let cnt_paren, cnt_brace = ref 0, ref 0 in
xs +> iter_token_ifdef (fun x ->
(match x.tok with
| x when TH.is_opar x -> incr cnt_paren
| x when TH.is_obrace x -> incr cnt_brace
| x when TH.is_cpar x -> decr cnt_paren
| x when TH.is_obrace x -> decr cnt_brace
| _ -> ()
)
);
!cnt_paren, !cnt_brace
(* ------------------------------------------------------------------------- *)
(* c++ext: *)
let templateLOOKAHEAD = 30
(* note: no need to check for TCPar to stop for instance the search,
* this is will be done automatically because we would be inside a
* Parenthised expression.
*)
let rec have_a_tsup_quite_close xs =
match xs with
| [] -> false
| (PToken x)::xs ->
(match x with
| {tok = TSup i} -> true
(* false positive *)
| {tok = tok} when TH.is_static_cast_like tok -> false
(* bugfix: but want allow some binary operator :) like '*' *)
| {tok = tok} when TH.is_binary_operator_except_star tok -> false
| x -> have_a_tsup_quite_close xs
)
| (Parenthised (xxs, info_parens))::xs ->
have_a_tsup_quite_close xs
(* precondition: there is a tsup *)
let rec find_tsup_quite_close xs =
let rec aux acc xs =
match xs with
| [] -> failwith "find_tsup_quite_close, no tsup"
| (PToken x)::xs ->
(match x with
| {tok = TSup ii} ->
acc, (x,ii), xs
| {tok = TInf ii} ->
(* recurse *)
let (before, (tsuptok,_), after) =
find_tsup_quite_close xs in
(* we don't care about this one, it will be eventually be
* transformed by the caller *)
aux ((PToken x)::(before++[PToken tsuptok])) xs
| x -> aux ((PToken x)::acc) xs
)
| (Parenthised (xxs, info_parens))::xs ->
aux ((Parenthised (xxs, info_parens))::acc) xs
in
let (before, tsup, after) = aux [] xs in
List.rev before, tsup, after
(* ------------------------------------------------------------------------- *)
(* cppext: *)
let forLOOKAHEAD = 30
(* look if there is a '{' just after the closing ')', and handling the
* possibility to have nested expressions inside nested parenthesis
*)
let rec is_really_foreach xs =
let rec is_foreach_aux = function
| [] -> false, []
| TCPar _::TOBrace _::xs -> true, xs
(* the following attempts to handle the cases where there is a
single statement in the body of the loop. undoubtedly more
cases are needed.
todo: premier(statement) - suivant(funcall)
*)
| TCPar _::TIdent _::xs -> true, xs
| TCPar _::Tif _::xs -> true, xs
| TCPar _::Twhile _::xs -> true, xs
| TCPar _::Tfor _::xs -> true, xs
| TCPar _::Tswitch _::xs -> true, xs
| TCPar _::xs -> false, xs
| TOPar _::xs ->
let (_, xs') = is_foreach_aux xs in
is_foreach_aux xs'
| x::xs -> is_foreach_aux xs
in
is_foreach_aux xs +> fst
(*****************************************************************************)
(* CPP handling: macros, ifdefs, macros defs *)
(*****************************************************************************)
(* ------------------------------------------------------------------------- *)
(* ifdef keeping/passing *)
(* ------------------------------------------------------------------------- *)
(* #if 0, #if 1, #if LINUX_VERSION handling *)
let rec find_ifdef_bool xs =
xs +> List.iter (function
| NotIfdefLine _ -> ()
| Ifdefbool (is_ifdef_positif, xxs, info_ifdef_stmt) ->
if is_ifdef_positif
then pr2_pp "commenting parts of a #if 1 or #if LINUX_VERSION"
else pr2_pp "commenting a #if 0 or #if LINUX_VERSION or __cplusplus";
(match xxs with
| [] -> raise Impossible
| firstclause::xxs ->
info_ifdef_stmt +> List.iter (set_as_comment Ast.CppDirective);
if is_ifdef_positif
then xxs +> List.iter
(iter_token_ifdef (set_as_comment Ast.CppOther))
else begin
firstclause +> iter_token_ifdef (set_as_comment Ast.CppOther);
(match List.rev xxs with
(* keep only last *)
| last::startxs ->
startxs +> List.iter
(iter_token_ifdef (set_as_comment Ast.CppOther))
| [] -> (* not #else *) ()
);
end
);
| Ifdef (xxs, info_ifdef_stmt) -> xxs +> List.iter find_ifdef_bool
)
let thresholdIfdefSizeMid = 6
(* infer ifdef involving not-closed expressions/statements *)
let rec find_ifdef_mid xs =
xs +> List.iter (function