Source file kernel_ast.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
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
open Data
module Md = Markdown
module Js = Yojson.Basic.Util
module Pkg = Package
open Cil_types
let package = Pkg.package ~title:"Ast Services" ~name:"ast" ~readme:"ast.md" ()
let () = Request.register ~package
~kind:`EXEC ~name:"compute"
~descr:(Md.plain "Ensures that AST is computed")
~input:(module Junit) ~output:(module Junit) Ast.compute
let ast_changed_signal = Request.signal ~package ~name:"changed"
~descr:(Md.plain "Emitted when the AST has been changed")
let ast_changed () = Request.emit ast_changed_signal
let ast_update_hook f =
begin
Ast.add_hook_on_update f;
Ast.apply_after_computed (fun _ -> f ());
end
let () = ast_update_hook ast_changed
let () = Annotations.add_hook_on_change ast_changed
module Position =
struct
type t = Filepath.position
let jtype = Data.declare ~package ~name:"source"
~descr:(Md.plain "Source file positions.")
(Jrecord [
"dir", Jstring;
"base", Jstring;
"file", Jstring;
"line", Jnumber;
])
let to_json p =
let path = Filepath.(to_string p.pos_path) in
let file =
if Server_parameters.has_relative_filepath ()
then path
else (p.Filepath.pos_path :> string)
in
`Assoc [
"dir" , `String (Filename.dirname path) ;
"base" , `String (Filename.basename path) ;
"file" , `String file ;
"line" , `Int p.Filepath.pos_lnum ;
]
let of_json js =
let fail () = failure_from_type_error "Invalid source format" js in
match js with
| `Assoc assoc ->
begin
match List.assoc "file" assoc, List.assoc "line" assoc with
| `String path, `Int line ->
Log.source ~file:(Filepath.of_string path) ~line
| _, _ -> fail ()
| exception Not_found -> fail ()
end
| _ -> fail ()
end
module type TagInfo =
sig
type t
val name : string
val descr : string
val create : t -> string
module H : Hashtbl.S with type key = t
end
module type Tag =
sig
include Data.S
val index : t -> string
val find : string -> t
end
module MakeTag(T : TagInfo) :
sig
include Tag with type t = T.t
val iter : (t * string -> unit) -> unit
val hook : (t * string -> unit) -> unit
end =
struct
type t = T.t
type index = {
tags : string T.H.t ;
items : (string,T.t) Hashtbl.t ;
}
let index () = {
tags = T.H.create 0 ;
items = Hashtbl.create 0 ;
}
let module_name = String.capitalize_ascii T.name
module TYPE : Datatype.S with type t = index =
Datatype.Make
(struct
type t = index
include Datatype.Undefined
let reprs = [index()]
let name = Printf.sprintf "Server.Kernel_ast.%s.TYPE" module_name
let mem_project = Datatype.never_any_project
end)
module STATE = State_builder.Ref(TYPE)
(struct
let name = Printf.sprintf "Server.Kernel_ast.%s.STATE" module_name
let dependencies = []
let default = index
end)
let iter f =
T.H.iter (fun key str -> f (key, str)) (STATE.get ()).tags
let hooks = ref []
let hook f = hooks := !hooks @ [f]
let index item =
let { tags ; items } = STATE.get () in
try T.H.find tags item
with Not_found ->
let tag = T.create item in
T.H.add tags item tag ;
Hashtbl.add items tag item ;
List.iter (fun fn -> fn (item,tag)) !hooks ; tag
let find tag = Hashtbl.find (STATE.get()).items tag
let jtype = Data.declare ~package ~name:T.name
~descr:(Md.plain T.descr)
(Pkg.Jkey T.name)
let to_json item = `String (index item)
let of_json js =
try find (Js.to_string js)
with Not_found ->
Data.failure "invalid %s (%a)" T.name Json.pp_dump js
end
module Decl = MakeTag
(struct
open Printer_tag
type t = declaration
let name = "decl"
let descr = "AST Declarations markers"
module H = Declaration.Hashtbl
let kid = ref 0
let create = function
| SEnum _ -> Printf.sprintf "#E%d" (incr kid ; !kid)
| SComp _ -> Printf.sprintf "#C%d" (incr kid ; !kid)
| SType _ -> Printf.sprintf "#T%d" (incr kid ; !kid)
| SGlobal vi -> Printf.sprintf "#G%d" vi.vid
| SFunction kf -> Printf.sprintf "#F%d" @@ Kernel_function.get_id kf
| SGAnnot _ -> Printf.sprintf "#A%d" (incr kid; !kid)
end)
module Marker = MakeTag
(struct
open Printer_tag
type t = localizable
let name = "marker"
let descr = "Localizable AST markers"
module H = Localizable.Hashtbl
let kid = ref 0
let create = function
| PStmt(_,s) -> Printf.sprintf "#s%d" s.sid
| PStmtStart(_,s) -> Printf.sprintf "#k%d" s.sid
| PVDecl(_,_,v) -> Printf.sprintf "#v%d" v.vid
| PLval _ -> Printf.sprintf "#l%d" (incr kid ; !kid)
| PExp(_,_,e) -> Printf.sprintf "#e%d" e.eid
| PTermLval _ -> Printf.sprintf "#t%d" (incr kid ; !kid)
| PGlobal _ -> Printf.sprintf "#g%d" (incr kid ; !kid)
| PIP _ -> Printf.sprintf "#p%d" (incr kid ; !kid)
| PType _ -> Printf.sprintf "#y%d" (incr kid ; !kid)
end)
module PrinterTag = Printer_tag.Make(struct let tag = Marker.index end)
module Lval =
struct
open Printer_tag
type t = kinstr * lval
let jtype = Marker.jtype
let to_json (kinstr, lval) =
let kf = match kinstr with
| Kglobal -> None
| Kstmt stmt -> Some (Kernel_function.find_englobing_kf stmt)
in
Marker.to_json (PLval (kf, kinstr, lval))
let find = function
| PLval (_, kinstr, lval) -> kinstr, lval
| PVDecl (_, kinstr, vi) -> kinstr, Cil.var vi
| PGlobal (GVar (vi, _, _) | GVarDecl (vi, _)) -> Kglobal, Cil.var vi
| _ -> raise Not_found
let mem tag = try let _ = find tag in true with Not_found -> false
let of_json js =
try find (Marker.of_json js)
with Not_found -> Data.failure "not a lval marker"
end
module Stmt =
struct
type t = stmt
let jtype = Marker.jtype
let to_json st =
let kf = Kernel_function.find_englobing_kf st in
Marker.to_json (PStmtStart(kf,st))
let of_json js =
let open Printer_tag in
match Marker.of_json js with
| PStmt(_,st) | PStmtStart(_,st) -> st
| _ -> Data.failure "not a stmt marker"
end
module Kinstr =
struct
type t = kinstr
let jtype = Pkg.Joption Marker.jtype
let to_json = function
| Kglobal -> `Null
| Kstmt st -> Stmt.to_json st
let of_json = function
| `Null -> Kglobal
| js -> Kstmt (Stmt.of_json js)
end
module DeclKind =
struct
open Printer_tag
type t = declaration
let jtype = Data.declare
~package ~name:"declKind"
~descr:(Md.plain "Declaration kind")
(Junion [
Jkey "ENUM";
Jkey "UNION";
Jkey "STRUCT";
Jkey "TYPEDEF";
Jkey "GLOBAL";
Jkey "FUNCTION";
Jkey "LFUNPRED";
Jkey "INVARIANT";
Jkey "AXIOMATIC";
Jkey "MODULE";
Jkey "LEMMA";
Jkey "EXTENSION";
Jkey "VOLATILE";
Jkey "LTYPE";
Jkey "MODEL";
])
let global_annotation_kind = function
| Dfun_or_pred _ -> "LFUNPRED"
| Dinvariant _ -> "INVARIANT"
| Dtype_annot _ -> "INVARIANT"
| Daxiomatic _ -> "AXIOMATIC"
| Dmodule _ -> "MODULE"
| Dlemma _ -> "LEMMA"
| Dextended _ -> "EXTENSION"
| Dvolatile _ -> "VOLATILE"
| Dtype _ -> "LTYPE"
| Dmodel_annot _ -> "MODEL"
let to_json = function
| SEnum _ -> `String "ENUM"
| SComp { cstruct = true } -> `String "STRUCT"
| SComp { cstruct = false } -> `String "UNION"
| SType _ -> `String "TYPEDEF"
| SGlobal _ -> `String "GLOBAL"
| SFunction _ -> `String "FUNCTION"
| SGAnnot a -> `String (global_annotation_kind a)
end
module GAnnotRoots = struct
include State_builder.Hashtbl
(Cil_datatype.Global_annotation.Hashtbl)
(Datatype.Unit)
(struct
let name = "Server.Kernel_ast.GAnnotsRoots"
let size = 43
let dependencies = [ Ast.self ]
end)
let is_root ga = mem ga
end
module DeclAttributes =
struct
open Printer_tag
let model = States.model ()
let iter_declaration f =
if Ast.is_computed () then
let marked = Declaration.Hashtbl.create 0 in
Cil.iterGlobals
(Ast.get())
(fun g ->
begin match g with
| GAnnot(ga,_) -> GAnnotRoots.add ga ()
| _ -> ()
end ;
match declaration_of_global g with
| None -> ()
| Some d ->
if not @@ Declaration.Hashtbl.mem marked d then
begin
Declaration.Hashtbl.add marked d () ;
f (d,Decl.index d)
end)
let () =
States.column
~name:"kind"
~descr:(Md.plain "Declaration kind")
~data:(module DeclKind)
~get:fst
model
let () =
States.column
~name:"self"
~descr:(Md.plain "Declaration's marker")
~data:(module Marker)
~get:(fun (decl,_) -> localizable_of_declaration decl)
model
let () =
States.column
~name:"name"
~descr:(Md.plain "Declaration identifier")
~data:(module Jstring)
~get:(fun (decl,_) -> name_of_declaration decl)
model
let () =
States.column
~name:"label"
~descr:(Md.plain "Declaration label (uncapitalized kind & name)")
~data:(module Jstring)
~get:(fun (decl,_) -> Pretty_utils.to_string pp_declaration decl)
model
let () =
States.column
~name:"source"
~descr:(Md.plain "Source location")
~data:(module Position)
~get:(fun (decl,_) -> fst @@ loc_of_declaration decl)
model
let array = States.register_array
~package
~name:"declAttributes"
~descr:(Md.plain "Declaration attributes")
~key:snd
~keyName:"decl"
~keyType:Decl.jtype
~iter:iter_declaration
~add_reload_hook:ast_update_hook
model
let update (d, s) =
match d with
| SGAnnot ga when not @@ GAnnotRoots.is_root ga -> ()
| _ -> States.update array (d,s)
let () = Decl.hook update
end
let print_global_ast global =
let stacked_libc = Kernel.PrintLibc.get () in
try
if not stacked_libc then Kernel.PrintLibc.set true ;
let printer = PrinterTag.(with_unfold_precond (fun _ -> true) pp_global) in
let ast = Jbuffer.to_json printer global in
if not stacked_libc then Kernel.PrintLibc.set false ; ast
with err ->
if not stacked_libc then Kernel.PrintLibc.set false ; raise err
let () = Request.register ~package
~kind:`GET ~name:"printDeclaration"
~descr:(Md.plain "Prints an AST Declaration")
~signals:[ast_changed_signal]
~input:(module Decl) ~output:(module Jtext)
(fun d -> print_global_ast @@ Printer_tag.definition_of_declaration d)
module MarkerKind =
struct
open Printer_tag
type t = localizable
let jtype = Data.declare
~package ~name:"markerKind"
~descr:(Md.plain "Marker kind")
(Junion [
Jkey "STMT";
Jkey "LFUN"; Jkey "DFUN";
Jkey "LVAR"; Jkey "DVAR";
Jkey "LVAL"; Jkey "EXP";
Jkey "TERM";
Jkey "TYPE";
Jkey "PROPERTY";
Jkey "DECLARATION";
])
let to_json = function
| PStmt _ | PStmtStart _ -> `String "STMT"
| PVDecl(_,Kglobal,vi) ->
`String (if Globals.Functions.mem vi then "DFUN" else "DVAR")
| PVDecl _ -> `String "DVAR"
| PTermLval(_,_,_,(TVar { lv_origin = Some vi },TNoOffset))
| PLval(_,_,(Var vi,NoOffset)) ->
`String (if Globals.Functions.mem vi then "LFUN" else "LVAR")
| PLval _ -> `String "LVAL"
| PExp _ -> `String "EXP"
| PTermLval _ -> `String "TERM"
| PType _ -> `String "TYPE"
| PIP _ -> `String "PROPERTY"
| PGlobal _ -> `String "DECLARATION"
end
module MarkerAttributes =
struct
open Printer_tag
let global_annotation_label_kind short = function
| Dfun_or_pred ({ l_type = None }, _) ->
if short then "Pred" else "Predicate"
| Dfun_or_pred _ ->
if short then "LFun" else "Logic Function"
| Dinvariant _ ->
if short then "Inv" else "Invariant"
| Dtype_annot _ ->
if short then "TInv" else "Type Invariant"
| Daxiomatic _ ->
if short then "Ax" else "Axiomatic"
| Dmodule _ ->
if short then "Mod" else "Module"
| Dlemma _ ->
"Lemma"
| Dextended _ ->
if short then "Ext" else "Extension"
| Dvolatile _ ->
if short then "Vol" else "Volatile"
| Dtype _ ->
if short then "LType" else "Logic Type"
| Dmodel_annot _ ->
"Model"
let label_kind ~short m =
match varinfo_of_localizable m with
| Some vi ->
if Globals.Functions.mem vi then "Function" else
if vi.vglob then
if short then "Global" else "Global Variable"
else if vi.vformal then
if short then "Formal" else "Formal Parameter"
else if vi.vtemp then
if short then "Temp" else "Temporary Variable (generated)"
else
if short then "Local" else "Local Variable"
| None ->
match m with
| PStmt _ | PStmtStart _ -> if short then "Stmt" else "Statement"
| PLval _ -> if short then "Lval" else "L-value"
| PTermLval _ -> if short then "Lval" else "ACSL L-value"
| PVDecl _ -> assert false
| PExp _ -> if short then "Expr" else "Expression"
| PIP _ -> if short then "Prop" else "Property"
| PGlobal (GType _ | GCompTag _ | GEnumTag _ | GEnumTagDecl _)
| PType _ -> "Type"
| PGlobal (GAnnot (ga, _)) ->
global_annotation_label_kind short ga
| PGlobal _ -> if short then "Decl" else "Declaration"
let descr_localizable fmt = function
| PGlobal (GType(ti,_)) ->
PrinterTag.pp_typ fmt (Cil_const.mk_tnamed ti)
| PGlobal (GCompTag(ci,_) | GCompTagDecl(ci,_)) ->
PrinterTag.pp_typ fmt (Cil_const.mk_tcomp ci)
| PGlobal (GEnumTag(ei,_) | GEnumTagDecl(ei,_)) ->
PrinterTag.pp_typ fmt (Cil_const.mk_tenum ei)
| g -> pp_localizable fmt g
let model = States.model ()
let () =
States.column
~name:"kind"
~descr:(Md.plain "Marker kind (key)")
~data:(module MarkerKind) ~get:fst
model
let () =
States.option
~name:"scope"
~descr:(Md.plain "Marker Scope (where it is printed in)")
~data:(module Decl)
~get:(fun (tag,_) -> declaration_of_localizable tag)
model
let () =
States.option
~name:"definition"
~descr:(Md.plain "Marker's Target Definition (when applicable)")
~data:(module Marker)
~get:(fun (tag,_) -> definition_of_localizable tag)
model
let () =
States.column
~name:"labelKind"
~descr:(Md.plain "Marker kind label")
~data:(module Jalpha)
~get:(fun (tag,_) -> label_kind ~short:true tag)
model
let () =
States.column
~name:"titleKind"
~descr:(Md.plain "Marker kind title")
~data:(module Jalpha)
~get:(fun (tag,_) -> label_kind ~short:false tag)
model
let () =
States.option
~name:"name"
~descr:(Md.plain "Marker identifier (when applicable)")
~data:(module Jalpha)
~get:(fun (tag, _) -> Printer_tag.name_of_localizable tag)
model
let () =
States.column
~name:"descr"
~descr:(Md.plain "Marker description")
~data:(module Jstring)
~get:(fun (tag, _) -> Rich_text.to_string descr_localizable tag)
model
let () =
let get (tag, _) =
let pos = fst (Printer_tag.loc_of_localizable tag) in
if Cil_datatype.Position.(equal unknown pos) then None else Some pos
in
States.option
~name:"sloc"
~descr:(Md.plain "Source location")
~data:(module Position)
~get
model
let array = States.register_array
~package
~name:"markerAttributes"
~descr:(Md.plain "Marker attributes")
~key:snd
~keyName:"marker"
~keyType:Marker.jtype
~iter:Marker.iter
~add_reload_hook:ast_update_hook
model
let () = Marker.hook (States.update array)
end
let () = Request.register ~package
~kind:`GET ~name:"getMainFunction"
~descr:(Md.plain "Get the current 'main' function.")
~input:(module Junit) ~output:(module Joption(Decl))
begin fun () ->
try Some (SFunction (fst @@ Globals.entry_point ()))
with Globals.No_such_entry_point _ -> None
end
let () = Request.register ~package
~kind:`GET ~name:"getFunctions"
~descr:(Md.plain "Collect all functions in the AST")
~input:(module Junit) ~output:(module Jlist(Decl))
begin fun () ->
let pool = ref [] in
Globals.Functions.iter
(fun kf -> pool := Printer_tag.SFunction kf :: !pool) ;
List.rev !pool
end
module Functions =
struct
let key kf = Printf.sprintf "kf#%d" (Kernel_function.get_id kf)
let signature kf =
let g = Kernel_function.get_global kf in
let libc = Kernel.PrintLibc.get () in
try
if not libc then Kernel.PrintLibc.set true ;
let txt = Rich_text.to_string Printer_tag.pp_localizable (PGlobal g) in
if not libc then Kernel.PrintLibc.set false ;
if Kernel_function.is_entry_point kf then (txt ^ " /* main */") else txt
with err ->
if not libc then Kernel.PrintLibc.set false ; raise err
let is_builtin kf =
Cil_builtins.has_fc_builtin_attr (Kernel_function.get_vi kf)
let is_extern kf =
let vi = Kernel_function.get_vi kf in
vi.vstorage = Extern
let iter f =
Globals.Functions.iter
(fun kf ->
let name = Kernel_function.get_name kf in
if not (Ast_info.start_with_frama_c_builtin name) then f kf)
let array : kernel_function States.array =
begin
let model = States.model () in
States.column model
~name:"decl"
~descr:(Md.plain "Declaration Tag")
~data:(module Decl)
~get:(fun kf -> Printer_tag.SFunction kf) ;
States.column model
~name:"name"
~descr:(Md.plain "Name")
~data:(module Data.Jalpha)
~get:Kernel_function.get_name ;
States.column model
~name:"signature"
~descr:(Md.plain "Signature")
~data:(module Data.Jstring)
~get:signature ;
States.column model
~name:"main"
~descr:(Md.plain "Is the function the main entry point")
~data:(module Data.Jbool)
~default:false
~get:Kernel_function.is_entry_point;
States.column model
~name:"defined"
~descr:(Md.plain "Is the function defined?")
~data:(module Data.Jbool)
~default:false
~get:Kernel_function.is_definition;
States.column model
~name:"stdlib"
~descr:(Md.plain "Is the function from the Frama-C stdlib?")
~data:(module Data.Jbool)
~default:false
~get:Kernel_function.is_in_libc;
States.column model
~name:"builtin"
~descr:(Md.plain "Is the function a Frama-C builtin?")
~data:(module Data.Jbool)
~default:false
~get:is_builtin;
States.column model
~name:"extern"
~descr:(Md.plain "Is the function extern?")
~data:(module Data.Jbool)
~default:false
~get:is_extern;
States.column model
~name:"sloc"
~descr:(Md.plain "Source location")
~data:(module Position)
~get:(fun kf -> fst (Kernel_function.get_location kf));
States.register_array model
~package ~key
~name:"functions"
~descr:(Md.plain "AST Functions")
~iter
~add_reload_hook:ast_update_hook
end
end
module GlobalVars = struct
let key vi = Printf.sprintf "vi#%d" vi.vid
let _ : varinfo States.array =
let model = States.model () in
States.column model
~name:"decl"
~descr:(Md.plain "Declaration Tag")
~data:(module Decl)
~get:(fun vi -> Printer_tag.SGlobal vi);
States.column model
~name:"name"
~descr:(Md.plain "Name")
~data:(module Data.Jalpha)
~get:(fun vi -> vi.vname);
States.column model
~name:"type"
~descr:(Md.plain "Type")
~data:(module Jstring)
~get:(fun vi -> Rich_text.to_string Printer.pp_typ vi.vtype);
States.column model
~name:"stdlib"
~descr:(Md.plain "Is the variable from the Frama-C stdlib?")
~data:(module Data.Jbool)
~get:(fun vi -> Cil.is_in_libc vi.vattr);
States.column model
~name:"extern"
~descr:(Md.plain "Is the variable extern?")
~data:(module Data.Jbool)
~get:(fun vi -> vi.vstorage = Extern);
States.column model
~name:"const"
~descr:(Md.plain "Is the variable const?")
~data:(module Data.Jbool)
~get:(fun vi -> Cil.isGlobalInitConst vi);
States.column model
~name:"volatile"
~descr:(Md.plain "Is the variable volatile?")
~data:(module Data.Jbool)
~get:(fun vi -> Ast_types.is_volatile vi.vtype);
States.column model
~name:"ghost"
~descr:(Md.plain "Is the variable ghost?")
~data:(module Data.Jbool)
~get:(fun vi -> Ast_types.is_ghost vi.vtype);
States.column model
~name:"init"
~descr:(Md.plain "Is the variable explicitly initialized?")
~data:(module Data.Jbool)
~get:(fun vi -> Option.is_some (Globals.Vars.find vi).init);
States.column model
~name:"source"
~descr:(Md.plain "Is the variable in the source code?")
~data:(module Data.Jbool)
~get:(fun vi -> vi.vsource);
States.column model
~name:"sloc"
~descr:(Md.plain "Source location")
~data:(module Position)
~get:(fun vi -> fst vi.vdecl);
States.register_array model
~package ~key
~name:"globals"
~descr:(Md.plain "AST global variables")
~iter:(fun f -> Globals.Vars.iter (fun vi _init -> f vi))
~add_reload_hook:ast_update_hook
end
module Information =
struct
type info = {
id: string;
rank: int;
label: string;
title: string;
descr: string;
enable: unit -> bool;
pretty: Format.formatter -> Printer_tag.localizable -> unit
}
module S =
struct
type t = (info * Jtext.t)
let jtype = Package.(Jrecord[
"id", Jstring ;
"label", Jstring ;
"title", Jstring ;
"descr", Jstring ;
"text", Jtext.jtype ;
])
let of_json _ = failwith "Information.Info"
let to_json (info,text) = `Assoc [
"id", `String info.id ;
"label", `String info.label ;
"title", `String info.title ;
"descr", `String info.descr ;
"text", text ;
]
end
let rankId = ref 0
let registry : (string,info) Hashtbl.t = Hashtbl.create 0
let jtext pp marker =
try
let buffer = Jbuffer.create () in
let fmt = Jbuffer.formatter buffer in
pp fmt marker;
Format.pp_print_flush fmt ();
Jbuffer.contents buffer
with Not_found ->
`Null
let rank ({rank},_) = rank
let by_rank a b = Stdlib.compare (rank a) (rank b)
let get_information tgt =
let infos = ref [] in
Hashtbl.iter
(fun _ info ->
if info.enable () then
match tgt with
| None -> infos := (info, `Null) :: !infos
| Some marker ->
let text = jtext info.pretty marker in
if not (Jbuffer.is_empty text) then
infos := (info, text) :: !infos
) registry ;
List.sort by_rank !infos
let signal = Request.signal ~package
~name:"getInformationUpdate"
~descr:(Md.plain "Updated AST information")
let update () = Request.emit signal
let register ~id ~label ~title
?(descr = title)
?(enable = fun _ -> true)
pretty =
let rank = incr rankId ; !rankId in
let info = { id ; rank ; label ; title ; descr; enable ; pretty } in
if Hashtbl.mem registry id then
( let msg = Format.sprintf
"Server.Kernel_ast.register_info: duplicate %S" id in
raise (Invalid_argument msg) );
Hashtbl.add registry id info
end
let () = Request.register ~package
~kind:`GET ~name:"getInformation"
~descr:(Md.plain
"Get available information about markers. \
When no marker is given, returns all kinds \
of information (with empty `descr` field).")
~input:(module Joption(Marker))
~output:(module Jlist(Information.S))
~signals:[Information.signal]
Information.get_information
let () = Information.register
~id:"kernel.ast.location"
~label:"Location"
~title:"Source file location"
begin fun fmt loc ->
let pos = fst @@ Printer_tag.loc_of_localizable loc in
if Filepath.is_empty pos.pos_path then
raise Not_found ;
Filepath.pp_pos fmt pos
end
let () = Information.register
~id:"kernel.ast.varinfo"
~label:"Var"
~title:"Variable Information"
begin fun fmt loc ->
match loc with
| PLval (_ , _, (Var x,NoOffset)) | PVDecl(_,_,x) ->
if not x.vreferenced then Format.pp_print_string fmt "unused " ;
begin
match x.vstorage with
| NoStorage -> ()
| Extern -> Format.pp_print_string fmt "extern "
| Static -> Format.pp_print_string fmt "static "
| Register -> Format.pp_print_string fmt "register "
end ;
if x.vghost then Format.pp_print_string fmt "ghost " ;
if x.vaddrof then Format.pp_print_string fmt "aliased " ;
if x.vformal then Format.pp_print_string fmt "formal" else
if x.vglob then Format.pp_print_string fmt "global" else
if x.vtemp then Format.pp_print_string fmt "temporary" else
Format.pp_print_string fmt "local" ;
| _ -> raise Not_found
end
let () = Information.register
~id:"kernel.ast.typeinfo"
~label:"Type"
~title:"Type of C/ASCL expression"
begin fun fmt loc ->
let open Printer in
match loc with
| PExp (_, _, e) -> pp_typ fmt (Cil.typeOf e)
| PLval (_, _, lval) -> pp_typ fmt (Cil.typeOfLval lval)
| PTermLval(_,_,_,lv) -> pp_logic_type fmt (Cil.typeOfTermLval lv)
| PVDecl (_,_,vi) -> pp_typ fmt vi.vtype
| _ -> raise Not_found
end
let () = Information.register
~id:"kernel.ast.typedef"
~label:"Typedef"
~title:"Type Definition"
begin fun fmt loc ->
match loc with
| PType ({ tnode = TNamed _ } as ty)
| PGlobal (GType({ ttype = ty },_)) ->
begin
let tdef = Ast_types.unroll ty in
match Printer_tag.definition_of_type tdef with
| Some marker ->
let tag = Marker.index marker in
Format.fprintf fmt "@{<%s>%a@}" tag Printer.pp_typ tdef
| None -> PrinterTag.pp_typ fmt tdef
end
| _ -> raise Not_found
end
let () = Information.register
~id:"kernel.ast.typesizeof"
~label:"Sizeof"
~title:"Size of a C-type or C-variable"
begin fun fmt loc ->
let typ =
match loc with
| PType typ -> typ
| PVDecl(_,_,vi) -> vi.vtype
| PGlobal (GType(ti,_)) -> ti.ttype
| PGlobal (GCompTagDecl(ci,_) | GCompTag(ci,_)) -> Cil_const.mk_tcomp ci
| PGlobal (GEnumTagDecl(ei,_) | GEnumTag(ei,_)) -> Cil_const.mk_tenum ei
| _ -> raise Not_found
in
let bits =
try Cil.bitsSizeOf typ
with Cil.SizeOfError _ -> raise Not_found
in
let bytes = bits / 8 in
let rbits = bits mod 8 in
if rbits > 0 then
if bytes > 0 then
Format.fprintf fmt "%d bytes + %d bits" bytes rbits
else
Format.fprintf fmt "%d bits" rbits
else
Format.fprintf fmt "%d bytes" bytes
end
let () = Information.register
~id:"kernel.ast.propertyStatus"
~label:"Status"
~title:"Property Consolidated Status"
begin fun fmt loc ->
match loc with
| PIP prop when Property.has_status prop ->
Property_status.Feedback.pretty fmt @@
Property_status.Feedback.get prop
| _ -> raise Not_found
end
let () = Information.register
~id:"kernel.ast.marker"
~label:"Marker"
~title:"Ivette marker (for debugging)"
~enable:(fun _ -> Server_parameters.debug_atleast 1)
begin fun fmt loc ->
let tag = Marker.index loc in
Format.fprintf fmt "%S" tag
end
let () = Server_parameters.Debug.add_hook_on_update
(fun _ -> Information.update ())
let get_marker_at ~file ~line ~col =
if file="" then None else
let pos_path = Filepath.of_string file in
let pos =
Filepath.{ pos_path; pos_lnum = line; pos_cnum = col; pos_bol = 0; }
in
Printer_tag.loc_to_localizable ~precise_col:true pos
let () =
let descr =
Md.plain
"Returns the marker and function at a source file position, if any. \
Input: file path, line and column. \
File can be empty, in case no marker is returned."
in
let signature = Request.signature
~output:(module Joption(Marker)) () in
let get_file = Request.param signature
~name:"file" ~descr:(Md.plain "File path") (module Jstring) in
let get_line = Request.param signature
~name:"line" ~descr:(Md.plain "Line (1-based)") (module Jint) in
let get_col = Request.param signature
~name:"column" ~descr:(Md.plain "Column (0-based)") (module Jint) in
Request.register_sig signature
~package ~descr ~kind:`GET ~name:"getMarkerAt"
~signals:[ast_changed_signal]
(fun rq () ->
get_marker_at ~file:(get_file rq) ~line:(get_line rq) ~col:(get_col rq))
let get_files () =
let files = Kernel.Files.get () in
List.map (fun f -> (f:Filepath.t:>string)) files
let () =
Request.register
~package
~descr:(Md.plain "Get the currently analyzed source file names")
~kind:`GET
~name:"getFiles"
~input:(module Junit) ~output:(module Jlist(Jstring))
get_files
let set_files files =
let s = String.concat "," files in
Kernel.Files.As_string.set s
let () =
Request.register
~package
~descr:(Md.plain "Set the source file names to analyze.")
~kind:`SET
~name:"setFiles"
~input:(module Jlist(Jstring))
~output:(module Junit)
set_files
let environment () =
let open Logic_typing in
Lenv.empty () |> append_pre_label |> append_here_label
let parse_expr env kf stmt term =
let term = Logic_parse_string.term ~env kf term in
let exp = Logic_to_c.term_to_exp term in
Printer_tag.PExp (Some kf, Kstmt stmt, exp)
let parse_lval env kf stmt term =
let term = Logic_parse_string.term_lval ~env kf term in
let lval = Logic_to_c.term_lval_to_lval term in
Printer_tag.PLval (Some kf, Kstmt stmt, lval)
let build_marker parse marker term =
match Printer_tag.ki_of_localizable marker with
| Kglobal -> Data.failure "No statement at selection point"
| Kstmt stmt ->
let module C = Logic_to_c in
let module Parser = Logic_parse_string in
let kf () = Kernel_function.find_englobing_kf stmt in
try parse (environment ()) (kf ()) stmt term
with Not_found | Parser.(Error _ | Unbound _) | C.No_conversion ->
Data.failure "Invalid term"
let () =
let module Md = Markdown in
let s = Request.signature ~output:(module Marker) () in
let get_marker = Request.param s ~name:"stmt"
~descr:(Md.plain "Marker position from where to localize the term")
(module Marker) in
let get_term = Request.param s ~name:"term"
~descr:(Md.plain "ACSL term to parse")
(module Data.Jstring) in
Request.register_sig ~package s
~kind:`GET ~name:"parseExpr"
~descr:(Md.plain "Parse a C expression and returns the associated marker")
(fun rq () -> build_marker parse_expr (get_marker rq) (get_term rq))
let () =
let module Md = Markdown in
let s = Request.signature ~output:(module Marker) () in
let get_marker = Request.param s ~name:"stmt"
~descr:(Md.plain "Marker position from where to localize the term")
(module Marker) in
let get_term = Request.param s ~name:"term"
~descr:(Md.plain "ACSL term to parse")
(module Data.Jstring) in
Request.register_sig ~package s
~kind:`GET ~name:"parseLval"
~descr:(Md.plain "Parse a C lvalue and returns the associated marker")
(fun rq () -> build_marker parse_lval (get_marker rq) (get_term rq))