metrics: factorize `mainmodule` and `model_view` attributes for all MClass metrics
[nit.git] / src / metrics / nullables_metrics.nit
index 895f7d5..abd8180 100644 (file)
 # Statistics about the usage of nullables
 module nullables_metrics
 
-import modelbuilder
-private import typing
-import model_utils
+import metrics_base
 import mclasses_metrics
-import frontend
+import semantize
 
 redef class ToolContext
        var nullables_metrics_phase: Phase = new NullablesMetricsPhase(self, null)
@@ -39,18 +37,19 @@ private class NullablesMetricsPhase
 
                print toolcontext.format_h1("\n# Nullable metrics")
 
+               var model = toolcontext.modelbuilder.model
+               var model_view = model.private_view
+
                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(mainmodule, model_view))
+               metrics.register(new CNBNA(mainmodule, model_view))
 
-               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
 
@@ -62,7 +61,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
@@ -71,7 +70,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)
@@ -86,16 +85,16 @@ class CNBNA
        redef fun desc do return "number of accessible nullable attributes (inherited + local)"
 
        var mainmodule: MModule
-       var min_visibility: MVisibility
+       var model_view: ModelView
 
-       init(mainmodule: MModule, min_visibility: MVisibility) do
+       init(mainmodule: MModule, model_view: ModelView) do
                self.mainmodule = mainmodule
-               self.min_visibility = min_visibility
+               self.model_view = model_view
        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(model_view)
                        for mattr in all do
                                if mattr.is_nullable then values.inc(mclass)
                        end
@@ -103,6 +102,15 @@ 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
@@ -111,6 +119,7 @@ private class NullableSends
 
        var total_sends: Int = 0
        var nullable_sends: Int = 0
+       var nullable_eq_sends: Int = 0
        var buggy_sends: Int = 0
 
        # Get a new visitor on a classef to add type count in `typecount`.
@@ -132,7 +141,12 @@ private class NullableSends
                        end
                        t = t.anchor_to(self.nclassdef.mclassdef.mmodule, self.nclassdef.mclassdef.bound_mtype)
                        if t isa MNullableType then
-                               self.nullable_sends += 1
+                               var p = n.callsite.mproperty
+                               if p.is_null_safe then
+                                       self.nullable_eq_sends += 1
+                               else
+                                       self.nullable_sends += 1
+                               end
                        else if t isa MClassType then
                                # Nothing
                        else
@@ -148,6 +162,7 @@ do
        print "--- Sends on Nullable Receiver ---"
        var total_sends = 0
        var nullable_sends = 0
+       var nullable_eq_sends = 0
        var buggy_sends = 0
 
        # Visit all the source code to collect data
@@ -157,10 +172,12 @@ do
                        visitor.enter_visit(nclassdef)
                        total_sends += visitor.total_sends
                        nullable_sends += visitor.nullable_sends
+                       nullable_eq_sends += visitor.nullable_eq_sends
                        buggy_sends += visitor.buggy_sends
                end
        end
        print "Total number of sends: {total_sends}"
-       print "Number of sends on a nullable receiver: {nullable_sends} ({div(nullable_sends*100,total_sends)}%)"
+       print "Number of sends on a unsafe nullable receiver: {nullable_sends} ({div(nullable_sends*100,total_sends)}%)"
+       print "Number of sends on a safe nullable receiver: {nullable_eq_sends} ({div(nullable_eq_sends*100,total_sends)}%)"
        print "Number of buggy sends (cannot determine the type of the receiver): {buggy_sends} ({div(buggy_sends*100,total_sends)}%)"
 end