Skip to content

Commit

Permalink
Add conditional turning off of cov info
Browse files Browse the repository at this point in the history
  • Loading branch information
aDogCalledSpot committed Jan 18, 2024
1 parent 9ed899e commit 8cdd467
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Expand Up @@ -41,6 +41,9 @@ xxx_debug_only_print_generated_code = [
"wasm-bindgen-macro/xxx_debug_only_print_generated_code",
]

## Turns off coverage generation for describe blocks
unstable-coverage = ["wasm-bindgen-macro/unstable-coverage"]

[dependencies]
wasm-bindgen-macro = { path = "crates/macro", version = "=0.2.90" }
serde = { version = "1.0", optional = true }
Expand Down
1 change: 1 addition & 0 deletions crates/backend/Cargo.toml
Expand Up @@ -15,6 +15,7 @@ rust-version = "1.57"
[features]
spans = []
extra-traits = ["syn/extra-traits"]
unstable-coverage = []

[dependencies]
bumpalo = "3.0.0"
Expand Down
18 changes: 18 additions & 0 deletions crates/backend/src/codegen.rs
Expand Up @@ -160,6 +160,16 @@ impl TryToTokens for ast::LinkToModule {
}
}

#[cfg(feature = "unstable-coverage")]
fn coverage() -> TokenStream {
quote!(#[coverage(off)])
}

#[cfg(not(feature = "unstable-coverage"))]
fn coverage() -> TokenStream {
quote!()
}

impl ToTokens for ast::Struct {
fn to_tokens(&self, tokens: &mut TokenStream) {
let name = &self.rust_name;
Expand All @@ -169,10 +179,12 @@ impl ToTokens for ast::Struct {
let new_fn = Ident::new(&shared::new_function(&name_str), Span::call_site());
let free_fn = Ident::new(&shared::free_function(&name_str), Span::call_site());
let unwrap_fn = Ident::new(&shared::unwrap_function(&name_str), Span::call_site());
let coverage = coverage();
let wasm_bindgen = &self.wasm_bindgen;
(quote! {
#[automatically_derived]
impl #wasm_bindgen::describe::WasmDescribe for #name {
#coverage
fn describe() {
use #wasm_bindgen::__wbindgen_if_not_std;
__wbindgen_if_not_std! {
Expand Down Expand Up @@ -814,6 +826,7 @@ impl ToTokens for ast::ImportType {
});

let no_deref = self.no_deref;
let coverage = coverage();

(quote! {
#[automatically_derived]
Expand All @@ -835,6 +848,7 @@ impl ToTokens for ast::ImportType {
use #wasm_bindgen::__rt::core;

impl WasmDescribe for #rust_name {
#coverage
fn describe() {
#description
}
Expand Down Expand Up @@ -1048,6 +1062,7 @@ impl ToTokens for ast::ImportEnum {
let variant_paths_ref = &variant_paths;

let wasm_bindgen = &self.wasm_bindgen;
let coverage = coverage();

(quote! {
#(#attrs)*
Expand Down Expand Up @@ -1082,6 +1097,7 @@ impl ToTokens for ast::ImportEnum {
// It should really be using &str for all of these, but that requires some major changes to cli-support
#[automatically_derived]
impl #wasm_bindgen::describe::WasmDescribe for #name {
#coverage
fn describe() {
<#wasm_bindgen::JsValue as #wasm_bindgen::describe::WasmDescribe>::describe()
}
Expand Down Expand Up @@ -1404,6 +1420,7 @@ impl ToTokens for ast::Enum {
});
let try_from_cast_clauses = cast_clauses.clone();
let wasm_bindgen = &self.wasm_bindgen;
let coverage = coverage();
(quote! {
#[automatically_derived]
impl #wasm_bindgen::convert::IntoWasmAbi for #enum_name {
Expand Down Expand Up @@ -1441,6 +1458,7 @@ impl ToTokens for ast::Enum {

#[automatically_derived]
impl #wasm_bindgen::describe::WasmDescribe for #enum_name {
#coverage
fn describe() {
use #wasm_bindgen::describe::*;
inform(ENUM);
Expand Down
5 changes: 5 additions & 0 deletions crates/backend/src/lib.rs
Expand Up @@ -27,6 +27,11 @@
#![cfg_attr(feature = "extra-traits", deny(missing_debug_implementations))]
#![deny(missing_docs)]
#![doc(html_root_url = "https://docs.rs/wasm-bindgen-backend/0.2")]
#![cfg_attr(
feature = "unstable-coverage",
feature(allow_internal_unstable),
allow(internal_features)
)]

pub use crate::codegen::TryToTokens;
pub use crate::error::Diagnostic;
Expand Down
1 change: 0 additions & 1 deletion crates/cli/src/bin/wasm-bindgen-test-runner/main.rs
Expand Up @@ -264,7 +264,6 @@ fn coverage_args(tmpdir: &Path) -> Option<PathBuf> {
.map(|s| s.to_str().unwrap().to_string())
.unwrap_or_default();


match env::var_os("WASM_BINDGEN_UNSTABLE_TEST_PROFRAW_OUT") {
Some(s) => {
let mut buf = PathBuf::from(s);
Expand Down
1 change: 1 addition & 0 deletions crates/macro-support/Cargo.toml
Expand Up @@ -16,6 +16,7 @@ rust-version = "1.57"
spans = ["wasm-bindgen-backend/spans"]
extra-traits = ["syn/extra-traits"]
strict-macro = []
unstable-coverage = ["wasm-bindgen-backend/unstable-coverage"]

[dependencies]
syn = { version = '2.0', features = ['visit', 'full'] }
Expand Down
1 change: 1 addition & 0 deletions crates/macro/Cargo.toml
Expand Up @@ -19,6 +19,7 @@ proc-macro = true
spans = ["wasm-bindgen-macro-support/spans"]
xxx_debug_only_print_generated_code = []
strict-macro = ["wasm-bindgen-macro-support/strict-macro"]
unstable-coverage = ["wasm-bindgen-macro-support/unstable-coverage"]

[dependencies]
wasm-bindgen-macro-support = { path = "../macro-support", version = "=0.2.90" }
Expand Down
9 changes: 9 additions & 0 deletions crates/macro/src/lib.rs
@@ -1,11 +1,20 @@
#![doc(html_root_url = "https://docs.rs/wasm-bindgen-macro/0.2")]
#![cfg_attr(
feature = "unstable-coverage",
feature(allow_internal_unstable),
allow(internal_features)
)]

extern crate proc_macro;

use proc_macro::TokenStream;
use quote::quote;

#[proc_macro_attribute]
#[cfg_attr(
feature = "unstable-coverage",
allow_internal_unstable(coverage_attribute)
)]
pub fn wasm_bindgen(attr: TokenStream, input: TokenStream) -> TokenStream {
match wasm_bindgen_macro_support::expand(attr.into(), input.into()) {
Ok(tokens) => {
Expand Down
2 changes: 1 addition & 1 deletion crates/test/Cargo.toml
Expand Up @@ -10,7 +10,7 @@ rust-version = "1.57"

[features]
default = []
unstable-coverage = ["minicov"]
unstable-coverage = ["minicov", "wasm-bindgen/unstable-coverage"]

[dependencies]
console_error_panic_hook = '0.1'
Expand Down
5 changes: 5 additions & 0 deletions src/closure.rs
Expand Up @@ -465,6 +465,7 @@ impl<T> WasmDescribe for Closure<T>
where
T: WasmClosure + ?Sized,
{
#[cfg_attr(feature = "unstable-coverage", coverage(off))]
fn describe() {
inform(EXTERNREF);
}
Expand Down Expand Up @@ -565,6 +566,7 @@ macro_rules! doit {
where $($var: FromWasmAbi + 'static,)*
R: ReturnWasmAbi + 'static,
{
#[cfg_attr(feature = "unstable-coverage", coverage(off))]
fn describe() {
#[allow(non_snake_case)]
unsafe extern "C" fn invoke<$($var: FromWasmAbi,)* R: ReturnWasmAbi>(
Expand Down Expand Up @@ -622,6 +624,7 @@ macro_rules! doit {
where $($var: FromWasmAbi + 'static,)*
R: ReturnWasmAbi + 'static,
{
#[cfg_attr(feature = "unstable-coverage", coverage(off))]
fn describe() {
#[allow(non_snake_case)]
unsafe extern "C" fn invoke<$($var: FromWasmAbi,)* R: ReturnWasmAbi>(
Expand Down Expand Up @@ -763,6 +766,7 @@ where
A: RefFromWasmAbi,
R: ReturnWasmAbi + 'static,
{
#[cfg_attr(feature = "unstable-coverage", coverage(off))]
fn describe() {
#[allow(non_snake_case)]
unsafe extern "C" fn invoke<A: RefFromWasmAbi, R: ReturnWasmAbi>(
Expand Down Expand Up @@ -809,6 +813,7 @@ where
A: RefFromWasmAbi,
R: ReturnWasmAbi + 'static,
{
#[cfg_attr(feature = "unstable-coverage", coverage(off))]
fn describe() {
#[allow(non_snake_case)]
unsafe extern "C" fn invoke<A: RefFromWasmAbi, R: ReturnWasmAbi>(
Expand Down
4 changes: 4 additions & 0 deletions src/convert/closures.rs
Expand Up @@ -54,6 +54,7 @@ macro_rules! stack_closures {
where $($var: FromWasmAbi,)*
R: ReturnWasmAbi
{
#[cfg_attr(feature = "unstable-coverage", coverage(off))]
fn describe() {
inform(FUNCTION);
inform($invoke::<$($var,)* R> as u32);
Expand Down Expand Up @@ -108,6 +109,7 @@ macro_rules! stack_closures {
where $($var: FromWasmAbi,)*
R: ReturnWasmAbi
{
#[cfg_attr(feature = "unstable-coverage", coverage(off))]
fn describe() {
inform(FUNCTION);
inform($invoke_mut::<$($var,)* R> as u32);
Expand Down Expand Up @@ -177,6 +179,7 @@ where
A: RefFromWasmAbi,
R: ReturnWasmAbi,
{
#[cfg_attr(feature = "unstable-coverage", coverage(off))]
fn describe() {
inform(FUNCTION);
inform(invoke1_ref::<A, R> as u32);
Expand Down Expand Up @@ -232,6 +235,7 @@ where
A: RefFromWasmAbi,
R: ReturnWasmAbi,
{
#[cfg_attr(feature = "unstable-coverage", coverage(off))]
fn describe() {
inform(FUNCTION);
inform(invoke1_mut_ref::<A, R> as u32);
Expand Down
12 changes: 12 additions & 0 deletions src/describe.rs
Expand Up @@ -103,32 +103,37 @@ cfg_if! {
}

impl<T> WasmDescribe for *const T {
#[cfg_attr(feature = "unstable-coverage", coverage(off))]
fn describe() {
inform(U32)
}
}

impl<T> WasmDescribe for *mut T {
#[cfg_attr(feature = "unstable-coverage", coverage(off))]
fn describe() {
inform(U32)
}
}

impl<T: WasmDescribe> WasmDescribe for [T] {
#[cfg_attr(feature = "unstable-coverage", coverage(off))]
fn describe() {
inform(SLICE);
T::describe();
}
}

impl<'a, T: WasmDescribe + ?Sized> WasmDescribe for &'a T {
#[cfg_attr(feature = "unstable-coverage", coverage(off))]
fn describe() {
inform(REF);
T::describe();
}
}

impl<'a, T: WasmDescribe + ?Sized> WasmDescribe for &'a mut T {
#[cfg_attr(feature = "unstable-coverage", coverage(off))]
fn describe() {
inform(REFMUT);
T::describe();
Expand Down Expand Up @@ -166,46 +171,53 @@ if_std! {
}

impl<T: WasmDescribeVector> WasmDescribe for Box<[T]> {
#[cfg_attr(feature = "unstable-coverage", coverage(off))]
fn describe() {
T::describe_vector();
}
}

impl<T> WasmDescribe for Vec<T> where Box<[T]>: WasmDescribe {
#[cfg_attr(feature = "unstable-coverage", coverage(off))]
fn describe() {
<Box<[T]>>::describe();
}
}
}

impl<T: WasmDescribe> WasmDescribe for Option<T> {
#[cfg_attr(feature = "unstable-coverage", coverage(off))]
fn describe() {
inform(OPTIONAL);
T::describe();
}
}

impl WasmDescribe for () {
#[cfg_attr(feature = "unstable-coverage", coverage(off))]
fn describe() {
inform(UNIT)
}
}

impl<T: WasmDescribe, E: Into<JsValue>> WasmDescribe for Result<T, E> {
#[cfg_attr(feature = "unstable-coverage", coverage(off))]
fn describe() {
inform(RESULT);
T::describe();
}
}

impl<T: WasmDescribe> WasmDescribe for Clamped<T> {
#[cfg_attr(feature = "unstable-coverage", coverage(off))]
fn describe() {
inform(CLAMPED);
T::describe();
}
}

impl WasmDescribe for JsError {
#[cfg_attr(feature = "unstable-coverage", coverage(off))]
fn describe() {
JsValue::describe();
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Expand Up @@ -8,6 +8,7 @@
#![no_std]
#![allow(coherence_leak_check)]
#![doc(html_root_url = "https://docs.rs/wasm-bindgen/0.2")]
#![cfg_attr(feature = "unstable-coverage", feature(coverage_attribute))]

use core::convert::TryFrom;
use core::fmt;
Expand Down

0 comments on commit 8cdd467

Please sign in to comment.