Skip to content

Commit f26fdce

Browse files
committed
Improve HIR stats collector.
Adds and removes some `visit_*` methods accordingly, improving coverage, and avoiding some double counting. Brings it in line with the AST stats collector.
1 parent 0a52fbe commit f26fdce

File tree

2 files changed

+246
-70
lines changed

2 files changed

+246
-70
lines changed

compiler/rustc_passes/src/hir_stats.rs

+194-50
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,22 @@ impl<'k> StatCollector<'k> {
168168
}
169169
}
170170

171+
// Used to avoid boilerplate for types with many variants.
172+
macro_rules! record_variants {
173+
(
174+
($self:ident, $val:expr, $kind:expr, $id:expr, $mod:ident, $ty:ty, $tykind:ident),
175+
[$($variant:ident),*]
176+
) => {
177+
match $kind {
178+
$(
179+
$mod::$tykind::$variant { .. } => {
180+
$self.record_variant(stringify!($ty), stringify!($variant), $id, $val)
181+
}
182+
)*
183+
}
184+
};
185+
}
186+
171187
impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
172188
fn visit_param(&mut self, param: &'v hir::Param<'v>) {
173189
self.record("Param", Id::Node(param.hir_id), param);
@@ -200,12 +216,46 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
200216
}
201217

202218
fn visit_item(&mut self, i: &'v hir::Item<'v>) {
203-
self.record("Item", Id::Node(i.hir_id()), i);
219+
record_variants!(
220+
(self, i, i.kind, Id::Node(i.hir_id()), hir, Item, ItemKind),
221+
[
222+
ExternCrate,
223+
Use,
224+
Static,
225+
Const,
226+
Fn,
227+
Macro,
228+
Mod,
229+
ForeignMod,
230+
GlobalAsm,
231+
TyAlias,
232+
OpaqueTy,
233+
Enum,
234+
Struct,
235+
Union,
236+
Trait,
237+
TraitAlias,
238+
Impl
239+
]
240+
);
204241
hir_visit::walk_item(self, i)
205242
}
206243

244+
fn visit_body(&mut self, b: &'v hir::Body<'v>) {
245+
self.record("Body", Id::None, b);
246+
hir_visit::walk_body(self, b);
247+
}
248+
249+
fn visit_mod(&mut self, m: &'v hir::Mod<'v>, _s: Span, n: HirId) {
250+
self.record("Mod", Id::None, m);
251+
hir_visit::walk_mod(self, m, n)
252+
}
253+
207254
fn visit_foreign_item(&mut self, i: &'v hir::ForeignItem<'v>) {
208-
self.record("ForeignItem", Id::Node(i.hir_id()), i);
255+
record_variants!(
256+
(self, i, i.kind, Id::Node(i.hir_id()), hir, ForeignItem, ForeignItemKind),
257+
[Fn, Static, Type]
258+
);
209259
hir_visit::walk_foreign_item(self, i)
210260
}
211261

@@ -220,7 +270,10 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
220270
}
221271

222272
fn visit_stmt(&mut self, s: &'v hir::Stmt<'v>) {
223-
self.record("Stmt", Id::Node(s.hir_id), s);
273+
record_variants!(
274+
(self, s, s.kind, Id::Node(s.hir_id), hir, Stmt, StmtKind),
275+
[Local, Item, Expr, Semi]
276+
);
224277
hir_visit::walk_stmt(self, s)
225278
}
226279

@@ -230,20 +283,80 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
230283
}
231284

232285
fn visit_pat(&mut self, p: &'v hir::Pat<'v>) {
233-
self.record("Pat", Id::Node(p.hir_id), p);
286+
record_variants!(
287+
(self, p, p.kind, Id::Node(p.hir_id), hir, Pat, PatKind),
288+
[Wild, Binding, Struct, TupleStruct, Or, Path, Tuple, Box, Ref, Lit, Range, Slice]
289+
);
234290
hir_visit::walk_pat(self, p)
235291
}
236292

