1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 # This module call visitor for get number of line, total attributs call and total of self attributes call
16 module method_analyze_metrics
18 # We usualy need specific phases
19 # NOTE: `frontend` is sufficent in most case (it is often too much)
21 import mclasses_metrics
23 import mclassdef_collect
26 fun call_analyze_methods
(mclassdef
: MClassDef, model_builder
: ModelBuilder): Array[MMethodDef] do
27 var mmethoddefs
= new Array[MMethodDef]
28 for m_prop
in mclassdef
.collect_intro_and_redef_mpropdefs
(model_builder
.model
.private_view
) do
29 var n_prop
= model_builder
.mpropdef2node
(m_prop
)
30 #Check if the property is a method definition
31 if n_prop
isa AMethPropdef and m_prop
isa MMethodDef then
32 if n_prop
.n_methid
isa AIdMethid then
33 #Call visitor to analyse the method
34 var visitor
= new MethodAnalyzeMetrics(n_prop
)
35 visitor
.enter_visit
(n_prop
)
36 mmethoddefs
.add
(set_analyse_result_methoddef
(m_prop
,visitor
))
43 fun set_analyse_result_methoddef
(mmethoddef
: MMethodDef, visitor
: MethodAnalyzeMetrics): MMethodDef do
44 mmethoddef
.total_call
= visitor
.total_call
45 mmethoddef
.line_number
= visitor
.line_number
.length
46 mmethoddef
.total_self_call
= visitor
.total_self_call
47 mmethoddef
.total_extern_call
= visitor
.total_call
- visitor
.total_self_call
51 public
class MethodAnalyzeMetrics
53 var ameth_prop_def
: AMethPropdef
55 var line_number
= new Counter[nullable Int]
56 var total_self_call
= 0
61 if not n
isa ABlockExpr then
62 if n
.first_location
!= null then
63 line_number
.inc
(n
.first_location
.line_start
)
68 if n
isa ASendExpr then
69 var callsite
= n
.callsite
70 if callsite
!= null then
72 if callsite
.recv_is_self
== true then self.total_self_call
+= 1
78 redef class MMethodDef
81 var total_self_call
= 0
82 var total_extern_call
= 0