2ea642d3f31620f9ef5bdb75bf016966783f8c71
[nit.git] / src / metrics / nullables_metrics.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012 Jean Privat <jean@pryen.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Statistics about the usage of nullables
18 module nullables_metrics
19
20 import modelbuilder
21 private import typing
22 private import metrics_base
23 import frontend
24
25 redef class ToolContext
26 var nullables_metrics_phase = new NullablesMetricsPhase(self, null)
27 end
28
29 private class NullablesMetricsPhase
30 super Phase
31 redef fun process_mainmodule(mainmodule, given_mmodules)
32 do
33 if not toolcontext.opt_nullables.value and not toolcontext.opt_all.value then return
34 compute_nullables_metrics(toolcontext.modelbuilder)
35 end
36 end
37
38 private class NullableSends
39 super Visitor
40 var modelbuilder: ModelBuilder
41 var nclassdef: AClassdef
42
43 var total_sends: Int = 0
44 var nullable_sends: Int = 0
45 var buggy_sends: Int = 0
46
47 # Get a new visitor on a classef to add type count in `typecount`.
48 init(modelbuilder: ModelBuilder, nclassdef: AClassdef)
49 do
50 self.modelbuilder = modelbuilder
51 self.nclassdef = nclassdef
52 end
53
54 redef fun visit(n)
55 do
56 n.visit_all(self)
57 if n isa ASendExpr then
58 self.total_sends += 1
59 var t = n.n_expr.mtype
60 if t == null then
61 self.buggy_sends += 1
62 return
63 end
64 t = t.anchor_to(self.nclassdef.mclassdef.mmodule, self.nclassdef.mclassdef.bound_mtype)
65 if t isa MNullableType then
66 self.nullable_sends += 1
67 else if t isa MClassType then
68 # Nothing
69 else
70 n.debug("Problem: strange receiver type found: {t} ({t.class_name})")
71 end
72 end
73 end
74 end
75
76 # Visit the AST and print metrics about the usage of send on nullable reciever.
77 fun compute_nullables_metrics(modelbuilder: ModelBuilder)
78 do
79 print "--- Sends on Nullable Receiver ---"
80 var total_sends = 0
81 var nullable_sends = 0
82 var buggy_sends = 0
83
84 # Visit all the source code to collect data
85 for nmodule in modelbuilder.nmodules do
86 for nclassdef in nmodule.n_classdefs do
87 var visitor = new NullableSends(modelbuilder, nclassdef)
88 visitor.enter_visit(nclassdef)
89 total_sends += visitor.total_sends
90 nullable_sends += visitor.nullable_sends
91 buggy_sends += visitor.buggy_sends
92 end
93 end
94 print "Total number of sends: {total_sends}"
95 print "Number of sends on a nullable receiver: {nullable_sends} ({div(nullable_sends*100,total_sends)}%)"
96 print "Number of buggy sends (cannot determine the type of the receiver): {buggy_sends} ({div(buggy_sends*100,total_sends)}%)"
97 end