@@ -7,10 +7,10 @@ use rustc_errors::{Applicability, DiagnosticBuilder};
77use rustc_expand:: base:: { self , * } ;
88use rustc_parse:: parser:: Parser ;
99use rustc_parse_format as parse;
10- use rustc_session:: lint:: { self , BuiltinLintDiagnostics } ;
10+ use rustc_session:: lint;
1111use rustc_span:: symbol:: Ident ;
1212use rustc_span:: symbol:: { kw, sym, Symbol } ;
13- use rustc_span:: { InnerSpan , MultiSpan , Span } ;
13+ use rustc_span:: { InnerSpan , Span } ;
1414use rustc_target:: asm:: InlineAsmArch ;
1515use smallvec:: smallvec;
1616
@@ -484,11 +484,7 @@ fn parse_reg<'a>(
484484 Ok ( result)
485485}
486486
487- fn expand_preparsed_asm (
488- ecx : & mut ExtCtxt < ' _ > ,
489- args : AsmArgs ,
490- is_local_asm : bool ,
491- ) -> Option < ast:: InlineAsm > {
487+ fn expand_preparsed_asm ( ecx : & mut ExtCtxt < ' _ > , args : AsmArgs ) -> Option < ast:: InlineAsm > {
492488 let mut template = vec ! [ ] ;
493489 // Register operands are implicitly used since they are not allowed to be
494490 // referenced in the template string.
@@ -501,6 +497,8 @@ fn expand_preparsed_asm(
501497 let mut line_spans = Vec :: with_capacity ( args. templates . len ( ) ) ;
502498 let mut curarg = 0 ;
503499
500+ let mut template_strs = Vec :: with_capacity ( args. templates . len ( ) ) ;
501+
504502 for template_expr in args. templates . into_iter ( ) {
505503 if !template. is_empty ( ) {
506504 template. push ( ast:: InlineAsmTemplatePiece :: String ( "\n " . to_string ( ) ) ) ;
@@ -524,8 +522,13 @@ fn expand_preparsed_asm(
524522 ast:: StrStyle :: Raw ( raw) => Some ( raw as usize ) ,
525523 } ;
526524
527- let template_str = & template_str. as_str ( ) ;
528525 let template_snippet = ecx. source_map ( ) . span_to_snippet ( template_sp) . ok ( ) ;
526+ template_strs. push ( (
527+ template_str,
528+ template_snippet. as_ref ( ) . map ( |s| Symbol :: intern ( s) ) ,
529+ template_sp,
530+ ) ) ;
531+ let template_str = & template_str. as_str ( ) ;
529532
530533 if let Some ( InlineAsmArch :: X86 | InlineAsmArch :: X86_64 ) = ecx. sess . asm_arch {
531534 let find_span = |needle : & str | -> Span {
@@ -560,72 +563,6 @@ fn expand_preparsed_asm(
560563 }
561564 }
562565
563- // Lint against the use of named labels in inline `asm!` but not `global_asm!`
564- if is_local_asm {
565- let find_label_span = |needle : & str | -> Option < Span > {
566- if let Some ( snippet) = & template_snippet {
567- if let Some ( pos) = snippet. find ( needle) {
568- let end = pos
569- + & snippet[ pos..]
570- . find ( |c| c == ':' )
571- . unwrap_or ( snippet[ pos..] . len ( ) - 1 ) ;
572- let inner = InnerSpan :: new ( pos, end) ;
573- return Some ( template_sp. from_inner ( inner) ) ;
574- }
575- }
576-
577- None
578- } ;
579-
580- let mut found_labels = Vec :: new ( ) ;
581-
582- // A semicolon might not actually be specified as a separator for all targets, but it seems like LLVM accepts it always
583- let statements = template_str. split ( |c| matches ! ( c, '\n' | ';' ) ) ;
584- for statement in statements {
585- // If there's a comment, trim it from the statement
586- let statement = statement. find ( "//" ) . map_or ( statement, |idx| & statement[ ..idx] ) ;
587- let mut start_idx = 0 ;
588- for ( idx, _) in statement. match_indices ( ':' ) {
589- let possible_label = statement[ start_idx..idx] . trim ( ) ;
590- let mut chars = possible_label. chars ( ) ;
591- if let Some ( c) = chars. next ( ) {
592- // A label starts with an alphabetic character or . or _ and continues with alphanumeric characters, _, or $
593- if ( c. is_alphabetic ( ) || matches ! ( c, '.' | '_' ) )
594- && chars. all ( |c| c. is_alphanumeric ( ) || matches ! ( c, '_' | '$' ) )
595- {
596- found_labels. push ( possible_label) ;
597- } else {
598- // If we encounter a non-label, there cannot be any further labels, so stop checking
599- break ;
600- }
601- } else {
602- // Empty string means a leading ':' in this section, which is not a label
603- break ;
604- }
605-
606- start_idx = idx + 1 ;
607- }
608- }
609-
610- if found_labels. len ( ) > 0 {
611- let spans =
612- found_labels. into_iter ( ) . filter_map ( find_label_span) . collect :: < Vec < Span > > ( ) ;
613- // If there were labels but we couldn't find a span, combine the warnings and use the template span
614- let target_spans: MultiSpan =
615- if spans. len ( ) > 0 { spans. into ( ) } else { template_sp. into ( ) } ;
616- ecx. parse_sess ( ) . buffer_lint_with_diagnostic (
617- lint:: builtin:: NAMED_ASM_LABELS ,
618- target_spans,
619- ecx. current_expansion . lint_node_id ,
620- "avoid using named labels in inline assembly" ,
621- BuiltinLintDiagnostics :: NamedAsmLabel (
622- "only local labels of the form `<number>:` should be used in inline asm"
623- . to_string ( ) ,
624- ) ,
625- ) ;
626- }
627- }
628-
629566 // Don't treat raw asm as a format string.
630567 if args. options . contains ( ast:: InlineAsmOptions :: RAW ) {
631568 template. push ( ast:: InlineAsmTemplatePiece :: String ( template_str. to_string ( ) ) ) ;
@@ -819,6 +756,7 @@ fn expand_preparsed_asm(
819756
820757 Some ( ast:: InlineAsm {
821758 template,
759+ template_strs : template_strs. into_boxed_slice ( ) ,
822760 operands : args. operands ,
823761 clobber_abi : args. clobber_abi ,
824762 options : args. options ,
@@ -833,7 +771,7 @@ pub fn expand_asm<'cx>(
833771) -> Box < dyn base:: MacResult + ' cx > {
834772 match parse_args ( ecx, sp, tts, false ) {
835773 Ok ( args) => {
836- let expr = if let Some ( inline_asm) = expand_preparsed_asm ( ecx, args, true ) {
774+ let expr = if let Some ( inline_asm) = expand_preparsed_asm ( ecx, args) {
837775 P ( ast:: Expr {
838776 id : ast:: DUMMY_NODE_ID ,
839777 kind : ast:: ExprKind :: InlineAsm ( P ( inline_asm) ) ,
@@ -860,7 +798,7 @@ pub fn expand_global_asm<'cx>(
860798) -> Box < dyn base:: MacResult + ' cx > {
861799 match parse_args ( ecx, sp, tts, true ) {
862800 Ok ( args) => {
863- if let Some ( inline_asm) = expand_preparsed_asm ( ecx, args, false ) {
801+ if let Some ( inline_asm) = expand_preparsed_asm ( ecx, args) {
864802 MacEager :: items ( smallvec ! [ P ( ast:: Item {
865803 ident: Ident :: invalid( ) ,
866804 attrs: Vec :: new( ) ,
0 commit comments