@@ -6,11 +6,13 @@ use rustc_middle::query::TyCtxtAt;
66use rustc_middle:: ty:: { self , TyCtxt } ;
77use rustc_middle:: util:: Providers ;
88use rustc_span:: def_id:: LocalDefId ;
9+ use rustc_span:: sym;
910
1011/// Registers query/hook implementations related to coverage.
1112pub ( crate ) fn provide ( providers : & mut Providers ) {
1213 providers. hooks . is_eligible_for_coverage =
1314 |TyCtxtAt { tcx, .. } , def_id| is_eligible_for_coverage ( tcx, def_id) ;
15+ providers. queries . coverage_attr_on = coverage_attr_on;
1416 providers. queries . coverage_ids_info = coverage_ids_info;
1517}
1618
@@ -38,14 +40,37 @@ fn is_eligible_for_coverage(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
3840 return false ;
3941 }
4042
41- if tcx. codegen_fn_attrs ( def_id) . flags . contains ( CodegenFnAttrFlags :: NO_COVERAGE ) {
43+ if tcx. codegen_fn_attrs ( def_id) . flags . contains ( CodegenFnAttrFlags :: NAKED ) {
44+ trace ! ( "InstrumentCoverage skipped for {def_id:?} (`#[naked]`)" ) ;
45+ return false ;
46+ }
47+
48+ if !tcx. coverage_attr_on ( def_id) {
4249 trace ! ( "InstrumentCoverage skipped for {def_id:?} (`#[coverage(off)]`)" ) ;
4350 return false ;
4451 }
4552
4653 true
4754}
4855
56+ /// Query implementation for `coverage_attr_on`.
57+ fn coverage_attr_on ( tcx : TyCtxt < ' _ > , def_id : LocalDefId ) -> bool {
58+ if let Some ( attr) = tcx. get_attr ( def_id, sym:: coverage) {
59+ match attr. meta_item_list ( ) . as_deref ( ) {
60+ Some ( [ item] ) if item. has_name ( sym:: off) => return false ,
61+ Some ( [ item] ) if item. has_name ( sym:: on) => return true ,
62+ Some ( _) | None => {
63+ // Other possibilities should have been rejected by `rustc_parse::validate_attr`.
64+ tcx. dcx ( ) . span_bug ( attr. span , "unexpected value of coverage attribute" ) ;
65+ }
66+ }
67+ }
68+
69+ // We didn't see an explicit coverage attribute, so
70+ // allow coverage instrumentation by default.
71+ true
72+ }
73+
4974/// Query implementation for `coverage_ids_info`.
5075fn coverage_ids_info < ' tcx > (
5176 tcx : TyCtxt < ' tcx > ,
0 commit comments