237-
fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) {
238-
self.record("Expr", Id::Node(ex.hir_id), ex);
239-
hir_visit::walk_expr(self, ex)
293+
fn visit_pat_field(&mut self, f: &'v hir::PatField<'v>) {
294+
self.record("PatField", Id::Node(f.hir_id), f);
295+
hir_visit::walk_pat_field(self, f)
296+
}
297+
298+
fn visit_expr(&mut self, e: &'v hir::Expr<'v>) {
299+
record_variants!(
300+
(self, e, e.kind, Id::Node(e.hir_id), hir, Expr, ExprKind),
301+
[
302+
Box, ConstBlock, Array, Call, MethodCall, Tup, Binary, Unary, Lit, Cast, Type,
303+
DropTemps, Let, If, Loop, Match, Closure, Block, Assign, AssignOp, Field, Index,
304+
Path, AddrOf, Break, Continue, Ret, InlineAsm, Struct, Repeat, Yield, Err
305+
]
306+
);
307+
hir_visit::walk_expr(self, e)
308+
}
309+
310+
fn visit_let_expr(&mut self, lex: &'v hir::Let<'v>) {
311+
self.record("Let", Id::Node(lex.hir_id), lex);
312+
hir_visit::walk_let_expr(self, lex)
313+
}
314+
315+
fn visit_expr_field(&mut self, f: &'v hir::ExprField<'v>) {
316+
self.record("ExprField", Id::Node(f.hir_id), f);
317+
hir_visit::walk_expr_field(self, f)
240318
}
241319

242320
fn visit_ty(&mut self, t: &'v hir::Ty<'v>) {
243-
self.record("Ty", Id::Node(t.hir_id), t);
321+
record_variants!(
322+
(self, t, t.kind, Id::Node(t.hir_id), hir, Ty, TyKind),
323+
[
324+
Slice,
325+
Array,
326+
Ptr,
327+
Rptr,
328+
BareFn,
329+
Never,
330+
Tup,
331+
Path,
332+
OpaqueDef,
333+
TraitObject,
334+
Typeof,
335+
Infer,
336+
Err
337+
]
338+
);
244339
hir_visit::walk_ty(self, t)
245340
}
246341

342+
fn visit_generic_param(&mut self, p: &'v hir::GenericParam<'v>) {
343+
self.record("GenericParam", Id::Node(p.hir_id), p);
344+
hir_visit::walk_generic_param(self, p)
345+
}
346+
347+
fn visit_generics(&mut self, g: &'v hir::Generics<'v>) {
348+
self.record("Generics", Id::None, g);
349+
hir_visit::walk_generics(self, g)
350+
}
351+
352+
fn visit_where_predicate(&mut self, p: &'v hir::WherePredicate<'v>) {
353+
record_variants!(
354+
(self, p, p, Id::None, hir, WherePredicate, WherePredicate),
355+
[BoundPredicate, RegionPredicate, EqPredicate]
356+
);
357+
hir_visit::walk_where_predicate(self, p)
358+
}
359+
247360
fn visit_fn(
248361
&mut self,
249362
fk: hir_visit::FnKind<'v>,
@@ -256,24 +369,49 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
256369
hir_visit::walk_fn(self, fk, fd, b, s, id)
257370
}
258371

259-
fn visit_where_predicate(&mut self, predicate: &'v hir::WherePredicate<'v>) {
260-
self.record("WherePredicate", Id::None, predicate);
261-
hir_visit::walk_where_predicate(self, predicate)
372+
fn visit_use(&mut self, p: &'v hir::Path<'v>, hir_id: hir::HirId) {
373+
// This is `visit_use`, but the type is `Path` so record it that way.
374+
self.record("Path", Id::None, p);
375+
hir_visit::walk_use(self, p, hir_id)
262376
}
263377

264378
fn visit_trait_item(&mut self, ti: &'v hir::TraitItem<'v>) {
265-
self.record("TraitItem", Id::Node(ti.hir_id()), ti);
379+
record_variants!(
380+
(self, ti, ti.kind, Id::Node(ti.hir_id()), hir, TraitItem, TraitItemKind),
381+
[Const, Fn, Type]
382+
);
266383
hir_visit::walk_trait_item(self, ti)
267384
}
268385

386+
fn visit_trait_item_ref(&mut self, ti: &'v hir::TraitItemRef) {
387+
self.record("TraitItemRef", Id::Node(ti.id.hir_id()), ti);
388+
hir_visit::walk_trait_item_ref(self, ti)
389+
}
390+
269391
fn visit_impl_item(&mut self, ii: &'v hir::ImplItem<'v>) {
270-
self.record("ImplItem", Id::Node(ii.hir_id()), ii);
392+
record_variants!(
393+
(self, ii, ii.kind, Id::Node(ii.hir_id()), hir, ImplItem, ImplItemKind),
394+
[Const, Fn, TyAlias]
395+
);
271396
hir_visit::walk_impl_item(self, ii)
272397
}
273398

274-
fn visit_param_bound(&mut self, bounds: &'v hir::GenericBound<'v>) {
275-
self.record("GenericBound", Id::None, bounds);
276-
hir_visit::walk_param_bound(self, bounds)
399+
fn visit_foreign_item_ref(&mut self, fi: &'v hir::ForeignItemRef) {
400+
self.record("ForeignItemRef", Id::Node(fi.id.hir_id()), fi);
401+
hir_visit::walk_foreign_item_ref(self, fi)
402+
}
403+
404+
fn visit_impl_item_ref(&mut self, ii: &'v hir::ImplItemRef) {
405+
self.record("ImplItemRef", Id::Node(ii.id.hir_id()), ii);
406+
hir_visit::walk_impl_item_ref(self, ii)
407+
}
408+
409+
fn visit_param_bound(&mut self, b: &'v hir::GenericBound<'v>) {
410+
record_variants!(
411+
(self, b, b, Id::None, hir, GenericBound, GenericBound),
412+
[Trait, LangItemTrait, Outlives]
413+
);
414+
hir_visit::walk_param_bound(self, b)
277415
}
278416

279417
fn visit_field_def(&mut self, s: &'v hir::FieldDef<'v>) {
@@ -286,14 +424,17 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
286424
hir_visit::walk_variant(self, v)
287425
}
288426

289-
fn visit_lifetime(&mut self, lifetime: &'v hir::Lifetime) {
290-
self.record("Lifetime", Id::Node(lifetime.hir_id), lifetime);
291-
hir_visit::walk_lifetime(self, lifetime)
292-
}
293-
294-
fn visit_qpath(&mut self, qpath: &'v hir::QPath<'v>, id: hir::HirId, span: Span) {
295-
self.record("QPath", Id::None, qpath);
296-
hir_visit::walk_qpath(self, qpath, id, span)
427+
fn visit_generic_arg(&mut self, ga: &'v hir::GenericArg<'v>) {
428+
record_variants!(
429+
(self, ga, ga, Id::Node(ga.hir_id()), hir, GenericArg, GenericArg),
430+
[Lifetime, Type, Const, Infer]
431+
);
432+
match ga {
433+
hir::GenericArg::Lifetime(lt) => self.visit_lifetime(lt),
434+
hir::GenericArg::Type(ty) => self.visit_ty(ty),
435+
hir::GenericArg::Const(ct) => self.visit_anon_const(&ct.value),
436+
hir::GenericArg::Infer(inf) => self.visit_infer(inf),
437+
}
297438
}
298439

299440
fn visit_path(&mut self, path: &'v hir::Path<'v>, _id: hir::HirId) {
@@ -306,6 +447,11 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
306447
hir_visit::walk_path_segment(self, path_span, path_segment)
307448
}
308449

450+
fn visit_generic_args(&mut self, sp: Span, ga: &'v hir::GenericArgs<'v>) {
451+
self.record("GenericArgs", Id::None, ga);
452+
hir_visit::walk_generic_args(self, sp, ga)
453+
}
454+
309455
fn visit_assoc_type_binding(&mut self, type_binding: &'v hir::TypeBinding<'v>) {
310456
self.record("TypeBinding", Id::Node(type_binding.hir_id), type_binding);
311457
hir_visit::walk_assoc_type_binding(self, type_binding)
@@ -314,36 +460,25 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
314460
fn visit_attribute(&mut self, attr: &'v ast::Attribute) {
315461
self.record("Attribute", Id::Attr(attr.id), attr);
316462
}
317-
}
318463

319-
// Used to avoid boilerplate for types with many variants.
320-
macro_rules! record_variants {
321-
(
322-
($self:ident, $val:expr, $kind:expr, $ty:ty, $tykind:ident), // mandatory pieces
323-
[$($variant:ident),*]
324-
) => {
325-
match $kind {
326-
$(
327-
ast::$tykind::$variant { .. } => {
328-
$self.record_variant(stringify!($ty), stringify!($variant), Id::None, $val)
329-
}
330-
)*
331-
}
332-
};
464+
fn visit_inline_asm(&mut self, asm: &'v hir::InlineAsm<'v>, id: HirId) {
465+
self.record("InlineAsm", Id::None, asm);
466+
hir_visit::walk_inline_asm(self, asm, id);
467+
}
333468
}
334469

335470
impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
336471
fn visit_foreign_item(&mut self, i: &'v ast::ForeignItem) {
337472
record_variants!(
338-
(self, i, i.kind, ForeignItem, ForeignItemKind),
473+
(self, i, i.kind, Id::None, ast, ForeignItem, ForeignItemKind),
339474
[Static, Fn, TyAlias, MacCall]
340475
);
341476
ast_visit::walk_foreign_item(self, i)
342477
}
343478

344479
fn visit_item(&mut self, i: &'v ast::Item) {
345480
record_variants!(
346-
(self, i, i.kind, Item, ItemKind),
481+
(self, i, i.kind, Id::None, ast, Item, ItemKind),
347482
[
348483
ExternCrate,
349484
Use,
@@ -379,7 +514,7 @@ impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
379514

380515
fn visit_stmt(&mut self, s: &'v ast::Stmt) {
381516
record_variants!(
382-
(self, s, s.kind, Stmt, StmtKind),
517+
(self, s, s.kind, Id::None, ast, Stmt, StmtKind),
383518
[Local, Item, Expr, Semi, Empty, MacCall]
384519
);
385520
ast_visit::walk_stmt(self, s)
@@ -397,7 +532,7 @@ impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
397532

398533
fn visit_pat(&mut self, p: &'v ast::Pat) {
399534
record_variants!(
400-
(self, p, p.kind, Pat, PatKind),
535+
(self, p, p.kind, Id::None, ast, Pat, PatKind),
401536
[
402537
Wild,
403538
Ident,
@@ -421,7 +556,7 @@ impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
421556

422557
fn visit_expr(&mut self, e: &'v ast::Expr) {
423558
record_variants!(
424-
(self, e, e.kind, Expr, ExprKind),
559+
(self, e, e.kind, Id::None, ast, Expr, ExprKind),
425560
[
426561
Box, Array, ConstBlock, Call, MethodCall, Tup, Binary, Unary, Lit, Cast, Type, Let,
427562
If, While, ForLoop, Loop, Match, Closure, Block, Async, Await, TryBlock, Assign,
@@ -434,7 +569,7 @@ impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
434569

435570
fn visit_ty(&mut self, t: &'v ast::Ty) {
436571
record_variants!(
437-
(self, t, t.kind, Ty, TyKind),
572+
(self, t, t.kind, Id::None, ast, Ty, TyKind),
438573
[
439574
Slice,
440575
Array,
@@ -466,7 +601,7 @@ impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
466601

467602
fn visit_where_predicate(&mut self, p: &'v ast::WherePredicate) {
468603
record_variants!(
469-
(self, p, p, WherePredicate, WherePredicate),
604+
(self, p, p, Id::None, ast, WherePredicate, WherePredicate),
470605
[BoundPredicate, RegionPredicate, EqPredicate]
471606
);
472607
ast_visit::walk_where_predicate(self, p)
@@ -479,14 +614,17 @@ impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
479614

480615
fn visit_assoc_item(&mut self, i: &'v ast::AssocItem, ctxt: ast_visit::AssocCtxt) {
481616
record_variants!(
482-
(self, i, i.kind, AssocItem, AssocItemKind),
617+
(self, i, i.kind, Id::None, ast, AssocItem, AssocItemKind),
483618
[Const, Fn, TyAlias, MacCall]
484619
);
485620
ast_visit::walk_assoc_item(self, i, ctxt);
486621
}
487622

488623
fn visit_param_bound(&mut self, b: &'v ast::GenericBound, _ctxt: BoundKind) {
489-
record_variants!((self, b, b, GenericBound, GenericBound), [Trait, Outlives]);
624+
record_variants!(
625+
(self, b, b, Id::None, ast, GenericBound, GenericBound),
626+
[Trait, Outlives]
627+
);
490628
ast_visit::walk_param_bound(self, b)
491629
}
492630

@@ -519,12 +657,18 @@ impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
519657
// common, so we implement `visit_generic_args` and tolerate the double
520658
// counting in the former case.
521659
fn visit_generic_args(&mut self, sp: Span, g: &'v ast::GenericArgs) {
522-
record_variants!((self, g, g, GenericArgs, GenericArgs), [AngleBracketed, Parenthesized]);
660+
record_variants!(
661+
(self, g, g, Id::None, ast, GenericArgs, GenericArgs),
662+
[AngleBracketed, Parenthesized]
663+
);
523664
ast_visit::walk_generic_args(self, sp, g)
524665
}
525666

526667
fn visit_attribute(&mut self, attr: &'v ast::Attribute) {
527-
record_variants!((self, attr, attr.kind, Attribute, AttrKind), [Normal, DocComment]);
668+
record_variants!(
669+
(self, attr, attr.kind, Id::None, ast, Attribute, AttrKind),
670+
[Normal, DocComment]
671+
);
528672
ast_visit::walk_attribute(self, attr)
529673
}
530674

0 commit comments

Comments
 (0)