taiki-e / cargo-llvm-cov

Cargo subcommand to easily use LLVM source-based code coverage (-C instrument-coverage).
Apache License 2.0
863 stars 57 forks source link

Incorrect Rust region coverage? #324

Open wintered opened 7 months ago

wintered commented 7 months ago

Hi,

cargo-llvm-cov reports that the following Rust code had 5 region coverage. Why? I count only 4: (1+2) both functions, (3) the if condition, (4) the "then branch". I also looked at the generated llvm-ir but could not figure out where the fifth region came from.

// main.rs
fn foo (c1: bool) {
    if c1 {
        println!("c1 true");
    }
    println!("join");
}

fn main() {
    foo(true);
}
Screenshot 2023-12-05 at 12 25 59 PM

The corresponding C++ program has 4 regions.

// main.cpp
#include <iostream>
void foo(bool c1) {
    if (c1) {
        std::cout << "c1 true" << std::endl;
    }
    std::cout << "join" << std::endl;
}
int main() {
    foo(true);
}
image

cargo-llvm-cov: 0.5.36 LLVM version 17.0.4

Zalathar commented 7 months ago

The else arm of the if expression gets its own region, even if there is no else in the source file.

In your example program the else arm isn't executed and has an execution count of 0.

In the C++ program, this fact is represented by the Branch Coverage column instead. (Rust currently doesn't support LLVM branch coverage mappings.)

wintered commented 7 months ago

The else arm of the if expression gets its own region, even if there is no else in the source file.

Why in C++, the else branch does not get its own region even though it does in Rust?

taiki-e commented 3 months ago

In Rust with branch coverage enabled (--branch, #8):

issue324-rust-branch-cov issue324-rust-branch-cov-2
taiki-e commented 3 months ago

I believe this is an issue on the rustc rather than the cargo-llvm-cov.

Would you mind submitting an issue at https://github.com/rust-lang/rust?

Zalathar commented 3 months ago

I’m not quite sure what the bug here is supposed to be.

Are you referring to the fact that the uncovered else block isn’t highlighted on line 4?

taiki-e commented 3 months ago

Are you referring to the fact that the uncovered else block isn’t highlighted on line 4?

Yes.

I'm not 100% certain that this should be considered a bug, but in any case, since this is a discussion about the behavior of rustc and not cargo-llvm-cov, it is better to discuss it in the rust-lang/rust and not in this repository.