@@ -6,7 +6,8 @@ pub use self::OptimizationDiagnosticKind::*;
66use crate :: value:: Value ;
77use libc:: c_uint;
88
9- use super :: { DiagnosticInfo , Twine } ;
9+ use super :: { DiagnosticInfo , SMDiagnostic } ;
10+ use rustc_span:: InnerSpan ;
1011
1112#[ derive( Copy , Clone ) ]
1213pub enum OptimizationDiagnosticKind {
@@ -86,36 +87,91 @@ impl OptimizationDiagnostic<'ll> {
8687 }
8788}
8889
89- #[ derive( Copy , Clone ) ]
90- pub struct InlineAsmDiagnostic < ' ll > {
90+ pub struct SrcMgrDiagnostic {
91+ pub level : super :: DiagnosticLevel ,
92+ pub message : String ,
93+ pub source : Option < ( String , Vec < InnerSpan > ) > ,
94+ }
95+
96+ impl SrcMgrDiagnostic {
97+ pub unsafe fn unpack ( diag : & SMDiagnostic ) -> SrcMgrDiagnostic {
98+ // Recover the post-substitution assembly code from LLVM for better
99+ // diagnostics.
100+ let mut have_source = false ;
101+ let mut buffer = String :: new ( ) ;
102+ let mut level = super :: DiagnosticLevel :: Error ;
103+ let mut loc = 0 ;
104+ let mut ranges = [ 0 ; 8 ] ;
105+ let mut num_ranges = ranges. len ( ) / 2 ;
106+ let message = super :: build_string ( |message| {
107+ buffer = super :: build_string ( |buffer| {
108+ have_source = super :: LLVMRustUnpackSMDiagnostic (
109+ diag,
110+ message,
111+ buffer,
112+ & mut level,
113+ & mut loc,
114+ ranges. as_mut_ptr ( ) ,
115+ & mut num_ranges,
116+ ) ;
117+ } )
118+ . expect ( "non-UTF8 inline asm" ) ;
119+ } )
120+ . expect ( "non-UTF8 SMDiagnostic" ) ;
121+
122+ SrcMgrDiagnostic {
123+ message,
124+ level,
125+ source : have_source. then ( || {
126+ let mut spans = vec ! [ InnerSpan :: new( loc as usize , loc as usize ) ] ;
127+ for i in 0 ..num_ranges {
128+ spans. push ( InnerSpan :: new ( ranges[ i * 2 ] as usize , ranges[ i * 2 + 1 ] as usize ) ) ;
129+ }
130+ ( buffer, spans)
131+ } ) ,
132+ }
133+ }
134+ }
135+
136+ #[ derive( Clone ) ]
137+ pub struct InlineAsmDiagnostic {
91138 pub level : super :: DiagnosticLevel ,
92139 pub cookie : c_uint ,
93- pub message : & ' ll Twine ,
94- pub instruction : Option < & ' ll Value > ,
140+ pub message : String ,
141+ pub source : Option < ( String , Vec < InnerSpan > ) > ,
95142}
96143
97- impl InlineAsmDiagnostic < ' ll > {
98- unsafe fn unpack ( di : & ' ll DiagnosticInfo ) -> Self {
144+ impl InlineAsmDiagnostic {
145+ unsafe fn unpackInlineAsm ( di : & ' ll DiagnosticInfo ) -> Self {
99146 let mut cookie = 0 ;
100147 let mut message = None ;
101- let mut instruction = None ;
102148 let mut level = super :: DiagnosticLevel :: Error ;
103149
104- super :: LLVMRustUnpackInlineAsmDiagnostic (
105- di,
106- & mut level,
107- & mut cookie,
108- & mut message,
109- & mut instruction,
110- ) ;
150+ super :: LLVMRustUnpackInlineAsmDiagnostic ( di, & mut level, & mut cookie, & mut message) ;
111151
112- InlineAsmDiagnostic { level, cookie, message : message. unwrap ( ) , instruction }
152+ InlineAsmDiagnostic {
153+ level,
154+ cookie,
155+ message : super :: twine_to_string ( message. unwrap ( ) ) ,
156+ source : None ,
157+ }
158+ }
159+
160+ unsafe fn unpackSrcMgr ( di : & ' ll DiagnosticInfo ) -> Self {
161+ let mut cookie = 0 ;
162+ let smdiag = SrcMgrDiagnostic :: unpack ( super :: LLVMRustGetSMDiagnostic ( di, & mut cookie) ) ;
163+ InlineAsmDiagnostic {
164+ level : smdiag. level ,
165+ cookie,
166+ message : smdiag. message ,
167+ source : smdiag. source ,
168+ }
113169 }
114170}
115171
116172pub enum Diagnostic < ' ll > {
117173 Optimization ( OptimizationDiagnostic < ' ll > ) ,
118- InlineAsm ( InlineAsmDiagnostic < ' ll > ) ,
174+ InlineAsm ( InlineAsmDiagnostic ) ,
119175 PGO ( & ' ll DiagnosticInfo ) ,
120176 Linker ( & ' ll DiagnosticInfo ) ,
121177 Unsupported ( & ' ll DiagnosticInfo ) ,
@@ -130,7 +186,7 @@ impl Diagnostic<'ll> {
130186 let kind = super :: LLVMRustGetDiagInfoKind ( di) ;
131187
132188 match kind {
133- Dk :: InlineAsm => InlineAsm ( InlineAsmDiagnostic :: unpack ( di) ) ,
189+ Dk :: InlineAsm => InlineAsm ( InlineAsmDiagnostic :: unpackInlineAsm ( di) ) ,
134190
135191 Dk :: OptimizationRemark => {
136192 Optimization ( OptimizationDiagnostic :: unpack ( OptimizationRemark , di) )
@@ -162,6 +218,8 @@ impl Diagnostic<'ll> {
162218 Dk :: Linker => Linker ( di) ,
163219 Dk :: Unsupported => Unsupported ( di) ,
164220
221+ Dk :: SrcMgr => InlineAsm ( InlineAsmDiagnostic :: unpackSrcMgr ( di) ) ,
222+
165223 _ => UnknownDiagnostic ( di) ,
166224 }
167225 }
0 commit comments