Skip to content

Commit 9a5ac56

Browse files
committed
Rework no_coverage to coverage(off)
1 parent 19a647d commit 9a5ac56

File tree

27 files changed

+183
-158
lines changed

27 files changed

+183
-158
lines changed

compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub fn expand_deriving_eq(
3434
attributes: thin_vec![
3535
cx.attr_word(sym::inline, span),
3636
cx.attr_nested_word(sym::doc, sym::hidden, span),
37-
cx.attr_word(sym::no_coverage, span)
37+
cx.attr_nested_word(sym::coverage, sym::off, span)
3838
],
3939
fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
4040
combine_substructure: combine_substructure(Box::new(|a, b, c| {

compiler/rustc_builtin_macros/src/test_harness.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ fn generate_test_harness(
251251
let expn_id = ext_cx.resolver.expansion_for_ast_pass(
252252
DUMMY_SP,
253253
AstPass::TestHarness,
254-
&[sym::test, sym::rustc_attrs, sym::no_coverage],
254+
&[sym::test, sym::rustc_attrs, sym::coverage],
255255
None,
256256
);
257257
let def_site = DUMMY_SP.with_def_site_ctxt(expn_id.to_expn_id());
@@ -332,8 +332,8 @@ fn mk_main(cx: &mut TestCtxt<'_>) -> P<ast::Item> {
332332

333333
// #[rustc_main]
334334
let main_attr = ecx.attr_word(sym::rustc_main, sp);
335-
// #[no_coverage]
336-
let no_coverage_attr = ecx.attr_word(sym::no_coverage, sp);
335+
// #[coverage(off)]
336+
let coverage_attr = ecx.attr_nested_word(sym::coverage, sym::off, sp);
337337

338338
// pub fn main() { ... }
339339
let main_ret_ty = ecx.ty(sp, ast::TyKind::Tup(ThinVec::new()));
@@ -363,7 +363,7 @@ fn mk_main(cx: &mut TestCtxt<'_>) -> P<ast::Item> {
363363

364364
let main = P(ast::Item {
365365
ident: main_id,
366-
attrs: thin_vec![main_attr, no_coverage_attr],
366+
attrs: thin_vec![main_attr, coverage_attr],
367367
id: ast::DUMMY_NODE_ID,
368368
kind: main,
369369
vis: ast::Visibility { span: sp, kind: ast::VisibilityKind::Public, tokens: None },

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,10 +318,10 @@ fn add_unused_functions(cx: &CodegenCx<'_, '_>) {
318318
{
319319
let codegen_fn_attrs = tcx.codegen_fn_attrs(non_codegenned_def_id);
320320

321-
// If a function is marked `#[no_coverage]`, then skip generating a
321+
// If a function is marked `#[coverage(off)]`, then skip generating a
322322
// dead code stub for it.
323323
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NO_COVERAGE) {
324-
debug!("skipping unused fn marked #[no_coverage]: {:?}", non_codegenned_def_id);
324+
debug!("skipping unused fn marked #[coverage(off)]: {:?}", non_codegenned_def_id);
325325
continue;
326326
}
327327

compiler/rustc_codegen_ssa/messages.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ codegen_ssa_erroneous_constant = erroneous constant encountered
2323
2424
codegen_ssa_error_creating_remark_dir = failed to create remark directory: {$error}
2525
26+
codegen_ssa_expected_coverage_symbol = expected `coverage(off)` or `coverage(on)`
27+
2628
codegen_ssa_expected_used_symbol = expected `used`, `used(compiler)` or `used(linker)`
2729
2830
codegen_ssa_extern_funcs_not_found = some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ use rustc_target::spec::{abi, SanitizerSet};
1616

1717
use crate::errors;
1818
use crate::target_features::from_target_feature;
19-
use crate::{errors::ExpectedUsedSymbol, target_features::check_target_feature_trait_unsafe};
19+
use crate::{
20+
errors::{ExpectedCoverageSymbol, ExpectedUsedSymbol},
21+
target_features::check_target_feature_trait_unsafe,
22+
};
2023

2124
fn linkage_by_name(tcx: TyCtxt<'_>, def_id: LocalDefId, name: &str) -> Linkage {
2225
use rustc_middle::mir::mono::Linkage::*;
@@ -128,7 +131,21 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
128131
.emit();
129132
}
130133
}
131-
sym::no_coverage => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_COVERAGE,
134+
sym::coverage => {
135+
let inner = attr.meta_item_list();
136+
match inner.as_deref() {
137+
Some([item]) if item.has_name(sym::off) => {
138+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_COVERAGE;
139+
}
140+
Some([item]) if item.has_name(sym::on) => {
141+
// Allow #[coverage(on)] for being explicit, maybe also in future to enable
142+
// coverage on a smaller scope within an excluded larger scopy.
143+
}
144+
Some(_) | None => {
145+
tcx.sess.emit_err(ExpectedCoverageSymbol { span: attr.span });
146+
}
147+
}
148+
}
132149
sym::rustc_std_internal_symbol => {
133150
codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL
134151
}

compiler/rustc_codegen_ssa/src/errors.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,13 @@ pub struct UnknownArchiveKind<'a> {
560560
pub kind: &'a str,
561561
}
562562

563+
#[derive(Diagnostic)]
564+
#[diag(codegen_ssa_expected_coverage_symbol)]
565+
pub struct ExpectedCoverageSymbol {
566+
#[primary_span]
567+
pub span: Span,
568+
}
569+
563570
#[derive(Diagnostic)]
564571
#[diag(codegen_ssa_expected_used_symbol)]
565572
pub struct ExpectedUsedSymbol {
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
A `#[no_coverage]` attribute was applied to something which does not show up
1+
A `#[coverage]` attribute was applied to something which does not show up
22
in code coverage, or is too granular to be excluded from the coverage report.
33

44
For now, this attribute can only be applied to function, method, and closure
@@ -9,18 +9,18 @@ will just emit an `unused_attributes` lint instead of this error.
99
Example of erroneous code:
1010

1111
```compile_fail,E0788
12-
#[no_coverage]
12+
#[coverage(off)]
1313
struct Foo;
1414
15-
#[no_coverage]
15+
#[coverage(on)]
1616
const FOO: Foo = Foo;
1717
```
1818

19-
`#[no_coverage]` tells the compiler to not generate coverage instrumentation for
20-
a piece of code when the `-C instrument-coverage` flag is passed. Things like
21-
structs and consts are not coverable code, and thus cannot do anything with this
22-
attribute.
19+
`#[coverage(off)]` tells the compiler to not generate coverage instrumentation
20+
for a piece of code when the `-C instrument-coverage` flag is passed. Things
21+
like structs and consts are not coverable code, and thus cannot do anything
22+
with this attribute.
2323

2424
If you wish to apply this attribute to all methods in an impl or module,
2525
manually annotate each method; it is not possible to annotate the entire impl
26-
with a `#[no_coverage]` attribute.
26+
with a `#[coverage]` attribute.

compiler/rustc_feature/src/active.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,9 @@ declare_features! (
393393
(active, const_trait_impl, "1.42.0", Some(67792), None),
394394
/// Allows the `?` operator in const contexts.
395395
(active, const_try, "1.56.0", Some(74935), None),
396+
/// Allows function attribute `#[coverage(on/off)]`, to control coverage
397+
/// instrumentation of that function.
398+
(active, coverage, "1.53.0", Some(84605), None),
396399
/// Allows non-builtin attributes in inner attribute position.
397400
(active, custom_inner_attributes, "1.30.0", Some(54726), None),
398401
/// Allows custom test frameworks with `#![test_runner]` and `#[test_case]`.
@@ -504,9 +507,6 @@ declare_features! (
504507
(active, never_type_fallback, "1.41.0", Some(65992), None),
505508
/// Allows `#![no_core]`.
506509
(active, no_core, "1.3.0", Some(29639), None),
507-
/// Allows function attribute `#[no_coverage]`, to bypass coverage
508-
/// instrumentation of that function.
509-
(active, no_coverage, "1.53.0", Some(84605), None),
510510
/// Allows the use of `no_sanitize` attribute.
511511
(active, no_sanitize, "1.42.0", Some(39699), None),
512512
/// Allows using the `non_exhaustive_omitted_patterns` lint.

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
394394
template!(List: "address, kcfi, memory, thread"), DuplicatesOk,
395395
experimental!(no_sanitize)
396396
),
397-
gated!(no_coverage, Normal, template!(Word), WarnFollowing, experimental!(no_coverage)),
397+
gated!(coverage, Normal, template!(Word, List: "on|off"), WarnFollowing, experimental!(coverage)),
398398

399399
ungated!(
400400
doc, Normal, template!(List: "hidden|inline|...", NameValueStr: "string"), DuplicatesOk

compiler/rustc_middle/src/middle/codegen_fn_attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ bitflags! {
8787
/// #[cmse_nonsecure_entry]: with a TrustZone-M extension, declare a
8888
/// function as an entry function from Non-Secure code.
8989
const CMSE_NONSECURE_ENTRY = 1 << 14;
90-
/// `#[no_coverage]`: indicates that the function should be ignored by
90+
/// `#[coverage(off)]`: indicates that the function should be ignored by
9191
/// the MIR `InstrumentCoverage` pass and not added to the coverage map
9292
/// during codegen.
9393
const NO_COVERAGE = 1 << 15;

0 commit comments

Comments
 (0)