rta: use callsites to do the analysis
[nit.git] / src / metrics / metrics_base.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012 Jean Privat <jean@pryen.org>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # Helpers for various statistics tools.
18 module metrics_base
19
20 import model_utils
21 import csv
22 import counter
23
24 redef class ToolContext
25
26 # --all
27 var opt_all = new OptionBool("Compute all metrics", "--all")
28
29 # --inheritance
30 var opt_inheritance = new OptionBool("Compute metrics about inheritance usage", "--inheritance")
31 # --genericity
32 var opt_refinement = new OptionBool("Compute metrics about refinement usage", "--refinement")
33 # --self
34 var opt_self = new OptionBool("Compute metrics about the usage of explicit and implicit self", "--self")
35 # --ast
36 var opt_ast = new OptionBool("Compute metrics about the usage of nodes and identifiers in the AST", "--ast")
37 # --nullables
38 var opt_nullables = new OptionBool("Compute metrics on nullables send", "--nullables")
39 # --static-types
40 var opt_static_types = new OptionBool("Compute explicit static types metrics", "--static-types")
41 # --tables
42 var opt_tables = new OptionBool("Compute tables metrics", "--tables")
43 # --rta
44 var opt_rta = new OptionBool("Compute RTA metrics", "--rta")
45 # --generate-csv
46 var opt_generate_csv = new OptionBool("Generate CVS format metrics", "--generate-csv")
47 # --generate_hyperdoc
48 var opt_generate_hyperdoc = new OptionBool("Generate Hyperdoc", "--generate_hyperdoc")
49 # --poset
50 var opt_poset = new OptionBool("Complete metrics on posets", "--poset")
51
52 var opt_dir = new OptionString("Directory where some statistics files are generated", "-d", "--dir")
53 var output_dir: String = "."
54
55 redef init
56 do
57 super
58 self.option_context.add_option(opt_all)
59 self.option_context.add_option(opt_inheritance)
60 self.option_context.add_option(opt_refinement)
61 self.option_context.add_option(opt_self)
62 self.option_context.add_option(opt_ast)
63 self.option_context.add_option(opt_nullables)
64 self.option_context.add_option(opt_static_types)
65 self.option_context.add_option(opt_tables)
66 self.option_context.add_option(opt_rta)
67 self.option_context.add_option(opt_generate_csv)
68 self.option_context.add_option(opt_generate_hyperdoc)
69 self.option_context.add_option(opt_poset)
70 self.option_context.add_option(opt_dir)
71 end
72
73 redef fun process_options
74 do
75 super
76 var val = self.opt_dir.value
77 if val != null then
78 val = val.simplify_path
79 val.mkdir
80 self.output_dir = val
81 end
82 end
83 end
84
85 redef class Model
86
87 # List of modules in std lib
88 # FIXME this is quite ugly, find a dynamic way...
89 fun std_modules: Set[String] do
90 if self.std_modules_cache == null then
91 self.std_modules_cache = new HashSet[String]
92 self.std_modules_cache.add("collection")
93 self.std_modules_cache.add("abstract_collection")
94 self.std_modules_cache.add("array")
95 self.std_modules_cache.add("hash_collection")
96 self.std_modules_cache.add("list")
97 self.std_modules_cache.add("range")
98 self.std_modules_cache.add("sorter")
99 self.std_modules_cache.add("environ")
100 self.std_modules_cache.add("exec")
101 self.std_modules_cache.add("file")
102 self.std_modules_cache.add("gc")
103 self.std_modules_cache.add("hash")
104 self.std_modules_cache.add("kernel")
105 self.std_modules_cache.add("math")
106 self.std_modules_cache.add("standard")
107 self.std_modules_cache.add("stream")
108 self.std_modules_cache.add("string")
109 self.std_modules_cache.add("string_search")
110 self.std_modules_cache.add("time")
111 end
112 return self.std_modules_cache.as(not null)
113 end
114 private var std_modules_cache: nullable Set[String]
115 end
116
117 redef class MClass
118 fun is_user_defined: Bool do
119 return self.intro_mmodule.is_user_defined
120 end
121 end
122
123 redef class MModule
124 fun is_user_defined: Bool do
125 return not self.model.std_modules.has(self.name)
126 end
127 end