diff options
Diffstat (limited to 'crates/ra_cli/src/analysis_stats.rs')
-rw-r--r-- | crates/ra_cli/src/analysis_stats.rs | 259 |
1 files changed, 0 insertions, 259 deletions
diff --git a/crates/ra_cli/src/analysis_stats.rs b/crates/ra_cli/src/analysis_stats.rs deleted file mode 100644 index d40f04391..000000000 --- a/crates/ra_cli/src/analysis_stats.rs +++ /dev/null | |||
@@ -1,259 +0,0 @@ | |||
1 | //! FIXME: write short doc here | ||
2 | |||
3 | use std::{collections::HashSet, fmt::Write, path::Path, time::Instant}; | ||
4 | |||
5 | use hir::{ | ||
6 | db::{DefDatabase, HirDatabase}, | ||
7 | AssocItem, Crate, HasSource, HirDisplay, ModuleDef, | ||
8 | }; | ||
9 | use hir_def::FunctionId; | ||
10 | use hir_ty::{Ty, TypeWalk}; | ||
11 | use itertools::Itertools; | ||
12 | use ra_db::SourceDatabaseExt; | ||
13 | use ra_syntax::AstNode; | ||
14 | use rand::{seq::SliceRandom, thread_rng}; | ||
15 | |||
16 | use crate::{load_cargo::load_cargo, progress_report::ProgressReport, Result, Verbosity}; | ||
17 | |||
18 | pub fn run( | ||
19 | verbosity: Verbosity, | ||
20 | memory_usage: bool, | ||
21 | path: &Path, | ||
22 | only: Option<&str>, | ||
23 | with_deps: bool, | ||
24 | randomize: bool, | ||
25 | ) -> Result<()> { | ||
26 | let db_load_time = Instant::now(); | ||
27 | let (mut host, roots) = load_cargo(path)?; | ||
28 | let db = host.raw_database(); | ||
29 | println!("Database loaded, {} roots, {:?}", roots.len(), db_load_time.elapsed()); | ||
30 | let analysis_time = Instant::now(); | ||
31 | let mut num_crates = 0; | ||
32 | let mut visited_modules = HashSet::new(); | ||
33 | let mut visit_queue = Vec::new(); | ||
34 | |||
35 | let members = | ||
36 | roots | ||
37 | .into_iter() | ||
38 | .filter_map(|(source_root_id, project_root)| { | ||
39 | if with_deps || project_root.is_member() { | ||
40 | Some(source_root_id) | ||
41 | } else { | ||
42 | None | ||
43 | } | ||
44 | }) | ||
45 | .collect::<HashSet<_>>(); | ||
46 | |||
47 | let mut krates = Crate::all(db); | ||
48 | if randomize { | ||
49 | krates.shuffle(&mut thread_rng()); | ||
50 | } | ||
51 | for krate in krates { | ||
52 | let module = krate.root_module(db).expect("crate without root module"); | ||
53 | let file_id = module.definition_source(db).file_id; | ||
54 | if members.contains(&db.file_source_root(file_id.original_file(db))) { | ||
55 | num_crates += 1; | ||
56 | visit_queue.push(module); | ||
57 | } | ||
58 | } | ||
59 | |||
60 | if randomize { | ||
61 | visit_queue.shuffle(&mut thread_rng()); | ||
62 | } | ||
63 | |||
64 | println!("Crates in this dir: {}", num_crates); | ||
65 | let mut num_decls = 0; | ||
66 | let mut funcs = Vec::new(); | ||
67 | while let Some(module) = visit_queue.pop() { | ||
68 | if visited_modules.insert(module) { | ||
69 | visit_queue.extend(module.children(db)); | ||
70 | |||
71 | for decl in module.declarations(db) { | ||
72 | num_decls += 1; | ||
73 | if let ModuleDef::Function(f) = decl { | ||
74 | funcs.push(f); | ||
75 | } | ||
76 | } | ||
77 | |||
78 | for impl_block in module.impl_blocks(db) { | ||
79 | for item in impl_block.items(db) { | ||
80 | num_decls += 1; | ||
81 | if let AssocItem::Function(f) = item { | ||
82 | funcs.push(f); | ||
83 | } | ||
84 | } | ||
85 | } | ||
86 | } | ||
87 | } | ||
88 | println!("Total modules found: {}", visited_modules.len()); | ||
89 | println!("Total declarations: {}", num_decls); | ||
90 | println!("Total functions: {}", funcs.len()); | ||
91 | println!("Item Collection: {:?}, {}", analysis_time.elapsed(), ra_prof::memory_usage()); | ||
92 | |||
93 | if randomize { | ||
94 | funcs.shuffle(&mut thread_rng()); | ||
95 | } | ||
96 | |||
97 | let inference_time = Instant::now(); | ||
98 | let mut bar = match verbosity { | ||
99 | Verbosity::Quiet | Verbosity::Spammy => ProgressReport::hidden(), | ||
100 | _ => ProgressReport::new(funcs.len() as u64), | ||
101 | }; | ||
102 | |||
103 | bar.tick(); | ||
104 | let mut num_exprs = 0; | ||
105 | let mut num_exprs_unknown = 0; | ||
106 | let mut num_exprs_partially_unknown = 0; | ||
107 | let mut num_type_mismatches = 0; | ||
108 | for f in funcs { | ||
109 | let name = f.name(db); | ||
110 | let full_name = f | ||
111 | .module(db) | ||
112 | .path_to_root(db) | ||
113 | .into_iter() | ||
114 | .rev() | ||
115 | .filter_map(|it| it.name(db)) | ||
116 | .chain(Some(f.name(db))) | ||
117 | .join("::"); | ||
118 | if let Some(only_name) = only { | ||
119 | if name.to_string() != only_name && full_name != only_name { | ||
120 | continue; | ||
121 | } | ||
122 | } | ||
123 | let mut msg = format!("processing: {}", full_name); | ||
124 | if verbosity.is_verbose() { | ||
125 | let src = f.source(db); | ||
126 | let original_file = src.file_id.original_file(db); | ||
127 | let path = db.file_relative_path(original_file); | ||
128 | let syntax_range = src.value.syntax().text_range(); | ||
129 | write!(msg, " ({:?} {})", path, syntax_range).unwrap(); | ||
130 | } | ||
131 | if verbosity.is_spammy() { | ||
132 | bar.println(format!("{}", msg)); | ||
133 | } | ||
134 | bar.set_message(&msg); | ||
135 | let f_id = FunctionId::from(f); | ||
136 | let body = db.body(f_id.into()); | ||
137 | let inference_result = db.infer(f_id.into()); | ||
138 | let (previous_exprs, previous_unknown, previous_partially_unknown) = | ||
139 | (num_exprs, num_exprs_unknown, num_exprs_partially_unknown); | ||
140 | for (expr_id, _) in body.exprs.iter() { | ||
141 | let ty = &inference_result[expr_id]; | ||
142 | num_exprs += 1; | ||
143 | if let Ty::Unknown = ty { | ||
144 | num_exprs_unknown += 1; | ||
145 | } else { | ||
146 | let mut is_partially_unknown = false; | ||
147 | ty.walk(&mut |ty| { | ||
148 | if let Ty::Unknown = ty { | ||
149 | is_partially_unknown = true; | ||
150 | } | ||
151 | }); | ||
152 | if is_partially_unknown { | ||
153 | num_exprs_partially_unknown += 1; | ||
154 | } | ||
155 | } | ||
156 | if only.is_some() && verbosity.is_spammy() { | ||
157 | // in super-verbose mode for just one function, we print every single expression | ||
158 | let (_, sm) = db.body_with_source_map(f_id.into()); | ||
159 | let src = sm.expr_syntax(expr_id); | ||
160 | if let Some(src) = src { | ||
161 | let original_file = src.file_id.original_file(db); | ||
162 | let line_index = host.analysis().file_line_index(original_file).unwrap(); | ||
163 | let text_range = src.value.either( | ||
164 | |it| it.syntax_node_ptr().range(), | ||
165 | |it| it.syntax_node_ptr().range(), | ||
166 | ); | ||
167 | let (start, end) = ( | ||
168 | line_index.line_col(text_range.start()), | ||
169 | line_index.line_col(text_range.end()), | ||
170 | ); | ||
171 | bar.println(format!( | ||
172 | "{}:{}-{}:{}: {}", | ||
173 | start.line + 1, | ||
174 | start.col_utf16, | ||
175 | end.line + 1, | ||
176 | end.col_utf16, | ||
177 | ty.display(db) | ||
178 | )); | ||
179 | } else { | ||
180 | bar.println(format!("unknown location: {}", ty.display(db))); | ||
181 | } | ||
182 | } | ||
183 | if let Some(mismatch) = inference_result.type_mismatch_for_expr(expr_id) { | ||
184 | num_type_mismatches += 1; | ||
185 | if verbosity.is_verbose() { | ||
186 | let (_, sm) = db.body_with_source_map(f_id.into()); | ||
187 | let src = sm.expr_syntax(expr_id); | ||
188 | if let Some(src) = src { | ||
189 | // FIXME: it might be nice to have a function (on Analysis?) that goes from Source<T> -> (LineCol, LineCol) directly | ||
190 | let original_file = src.file_id.original_file(db); | ||
191 | let path = db.file_relative_path(original_file); | ||
192 | let line_index = host.analysis().file_line_index(original_file).unwrap(); | ||
193 | let text_range = src.value.either( | ||
194 | |it| it.syntax_node_ptr().range(), | ||
195 | |it| it.syntax_node_ptr().range(), | ||
196 | ); | ||
197 | let (start, end) = ( | ||
198 | line_index.line_col(text_range.start()), | ||
199 | line_index.line_col(text_range.end()), | ||
200 | ); | ||
201 | bar.println(format!( | ||
202 | "{} {}:{}-{}:{}: Expected {}, got {}", | ||
203 | path, | ||
204 | start.line + 1, | ||
205 | start.col_utf16, | ||
206 | end.line + 1, | ||
207 | end.col_utf16, | ||
208 | mismatch.expected.display(db), | ||
209 | mismatch.actual.display(db) | ||
210 | )); | ||
211 | } else { | ||
212 | bar.println(format!( | ||
213 | "{}: Expected {}, got {}", | ||
214 | name, | ||
215 | mismatch.expected.display(db), | ||
216 | mismatch.actual.display(db) | ||
217 | )); | ||
218 | } | ||
219 | } | ||
220 | } | ||
221 | } | ||
222 | if verbosity.is_spammy() { | ||
223 | bar.println(format!( | ||
224 | "In {}: {} exprs, {} unknown, {} partial", | ||
225 | full_name, | ||
226 | num_exprs - previous_exprs, | ||
227 | num_exprs_unknown - previous_unknown, | ||
228 | num_exprs_partially_unknown - previous_partially_unknown | ||
229 | )); | ||
230 | } | ||
231 | bar.inc(1); | ||
232 | } | ||
233 | bar.finish_and_clear(); | ||
234 | println!("Total expressions: {}", num_exprs); | ||
235 | println!( | ||
236 | "Expressions of unknown type: {} ({}%)", | ||
237 | num_exprs_unknown, | ||
238 | if num_exprs > 0 { num_exprs_unknown * 100 / num_exprs } else { 100 } | ||
239 | ); | ||
240 | println!( | ||
241 | "Expressions of partially unknown type: {} ({}%)", | ||
242 | num_exprs_partially_unknown, | ||
243 | if num_exprs > 0 { num_exprs_partially_unknown * 100 / num_exprs } else { 100 } | ||
244 | ); | ||
245 | println!("Type mismatches: {}", num_type_mismatches); | ||
246 | println!("Inference: {:?}, {}", inference_time.elapsed(), ra_prof::memory_usage()); | ||
247 | println!("Total: {:?}, {}", analysis_time.elapsed(), ra_prof::memory_usage()); | ||
248 | |||
249 | if memory_usage { | ||
250 | for (name, bytes) in host.per_query_memory_usage() { | ||
251 | println!("{:>8} {}", bytes, name) | ||
252 | } | ||
253 | let before = ra_prof::memory_usage(); | ||
254 | drop(host); | ||
255 | println!("leftover: {}", before.allocated - ra_prof::memory_usage().allocated) | ||
256 | } | ||
257 | |||
258 | Ok(()) | ||
259 | } | ||