Statistics about the usage of nullables

Introduced classes

class CNBNA

nitc :: CNBNA

Class Metric: Number of nullables MAttributes
private class NullableSends

nitc :: NullableSends

Redefined classes

redef class MAttribute

nitc :: nullables_metrics $ MAttribute

A global attribute
redef class Sys

nitc :: nullables_metrics $ Sys

The main class of the program.
redef class ToolContext

nitc :: nullables_metrics $ ToolContext

Global context for tools

All class definitions

class CNBNA

nitc $ CNBNA

Class Metric: Number of nullables MAttributes
redef class MAttribute

nitc :: nullables_metrics $ MAttribute

A global attribute
private class NullableSends

nitc $ NullableSends

redef class Sys

nitc :: nullables_metrics $ Sys

The main class of the program.
redef class ToolContext

nitc :: nullables_metrics $ ToolContext

Global context for tools
package_diagram nitc::nullables_metrics nullables_metrics nitc::mclasses_metrics mclasses_metrics nitc::nullables_metrics->nitc::mclasses_metrics nitc\>semantize\> semantize nitc::nullables_metrics->nitc\>semantize\> nitc::metrics_base metrics_base nitc::mclasses_metrics->nitc::metrics_base nitc::model_collect model_collect nitc::mclasses_metrics->nitc::model_collect nitc nitc nitc\>semantize\>->nitc nitc\>modelize\> modelize nitc\>semantize\>->nitc\>modelize\> ...nitc::metrics_base ... ...nitc::metrics_base->nitc::metrics_base ...nitc::model_collect ... ...nitc::model_collect->nitc::model_collect ...nitc ... ...nitc->nitc ...nitc\>modelize\> ... ...nitc\>modelize\>->nitc\>modelize\> nitc::metrics metrics nitc::metrics->nitc::nullables_metrics nitc::nitmetrics nitmetrics nitc::nitmetrics->nitc::metrics nitc::api_metrics api_metrics nitc::api_metrics->nitc::metrics nitc::nitmetrics... ... nitc::nitmetrics...->nitc::nitmetrics nitc::api_metrics... ... nitc::api_metrics...->nitc::api_metrics

Ancestors

module abstract_collection

core :: abstract_collection

Abstract collection classes and services.
module abstract_text

core :: abstract_text

Abstract class for manipulation of sequences of characters
module annotation

nitc :: annotation

Management and utilities on annotations
module array

core :: array

This module introduces the standard array structure.
module auto_super_init

nitc :: auto_super_init

Computing of super-constructors that must be implicitly called at the begin of constructors.
module bitset

core :: bitset

Services to handle BitSet
module bytes

core :: bytes

Services for byte streams and arrays
module caching

serialization :: caching

Services for caching serialization engines
module circular_array

core :: circular_array

Efficient data structure to access both end of the sequence.
module codec_base

core :: codec_base

Base for codecs to use with streams
module codecs

core :: codecs

Group module for all codec-related manipulations
module collection

core :: collection

This module define several collection classes.
module console

console :: console

Defines some ANSI Terminal Control Escape Sequences.
module core

core :: core

Standard classes and methods used by default by Nit programs and libraries.
module counter

counter :: counter

Simple numerical statistical analysis and presentation
module csv

csv :: csv

CSV document handling.
module digraph

graph :: digraph

Implementation of directed graphs, also called digraphs.
module engine_tools

serialization :: engine_tools

Advanced services for serialization engines
module environ

core :: environ

Access to the environment variables of the process
module error

core :: error

Standard error-management infrastructure.
module exec

core :: exec

Invocation and management of operating system sub-processes.
module file

core :: file

File manipulations (create, read, write, etc.)
module fixed_ints

core :: fixed_ints

Basic integers of fixed-precision
module fixed_ints_text

core :: fixed_ints_text

Text services to complement fixed_ints
module flat

core :: flat

All the array-based text representations
module flow

nitc :: flow

Intraprocedural static flow.
module gc

