Skip to content

Commit afd7a4c

Browse files
authored
Rollup merge of rust-lang#78963 - richkadel:llvm-coverage-counters-2.0.4, r=tmandry
Added some unit tests as requested As discussed in PR rust-lang#78267, for example: * rust-lang#78267 (comment) * rust-lang#78267 (comment) r? ``@tmandry`` FYI: ``@wesleywiser`` This is pretty much self contained, but depending on feedback and timing, I may have a chance to add a few more unit tests requested against `counters.rs`. I'm looking at those now.
2 parents 6c58f45 + b4b0ef3 commit afd7a4c

File tree

11 files changed

+788
-25
lines changed

11 files changed

+788
-25
lines changed

Cargo.lock

+8
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,13 @@ version = "0.8.0"
721721
source = "registry+https://github.com/rust-lang/crates.io-index"
722722
checksum = "9a21fa21941700a3cd8fcb4091f361a6a712fac632f85d9f487cc892045d55c6"
723723

724+
[[package]]
725+
name = "coverage_test_macros"
726+
version = "0.0.0"
727+
dependencies = [
728+
"proc-macro2",
729+
]
730+
724731
[[package]]
725732
name = "cpuid-bool"
726733
version = "0.1.2"
@@ -3922,6 +3929,7 @@ dependencies = [
39223929
name = "rustc_mir"
39233930
version = "0.0.0"
39243931
dependencies = [
3932+
"coverage_test_macros",
39253933
"either",
39263934
"itertools 0.9.0",
39273935
"polonius-engine",

compiler/rustc_middle/src/ty/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,18 @@ pub struct TyS<'tcx> {
611611
outer_exclusive_binder: ty::DebruijnIndex,
612612
}
613613

614+
impl<'tcx> TyS<'tcx> {
615+
/// A constructor used only for internal testing.
616+
#[allow(rustc::usage_of_ty_tykind)]
617+
pub fn make_for_test(
618+
kind: TyKind<'tcx>,
619+
flags: TypeFlags,
620+
outer_exclusive_binder: ty::DebruijnIndex,
621+
) -> TyS<'tcx> {
622+
TyS { kind, flags, outer_exclusive_binder }
623+
}
624+
}
625+
614626
// `TyS` is used a lot. Make sure it doesn't unintentionally get bigger.
615627
#[cfg(target_arch = "x86_64")]
616628
static_assert_size!(TyS<'_>, 32);

compiler/rustc_mir/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,6 @@ rustc_ast = { path = "../rustc_ast" }
3131
rustc_span = { path = "../rustc_span" }
3232
rustc_apfloat = { path = "../rustc_apfloat" }
3333
smallvec = { version = "1.0", features = ["union", "may_dangle"] }
34+
35+
[dev-dependencies]
36+
coverage_test_macros = { path = "src/transform/coverage/test_macros" }

compiler/rustc_mir/src/transform/coverage/counters.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_middle::mir::coverage::*;
1414

1515
/// Manages the counter and expression indexes/IDs to generate `CoverageKind` components for MIR
1616
/// `Coverage` statements.
17-
pub(crate) struct CoverageCounters {
17+
pub(super) struct CoverageCounters {
1818
function_source_hash: u64,
1919
next_counter_id: u32,
2020
num_expressions: u32,
@@ -37,7 +37,7 @@ impl CoverageCounters {
3737
self.debug_counters.enable();
3838
}
3939

40-
/// Makes `CoverageKind` `Counter`s and `Expressions` for the `BasicCoverageBlocks` directly or
40+
/// Makes `CoverageKind` `Counter`s and `Expressions` for the `BasicCoverageBlock`s directly or
4141
/// indirectly associated with `CoverageSpans`, and returns additional `Expression`s
4242
/// representing intermediate values.
4343
pub fn make_bcb_counters(
@@ -120,7 +120,6 @@ struct BcbCounters<'a> {
120120
basic_coverage_blocks: &'a mut CoverageGraph,
121121
}
122122

123-
// FIXME(richkadel): Add unit tests for `BcbCounters` functions/algorithms.
124123
impl<'a> BcbCounters<'a> {
125124
fn new(
126125
coverage_counters: &'a mut CoverageCounters,

compiler/rustc_mir/src/transform/coverage/debug.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub const NESTED_INDENT: &str = " ";
127127

128128
const RUSTC_COVERAGE_DEBUG_OPTIONS: &str = "RUSTC_COVERAGE_DEBUG_OPTIONS";
129129

130-
pub(crate) fn debug_options<'a>() -> &'a DebugOptions {
130+
pub(super) fn debug_options<'a>() -> &'a DebugOptions {
131131
static DEBUG_OPTIONS: SyncOnceCell<DebugOptions> = SyncOnceCell::new();
132132

133133
&DEBUG_OPTIONS.get_or_init(|| DebugOptions::from_env())
@@ -136,7 +136,7 @@ pub(crate) fn debug_options<'a>() -> &'a DebugOptions {
136136
/// Parses and maintains coverage-specific debug options captured from the environment variable
137137
/// "RUSTC_COVERAGE_DEBUG_OPTIONS", if set.
138138
#[derive(Debug, Clone)]
139-
pub(crate) struct DebugOptions {
139+
pub(super) struct DebugOptions {
140140
pub allow_unused_expressions: bool,
141141
counter_format: ExpressionFormat,
142142
}
@@ -250,7 +250,7 @@ impl Default for ExpressionFormat {
250250
///
251251
/// `DebugCounters` supports a recursive rendering of `Expression` counters, so they can be
252252
/// presented as nested expressions such as `(bcb3 - (bcb0 + bcb1))`.
253-
pub(crate) struct DebugCounters {
253+
pub(super) struct DebugCounters {
254254
some_counters: Option<FxHashMap<ExpressionOperandId, DebugCounter>>,
255255
}
256256

@@ -386,7 +386,7 @@ impl DebugCounter {
386386

387387
/// If enabled, this data structure captures additional debugging information used when generating
388388
/// a Graphviz (.dot file) representation of the `CoverageGraph`, for debugging purposes.
389-
pub(crate) struct GraphvizData {
389+
pub(super) struct GraphvizData {
390390
some_bcb_to_coverage_spans_with_counters:
391391
Option<FxHashMap<BasicCoverageBlock, Vec<(CoverageSpan, CoverageKind)>>>,
392392
some_bcb_to_dependency_counters: Option<FxHashMap<BasicCoverageBlock, Vec<CoverageKind>>>,
@@ -496,7 +496,7 @@ impl GraphvizData {
496496
/// directly or indirectly, to compute the coverage counts for all `CoverageSpan`s, and any that are
497497
/// _not_ used are retained in the `unused_expressions` Vec, to be included in debug output (logs
498498
/// and/or a `CoverageGraph` graphviz output).
499-
pub(crate) struct UsedExpressions {
499+
pub(super) struct UsedExpressions {
500500
some_used_expression_operands:
501501
Option<FxHashMap<ExpressionOperandId, Vec<InjectedExpressionId>>>,
502502
some_unused_expressions:
@@ -626,7 +626,7 @@ impl UsedExpressions {
626626
}
627627

628628
/// Generates the MIR pass `CoverageSpan`-specific spanview dump file.
629-
pub(crate) fn dump_coverage_spanview(
629+
pub(super) fn dump_coverage_spanview(
630630
tcx: TyCtxt<'tcx>,
631631
mir_body: &mir::Body<'tcx>,
632632
basic_coverage_blocks: &CoverageGraph,
@@ -666,7 +666,7 @@ fn span_viewables(
666666
}
667667

668668
/// Generates the MIR pass coverage-specific graphviz dump file.
669-
pub(crate) fn dump_coverage_graphviz(
669+
pub(super) fn dump_coverage_graphviz(
670670
tcx: TyCtxt<'tcx>,
671671
mir_body: &mir::Body<'tcx>,
672672
pass_name: &str,
@@ -815,7 +815,7 @@ fn bcb_to_string_sections(
815815

816816
/// Returns a simple string representation of a `TerminatorKind` variant, indenpendent of any
817817
/// values it might hold.
818-
pub(crate) fn term_type(kind: &TerminatorKind<'tcx>) -> &'static str {
818+
pub(super) fn term_type(kind: &TerminatorKind<'tcx>) -> &'static str {
819819
match kind {
820820
TerminatorKind::Goto { .. } => "Goto",
821821
TerminatorKind::SwitchInt { .. } => "SwitchInt",

compiler/rustc_mir/src/transform/coverage/graph.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ const ID_SEPARATOR: &str = ",";
1717
/// `CoverageKind` counter (to be added by `CoverageCounters::make_bcb_counters`), and an optional
1818
/// set of additional counters--if needed--to count incoming edges, if there are more than one.
1919
/// (These "edge counters" are eventually converted into new MIR `BasicBlock`s.)
20-
pub(crate) struct CoverageGraph {
20+
#[derive(Debug)]
21+
pub(super) struct CoverageGraph {
2122
bcbs: IndexVec<BasicCoverageBlock, BasicCoverageBlockData>,
2223
bb_to_bcb: IndexVec<BasicBlock, Option<BasicCoverageBlock>>,
2324
pub successors: IndexVec<BasicCoverageBlock, Vec<BasicCoverageBlock>>,
@@ -275,7 +276,7 @@ impl graph::WithPredecessors for CoverageGraph {
275276

276277
rustc_index::newtype_index! {
277278
/// A node in the [control-flow graph][CFG] of CoverageGraph.
278-
pub(crate) struct BasicCoverageBlock {
279+
pub(super) struct BasicCoverageBlock {
279280
DEBUG_FORMAT = "bcb{}",
280281
}
281282
}
@@ -305,7 +306,7 @@ rustc_index::newtype_index! {
305306
/// queries (`is_dominated_by()`, `predecessors`, `successors`, etc.) have branch (control flow)
306307
/// significance.
307308
#[derive(Debug, Clone)]
308-
pub(crate) struct BasicCoverageBlockData {
309+
pub(super) struct BasicCoverageBlockData {
309310
pub basic_blocks: Vec<BasicBlock>,
310311
pub counter_kind: Option<CoverageKind>,
311312
edge_from_bcbs: Option<FxHashMap<BasicCoverageBlock, CoverageKind>>,
@@ -431,7 +432,7 @@ impl BasicCoverageBlockData {
431432
/// the specific branching BCB, representing the edge between the two. The latter case
432433
/// distinguishes this incoming edge from other incoming edges to the same `target_bcb`.
433434
#[derive(Clone, Copy, PartialEq, Eq)]
434-
pub(crate) struct BcbBranch {
435+
pub(super) struct BcbBranch {
435436
pub edge_from_bcb: Option<BasicCoverageBlock>,
436437
pub target_bcb: BasicCoverageBlock,
437438
}
@@ -498,9 +499,8 @@ fn bcb_filtered_successors<'a, 'tcx>(
498499
/// Maintains separate worklists for each loop in the BasicCoverageBlock CFG, plus one for the
499500
/// CoverageGraph outside all loops. This supports traversing the BCB CFG in a way that
500501
/// ensures a loop is completely traversed before processing Blocks after the end of the loop.
501-
// FIXME(richkadel): Add unit tests for TraversalContext.
502502
#[derive(Debug)]
503-
pub(crate) struct TraversalContext {
503+
pub(super) struct TraversalContext {
504504
/// From one or more backedges returning to a loop header.
505505
pub loop_backedges: Option<(Vec<BasicCoverageBlock>, BasicCoverageBlock)>,
506506

@@ -510,7 +510,7 @@ pub(crate) struct TraversalContext {
510510
pub worklist: Vec<BasicCoverageBlock>,
511511
}
512512

513-
pub(crate) struct TraverseCoverageGraphWithLoops {
513+
pub(super) struct TraverseCoverageGraphWithLoops {
514514
pub backedges: IndexVec<BasicCoverageBlock, Vec<BasicCoverageBlock>>,
515515
pub context_stack: Vec<TraversalContext>,
516516
visited: BitSet<BasicCoverageBlock>,
@@ -642,7 +642,7 @@ impl TraverseCoverageGraphWithLoops {
642642
}
643643
}
644644

645-
fn find_loop_backedges(
645+
pub(super) fn find_loop_backedges(
646646
basic_coverage_blocks: &CoverageGraph,
647647
) -> IndexVec<BasicCoverageBlock, Vec<BasicCoverageBlock>> {
648648
let num_bcbs = basic_coverage_blocks.num_nodes();

compiler/rustc_mir/src/transform/coverage/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ mod debug;
55
mod graph;
66
mod spans;
77

8+
#[cfg(test)]
9+
mod tests;
10+
811
use counters::CoverageCounters;
912
use graph::{BasicCoverageBlock, BasicCoverageBlockData, CoverageGraph};
1013
use spans::{CoverageSpan, CoverageSpans};
@@ -31,7 +34,7 @@ use rustc_span::{CharPos, Pos, SourceFile, Span, Symbol};
3134

3235
/// A simple error message wrapper for `coverage::Error`s.
3336
#[derive(Debug)]
34-
pub(crate) struct Error {
37+
struct Error {
3538
message: String,
3639
}
3740

compiler/rustc_mir/src/transform/coverage/spans.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_span::{BytePos, Span, SyntaxContext};
1717
use std::cmp::Ordering;
1818

1919
#[derive(Debug, Copy, Clone)]
20-
pub(crate) enum CoverageStatement {
20+
pub(super) enum CoverageStatement {
2121
Statement(BasicBlock, Span, usize),
2222
Terminator(BasicBlock, Span),
2323
}
@@ -66,7 +66,7 @@ impl CoverageStatement {
6666
/// or is subsumed by the `Span` associated with this `CoverageSpan`, and it's `BasicBlock`
6767
/// `is_dominated_by()` the `BasicBlock`s in this `CoverageSpan`.
6868
#[derive(Debug, Clone)]
69-
pub(crate) struct CoverageSpan {
69+
pub(super) struct CoverageSpan {
7070
pub span: Span,
7171
pub bcb: BasicCoverageBlock,
7272
pub coverage_statements: Vec<CoverageStatement>,
@@ -214,7 +214,7 @@ pub struct CoverageSpans<'a, 'tcx> {
214214
}
215215

216216
impl<'a, 'tcx> CoverageSpans<'a, 'tcx> {
217-
pub(crate) fn generate_coverage_spans(
217+
pub(super) fn generate_coverage_spans(
218218
mir_body: &'a mir::Body<'tcx>,
219219
body_span: Span,
220220
basic_coverage_blocks: &'a CoverageGraph,
@@ -645,7 +645,10 @@ impl<'a, 'tcx> CoverageSpans<'a, 'tcx> {
645645
}
646646
}
647647

648-
fn filtered_statement_span(statement: &'a Statement<'tcx>, body_span: Span) -> Option<Span> {
648+
pub(super) fn filtered_statement_span(
649+
statement: &'a Statement<'tcx>,
650+
body_span: Span,
651+
) -> Option<Span> {
649652
match statement.kind {
650653
// These statements have spans that are often outside the scope of the executed source code
651654
// for their parent `BasicBlock`.
@@ -686,7 +689,10 @@ fn filtered_statement_span(statement: &'a Statement<'tcx>, body_span: Span) -> O
686689
}
687690
}
688691

689-
fn filtered_terminator_span(terminator: &'a Terminator<'tcx>, body_span: Span) -> Option<Span> {
692+
pub(super) fn filtered_terminator_span(
693+
terminator: &'a Terminator<'tcx>,
694+
body_span: Span,
695+
) -> Option<Span> {
690696
match terminator.kind {
691697
// These terminators have spans that don't positively contribute to computing a reasonable
692698
// span of actually executed source code. (For example, SwitchInt terminators extracted from
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
authors = ["The Rust Project Developers"]
3+
name = "coverage_test_macros"
4+
version = "0.0.0"
5+
edition = "2018"
6+
7+
[lib]
8+
proc-macro = true
9+
doctest = false
10+
11+
[dependencies]
12+
proc-macro2 = "1"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use proc_macro::TokenStream;
2+
3+
#[proc_macro]
4+
pub fn let_bcb(item: TokenStream) -> TokenStream {
5+
format!("let bcb{} = graph::BasicCoverageBlock::from_usize({});", item, item).parse().unwrap()
6+
}

0 commit comments

Comments
 (0)