Source file metrics_cabs.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
(** Implementation of cyclomatic complexity measures on CAbs' AST *)
open Cabs
open Metrics_base
open Metrics_base.BasicMetrics
class metricsCabsVisitor = object(self)
inherit Cabsvisit.nopCabsVisitor
val global_metrics = ref empty_metrics
val local_metrics = ref empty_metrics
val was_case = ref false
val mutable metrics_map:
(BasicMetrics.t Metrics_base.OptionKf.Map.t) Datatype.Filepath.Map.t =
Datatype.Filepath.Map.empty
val functions_no_source: (string, int) Hashtbl.t = Hashtbl.create 97
val functions_with_source: (string, int) Hashtbl.t = Hashtbl.create 97
val mutable standalone = true
method functions_no_source = functions_no_source
method functions_with_source = functions_with_source
method set_standalone v = standalone <- v
method get_metrics = !global_metrics
method private update_metrics_map filename strmap =
metrics_map <- Datatype.Filepath.Map.add filename strmap metrics_map
method private incr_both_metrics f =
apply_then_set f global_metrics;
apply_then_set f local_metrics
method add_to_functions_with_source (funcname:string) =
Hashtbl.add functions_with_source funcname 0;
Hashtbl.remove functions_no_source funcname;
method private record_and_clear metrics =
let filename = metrics.cfile_name
and func = metrics.cfunc in
local_metrics := BasicMetrics.set_cyclo !local_metrics
(BasicMetrics.compute_cyclo !local_metrics);
global_metrics := BasicMetrics.set_cyclo !global_metrics
(!global_metrics.ccyclo + !local_metrics.ccyclo);
(try
let fun_tbl = Datatype.Filepath.Map.find filename metrics_map in
self#update_metrics_map filename
(Metrics_base.OptionKf.Map.add func !local_metrics fun_tbl);
with
| Not_found ->
let new_stringmap =
Metrics_base.OptionKf.Map.add func !local_metrics
Metrics_base.OptionKf.Map.empty
in
self#update_metrics_map filename new_stringmap;
);
local_metrics := empty_metrics;
method! vdef def =
match def with
| FUNDEF (_, sname, _, _, _) ->
begin
let funcname = Metrics_base.extract_fundef_name sname in
local_metrics :=
{!local_metrics with
cfile_name = get_filename def;
cfunc = Some (Metrics_base.kf_of_cabs_name sname);
cfuncs = 1; };
Metrics_parameters.debug
~level:1 "Definition of function %s encountered@." funcname;
apply_then_set incr_funcs global_metrics;
self#add_to_functions_with_source funcname;
Cil.ChangeDoChildrenPost
([def],
fun _ ->
begin
if !local_metrics <> empty_metrics
then self#record_and_clear !local_metrics;
[def]
end
);
end
| DECDEF _
| TYPEDEF _
| ONLYTYPEDEF _
| GLOBASM _
| PRAGMA _
| STATIC_ASSERT _
| LINKAGE _
| GLOBANNOT _ -> Cil.DoChildren;
method! vexpr expr =
(match expr.expr_node with
| NOTHING -> ()
| UNARY (unop, _) ->
begin
match unop with
| PREINCR
| POSINCR
| PREDECR
| POSDECR -> self#incr_both_metrics incr_assigns
| MINUS
| PLUS
| NOT
| BNOT -> ()
| MEMOF -> self#incr_both_metrics incr_ptrs
| ADDROF -> ()
end
| LABELADDR _ -> ()
| BINARY (bop, _, _) ->
begin
match bop with
| ADD | SUB | MUL | DIV | MOD
| BAND | BOR | XOR
| SHL | SHR | EQ | NE | LT
| GT | LE | GE -> ()
| AND | OR -> self#incr_both_metrics incr_dpoints
| ASSIGN
| ADD_ASSIGN | SUB_ASSIGN | MUL_ASSIGN
| DIV_ASSIGN | BOR_ASSIGN | XOR_ASSIGN
| SHL_ASSIGN | SHR_ASSIGN | BAND_ASSIGN
| MOD_ASSIGN ->
self#incr_both_metrics incr_assigns;
end
| CAST _ -> ()
| CALL _ -> self#incr_both_metrics incr_calls;
| QUESTION _ ->
self#incr_both_metrics incr_dpoints;
self#incr_both_metrics incr_ifs;
| COMMA _
| CONSTANT _
| PAREN _
| VARIABLE _
| EXPR_SIZEOF _
| TYPE_SIZEOF _
| EXPR_ALIGNOF _
| TYPE_ALIGNOF _
| INDEX _
| MEMBEROF _
| MEMBEROFPTR _
| GNU_BODY _
| EXPR_PATTERN _
| GENERIC _ -> ());
Cil.DoChildren
method private set_case stmt =
match stmt.stmt_node with
| CASERANGE _ | CASE _ -> was_case := true;
| DEFAULT _
| _ -> was_case := false
method! vstmt stmt =
self#incr_both_metrics incr_slocs;
(match stmt.stmt_node with
| DEFAULT _ -> ()
| CASERANGE _
| CASE _ ->
if not !was_case then self#incr_both_metrics incr_dpoints;
| IF _ ->
self#incr_both_metrics incr_ifs;
self#incr_both_metrics incr_dpoints;
| NOP _
| COMPUTATION _
| BLOCK _ -> ()
| WHILE _
| DOWHILE _
| FOR _ ->
self#incr_both_metrics incr_loops;
self#incr_both_metrics incr_dpoints;
| BREAK _
| CONTINUE _ -> ()
| RETURN _ | THROW _ -> self#incr_both_metrics incr_exits;
| SWITCH _ -> ()
| LABEL _ -> ()
| GOTO _
| COMPGOTO _ -> self#incr_both_metrics incr_gotos;
| DEFINITION _
| ASM _
| SEQUENCE _
| TRY_EXCEPT _
| TRY_FINALLY _
| TRY_CATCH _
| CODE_ANNOT _
| CODE_SPEC _ -> ());
self#set_case stmt;
Cil.DoChildren
method private stats_of_filename filename =
try Datatype.Filepath.Map.find filename metrics_map
with
| Not_found ->
Metrics_parameters.fatal "Metrics for file %a not_found@."
Datatype.Filepath.pretty filename
method pp_file_metrics fmt filename =
Format.fprintf fmt "@[<v 0>%a@]"
(fun fmt filename ->
let fun_tbl = self#stats_of_filename filename in
OptionKf.Map.iter (fun _fun_name fmetrics ->
Format.fprintf fmt "@ %a" pp_base_metrics fmetrics)
fun_tbl;
) filename
method pp_detailed_text_metrics fmt () =
Datatype.Filepath.Map.iter
(fun filename _func_tbl ->
Format.fprintf fmt "%a" self#pp_file_metrics filename) metrics_map
end
;;
(** Halstead metrics computation *)
module Halstead = struct
let update_val value key tbl =
try
let v = Hashtbl.find tbl key in
Hashtbl.replace tbl key (v + value);
with
| Not_found -> Hashtbl.add tbl key value
;;
let update_val_incr key tbl = update_val 1 key tbl;;
type operand_tbl = {
var_tbl : (string, int) Hashtbl.t;
cst_tbl : (Cabs.constant, int) Hashtbl.t;
}
;;
type operator_tbl = {
knownop_tbl : (string, int) Hashtbl.t;
otherop_tbl : (string, int) Hashtbl.t;
reserved_tbl : (string, int) Hashtbl.t;
tspec_tbl : (Cabs.typeSpecifier, int) Hashtbl.t;
}
;;
let id_from_init iname =
match (fst iname) with
| s, _, _, _ -> s
;;
class halsteadCabsVisitor = object(self)
inherit Cabsvisit.nopCabsVisitor
val operand_tbl = {
var_tbl = Hashtbl.create 7;
cst_tbl = Hashtbl.create 7;
}
val operator_tbl = {
knownop_tbl = Hashtbl.create 7;
otherop_tbl = Hashtbl.create 7;
reserved_tbl = Hashtbl.create 7;
tspec_tbl = Hashtbl.create 7;
}
method get_operator_tbl () = operator_tbl
method get_operand_tbl () = operand_tbl
method add_paren () =
update_val_incr "(" operator_tbl.otherop_tbl;
update_val_incr ")" operator_tbl.otherop_tbl;
method! vexpr e =
match e.Cabs.expr_node with
| UNARY _ ->
let unop = fst (Cprint.get_operator e) in
update_val_incr unop operator_tbl.knownop_tbl;
Cil.DoChildren;
| BINARY _ ->
let binop = fst (Cprint.get_operator e) in
update_val_incr binop operator_tbl.knownop_tbl;
Cil.DoChildren;
| QUESTION _ ->
update_val_incr "?" operator_tbl.otherop_tbl;
update_val_incr ":" operator_tbl.otherop_tbl;
Cil.DoChildren;
| COMMA elist ->
let n = List.length elist in
if (n > 1) then
update_val (n - 1) "," operator_tbl.otherop_tbl;
Cil.DoChildren;
| CONSTANT c ->
update_val_incr c operand_tbl.cst_tbl;
Cil.DoChildren;
| PAREN _ ->
self#add_paren ();
Cil.DoChildren;
| VARIABLE s ->
update_val_incr s operand_tbl.var_tbl;
Cil.DoChildren;
| EXPR_SIZEOF _ ->
update_val_incr "sizeof" operator_tbl.reserved_tbl;
Cil.DoChildren;
| TYPE_SIZEOF _ ->
update_val_incr "sizeof" operator_tbl.reserved_tbl;
Cil.DoChildren;
| INDEX _ ->
update_val_incr "[]" operator_tbl.otherop_tbl;
Cil.DoChildren;
| _ -> Cil.DoChildren;
method! vstmt s =
let reserved rstr =
update_val_incr rstr operator_tbl.reserved_tbl;
Cil.DoChildren;
in
match s.Cabs.stmt_node with
| BLOCK _ ->
update_val_incr "{" operator_tbl.otherop_tbl;
update_val_incr "}" operator_tbl.otherop_tbl;
Cil.DoChildren;
| SEQUENCE _ ->
print_string "seq\n";
update_val_incr ";" operator_tbl.otherop_tbl;
Cil.DoChildren;
| IF _ -> self#add_paren (); reserved "if";
| WHILE _ -> self#add_paren (); reserved "while";
| DOWHILE _ ->
update_val_incr "do" operator_tbl.reserved_tbl;
self#add_paren ();
reserved "while";
| FOR _ ->
self#add_paren ();
update_val 2 ";" operator_tbl.otherop_tbl;
reserved "for";
| BREAK _ -> reserved "break";
| CONTINUE _ -> reserved "continue";
| RETURN _ -> reserved "return";
| SWITCH _ -> self#add_paren (); reserved "switch";
| CASE _ -> reserved "case";
| CASERANGE _ ->
update_val_incr "..." operator_tbl.otherop_tbl;
update_val 2 ";" operator_tbl.otherop_tbl;
reserved "case";
| DEFAULT _ -> reserved "default";
| LABEL _ ->
update_val_incr ":" operator_tbl.otherop_tbl;
Cil.DoChildren;
| GOTO (s, _) ->
let lname = Format.sprintf "label_%s" s in
update_val_incr lname operand_tbl.var_tbl;
reserved "goto";
| COMPGOTO _ ->
update_val_incr "*" operator_tbl.otherop_tbl;
reserved "goto";
| DEFINITION _ -> Cil.DoChildren;
| ASM _ -> reserved "asm";
| TRY_EXCEPT _ ->
update_val_incr "except" operator_tbl.reserved_tbl;
reserved "try";
| TRY_FINALLY _ ->
update_val_incr "finally" operator_tbl.reserved_tbl;
reserved "try";
| _ -> Cil.DoChildren;
method! vtypespec tspec =
update_val_incr tspec operator_tbl.tspec_tbl;
Cil.DoChildren;
method! vspec spec =
let reserved rstr =
update_val_incr rstr operator_tbl.reserved_tbl;
in
let do_spec s =
match s with
| SpecTypedef -> reserved "typedef"
| SpecInline -> reserved "inline"
| SpecStorage AUTO -> reserved "auto"
| SpecStorage STATIC -> reserved "static"
| SpecStorage EXTERN -> reserved "extern"
| SpecStorage REGISTER -> reserved "register"
| SpecCV CV_CONST -> reserved "const"
| SpecCV CV_VOLATILE -> reserved "volatile"
| SpecCV CV_RESTRICT -> reserved "restrict"
| _ -> ()
in List.iter do_spec spec; Cil.DoChildren;
method! vdecltype tdecl =
match tdecl with
| JUSTBASE ->
Cil.SkipChildren;
| PARENTYPE _ ->
self#add_paren ();
Cil.DoChildren;
| ARRAY _ ->
update_val_incr "array" operator_tbl.reserved_tbl;
Cil.DoChildren;
| PTR _ ->
update_val_incr "*" operator_tbl.otherop_tbl;
Cil.DoChildren;
| PROTO _ ->
Cil.SkipChildren;
method! vinitexpr ie =
( match ie with
| COMPOUND_INIT l ->
let n = List.length l in
if n > 0 then
update_val n "," operator_tbl.otherop_tbl;
| _ -> ());
Cil.DoChildren
method! vblock b =
if b.bstmts <> [] then (
let n = List.length b.bstmts in
update_val n ";" operator_tbl.otherop_tbl);
if b.battrs <> [] then
update_val (List.length b.battrs) "," operator_tbl.otherop_tbl;
Cil.DoChildren;
method! vdef d =
match d with
| FUNDEF (bl, (_, (fname, dtype, _, nloc)), b, loc1, loc2) ->
Cil.ChangeDoChildrenPost(
[FUNDEF(bl, ([], (fname, dtype, [], nloc)), b, loc1, loc2)],
fun x -> x)
| DECDEF (_, (_, name_list), _) ->
let n =
List.fold_left
(fun acc n ->
update_val_incr (id_from_init n) operand_tbl.var_tbl;
acc + 1 )
(-1) name_list in
begin
assert(n >= 0);
if (n > 0) then update_val n "," operator_tbl.otherop_tbl;
Cil.DoChildren;
end
| _ -> Cil.DoChildren
end
;;
let compose _x1 y1 (x2, y2) = (1 + x2), (y1 + y2);;
let fold x y = Hashtbl.fold compose x y;;
let compute_operators operator_tbl =
let x, y =
fold operator_tbl.tspec_tbl (
fold operator_tbl.otherop_tbl (
fold operator_tbl.reserved_tbl (
fold operator_tbl.knownop_tbl (0,0))))
in (float_of_int x), (float_of_int y)
;;
let compute_operands operand_tbl =
let x, y =
fold operand_tbl.cst_tbl (
fold operand_tbl.var_tbl (0,0))
in (float_of_int x), (float_of_int y)
;;
type halstead_metrics = {
distinct_operators : float;
total_operators : float;
distinct_operands : float;
total_operands : float;
program_length : float;
program_volume : float;
program_level : float;
vocabulary_size : float;
difficulty_level : float;
effort_to_implement : float;
time_to_implement : float;
bugs_delivered : float;
}
let get_metrics cabs_visitor =
let operator_tbl = cabs_visitor#get_operator_tbl () in
let operand_tbl = cabs_visitor#get_operand_tbl () in
let distinct_operators, total_operators = compute_operators operator_tbl
and distinct_operands, total_operands = compute_operands operand_tbl in
let program_length = total_operands +. total_operators in
let vocabulary_size = distinct_operands +. distinct_operators in
let log2 x = (log x) /. (log 2.0) in
let program_volume = program_length *. (log2 vocabulary_size) in
let difficulty_level =
(distinct_operators /. 2.) *. (total_operands /. distinct_operands) in
let program_level = 1. /. difficulty_level in
let effort_to_implement = program_volume *. difficulty_level in
let time_to_implement = effort_to_implement /. 18. in
let bugs_delivered = (effort_to_implement ** (2./.3.)) /. 3000. in
{ distinct_operators = distinct_operators;
total_operators = total_operators;
distinct_operands = distinct_operands;
total_operands = total_operands;
program_length = program_length;
program_volume = program_volume;
program_level = program_level;
vocabulary_size = vocabulary_size;
difficulty_level = difficulty_level;
effort_to_implement = effort_to_implement;
time_to_implement = time_to_implement;
bugs_delivered = bugs_delivered;
}
;;
let to_list hmetrics =
[ [ "Total operators"; float_to_string hmetrics.total_operators; ];
[ "Distinct operators"; float_to_string hmetrics.distinct_operators; ];
[ "Total_operands"; float_to_string hmetrics.total_operands; ];
[ "Distinct operands"; float_to_string hmetrics.distinct_operands; ];
[ "Program length"; float_to_string hmetrics.program_length; ];
[ "Vocabulary size"; float_to_string hmetrics.vocabulary_size; ];
[ "Program volume"; float_to_string hmetrics.program_volume; ];
[ "Effort"; float_to_string hmetrics.effort_to_implement; ];
[ "Program level"; float_to_string hmetrics.program_level; ];
[ "Difficulty level"; float_to_string hmetrics.difficulty_level; ];
[ "Time to implement"; float_to_string hmetrics.time_to_implement; ];
[ "Bugs delivered"; float_to_string hmetrics.bugs_delivered; ];
]
;;
let pp_metrics ppf cabs_visitor =
let metrics = get_metrics cabs_visitor in
let minutes = (int_of_float metrics.time_to_implement) / 60 in
let _hours, _minutes = minutes / 60, minutes mod 60 in
let operator_tbl = cabs_visitor#get_operator_tbl () in
let operand_tbl = cabs_visitor#get_operand_tbl () in
let dummy_cst cst =
{ expr_loc = Cil_datatype.Location.unknown;
expr_node = CONSTANT cst;
}
and simple_pp_htbl ppf htbl =
Hashtbl.iter (fun k v -> Format.fprintf ppf "%s: %d@ " k v) htbl in
let title = "Halstead metrics"
and stats = "Global statistics (Halstead)"
and operator_sec = "Operators"
and operand_sec = "Operands" in
Format.fprintf ppf
"@[<v 0>%a@ %a@ @ \
%a@ \
@[<v 2>%a@ %a%a%a%a@]@ \
@[<v 2>%a@ %a%a@]@ \
@]"
(mk_hdr 1) title
(fun ppf l ->
List.iter (fun rowl ->
Format.fprintf ppf "@[<hov>";
(match rowl with
| title :: contents ->
Format.fprintf ppf "%s:@ " title;
List.iter (fun s -> Format.fprintf ppf "%s@ " s) contents;
| [] -> ());
Format.fprintf ppf "@]@ ";
) l) (to_list metrics)
(mk_hdr 1) stats
(mk_hdr 2) operator_sec
simple_pp_htbl operator_tbl.reserved_tbl
simple_pp_htbl operator_tbl.otherop_tbl
simple_pp_htbl operator_tbl.knownop_tbl
(fun ppf htbl ->
Hashtbl.iter
(fun k v ->
Format.fprintf ppf "%a: %d@ " Cprint.print_type_spec k v) htbl)
operator_tbl.tspec_tbl
(mk_hdr 2) operand_sec
simple_pp_htbl operand_tbl.var_tbl
(fun ppf htbl ->
Hashtbl.iter
(fun k v ->
Format.fprintf ppf "%a: %d@ " Cprint.print_expression (dummy_cst k) v)
htbl)
operand_tbl.cst_tbl;
;;
let compute_metrics () =
let cabs_files = Ast.UntypedFiles.get () in
let cabs_visitor = new halsteadCabsVisitor in
List.iter (fun file ->
ignore (Cabsvisit.visitCabsFile (cabs_visitor:>Cabsvisit.cabsVisitor) file))
cabs_files ;
Metrics_parameters.result "%a" pp_metrics cabs_visitor
let get_metrics () =
let cabs_files = Ast.UntypedFiles.get () in
let cabs_visitor = new halsteadCabsVisitor in
List.iter (fun file ->
ignore (Cabsvisit.visitCabsFile (cabs_visitor:>Cabsvisit.cabsVisitor) file))
cabs_files ;
get_metrics cabs_visitor
;;
end
let compute_on_cabs () =
try
let cabs_files = Ast.UntypedFiles.get () in
let cabs_visitor = new metricsCabsVisitor in
List.iter (fun file ->
Metrics_parameters.debug
~level:2 "Compute Cabs metrics for file %a@."
Datatype.Filepath.pretty (fst file);
ignore
(Cabsvisit.visitCabsFile (cabs_visitor:>Cabsvisit.cabsVisitor) file);
)
cabs_files
;
if Metrics_parameters.ByFunction.get () then
Metrics_parameters.result
"@[<v 0>Cabs:@ %a@]" cabs_visitor#pp_detailed_text_metrics ();
Halstead.compute_metrics ();
with
| Ast.NoUntypedAst ->
Metrics_parameters.warning
"@[<v 0> Project has no untyped AST. Only metrics over normalized CIL \
AST are available. \
@]@."