Source file parsing_hacks_pp.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
open Common
module TH = Token_helpers_cpp
module TV = Token_views_cpp
module Parser = Parser_cpp
module PI = Parse_info
open Parser_cpp
open Token_views_cpp
open Parsing_hacks_lib
let (==~) = Common2.(==~)
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.t 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
let filter_pp_or_comment_stuff xs =
let rec aux xs =
match xs with
| [] -> []
| x::xs ->
(match x.TV.t with
| tok when TH.is_comment tok ->
aux xs
| Parser.TDefine _ ->
x::aux xs
| tok when TH.is_pp_instruction tok ->
aux xs
| _ ->
x::aux xs
)
in
aux xs
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 Token_cpp.CppDirective);
if is_ifdef_positif
then xxs +> List.iter
(iter_token_ifdef (set_as_comment Token_cpp.CppOther))
else begin
firstclause +> iter_token_ifdef (set_as_comment Token_cpp.CppOther);
(match List.rev xxs with
| _last::startxs ->
startxs +> List.iter
(iter_token_ifdef (set_as_comment Token_cpp.CppOther))
| [] -> ()
);
end
);
| Ifdef (xxs, _info_ifdef_stmt) -> xxs +> List.iter find_ifdef_bool
)
let thresholdIfdefSizeMid = 6
let rec find_ifdef_mid xs =
xs +> List.iter (function
| NotIfdefLine _ -> ()
| Ifdef (xxs, info_ifdef_stmt) ->
(match xxs with
| [] -> raise Impossible
| [_first] -> ()
| _first::second::rest ->
if xxs +> List.for_all
(fun xs -> List.length xs <= thresholdIfdefSizeMid) &&
xxs +> List.for_all (fun xs ->
xs +> List.for_all
(function NotIfdefLine _ -> true | _ -> false)
)
then
let counts = xxs +> List.map count_open_close_stuff_ifdef_clause in
let cnt1, cnt2 = List.hd counts in
if cnt1 <> 0 || cnt2 <> 0
then begin
pr2_pp "found ifdef-mid-something";
info_ifdef_stmt +> List.iter (set_as_comment Token_cpp.CppDirective);
(second::rest) +> List.iter
(iter_token_ifdef (set_as_comment Token_cpp.CppOther));
end
);
List.iter find_ifdef_mid xxs
| Ifdefbool (_, xxs, _info_ifdef_stmt) ->
List.iter find_ifdef_mid xxs
)
let thresholdFunheaderLimit = 4
let rec find_ifdef_funheaders = function
| [] -> ()
| NotIfdefLine _::xs -> find_ifdef_funheaders xs
| Ifdef
([(NotIfdefLine (({col = 0} as _xline1)::_line1))::ifdefblock1;
(NotIfdefLine (({col = 0} as xline2)::line2))::ifdefblock2
], info_ifdef_stmt
)
::NotIfdefLine (({t=TOBrace _i; col = 0})::_line3)
::xs
when List.length ifdefblock1 <= thresholdFunheaderLimit &&
List.length ifdefblock2 <= thresholdFunheaderLimit
->
find_ifdef_funheaders xs;
info_ifdef_stmt +> List.iter (set_as_comment Token_cpp.CppDirective);
let all_toks = [xline2] @ line2 in
all_toks +> List.iter (set_as_comment Token_cpp.CppOther) ;
ifdefblock2 +> iter_token_ifdef (set_as_comment Token_cpp.CppOther);
| Ifdef
([[NotIfdefLine (({col = 0} as _xline1)::_line1)];
[Ifdef
([[NotIfdefLine (({col = 0} as xline2)::line2)];
[NotIfdefLine (({col = 0} as xline3)::line3)];
], info_ifdef_stmt2
)
]
], info_ifdef_stmt
)
::NotIfdefLine (({t=TOBrace _i; col = 0})::_line4)
::xs
->
find_ifdef_funheaders xs;
info_ifdef_stmt +> List.iter (set_as_comment Token_cpp.CppDirective);
info_ifdef_stmt2 +> List.iter (set_as_comment Token_cpp.CppDirective);
let all_toks = [xline2;xline3] @ line2 @ line3 in
all_toks +> List.iter (set_as_comment Token_cpp.CppOther);
| Ifdef
([[NotIfdefLine (({col = 0} as _xline1)::_line1)];
[NotIfdefLine (({col = 0} as xline2)::line2)];
[NotIfdefLine (({col = 0} as xline3)::line3)];
], info_ifdef_stmt
)
::NotIfdefLine (({t=TOBrace _i; col = 0})::_line4)
::xs
->
find_ifdef_funheaders xs;
info_ifdef_stmt +> List.iter (set_as_comment Token_cpp.CppDirective);
let all_toks = [xline2;xline3] @ line2 @ line3 in
all_toks +> List.iter (set_as_comment Token_cpp.CppOther)
| Ifdef (xxs,_)::xs
| Ifdefbool (_, xxs,_)::xs ->
List.iter find_ifdef_funheaders xxs;
find_ifdef_funheaders xs
let rec find_string_macro_paren xs =
match xs with
| [] -> ()
| Parenthised(xxs, _)::xs ->
xxs +> List.iter (fun xs ->
if xs +> List.exists
(function PToken({t=TString _}) -> true | _ -> false) &&
xs +> List.for_all
(function PToken({t=TString _}) | PToken({t=TIdent _}) ->
true | _ -> false)
then
xs +> List.iter (fun tok ->
match tok with
| PToken({t=TIdent (_s,_)} as id) ->
change_tok id (TIdent_MacroString (TH.info_of_tok id.t))
| _ -> ()
)
else
find_string_macro_paren xs
);
find_string_macro_paren xs
| PToken _ ::xs ->
find_string_macro_paren xs
let rec find_macro_paren xs =
match xs with
| [] -> ()
| PToken ({t=Tattribute _} as id)
::Parenthised (xxs,info_parens)
::xs
->
pr2_pp ("MACRO: __attribute detected ");
[Parenthised (xxs, info_parens)] +>
iter_token_paren (set_as_comment Token_cpp.CppAttr);
set_as_comment Token_cpp.CppAttr id;
find_macro_paren xs
| PToken ({t=TString _})::PToken ({t=TIdent (_s,_)} as id)
::Parenthised (xxs, info_parens)
::xs ->
change_tok id (TIdent_MacroString (TH.info_of_tok id.t));
[Parenthised (xxs, info_parens)] +>
iter_token_paren (set_as_comment Token_cpp.CppMacro);
find_macro_paren xs
| PToken ({t=TIdent (_s,_)} as id)
::Parenthised (xxs, info_parens)
::PToken ({t=TString _})
::xs ->
change_tok id (TIdent_MacroString (TH.info_of_tok id.t));
[Parenthised (xxs, info_parens)] +>
iter_token_paren (set_as_comment Token_cpp.CppMacro);
find_macro_paren xs
| PToken ({t=TString ((str,_),_)})::PToken ({t=TIdent (_s,_)} as id)
::xs ->
if str <> "C" then begin
change_tok id (TIdent_MacroString (TH.info_of_tok id.t));
find_macro_paren xs
end
else
find_macro_paren xs
| PToken ({t=TIdent (_s,_)} as id)::PToken ({t=TString _})
::xs ->
change_tok id (TIdent_MacroString (TH.info_of_tok id.t));
find_macro_paren xs
| PToken ({t=TIdent (s,_i1)} as id)::xs
when s = "MACROSTATEMENT" ->
change_tok id (TIdent_MacroStmt(TH.info_of_tok id.t));
find_macro_paren xs
| (PToken _x)::xs -> find_macro_paren xs
| (Parenthised (xxs, _))::xs ->
xxs +> List.iter find_macro_paren;
find_macro_paren xs
let rec find_macro_lineparen xs =
match xs with
| [] -> ()
| (Line ([PToken ({t=TIdent (s,_)} as macro);]))::xs
when s ==~ regexp_ns_decl_like ->
set_as_comment Token_cpp.CppMacro macro;
find_macro_lineparen (xs)
| (Line ([PToken ({t=TIdent (s,_)} as macro);
PToken ({t=TPtVirg _})]))::xs
when s ==~ regexp_ns_decl_like ->
set_as_comment Token_cpp.CppMacro macro;
find_macro_lineparen (xs)
| (Line ([PToken ({t=TIdent (s,_)} as macro);
Parenthised (xxs,info_parens);
]))::xs
when s ==~ regexp_ns_decl_like ->
[Parenthised (xxs, info_parens)] +>
iter_token_paren (set_as_comment Token_cpp.CppMacro);
set_as_comment Token_cpp.CppMacro macro;
find_macro_lineparen (xs)
| (Line
(
[PToken ({t=Tstatic _});
PToken ({t=TIdent (s,_)} as macro);
Parenthised (_xxs,_);
PToken ({t=TPtVirg _});
]
))::xs
when (s ==~ regexp_macro) ->
let info = TH.info_of_tok macro.t in
change_tok macro (TIdent_MacroDecl (PI.str_of_info info, info));
find_macro_lineparen (xs)
| (Line
(
[PToken ({t=Tstatic _});
PToken ({t=Tconst _} as const);
PToken ({t=TIdent (s,_)} as macro);
Parenthised (_xxs,_info_parens);
PToken ({t=TPtVirg _});
]
))
::xs
when (s ==~ regexp_macro) ->
let info = TH.info_of_tok macro.t in
change_tok macro (TIdent_MacroDecl (PI.str_of_info info, info));
change_tok const (Tconst_MacroDeclConst (TH.info_of_tok const.t));
find_macro_lineparen (xs)
| (Line
([PToken ({t=Tstatic _});
PToken ({t=TIdent (s,_)} as macro);
Parenthised (_xxs,_);
]
))::xs
when s ==~ regexp_macro ->
let info = TH.info_of_tok macro.t in
change_tok macro (TIdent_MacroDecl (PI.str_of_info info, info));
find_macro_lineparen (xs)
| (Line
(
(PToken ({t=Tstatic _})::[]
)))
::(Line
(
[PToken ({t=TIdent (s,_)} as macro);
Parenthised (_,_);
PToken ({t=TPtVirg _});
]
)
)::xs
when (s ==~ regexp_macro) ->
let info = TH.info_of_tok macro.t in
change_tok macro (TIdent_MacroDecl (PI.str_of_info info, info));
find_macro_lineparen xs
| (Line
([PToken ({t=TIdent (s,_)} as macro);
Parenthised (_,_);
PToken ({t=TPtVirg _});
]
))::xs
when (s ==~ regexp_declare) ->
let info = TH.info_of_tok macro.t in
change_tok macro (TIdent_MacroDecl (PI.str_of_info info, info));
find_macro_lineparen xs
| (Line
([PToken ({t=TIdent (_s,_ii); col = col1; where = ctx} as _macro);
Parenthised (_,info_parens);
] as _line1
))
::xs when col1 = 0
->
let condition =
(match xs with
| (Line (PToken ({col = col2 } as other)::_restline2))::_ ->
TH.is_eof other.t || (col2 = 0 &&
(match other.t with
| TOBrace _ -> false
| TCBrace _ when List.hd ctx <> InFunction -> false
| TPtVirg _
| TCol _
-> false
| tok when TH.is_binary_operator tok -> false
| _ -> true
)
)
| _ -> false
)
in
if condition
then begin
let tcpar = Common2.list_last info_parens in
change_tok tcpar (TCPar_EOL (TH.info_of_tok tcpar.t));
end;
find_macro_lineparen xs
| (Line
([PToken ({t=TIdent (_s,_ii); col = col1; where = ctx} as macro);
Parenthised (xxs,info_parens);
] as _line1
))
::(Line
(PToken ({col = col2 } as other)::_restline2
) as line2)
::xs
->
let condition =
(col1 = col2 &&
(match other.t with
| TOBrace _ -> false
| TCBrace _ when List.hd ctx <> InFunction -> false
| TPtVirg _
| TCol _
-> false
| tok when TH.is_binary_operator tok -> false
| _ -> true
)
)
||
(col2 <= col1 &&
(match other.t with
| TCBrace _ when List.hd ctx = InFunction -> true
| Treturn _ -> true
| Tif _ -> true
| Telse _ -> true
| _ -> false
)
)
in
if condition
then
if col1 = 0 then ()
else begin
change_tok macro (TIdent_MacroStmt (TH.info_of_tok macro.t));
[Parenthised (xxs, info_parens)] +>
iter_token_paren (set_as_comment Token_cpp.CppMacro);
end;
find_macro_lineparen (line2::xs)
| (Line
([PToken ({t=TIdent (_s,_ii); col = col1; where = ctx} as macro);
] as _line1
))
::(Line
(PToken ({col = col2 } as other)::_restline2
) as line2)
::xs ->
let condition =
(col1 = col2 &&
col1 <> 0 &&
(match other.t with
| TPtVirg _ -> false
| TOr _ -> false
| TCBrace _ when List.hd ctx <> InFunction -> false
| tok when TH.is_binary_operator tok -> false
| _ -> true
)) ||
(col2 <= col1 &&
(match other.t with
| TCBrace _ when List.hd ctx = InFunction -> true
| Treturn _ -> true
| Tif _ -> true
| Telse _ -> true
| _ -> false
))
in
if condition
then change_tok macro (TIdent_MacroStmt (TH.info_of_tok macro.t));
find_macro_lineparen (line2::xs)
| _x::xs ->
find_macro_lineparen xs
let is_init tok2 tok3 =
match tok2.t, tok3.t with
| TInt _, TComma _ -> true
| TString _, TComma _ -> true
| TIdent _, TComma _ -> true
| _ -> false
let find_define_init_brace_paren xs =
let rec aux xs =
match xs with
| [] -> ()
| (PToken {t=TDefine _})
::(PToken {t=TIdent_Define (_s,_)})
::(PToken ({t=TOBrace i1} as tokbrace))
::(PToken tok2)
::(PToken tok3)
::xs ->
if is_init tok2 tok3
then change_tok tokbrace (TOBrace_DefineInit i1);
aux xs
| (PToken {t=TDefine _})
::(PToken {t=TIdent_Define (s,_); col=c})
::(Parenthised(_, {col=c2; _}::_))
::(PToken ({t=TOBrace i1} as tokbrace))
::(PToken tok2)
::(PToken tok3)
::xs when c2 = c + String.length s ->
if is_init tok2 tok3
then change_tok tokbrace (TOBrace_DefineInit i1);
aux xs
| (PToken {t=TDefine _})
::(PToken {t=TIdent_Define (_s,_)})
::(Parenthised(_xxx, _))
::(PToken ({t=TOBrace i1} as tokbrace))
::(Parenthised(_, _))
::(PToken {t=(TAnd _|TOr _);_})
::xs ->
change_tok tokbrace (TOBrace_DefineInit i1);
aux xs
| (PToken _)::xs -> aux xs
| (Parenthised (_, _))::xs ->
aux xs
in
aux xs