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 # Metrics on the usage of explicit static types.
18 module static_types_metrics
20 private import metrics_base
25 redef class ToolContext
26 var static_types_metrics_phase
: Phase = new StaticTypesMetricsPhase(self, null)
29 private class StaticTypesMetricsPhase
31 redef fun process_mainmodule
(mainmodule
, given_mmodules
)
33 if not toolcontext
.opt_static_types
.value
and not toolcontext
.opt_all
.value
then return
34 compute_static_types_metrics
(toolcontext
.modelbuilder
)
38 # The job of this visitor is to resolve all types found
39 private class ATypeCounterVisitor
41 var modelbuilder
: ModelBuilder
42 var nclassdef
: AClassdef
44 var typecount
: Counter[MType]
46 # Get a new visitor on a classef to add type count in `typecount`.
47 init(modelbuilder
: ModelBuilder, nclassdef
: AClassdef, typecount
: Counter[MType])
49 self.modelbuilder
= modelbuilder
50 self.nclassdef
= nclassdef
51 self.typecount
= typecount
57 var mclassdef
= self.nclassdef
.mclassdef
58 var mtype
= modelbuilder
.resolve_mtype
(mclassdef
.mmodule
, mclassdef
, n
)
60 self.typecount
.inc
(mtype
)
67 # Visit the AST and print metrics on the usage of explicit static types.
68 fun compute_static_types_metrics
(modelbuilder
: ModelBuilder)
70 # Count each occurence of a specific static type
71 var typecount
= new Counter[MType]
73 # Visit all the source code to collect data
74 for nmodule
in modelbuilder
.nmodules
do
75 for nclassdef
in nmodule
.n_classdefs
do
76 var visitor
= new ATypeCounterVisitor(modelbuilder
, nclassdef
, typecount
)
77 visitor
.enter_visit
(nclassdef
)
82 print
"--- Metrics of the explitic static types ---"
83 print
"Total number of explicit static types: {typecount.sum}"
84 if typecount
.sum
== 0 then return
86 print
"Statistics of type usage:"
87 typecount
.print_summary
88 typecount
.print_elements
(10)