core :: gc

Access to the Nit internal garbage collection mechanism
module hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
module ini

ini :: ini

Read and write INI configuration files
module inspect

serialization :: inspect

Refine Serializable::inspect to show more useful information
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module kernel

core :: kernel

Most basic classes and methods.
module lexer

nitc :: lexer

Lexer and its tokens.
module lexer_work

nitc :: lexer_work

Internal algorithm and data structures for the Nit lexer
module list

core :: list

This module handle double linked lists
module literal

nitc :: literal

Parsing of literal values in the abstract syntax tree.
module loader

nitc :: loader

Loading of Nit source files
module local_var_init

nitc :: local_var_init

Verify that local variables are initialized before their usage
module location

nitc :: location

Nit source-file and locations in source-file
module math

core :: math

Mathematical operations
module mdoc

nitc :: mdoc

Documentation of model entities
module meta

meta :: meta

Simple user-defined meta-level to manipulate types of instances as object.
module metrics_base

nitc :: metrics_base

Helpers for various statistics tools.
module mmodule

nitc :: mmodule

modules and module hierarchies in the metamodel
module mmodule_data

nitc :: mmodule_data

Define and retrieve data in modules
module model

nitc :: model

Classes, types and properties
module model_base

nitc :: model_base

The abstract concept of model and related common things
module model_collect

nitc :: model_collect

Collect things from the model.
module model_examples

nitc :: model_examples

Examples for Model entities
module modelbuilder_base

nitc :: modelbuilder_base

Load nit source files and build the associated model
module modelize

nitc :: modelize

Create a model from nit source files
module modelize_class

nitc :: modelize_class

Analysis and verification of class definitions to instantiate model element
module modelize_property

nitc :: modelize_property

Analysis and verification of property definitions to instantiate model element
module more_collections

more_collections :: more_collections

Highly specific, but useful, collections-related classes.
module mpackage

nitc :: mpackage

Modelisation of a Nit package
module native

core :: native

Native structures for text and bytes
module nitpm_shared

nitc :: nitpm_shared

Services related to the Nit package manager
module numeric

core :: numeric

Advanced services for Numeric types
module opts

opts :: opts

Management of options on the command line
module ordered_tree

ordered_tree :: ordered_tree

Manipulation and presentation of ordered trees.
module parse_annotations

nitc :: parse_annotations

Simple annotation parsing
module parser

nitc :: parser

Parser.
module parser_nodes

nitc :: parser_nodes

AST nodes of the Nit language
module parser_prod

nitc :: parser_prod

Production AST nodes full definition.
module parser_work

nitc :: parser_work

Internal algorithm and data structures for the Nit parser
module phase

nitc :: phase

Phases of the processing of nit programs
module poset

poset :: poset

Pre order sets and partial order set (ie hierarchies)
module protocol

core :: protocol

module queue

core :: queue

Queuing data structures and wrappers
module range

core :: range

Module for range of discrete objects.
module re

core :: re

Regular expression support for all services based on Pattern
module ropes

core :: ropes

Tree-based representation of a String.
module scope

nitc :: scope

Identification and scoping of local variables and labels.
module serialization

serialization :: serialization

General serialization services
module serialization_core

serialization :: serialization_core

Abstract services to serialize Nit objects to different formats
module sorter

core :: sorter

This module contains classes used to compare things and sorts arrays.
module stream

core :: stream

Input and output streams of characters
module tables

nitc :: tables

Module that interfaces the parsing tables.
module template

template :: template

Basic template system
module text

core :: text

All the classes and methods related to the manipulation of text entities
module time

core :: time

Management of time and dates
module toolcontext

nitc :: toolcontext

Common command-line tool infrastructure than handle options and error messages
module typing

nitc :: typing

Intraprocedural resolution of static types and OO-services
module union_find

core :: union_find

union–find algorithm using an efficient disjoint-set data structure
module utf8

core :: utf8

Codec for UTF-8 I/O
module version

