|
1 |
| -mod counters; |
2 |
| -mod graph; |
3 |
| -mod mappings; |
4 |
| -pub(super) mod query; |
5 |
| -mod spans; |
6 |
| -#[cfg(test)] |
7 |
| -mod tests; |
8 |
| -mod unexpand; |
9 |
| - |
10 |
| -use rustc_hir as hir; |
11 |
| -use rustc_hir::intravisit::{Visitor, walk_expr}; |
12 |
| -use rustc_middle::hir::nested_filter; |
13 | 1 | use rustc_middle::mir::coverage::{
|
14 | 2 | CoverageKind, DecisionInfo, FunctionCoverageInfo, Mapping, MappingKind,
|
15 | 3 | };
|
16 | 4 | use rustc_middle::mir::{self, BasicBlock, Statement, StatementKind, TerminatorKind};
|
17 | 5 | use rustc_middle::ty::TyCtxt;
|
18 |
| -use rustc_span::Span; |
19 |
| -use rustc_span::def_id::LocalDefId; |
20 | 6 | use tracing::{debug, debug_span, trace};
|
21 | 7 |
|
22 | 8 | use crate::coverage::counters::BcbCountersData;
|
23 | 9 | use crate::coverage::graph::CoverageGraph;
|
24 | 10 | use crate::coverage::mappings::ExtractedMappings;
|
25 | 11 |
|
| 12 | +mod counters; |
| 13 | +mod graph; |
| 14 | +mod hir_info; |
| 15 | +mod mappings; |
| 16 | +pub(super) mod query; |
| 17 | +mod spans; |
| 18 | +#[cfg(test)] |
| 19 | +mod tests; |
| 20 | +mod unexpand; |
| 21 | + |
26 | 22 | /// Inserts `StatementKind::Coverage` statements that either instrument the binary with injected
|
27 | 23 | /// counters, via intrinsic `llvm.instrprof.increment`, and/or inject metadata used during codegen
|
28 | 24 | /// to construct the coverage map.
|
@@ -69,7 +65,7 @@ fn instrument_function_for_coverage<'tcx>(tcx: TyCtxt<'tcx>, mir_body: &mut mir:
|
69 | 65 | let def_id = mir_body.source.def_id();
|
70 | 66 | let _span = debug_span!("instrument_function_for_coverage", ?def_id).entered();
|
71 | 67 |
|
72 |
| - let hir_info = extract_hir_info(tcx, def_id.expect_local()); |
| 68 | + let hir_info = hir_info::extract_hir_info(tcx, def_id.expect_local()); |
73 | 69 |
|
74 | 70 | // Build the coverage graph, which is a simplified view of the MIR control-flow
|
75 | 71 | // graph that ignores some details not relevant to coverage instrumentation.
|
@@ -262,122 +258,3 @@ fn inject_statement(mir_body: &mut mir::Body<'_>, counter_kind: CoverageKind, bb
|
262 | 258 | let statement = Statement::new(source_info, StatementKind::Coverage(counter_kind));
|
263 | 259 | data.statements.insert(0, statement);
|
264 | 260 | }
|
265 |
| - |
266 |
| -/// Function information extracted from HIR by the coverage instrumentor. |
267 |
| -#[derive(Debug)] |
268 |
| -struct ExtractedHirInfo { |
269 |
| - function_source_hash: u64, |
270 |
| - is_async_fn: bool, |
271 |
| - /// The span of the function's signature, if available. |
272 |
| - /// Must have the same context and filename as the body span. |
273 |
| - fn_sig_span: Option<Span>, |
274 |
| - body_span: Span, |
275 |
| - /// "Holes" are regions within the function body (or its expansions) that |
276 |
| - /// should not be included in coverage spans for this function |
277 |
| - /// (e.g. closures and nested items). |
278 |
| - hole_spans: Vec<Span>, |
279 |
| -} |
280 |
| - |
281 |
| -fn extract_hir_info<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> ExtractedHirInfo { |
282 |
| - // FIXME(#79625): Consider improving MIR to provide the information needed, to avoid going back |
283 |
| - // to HIR for it. |
284 |
| - |
285 |
| - // HACK: For synthetic MIR bodies (async closures), use the def id of the HIR body. |
286 |
| - if tcx.is_synthetic_mir(def_id) { |
287 |
| - return extract_hir_info(tcx, tcx.local_parent(def_id)); |
288 |
| - } |
289 |
| - |
290 |
| - let hir_node = tcx.hir_node_by_def_id(def_id); |
291 |
| - let fn_body_id = hir_node.body_id().expect("HIR node is a function with body"); |
292 |
| - let hir_body = tcx.hir_body(fn_body_id); |
293 |
| - |
294 |
| - let maybe_fn_sig = hir_node.fn_sig(); |
295 |
| - let is_async_fn = maybe_fn_sig.is_some_and(|fn_sig| fn_sig.header.is_async()); |
296 |
| - |
297 |
| - let mut body_span = hir_body.value.span; |
298 |
| - |
299 |
| - use hir::{Closure, Expr, ExprKind, Node}; |
300 |
| - // Unexpand a closure's body span back to the context of its declaration. |
301 |
| - // This helps with closure bodies that consist of just a single bang-macro, |
302 |
| - // and also with closure bodies produced by async desugaring. |
303 |
| - if let Node::Expr(&Expr { kind: ExprKind::Closure(&Closure { fn_decl_span, .. }), .. }) = |
304 |
| - hir_node |
305 |
| - { |
306 |
| - body_span = body_span.find_ancestor_in_same_ctxt(fn_decl_span).unwrap_or(body_span); |
307 |
| - } |
308 |
| - |
309 |
| - // The actual signature span is only used if it has the same context and |
310 |
| - // filename as the body, and precedes the body. |
311 |
| - let fn_sig_span = maybe_fn_sig.map(|fn_sig| fn_sig.span).filter(|&fn_sig_span| { |
312 |
| - let source_map = tcx.sess.source_map(); |
313 |
| - let file_idx = |span: Span| source_map.lookup_source_file_idx(span.lo()); |
314 |
| - |
315 |
| - fn_sig_span.eq_ctxt(body_span) |
316 |
| - && fn_sig_span.hi() <= body_span.lo() |
317 |
| - && file_idx(fn_sig_span) == file_idx(body_span) |
318 |
| - }); |
319 |
| - |
320 |
| - let function_source_hash = hash_mir_source(tcx, hir_body); |
321 |
| - |
322 |
| - let hole_spans = extract_hole_spans_from_hir(tcx, hir_body); |
323 |
| - |
324 |
| - ExtractedHirInfo { function_source_hash, is_async_fn, fn_sig_span, body_span, hole_spans } |
325 |
| -} |
326 |
| - |
327 |
| -fn hash_mir_source<'tcx>(tcx: TyCtxt<'tcx>, hir_body: &'tcx hir::Body<'tcx>) -> u64 { |
328 |
| - // FIXME(cjgillot) Stop hashing HIR manually here. |
329 |
| - let owner = hir_body.id().hir_id.owner; |
330 |
| - tcx.hir_owner_nodes(owner).opt_hash_including_bodies.unwrap().to_smaller_hash().as_u64() |
331 |
| -} |
332 |
| - |
333 |
| -fn extract_hole_spans_from_hir<'tcx>(tcx: TyCtxt<'tcx>, hir_body: &hir::Body<'tcx>) -> Vec<Span> { |
334 |
| - struct HolesVisitor<'tcx> { |
335 |
| - tcx: TyCtxt<'tcx>, |
336 |
| - hole_spans: Vec<Span>, |
337 |
| - } |
338 |
| - |
339 |
| - impl<'tcx> Visitor<'tcx> for HolesVisitor<'tcx> { |
340 |
| - /// We have special handling for nested items, but we still want to |
341 |
| - /// traverse into nested bodies of things that are not considered items, |
342 |
| - /// such as "anon consts" (e.g. array lengths). |
343 |
| - type NestedFilter = nested_filter::OnlyBodies; |
344 |
| - |
345 |
| - fn maybe_tcx(&mut self) -> TyCtxt<'tcx> { |
346 |
| - self.tcx |
347 |
| - } |
348 |
| - |
349 |
| - /// We override `visit_nested_item` instead of `visit_item` because we |
350 |
| - /// only need the item's span, not the item itself. |
351 |
| - fn visit_nested_item(&mut self, id: hir::ItemId) -> Self::Result { |
352 |
| - let span = self.tcx.def_span(id.owner_id.def_id); |
353 |
| - self.visit_hole_span(span); |
354 |
| - // Having visited this item, we don't care about its children, |
355 |
| - // so don't call `walk_item`. |
356 |
| - } |
357 |
| - |
358 |
| - // We override `visit_expr` instead of the more specific expression |
359 |
| - // visitors, so that we have direct access to the expression span. |
360 |
| - fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) { |
361 |
| - match expr.kind { |
362 |
| - hir::ExprKind::Closure(_) | hir::ExprKind::ConstBlock(_) => { |
363 |
| - self.visit_hole_span(expr.span); |
364 |
| - // Having visited this expression, we don't care about its |
365 |
| - // children, so don't call `walk_expr`. |
366 |
| - } |
367 |
| - |
368 |
| - // For other expressions, recursively visit as normal. |
369 |
| - _ => walk_expr(self, expr), |
370 |
| - } |
371 |
| - } |
372 |
| - } |
373 |
| - impl HolesVisitor<'_> { |
374 |
| - fn visit_hole_span(&mut self, hole_span: Span) { |
375 |
| - self.hole_spans.push(hole_span); |
376 |
| - } |
377 |
| - } |
378 |
| - |
379 |
| - let mut visitor = HolesVisitor { tcx, hole_spans: vec![] }; |
380 |
| - |
381 |
| - visitor.visit_body(hir_body); |
382 |
| - visitor.hole_spans |
383 |
| -} |
0 commit comments