@@ -7,10 +7,10 @@ use rustc_errors::{Applicability, DiagnosticBuilder};
7
7
use rustc_expand:: base:: { self , * } ;
8
8
use rustc_parse:: parser:: Parser ;
9
9
use rustc_parse_format as parse;
10
- use rustc_session:: lint:: { self , BuiltinLintDiagnostics } ;
10
+ use rustc_session:: lint;
11
11
use rustc_span:: symbol:: Ident ;
12
12
use rustc_span:: symbol:: { kw, sym, Symbol } ;
13
- use rustc_span:: { InnerSpan , MultiSpan , Span } ;
13
+ use rustc_span:: { InnerSpan , Span } ;
14
14
use rustc_target:: asm:: InlineAsmArch ;
15
15
use smallvec:: smallvec;
16
16
@@ -484,11 +484,7 @@ fn parse_reg<'a>(
484
484
Ok ( result)
485
485
}
486
486
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 > {
492
488
let mut template = vec ! [ ] ;
493
489
// Register operands are implicitly used since they are not allowed to be
494
490
// referenced in the template string.
@@ -501,6 +497,8 @@ fn expand_preparsed_asm(
501
497
let mut line_spans = Vec :: with_capacity ( args. templates . len ( ) ) ;
502
498
let mut curarg = 0 ;
503
499
500
+ let mut template_strs = Vec :: with_capacity ( args. templates . len ( ) ) ;
501
+
504
502
for template_expr in args. templates . into_iter ( ) {
505
503
if !template. is_empty ( ) {
506
504
template. push ( ast:: InlineAsmTemplatePiece :: String ( "\n " . to_string ( ) ) ) ;
@@ -524,8 +522,13 @@ fn expand_preparsed_asm(
524
522
ast:: StrStyle :: Raw ( raw) => Some ( raw as usize ) ,
525
523
} ;
526
524
527
- let template_str = & template_str. as_str ( ) ;
528
525
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 ( ) ;
529
532
530
533
if let Some ( InlineAsmArch :: X86 | InlineAsmArch :: X86_64 ) = ecx. sess . asm_arch {
531
534
let find_span = |needle : & str | -> Span {
@@ -560,72 +563,6 @@ fn expand_preparsed_asm(
560
563
}
561
564
}
562
565
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
-
629
566
// Don't treat raw asm as a format string.
630
567
if args. options . contains ( ast:: InlineAsmOptions :: RAW ) {
631
568
template. push ( ast:: InlineAsmTemplatePiece :: String ( template_str. to_string ( ) ) ) ;
@@ -819,6 +756,7 @@ fn expand_preparsed_asm(
819
756
820
757
Some ( ast:: InlineAsm {
821
758
template,
759
+ template_strs : template_strs. into_boxed_slice ( ) ,
822
760
operands : args. operands ,
823
761
clobber_abi : args. clobber_abi ,
824
762
options : args. options ,
@@ -833,7 +771,7 @@ pub fn expand_asm<'cx>(
833
771
) -> Box < dyn base:: MacResult + ' cx > {
834
772
match parse_args ( ecx, sp, tts, false ) {
835
773
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) {
837
775
P ( ast:: Expr {
838
776
id : ast:: DUMMY_NODE_ID ,
839
777
kind : ast:: ExprKind :: InlineAsm ( P ( inline_asm) ) ,
@@ -860,7 +798,7 @@ pub fn expand_global_asm<'cx>(
860
798
) -> Box < dyn base:: MacResult + ' cx > {
861
799
match parse_args ( ecx, sp, tts, true ) {
862
800
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) {
864
802
MacEager :: items ( smallvec ! [ P ( ast:: Item {
865
803
ident: Ident :: invalid( ) ,
866
804
attrs: Vec :: new( ) ,
0 commit comments