Merge: doc: fixed some typos and other misc. corrections
[nit.git] / src / metrics / nullables_metrics.nit
index 2c96462..0cae25d 100644 (file)
@@ -22,6 +22,8 @@ import mclasses_metrics
 import semantize
 
 redef class ToolContext
+
+       # Nullable types related metrics
        var nullables_metrics_phase: Phase = new NullablesMetricsPhase(self, null)
 end
 
@@ -37,18 +39,19 @@ private class NullablesMetricsPhase
 
                print toolcontext.format_h1("\n# Nullable metrics")
 
+               var model = toolcontext.modelbuilder.model
+               var filter = new ModelFilter(private_visibility)
+
                var metrics = new MetricSet
-               var min_vis = private_visibility
-               metrics.register(new CNBA(mainmodule, min_vis))
-               metrics.register(new CNBNA(mainmodule, min_vis))
+               metrics.register(new CNBA(model, mainmodule, filter))
+               metrics.register(new CNBNA(model, mainmodule, filter))
 
-               var model = toolcontext.modelbuilder.model
                var mclasses = new HashSet[MClass]
-               for mproject in model.mprojects do
+               for mpackage in model.mpackages do
 
-                       print toolcontext.format_h2("\n ## project {mproject}")
+                       print toolcontext.format_h2("\n ## package {mpackage}")
 
-                       for mgroup in mproject.mgroups do
+                       for mgroup in mpackage.mgroups do
                                if mgroup.mmodules.is_empty then continue
                                metrics.clear
 
@@ -60,7 +63,7 @@ private class NullablesMetricsPhase
                                mclasses.add_all(mod_mclasses)
                                metrics.collect(new HashSet[MClass].from(mod_mclasses))
                                metrics.to_console(1, not toolcontext.opt_nocolors.value)
-                               if csv then metrics.to_csv.save("{out}/{mgroup}.csv")
+                               if csv then metrics.to_csv.write_to_file("{out}/{mgroup}.csv")
                        end
                end
                if not mclasses.is_empty then
@@ -69,7 +72,7 @@ private class NullablesMetricsPhase
                        print toolcontext.format_h2("\n ## global metrics")
                        metrics.collect(mclasses)
                        metrics.to_console(1, not toolcontext.opt_nocolors.value)
-                       if csv then metrics.to_csv.save("{out}/summary.csv")
+                       if csv then metrics.to_csv.write_to_file("{out}/summary.csv")
                end
 
                compute_nullables_metrics(toolcontext.modelbuilder)
@@ -83,17 +86,9 @@ class CNBNA
        redef fun name do return "cnbna"
        redef fun desc do return "number of accessible nullable attributes (inherited + local)"
 
-       var mainmodule: MModule
-       var min_visibility: MVisibility
-
-       init(mainmodule: MModule, min_visibility: MVisibility) do
-               self.mainmodule = mainmodule
-               self.min_visibility = min_visibility
-       end
-
        redef fun collect(mclasses) do
                for mclass in mclasses do
-                       var all = mclass.all_mattributes(mainmodule, min_visibility)
+                       var all = mclass.collect_accessible_mattributes(mainmodule, filter)
                        for mattr in all do
                                if mattr.is_nullable then values.inc(mclass)
                        end
@@ -101,10 +96,18 @@ class CNBNA
        end
 end
 
+redef class MAttribute
+       # Is this attribute nullable for sure?
+       #
+       # This mean that its introduction is declarred with a nullable static type
+       # since attributes are invariant this will work on most cases
+       # attributes with static type anchored with a virtual type are not "nullable for-sure"
+       # because this type can be redefined in subclasses
+       private fun is_nullable: Bool do return intro.static_mtype isa MNullableType
+end
 
 private class NullableSends
        super Visitor
-       var modelbuilder: ModelBuilder
        var nclassdef: AClassdef
 
        var total_sends: Int = 0
@@ -112,13 +115,6 @@ private class NullableSends
        var nullable_eq_sends: Int = 0
        var buggy_sends: Int = 0
 
-       # Get a new visitor on a classef to add type count in `typecount`.
-       init(modelbuilder: ModelBuilder, nclassdef: AClassdef)
-       do
-               self.modelbuilder = modelbuilder
-               self.nclassdef = nclassdef
-       end
-
        redef fun visit(n)
        do
                n.visit_all(self)
@@ -131,8 +127,8 @@ private class NullableSends
                        end
                        t = t.anchor_to(self.nclassdef.mclassdef.mmodule, self.nclassdef.mclassdef.bound_mtype)
                        if t isa MNullableType then
-                               var name = n.callsite.mproperty.name
-                               if name == "==" or name == "!=" or name == "is_same_instance" then
+                               var p = n.callsite.mproperty
+                               if p.is_null_safe then
                                        self.nullable_eq_sends += 1
                                else
                                        self.nullable_sends += 1
@@ -158,7 +154,7 @@ do
        # Visit all the source code to collect data
        for nmodule in modelbuilder.nmodules do
                for nclassdef in nmodule.n_classdefs do
-                       var visitor = new NullableSends(modelbuilder, nclassdef)
+                       var visitor = new NullableSends(nclassdef)
                        visitor.enter_visit(nclassdef)
                        total_sends += visitor.total_sends
                        nullable_sends += visitor.nullable_sends