|
1 | 1 | //! Checks validity of naked functions.
|
2 | 2 |
|
3 |
| -use rustc_ast::{Attribute, InlineAsmOptions}; |
| 3 | +use rustc_ast::InlineAsmOptions; |
4 | 4 | use rustc_errors::{struct_span_err, Applicability};
|
5 | 5 | use rustc_hir as hir;
|
| 6 | +use rustc_hir::def::DefKind; |
6 | 7 | use rustc_hir::def_id::LocalDefId;
|
7 |
| -use rustc_hir::intravisit::{FnKind, Visitor}; |
8 |
| -use rustc_hir::{ExprKind, HirId, InlineAsmOperand, StmtKind}; |
| 8 | +use rustc_hir::intravisit::Visitor; |
| 9 | +use rustc_hir::{ExprKind, InlineAsmOperand, StmtKind}; |
9 | 10 | use rustc_middle::ty::query::Providers;
|
10 | 11 | use rustc_middle::ty::TyCtxt;
|
11 | 12 | use rustc_session::lint::builtin::UNDEFINED_NAKED_FUNCTION_ABI;
|
12 | 13 | use rustc_span::symbol::sym;
|
13 | 14 | use rustc_span::Span;
|
14 | 15 | use rustc_target::spec::abi::Abi;
|
15 | 16 |
|
16 |
| -fn check_mod_naked_functions(tcx: TyCtxt<'_>, module_def_id: LocalDefId) { |
17 |
| - tcx.hir().visit_item_likes_in_module(module_def_id, &mut CheckNakedFunctions { tcx }); |
18 |
| -} |
19 |
| - |
20 | 17 | pub(crate) fn provide(providers: &mut Providers) {
|
21 | 18 | *providers = Providers { check_mod_naked_functions, ..*providers };
|
22 | 19 | }
|
23 | 20 |
|
24 |
| -struct CheckNakedFunctions<'tcx> { |
25 |
| - tcx: TyCtxt<'tcx>, |
26 |
| -} |
27 |
| - |
28 |
| -impl<'tcx> Visitor<'tcx> for CheckNakedFunctions<'tcx> { |
29 |
| - fn visit_fn( |
30 |
| - &mut self, |
31 |
| - fk: FnKind<'_>, |
32 |
| - _fd: &'tcx hir::FnDecl<'tcx>, |
33 |
| - body_id: hir::BodyId, |
34 |
| - span: Span, |
35 |
| - hir_id: HirId, |
36 |
| - ) { |
37 |
| - let ident_span; |
38 |
| - let fn_header; |
39 |
| - |
40 |
| - match fk { |
41 |
| - FnKind::Closure => { |
42 |
| - // Closures with a naked attribute are rejected during attribute |
43 |
| - // check. Don't validate them any further. |
44 |
| - return; |
45 |
| - } |
46 |
| - FnKind::ItemFn(ident, _, ref header, ..) => { |
47 |
| - ident_span = ident.span; |
48 |
| - fn_header = header; |
49 |
| - } |
50 |
| - |
51 |
| - FnKind::Method(ident, ref sig, ..) => { |
52 |
| - ident_span = ident.span; |
53 |
| - fn_header = &sig.header; |
54 |
| - } |
| 21 | +fn check_mod_naked_functions(tcx: TyCtxt<'_>, module_def_id: LocalDefId) { |
| 22 | + let items = tcx.hir_module_items(module_def_id); |
| 23 | + for def_id in items.definitions() { |
| 24 | + if !matches!(tcx.def_kind(def_id), DefKind::Fn | DefKind::AssocFn) { |
| 25 | + continue; |
55 | 26 | }
|
56 | 27 |
|
57 |
| - let attrs = self.tcx.hir().attrs(hir_id); |
58 |
| - let naked = attrs.iter().any(|attr| attr.has_name(sym::naked)); |
59 |
| - if naked { |
60 |
| - let body = self.tcx.hir().body(body_id); |
61 |
| - check_abi(self.tcx, hir_id, fn_header.abi, ident_span); |
62 |
| - check_no_patterns(self.tcx, body.params); |
63 |
| - check_no_parameters_use(self.tcx, body); |
64 |
| - check_asm(self.tcx, body, span); |
65 |
| - check_inline(self.tcx, attrs); |
| 28 | + let naked = tcx.has_attr(def_id.to_def_id(), sym::naked); |
| 29 | + if !naked { |
| 30 | + continue; |
66 | 31 | }
|
| 32 | + |
| 33 | + let (fn_header, body_id) = match tcx.hir().get_by_def_id(def_id) { |
| 34 | + hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, _, body_id), .. }) |
| 35 | + | hir::Node::TraitItem(hir::TraitItem { |
| 36 | + kind: hir::TraitItemKind::Fn(sig, hir::TraitFn::Provided(body_id)), |
| 37 | + .. |
| 38 | + }) |
| 39 | + | hir::Node::ImplItem(hir::ImplItem { |
| 40 | + kind: hir::ImplItemKind::Fn(sig, body_id), |
| 41 | + .. |
| 42 | + }) => (sig.header, *body_id), |
| 43 | + _ => continue, |
| 44 | + }; |
| 45 | + |
| 46 | + let body = tcx.hir().body(body_id); |
| 47 | + check_abi(tcx, def_id, fn_header.abi); |
| 48 | + check_no_patterns(tcx, body.params); |
| 49 | + check_no_parameters_use(tcx, body); |
| 50 | + check_asm(tcx, def_id, body); |
| 51 | + check_inline(tcx, def_id); |
67 | 52 | }
|
68 | 53 | }
|
69 | 54 |
|
70 | 55 | /// Check that the function isn't inlined.
|
71 |
| -fn check_inline(tcx: TyCtxt<'_>, attrs: &[Attribute]) { |
72 |
| - for attr in attrs.iter().filter(|attr| attr.has_name(sym::inline)) { |
| 56 | +fn check_inline(tcx: TyCtxt<'_>, def_id: LocalDefId) { |
| 57 | + let attrs = tcx.get_attrs(def_id.to_def_id(), sym::inline); |
| 58 | + for attr in attrs { |
73 | 59 | tcx.sess.struct_span_err(attr.span, "naked functions cannot be inlined").emit();
|
74 | 60 | }
|
75 | 61 | }
|
76 | 62 |
|
77 | 63 | /// Checks that function uses non-Rust ABI.
|
78 |
| -fn check_abi(tcx: TyCtxt<'_>, hir_id: HirId, abi: Abi, fn_ident_span: Span) { |
| 64 | +fn check_abi(tcx: TyCtxt<'_>, def_id: LocalDefId, abi: Abi) { |
79 | 65 | if abi == Abi::Rust {
|
80 |
| - tcx.struct_span_lint_hir(UNDEFINED_NAKED_FUNCTION_ABI, hir_id, fn_ident_span, |lint| { |
| 66 | + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); |
| 67 | + let span = tcx.def_span(def_id); |
| 68 | + tcx.struct_span_lint_hir(UNDEFINED_NAKED_FUNCTION_ABI, hir_id, span, |lint| { |
81 | 69 | lint.build("Rust ABI is unsupported in naked functions").emit();
|
82 | 70 | });
|
83 | 71 | }
|
@@ -141,15 +129,15 @@ impl<'tcx> Visitor<'tcx> for CheckParameters<'tcx> {
|
141 | 129 | }
|
142 | 130 |
|
143 | 131 | /// Checks that function body contains a single inline assembly block.
|
144 |
| -fn check_asm<'tcx>(tcx: TyCtxt<'tcx>, body: &'tcx hir::Body<'tcx>, fn_span: Span) { |
| 132 | +fn check_asm<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId, body: &'tcx hir::Body<'tcx>) { |
145 | 133 | let mut this = CheckInlineAssembly { tcx, items: Vec::new() };
|
146 | 134 | this.visit_body(body);
|
147 | 135 | if let [(ItemKind::Asm | ItemKind::Err, _)] = this.items[..] {
|
148 | 136 | // Ok.
|
149 | 137 | } else {
|
150 | 138 | let mut diag = struct_span_err!(
|
151 | 139 | tcx.sess,
|
152 |
| - fn_span, |
| 140 | + tcx.def_span(def_id), |
153 | 141 | E0787,
|
154 | 142 | "naked functions must contain a single asm block"
|
155 | 143 | );
|
|
0 commit comments