Skip to content

Commit 4dc70cb

Browse files
committed
rewrite #[naked] parser
1 parent b9107a8 commit 4dc70cb

File tree

10 files changed

+46
-17
lines changed

10 files changed

+46
-17
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,10 @@ pub enum AttributeKind {
233233

234234
/// Represents `#[rustc_macro_transparency]`.
235235
MacroTransparency(Transparency),
236+
237+
/// Represents #[naked]
238+
Naked(Span),
239+
236240
/// Represents `#[optimize(size|speed)]`
237241
Optimize(OptimizeAttr, Span),
238242
/// Represents [`#[repr]`](https://doc.rust-lang.org/stable/reference/type-layout.html#representations).

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,25 @@ impl<S: Stage> SingleAttributeParser<S> for ColdParser {
5151
if !args.no_args() {
5252
cx.expected_no_args(args.span().unwrap_or(cx.attr_span));
5353
return None;
54-
};
54+
}
5555

5656
Some(AttributeKind::Cold(cx.attr_span))
5757
}
5858
}
59+
60+
pub(crate) struct NakedParser;
61+
62+
impl<S: Stage> SingleAttributeParser<S> for NakedParser {
63+
const PATH: &[rustc_span::Symbol] = &[sym::naked];
64+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepLast;
65+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
66+
const TEMPLATE: AttributeTemplate = template!(Word);
67+
68+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
69+
if !args.no_args() {
70+
cx.expected_no_args(args.span().unwrap_or(cx.attr_span));
71+
return None;
72+
}
73+
Some(AttributeKind::Naked(cx.attr_span))
74+
}
75+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_session::Session;
1515
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, Symbol, sym};
1616

