|
1 | 1 | use rustc_ast::{ast, attr, MetaItemKind, NestedMetaItem}; |
2 | 2 | use rustc_attr::{list_contains_name, InlineAttr, InstructionSetAttr, OptimizeAttr}; |
| 3 | +use rustc_data_structures::packed::Pu128; |
3 | 4 | use rustc_errors::{codes::*, struct_span_code_err}; |
4 | 5 | use rustc_hir as hir; |
5 | 6 | use rustc_hir::def::DefKind; |
@@ -467,24 +468,55 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs { |
467 | 468 | } |
468 | 469 | sym::patchable_function_entry => { |
469 | 470 | codegen_fn_attrs.patchable_function_entry = attr.meta_item_list().and_then(|l| { |
470 | | - let mut prefix = 0; |
471 | | - let mut entry = 0; |
| 471 | + let mut prefix = None; |
| 472 | + let mut entry = None; |
472 | 473 | for item in l { |
473 | | - if let Some((sym, lit)) = item.name_value_literal() { |
474 | | - let val = match lit.kind { |
475 | | - // FIXME emit error if too many nops requested |
476 | | - rustc_ast::LitKind::Int(i, _) => i as u8, |
477 | | - _ => continue, |
478 | | - }; |
479 | | - match sym { |
480 | | - sym::prefix => prefix = val, |
481 | | - sym::entry => entry = val, |
482 | | - // FIXME possibly emit error here? |
483 | | - _ => continue, |
| 474 | + let Some(meta_item) = item.meta_item() else { |
| 475 | + tcx.dcx().span_err(item.span(), "Expected name value pair."); |
| 476 | + continue; |
| 477 | + }; |
| 478 | + |
| 479 | + let Some(name_value_lit) = meta_item.name_value_literal() else { |
| 480 | + tcx.dcx().span_err(item.span(), "Expected name value pair."); |
| 481 | + continue; |
| 482 | + }; |
| 483 | + |
| 484 | + let attrib_to_write = match meta_item.name_or_empty() { |
| 485 | + sym::prefix_nops => &mut prefix, |
| 486 | + sym::entry_nops => &mut entry, |
| 487 | + _ => { |
| 488 | + tcx.dcx().span_err( |
| 489 | + item.span(), |
| 490 | + format!( |
| 491 | + "Unexpected parameter name. Allowed names: {}, {}", |
| 492 | + sym::prefix_nops, |
| 493 | + sym::entry_nops |
| 494 | + ), |
| 495 | + ); |
| 496 | + continue; |
484 | 497 | } |
485 | | - } |
| 498 | + }; |
| 499 | + |
| 500 | + let rustc_ast::LitKind::Int(Pu128(val @ 0..=255), _) = name_value_lit.kind |
| 501 | + else { |
| 502 | + tcx.dcx().span_err( |
| 503 | + name_value_lit.span, |
| 504 | + "Expected integer value between 0 and 255.", |
| 505 | + ); |
| 506 | + continue; |
| 507 | + }; |
| 508 | + |
| 509 | + *attrib_to_write = Some(val.try_into().unwrap()); |
486 | 510 | } |
487 | | - Some(PatchableFunctionEntry::from_prefix_and_entry(prefix, entry)) |
| 511 | + |
| 512 | + if let (None, None) = (prefix, entry) { |
| 513 | + tcx.dcx().span_err(attr.span, "Must specify at least one parameter."); |
| 514 | + } |
| 515 | + |
| 516 | + Some(PatchableFunctionEntry::from_prefix_and_entry( |
| 517 | + prefix.unwrap_or(0), |
| 518 | + entry.unwrap_or(0), |
| 519 | + )) |
488 | 520 | }) |
489 | 521 | } |
490 | 522 | _ => {} |
|
0 commit comments