nitstats: renamed in nitmetrics
[nit.git] / src / metrics / visit_nullable_sends.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 visit_nullable_sends
19
20 import modelbuilder
21 private import typing
22 private import metrics_base
23
24 private class NullableSends
25 super Visitor
26 var modelbuilder: ModelBuilder
27 var nclassdef: AClassdef
28
29 var total_sends: Int = 0
30 var nullable_sends: Int = 0
31 var buggy_sends: Int = 0
32
33 # Get a new visitor on a classef to add type count in `typecount'.
34 init(modelbuilder: ModelBuilder, nclassdef: AClassdef)
35 do
36 self.modelbuilder = modelbuilder
37 self.nclassdef = nclassdef
38 end
39
40 redef fun visit(n)
41 do
42 n.visit_all(self)
43 if n isa ASendExpr then
44 self.total_sends += 1
45 var t = n.n_expr.mtype
46 if t == null then
47 self.buggy_sends += 1
48 return
49 end
50 t = t.anchor_to(self.nclassdef.mclassdef.mmodule, self.nclassdef.mclassdef.bound_mtype)
51 if t isa MNullableType then
52 self.nullable_sends += 1
53 else if t isa MClassType then
54 # Nothing
55 else
56 n.debug("Problem: strange receiver type found: {t} ({t.class_name})")
57 end
58 end
59 end
60 end
61
62 # Visit the AST and print statistics about the usage of send on nullable reciever.
63 fun visit_nullable_sends(modelbuilder: ModelBuilder)
64 do
65 print "--- Sends on Nullable Reciever ---"
66 var total_sends = 0
67 var nullable_sends = 0
68 var buggy_sends = 0
69
70 # Visit all the source code to collect data
71 for nmodule in modelbuilder.nmodules do
72 for nclassdef in nmodule.n_classdefs do
73 var visitor = new NullableSends(modelbuilder, nclassdef)
74 visitor.enter_visit(nclassdef)
75 total_sends += visitor.total_sends
76 nullable_sends += visitor.nullable_sends
77 buggy_sends += visitor.buggy_sends
78 end
79 end
80 print "Total number of sends: {total_sends}"
81 print "Number of sends on a nullable receiver: {nullable_sends} ({div(nullable_sends*100,total_sends)}%)"
82 print "Number of buggy sends (cannot determine the type of the receiver): {buggy_sends} ({div(buggy_sends*100,total_sends)}%)"
83 end