rustc_mir_transform/coverage/
hir_info.rs1use rustc_hir as hir;
2use rustc_hir::intravisit::{Visitor, walk_expr};
3use rustc_middle::hir::nested_filter;
4use rustc_middle::ty::TyCtxt;
5use rustc_span::Span;
6use rustc_span::def_id::LocalDefId;
7
8#[derive(Debug)]
10pub(crate) struct ExtractedHirInfo {
11 pub(crate) function_source_hash: u64,
12 pub(crate) is_async_fn: bool,
13 pub(crate) fn_sig_span: Option<Span>,
16 pub(crate) body_span: Span,
17 pub(crate) hole_spans: Vec<Span>,
21}
22
23pub(crate) fn extract_hir_info<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> ExtractedHirInfo {
24 if tcx.is_synthetic_mir(def_id) {
29 return extract_hir_info(tcx, tcx.local_parent(def_id));
30 }
31
32 let hir_node = tcx.hir_node_by_def_id(def_id);
33 let fn_body_id = hir_node.body_id().expect("HIR node is a function with body");
34 let hir_body = tcx.hir_body(fn_body_id);
35
36 let maybe_fn_sig = hir_node.fn_sig();
37 let is_async_fn = maybe_fn_sig.is_some_and(|fn_sig| fn_sig.header.is_async());
38
39 let mut body_span = hir_body.value.span;
40
41 use hir::{Closure, Expr, ExprKind, Node};
42 if let Node::Expr(&Expr { kind: ExprKind::Closure(&Closure { fn_decl_span, .. }), .. }) =
46 hir_node
47 {
48 body_span = body_span.find_ancestor_in_same_ctxt(fn_decl_span).unwrap_or(body_span);
49 }
50
51 let fn_sig_span = maybe_fn_sig.map(|fn_sig| fn_sig.span).filter(|&fn_sig_span| {
54 let source_map = tcx.sess.source_map();
55 let file_idx = |span: Span| source_map.lookup_source_file_idx(span.lo());
56
57 fn_sig_span.eq_ctxt(body_span)
58 && fn_sig_span.hi() <= body_span.lo()
59 && file_idx(fn_sig_span) == file_idx(body_span)
60 });
61
62 let function_source_hash = hash_mir_source(tcx, hir_body);
63
64 let hole_spans = extract_hole_spans_from_hir(tcx, hir_body);
65
66 ExtractedHirInfo { function_source_hash, is_async_fn, fn_sig_span, body_span, hole_spans }
67}
68
69fn hash_mir_source<'tcx>(tcx: TyCtxt<'tcx>, hir_body: &'tcx hir::Body<'tcx>) -> u64 {
70 let owner = hir_body.id().hir_id.owner;
71 tcx.hir_owner_nodes(owner)
72 .opt_hash_including_bodies
73 .expect("hash should be present when coverage instrumentation is enabled")
74 .to_smaller_hash()
75 .as_u64()
76}
77
78fn extract_hole_spans_from_hir<'tcx>(tcx: TyCtxt<'tcx>, hir_body: &hir::Body<'tcx>) -> Vec<Span> {
79 struct HolesVisitor<'tcx> {
80 tcx: TyCtxt<'tcx>,
81 hole_spans: Vec<Span>,
82 }
83
84 impl<'tcx> Visitor<'tcx> for HolesVisitor<'tcx> {
85 type NestedFilter = nested_filter::OnlyBodies;
89
90 fn maybe_tcx(&mut self) -> TyCtxt<'tcx> {
91 self.tcx
92 }
93
94 fn visit_nested_item(&mut self, id: hir::ItemId) -> Self::Result {
97 let span = self.tcx.def_span(id.owner_id.def_id);
98 self.visit_hole_span(span);
99 }
102
103 fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
106 match expr.kind {
107 hir::ExprKind::Closure(_) | hir::ExprKind::ConstBlock(_) => {
108 self.visit_hole_span(expr.span);
109 }
112
113 _ => walk_expr(self, expr),
115 }
116 }
117 }
118 impl HolesVisitor<'_> {
119 fn visit_hole_span(&mut self, hole_span: Span) {
120 self.hole_spans.push(hole_span);
121 }
122 }
123
124 let mut visitor = HolesVisitor { tcx, hole_spans: vec![] };
125
126 visitor.visit_body(hir_body);
127 visitor.hole_spans
128}