Source file graph_code_c.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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
open Common
module Flag = Flag_parsing
module E = Entity_code
module G = Graph_code
module P = Graph_code_prolog
open Ast_c
module Ast = Ast_c
type env = {
g: Graph_code.graph;
phase: phase;
current: Graph_code.node;
c_file_readable: Common.filename;
ctx: Graph_code_prolog.context;
in_assign: bool;
in_define: bool;
in_return: bool;
locals: (string * type_ option) list ref;
local_rename: (string, string) Hashtbl.t;
conf: config;
typedefs: (string, Ast.type_) Hashtbl.t;
structs: (string, Ast.struct_def) Hashtbl.t;
dupes: (Graph_code.node, bool) Hashtbl.t;
fields: (string, string list) Hashtbl.t;
log: string -> unit;
pr2_and_log: string -> unit;
}
and phase = Defs | Uses
and config = {
types_dependencies: bool;
fields_dependencies: bool;
macro_dependencies: bool;
typedefs_dependencies: bool;
propagate_deps_def_to_decl: bool;
}
type kind_file = Source | Header
let hook_use_edge = ref (fun _ctx _in_assign (_src, _dst) _g _loc -> ())
let hook_def_node = ref (fun _node _g -> ())
let facts = ref None
let parse ~show_parse_error file =
try
Common.save_excursion Flag.error_recovery true (fun () ->
Common.save_excursion Flag.show_parsing_error show_parse_error (fun () ->
Common.save_excursion Flag.verbose_parsing show_parse_error (fun () ->
Parse_c.parse_program file
)))
with
| Timeout -> raise Timeout
| exn ->
pr2_once (spf "PARSE ERROR with %s, exn = %s" file (Common.exn_to_s exn));
raise exn
let error s tok =
failwith (spf "%s: %s" (Parse_info.string_of_info tok) s)
let new_name_if_defs env (s, tok) =
if env.phase = Defs
then begin
if Hashtbl.mem env.local_rename s
then error (spf "Duped new name: %s" s) tok;
let s2 = Graph_code.gensym s in
Hashtbl.add env.local_rename s s2;
s2, tok
end
else Hashtbl.find env.local_rename s, tok
let str env (s, tok) =
if Hashtbl.mem env.local_rename s
then Hashtbl.find env.local_rename s, tok
else s, tok
let add_prefix prefix (s, tok) =
prefix ^ s, tok
let kind_file env =
match env.c_file_readable with
| s when s =~ ".*\\.[h]" -> Header
| s when s =~ ".*\\.[c]" -> Source
| _s ->
Source
let rec expand_typedefs env t =
match t with
| TBase _ | TStructName _ | TEnumName _ -> t
| TTypeName name ->
let s = Ast.str_of_name name in
if Hashtbl.mem env.typedefs s &&
not (Hashtbl.mem env.dupes ("T__" ^ s, E.Type))
then
let t' = (Hashtbl.find env.typedefs s) in
if t' =*= t
then t
else expand_typedefs env t'
else t
| TPointer x -> TPointer (expand_typedefs env x)
| TArray (eopt, x) -> TArray (eopt, expand_typedefs env x)
| TFunction (ret, params) ->
TFunction (expand_typedefs env ret,
params |> List.map (fun p ->
{ p with p_type = expand_typedefs env p.p_type }
))
let final_type env t =
if env.conf.typedefs_dependencies
then t
else
expand_typedefs env t
let find_existing_node_opt env name candidates last_resort =
let s = Ast.str_of_name name in
let kind_with_a_dupe =
candidates |> Common.find_opt (fun kind ->
Hashtbl.mem env.dupes (s, kind)
&& kind <> E.Prototype && kind <> E.GlobalExtern
)
in
let existing_nodes =
candidates |> List.filter (fun kind -> G.has_node (s, kind) env.g)
in
let non_proto_existing_nodes =
existing_nodes |> Common.exclude (fun kind ->
kind =*= E.Prototype || kind =*= E.GlobalExtern
)
in
(match kind_with_a_dupe with
| Some kind_dupe -> Some kind_dupe
| None ->
(match existing_nodes with
| [] -> Some last_resort
| [x] -> Some x
| x::_::_ ->
(match non_proto_existing_nodes with
| [] -> Some x
| [y] -> Some y
| x::y::_ ->
env.pr2_and_log
(spf "skipping edge, multiple def kinds for entity %s (%s<>%s)"
s (E.string_of_entity_kind x) (E.string_of_entity_kind y));
let xfile = G.file_of_node (s, x) env.g in
let yfile = G.file_of_node (s, y) env.g in
env.log (spf " orig = %s" xfile);
env.log (spf " dupe = %s" yfile);
None
)
)
)
let is_local env s =
(Common.find_opt (fun (x, _) -> x =$= s) !(env.locals)) <> None
let with_datalog_env env f =
!facts |> Common.do_option (fun aref ->
let env2 = { Datalog_c.
scope = fst env.current;
c_file_readable = env.c_file_readable;
long_format = !Datalog_c.long_format;
globals = env.g;
locals = env.locals;
facts = aref;
globals_renames = (fun n -> str env n)
}
in
f env2
)
let hook_expr_toplevel env_orig x =
if env_orig.phase = Uses
then
with_datalog_env env_orig (fun env ->
let instrs = Datalog_c.instrs_of_expr env x in
instrs |> List.iter (fun instr ->
let facts = Datalog_c.facts_of_instr env instr in
facts |> List.iter (fun fact -> Common.push fact env.Datalog_c.facts);
);
if env_orig.in_return
then
let fact = Datalog_c.return_fact env (Common2.list_last instrs) in
Common.push fact env.Datalog_c.facts
)
let hook_def env def =
if env.phase = Defs
then
with_datalog_env env (fun env ->
let facts = Datalog_c.facts_of_def env def in
facts |> List.iter (fun fact -> Common.push fact env.Datalog_c.facts);
)
let add_node_and_edge_if_defs_mode env (name, kind) typopt =
let str = Ast.str_of_name name in
let str' =
match kind, env.current with
| E.Field, (s, E.Type) -> s ^ "." ^ str
| _ -> str
in
let node = (str', kind) in
if env.phase = Defs then begin
match () with
| _ when Hashtbl.mem env.dupes env.current ->
Hashtbl.replace env.dupes node true
| _ when G.has_node node env.g ->
(match kind with
| E.Function | E.Global | E.Constructor
| E.Type | E.Field | E.Constant | E.Macro
->
(match kind, str with
| E.Type, s when s =~ "[ST]__" -> ()
| _ when env.c_file_readable =~ ".*EXTERNAL" -> ()
| _ ->
env.pr2_and_log (spf "DUPE entity: %s" (G.string_of_node node));
let orig_file = G.file_of_node node env.g in
env.log (spf " orig = %s" orig_file);
env.log (spf " dupe = %s" env.c_file_readable);
Hashtbl.replace env.dupes node true;
)
| E.Prototype | E.GlobalExtern ->
Hashtbl.replace env.dupes node true;
| _ ->
failwith (spf "Unhandled category: %s" (G.string_of_node node))
)
| _ ->
let typ =
match typopt with
| None -> None
| Some t ->
let v = Meta_ast_c.vof_any (Type t) in
let _s = Ocaml.string_of_v v in
Some "_TODO_type"
in
try
let pos = Parse_info.token_location_of_info (snd name) in
let nodeinfo = { Graph_code.
pos; typ;
props = [];
} in
env.g |> G.add_node node;
env.g |> G.add_edge (env.current, node) G.Has;
env.g |> G.add_nodeinfo node nodeinfo;
!hook_def_node node env.g;
with Not_found ->
error ("Not_found:" ^ str) (snd name)
end;
if Hashtbl.mem env.dupes node
then env
else { env with current = node }
let add_use_edge env (name, kind) =
let s = Ast.str_of_name name in
let src = env.current in
let dst = (s, kind) in
match () with
| _ when Hashtbl.mem env.dupes src || Hashtbl.mem env.dupes dst ->
env.pr2_and_log (spf "skipping edge (%s -> %s), one of it is a dupe"
(G.string_of_node src) (G.string_of_node dst));
| _ when s =$= "USED" || s =$= "SET" -> ()
| _ when not (G.has_node src env.g) ->
error (spf "SRC FAIL: %s (-> %s)"
(G.string_of_node src) (G.string_of_node dst)) (snd name)
| _ when G.has_node dst env.g ->
G.add_edge (src, dst) G.Use env.g;
let pos = Parse_info.token_location_of_info (snd name) in
!hook_use_edge env.ctx env.in_assign (src, dst) env.g pos
| _ ->
let prfn =
if env.in_define
then env.log
else env.pr2_and_log
in
prfn (spf "Lookup failure on %s (%s)"
(G.string_of_node dst)
(Parse_info.string_of_info (snd name)))
let rec extract_defs_uses env ast =
if env.phase = Defs then begin
let dir = Common2.dirname env.c_file_readable in
G.create_intermediate_directories_if_not_present env.g dir;
let node = (env.c_file_readable, E.File) in
env.g |> G.add_node node;
env.g |> G.add_edge ((dir, E.Dir), node) G.Has;
end;
let env = { env with current = (env.c_file_readable, E.File); } in
toplevels env ast
and toplevel env x =
match x with
| Define (name, body) ->
let name =
if kind_file env =*= Source then new_name_if_defs env name else name in
let env = add_node_and_edge_if_defs_mode env (name, E.Constant) None in
hook_def env x;
if env.phase = Uses && env.conf.macro_dependencies
then define_body env body
| Macro (name, params, body) ->
let name =
if kind_file env =*= Source then new_name_if_defs env name else name in
let env = add_node_and_edge_if_defs_mode env (name, E.Macro) None in
hook_def env x;
let env = { env with locals = ref
(params |> List.map (fun p -> Ast.str_of_name p, None))
} in
if env.phase = Uses && env.conf.macro_dependencies
then define_body env body
| FuncDef def | Prototype def ->
let name = def.f_name in
let typ = Some (TFunction def.f_type) in
let kind =
match x with
| Prototype _ -> E.Prototype
| FuncDef _ -> E.Function
| _ -> raise Impossible
in
let static =
(def.f_static && kind_file env =*= Source)
|| Ast.str_of_name name = "main"
in
(match kind with
| E.Prototype when static -> ()
| E.Prototype ->
add_node_and_edge_if_defs_mode env (name, kind) typ |> ignore
| E.Function ->
let name = if static then new_name_if_defs env name else name in
let env = add_node_and_edge_if_defs_mode env (name, kind) typ in
type_ env (TFunction def.f_type);
hook_def env x;
let xs = snd def.f_type |> Common.map_filter (fun x ->
match x.p_name with
| None -> None
| Some n -> Some (Ast.str_of_name n, Some x.p_type)
)
in
let env = { env with locals = ref xs } in
if env.phase = Uses
then stmts env def.f_body
| _ -> raise Impossible
)
| Global v ->
let { v_name = name; v_type = t; v_storage = sto; v_init = eopt } = v in
let finalt = expand_typedefs env t in
let typ = Some t in
let kind =
match sto, finalt, eopt with
| _, TFunction _, _ -> E.Prototype
| Extern, _, _ -> E.GlobalExtern
| _, _, Some _ when kind_file env = Header -> E.Global
| _, _, _ when kind_file env = Header -> E.GlobalExtern
| DefaultStorage, _, _ | Static, _, _ -> E.Global
in
let static = sto =*= Static in
(match kind with
| E.Prototype when static -> ()
| E.Prototype | E.GlobalExtern ->
add_node_and_edge_if_defs_mode env (name, kind) typ |> ignore
| E.Global ->
let name = if static then new_name_if_defs env name else name in
let env = add_node_and_edge_if_defs_mode env (name, kind) typ in
hook_def env x;
type_ env t;
if env.phase = Uses
then
eopt |> Common.do_option (fun e ->
let n = name in
expr_toplevel env
(Assign ((Cst_cpp.SimpleAssign, snd n), Id n, e))
)
| _ -> raise Impossible
)
| StructDef def ->
let { s_name = name; s_kind = kind; s_flds = flds } = def in
let prefix = match kind with Struct -> "S__" | Union -> "U__" in
let name = add_prefix prefix name in
let s = Ast.str_of_name name in
if env.phase = Defs
then begin
if Hashtbl.mem env.structs s
then
let old = Hashtbl.find env.structs s in
if (Meta_ast_c.vof_any (Toplevel (StructDef old))) =*=
(Meta_ast_c.vof_any (Toplevel (StructDef def)))
then env.log (spf "you should factorize struct %s definitions" s)
else begin
env.pr2_and_log (spf "conflicting structs for %s, %s <> %s"
s (Common.dump old) (Common.dump def));
Hashtbl.replace env.dupes (fst name, E.Type) true
end
else begin
Hashtbl.add env.structs s def;
let env = add_node_and_edge_if_defs_mode env (name, E.Type) None in
hook_def env x;
let fields = flds |> Common.map_filter (function
| { fld_name = Some name; _ } -> Some (Ast.str_of_name name)
| _ -> None
)
in
Hashtbl.replace env.fields (prefix ^ s) fields;
flds |> List.iter (fun { fld_name = nameopt; fld_type = t; } ->
nameopt |> Common.do_option (fun name ->
add_node_and_edge_if_defs_mode env (name, E.Field) (Some t)
|>ignore;
)
);
end
end else begin
let env = add_node_and_edge_if_defs_mode env (name, E.Type) None in
flds |> List.iter (fun { fld_name = nameopt; fld_type = t; } ->
match nameopt with
| Some name ->
let typ = Some t in
let env = add_node_and_edge_if_defs_mode env (name, E.Field) typ in
type_ env t
| None ->
type_ env t
)
end
| EnumDef (name, xs) ->
let name = add_prefix "E__" name in
let env = add_node_and_edge_if_defs_mode env (name, E.Type) None in
xs |> List.iter (fun (name, eopt) ->
let name =
if kind_file env=*=Source then new_name_if_defs env name else name in
let env = add_node_and_edge_if_defs_mode env (name, E.Constructor) None in
if env.phase = Uses
then Common2.opt (expr_toplevel env) eopt
);
hook_def env x;
| TypeDef (name, t) ->
let s = Ast.str_of_name name in
let name = add_prefix "T__" name in
if env.phase = Defs
then begin
if Hashtbl.mem env.typedefs s
then
let old = Hashtbl.find env.typedefs s in
if (Meta_ast_c.vof_any (Type old) =*= (Meta_ast_c.vof_any (Type t)))
then ()
else begin
env.pr2_and_log (spf "conflicting typedefs for %s, %s <> %s"
s (Common.dump old) (Common.dump t));
Hashtbl.replace env.dupes (fst name, E.Type) true
end
else Hashtbl.add env.typedefs s t
end;
let typ = Some t in
let _env = add_node_and_edge_if_defs_mode env (name,E.Type) typ in
()
| Include _ -> ()
and toplevels env xs = List.iter (toplevel env) xs
and define_body env v =
let env = { env with in_define = true } in
match v with
| CppExpr e -> expr_toplevel env e
| CppStmt st -> stmt env st
and stmt env = function
| ExprSt e -> expr_toplevel env e
| Block xs -> stmts env xs
| Asm xs -> List.iter (expr_toplevel env) xs
| If (e, st1, st2) ->
expr_toplevel env e;
stmts env [st1; st2]
| Switch (e, xs) ->
expr_toplevel env e;
cases env xs
| While (e, st) | DoWhile (st, e) ->
expr_toplevel env e;
stmt env st
| For (e1, e2, e3, st) ->
Common2.opt (expr_toplevel env) e1;
Common2.opt (expr_toplevel env) e2;
Common2.opt (expr_toplevel env) e3;
stmt env st
| Return eopt ->
Common2.opt (expr_toplevel { env with in_return = true }) eopt;
| Continue | Break -> ()
| Label (_name, st) ->
stmt env st
| Goto _name ->
()
| Vars xs ->
xs |> List.iter (fun x ->
let { v_name = n; v_type = t; v_storage = sto; v_init = eopt } = x in
if sto <> Extern
then begin
env.locals := (Ast.str_of_name n, Some t)::!(env.locals);
type_ env t;
end;
(match eopt with
| None -> ()
| Some e ->
expr_toplevel env (Assign ((Cst_cpp.SimpleAssign, snd n), Id n, e))
)
)
and case env = function
| Case (e, xs) ->
expr_toplevel env e;
stmts env xs
| Default xs ->
stmts env xs
and stmts env xs = List.iter (stmt env) xs
and cases env xs = List.iter (case env) xs
and expr_toplevel env x =
expr env x;
hook_expr_toplevel env x
and expr env = function
| Int _ | Float _ | Char _ -> ()
| String _ -> ()
| Id name ->
let s = Ast.str_of_name name in
if is_local env s
then ()
else
let name = str env name in
let kind_opt = find_existing_node_opt env name
[E.Constant;
E.Constructor;
E.Global;
E.Function;
E.Prototype;
E.GlobalExtern;
]
(if looks_like_macro name then E.Constant else E.Global)
in
kind_opt |> Common.do_option (fun kind -> add_use_edge env (name, kind))
| Call (e, es) ->
(match e with
| Id name ->
let s = Ast.str_of_name name in
if is_local env s
then ()
else
let name = str env name in
let kind_opt = find_existing_node_opt env name
[E.Macro;
E.Constant;
E.Function;
E.Global;
E.Prototype;
E.GlobalExtern;
]
(if looks_like_macro name then E.Macro else E.Function)
in
kind_opt |> Common.do_option (fun kind ->
add_use_edge { env with ctx = P.NoCtx } (name, kind);
exprs { env with ctx = (P.CallCtx (fst name, kind)) } es
)
| _ ->
expr env e;
exprs env es
)
| Assign (_, e1, e2) ->
expr { env with in_assign = true } e1;
expr env e2;
| ArrayAccess (e1, e2) -> exprs env [e1; e2]
| RecordPtAccess (e, _name) -> expr env e
| Cast (t, e) ->
type_ env t;
expr env e
| Postfix (e, _op) | Infix (e, _op) ->
expr { env with in_assign = true } e
| Unary (e, op) ->
(match Ast.unwrap op with
| Cst_cpp.GetRef -> expr { env with in_assign = true } e
| _ -> expr env e
)
| Binary (e1, _op, e2) -> exprs env [e1;e2]
| CondExpr (e1, e2, e3) -> exprs env [e1;e2;e3]
| Sequence (e1, e2) -> exprs env [e1;e2]
| ArrayInit xs ->
xs |> List.iter (fun (eopt, init) ->
Common2.opt (expr env) eopt;
expr env init
)
| RecordInit xs -> xs |> List.map snd |> exprs env
| SizeOf x ->
(match x with
| Left (Id (origname)) ->
let s = Ast.str_of_name origname in
if is_local env s
then ()
else
let name = str env origname in
if not (G.has_node (Ast.str_of_name name, E.Global) env.g) &&
not (G.has_node (Ast.str_of_name name, E.GlobalExtern) env.g)
then
type_ env (TTypeName origname)
else expr env (Id origname)
| Left e -> expr env e
| Right t -> type_ env t
)
| GccConstructor (t, e) ->
type_ env t;
expr env e
| Ellipses _ -> ()
and exprs env xs = List.iter (expr env) xs
and type_ env typ =
if env.phase = Uses && env.conf.types_dependencies
then begin
let t = final_type env typ in
let rec aux t =
match t with
| TBase _ -> ()
| TStructName (Struct, name) ->
add_use_edge env (add_prefix "S__" name, E.Type)
| TStructName (Union, name) ->
add_use_edge env (add_prefix "U__" name, E.Type)
| TEnumName name ->
add_use_edge env (add_prefix "E__" name, E.Type)
| TTypeName name ->
let s = Ast.str_of_name name in
if is_local env s && env.in_define
then ()
else
if env.conf.typedefs_dependencies
then add_use_edge env (add_prefix "T__" name, E.Type)
else
if Hashtbl.mem env.typedefs s
then
let t' = (Hashtbl.find env.typedefs s) in
if t' = t
then add_use_edge env (add_prefix "T__" name, E.Type)
else
env.pr2_and_log (spf "skipping edge, probably dupe typedef %s (%s)"
s (Parse_info.string_of_info (snd name)))
else env.pr2_and_log (spf "typedef not found: %s (%s)" s
(Parse_info.string_of_info (snd name)))
| TPointer x -> aux x
| TArray (eopt, x) ->
Common2.opt (expr env) eopt;
aux x
| TFunction (t, xs) ->
aux t;
xs |> List.iter (fun p -> aux p.p_type)
in
aux t
end
let build ?(verbose=true) root files =
let g = G.create () in
G.create_initial_hierarchy g;
let chan = open_out (Filename.concat root "pfff.log") in
let conf = {
types_dependencies = true;
fields_dependencies = true;
macro_dependencies = false;
propagate_deps_def_to_decl = false;
typedefs_dependencies = false;
} in
let env = {
g;
phase = Defs;
current = G.pb;
ctx = P.NoCtx;
c_file_readable = "__filled_later__";
conf;
in_assign = false;
in_define = false;
in_return = false;
local_rename = Hashtbl.create 0;
dupes = Hashtbl.create 101;
typedefs = Hashtbl.create 101;
structs = Hashtbl.create 101;
fields = Hashtbl.create 101;
locals = ref [];
log = (fun s -> output_string chan (s ^ "\n"); flush chan;);
pr2_and_log = (fun s ->
pr2 s;
output_string chan (s ^ "\n"); flush chan;
);
} in
env.pr2_and_log "\nstep0: parsing";
let elems =
files |> Console.progress ~show:verbose (fun k ->
List.map (fun file ->
k();
let ast = parse ~show_parse_error:true file in
let readable = Common.readable ~root file in
let local_rename = Hashtbl.create 101 in
ast, readable, local_rename
)
)
in
env.pr2_and_log "\nstep1: extract defs";
elems |> Console.progress ~show:verbose (fun k ->
List.iter (fun (ast, c_file_readable, local_rename) ->
k();
extract_defs_uses { env with
phase = Defs; c_file_readable; local_rename;
} ast
));
env.pr2_and_log "\nstep2: extract Uses";
elems |> Console.progress ~show:verbose (fun k ->
List.iter (fun (ast, c_file_readable, local_rename) ->
k();
extract_defs_uses { env with
phase = Uses; c_file_readable; local_rename;
} ast
));
env.pr2_and_log "\nstep3: adjusting";
if conf.propagate_deps_def_to_decl
then Graph_code_helpers.propagate_users_of_functions_globals_types_to_prototype_extern_typedefs g;
G.remove_empty_nodes g [G.not_found; G.dupe; G.pb];
g