1 //! The `InstrumentCoverage` MIR pass implementation includes debugging tools and options
2 //! to help developers understand and/or improve the analysis and instrumentation of a MIR.
4 //! To enable coverage, include the rustc command line option:
6 //! * `-Z instrument-coverage`
8 //! MIR Dump Files, with additional `CoverageGraph` graphviz and `CoverageSpan` spanview
9 //! ------------------------------------------------------------------------------------
11 //! Additional debugging options include:
13 //! * `-Z dump-mir=InstrumentCoverage` - Generate `.mir` files showing the state of the MIR,
14 //! before and after the `InstrumentCoverage` pass, for each compiled function.
16 //! * `-Z dump-mir-graphviz` - If `-Z dump-mir` is also enabled for the current MIR node path,
17 //! each MIR dump is accompanied by a before-and-after graphical view of the MIR, in Graphviz
18 //! `.dot` file format (which can be visually rendered as a graph using any of a number of free
19 //! Graphviz viewers and IDE extensions).
21 //! For the `InstrumentCoverage` pass, this option also enables generation of an additional
22 //! Graphviz `.dot` file for each function, rendering the `CoverageGraph`: the control flow
23 //! graph (CFG) of `BasicCoverageBlocks` (BCBs), as nodes, internally labeled to show the
24 //! `CoverageSpan`-based MIR elements each BCB represents (`BasicBlock`s, `Statement`s and
25 //! `Terminator`s), assigned coverage counters and/or expressions, and edge counters, as needed.
27 //! (Note the additional option, `-Z graphviz-dark-mode`, can be added, to change the rendered
28 //! output from its default black-on-white background to a dark color theme, if desired.)
30 //! * `-Z dump-mir-spanview` - If `-Z dump-mir` is also enabled for the current MIR node path,
31 //! each MIR dump is accompanied by a before-and-after `.html` document showing the function's
32 //! original source code, highlighted by it's MIR spans, at the `statement`-level (by default),
33 //! `terminator` only, or encompassing span for the `Terminator` plus all `Statement`s, in each
34 //! `block` (`BasicBlock`).
36 //! For the `InstrumentCoverage` pass, this option also enables generation of an additional
37 //! spanview `.html` file for each function, showing the aggregated `CoverageSpan`s that will
38 //! require counters (or counter expressions) for accurate coverage analysis.
43 //! The `InstrumentCoverage` pass includes debug logging messages at various phases and decision
44 //! points, which can be enabled via environment variable:
47 //! RUSTC_LOG=rustc_mir_transform::transform::coverage=debug
50 //! Other module paths with coverage-related debug logs may also be of interest, particularly for
51 //! debugging the coverage map data, injected as global variables in the LLVM IR (during rustc's
52 //! code generation pass). For example:
55 //! RUSTC_LOG=rustc_mir_transform::transform::coverage,rustc_codegen_ssa::coverageinfo,rustc_codegen_llvm::coverageinfo=debug
58 //! Coverage Debug Options
59 //! ---------------------------------
61 //! Additional debugging options can be enabled using the environment variable:
64 //! RUSTC_COVERAGE_DEBUG_OPTIONS=<options>
67 //! These options are comma-separated, and specified in the format `option-name=value`. For example:
70 //! $ RUSTC_COVERAGE_DEBUG_OPTIONS=counter-format=id+operation,allow-unused-expressions=yes cargo build
73 //! Coverage debug options include:
75 //! * `allow-unused-expressions=yes` or `no` (default: `no`)
77 //! The `InstrumentCoverage` algorithms _should_ only create and assign expressions to a
78 //! `BasicCoverageBlock`, or an incoming edge, if that expression is either (a) required to
79 //! count a `CoverageSpan`, or (b) a dependency of some other required counter expression.
81 //! If an expression is generated that does not map to a `CoverageSpan` or dependency, this
82 //! probably indicates there was a bug in the algorithm that creates and assigns counters
85 //! When this kind of bug is encountered, the rustc compiler will panic by default. Setting:
86 //! `allow-unused-expressions=yes` will log a warning message instead of panicking (effectively
87 //! ignoring the unused expressions), which may be helpful when debugging the root cause of
90 //! * `counter-format=<choices>`, where `<choices>` can be any plus-separated combination of `id`,
91 //! `block`, and/or `operation` (default: `block+operation`)
93 //! This option effects both the `CoverageGraph` (graphviz `.dot` files) and debug logging, when
94 //! generating labels for counters and expressions.
96 //! Depending on the values and combinations, counters can be labeled by:
98 //! * `id` - counter or expression ID (ascending counter IDs, starting at 1, or descending
99 //! expression IDs, starting at `u32:MAX`)
100 //! * `block` - the `BasicCoverageBlock` label (for example, `bcb0`) or edge label (for
101 //! example `bcb0->bcb1`), for counters or expressions assigned to count a
102 //! `BasicCoverageBlock` or edge. Intermediate expressions (not directly associated with
103 //! a BCB or edge) will be labeled by their expression ID, unless `operation` is also
105 //! * `operation` - applied to expressions only, labels include the left-hand-side counter
106 //! or expression label (lhs operand), the operator (`+` or `-`), and the right-hand-side
107 //! counter or expression (rhs operand). Expression operand labels are generated
108 //! recursively, generating labels with nested operations, enclosed in parentheses
109 //! (for example: `bcb2 + (bcb0 - bcb1)`).
111 use super::graph
::{BasicCoverageBlock, BasicCoverageBlockData, CoverageGraph}
;
112 use super::spans
::CoverageSpan
;
114 use rustc_middle
::mir
::create_dump_file
;
115 use rustc_middle
::mir
::generic_graphviz
::GraphvizWriter
;
116 use rustc_middle
::mir
::spanview
::{self, SpanViewable}
;
118 use rustc_data_structures
::fx
::FxHashMap
;
119 use rustc_middle
::mir
::coverage
::*;
120 use rustc_middle
::mir
::{self, BasicBlock, TerminatorKind}
;
121 use rustc_middle
::ty
::TyCtxt
;
122 use rustc_span
::Span
;
125 use std
::lazy
::SyncOnceCell
;
127 pub const NESTED_INDENT
: &str = " ";
129 const RUSTC_COVERAGE_DEBUG_OPTIONS
: &str = "RUSTC_COVERAGE_DEBUG_OPTIONS";
131 pub(super) fn debug_options
<'a
>() -> &'a DebugOptions
{
132 static DEBUG_OPTIONS
: SyncOnceCell
<DebugOptions
> = SyncOnceCell
::new();
134 &DEBUG_OPTIONS
.get_or_init(DebugOptions
::from_env
)
137 /// Parses and maintains coverage-specific debug options captured from the environment variable
138 /// "RUSTC_COVERAGE_DEBUG_OPTIONS", if set.
139 #[derive(Debug, Clone)]
140 pub(super) struct DebugOptions
{
141 pub allow_unused_expressions
: bool
,
142 counter_format
: ExpressionFormat
,
146 fn from_env() -> Self {
147 let mut allow_unused_expressions
= true;
148 let mut counter_format
= ExpressionFormat
::default();
150 if let Ok(env_debug_options
) = std
::env
::var(RUSTC_COVERAGE_DEBUG_OPTIONS
) {
151 for setting_str
in env_debug_options
.replace(' '
, "").replace('
-'
, "_").split('
,'
) {
152 let (option
, value
) = match setting_str
.split_once('
='
) {
153 None
=> (setting_str
, None
),
154 Some((k
, v
)) => (k
, Some(v
)),
157 "allow_unused_expressions" => {
158 allow_unused_expressions
= bool_option_val(option
, value
);
160 "{} env option `allow_unused_expressions` is set to {}",
161 RUSTC_COVERAGE_DEBUG_OPTIONS
, allow_unused_expressions
164 "counter_format" => {
168 "`{}` option in environment variable {} requires one or more \
169 plus-separated choices (a non-empty subset of \
170 `id+block+operation`)",
172 RUSTC_COVERAGE_DEBUG_OPTIONS
176 counter_format
= counter_format_option_val(val
);
178 "{} env option `counter_format` is set to {:?}",
179 RUSTC_COVERAGE_DEBUG_OPTIONS
, counter_format
185 "Unsupported setting `{}` in environment variable {}",
187 RUSTC_COVERAGE_DEBUG_OPTIONS
193 Self { allow_unused_expressions, counter_format }
197 fn bool_option_val(option
: &str, some_strval
: Option
<&str>) -> bool
{
198 if let Some(val
) = some_strval
{
199 if vec
!["yes", "y", "on", "true"].contains(&val
) {
201 } else if vec
!["no", "n", "off", "false"].contains(&val
) {
205 "Unsupported value `{}` for option `{}` in environment variable {}",
208 RUSTC_COVERAGE_DEBUG_OPTIONS
216 fn counter_format_option_val(strval
: &str) -> ExpressionFormat
{
217 let mut counter_format
= ExpressionFormat { id: false, block: false, operation: false }
;
218 let components
= strval
.splitn(3, '
+'
);
219 for component
in components
{
221 "id" => counter_format
.id
= true,
222 "block" => counter_format
.block
= true,
223 "operation" => counter_format
.operation
= true,
225 "Unsupported counter_format choice `{}` in environment variable {}",
227 RUSTC_COVERAGE_DEBUG_OPTIONS
234 #[derive(Debug, Clone)]
235 struct ExpressionFormat
{
241 impl Default
for ExpressionFormat
{
242 fn default() -> Self {
243 Self { id: false, block: true, operation: true }
247 /// If enabled, this struct maintains a map from `CoverageKind` IDs (as `ExpressionOperandId`) to
248 /// the `CoverageKind` data and optional label (normally, the counter's associated
249 /// `BasicCoverageBlock` format string, if any).
251 /// Use `format_counter` to convert one of these `CoverageKind` counters to a debug output string,
252 /// as directed by the `DebugOptions`. This allows the format of counter labels in logs and dump
253 /// files (including the `CoverageGraph` graphviz file) to be changed at runtime, via environment
256 /// `DebugCounters` supports a recursive rendering of `Expression` counters, so they can be
257 /// presented as nested expressions such as `(bcb3 - (bcb0 + bcb1))`.
258 pub(super) struct DebugCounters
{
259 some_counters
: Option
<FxHashMap
<ExpressionOperandId
, DebugCounter
>>,
263 pub fn new() -> Self {
264 Self { some_counters: None }
267 pub fn enable(&mut self) {
268 debug_assert
!(!self.is_enabled());
269 self.some_counters
.replace(FxHashMap
::default());
272 pub fn is_enabled(&self) -> bool
{
273 self.some_counters
.is_some()
276 pub fn add_counter(&mut self, counter_kind
: &CoverageKind
, some_block_label
: Option
<String
>) {
277 if let Some(counters
) = &mut self.some_counters
{
278 let id
: ExpressionOperandId
= match *counter_kind
{
279 CoverageKind
::Counter { id, .. }
=> id
.into(),
280 CoverageKind
::Expression { id, .. }
=> id
.into(),
282 "the given `CoverageKind` is not an counter or expression: {:?}",
287 .try_insert(id
, DebugCounter
::new(counter_kind
.clone(), some_block_label
))
288 .expect("attempt to add the same counter_kind to DebugCounters more than once");
292 pub fn some_block_label(&self, operand
: ExpressionOperandId
) -> Option
<&String
> {
293 self.some_counters
.as_ref().map_or(None
, |counters
| {
296 .map_or(None
, |debug_counter
| debug_counter
.some_block_label
.as_ref())
300 pub fn format_counter(&self, counter_kind
: &CoverageKind
) -> String
{
301 match *counter_kind
{
302 CoverageKind
::Counter { .. }
=> {
303 format
!("Counter({})", self.format_counter_kind(counter_kind
))
305 CoverageKind
::Expression { .. }
=> {
306 format
!("Expression({})", self.format_counter_kind(counter_kind
))
308 CoverageKind
::Unreachable { .. }
=> "Unreachable".to_owned(),
312 fn format_counter_kind(&self, counter_kind
: &CoverageKind
) -> String
{
313 let counter_format
= &debug_options().counter_format
;
314 if let CoverageKind
::Expression { id, lhs, op, rhs }
= *counter_kind
{
315 if counter_format
.operation
{
318 if counter_format
.id
|| self.some_counters
.is_none() {
319 format
!("#{} = ", id
.index())
323 self.format_operand(lhs
),
324 if op
== Op
::Add { "+" }
else { "-" }
,
325 self.format_operand(rhs
),
330 let id
: ExpressionOperandId
= match *counter_kind
{
331 CoverageKind
::Counter { id, .. }
=> id
.into(),
332 CoverageKind
::Expression { id, .. }
=> id
.into(),
334 bug
!("the given `CoverageKind` is not an counter or expression: {:?}", counter_kind
)
337 if self.some_counters
.is_some() && (counter_format
.block
|| !counter_format
.id
) {
338 let counters
= self.some_counters
.as_ref().unwrap();
339 if let Some(DebugCounter { some_block_label: Some(block_label), .. }
) =
342 return if counter_format
.id
{
343 format
!("{}#{}", block_label
, id
.index())
345 block_label
.to_string()
349 format
!("#{}", id
.index())
352 fn format_operand(&self, operand
: ExpressionOperandId
) -> String
{
353 if operand
.index() == 0 {
354 return String
::from("0");
356 if let Some(counters
) = &self.some_counters
{
357 if let Some(DebugCounter { counter_kind, some_block_label }
) = counters
.get(&operand
) {
358 if let CoverageKind
::Expression { .. }
= counter_kind
{
359 if let Some(block_label
) = some_block_label
{
360 if debug_options().counter_format
.block
{
364 self.format_counter_kind(counter_kind
)
368 return format
!("({})", self.format_counter_kind(counter_kind
));
370 return self.format_counter_kind(counter_kind
);
373 format
!("#{}", operand
.index())
377 /// A non-public support class to `DebugCounters`.
379 struct DebugCounter
{
380 counter_kind
: CoverageKind
,
381 some_block_label
: Option
<String
>,
385 fn new(counter_kind
: CoverageKind
, some_block_label
: Option
<String
>) -> Self {
386 Self { counter_kind, some_block_label }
390 /// If enabled, this data structure captures additional debugging information used when generating
391 /// a Graphviz (.dot file) representation of the `CoverageGraph`, for debugging purposes.
392 pub(super) struct GraphvizData
{
393 some_bcb_to_coverage_spans_with_counters
:
394 Option
<FxHashMap
<BasicCoverageBlock
, Vec
<(CoverageSpan
, CoverageKind
)>>>,
395 some_bcb_to_dependency_counters
: Option
<FxHashMap
<BasicCoverageBlock
, Vec
<CoverageKind
>>>,
396 some_edge_to_counter
: Option
<FxHashMap
<(BasicCoverageBlock
, BasicBlock
), CoverageKind
>>,
400 pub fn new() -> Self {
402 some_bcb_to_coverage_spans_with_counters
: None
,
403 some_bcb_to_dependency_counters
: None
,
404 some_edge_to_counter
: None
,
408 pub fn enable(&mut self) {
409 debug_assert
!(!self.is_enabled());
410 self.some_bcb_to_coverage_spans_with_counters
= Some(FxHashMap
::default());
411 self.some_bcb_to_dependency_counters
= Some(FxHashMap
::default());
412 self.some_edge_to_counter
= Some(FxHashMap
::default());
415 pub fn is_enabled(&self) -> bool
{
416 self.some_bcb_to_coverage_spans_with_counters
.is_some()
419 pub fn add_bcb_coverage_span_with_counter(
421 bcb
: BasicCoverageBlock
,
422 coverage_span
: &CoverageSpan
,
423 counter_kind
: &CoverageKind
,
425 if let Some(bcb_to_coverage_spans_with_counters
) =
426 self.some_bcb_to_coverage_spans_with_counters
.as_mut()
428 bcb_to_coverage_spans_with_counters
430 .or_insert_with(Vec
::new
)
431 .push((coverage_span
.clone(), counter_kind
.clone()));
435 pub fn get_bcb_coverage_spans_with_counters(
437 bcb
: BasicCoverageBlock
,
438 ) -> Option
<&Vec
<(CoverageSpan
, CoverageKind
)>> {
439 if let Some(bcb_to_coverage_spans_with_counters
) =
440 self.some_bcb_to_coverage_spans_with_counters
.as_ref()
442 bcb_to_coverage_spans_with_counters
.get(&bcb
)
448 pub fn add_bcb_dependency_counter(
450 bcb
: BasicCoverageBlock
,
451 counter_kind
: &CoverageKind
,
453 if let Some(bcb_to_dependency_counters
) = self.some_bcb_to_dependency_counters
.as_mut() {
454 bcb_to_dependency_counters
456 .or_insert_with(Vec
::new
)
457 .push(counter_kind
.clone());
461 pub fn get_bcb_dependency_counters(
463 bcb
: BasicCoverageBlock
,
464 ) -> Option
<&Vec
<CoverageKind
>> {
465 if let Some(bcb_to_dependency_counters
) = self.some_bcb_to_dependency_counters
.as_ref() {
466 bcb_to_dependency_counters
.get(&bcb
)
472 pub fn set_edge_counter(
474 from_bcb
: BasicCoverageBlock
,
476 counter_kind
: &CoverageKind
,
478 if let Some(edge_to_counter
) = self.some_edge_to_counter
.as_mut() {
480 .try_insert((from_bcb
, to_bb
), counter_kind
.clone())
481 .expect("invalid attempt to insert more than one edge counter for the same edge");
485 pub fn get_edge_counter(
487 from_bcb
: BasicCoverageBlock
,
489 ) -> Option
<&CoverageKind
> {
490 if let Some(edge_to_counter
) = self.some_edge_to_counter
.as_ref() {
491 edge_to_counter
.get(&(from_bcb
, to_bb
))
498 /// If enabled, this struct captures additional data used to track whether expressions were used,
499 /// directly or indirectly, to compute the coverage counts for all `CoverageSpan`s, and any that are
500 /// _not_ used are retained in the `unused_expressions` Vec, to be included in debug output (logs
501 /// and/or a `CoverageGraph` graphviz output).
502 pub(super) struct UsedExpressions
{
503 some_used_expression_operands
:
504 Option
<FxHashMap
<ExpressionOperandId
, Vec
<InjectedExpressionId
>>>,
505 some_unused_expressions
:
506 Option
<Vec
<(CoverageKind
, Option
<BasicCoverageBlock
>, BasicCoverageBlock
)>>,
509 impl UsedExpressions
{
510 pub fn new() -> Self {
511 Self { some_used_expression_operands: None, some_unused_expressions: None }
514 pub fn enable(&mut self) {
515 debug_assert
!(!self.is_enabled());
516 self.some_used_expression_operands
= Some(FxHashMap
::default());
517 self.some_unused_expressions
= Some(Vec
::new());
520 pub fn is_enabled(&self) -> bool
{
521 self.some_used_expression_operands
.is_some()
524 pub fn add_expression_operands(&mut self, expression
: &CoverageKind
) {
525 if let Some(used_expression_operands
) = self.some_used_expression_operands
.as_mut() {
526 if let CoverageKind
::Expression { id, lhs, rhs, .. }
= *expression
{
527 used_expression_operands
.entry(lhs
).or_insert_with(Vec
::new
).push(id
);
528 used_expression_operands
.entry(rhs
).or_insert_with(Vec
::new
).push(id
);
533 pub fn expression_is_used(&self, expression
: &CoverageKind
) -> bool
{
534 if let Some(used_expression_operands
) = self.some_used_expression_operands
.as_ref() {
535 used_expression_operands
.contains_key(&expression
.as_operand_id())
541 pub fn add_unused_expression_if_not_found(
543 expression
: &CoverageKind
,
544 edge_from_bcb
: Option
<BasicCoverageBlock
>,
545 target_bcb
: BasicCoverageBlock
,
547 if let Some(used_expression_operands
) = self.some_used_expression_operands
.as_ref() {
548 if !used_expression_operands
.contains_key(&expression
.as_operand_id()) {
549 self.some_unused_expressions
.as_mut().unwrap().push((
558 /// Return the list of unused counters (if any) as a tuple with the counter (`CoverageKind`),
559 /// optional `from_bcb` (if it was an edge counter), and `target_bcb`.
560 pub fn get_unused_expressions(
562 ) -> Vec
<(CoverageKind
, Option
<BasicCoverageBlock
>, BasicCoverageBlock
)> {
563 if let Some(unused_expressions
) = self.some_unused_expressions
.as_ref() {
564 unused_expressions
.clone()
570 /// If enabled, validate that every BCB or edge counter not directly associated with a coverage
571 /// span is at least indirectly associated (it is a dependency of a BCB counter that _is_
572 /// associated with a coverage span).
575 bcb_counters_without_direct_coverage_spans
: &Vec
<(
576 Option
<BasicCoverageBlock
>,
581 if self.is_enabled() {
582 let mut not_validated
= bcb_counters_without_direct_coverage_spans
584 .map(|(_
, _
, counter_kind
)| counter_kind
)
585 .collect
::<Vec
<_
>>();
586 let mut validating_count
= 0;
587 while not_validated
.len() != validating_count
{
588 let to_validate
= not_validated
.split_off(0);
589 validating_count
= to_validate
.len();
590 for counter_kind
in to_validate
{
591 if self.expression_is_used(counter_kind
) {
592 self.add_expression_operands(counter_kind
);
594 not_validated
.push(counter_kind
);
601 pub fn alert_on_unused_expressions(&self, debug_counters
: &DebugCounters
) {
602 if let Some(unused_expressions
) = self.some_unused_expressions
.as_ref() {
603 for (counter_kind
, edge_from_bcb
, target_bcb
) in unused_expressions
{
604 let unused_counter_message
= if let Some(from_bcb
) = edge_from_bcb
.as_ref() {
606 "non-coverage edge counter found without a dependent expression, in \
607 {:?}->{:?}; counter={}",
610 debug_counters
.format_counter(&counter_kind
),
614 "non-coverage counter found without a dependent expression, in {:?}; \
617 debug_counters
.format_counter(&counter_kind
),
621 if debug_options().allow_unused_expressions
{
622 debug
!("WARNING: {}", unused_counter_message
);
624 bug
!("{}", unused_counter_message
);
631 /// Generates the MIR pass `CoverageSpan`-specific spanview dump file.
632 pub(super) fn dump_coverage_spanview
<'tcx
>(
634 mir_body
: &mir
::Body
<'tcx
>,
635 basic_coverage_blocks
: &CoverageGraph
,
638 coverage_spans
: &Vec
<CoverageSpan
>,
640 let mir_source
= mir_body
.source
;
641 let def_id
= mir_source
.def_id();
643 let span_viewables
= span_viewables(tcx
, mir_body
, basic_coverage_blocks
, &coverage_spans
);
644 let mut file
= create_dump_file(tcx
, "html", None
, pass_name
, &0, mir_source
)
645 .expect("Unexpected error creating MIR spanview HTML file");
646 let crate_name
= tcx
.crate_name(def_id
.krate
);
647 let item_name
= tcx
.def_path(def_id
).to_filename_friendly_no_crate();
648 let title
= format
!("{}.{} - Coverage Spans", crate_name
, item_name
);
649 spanview
::write_document(tcx
, body_span
, span_viewables
, &title
, &mut file
)
650 .expect("Unexpected IO error dumping coverage spans as HTML");
653 /// Converts the computed `BasicCoverageBlockData`s into `SpanViewable`s.
654 fn span_viewables
<'tcx
>(
656 mir_body
: &mir
::Body
<'tcx
>,
657 basic_coverage_blocks
: &CoverageGraph
,
658 coverage_spans
: &Vec
<CoverageSpan
>,
659 ) -> Vec
<SpanViewable
> {
660 let mut span_viewables
= Vec
::new();
661 for coverage_span
in coverage_spans
{
662 let tooltip
= coverage_span
.format_coverage_statements(tcx
, mir_body
);
663 let CoverageSpan { span, bcb, .. }
= coverage_span
;
664 let bcb_data
= &basic_coverage_blocks
[*bcb
];
665 let id
= bcb_data
.id();
666 let leader_bb
= bcb_data
.leader_bb();
667 span_viewables
.push(SpanViewable { bb: leader_bb, span: *span, id, tooltip }
);
672 /// Generates the MIR pass coverage-specific graphviz dump file.
673 pub(super) fn dump_coverage_graphviz
<'tcx
>(
675 mir_body
: &mir
::Body
<'tcx
>,
677 basic_coverage_blocks
: &CoverageGraph
,
678 debug_counters
: &DebugCounters
,
679 graphviz_data
: &GraphvizData
,
680 intermediate_expressions
: &Vec
<CoverageKind
>,
681 debug_used_expressions
: &UsedExpressions
,
683 let mir_source
= mir_body
.source
;
684 let def_id
= mir_source
.def_id();
685 let node_content
= |bcb
| {
686 bcb_to_string_sections(
690 &basic_coverage_blocks
[bcb
],
691 graphviz_data
.get_bcb_coverage_spans_with_counters(bcb
),
692 graphviz_data
.get_bcb_dependency_counters(bcb
),
693 // intermediate_expressions are injected into the mir::START_BLOCK, so
694 // include them in the first BCB.
695 if bcb
.index() == 0 { Some(&intermediate_expressions) }
else { None }
,
698 let edge_labels
= |from_bcb
| {
699 let from_bcb_data
= &basic_coverage_blocks
[from_bcb
];
700 let from_terminator
= from_bcb_data
.terminator(mir_body
);
701 let mut edge_labels
= from_terminator
.kind
.fmt_successor_labels();
702 edge_labels
.retain(|label
| label
!= "unreachable");
703 let edge_counters
= from_terminator
705 .map(|&successor_bb
| graphviz_data
.get_edge_counter(from_bcb
, successor_bb
));
706 iter
::zip(&edge_labels
, edge_counters
)
707 .map(|(label
, some_counter
)| {
708 if let Some(counter
) = some_counter
{
709 format
!("{}\n{}", label
, debug_counters
.format_counter(counter
))
716 let graphviz_name
= format
!("Cov_{}_{}", def_id
.krate
.index(), def_id
.index
.index());
717 let mut graphviz_writer
=
718 GraphvizWriter
::new(basic_coverage_blocks
, &graphviz_name
, node_content
, edge_labels
);
719 let unused_expressions
= debug_used_expressions
.get_unused_expressions();
720 if unused_expressions
.len() > 0 {
721 graphviz_writer
.set_graph_label(&format
!(
722 "Unused expressions:\n {}",
726 .map(|(counter_kind
, edge_from_bcb
, target_bcb
)| {
727 if let Some(from_bcb
) = edge_from_bcb
.as_ref() {
732 debug_counters
.format_counter(&counter_kind
),
738 debug_counters
.format_counter(&counter_kind
),
746 let mut file
= create_dump_file(tcx
, "dot", None
, pass_name
, &0, mir_source
)
747 .expect("Unexpected error creating BasicCoverageBlock graphviz DOT file");
749 .write_graphviz(tcx
, &mut file
)
750 .expect("Unexpected error writing BasicCoverageBlock graphviz DOT file");
753 fn bcb_to_string_sections
<'tcx
>(
755 mir_body
: &mir
::Body
<'tcx
>,
756 debug_counters
: &DebugCounters
,
757 bcb_data
: &BasicCoverageBlockData
,
758 some_coverage_spans_with_counters
: Option
<&Vec
<(CoverageSpan
, CoverageKind
)>>,
759 some_dependency_counters
: Option
<&Vec
<CoverageKind
>>,
760 some_intermediate_expressions
: Option
<&Vec
<CoverageKind
>>,
762 let len
= bcb_data
.basic_blocks
.len();
763 let mut sections
= Vec
::new();
764 if let Some(collect_intermediate_expressions
) = some_intermediate_expressions
{
766 collect_intermediate_expressions
769 format
!("Intermediate {}", debug_counters
.format_counter(expression
))
775 if let Some(coverage_spans_with_counters
) = some_coverage_spans_with_counters
{
777 coverage_spans_with_counters
779 .map(|(covspan
, counter
)| {
782 debug_counters
.format_counter(counter
),
783 covspan
.format(tcx
, mir_body
)
790 if let Some(dependency_counters
) = some_dependency_counters
{
791 sections
.push(format
!(
792 "Non-coverage counters:\n {}",
795 .map(|counter
| debug_counters
.format_counter(counter
))
800 if let Some(counter_kind
) = &bcb_data
.counter_kind
{
801 sections
.push(format
!("{:?}", counter_kind
));
803 let non_term_blocks
= bcb_data
.basic_blocks
[0..len
- 1]
805 .map(|&bb
| format
!("{:?}: {}", bb
, term_type(&mir_body
[bb
].terminator().kind
)))
806 .collect
::<Vec
<_
>>();
807 if non_term_blocks
.len() > 0 {
808 sections
.push(non_term_blocks
.join("\n"));
810 sections
.push(format
!(
812 bcb_data
.basic_blocks
.last().unwrap(),
813 term_type(&bcb_data
.terminator(mir_body
).kind
)
818 /// Returns a simple string representation of a `TerminatorKind` variant, independent of any
819 /// values it might hold.
820 pub(super) fn term_type(kind
: &TerminatorKind
<'_
>) -> &'
static str {
822 TerminatorKind
::Goto { .. }
=> "Goto",
823 TerminatorKind
::SwitchInt { .. }
=> "SwitchInt",
824 TerminatorKind
::Resume
=> "Resume",
825 TerminatorKind
::Abort
=> "Abort",
826 TerminatorKind
::Return
=> "Return",
827 TerminatorKind
::Unreachable
=> "Unreachable",
828 TerminatorKind
::Drop { .. }
=> "Drop",
829 TerminatorKind
::DropAndReplace { .. }
=> "DropAndReplace",
830 TerminatorKind
::Call { .. }
=> "Call",
831 TerminatorKind
::Assert { .. }
=> "Assert",
832 TerminatorKind
::Yield { .. }
=> "Yield",
833 TerminatorKind
::GeneratorDrop
=> "GeneratorDrop",
834 TerminatorKind
::FalseEdge { .. }
=> "FalseEdge",
835 TerminatorKind
::FalseUnwind { .. }
=> "FalseUnwind",
836 TerminatorKind
::InlineAsm { .. }
=> "InlineAsm",