nitc :: version

This file was generated by git-gen-version.sh

Parents

module mclasses_metrics

nitc :: mclasses_metrics

Collect common metrics about mclasses
module semantize

nitc :: semantize

Process bodies of methods in regard with the model.

Children

module metrics

nitc :: metrics

Various statistics about Nit models and programs

Descendants

module a_star-m

a_star-m

module api

nitc :: api

Components required to build a web server about the nit model.
module nitmetrics

nitc :: nitmetrics

A program that collects various metrics on nit programs and libraries
module nitweb

nitc :: nitweb

Runs a webserver based on nitcorn that render things from model.
# Statistics about the usage of nullables
module nullables_metrics

import metrics_base
import mclasses_metrics
import semantize

redef class ToolContext

	# Nullable types related metrics
	var nullables_metrics_phase: Phase = new NullablesMetricsPhase(self, null)
end

private class NullablesMetricsPhase
	super Phase
	redef fun process_mainmodule(mainmodule, given_mmodules)
	do
		if not toolcontext.opt_nullables.value and not toolcontext.opt_all.value then return

		var csv = toolcontext.opt_csv.value
		var out = "{toolcontext.opt_dir.value or else "metrics"}/nullables"
		out.mkdir

		print toolcontext.format_h1("\n# Nullable metrics")

		var model = toolcontext.modelbuilder.model
		var filter = new ModelFilter(private_visibility)

		var metrics = new MetricSet
		metrics.register(new CNBA(model, mainmodule, filter))
		metrics.register(new CNBNA(model, mainmodule, filter))

		var mclasses = new HashSet[MClass]
		for mpackage in model.mpackages do

			print toolcontext.format_h2("\n ## package {mpackage}")

			for mgroup in mpackage.mgroups do
				if mgroup.mmodules.is_empty then continue
				metrics.clear

				# Scalar metrics
				print toolcontext.format_h3("  `- group {mgroup.full_name}")
				var mod_mclasses = new HashSet[MClass]
				for mmodule in mgroup.mmodules do mod_mclasses.add_all(mmodule.intro_mclasses)
				if mod_mclasses.is_empty then continue
				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.write_to_file("{out}/{mgroup}.csv")
			end
		end
		if not mclasses.is_empty then
			metrics.clear
			# Global metrics
			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.write_to_file("{out}/summary.csv")
		end

		compute_nullables_metrics(toolcontext.modelbuilder)
	end
end

# Class Metric: Number of nullables MAttributes
class CNBNA
	super MClassMetric
	super IntMetric
	redef fun name do return "cnbna"
	redef fun desc do return "number of accessible nullable attributes (inherited + local)"

	redef fun collect(mclasses) do
		for mclass in mclasses do
			var all = mclass.collect_accessible_mattributes(mainmodule, filter)
			for mattr in all do
				if mattr.is_nullable then values.inc(mclass)
			end
		end
	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 nclassdef: AClassdef

	var total_sends: Int = 0
	var nullable_sends: Int = 0
	var nullable_eq_sends: Int = 0
	var buggy_sends: Int = 0

	redef fun visit(n)
	do
		n.visit_all(self)
		if n isa ASendExpr then
			self.total_sends += 1
			var t = n.n_expr.mtype
			if t == null then
				self.buggy_sends += 1
				return
			end
			t = t.anchor_to(self.nclassdef.mclassdef.mmodule, self.nclassdef.mclassdef.bound_mtype)
			if t isa MNullableType then
				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
				n.debug("Problem: strange receiver type found: {t} ({t.class_name})")
			end
		end
	end
end

# Visit the AST and print metrics about the usage of send on nullable reciever.
fun compute_nullables_metrics(modelbuilder: ModelBuilder)
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
	for nmodule in modelbuilder.nmodules do
		for nclassdef in nmodule.n_classdefs do
			var visitor = new NullableSends(nclassdef)
			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 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
src/metrics/nullables_metrics.nit:17,1--169,3