1717
use crate::attributes::allow_unstable::{AllowConstFnUnstableParser, AllowInternalUnstableParser};
18-
use crate::attributes::codegen_attrs::{ColdParser, OptimizeParser};
18+
use crate::attributes::codegen_attrs::{ColdParser, NakedParser, OptimizeParser};
1919
use crate::attributes::confusables::ConfusablesParser;
2020
use crate::attributes::deprecation::DeprecationParser;
2121
use crate::attributes::inline::{InlineParser, RustcForceInlineParser};
@@ -110,6 +110,7 @@ attribute_parsers!(
110110
Single<ConstStabilityIndirectParser>,
111111
Single<DeprecationParser>,
112112
Single<InlineParser>,
113+
Single<NakedParser>,
113114
Single<OptimizeParser>,
114115
Single<RustcForceInlineParser>,
115116
Single<TransparencyParser>,

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
121121
.max();
122122
}
123123
AttributeKind::Cold(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::COLD,
124+
AttributeKind::Naked(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NAKED,
124125
AttributeKind::Align { align, .. } => codegen_fn_attrs.alignment = Some(*align),
125126
_ => {}
126127
}
@@ -140,7 +141,6 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
140141
sym::rustc_allocator_zeroed => {
141142
codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR_ZEROED
142143
}
143-
sym::naked => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NAKED,
144144
sym::no_mangle => {
145145
no_mangle_span = Some(attr.span());
146146
if tcx.opt_item_name(did.to_def_id()).is_some() {

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::ops::ControlFlow;
33

44
use rustc_abi::FieldIdx;
55
use rustc_attr_data_structures::ReprAttr::ReprPacked;
6+
use rustc_attr_data_structures::{AttributeKind, find_attr};
67
use rustc_data_structures::unord::{UnordMap, UnordSet};
78
use rustc_errors::codes::*;
89
use rustc_errors::{EmissionGuarantee, MultiSpan};
@@ -103,7 +104,7 @@ pub fn check_abi_fn_ptr(tcx: TyCtxt<'_>, hir_id: hir::HirId, span: Span, abi: Ex
103104
pub fn check_custom_abi(tcx: TyCtxt<'_>, def_id: LocalDefId, fn_sig: FnSig<'_>, fn_sig_span: Span) {
104105
if fn_sig.abi == ExternAbi::Custom {
105106
// Function definitions that use `extern "custom"` must be naked functions.
106-
if !tcx.has_attr(def_id, sym::naked) {
107+
if !find_attr!(tcx.get_all_attrs(def_id), AttributeKind::Naked(_)) {
107108
tcx.dcx().emit_err(crate::errors::AbiCustomClothedFunction {
108109
span: fn_sig_span,
109110
naked_span: tcx.def_span(def_id).shrink_to_lo(),

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
88
use rustc_abi::{ExternAbi, FIRST_VARIANT, FieldIdx};
99
use rustc_ast::util::parser::ExprPrecedence;
10+
use rustc_attr_data_structures::{AttributeKind, find_attr};
1011
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1112
use rustc_data_structures::stack::ensure_sufficient_stack;
1213
use rustc_data_structures::unord::UnordMap;
@@ -3784,7 +3785,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
37843785

37853786
fn check_expr_asm(&self, asm: &'tcx hir::InlineAsm<'tcx>, span: Span) -> Ty<'tcx> {
37863787
if let rustc_ast::AsmMacro::NakedAsm = asm.asm_macro {
3787-
if !self.tcx.has_attr(self.body_id, sym::naked) {
3788+
if !find_attr!(self.tcx.get_all_attrs(self.body_id), AttributeKind::Naked(..)) {
37883789
self.tcx.dcx().emit_err(NakedAsmOutsideNakedFn { span });
37893790
}
37903791
}

compiler/rustc_hir_typeck/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ mod writeback;
4242

4343
pub use coercion::can_coerce;
4444
use fn_ctxt::FnCtxt;
45+
use rustc_attr_data_structures::{AttributeKind, find_attr};
4546
use rustc_data_structures::unord::UnordSet;
4647
use rustc_errors::codes::*;
4748
use rustc_errors::{Applicability, ErrorGuaranteed, pluralize, struct_span_code_err};
@@ -55,8 +56,8 @@ use rustc_middle::query::Providers;
5556
use rustc_middle::ty::{self, Ty, TyCtxt};
5657
use rustc_middle::{bug, span_bug};
5758
use rustc_session::config;
59+
use rustc_span::Span;
5860
use rustc_span::def_id::LocalDefId;
59-
use rustc_span::{Span, sym};
6061
use tracing::{debug, instrument};
6162
use typeck_root_ctxt::TypeckRootCtxt;
6263

@@ -173,7 +174,7 @@ fn typeck_with_inspect<'tcx>(
173174
.map(|(idx, ty)| fcx.normalize(arg_span(idx), ty)),
174175
);
175176

176-
if tcx.has_attr(def_id, sym::naked) {
177+
if find_attr!(tcx.get_all_attrs(def_id), AttributeKind::Naked(..)) {
177178
naked_functions::typeck_naked_fn(tcx, def_id, body);
178179
}
179180

compiler/rustc_hir_typeck/src/naked_functions.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
//! Checks validity of naked functions.
22
3+
use rustc_attr_data_structures::{AttributeKind, find_attr};
34
use rustc_hir as hir;
45
use rustc_hir::def_id::LocalDefId;
56
use rustc_hir::intravisit::Visitor;
67
use rustc_hir::{ExprKind, HirIdSet, StmtKind};
78
use rustc_middle::span_bug;
89
use rustc_middle::ty::TyCtxt;
9-
use rustc_span::{Span, sym};
10+
use rustc_span::Span;
1011

1112
use crate::errors::{
1213
NakedFunctionsAsmBlock, NakedFunctionsMustNakedAsm, NoPatterns, ParamsNotAllowed,
@@ -20,7 +21,7 @@ pub(crate) fn typeck_naked_fn<'tcx>(
2021
def_id: LocalDefId,
2122
body: &'tcx hir::Body<'tcx>,
2223
) {
23-
debug_assert!(tcx.has_attr(def_id, sym::naked));
24+
debug_assert!(find_attr!(tcx.get_all_attrs(def_id), AttributeKind::Naked(..)));
2425
check_no_patterns(tcx, body.params);
2526
check_no_parameters_use(tcx, body);
2627
check_asm(tcx, def_id, body);

compiler/rustc_passes/src/check_attr.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
155155
Attribute::Parsed(AttributeKind::Align { align, span: repr_span }) => {
156156
self.check_align(span, target, *align, *repr_span)
157157
}
158+
Attribute::Parsed(AttributeKind::Naked(attr_span)) => {
159+
self.check_naked(hir_id, *attr_span, span, target, attrs)
160+
}
158161
Attribute::Parsed(
159162
AttributeKind::BodyStability { .. }
160163
| AttributeKind::ConstStabilityIndirect
@@ -201,7 +204,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
201204
[sym::rustc_std_internal_symbol, ..] => {
202205
self.check_rustc_std_internal_symbol(attr, span, target)
203206
}
204-
[sym::naked, ..] => self.check_naked(hir_id, attr, span, target, attrs),
205207
[sym::rustc_no_implicit_autorefs, ..] => {
206208
self.check_applied_to_fn_or_method(hir_id, attr.span(), span, target)
207209
}
@@ -611,7 +613,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
611613
fn check_naked(
612614
&self,
613615
hir_id: HirId,
614-
attr: &Attribute,
616+
attr_span: Span,
615617
span: Span,
616618
target: Target,
617619
attrs: &[Attribute],
@@ -647,7 +649,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
647649
sym::link_section,
648650
sym::linkage,
649651
sym::no_mangle,
650-
sym::naked,
651652
sym::instruction_set,
652653
sym::repr,
653654
sym::align,
@@ -688,14 +689,15 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
688689
AttributeKind::Deprecation { .. }
689690
| AttributeKind::Repr { .. }
690691
| AttributeKind::Align { .. }
691-
| AttributeKind::Cold(..),
692+
| AttributeKind::Cold(..)
693+
| AttributeKind::Naked(..),
692694
) => {
693695
continue;
694696
}
695697
Attribute::Parsed(AttributeKind::Inline(.., span)) => {
696698
self.dcx().emit_err(errors::NakedFunctionIncompatibleAttribute {
697699
span: *span,
698-
naked_span: attr.span(),
700+
naked_span: attr_span,
699701
attr: sym::inline.to_string(),
700702
});
701703

@@ -729,7 +731,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
729731

730732
self.dcx().emit_err(errors::NakedFunctionIncompatibleAttribute {
731733
span: other_attr.span(),
732-
naked_span: attr.span(),
734+
naked_span: attr_span,
733735
attr: other_attr_name,
734736
});
735737

@@ -739,7 +741,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
739741
}
740742
_ => {
741743
self.dcx().emit_err(errors::AttrShouldBeAppliedToFn {
742-
attr_span: attr.span(),
744+
attr_span,
743745
defn_span: span,
744746
on_crate: hir_id == CRATE_HIR_ID,
745747
});

compiler/rustc_passes/src/liveness.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ use std::io;
8585
use std::io::prelude::*;
8686
use std::rc::Rc;
8787

88+
use rustc_attr_data_structures::{AttributeKind, find_attr};
8889
use rustc_data_structures::fx::FxIndexMap;
8990
use rustc_hir as hir;
9091
use rustc_hir::def::*;
@@ -145,7 +146,7 @@ fn check_liveness(tcx: TyCtxt<'_>, def_id: LocalDefId) {
145146
}
146147

147148
// Don't run unused pass for #[naked]
148-
if tcx.has_attr(def_id.to_def_id(), sym::naked) {
149+
if find_attr!(tcx.get_all_attrs(def_id.to_def_id()), AttributeKind::Naked(..)) {
149150
return;
150151
}
151152

0 commit comments

Comments
 (0)