Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

coverage: Harmonize the coverage-map and run-coverage test directories #117340

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/tools/compiletest/src/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2193,7 +2193,7 @@ impl<'test> TestCx<'test> {
|| self.is_vxworks_pure_static()
|| self.config.target.contains("bpf")
|| !self.config.target_cfg().dynamic_linking
|| self.config.mode == RunCoverage
|| matches!(self.config.mode, CoverageMap | RunCoverage)
{
// We primarily compile all auxiliary libraries as dynamic libraries
// to avoid code size bloat and large binaries as much as possible
Expand Down Expand Up @@ -2481,9 +2481,9 @@ impl<'test> TestCx<'test> {
RunCoverage => {
rustc.arg("-Cinstrument-coverage");
// Coverage reports are sometimes sensitive to optimizations,
// and the current snapshots assume no optimization unless
// and the current snapshots assume `opt-level=2` unless
// overridden by `compile-flags`.
rustc.arg("-Copt-level=0");
rustc.arg("-Copt-level=2");
}
RunPassValgrind | Pretty | DebugInfo | Codegen | Rustdoc | RustdocJson | RunMake
| CodegenUnits | JsDocTest | Assembly => {
Expand Down
2 changes: 1 addition & 1 deletion tests/coverage-map/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The tests in `./status-quo` were copied from `tests/run-coverage` in order to
Many of these tests were copied from `tests/run-coverage` in order to
capture the current behavior of the instrumentor on non-trivial programs.
The actual mappings have not been closely inspected.

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
22 changes: 22 additions & 0 deletions tests/coverage-map/auxiliary/inline_always_with_dead_code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// compile-flags: -Cinstrument-coverage -Ccodegen-units=4 -Copt-level=0

#![allow(dead_code)]

mod foo {
#[inline(always)]
pub fn called() {}

fn uncalled() {}
}

pub mod bar {
pub fn call_me() {
super::foo::called();
}
}

pub mod baz {
pub fn call_me() {
super::foo::called();
}
}
4 changes: 4 additions & 0 deletions tests/coverage-map/auxiliary/unused_mod_helper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[allow(dead_code)]
pub fn never_called_function() {
println!("I am never called");
}
103 changes: 103 additions & 0 deletions tests/coverage-map/auxiliary/used_crate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#![allow(unused_assignments, unused_variables)]
// Verify that coverage works with optimizations:
// compile-flags: -C opt-level=3

use std::fmt::Debug;

pub fn used_function() {
// Initialize test constants in a way that cannot be determined at compile time, to ensure
// rustc and LLVM cannot optimize out statements (or coverage counters) downstream from
// dependent conditions.
let is_true = std::env::args().len() == 1;
let mut countdown = 0;
if is_true {
countdown = 10;
}
use_this_lib_crate();
}

pub fn used_only_from_bin_crate_generic_function<T: Debug>(arg: T) {
println!("used_only_from_bin_crate_generic_function with {:?}", arg);
}
// Expect for above function: `Unexecuted instantiation` (see below)
pub fn used_only_from_this_lib_crate_generic_function<T: Debug>(arg: T) {
println!("used_only_from_this_lib_crate_generic_function with {:?}", arg);
}

pub fn used_from_bin_crate_and_lib_crate_generic_function<T: Debug>(arg: T) {
println!("used_from_bin_crate_and_lib_crate_generic_function with {:?}", arg);
}

pub fn used_with_same_type_from_bin_crate_and_lib_crate_generic_function<T: Debug>(arg: T) {
println!("used_with_same_type_from_bin_crate_and_lib_crate_generic_function with {:?}", arg);
}

pub fn unused_generic_function<T: Debug>(arg: T) {
println!("unused_generic_function with {:?}", arg);
}

pub fn unused_function() {
let is_true = std::env::args().len() == 1;
let mut countdown = 2;
if !is_true {
countdown = 20;
}
}

#[allow(dead_code)]
fn unused_private_function() {
let is_true = std::env::args().len() == 1;
let mut countdown = 2;
if !is_true {
countdown = 20;
}
}

fn use_this_lib_crate() {
used_from_bin_crate_and_lib_crate_generic_function("used from library used_crate.rs");
used_with_same_type_from_bin_crate_and_lib_crate_generic_function(
"used from library used_crate.rs",
);
let some_vec = vec![5, 6, 7, 8];
used_only_from_this_lib_crate_generic_function(some_vec);
used_only_from_this_lib_crate_generic_function("used ONLY from library used_crate.rs");
}

// FIXME(#79651): "Unexecuted instantiation" errors appear in coverage results,
// for example:
//
// | Unexecuted instantiation: used_crate::used_only_from_bin_crate_generic_function::<_>
//
// These notices appear when `llvm-cov` shows instantiations. This may be a
// default option, but it can be suppressed with:
//
// ```shell
// $ `llvm-cov show --show-instantiations=0 ...`
// ```
//
// The notice is triggered because the function is unused by the library itself,
// and when the library is compiled, a synthetic function is generated, so
// unused function coverage can be reported. Coverage can be skipped for unused
// generic functions with:
//
// ```shell
// $ `rustc -Zunstable-options -C instrument-coverage=except-unused-generics ...`
// ```
//
// Even though this function is used by `uses_crate.rs` (and
// counted), with substitutions for `T`, those instantiations are only generated
// when the generic function is actually used (from the binary, not from this
// library crate). So the test result shows coverage for all instantiated
// versions and their generic type substitutions, plus the `Unexecuted
// instantiation` message for the non-substituted version. This is valid, but
// unfortunately a little confusing.
//
// The library crate has its own coverage map, and the only way to show unused
// coverage of a generic function is to include the generic function in the
// coverage map, marked as an "unused function". If the library were used by
// another binary that never used this generic function, then it would be valid
// to show the unused generic, with unknown substitution (`_`).
//
// The alternative is to exclude all generics from being included in the "unused
// functions" list, which would then omit coverage results for
// `unused_generic_function<T>()`, below.
85 changes: 85 additions & 0 deletions tests/coverage-map/auxiliary/used_inline_crate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#![allow(unused_assignments, unused_variables)]
// Verify that coverage works with optimizations:
// compile-flags: -C opt-level=3

use std::fmt::Debug;

pub fn used_function() {
// Initialize test constants in a way that cannot be determined at compile time, to ensure
// rustc and LLVM cannot optimize out statements (or coverage counters) downstream from
// dependent conditions.
let is_true = std::env::args().len() == 1;
let mut countdown = 0;
if is_true {
countdown = 10;
}
use_this_lib_crate();
}

#[inline(always)]
pub fn used_inline_function() {
// Initialize test constants in a way that cannot be determined at compile time, to ensure
// rustc and LLVM cannot optimize out statements (or coverage counters) downstream from
// dependent conditions.
let is_true = std::env::args().len() == 1;
let mut countdown = 0;
if is_true {
countdown = 10;
}
use_this_lib_crate();
}

#[inline(always)]
pub fn used_only_from_bin_crate_generic_function<T: Debug>(arg: T) {
println!("used_only_from_bin_crate_generic_function with {:?}", arg);
}
// Expect for above function: `Unexecuted instantiation` (see notes in `used_crate.rs`)

#[inline(always)]
pub fn used_only_from_this_lib_crate_generic_function<T: Debug>(arg: T) {
println!("used_only_from_this_lib_crate_generic_function with {:?}", arg);
}

#[inline(always)]
pub fn used_from_bin_crate_and_lib_crate_generic_function<T: Debug>(arg: T) {
println!("used_from_bin_crate_and_lib_crate_generic_function with {:?}", arg);
}

#[inline(always)]
pub fn used_with_same_type_from_bin_crate_and_lib_crate_generic_function<T: Debug>(arg: T) {
println!("used_with_same_type_from_bin_crate_and_lib_crate_generic_function with {:?}", arg);
}

#[inline(always)]
pub fn unused_generic_function<T: Debug>(arg: T) {
println!("unused_generic_function with {:?}", arg);
}

#[inline(always)]
pub fn unused_function() {
let is_true = std::env::args().len() == 1;
let mut countdown = 2;
if !is_true {
countdown = 20;
}
}

#[inline(always)]
#[allow(dead_code)]
fn unused_private_function() {
let is_true = std::env::args().len() == 1;
let mut countdown = 2;
if !is_true {
countdown = 20;
}
}

fn use_this_lib_crate() {
used_from_bin_crate_and_lib_crate_generic_function("used from library used_crate.rs");
used_with_same_type_from_bin_crate_and_lib_crate_generic_function(
"used from library used_crate.rs",
);
let some_vec = vec![5, 6, 7, 8];
used_only_from_this_lib_crate_generic_function(some_vec);
used_only_from_this_lib_crate_generic_function("used ONLY from library used_crate.rs");
}
File renamed without changes.
10 changes: 5 additions & 5 deletions tests/coverage-map/if.cov-map
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
Function name: if::main
Raw bytes (28): 0x[01, 01, 02, 01, 05, 05, 02, 04, 01, 03, 01, 02, 0c, 05, 02, 0d, 02, 06, 02, 02, 06, 00, 07, 07, 01, 05, 01, 02]
Raw bytes (28): 0x[01, 01, 02, 01, 05, 05, 02, 04, 01, 03, 01, 12, 10, 05, 13, 05, 05, 06, 02, 05, 06, 00, 07, 07, 01, 01, 00, 02]
Number of files: 1
- file 0 => global file 1
Number of expressions: 2
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
- expression 1 operands: lhs = Counter(1), rhs = Expression(0, Sub)
Number of file 0 mappings: 4
- Code(Counter(0)) at (prev + 3, 1) to (start + 2, 12)
- Code(Counter(1)) at (prev + 2, 13) to (start + 2, 6)
- Code(Expression(0, Sub)) at (prev + 2, 6) to (start + 0, 7)
- Code(Counter(0)) at (prev + 3, 1) to (start + 18, 16)
- Code(Counter(1)) at (prev + 19, 5) to (start + 5, 6)
- Code(Expression(0, Sub)) at (prev + 5, 6) to (start + 0, 7)
= (c0 - c1)
- Code(Expression(1, Add)) at (prev + 1, 5) to (start + 1, 2)
- Code(Expression(1, Add)) at (prev + 1, 1) to (start + 0, 2)
= (c1 + (c0 - c1))

29 changes: 24 additions & 5 deletions tests/coverage-map/if.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
// compile-flags: --edition=2021
#![allow(unused_assignments, unused_variables)]

fn main() {
let cond = std::env::args().len() == 1;
if cond {
println!("true");
// Initialize test constants in a way that cannot be determined at compile time, to ensure
// rustc and LLVM cannot optimize out statements (or coverage counters) downstream from
// dependent conditions.
let
is_true
=
std::env::args().len()
==
1
;
let
mut
countdown
=
0
;
if
is_true
{
countdown
=
10
;
}
println!("done");
}
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions tests/coverage-map/issue-85461.cov-map
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Function name: issue_85461::main
Raw bytes (9): 0x[01, 01, 00, 01, 01, 08, 01, 03, 02]
Number of files: 1
- file 0 => global file 1
Number of expressions: 0
Number of file 0 mappings: 1
- Code(Counter(0)) at (prev + 8, 1) to (start + 3, 2)

11 changes: 11 additions & 0 deletions tests/coverage-map/issue-85461.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Regression test for #85461: MSVC sometimes fail to link with dead code and #[inline(always)]

// aux-build:inline_always_with_dead_code.rs
extern crate inline_always_with_dead_code;

use inline_always_with_dead_code::{bar, baz};

fn main() {
bar::call_me();
baz::call_me();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Function name: overflow::main
Raw bytes (65): 0x[01, 01, 08, 01, 1b, 05, 1f, 09, 0d, 03, 11, 16, 05, 03, 11, 05, 1f, 09, 0d, 09, 01, 0f, 01, 01, 1b, 03, 02, 0b, 00, 18, 16, 01, 0c, 00, 1a, 05, 00, 1b, 03, 0a, 12, 03, 13, 00, 20, 09, 00, 21, 03, 0a, 0d, 03, 0a, 00, 0b, 1b, 01, 09, 00, 17, 11, 02, 05, 01, 02]
Raw bytes (65): 0x[01, 01, 08, 01, 1b, 05, 1f, 09, 0d, 03, 11, 16, 05, 03, 11, 05, 1f, 09, 0d, 09, 01, 10, 01, 01, 1b, 03, 02, 0b, 00, 18, 16, 01, 0c, 00, 1a, 05, 00, 1b, 03, 0a, 12, 03, 13, 00, 20, 09, 00, 21, 03, 0a, 0d, 03, 0a, 00, 0b, 1b, 01, 09, 00, 17, 11, 02, 05, 01, 02]
Number of files: 1
- file 0 => global file 1
Number of expressions: 8
Expand All @@ -12,7 +12,7 @@ Number of expressions: 8
- expression 6 operands: lhs = Counter(1), rhs = Expression(7, Add)
- expression 7 operands: lhs = Counter(2), rhs = Counter(3)
Number of file 0 mappings: 9
- Code(Counter(0)) at (prev + 15, 1) to (start + 1, 27)
- Code(Counter(0)) at (prev + 16, 1) to (start + 1, 27)
- Code(Expression(0, Add)) at (prev + 2, 11) to (start + 0, 24)
= (c0 + (c1 + (c2 + c3)))
- Code(Expression(5, Sub)) at (prev + 1, 12) to (start + 0, 26)
Expand All @@ -27,14 +27,14 @@ Number of file 0 mappings: 9
- Code(Counter(4)) at (prev + 2, 5) to (start + 1, 2)

Function name: overflow::might_overflow
Raw bytes (28): 0x[01, 01, 02, 01, 05, 05, 02, 04, 01, 04, 01, 01, 12, 05, 01, 13, 02, 06, 02, 02, 06, 00, 07, 07, 01, 09, 05, 02]
Raw bytes (28): 0x[01, 01, 02, 01, 05, 05, 02, 04, 01, 05, 01, 01, 12, 05, 01, 13, 02, 06, 02, 02, 06, 00, 07, 07, 01, 09, 05, 02]
Number of files: 1
- file 0 => global file 1
Number of expressions: 2
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
- expression 1 operands: lhs = Counter(1), rhs = Expression(0, Sub)
Number of file 0 mappings: 4
- Code(Counter(0)) at (prev + 4, 1) to (start + 1, 18)
- Code(Counter(0)) at (prev + 5, 1) to (start + 1, 18)
- Code(Counter(1)) at (prev + 1, 19) to (start + 2, 6)
- Code(Expression(0, Sub)) at (prev + 2, 6) to (start + 0, 7)
= (c0 - c1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![allow(unused_assignments)]
// compile-flags: -Coverflow-checks=yes
// failure-status: 101

fn might_overflow(to_add: u32) -> u32 {
Expand Down
Loading
Loading