Skip to content

Commit 1ee87b3

Browse files
committed
rustllvm: split DebugLoc in UnpackOptimizationDiagnostic
1 parent a536767 commit 1ee87b3

File tree

4 files changed

+52
-21
lines changed

4 files changed

+52
-21
lines changed

src/librustc_llvm/diagnostic.rs

+23-9
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use libc::c_uint;
1717
use std::ptr;
1818

1919
use {DiagnosticInfoRef, TwineRef, ValueRef};
20-
use ffi::DebugLocRef;
2120

2221
#[derive(Copy, Clone)]
2322
pub enum OptimizationDiagnosticKind {
@@ -47,7 +46,9 @@ pub struct OptimizationDiagnostic {
4746
pub kind: OptimizationDiagnosticKind,
4847
pub pass_name: String,
4948
pub function: ValueRef,
50-
pub debug_loc: DebugLocRef,
49+
pub line: c_uint,
50+
pub column: c_uint,
51+
pub filename: String,
5152
pub message: String,
5253
}
5354

@@ -56,24 +57,37 @@ impl OptimizationDiagnostic {
5657
di: DiagnosticInfoRef)
5758
-> OptimizationDiagnostic {
5859
let mut function = ptr::null_mut();
59-
let mut debug_loc = ptr::null_mut();
60+
let mut line = 0;
61+
let mut column = 0;
6062

6163
let mut message = None;
64+
let mut filename = None;
6265
let pass_name = super::build_string(|pass_name|
6366
message = super::build_string(|message|
64-
super::LLVMRustUnpackOptimizationDiagnostic(di,
65-
pass_name,
66-
&mut function,
67-
&mut debug_loc,
68-
message)
67+
filename = super::build_string(|filename|
68+
super::LLVMRustUnpackOptimizationDiagnostic(di,
69+
pass_name,
70+
&mut function,
71+
&mut line,
72+
&mut column,
73+
filename,
74+
message)
75+
)
6976
)
7077
);
7178

79+
let mut filename = filename.unwrap_or(String::new());
80+
if filename.is_empty() {
81+
filename.push_str("<unknown file>");
82+
}
83+
7284
OptimizationDiagnostic {
7385
kind: kind,
7486
pass_name: pass_name.expect("got a non-UTF8 pass name from LLVM"),
7587
function: function,
76-
debug_loc: debug_loc,
88+
line: line,
89+
column: column,
90+
filename: filename,
7791
message: message.expect("got a non-UTF8 OptimizationDiagnostic message from LLVM")
7892
}
7993
}

src/librustc_llvm/ffi.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1633,7 +1633,9 @@ extern "C" {
16331633
pub fn LLVMRustUnpackOptimizationDiagnostic(DI: DiagnosticInfoRef,
16341634
pass_name_out: RustStringRef,
16351635
function_out: *mut ValueRef,
1636-
debugloc_out: *mut DebugLocRef,
1636+
loc_line_out: *mut c_uint,
1637+
loc_column_out: *mut c_uint,
1638+
loc_filename_out: RustStringRef,
16371639
message_out: RustStringRef);
16381640
pub fn LLVMRustUnpackInlineAsmDiagnostic(DI: DiagnosticInfoRef,
16391641
cookie_out: *mut c_uint,

src/librustc_trans/back/write.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc::session::config::{self, OutputFilenames, OutputType, OutputTypes, Pas
1616
AllPasses, Sanitizer};
1717
use rustc::session::Session;
1818
use llvm;
19-
use llvm::{ModuleRef, TargetMachineRef, PassManagerRef, DiagnosticInfoRef, ContextRef};
19+
use llvm::{ModuleRef, TargetMachineRef, PassManagerRef, DiagnosticInfoRef};
2020
use llvm::SMDiagnosticRef;
2121
use {CrateTranslation, ModuleLlvm, ModuleSource, ModuleTranslation};
2222
use rustc::hir::def_id::CrateNum;
@@ -307,7 +307,6 @@ pub struct CodegenContext<'a> {
307307
}
308308

309309
struct HandlerFreeVars<'a> {
310-
llcx: ContextRef,
311310
cgcx: &'a CodegenContext<'a>,
312311
}
313312

@@ -329,7 +328,7 @@ unsafe extern "C" fn inline_asm_handler(diag: SMDiagnosticRef,
329328
}
330329

331330
unsafe extern "C" fn diagnostic_handler(info: DiagnosticInfoRef, user: *mut c_void) {
332-
let HandlerFreeVars { llcx, cgcx } = *(user as *const HandlerFreeVars);
331+
let HandlerFreeVars { cgcx, .. } = *(user as *const HandlerFreeVars);
333332

334333
match llvm::diagnostic::Diagnostic::unpack(info) {
335334
llvm::diagnostic::InlineAsm(inline) => {
@@ -345,11 +344,12 @@ unsafe extern "C" fn diagnostic_handler(info: DiagnosticInfoRef, user: *mut c_vo
345344
};
346345

347346
if enabled {
348-
let loc = llvm::debug_loc_to_string(llcx, opt.debug_loc);
349-
cgcx.handler.note_without_error(&format!("optimization {} for {} at {}: {}",
347+
cgcx.handler.note_without_error(&format!("optimization {} for {} at {}:{}:{}: {}",
350348
opt.kind.describe(),
351349
opt.pass_name,
352-
if loc.is_empty() { "[unknown]" } else { &*loc },
350+
opt.filename,
351+
opt.line,
352+
opt.column,
353353
opt.message));
354354
}
355355
}
@@ -370,9 +370,7 @@ unsafe fn optimize_and_codegen(cgcx: &CodegenContext,
370370
let llcx = mllvm.llcx;
371371
let tm = config.tm;
372372

373-
// llcx doesn't outlive this function, so we can put this on the stack.
374373
let fv = HandlerFreeVars {
375-
llcx: llcx,
376374
cgcx: cgcx,
377375
};
378376
let fv = &fv as *const HandlerFreeVars as *mut c_void;

src/rustllvm/RustWrapper.cpp

+20-3
Original file line numberDiff line numberDiff line change
@@ -906,16 +906,33 @@ extern "C" void LLVMRustWriteTwineToString(LLVMTwineRef T, RustStringRef Str) {
906906

907907
extern "C" void LLVMRustUnpackOptimizationDiagnostic(
908908
LLVMDiagnosticInfoRef DI, RustStringRef PassNameOut,
909-
LLVMValueRef *FunctionOut, LLVMDebugLocRef *DebugLocOut,
910-
RustStringRef MessageOut) {
909+
LLVMValueRef *FunctionOut, unsigned* Line, unsigned* Column,
910+
RustStringRef FilenameOut, RustStringRef MessageOut) {
911911
// Undefined to call this not on an optimization diagnostic!
912912
llvm::DiagnosticInfoOptimizationBase *Opt =
913913
static_cast<llvm::DiagnosticInfoOptimizationBase *>(unwrap(DI));
914914

915915
RawRustStringOstream PassNameOS(PassNameOut);
916916
PassNameOS << Opt->getPassName();
917917
*FunctionOut = wrap(&Opt->getFunction());
918-
*DebugLocOut = wrap(&Opt->getDebugLoc());
918+
919+
RawRustStringOstream FilenameOS(FilenameOut);
920+
#if LLVM_VERSION_GE(5,0)
921+
DiagnosticLocation loc = Opt->getLocation();
922+
if (loc.isValid()) {
923+
*Line = loc.getLine();
924+
*Column = loc.getColumn();
925+
FilenameOS << loc.getFilename();
926+
}
927+
#else
928+
const DebugLoc &loc = Opt->getDebugLoc();
929+
if (loc) {
930+
*Line = loc.getLine();
931+
*Column = loc.getCol();
932+
FilenameOS << cast<DIScope>(loc.getScope())->getFilename();
933+
}
934+
#endif
935+
919936
RawRustStringOstream MessageOS(MessageOut);
920937
MessageOS << Opt->getMsg();
921938
}

0 commit comments

Comments
 (0)