5e468d0282267cbb1a9d9f9a83582a7c46df9134
1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 Jean Privat <jean@pryen.org>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # Statistics about the usage of nullables
18 module visit_nullable_sends
22 private import stats_base
24 private class NullableSends
26 var modelbuilder
: ModelBuilder
27 var nclassdef
: AClassdef
29 var total_sends
: Int = 0
30 var nullable_sends
: Int = 0
31 var buggy_sends
: Int = 0
33 # Get a new visitor on a classef to add type count in `typecount'.
34 init(modelbuilder
: ModelBuilder, nclassdef
: AClassdef)
36 self.modelbuilder
= modelbuilder
37 self.nclassdef
= nclassdef
43 if n
isa ASendExpr then
45 var t
= n
.n_expr
.mtype
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
56 n
.debug
("Problem: strange receiver type found: {t} ({t.class_name})")
62 # Visit the AST and print statistics about the usage of send on nullable reciever.
63 fun visit_nullable_sends
(modelbuilder
: ModelBuilder)
65 print
"--- Sends on Nullable Reciever ---"
67 var nullable_sends
= 0
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
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)}%)"