Skip to content

Commit c6bc506

Browse files
committed
rewrite #[naked] parser
1 parent 414d859 commit c6bc506

File tree

10 files changed

+46
-18
lines changed

10 files changed

+46
-18
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

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

231231
/// Represents `#[rustc_macro_transparency]`.
232232
MacroTransparency(Transparency),
233+
234+
/// Represents #[naked]
235+
Naked(Span),
236+
233237
/// Represents `#[optimize(size|speed)]`
234238
Optimize(OptimizeAttr, Span),
235239
/// 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};
@@ -109,6 +109,7 @@ attribute_parsers!(
109109
Single<ConstStabilityIndirectParser>,
110110
Single<DeprecationParser>,
111111
Single<InlineParser>,
112+
Single<NakedParser>,
112113
Single<OptimizeParser>,
113114
Single<RustcForceInlineParser>,
114115
Single<TransparencyParser>,

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
119119
.max();
120120
}
121121
AttributeKind::Cold(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::COLD,
122-
122+
AttributeKind::Naked(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NAKED,
123123
_ => {}
124124
}
125125
}
@@ -138,7 +138,6 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
138138
sym::rustc_allocator_zeroed => {
139139
codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR_ZEROED
140140
}
141-
sym::naked => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NAKED,
142141
sym::no_mangle => {
143142
no_mangle_span = Some(attr.span());
144143
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;
@@ -3795,7 +3796,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
37953796

37963797
fn check_expr_asm(&self, asm: &'tcx hir::InlineAsm<'tcx>, span: Span) -> Ty<'tcx> {
37973798
if let rustc_ast::AsmMacro::NakedAsm = asm.asm_macro {
3798-
if !self.tcx.has_attr(self.body_id, sym::naked) {
3799+
if !find_attr!(self.tcx.get_all_attrs(self.body_id), AttributeKind::Naked(..)) {
37993800
self.tcx.dcx().emit_err(NakedAsmOutsideNakedFn { span });
38003801
}
38013802
}

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
@@ -152,6 +152,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
152152
Attribute::Parsed(AttributeKind::Cold(attr_span)) => {
153153
self.check_cold(hir_id, *attr_span, span, target)
154154
}
155+
Attribute::Parsed(AttributeKind::Naked(attr_span)) => {
156+
self.check_naked(hir_id, *attr_span, span, target, attrs)
157+
}
155158
Attribute::Parsed(
156159
AttributeKind::BodyStability { .. }
157160
| AttributeKind::ConstStabilityIndirect
@@ -198,7 +201,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
198201
[sym::rustc_std_internal_symbol, ..] => {
199202
self.check_rustc_std_internal_symbol(attr, span, target)
200203
}
201-
[sym::naked, ..] => self.check_naked(hir_id, attr, span, target, attrs),
202204
[sym::rustc_no_implicit_autorefs, ..] => {
203205
self.check_applied_to_fn_or_method(hir_id, attr.span(), span, target)
204206
}
@@ -608,7 +610,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
608610
fn check_naked(
609611
&self,
610612
hir_id: HirId,
611-
attr: &Attribute,
613+
attr_span: Span,
612614
span: Span,
613615
target: Target,
614616
attrs: &[Attribute],
@@ -644,7 +646,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
644646
sym::link_section,
645647
sym::linkage,
646648
sym::no_mangle,
647-
sym::naked,
648649
sym::instruction_set,
649650
sym::repr,
650651
sym::rustc_std_internal_symbol,
@@ -683,14 +684,15 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
683684
Attribute::Parsed(
684685
AttributeKind::Deprecation { .. }
685686
| AttributeKind::Repr { .. }
686-
| AttributeKind::Cold(..),
687+
| AttributeKind::Cold(..)
688+
| AttributeKind::Naked(..),
687689
) => {
688690
continue;
689691
}
690692
Attribute::Parsed(AttributeKind::Inline(.., span)) => {
691693
self.dcx().emit_err(errors::NakedFunctionIncompatibleAttribute {
692694
span: *span,
693-
naked_span: attr.span(),
695+
naked_span: attr_span,
694696
attr: sym::inline.to_string(),
695697
});
696698

@@ -724,7 +726,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
724726

725727
self.dcx().emit_err(errors::NakedFunctionIncompatibleAttribute {
726728
span: other_attr.span(),
727-
naked_span: attr.span(),
729+
naked_span: attr_span,
728730
attr: other_attr_name,
729731
});
730732

@@ -734,7 +736,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
734736
}
735737
_ => {
736738
self.dcx().emit_err(errors::AttrShouldBeAppliedToFn {
737-
attr_span: attr.span(),
739+
attr_span,
738740
defn_span: span,
739741
on_crate: hir_id == CRATE_HIR_ID,
740742
});

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)