-
Notifications
You must be signed in to change notification settings - Fork 21
/
mh_metric.py
1171 lines (963 loc) · 42.1 KB
/
mh_metric.py
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
#!/usr/bin/env python3
##############################################################################
## ##
## MATLAB Independent, Small & Safe, High Integrity Tools ##
## ##
## Copyright (C) 2020-2022, Florian Schanda ##
## Copyright (C) 2020, Veoneer System Software GmbH ##
## ##
## This file is part of MISS_HIT. ##
## ##
## MATLAB Independent, Small & Safe, High Integrity Tools (MISS_HIT) is ##
## free software: you can redistribute it and/or modify it under the ##
## terms of the GNU General Public License as published by the Free ##
## Software Foundation, either version 3 of the License, or (at your ##
## option) any later version. ##
## ##
## MISS_HIT 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 ##
## GNU General Public License for more details. ##
## ##
## You should have received a copy of the GNU General Public License ##
## along with MISS_HIT. If not, see <http://www.gnu.org/licenses/>. ##
## ##
##############################################################################
# This is a code metric tool. It will implement popular and common
# metrics like path count or cyclomatic complexity.
import os
import sys
import html
import functools
import json
from miss_hit_core import command_line
from miss_hit_core import work_package
from miss_hit_core import config
from miss_hit_core.resources import PORTABLE_RES_URL, RES_URL
from miss_hit_core.errors import Error, ICE, Message_Handler
from miss_hit_core.m_ast import *
from miss_hit_core.m_lexer import MATLAB_Lexer
from miss_hit_core.m_parser import MATLAB_Parser
MEASURE = {m : None for m in config.METRICS}
def measures(metric_name):
assert isinstance(metric_name, str)
assert metric_name in config.METRICS
def decorator(func):
MEASURE[metric_name] = func
return func
return decorator
##############################################################################
# Metrics
##############################################################################
@measures("npath")
def npath(node):
assert isinstance(node, (Sequence_Of_Statements,
Statement,
Pragma,
Function_Definition,
Script_File))
if isinstance(node, Function_Definition):
return npath(node.n_body)
elif isinstance(node, Script_File):
return npath(node.n_statements)
elif isinstance(node, Sequence_Of_Statements):
paths = 1
for n_statement in node.l_statements:
if isinstance(n_statement, (If_Statement,
For_Loop_Statement,
Switch_Statement,
Try_Statement,
While_Statement)):
paths *= npath(n_statement)
return paths
elif isinstance(node, If_Statement):
paths = 0
for n_action in node.l_actions:
paths += npath(n_action.n_body)
if not node.has_else:
paths += 1
return paths
elif isinstance(node, Switch_Statement):
paths = 0
for n_action in node.l_actions:
paths += npath(n_action.n_body)
if not node.has_otherwise:
paths += 1
return paths
elif isinstance(node, (For_Loop_Statement, While_Statement)):
return 1 + npath(node.n_body)
elif isinstance(node, Try_Statement):
return npath(node.n_body) * 2
else:
raise ICE("unexpected node %s" % node.__class__.__name__)
@measures("cnest")
def cnest(node):
assert isinstance(node, (Sequence_Of_Statements,
Statement,
Pragma,
Function_Definition,
Script_File))
if isinstance(node, Function_Definition):
return cnest(node.n_body)
elif isinstance(node, Script_File):
return cnest(node.n_statements)
elif isinstance(node, Sequence_Of_Statements):
return max(map(cnest, node.l_statements),
default=0)
elif isinstance(node, (Simple_Statement,
Pragma)):
return 0
elif isinstance(node, SPMD_Statement):
return cnest(node.n_body)
elif isinstance(node, If_Statement):
return 1 + max((cnest(a.n_body) for a in node.l_actions),
default=0)
elif isinstance(node, Switch_Statement):
return 1 + max((cnest(a.n_body) for a in node.l_actions),
default=0)
elif isinstance(node, (For_Loop_Statement, While_Statement)):
return 1 + cnest(node.n_body)
elif isinstance(node, Try_Statement):
if node.n_handler:
return 1 + max(cnest(node.n_body),
cnest(node.n_handler))
else:
return 1 + cnest(node.n_body)
else:
raise ICE("unexpected node %s" % node.__class__.__name__)
@measures("parameters")
def parameters(node):
assert isinstance(node, (Function_Definition,
Script_File))
if isinstance(node, Function_Definition):
return len(node.n_sig.l_inputs) + len(node.n_sig.l_outputs)
else:
return 0
@measures("globals")
def direct_globals(node):
assert isinstance(node, (Function_Definition,
Script_File))
class Global_Visitor(AST_Visitor):
def __init__(self):
self.names = set()
def visit(self, node, n_parent, relation):
if isinstance(node, Global_Statement):
self.names |= set(n_ident.t_ident.value
for n_ident in node.l_names)
gvis = Global_Visitor()
if isinstance(node, Function_Definition):
node.n_body.visit(None, gvis, "Root")
else:
node.n_statements.visit(None, gvis, "Root")
return len(gvis.names)
@measures("persistent")
def persistent_variables(node):
assert isinstance(node, (Function_Definition,
Script_File))
class Persistent_Visitor(AST_Visitor):
def __init__(self):
self.names = set()
def visit(self, node, n_parent, relation):
if isinstance(node, Persistent_Statement):
self.names |= set(n_ident.t_ident.value
for n_ident in node.l_names)
pvis = Persistent_Visitor()
if isinstance(node, Function_Definition):
node.n_body.visit(None, pvis, "Root")
else:
node.n_statements.visit(None, pvis, "Root")
return len(pvis.names)
@measures("function_length")
def function_length(node):
assert isinstance(node, (Function_Definition,
Script_File))
if isinstance(node, Script_File):
return None
elif node.t_end:
return node.t_end.location.line - node.t_fun.location.line + 1
else:
# If a function does not have an end, its length is from this
# function up to and including the next function or the end of
# the file
n_cu = node.n_parent
if not isinstance(n_cu, Function_File):
raise ICE("unterminated function must be a child of a "
"function file")
this_function_idx = n_cu.l_functions.index(node)
next_function_idx = this_function_idx + 1
if next_function_idx < len(n_cu.l_functions):
# We have a function following this one
return (n_cu.l_functions[next_function_idx].t_fun.location.line -
node.t_fun.location.line)
else:
# This is the last function in the file
return n_cu.file_length - node.t_fun.location.line + 1
@measures("cyc")
def cyclomatic_complexity(node):
assert isinstance(node, (Function_Definition,
Script_File))
# See
# https://uk.mathworks.com/help/matlab/ref/logicaloperatorsshortcircuit.html
# for short-circuit semantics
class Cyclomatic_Complexity_Visitor(AST_Visitor):
def __init__(self):
self.metric = 1
def visit(self, node, n_parent, relation):
if isinstance(node, Binary_Logical_Operation):
if node.short_circuit:
self.metric += 1
elif isinstance(node, (For_Loop_Statement,
While_Statement,
Try_Statement)):
self.metric += 1
elif isinstance(node, If_Statement):
if node.has_else:
self.metric += len(node.l_actions) - 1
else:
self.metric += len(node.l_actions)
elif isinstance(node, Switch_Statement):
if node.has_otherwise:
self.metric += len(node.l_actions) - 1
else:
self.metric += len(node.l_actions)
cvis = Cyclomatic_Complexity_Visitor()
if isinstance(node, Function_Definition):
node.n_body.visit(None, cvis, "Root")
else:
node.n_statements.visit(None, cvis, "Root")
return cvis.metric
##############################################################################
# Infrastructure
##############################################################################
def check_metric(mh, cfg, loc, metric, metrics, justifications):
if not cfg.metric_enabled(metric):
return
elif cfg.metric_check(metric):
measure = metrics[metric]["measure"]
if measure is None:
return
limit = cfg.metric_upper_limit(metric)
metrics[metric]["limit"] = limit
if measure > limit:
if metric in justifications:
mh.metric_justifications += 1
justifications[metric].applies = True
metrics[metric]["reason"] = justifications[metric].reason()
metrics[metric]["tickets"] = justifications[metric].tickets
else:
mh.metric_issue(loc,
"exceeded %s: measured %u > limit %u" %
(config.METRICS[metric].longname.lower(),
measure,
limit),
metric)
def get_justifications(mh, n_root):
assert isinstance(mh, Message_Handler)
assert isinstance(n_root, Sequence_Of_Statements)
justifications = {}
for n_statement in n_root.l_statements:
if isinstance(n_statement, Metric_Justification_Pragma):
if n_statement.ignored:
pass
elif n_statement.metric() in justifications:
mh.warning(n_statement.loc(),
"duplicate justification for %s" %
n_statement.metric)
else:
justifications[n_statement.metric()] = n_statement
return justifications
def get_file_justifications(mh, n_cu):
assert isinstance(mh, Message_Handler)
assert isinstance(n_cu, Compilation_Unit)
justifications = {}
if isinstance(n_cu, Script_File):
# Pragmas are in the top statement list
for n_statement in n_cu.n_statements.l_statements:
if isinstance(n_statement, Metric_Justification_Pragma):
if n_statement.metric() in justifications:
mh.warning(n_statement.loc(),
"duplicate justification for %s" %
n_statement.metric())
else:
justifications[n_statement.metric()] = n_statement
else:
# Pragmas are in the dedicated file pragma list
for n_pragma in n_cu.l_pragmas:
if isinstance(n_pragma, Metric_Justification_Pragma):
if n_pragma.metric() in justifications:
mh.warning(n_pragma.loc(),
"duplicate justification for %s" %
n_pragma.metric())
else:
justifications[n_pragma.metric()] = n_pragma
return justifications
def get_function_metrics(mh, cfg, tree):
assert isinstance(mh, Message_Handler)
assert isinstance(cfg, config.Config)
assert isinstance(tree, Compilation_Unit)
metrics = {}
justifications = {}
class Function_Visitor(AST_Visitor):
def visit(self, node, n_parent, relation):
if isinstance(node, Function_Definition):
name = node.get_local_name()
n_body = node.n_body
elif isinstance(node, Script_File):
name = node.get_local_name()
n_body = node.n_statements
else:
return
metrics[name] = {m: {"measure" : MEASURE[m](node),
"limit" : None,
"reason" : None,
"tickets" : set()}
for m in config.FUNCTION_METRICS
if cfg.metric_enabled(m)}
justifications[name] = get_justifications(mh, n_body)
# Check+justify function metrics
for function_metric in config.FUNCTION_METRICS:
check_metric(mh, cfg, node.loc(), function_metric,
metrics[name],
justifications[name])
tree.visit(None, Function_Visitor(), "Root")
return metrics
def warn_unused_justifications(mh, n_cu):
assert isinstance(mh, Message_Handler)
assert isinstance(n_cu, Compilation_Unit)
class Justification_Visitor(AST_Visitor):
def __init__(self):
self.name_stack = []
def visit(self, node, n_parent, relation):
if isinstance(node, Metric_Justification_Pragma):
if not node.applies:
mh.warning(node.loc(),
"this justification does not apply to anything")
n_cu.visit(None, Justification_Visitor(), "Root")
def write_text_report(fd,
all_metrics,
ticket_summary,
worst_offenders):
first = True
fd.write("=== Code metric by file:\n\n")
for filename in sorted(all_metrics):
metrics = all_metrics[filename]
if first:
first = False
else:
fd.write("\n")
fd.write("* Code metrics for file %s:\n" % filename)
if metrics["errors"]:
fd.write(" Contains syntax or semantics errors,\n")
fd.write(" no metrics collected.\n")
continue
for file_metric in config.FILE_METRICS:
if file_metric not in metrics["metrics"]:
continue
results = metrics["metrics"][file_metric]
if results["measure"] is None:
continue
fd.write(" %s: %u" % (config.METRICS[file_metric].longname,
results["measure"]))
if results["reason"]:
fd.write(" (%s)\n" % results["reason"])
elif results["limit"] and results["measure"] > results["limit"]:
fd.write(" (!not justified!)\n")
else:
fd.write("\n")
for function in sorted(metrics["functions"]):
fd.write("\n")
fd.write(" Code metrics for function %s:\n" % function)
max_len = max((len(config.METRICS[m].longname)
for m in metrics["functions"][function]),
default=0)
for function_metric in config.FUNCTION_METRICS:
if function_metric not in metrics["functions"][function]:
continue
results = metrics["functions"][function][function_metric]
if results["measure"] is None:
continue
fd.write(" %-*s: %u" %
(max_len,
config.METRICS[function_metric].longname,
results["measure"]))
if results["reason"]:
fd.write(" (%s)\n" % results["reason"])
elif results["limit"] and \
results["measure"] > results["limit"]:
fd.write(" (!not justified!)\n")
else:
fd.write("\n")
if worst_offenders:
fd.write("\n=== Global summary of worst offenders by metric:\n\n")
for file_metric in config.FILE_METRICS:
if file_metric not in worst_offenders:
continue
fd.write("* File metric '%s':\n" %
config.METRICS[file_metric].longname)
for rank, file_name in enumerate(worst_offenders[file_metric], 1):
if file_name:
mdata = all_metrics[file_name]["metrics"][file_metric]
fd.write(" %u. %u (%s)\n" % (rank,
mdata["measure"],
file_name))
fd.write("\n")
for function_metric in config.FUNCTION_METRICS:
if function_metric not in worst_offenders:
continue
fd.write("* Function metric '%s':\n" %
config.METRICS[function_metric].longname)
for rank, tup in enumerate(worst_offenders[function_metric], 1):
if tup:
file_name, function_name = tup
mdata = (all_metrics[file_name]["functions"]
[function_name][function_metric])
fd.write(" %u. %u (%s, function %s)\n" %
(rank,
mdata["measure"],
file_name,
function_name))
fd.write("\n")
if ticket_summary:
fd.write("\n=== Summary of tickets mentioned in justifications:\n")
for ticket_id in sorted(ticket_summary):
if ticket_summary[ticket_id] > 1:
fd.write(" %s: referenced %u times\n" %
(ticket_id,
ticket_summary[ticket_id]))
else:
assert ticket_summary[ticket_id] == 1
fd.write(" %s: referenced once\n" % ticket_id)
def write_html_report(fd,
entry_point,
portable_html,
all_metrics,
ticket_summary,
worst_offenders):
base_url = PORTABLE_RES_URL if portable_html else RES_URL
if entry_point:
report_title = "MISS_HIT Report for %s" % entry_point
else:
report_title = "MISS_HIT Report"
fd.write("<!DOCTYPE html>\n")
fd.write("<html>\n")
fd.write("<head>\n")
fd.write("<meta charset=\"UTF-8\">\n")
fd.write("<link rel=\"stylesheet\" href=\"%s/style.css\">\n" %
base_url)
fd.write("<title>%s</title>\n" % report_title)
fd.write("</head>\n")
fd.write("<body>\n")
fd.write("<header>%s</header>\n" % report_title)
fd.write("<main>\n")
fd.write("<div></div>\n")
# Produce worst-offender table
if worst_offenders:
fd.write("<div class='title'>\n")
fd.write("<img src='%s/assets/alert-triangle.svg' alt='Warning'>\n" %
base_url)
fd.write("<h1>Worst offenders</h1>\n")
fd.write("</div>\n")
fd.write("<section>\n")
fd.write("<div class='metrics'>\n")
fd.write("<table>\n")
fd.write("<thead>\n")
fd.write("<tr>\n")
fd.write(" <td>Rank</td>\n")
for file_metric in config.FILE_METRICS:
if file_metric in worst_offenders:
fd.write(" <td class='tip' tip='%s'>%s</td>\n" %
(config.METRICS[file_metric].description,
config.METRICS[file_metric].longname))
for function_metric in config.FUNCTION_METRICS:
if function_metric in worst_offenders:
fd.write(" <td class='tip' tip='%s'>%s</td>\n" %
(config.METRICS[function_metric].description,
config.METRICS[function_metric].longname))
fd.write("</tr>\n")
fd.write("</thead>\n")
fd.write("<tbody>\n")
count = worst_offender_count(worst_offenders)
for rank in range(count):
fd.write("<tr>\n")
fd.write(" <td>%s</td>\n" % (rank + 1))
for file_metric in config.FILE_METRICS:
if file_metric not in worst_offenders:
continue
file_name = worst_offenders[file_metric][rank]
if file_name:
mdata = all_metrics[file_name]["metrics"][file_metric]
fd.write(" <td class='tip' tip='%s'>"
"<a href='#%s'>%u</a></td>\n" %
(os.path.basename(file_name),
file_name,
mdata["measure"]))
else:
fd.write(" <td class='na'></td>\n")
for function_metric in config.FUNCTION_METRICS:
if function_metric not in worst_offenders:
continue
metric = worst_offenders[function_metric][rank]
if metric:
file_name, function_name = metric
mdata = (all_metrics[file_name]["functions"]
[function_name][function_metric])
fd.write(" <td class='tip' tip='%s'>"
"<a href='#%s'>%u</a></td>\n" %
("%s in file %s" % (function_name,
os.path.basename(file_name)),
file_name,
mdata["measure"]))
else:
fd.write(" <td class='na'></td>\n")
fd.write("</tr>\n")
fd.write("</tbody>\n")
fd.write("</table>\n")
fd.write("</div>\n")
fd.write("</section>\n")
# Produce ticket overview
if ticket_summary:
fd.write("<div class='title'>\n")
fd.write("<img src='%s/assets/external-link.svg' alt='Tickets'>\n" %
base_url)
fd.write("<h1>Tickets referenced in justifications</h1>\n")
fd.write("</div>\n")
fd.write("<section>\n")
fd.write("<div>\n")
fd.write("<ul>\n")
for ticket_id in sorted(ticket_summary):
if ticket_summary[ticket_id] > 1:
fd.write("<li>%s: referenced %u times</li>" %
(ticket_id,
ticket_summary[ticket_id]))
else:
assert ticket_summary[ticket_id] == 1
fd.write("<li>%s: referenced once</li>\n" % ticket_id)
fd.write("</ul>\n")
fd.write("</div>\n")
fd.write("</section>\n")
# Produce full list of metrics
fd.write("<div class='title'>\n")
fd.write("<img src='%s/assets/bar-chart-2.svg' alt='Warning'>\n" %
base_url)
fd.write("<h1>Code metrics by file</h1>\n")
fd.write("</div>\n")
fd.write("<section>\n")
for filename in sorted(all_metrics):
metrics = all_metrics[filename]
n_active_file_metrics = len(set(config.FILE_METRICS) -
metrics["disabled"])
n_active_function_metrics = len(set(config.FUNCTION_METRICS) -
metrics["disabled"])
fd.write("<div class='metrics'>\n")
fd.write("<h2><a name='%s'>%s</a></h2>\n" % (filename,
filename))
if metrics["errors"]:
fd.write("<div>")
fd.write(" File contains syntax or semantics errors,")
fd.write(" no metrics collected.\n")
fd.write("</div>")
continue
fd.write("<table>\n")
fd.write("<thead>\n")
fd.write("<tr>\n")
fd.write(" <td>Item</td>\n")
for file_metric in config.FILE_METRICS:
if file_metric in metrics["disabled"]:
continue
fd.write(" <td class='tip' tip='%s'>%s</td>\n" %
(config.METRICS[file_metric].description,
config.METRICS[file_metric].longname))
for function_metric in config.FUNCTION_METRICS:
if function_metric in metrics["disabled"]:
continue
fd.write(" <td class='tip' tip='%s'>%s</td>\n" %
(config.METRICS[function_metric].description,
config.METRICS[function_metric].longname))
fd.write("</tr>\n")
fd.write("</thead>\n")
fd.write("<tbody>\n")
fd.write("<tr>\n")
fd.write(" <td>%s</td>\n" % os.path.basename(filename))
for file_metric in config.FILE_METRICS:
if file_metric in metrics["disabled"]:
continue
results = metrics["metrics"][file_metric]
if results["measure"] is None:
fd.write(" <td class='na'></td>\n")
elif results["reason"]:
fd.write(" <td class='ok_justified tip' tip='%s'>%u</td>\n" %
("Justification: " + html.escape(results["reason"]),
results["measure"]))
elif results["limit"] and results["measure"] > results["limit"]:
fd.write(" <td class='nok'>%u</td>\n" %
results["measure"])
else:
fd.write("<td class='ok'>%u</td>" % results["measure"])
fd.write(" <td class='na'></td>\n" * n_active_function_metrics)
fd.write("</tr>\n")
for function in sorted(metrics["functions"]):
fd.write("<tr>\n")
fd.write(" <td><a name='%s'></a>%s</td>\n" % (function,
function))
fd.write(" <td class='na'></td>\n" * n_active_file_metrics)
for function_metric in config.FUNCTION_METRICS:
if function_metric in metrics["disabled"]:
continue
results = metrics["functions"][function][function_metric]
if results["measure"] is None:
fd.write(" <td class='na'></td>\n")
elif results["reason"]:
fd.write(" <td class='ok_justified tip' tip='%s'>"
"%u</td>\n" %
("Justification: " +
html.escape(results["reason"]),
results["measure"]))
elif results["limit"] and \
results["measure"] > results["limit"]:
fd.write(" <td class='nok'>%u</td>\n" %
results["measure"])
else:
fd.write(" <td class='ok'>%u</td>\n" % results["measure"])
fd.write("</tr>\n")
fd.write("</tbody>\n")
fd.write("</table>\n")
fd.write("</div>\n")
fd.write("</section>\n")
fd.write("</main>\n")
fd.write("<footer>\n")
fd.write("MISS_HIT is licensed under the GPLv3\n")
fd.write("</footer>\n")
fd.write("</body>\n")
fd.write("</html>\n")
def build_json_report(all_metrics, worst_offenders):
rv = {"metrics" : {},
"worst_case" : {}}
def format_metrics_result(mres):
if mres["limit"] is None:
return {"status" : "measured only",
"measure" : mres["measure"]}
elif mres["measure"] <= mres["limit"]:
return {"status" : "checked: ok",
"measure" : mres["measure"],
"limit" : mres["limit"]}
elif mres["reason"]:
return {"status" : "checked: justified",
"measure" : mres["measure"],
"limit" : mres["limit"],
"justification" : mres["reason"]}
else:
return {"status" : "checked: fail",
"measure" : mres["measure"],
"limit" : mres["limit"]}
def format_ml(mlst):
return {name: format_metrics_result(mlst[name])
for name in mlst}
for filename in all_metrics:
if all_metrics[filename]["errors"]:
continue
rv["metrics"][filename] = {
"file_metrics" : format_ml(all_metrics[filename]["metrics"]),
"function_metrics" : {
fn: format_ml(all_metrics[filename]["functions"][fn])
for fn in all_metrics[filename]["functions"]
},
}
if worst_offenders:
for file_metric in config.FILE_METRICS:
if file_metric not in worst_offenders:
continue
rv["worst_case"][file_metric] = []
for file_name in worst_offenders[file_metric]:
if not file_name:
break
mdata = all_metrics[file_name]["metrics"][file_metric]
tmp = format_metrics_result(mdata)
tmp["file"] = file_name
rv["worst_case"][file_metric].append(tmp)
for function_metric in config.FUNCTION_METRICS:
if function_metric not in worst_offenders:
continue
rv["worst_case"][function_metric] = []
for tup in worst_offenders[function_metric]:
if not tup:
break
file_name, function_name = tup
mdata = (all_metrics[file_name]["functions"]
[function_name][function_metric])
tmp = format_metrics_result(mdata)
tmp["file"] = file_name
tmp["function"] = function_name
rv["worst_case"][function_metric].append(tmp)
return rv
def worst_offender_count(worst_offenders):
for metric in worst_offenders:
return len(worst_offenders[metric])
raise ICE("cannot determine length of wo table")
def build_worst_offenders_table(all_metrics, count):
assert isinstance(count, int) and count >= 1
# all_metrics = {filename -> {errors : bool
# functions : {fn_name -> MD}
# metrics : MD}}
# MD = {m_name -> {measure : INT
# limit : INT
# reason : STR}}
wot = {}
# file_metric -> [ filename, ... ]
# fn_metric -> [ (filename, fn_name), ... ]
def key_file_metric(file_name, metric):
return (all_metrics[file_name]["metrics"][metric]["measure"],
file_name)
def key_function_metric(name_tuple, metric):
file_name, function_name = name_tuple
metrics = all_metrics[file_name]["functions"][function_name]
return (metrics[metric]["measure"],
file_name,
function_name)
for file_metric in config.FILE_METRICS:
wot[file_metric] = []
for file_name in all_metrics:
metrics = all_metrics[file_name]
if metrics["errors"]:
continue
elif file_metric not in metrics["metrics"]:
continue
elif not metrics["metrics"][file_metric]["measure"]:
continue
wot[file_metric].append(file_name)
key_fn = functools.partial(key_file_metric,
metric=file_metric)
wot[file_metric].sort(key=key_fn, reverse=True)
for function_metric in config.FUNCTION_METRICS:
wot[function_metric] = []
for file_name in all_metrics:
metrics = all_metrics[file_name]
if metrics["errors"]:
continue
for function_name in metrics["functions"]:
function_metrics = metrics["functions"][function_name]
if function_metric not in function_metrics:
continue
elif not function_metrics[function_metric]["measure"]:
continue
wot[function_metric].append((file_name, function_name))
key_fn = functools.partial(key_function_metric,
metric=function_metric)
wot[function_metric].sort(key=key_fn, reverse=True)
# Make sure the length is as expected for each metric
for metric in config.METRICS:
if len(wot[metric]) == 0:
del wot[metric]
elif len(wot[metric]) < count:
wot[metric] += [None] * (count - len(wot[metric]))
elif len(wot[metric]) > count:
wot[metric] = wot[metric][:count]
assert metric not in wot or len(wot[metric]) == count
return wot
def build_ticket_summary(all_metrics):
ticket_map = {}
for filename in all_metrics:
metrics = all_metrics[filename]
for metric_name in metrics["metrics"]:
info = metrics["metrics"][metric_name]
for ticket_id in info["tickets"]:
if ticket_id not in ticket_map:
ticket_map[ticket_id] = 1
else:
ticket_map[ticket_id] += 1
for function_name in metrics["functions"]:
for metric_name in metrics["functions"][function_name]:
info = metrics["functions"][function_name][metric_name]
for ticket_id in info["tickets"]:
if ticket_id not in ticket_map:
ticket_map[ticket_id] = 1
else:
ticket_map[ticket_id] += 1
return ticket_map
class MH_Metric_Result(work_package.Result):
def __init__(self, wp, metrics):
super().__init__(wp, True)
self.metrics = metrics
class MH_Metric(command_line.MISS_HIT_Back_End):
def __init__(self, options):
super().__init__("MH Metric")
self.options = options
self.metrics = {}
# file -> { metrics -> {}
# functions -> {name -> {}} }
@classmethod
def process_wp(cls, wp):
if wp.blockname is None:
full_name = wp.filename
else:
full_name = wp.filename + "/" + wp.blockname
metrics = {
full_name: {
"errors" : False,
"metrics" : {},
"functions" : {},
"disabled" : set(m for m in config.METRICS
if not wp.cfg.metric_enabled(m))
}
}
justifications = {}
# Create lexer
lexer = MATLAB_Lexer(wp.cfg.language,
wp.mh,
wp.get_content(),
wp.filename,
wp.blockname)
if not wp.cfg.pragmas:
lexer.process_pragmas = False
# We're dealing with an empty file here. Lets just not do anything
if len(lexer.text.strip()) == 0:
return MH_Metric_Result(wp, metrics)
# Create parse tree
try:
parser = MATLAB_Parser(wp.mh, lexer, wp.cfg)
parse_tree = parser.parse_file()
except Error:
metrics[wp.filename]["errors"] = True
return MH_Metric_Result(wp, metrics)
# Collect file metrics
if wp.cfg.metric_enabled("file_length"):
metrics[full_name]["metrics"]["file_length"] = {
"measure" : lexer.line_count(),
"limit" : None,
"reason" : None,
"tickets" : set(),
}