diff options
| author | Mark Rousskov <mark.simulacrum@gmail.com> | 2019-12-24 17:38:22 -0500 |
|---|---|---|
| committer | Mark Rousskov <mark.simulacrum@gmail.com> | 2019-12-24 17:38:22 -0500 |
| commit | 6891388e661c2bbf965a330ff73bf8c08a7dbf7d (patch) | |
| tree | ff2f518c1dfdb558a162c73b3ee3f2a0b44d5adc /src/librustc_codegen_llvm | |
| parent | 48291a9dda821a81957b44034572c0ec62728f35 (diff) | |
x.py fmt after previous deignore
Diffstat (limited to 'src/librustc_codegen_llvm')
| -rw-r--r-- | src/librustc_codegen_llvm/back/write.rs | 493 | ||||
| -rw-r--r-- | src/librustc_codegen_llvm/consts.rs | 132 | ||||
| -rw-r--r-- | src/librustc_codegen_llvm/debuginfo/metadata.rs | 1446 |
3 files changed, 1023 insertions, 1048 deletions
diff --git a/src/librustc_codegen_llvm/back/write.rs b/src/librustc_codegen_llvm/back/write.rs index 796ea7aac36..818d09ce691 100644 --- a/src/librustc_codegen_llvm/back/write.rs +++ b/src/librustc_codegen_llvm/back/write.rs @@ -2,38 +2,38 @@ use crate::attributes; use crate::back::bytecode; use crate::back::lto::ThinBuffer; use crate::base; +use crate::common; use crate::consts; +use crate::context::{get_reloc_model, is_pie_binary}; use crate::llvm::{self, DiagnosticInfo, PassManager, SMDiagnostic}; use crate::llvm_util; -use crate::ModuleLlvm; use crate::type_::Type; -use crate::context::{is_pie_binary, get_reloc_model}; -use crate::common; use crate::LlvmCodegenBackend; +use crate::ModuleLlvm; +use log::debug; use rustc::bug; use rustc::hir::def_id::LOCAL_CRATE; -use rustc_codegen_ssa::back::write::{CodegenContext, ModuleConfig, run_assembler}; -use rustc_codegen_ssa::traits::*; -use rustc::session::config::{self, OutputType, Passes, Lto, Sanitizer, SwitchWithOptPath}; +use rustc::session::config::{self, Lto, OutputType, Passes, Sanitizer, SwitchWithOptPath}; use rustc::session::Session; use rustc::ty::TyCtxt; -use rustc_codegen_ssa::{RLIB_BYTECODE_EXTENSION, ModuleCodegen, CompiledModule}; use rustc::util::common::time_ext; -use rustc_fs_util::{path_to_c_string, link_or_copy}; +use rustc_codegen_ssa::back::write::{run_assembler, CodegenContext, ModuleConfig}; +use rustc_codegen_ssa::traits::*; +use rustc_codegen_ssa::{CompiledModule, ModuleCodegen, RLIB_BYTECODE_EXTENSION}; use rustc_data_structures::small_c_str::SmallCStr; -use rustc_errors::{Handler, FatalError}; -use log::debug; +use rustc_errors::{FatalError, Handler}; +use rustc_fs_util::{link_or_copy, path_to_c_string}; +use libc::{c_char, c_int, c_uint, c_void, size_t}; use std::ffi::CString; use std::fs; use std::io::{self, Write}; use std::path::{Path, PathBuf}; +use std::slice; use std::str; use std::sync::Arc; -use std::slice; -use libc::{c_int, c_uint, c_void, c_char, size_t}; -pub const RELOC_MODEL_ARGS : [(&str, llvm::RelocMode); 7] = [ +pub const RELOC_MODEL_ARGS: [(&str, llvm::RelocMode); 7] = [ ("pic", llvm::RelocMode::PIC), ("static", llvm::RelocMode::Static), ("default", llvm::RelocMode::Default), @@ -50,7 +50,7 @@ pub const CODE_GEN_MODEL_ARGS: &[(&str, llvm::CodeModel)] = &[ ("large", llvm::CodeModel::Large), ]; -pub const TLS_MODEL_ARGS : [(&str, llvm::ThreadLocalMode); 4] = [ +pub const TLS_MODEL_ARGS: [(&str, llvm::ThreadLocalMode); 4] = [ ("global-dynamic", llvm::ThreadLocalMode::GeneralDynamic), ("local-dynamic", llvm::ThreadLocalMode::LocalDynamic), ("initial-exec", llvm::ThreadLocalMode::InitialExec), @@ -65,12 +65,13 @@ pub fn llvm_err(handler: &rustc_errors::Handler, msg: &str) -> FatalError { } pub fn write_output_file( - handler: &rustc_errors::Handler, - target: &'ll llvm::TargetMachine, - pm: &llvm::PassManager<'ll>, - m: &'ll llvm::Module, - output: &Path, - file_type: llvm::FileType) -> Result<(), FatalError> { + handler: &rustc_errors::Handler, + target: &'ll llvm::TargetMachine, + pm: &llvm::PassManager<'ll>, + m: &'ll llvm::Module, + output: &Path, + file_type: llvm::FileType, +) -> Result<(), FatalError> { unsafe { let output_c = path_to_c_string(output); let result = llvm::LLVMRustWriteOutputFile(target, pm, m, output_c.as_ptr(), file_type); @@ -85,9 +86,8 @@ pub fn create_informational_target_machine( sess: &Session, find_features: bool, ) -> &'static mut llvm::TargetMachine { - target_machine_factory(sess, config::OptLevel::No, find_features)().unwrap_or_else(|err| { - llvm_err(sess.diagnostic(), &err).raise() - }) + target_machine_factory(sess, config::OptLevel::No, find_features)() + .unwrap_or_else(|err| llvm_err(sess.diagnostic(), &err).raise()) } pub fn create_target_machine( @@ -95,13 +95,12 @@ pub fn create_target_machine( find_features: bool, ) -> &'static mut llvm::TargetMachine { target_machine_factory(&tcx.sess, tcx.backend_optimization_level(LOCAL_CRATE), find_features)() - .unwrap_or_else(|err| { - llvm_err(tcx.sess.diagnostic(), &err).raise() - }) + .unwrap_or_else(|err| llvm_err(tcx.sess.diagnostic(), &err).raise()) } -pub fn to_llvm_opt_settings(cfg: config::OptLevel) -> (llvm::CodeGenOptLevel, llvm::CodeGenOptSize) -{ +pub fn to_llvm_opt_settings( + cfg: config::OptLevel, +) -> (llvm::CodeGenOptLevel, llvm::CodeGenOptSize) { use self::config::OptLevel::*; match cfg { No => (llvm::CodeGenOptLevel::None, llvm::CodeGenOptSizeNone), @@ -116,9 +115,11 @@ pub fn to_llvm_opt_settings(cfg: config::OptLevel) -> (llvm::CodeGenOptLevel, ll // If find_features is true this won't access `sess.crate_types` by assuming // that `is_pie_binary` is false. When we discover LLVM target features // `sess.crate_types` is uninitialized so we cannot access it. -pub fn target_machine_factory(sess: &Session, optlvl: config::OptLevel, find_features: bool) - -> Arc<dyn Fn() -> Result<&'static mut llvm::TargetMachine, String> + Send + Sync> -{ +pub fn target_machine_factory( + sess: &Session, + optlvl: config::OptLevel, + find_features: bool, +) -> Arc<dyn Fn() -> Result<&'static mut llvm::TargetMachine, String> + Send + Sync> { let reloc_model = get_reloc_model(sess); let (opt_level, _) = to_llvm_opt_settings(optlvl); @@ -127,22 +128,18 @@ pub fn target_machine_factory(sess: &Session, optlvl: config::OptLevel, find_fea let ffunction_sections = sess.target.target.options.function_sections; let fdata_sections = ffunction_sections; - let code_model_arg = sess.opts.cg.code_model.as_ref().or( - sess.target.target.options.code_model.as_ref(), - ); + let code_model_arg = + sess.opts.cg.code_model.as_ref().or(sess.target.target.options.code_model.as_ref()); let code_model = match code_model_arg { - Some(s) => { - match CODE_GEN_MODEL_ARGS.iter().find(|arg| arg.0 == s) { - Some(x) => x.1, - _ => { - sess.err(&format!("{:?} is not a valid code model", - code_model_arg)); - sess.abort_if_errors(); - bug!(); - } + Some(s) => match CODE_GEN_MODEL_ARGS.iter().find(|arg| arg.0 == s) { + Some(x) => x.1, + _ => { + sess.err(&format!("{:?} is not a valid code model", code_model_arg)); + sess.abort_if_errors(); + bug!(); } - } + }, None => llvm::CodeModel::None, }; @@ -152,9 +149,9 @@ pub fn target_machine_factory(sess: &Session, optlvl: config::OptLevel, find_fea // On the wasm target once the `atomics` feature is enabled that means that // we're no longer single-threaded, or otherwise we don't want LLVM to // lower atomic operations to single-threaded operations. - if singlethread && - sess.target.target.llvm_target.contains("wasm32") && - features.iter().any(|s| *s == "+atomics") + if singlethread + && sess.target.target.llvm_target.contains("wasm32") + && features.iter().any(|s| *s == "+atomics") { singlethread = false; } @@ -173,7 +170,10 @@ pub fn target_machine_factory(sess: &Session, optlvl: config::OptLevel, find_fea Arc::new(move || { let tm = unsafe { llvm::LLVMRustCreateTargetMachine( - triple.as_ptr(), cpu.as_ptr(), features.as_ptr(), abi.as_ptr(), + triple.as_ptr(), + cpu.as_ptr(), + features.as_ptr(), + abi.as_ptr(), code_model, reloc_model, opt_level, @@ -190,8 +190,7 @@ pub fn target_machine_factory(sess: &Session, optlvl: config::OptLevel, find_fea }; tm.ok_or_else(|| { - format!("Could not create LLVM TargetMachine for triple: {}", - triple.to_str().unwrap()) + format!("Could not create LLVM TargetMachine for triple: {}", triple.to_str().unwrap()) }) }) } @@ -199,10 +198,10 @@ pub fn target_machine_factory(sess: &Session, optlvl: config::OptLevel, find_fea pub(crate) fn save_temp_bitcode( cgcx: &CodegenContext<LlvmCodegenBackend>, module: &ModuleCodegen<ModuleLlvm>, - name: &str + name: &str, ) { if !cgcx.save_temps { - return + return; } unsafe { let ext = format!("{}.bc", name); @@ -220,9 +219,11 @@ pub struct DiagnosticHandlers<'a> { } impl<'a> DiagnosticHandlers<'a> { - pub fn new(cgcx: &'a CodegenContext<LlvmCodegenBackend>, - handler: &'a Handler, - llcx: &'a llvm::Context) -> Self { + pub fn new( + cgcx: &'a CodegenContext<LlvmCodegenBackend>, + handler: &'a Handler, + llcx: &'a llvm::Context, + ) -> Self { let data = Box::into_raw(Box::new((cgcx, handler))); unsafe { llvm::LLVMRustSetInlineAsmDiagnosticHandler(llcx, inline_asm_handler, data.cast()); @@ -243,17 +244,17 @@ impl<'a> Drop for DiagnosticHandlers<'a> { } } -unsafe extern "C" fn report_inline_asm(cgcx: &CodegenContext<LlvmCodegenBackend>, - msg: &str, - cookie: c_uint) { +unsafe extern "C" fn report_inline_asm( + cgcx: &CodegenContext<LlvmCodegenBackend>, + msg: &str, + cookie: c_uint, +) { cgcx.diag_emitter.inline_asm_error(cookie as u32, msg.to_owned()); } -unsafe extern "C" fn inline_asm_handler(diag: &SMDiagnostic, - user: *const c_void, - cookie: c_uint) { +unsafe extern "C" fn inline_asm_handler(diag: &SMDiagnostic, user: *const c_void, cookie: c_uint) { if user.is_null() { - return + return; } let (cgcx, _) = *(user as *const (&CodegenContext<LlvmCodegenBackend>, &Handler)); @@ -265,15 +266,13 @@ unsafe extern "C" fn inline_asm_handler(diag: &SMDiagnostic, unsafe extern "C" fn diagnostic_handler(info: &DiagnosticInfo, user: *mut c_void) { if user.is_null() { - return + return; } let (cgcx, diag_handler) = *(user as *const (&CodegenContext<LlvmCodegenBackend>, &Handler)); match llvm::diagnostic::Diagnostic::unpack(info) { llvm::diagnostic::InlineAsm(inline) => { - report_inline_asm(cgcx, - &llvm::twine_to_string(inline.message), - inline.cookie); + report_inline_asm(cgcx, &llvm::twine_to_string(inline.message), inline.cookie); } llvm::diagnostic::Optimization(opt) => { @@ -283,33 +282,35 @@ unsafe extern "C" fn diagnostic_handler(info: &DiagnosticInfo, user: *mut c_void }; if enabled { - diag_handler.note_without_error(&format!("optimization {} for {} at {}:{}:{}: {}", - opt.kind.describe(), - opt.pass_name, - opt.filename, - opt.line, - opt.column, - opt.message)); + diag_handler.note_without_error(&format!( + "optimization {} for {} at {}:{}:{}: {}", + opt.kind.describe(), + opt.pass_name, + opt.filename, + opt.line, + opt.column, + opt.message + )); } } - llvm::diagnostic::PGO(diagnostic_ref) | - llvm::diagnostic::Linker(diagnostic_ref) => { + llvm::diagnostic::PGO(diagnostic_ref) | llvm::diagnostic::Linker(diagnostic_ref) => { let msg = llvm::build_string(|s| { llvm::LLVMRustWriteDiagnosticInfoToString(diagnostic_ref, s) - }).expect("non-UTF8 diagnostic"); + }) + .expect("non-UTF8 diagnostic"); diag_handler.warn(&msg); } - llvm::diagnostic::UnknownDiagnostic(..) => {}, + llvm::diagnostic::UnknownDiagnostic(..) => {} } } // Unsafe due to LLVM calls. -pub(crate) unsafe fn optimize(cgcx: &CodegenContext<LlvmCodegenBackend>, - diag_handler: &Handler, - module: &ModuleCodegen<ModuleLlvm>, - config: &ModuleConfig) - -> Result<(), FatalError> -{ +pub(crate) unsafe fn optimize( + cgcx: &CodegenContext<LlvmCodegenBackend>, + diag_handler: &Handler, + module: &ModuleCodegen<ModuleLlvm>, + config: &ModuleConfig, +) -> Result<(), FatalError> { let _timer = cgcx.prof.generic_activity("LLVM_module_optimize"); let llmod = module.module_llvm.llmod(); @@ -376,11 +377,15 @@ pub(crate) unsafe fn optimize(cgcx: &CodegenContext<LlvmCodegenBackend>, llvm::LLVMAddAnalysisPasses(tm, fpm); llvm::LLVMAddAnalysisPasses(tm, mpm); let opt_level = to_llvm_opt_settings(opt_level).0; - let prepare_for_thin_lto = cgcx.lto == Lto::Thin || cgcx.lto == Lto::ThinLocal || - (cgcx.lto != Lto::Fat && cgcx.opts.cg.linker_plugin_lto.enabled()); + let prepare_for_thin_lto = cgcx.lto == Lto::Thin + || cgcx.lto == Lto::ThinLocal + || (cgcx.lto != Lto::Fat && cgcx.opts.cg.linker_plugin_lto.enabled()); with_llvm_pmb(llmod, &config, opt_level, prepare_for_thin_lto, &mut |b| { llvm::LLVMRustAddLastExtensionPasses( - b, extra_passes.as_ptr(), extra_passes.len() as size_t); + b, + extra_passes.as_ptr(), + extra_passes.len() as size_t, + ); llvm::LLVMPassManagerBuilderPopulateFunctionPassManager(b, fpm); llvm::LLVMPassManagerBuilderPopulateModulePassManager(b, mpm); }); @@ -401,13 +406,17 @@ pub(crate) unsafe fn optimize(cgcx: &CodegenContext<LlvmCodegenBackend>, if using_thin_buffers && !have_name_anon_globals_pass { // As described above, this will probably cause an error in LLVM if config.no_prepopulate_passes { - diag_handler.err("The current compilation is going to use thin LTO buffers \ + diag_handler.err( + "The current compilation is going to use thin LTO buffers \ without running LLVM's NameAnonGlobals pass. \ This will likely cause errors in LLVM. Consider adding \ - -C passes=name-anon-globals to the compiler command line."); + -C passes=name-anon-globals to the compiler command line.", + ); } else { - bug!("We are using thin LTO buffers without running the NameAnonGlobals pass. \ - This will likely cause errors in LLVM and should never happen."); + bug!( + "We are using thin LTO buffers without running the NameAnonGlobals pass. \ + This will likely cause errors in LLVM and should never happen." + ); } } } @@ -417,19 +426,19 @@ pub(crate) unsafe fn optimize(cgcx: &CodegenContext<LlvmCodegenBackend>, // Finally, run the actual optimization passes { let _timer = cgcx.prof.generic_activity("LLVM_module_optimize_function_passes"); - time_ext(config.time_passes, - &format!("llvm function passes [{}]", module_name.unwrap()), - || { - llvm::LLVMRustRunFunctionPassManager(fpm, llmod) - }); + time_ext( + config.time_passes, + &format!("llvm function passes [{}]", module_name.unwrap()), + || llvm::LLVMRustRunFunctionPassManager(fpm, llmod), + ); } { let _timer = cgcx.prof.generic_activity("LLVM_module_optimize_module_passes"); - time_ext(config.time_passes, - &format!("llvm module passes [{}]", module_name.unwrap()), - || { - llvm::LLVMRunPassManager(mpm, llmod) - }); + time_ext( + config.time_passes, + &format!("llvm module passes [{}]", module_name.unwrap()), + || llvm::LLVMRunPassManager(mpm, llmod), + ); } // Deallocate managers that we're now done with @@ -439,9 +448,7 @@ pub(crate) unsafe fn optimize(cgcx: &CodegenContext<LlvmCodegenBackend>, Ok(()) } -unsafe fn add_sanitizer_passes(config: &ModuleConfig, - passes: &mut Vec<&'static mut llvm::Pass>) { - +unsafe fn add_sanitizer_passes(config: &ModuleConfig, passes: &mut Vec<&'static mut llvm::Pass>) { let sanitizer = match &config.sanitizer { None => return, Some(s) => s, @@ -464,12 +471,12 @@ unsafe fn add_sanitizer_passes(config: &ModuleConfig, } } -pub(crate) unsafe fn codegen(cgcx: &CodegenContext<LlvmCodegenBackend>, - diag_handler: &Handler, - module: ModuleCodegen<ModuleLlvm>, - config: &ModuleConfig) - -> Result<CompiledModule, FatalError> -{ +pub(crate) unsafe fn codegen( + cgcx: &CodegenContext<LlvmCodegenBackend>, + diag_handler: &Handler, + module: ModuleCodegen<ModuleLlvm>, + config: &ModuleConfig, +) -> Result<CompiledModule, FatalError> { let _timer = cgcx.prof.generic_activity("LLVM_module_codegen"); { let llmod = module.module_llvm.llmod(); @@ -491,11 +498,14 @@ pub(crate) unsafe fn codegen(cgcx: &CodegenContext<LlvmCodegenBackend>, // pass manager passed to the closure should be ensured to not // escape the closure itself, and the manager should only be // used once. - unsafe fn with_codegen<'ll, F, R>(tm: &'ll llvm::TargetMachine, - llmod: &'ll llvm::Module, - no_builtins: bool, - f: F) -> R - where F: FnOnce(&'ll mut PassManager<'ll>) -> R, + unsafe fn with_codegen<'ll, F, R>( + tm: &'ll llvm::TargetMachine, + llmod: &'ll llvm::Module, + no_builtins: bool, + f: F, + ) -> R + where + F: FnOnce(&'ll mut PassManager<'ll>) -> R, { let cpm = llvm::LLVMCreatePassManager(); llvm::LLVMAddAnalysisPasses(tm, cpm); @@ -519,7 +529,6 @@ pub(crate) unsafe fn codegen(cgcx: &CodegenContext<LlvmCodegenBackend>, let bc_out = cgcx.output_filenames.temp_path(OutputType::Bitcode, module_name); let obj_out = cgcx.output_filenames.temp_path(OutputType::Object, module_name); - if write_bc || config.emit_bc_compressed || config.embed_bitcode { let _timer = cgcx.prof.generic_activity("LLVM_module_codegen_make_bitcode"); let thin = ThinBuffer::new(llmod); @@ -552,88 +561,103 @@ pub(crate) unsafe fn codegen(cgcx: &CodegenContext<LlvmCodegenBackend>, embed_bitcode(cgcx, llcx, llmod, None); } - time_ext(config.time_passes, &format!("codegen passes [{}]", module_name.unwrap()), + time_ext( + config.time_passes, + &format!("codegen passes [{}]", module_name.unwrap()), || -> Result<(), FatalError> { - if config.emit_ir { - let _timer = cgcx.prof.generic_activity("LLVM_module_codegen_emit_ir"); - let out = cgcx.output_filenames.temp_path(OutputType::LlvmAssembly, module_name); - let out_c = path_to_c_string(&out); - - extern "C" fn demangle_callback(input_ptr: *const c_char, - input_len: size_t, - output_ptr: *mut c_char, - output_len: size_t) -> size_t { - let input = unsafe { - slice::from_raw_parts(input_ptr as *const u8, input_len as usize) - }; - - let input = match str::from_utf8(input) { - Ok(s) => s, - Err(_) => return 0, - }; - - let output = unsafe { - slice::from_raw_parts_mut(output_ptr as *mut u8, output_len as usize) - }; - let mut cursor = io::Cursor::new(output); - - let demangled = match rustc_demangle::try_demangle(input) { - Ok(d) => d, - Err(_) => return 0, - }; - - if let Err(_) = write!(cursor, "{:#}", demangled) { - // Possible only if provided buffer is not big enough - return 0; + if config.emit_ir { + let _timer = cgcx.prof.generic_activity("LLVM_module_codegen_emit_ir"); + let out = + cgcx.output_filenames.temp_path(OutputType::LlvmAssembly, module_name); + let out_c = path_to_c_string(&out); + + extern "C" fn demangle_callback( + input_ptr: *const c_char, + input_len: size_t, + output_ptr: *mut c_char, + output_len: size_t, + ) -> size_t { + let input = unsafe { + slice::from_raw_parts(input_ptr as *const u8, input_len as usize) + }; + + let input = match str::from_utf8(input) { + Ok(s) => s, + Err(_) => return 0, + }; + + let output = unsafe { + slice::from_raw_parts_mut(output_ptr as *mut u8, output_len as usize) + }; + let mut cursor = io::Cursor::new(output); + + let demangled = match rustc_demangle::try_demangle(input) { + Ok(d) => d, + Err(_) => return 0, + }; + + if let Err(_) = write!(cursor, "{:#}", demangled) { + // Possible only if provided buffer is not big enough + return 0; + } + + cursor.position() as size_t } - cursor.position() as size_t + let result = + llvm::LLVMRustPrintModule(llmod, out_c.as_ptr(), demangle_callback); + result.into_result().map_err(|()| { + let msg = format!("failed to write LLVM IR to {}", out.display()); + llvm_err(diag_handler, &msg) + })?; } - let result = - llvm::LLVMRustPrintModule(llmod, out_c.as_ptr(), demangle_callback); - result.into_result().map_err(|()| { - let msg = format!("failed to write LLVM IR to {}", out.display()); - llvm_err(diag_handler, &msg) - })?; - } - - if config.emit_asm || asm_to_obj { - let _timer = cgcx.prof.generic_activity("LLVM_module_codegen_emit_asm"); - let path = cgcx.output_filenames.temp_path(OutputType::Assembly, module_name); - - // We can't use the same module for asm and binary output, because that triggers - // various errors like invalid IR or broken binaries, so we might have to clone the - // module to produce the asm output - let llmod = if config.emit_obj { - llvm::LLVMCloneModule(llmod) - } else { - llmod - }; - with_codegen(tm, llmod, config.no_builtins, |cpm| { - write_output_file(diag_handler, tm, cpm, llmod, &path, - llvm::FileType::AssemblyFile) - })?; - } + if config.emit_asm || asm_to_obj { + let _timer = cgcx.prof.generic_activity("LLVM_module_codegen_emit_asm"); + let path = cgcx.output_filenames.temp_path(OutputType::Assembly, module_name); + + // We can't use the same module for asm and binary output, because that triggers + // various errors like invalid IR or broken binaries, so we might have to clone the + // module to produce the asm output + let llmod = if config.emit_obj { llvm::LLVMCloneModule(llmod) } else { llmod }; + with_codegen(tm, llmod, config.no_builtins, |cpm| { + write_output_file( + diag_handler, + tm, + cpm, + llmod, + &path, + llvm::FileType::AssemblyFile, + ) + })?; + } - if write_obj { - let _timer = cgcx.prof.generic_activity("LLVM_module_codegen_emit_obj"); - with_codegen(tm, llmod, config.no_builtins, |cpm| { - write_output_file(diag_handler, tm, cpm, llmod, &obj_out, - llvm::FileType::ObjectFile) - })?; - } else if asm_to_obj { - let _timer = cgcx.prof.generic_activity("LLVM_module_codegen_asm_to_obj"); - let assembly = cgcx.output_filenames.temp_path(OutputType::Assembly, module_name); - run_assembler(cgcx, diag_handler, &assembly, &obj_out); - - if !config.emit_asm && !cgcx.save_temps { - drop(fs::remove_file(&assembly)); + if write_obj { + let _timer = cgcx.prof.generic_activity("LLVM_module_codegen_emit_obj"); + with_codegen(tm, llmod, config.no_builtins, |cpm| { + write_output_file( + diag_handler, + tm, + cpm, + llmod, + &obj_out, + llvm::FileType::ObjectFile, + ) + })?; + } else if asm_to_obj { + let _timer = cgcx.prof.generic_activity("LLVM_module_codegen_asm_to_obj"); + let assembly = + cgcx.output_filenames.temp_path(OutputType::Assembly, module_name); + run_assembler(cgcx, diag_handler, &assembly, &obj_out); + + if !config.emit_asm && !cgcx.save_temps { + drop(fs::remove_file(&assembly)); + } } - } - Ok(()) - })?; + Ok(()) + }, + )?; if copy_bc_to_obj { debug!("copying bitcode {:?} to obj {:?}", bc_out, obj_out); @@ -651,10 +675,12 @@ pub(crate) unsafe fn codegen(cgcx: &CodegenContext<LlvmCodegenBackend>, drop(handlers); } - Ok(module.into_compiled_module(config.emit_obj, - config.emit_bc, - config.emit_bc_compressed, - &cgcx.output_filenames)) + Ok(module.into_compiled_module( + config.emit_obj, + config.emit_bc, + config.emit_bc_compressed, + &cgcx.output_filenames, + )) } /// Embed the bitcode of an LLVM module in the LLVM module itself. @@ -675,10 +701,12 @@ pub(crate) unsafe fn codegen(cgcx: &CodegenContext<LlvmCodegenBackend>, /// /// Basically all of this is us attempting to follow in the footsteps of clang /// on iOS. See #35968 for lots more info. -unsafe fn embed_bitcode(cgcx: &CodegenContext<LlvmCodegenBackend>, - llcx: &llvm::Context, - llmod: &llvm::Module, - bitcode: Option<&[u8]>) { +unsafe fn embed_bitcode( + cgcx: &CodegenContext<LlvmCodegenBackend>, + llcx: &llvm::Context, + llmod: &llvm::Module, + bitcode: Option<&[u8]>, +) { let llconst = common::bytes_in_context(llcx, bitcode.unwrap_or(&[])); let llglobal = llvm::LLVMAddGlobal( llmod, @@ -687,14 +715,10 @@ unsafe fn embed_bitcode(cgcx: &CodegenContext<LlvmCodegenBackend>, ); llvm::LLVMSetInitializer(llglobal, llconst); - let is_apple = cgcx.opts.target_triple.triple().contains("-ios") || - cgcx.opts.target_triple.triple().contains("-darwin"); + let is_apple = cgcx.opts.target_triple.triple().contains("-ios") + || cgcx.opts.target_triple.triple().contains("-darwin"); - let section = if is_apple { - "__LLVM,__bitcode\0" - } else { - ".llvmbc\0" - }; + let section = if is_apple { "__LLVM,__bitcode\0" } else { ".llvmbc\0" }; llvm::LLVMSetSection(llglobal, section.as_ptr().cast()); llvm::LLVMRustSetLinkage(llglobal, llvm::Linkage::PrivateLinkage); llvm::LLVMSetGlobalConstant(llglobal, llvm::True); @@ -706,28 +730,26 @@ unsafe fn embed_bitcode(cgcx: &CodegenContext<LlvmCodegenBackend>, "rustc.embedded.cmdline\0".as_ptr().cast(), ); llvm::LLVMSetInitializer(llglobal, llconst); - let section = if is_apple { - "__LLVM,__cmdline\0" - } else { - ".llvmcmd\0" - }; + let section = if is_apple { "__LLVM,__cmdline\0" } else { ".llvmcmd\0" }; llvm::LLVMSetSection(llglobal, section.as_ptr().cast()); llvm::LLVMRustSetLinkage(llglobal, llvm::Linkage::PrivateLinkage); } -pub unsafe fn with_llvm_pmb(llmod: &llvm::Module, - config: &ModuleConfig, - opt_level: llvm::CodeGenOptLevel, - prepare_for_thin_lto: bool, - f: &mut dyn FnMut(&llvm::PassManagerBuilder)) { +pub unsafe fn with_llvm_pmb( + llmod: &llvm::Module, + config: &ModuleConfig, + opt_level: llvm::CodeGenOptLevel, + prepare_for_thin_lto: bool, + f: &mut dyn FnMut(&llvm::PassManagerBuilder), +) { use std::ptr; // Create the PassManagerBuilder for LLVM. We configure it with // reasonable defaults and prepare it to actually populate the pass // manager. let builder = llvm::LLVMPassManagerBuilderCreate(); - let opt_size = config.opt_size.map(|x| to_llvm_opt_settings(x).1) - .unwrap_or(llvm::CodeGenOptSizeNone); + let opt_size = + config.opt_size.map(|x| to_llvm_opt_settings(x).1).unwrap_or(llvm::CodeGenOptSizeNone); let inline_threshold = config.inline_threshold; let pgo_gen_path = match config.pgo_gen { @@ -740,14 +762,13 @@ pub unsafe fn with_llvm_pmb(llmod: &llvm::Module, Some(CString::new(format!("{}", path.display())).unwrap()) } - SwitchWithOptPath::Disabled => { - None - } + SwitchWithOptPath::Disabled => None, }; - let pgo_use_path = config.pgo_use.as_ref().map(|path_buf| { - CString::new(path_buf.to_string_lossy().as_bytes()).unwrap() - }); + let pgo_use_path = config + .pgo_use + .as_ref() + .map(|path_buf| CString::new(path_buf.to_string_lossy().as_bytes()).unwrap()); llvm::LLVMRustConfigurePassManagerBuilder( builder, @@ -794,9 +815,7 @@ pub unsafe fn with_llvm_pmb(llmod: &llvm::Module, (llvm::CodeGenOptLevel::Default, ..) => { llvm::LLVMPassManagerBuilderUseInlinerWithThreshold(builder, 225); } - (llvm::CodeGenOptLevel::Other, ..) => { - bug!("CodeGenOptLevel::Other selected") - } + (llvm::CodeGenOptLevel::Other, ..) => bug!("CodeGenOptLevel::Other selected"), } f(builder); @@ -811,36 +830,28 @@ pub unsafe fn with_llvm_pmb(llmod: &llvm::Module, fn create_msvc_imps( cgcx: &CodegenContext<LlvmCodegenBackend>, llcx: &llvm::Context, - llmod: &llvm::Module + llmod: &llvm::Module, ) { if !cgcx.msvc_imps_needed { - return + return; } // The x86 ABI seems to require that leading underscores are added to symbol // names, so we need an extra underscore on x86. There's also a leading // '\x01' here which disables LLVM's symbol mangling (e.g., no extra // underscores added in front). - let prefix = if cgcx.target_arch == "x86" { - "\x01__imp__" - } else { - "\x01__imp_" - }; + let prefix = if cgcx.target_arch == "x86" { "\x01__imp__" } else { "\x01__imp_" }; unsafe { let i8p_ty = Type::i8p_llcx(llcx); let globals = base::iter_globals(llmod) .filter(|&val| { - llvm::LLVMRustGetLinkage(val) == llvm::Linkage::ExternalLinkage && - llvm::LLVMIsDeclaration(val) == 0 + llvm::LLVMRustGetLinkage(val) == llvm::Linkage::ExternalLinkage + && llvm::LLVMIsDeclaration(val) == 0 }) .filter_map(|val| { // Exclude some symbols that we know are not Rust symbols. let name = llvm::get_value_name(val); - if ignored(name) { - None - } else { - Some((val, name)) - } + if ignored(name) { None } else { Some((val, name)) } }) .map(move |(val, name)| { let mut imp_name = prefix.as_bytes().to_vec(); @@ -851,9 +862,7 @@ fn create_msvc_imps( .collect::<Vec<_>>(); for (imp_name, val) in globals { - let imp = llvm::LLVMAddGlobal(llmod, - i8p_ty, - imp_name.as_ptr().cast()); + let imp = llvm::LLVMAddGlobal(llmod, i8p_ty, imp_name.as_ptr().cast()); llvm::LLVMSetInitializer(imp, consts::ptrcast(val, i8p_ty)); llvm::LLVMRustSetLinkage(imp, llvm::Linkage::ExternalLinkage); } diff --git a/src/librustc_codegen_llvm/consts.rs b/src/librustc_codegen_llvm/consts.rs index 78a86d33a14..12c44513159 100644 --- a/src/librustc_codegen_llvm/consts.rs +++ b/src/librustc_codegen_llvm/consts.rs @@ -1,27 +1,26 @@ -use crate::llvm::{self, SetUnnamedAddr, True}; -use crate::debuginfo; -use crate::common::CodegenCx; use crate::base; +use crate::common::CodegenCx; +use crate::debuginfo; +use crate::llvm::{self, SetUnnamedAddr, True}; use crate::type_::Type; use crate::type_of::LayoutLlvmExt; use crate::value::Value; use libc::c_uint; +use log::debug; use rustc::hir::def_id::DefId; -use rustc::mir::interpret::{ConstValue, Allocation, read_target_uint, - Pointer, ErrorHandled}; -use rustc::mir::mono::MonoItem; use rustc::hir::Node; -use rustc_target::abi::HasDataLayout; -use rustc::ty::{self, Ty, Instance}; +use rustc::mir::interpret::{read_target_uint, Allocation, ConstValue, ErrorHandled, Pointer}; +use rustc::mir::mono::MonoItem; +use rustc::ty::{self, Instance, Ty}; +use rustc::{bug, span_bug}; use rustc_codegen_ssa::traits::*; -use syntax::symbol::{Symbol, sym}; +use rustc_target::abi::HasDataLayout; +use syntax::symbol::{sym, Symbol}; use syntax_pos::Span; -use rustc::{bug, span_bug}; -use log::debug; -use rustc::ty::layout::{self, Size, Align, LayoutOf}; +use rustc::ty::layout::{self, Align, LayoutOf, Size}; -use rustc::hir::{self, CodegenFnAttrs, CodegenFnAttrFlags}; +use rustc::hir::{self, CodegenFnAttrFlags, CodegenFnAttrs}; use std::ffi::CStr; @@ -51,14 +50,13 @@ pub fn const_alloc_to_llvm(cx: &CodegenCx<'ll, '_>, alloc: &Allocation) -> &'ll // affect interpreter execution (we inspect the result after interpreter execution), // and we properly interpret the relocation as a relocation pointer offset. alloc.inspect_with_undef_and_ptr_outside_interpreter(offset..(offset + pointer_size)), - ).expect("const_alloc_to_llvm: could not read relocation pointer") as u64; + ) + .expect("const_alloc_to_llvm: could not read relocation pointer") + as u64; llvals.push(cx.scalar_to_backend( Pointer::new(alloc_id, Size::from_bytes(ptr_offset)).into(), - &layout::Scalar { - value: layout::Primitive::Pointer, - valid_range: 0..=!0 - }, - cx.type_i8p() + &layout::Scalar { value: layout::Primitive::Pointer, valid_range: 0..=!0 }, + cx.type_i8p(), )); next_offset = offset + pointer_size; } @@ -84,19 +82,13 @@ pub fn codegen_static_initializer( let static_ = cx.tcx.const_eval_poly(def_id)?; let alloc = match static_.val { - ty::ConstKind::Value(ConstValue::ByRef { - alloc, offset, - }) if offset.bytes() == 0 => { - alloc - }, + ty::ConstKind::Value(ConstValue::ByRef { alloc, offset }) if offset.bytes() == 0 => alloc, _ => bug!("static const eval returned {:#?}", static_), }; Ok((const_alloc_to_llvm(cx, alloc), alloc)) } -fn set_global_alignment(cx: &CodegenCx<'ll, '_>, - gv: &'ll Value, - mut align: Align) { +fn set_global_alignment(cx: &CodegenCx<'ll, '_>, gv: &'ll Value, mut align: Align) { // The target may require greater alignment for globals than the type does. // Note: GCC and Clang also allow `__attribute__((aligned))` on variables, // which can force it to be smaller. Rust doesn't support this yet. @@ -118,7 +110,7 @@ fn check_and_apply_linkage( attrs: &CodegenFnAttrs, ty: Ty<'tcx>, sym: Symbol, - span: Span + span: Span, ) -> &'ll Value { let llty = cx.layout_of(ty).llvm_type(cx); let sym = sym.as_str(); @@ -134,7 +126,9 @@ fn check_and_apply_linkage( cx.layout_of(mt.ty).llvm_type(cx) } else { cx.sess().span_fatal( - span, "must have type `*const T` or `*mut T` due to `#[linkage]` attribute") + span, + "must have type `*const T` or `*mut T` due to `#[linkage]` attribute", + ) }; unsafe { // Declare a symbol `foo` with the desired linkage. @@ -149,7 +143,7 @@ fn check_and_apply_linkage( // zero. let mut real_name = "_rust_extern_with_linkage_".to_string(); real_name.push_str(&sym); - let g2 = cx.define_global(&real_name, llty).unwrap_or_else(||{ + let g2 = cx.define_global(&real_name, llty).unwrap_or_else(|| { cx.sess().span_fatal(span, &format!("symbol `{}` is already defined", &sym)) }); llvm::LLVMRustSetLinkage(g2, llvm::Linkage::InternalLinkage); @@ -164,16 +158,12 @@ fn check_and_apply_linkage( } pub fn ptrcast(val: &'ll Value, ty: &'ll Type) -> &'ll Value { - unsafe { - llvm::LLVMConstPointerCast(val, ty) - } + unsafe { llvm::LLVMConstPointerCast(val, ty) } } impl CodegenCx<'ll, 'tcx> { crate fn const_bitcast(&self, val: &'ll Value, ty: &'ll Type) -> &'ll Value { - unsafe { - llvm::LLVMConstBitCast(val, ty) - } + unsafe { llvm::LLVMConstBitCast(val, ty) } } crate fn static_addr_of_mut( @@ -186,13 +176,12 @@ impl CodegenCx<'ll, 'tcx> { let gv = match kind { Some(kind) if !self.tcx.sess.fewer_names() => { let name = self.generate_local_symbol_name(kind); - let gv = self.define_global(&name[..], - self.val_ty(cv)).unwrap_or_else(||{ - bug!("symbol `{}` is already defined", name); + let gv = self.define_global(&name[..], self.val_ty(cv)).unwrap_or_else(|| { + bug!("symbol `{}` is already defined", name); }); llvm::LLVMRustSetLinkage(gv, llvm::Linkage::PrivateLinkage); gv - }, + } _ => self.define_private_global(self.val_ty(cv)), }; llvm::LLVMSetInitializer(gv, cv); @@ -208,13 +197,14 @@ impl CodegenCx<'ll, 'tcx> { return g; } - let defined_in_current_codegen_unit = self.codegen_unit - .items() - .contains_key(&MonoItem::Static(def_id)); - assert!(!defined_in_current_codegen_unit, - "consts::get_static() should always hit the cache for \ + let defined_in_current_codegen_unit = + self.codegen_unit.items().contains_key(&MonoItem::Static(def_id)); + assert!( + !defined_in_current_codegen_unit, + "consts::get_static() should always hit the cache for \ statics defined in the same CGU, but did not for `{:?}`", - def_id); + def_id + ); let ty = instance.ty(self.tcx); let sym = self.tcx.symbol_name(instance).name; @@ -222,12 +212,9 @@ impl CodegenCx<'ll, 'tcx> { debug!("get_static: sym={} instance={:?}", sym, instance); let g = if let Some(id) = self.tcx.hir().as_local_hir_id(def_id) { - let llty = self.layout_of(ty).llvm_type(self); let (g, attrs) = match self.tcx.hir().get(id) { - Node::Item(&hir::Item { - attrs, span, kind: hir::ItemKind::Static(..), .. - }) => { + Node::Item(&hir::Item { attrs, span, kind: hir::ItemKind::Static(..), .. }) => { let sym_str = sym.as_str(); if let Some(g) = self.get_declared_value(&sym_str) { if self.val_ty(g) != self.type_ptr_to(llty) { @@ -247,13 +234,16 @@ impl CodegenCx<'ll, 'tcx> { } Node::ForeignItem(&hir::ForeignItem { - ref attrs, span, kind: hir::ForeignItemKind::Static(..), .. + ref attrs, + span, + kind: hir::ForeignItemKind::Static(..), + .. }) => { let fn_attrs = self.tcx.codegen_fn_attrs(def_id); (check_and_apply_linkage(&self, &fn_attrs, ty, sym, span), &**attrs) } - item => bug!("get_static: expected static, found {:?}", item) + item => bug!("get_static: expected static, found {:?}", item), }; debug!("get_static: sym={} attrs={:?}", sym, attrs); @@ -283,8 +273,7 @@ impl CodegenCx<'ll, 'tcx> { llvm::set_thread_local_mode(g, self.tls_model); } - let needs_dll_storage_attr = - self.use_dll_storage_attrs && !self.tcx.is_foreign_item(def_id) && + let needs_dll_storage_attr = self.use_dll_storage_attrs && !self.tcx.is_foreign_item(def_id) && // ThinLTO can't handle this workaround in all cases, so we don't // emit the attrs. Instead we make them unnecessary by disallowing // dynamic linking when linker plugin based LTO is enabled. @@ -292,9 +281,11 @@ impl CodegenCx<'ll, 'tcx> { // If this assertion triggers, there's something wrong with commandline // argument validation. - debug_assert!(!(self.tcx.sess.opts.cg.linker_plugin_lto.enabled() && - self.tcx.sess.target.target.options.is_like_msvc && - self.tcx.sess.opts.cg.prefer_dynamic)); + debug_assert!( + !(self.tcx.sess.opts.cg.linker_plugin_lto.enabled() + && self.tcx.sess.target.target.options.is_like_msvc + && self.tcx.sess.opts.cg.prefer_dynamic) + ); if needs_dll_storage_attr { // This item is external but not foreign, i.e., it originates from an external Rust @@ -329,12 +320,7 @@ impl CodegenCx<'ll, 'tcx> { } impl StaticMethods for CodegenCx<'ll, 'tcx> { - fn static_addr_of( - &self, - cv: &'ll Value, - align: Align, - kind: Option<&str>, - ) -> &'ll Value { + fn static_addr_of(&self, cv: &'ll Value, align: Align, kind: Option<&str>) -> &'ll Value { if let Some(&gv) = self.const_globals.borrow().get(&cv) { unsafe { // Upgrade the alignment in cases where the same constant is used with different @@ -354,11 +340,7 @@ impl StaticMethods for CodegenCx<'ll, 'tcx> { gv } - fn codegen_static( - &self, - def_id: DefId, - is_mutable: bool, - ) { + fn codegen_static(&self, def_id: DefId, is_mutable: bool) { unsafe { let attrs = self.tcx.codegen_fn_attrs(def_id); @@ -395,7 +377,11 @@ impl StaticMethods for CodegenCx<'ll, 'tcx> { let visibility = llvm::LLVMRustGetVisibility(g); let new_g = llvm::LLVMRustGetOrInsertGlobal( - self.llmod, name.as_ptr().cast(), name.len(), val_llty); + self.llmod, + name.as_ptr().cast(), + name.len(), + val_llty, + ); llvm::LLVMRustSetLinkage(new_g, linkage); llvm::LLVMRustSetVisibility(new_g, visibility); @@ -464,7 +450,8 @@ impl StaticMethods for CodegenCx<'ll, 'tcx> { // The `inspect` method is okay here because we checked relocations, and // because we are doing this access to inspect the final interpreter state // (not as part of the interpreter execution). - alloc.inspect_with_undef_and_ptr_outside_interpreter(0..alloc.len()) + alloc + .inspect_with_undef_and_ptr_outside_interpreter(0..alloc.len()) .iter() .all(|b| *b == 0) }; @@ -477,7 +464,6 @@ impl StaticMethods for CodegenCx<'ll, 'tcx> { } } - // Wasm statics with custom link sections get special treatment as they // go into custom sections of the wasm executable. if self.tcx.sess.opts.target_triple.triple().starts_with("wasm32") { @@ -492,8 +478,8 @@ impl StaticMethods for CodegenCx<'ll, 'tcx> { // The `inspect` method is okay here because we checked relocations, and // because we are doing this access to inspect the final interpreter state (not // as part of the interpreter execution). - let bytes = alloc.inspect_with_undef_and_ptr_outside_interpreter( - 0..alloc.len()); + let bytes = + alloc.inspect_with_undef_and_ptr_outside_interpreter(0..alloc.len()); let alloc = llvm::LLVMMDStringInContext( self.llcx, bytes.as_ptr().cast(), diff --git a/src/librustc_codegen_llvm/debuginfo/metadata.rs b/src/librustc_codegen_llvm/debuginfo/metadata.rs index 8327ff257c2..e3c2dfac58b 100644 --- a/src/librustc_codegen_llvm/debuginfo/metadata.rs +++ b/src/librustc_codegen_llvm/debuginfo/metadata.rs @@ -1,56 +1,60 @@ -use self::RecursiveTypeDescription::*; -use self::MemberDescriptionFactory::*; use self::EnumDiscriminantInfo::*; +use self::MemberDescriptionFactory::*; +use self::RecursiveTypeDescription::*; -use super::utils::{debug_context, DIB, span_start, - get_namespace_for_item, create_DIArray, is_node_local_to_unit}; use super::namespace::mangled_name_of_instance; use super::type_names::compute_debuginfo_type_name; +use super::utils::{ + create_DIArray, debug_context, get_namespace_for_item, is_node_local_to_unit, span_start, DIB, +}; use super::CrateDebugContext; use crate::abi; use crate::common::CodegenCx; use crate::llvm; -use crate::llvm::debuginfo::{DIArray, DIType, DIFile, DIScope, DIDescriptor, - DICompositeType, DILexicalBlock, DIFlags, DebugEmissionKind}; +use crate::llvm::debuginfo::{ + DIArray, DICompositeType, DIDescriptor, DIFile, DIFlags, DILexicalBlock, DIScope, DIType, + DebugEmissionKind, +}; use crate::llvm_util; use crate::value::Value; -use rustc_codegen_ssa::traits::*; -use rustc_index::vec::{Idx, IndexVec}; -use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; -use rustc::hir::CodegenFnAttrFlags; +use log::debug; use rustc::hir::def::CtorKind; -use rustc::hir::def_id::{DefId, CrateNum, LOCAL_CRATE}; +use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE}; +use rustc::hir::CodegenFnAttrFlags; use rustc::ich::NodeIdHashingMode; -use rustc::mir::{self, Field, GeneratorLayout}; use rustc::mir::interpret::truncate; -use rustc_data_structures::fingerprint::Fingerprint; +use rustc::mir::{self, Field, GeneratorLayout}; +use rustc::session::config::{self, DebugInfo}; +use rustc::ty::layout::{ + self, Align, Integer, IntegerExt, LayoutOf, PrimitiveExt, Size, TyLayout, VariantIdx, +}; +use rustc::ty::subst::{GenericArgKind, SubstsRef}; use rustc::ty::Instance; use rustc::ty::{self, AdtKind, ParamEnv, Ty, TyCtxt}; -use rustc::ty::layout::{self, Align, Integer, IntegerExt, LayoutOf, - PrimitiveExt, Size, TyLayout, VariantIdx}; -use rustc::ty::subst::{GenericArgKind, SubstsRef}; -use rustc::session::config::{self, DebugInfo}; use rustc::util::nodemap::FxHashMap; -use rustc_fs_util::path_to_c_string; -use rustc_data_structures::small_c_str::SmallCStr; +use rustc::{bug, span_bug}; +use rustc_codegen_ssa::traits::*; use rustc_data_structures::const_cstr; +use rustc_data_structures::fingerprint::Fingerprint; +use rustc_data_structures::small_c_str::SmallCStr; +use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; +use rustc_fs_util::path_to_c_string; +use rustc_index::vec::{Idx, IndexVec}; use rustc_target::abi::HasDataLayout; use syntax::ast; use syntax::symbol::{Interner, Symbol}; -use syntax_pos::{self, Span, FileName}; -use rustc::{bug, span_bug}; -use log::debug; +use syntax_pos::{self, FileName, Span}; -use libc::{c_uint, c_longlong}; +use libc::{c_longlong, c_uint}; use std::collections::hash_map::Entry; use std::ffi::CString; use std::fmt::{self, Write}; use std::hash::{Hash, Hasher}; use std::iter; -use std::ptr; use std::path::{Path, PathBuf}; +use std::ptr; impl PartialEq for llvm::Metadata { fn eq(&self, other: &Self) -> bool { @@ -107,17 +111,13 @@ pub struct TypeMap<'ll, 'tcx> { /// A map from types to debuginfo metadata. This is an N:1 mapping. type_to_metadata: FxHashMap<Ty<'tcx>, &'ll DIType>, /// A map from types to `UniqueTypeId`. This is an N:1 mapping. - type_to_unique_id: FxHashMap<Ty<'tcx>, UniqueTypeId> + type_to_unique_id: FxHashMap<Ty<'tcx>, UniqueTypeId>, } impl TypeMap<'ll, 'tcx> { /// Adds a Ty to metadata mapping to the TypeMap. The method will fail if /// the mapping already exists. - fn register_type_with_metadata( - &mut self, - type_: Ty<'tcx>, - metadata: &'ll DIType, - ) { + fn register_type_with_metadata(&mut self, type_: Ty<'tcx>, metadata: &'ll DIType) { if self.type_to_metadata.insert(type_, metadata).is_some() { bug!("type metadata for `Ty` '{}' is already in the `TypeMap`!", type_); } @@ -140,10 +140,7 @@ impl TypeMap<'ll, 'tcx> { /// /// This function is used to remove the temporary metadata /// mapping after we've computed the actual metadata. - fn remove_type( - &mut self, - type_: Ty<'tcx>, - ) { + fn remove_type(&mut self, type_: Ty<'tcx>) { if self.type_to_metadata.remove(type_).is_none() { bug!("type metadata `Ty` '{}' is not in the `TypeMap`!", type_); } @@ -157,8 +154,10 @@ impl TypeMap<'ll, 'tcx> { metadata: &'ll DIType, ) { if self.unique_id_to_metadata.insert(unique_type_id, metadata).is_some() { - bug!("type metadata for unique ID '{}' is already in the `TypeMap`!", - self.get_unique_type_id_as_string(unique_type_id)); + bug!( + "type metadata for unique ID '{}' is already in the `TypeMap`!", + self.get_unique_type_id_as_string(unique_type_id) + ); } } @@ -180,8 +179,11 @@ impl TypeMap<'ll, 'tcx> { /// Gets the `UniqueTypeId` for the given type. If the `UniqueTypeId` for the given /// type has been requested before, this is just a table lookup. Otherwise, an /// ID will be generated and stored for later lookup. - fn get_unique_type_id_of_type<'a>(&mut self, cx: &CodegenCx<'a, 'tcx>, - type_: Ty<'tcx>) -> UniqueTypeId { + fn get_unique_type_id_of_type<'a>( + &mut self, + cx: &CodegenCx<'a, 'tcx>, + type_: Ty<'tcx>, + ) -> UniqueTypeId { // Let's see if we already have something in the cache. if let Some(unique_type_id) = self.type_to_unique_id.get(&type_).cloned() { return unique_type_id; @@ -209,15 +211,15 @@ impl TypeMap<'ll, 'tcx> { /// Gets the `UniqueTypeId` for an enum variant. Enum variants are not really /// types of their own, so they need special handling. We still need a /// `UniqueTypeId` for them, since to debuginfo they *are* real types. - fn get_unique_type_id_of_enum_variant<'a>(&mut self, - cx: &CodegenCx<'a, 'tcx>, - enum_type: Ty<'tcx>, - variant_name: &str) - -> UniqueTypeId { + fn get_unique_type_id_of_enum_variant<'a>( + &mut self, + cx: &CodegenCx<'a, 'tcx>, + enum_type: Ty<'tcx>, + variant_name: &str, + ) -> UniqueTypeId { let enum_type_id = self.get_unique_type_id_of_type(cx, enum_type); - let enum_variant_type_id = format!("{}::{}", - self.get_unique_type_id_as_string(enum_type_id), - variant_name); + let enum_variant_type_id = + format!("{}::{}", self.get_unique_type_id_as_string(enum_type_id), variant_name); let interner_key = self.unique_id_interner.intern(&enum_variant_type_id); UniqueTypeId(interner_key) } @@ -226,8 +228,8 @@ impl TypeMap<'ll, 'tcx> { /// Variant parts are not types and shouldn't really have their own ID, /// but it makes `set_members_of_composite_type()` simpler. fn get_unique_type_id_str_of_enum_variant_part(&mut self, enum_type_id: UniqueTypeId) -> &str { - let variant_part_type_id = format!("{}_variant_part", - self.get_unique_type_id_as_string(enum_type_id)); + let variant_part_type_id = + format!("{}_variant_part", self.get_unique_type_id_as_string(enum_type_id)); let interner_key = self.unique_id_interner.intern(&variant_part_type_id); self.unique_id_interner.get(interner_key) } @@ -246,7 +248,7 @@ enum RecursiveTypeDescription<'ll, 'tcx> { member_holding_stub: &'ll DICompositeType, member_description_factory: MemberDescriptionFactory<'ll, 'tcx>, }, - FinalMetadata(&'ll DICompositeType) + FinalMetadata(&'ll DICompositeType), } fn create_and_register_recursive_type_forward_declaration( @@ -257,7 +259,6 @@ fn create_and_register_recursive_type_forward_declaration( member_holding_stub: &'ll DICompositeType, member_description_factory: MemberDescriptionFactory<'ll, 'tcx>, ) -> RecursiveTypeDescription<'ll, 'tcx> { - // Insert the stub into the `TypeMap` in order to allow for recursive references. let mut type_map = debug_context(cx).type_map.borrow_mut(); type_map.register_unique_id_with_metadata(unique_type_id, metadata_stub); @@ -294,23 +295,27 @@ impl RecursiveTypeDescription<'ll, 'tcx> { // function. { let type_map = debug_context(cx).type_map.borrow(); - if type_map.find_metadata_for_unique_id(unique_type_id).is_none() || - type_map.find_metadata_for_type(unfinished_type).is_none() { - bug!("Forward declaration of potentially recursive type \ + if type_map.find_metadata_for_unique_id(unique_type_id).is_none() + || type_map.find_metadata_for_type(unfinished_type).is_none() + { + bug!( + "Forward declaration of potentially recursive type \ '{:?}' was not found in TypeMap!", - unfinished_type); + unfinished_type + ); } } // ... then create the member descriptions ... - let member_descriptions = - member_description_factory.create_member_descriptions(cx); + let member_descriptions = member_description_factory.create_member_descriptions(cx); // ... and attach them to the stub to complete it. - set_members_of_composite_type(cx, - unfinished_type, - member_holding_stub, - member_descriptions); + set_members_of_composite_type( + cx, + unfinished_type, + member_holding_stub, + member_descriptions, + ); return MetadataCreationResult::new(metadata_stub, true); } } @@ -320,14 +325,13 @@ impl RecursiveTypeDescription<'ll, 'tcx> { /// Returns from the enclosing function if the type metadata with the given /// unique ID can be found in the type map. macro_rules! return_if_metadata_created_in_meantime { - ($cx: expr, $unique_type_id: expr) => ( - if let Some(metadata) = debug_context($cx).type_map - .borrow() - .find_metadata_for_unique_id($unique_type_id) + ($cx: expr, $unique_type_id: expr) => { + if let Some(metadata) = + debug_context($cx).type_map.borrow().find_metadata_for_unique_id($unique_type_id) { return MetadataCreationResult::new(metadata, true); } - ) + }; } fn fixed_vec_metadata( @@ -345,12 +349,11 @@ fn fixed_vec_metadata( let upper_bound = match array_or_slice_type.kind { ty::Array(_, len) => len.eval_usize(cx.tcx, ty::ParamEnv::reveal_all()) as c_longlong, - _ => -1 + _ => -1, }; - let subrange = unsafe { - Some(llvm::LLVMRustDIBuilderGetOrCreateSubrange(DIB(cx), 0, upper_bound)) - }; + let subrange = + unsafe { Some(llvm::LLVMRustDIBuilderGetOrCreateSubrange(DIB(cx), 0, upper_bound)) }; let subscripts = create_DIArray(DIB(cx), &[subrange]); let metadata = unsafe { @@ -359,7 +362,8 @@ fn fixed_vec_metadata( size.bits(), align.bits() as u32, element_type_metadata, - subscripts) + subscripts, + ) }; return MetadataCreationResult::new(metadata, false); @@ -406,14 +410,16 @@ fn vec_slice_metadata( let file_metadata = unknown_file_metadata(cx); - let metadata = composite_type_metadata(cx, - slice_ptr_type, - &slice_type_name[..], - unique_type_id, - member_descriptions, - NO_SCOPE_METADATA, - file_metadata, - span); + let metadata = composite_type_metadata( + cx, + slice_ptr_type, + &slice_type_name[..], + unique_type_id, + member_descriptions, + NO_SCOPE_METADATA, + file_metadata, + span, + ); MetadataCreationResult::new(metadata, false) } @@ -423,23 +429,21 @@ fn subroutine_type_metadata( signature: ty::PolyFnSig<'tcx>, span: Span, ) -> MetadataCreationResult<'ll> { - let signature = cx.tcx.normalize_erasing_late_bound_regions( - ty::ParamEnv::reveal_all(), - &signature, - ); + let signature = + cx.tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &signature); let signature_metadata: Vec<_> = iter::once( // return type match signature.output().kind { ty::Tuple(ref tys) if tys.is_empty() => None, - _ => Some(type_metadata(cx, signature.output(), span)) - } - ).chain( + _ => Some(type_metadata(cx, signature.output(), span)), + }, + ) + .chain( // regular arguments - signature.inputs().iter().map(|argument_type| { - Some(type_metadata(cx, argument_type, span)) - }) - ).collect(); + signature.inputs().iter().map(|argument_type| Some(type_metadata(cx, argument_type, span))), + ) + .collect(); return_if_metadata_created_in_meantime!(cx, unique_type_id); @@ -448,9 +452,11 @@ fn subroutine_type_metadata( llvm::LLVMRustDIBuilderCreateSubroutineType( DIB(cx), unknown_file_metadata(cx), - create_DIArray(DIB(cx), &signature_metadata[..])) + create_DIArray(DIB(cx), &signature_metadata[..]), + ) }, - false); + false, + ); } // FIXME(1563): This is all a bit of a hack because 'trait pointer' is an ill- @@ -470,18 +476,20 @@ fn trait_pointer_metadata( // However, it does not describe the trait's methods. let containing_scope = match trait_type.kind { - ty::Dynamic(ref data, ..) => - data.principal_def_id().map(|did| get_namespace_for_item(cx, did)), + ty::Dynamic(ref data, ..) => { + data.principal_def_id().map(|did| get_namespace_for_item(cx, did)) + } _ => { - bug!("debuginfo: unexpected trait-object type in \ + bug!( + "debuginfo: unexpected trait-object type in \ trait_pointer_metadata(): {:?}", - trait_type); + trait_type + ); } }; let trait_object_type = trait_object_type.unwrap_or(trait_type); - let trait_type_name = - compute_debuginfo_type_name(cx.tcx, trait_object_type, false); + let trait_type_name = compute_debuginfo_type_name(cx.tcx, trait_object_type, false); let file_metadata = unknown_file_metadata(cx); @@ -495,9 +503,11 @@ fn trait_pointer_metadata( let member_descriptions = vec![ MemberDescription { name: "pointer".to_owned(), - type_metadata: type_metadata(cx, + type_metadata: type_metadata( + cx, cx.tcx.mk_mut_ptr(cx.tcx.types.u8), - syntax_pos::DUMMY_SP), + syntax_pos::DUMMY_SP, + ), offset: layout.fields.offset(0), size: data_ptr_field.size, align: data_ptr_field.align.abi, @@ -515,21 +525,19 @@ fn trait_pointer_metadata( }, ]; - composite_type_metadata(cx, - trait_object_type, - &trait_type_name[..], - unique_type_id, - member_descriptions, - containing_scope, - file_metadata, - syntax_pos::DUMMY_SP) + composite_type_metadata( + cx, + trait_object_type, + &trait_type_name[..], + unique_type_id, + member_descriptions, + containing_scope, + file_metadata, + syntax_pos::DUMMY_SP, + ) } -pub fn type_metadata( - cx: &CodegenCx<'ll, 'tcx>, - t: Ty<'tcx>, - usage_site_span: Span, -) -> &'ll DIType { +pub fn type_metadata(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>, usage_site_span: Span) -> &'ll DIType { // Get the unique type ID of this type. let unique_type_id = { let mut type_map = debug_context(cx).type_map.borrow_mut(); @@ -538,7 +546,7 @@ pub fn type_metadata( match type_map.find_metadata_for_type(t) { Some(metadata) => { return metadata; - }, + } None => { // The Ty is not in the `TypeMap` but maybe we have already seen // an equivalent type (e.g., only differing in region arguments). @@ -552,7 +560,7 @@ pub fn type_metadata( // return the cached metadata. type_map.register_type_with_metadata(t, metadata); return metadata; - }, + } None => { // There really is no type metadata for this type, so // proceed by creating it. @@ -565,82 +573,54 @@ pub fn type_metadata( debug!("type_metadata: {:?}", t); - let ptr_metadata = |ty: Ty<'tcx>| { - match ty.kind { - ty::Slice(typ) => { - Ok(vec_slice_metadata(cx, t, typ, unique_type_id, usage_site_span)) - } - ty::Str => { - Ok(vec_slice_metadata(cx, t, cx.tcx.types.u8, unique_type_id, usage_site_span)) - } - ty::Dynamic(..) => { - Ok(MetadataCreationResult::new( - trait_pointer_metadata(cx, ty, Some(t), unique_type_id), - false)) - } - _ => { - let pointee_metadata = type_metadata(cx, ty, usage_site_span); - - if let Some(metadata) = debug_context(cx).type_map - .borrow() - .find_metadata_for_unique_id(unique_type_id) - { - return Err(metadata); - } + let ptr_metadata = |ty: Ty<'tcx>| match ty.kind { + ty::Slice(typ) => Ok(vec_slice_metadata(cx, t, typ, unique_type_id, usage_site_span)), + ty::Str => Ok(vec_slice_metadata(cx, t, cx.tcx.types.u8, unique_type_id, usage_site_span)), + ty::Dynamic(..) => Ok(MetadataCreationResult::new( + trait_pointer_metadata(cx, ty, Some(t), unique_type_id), + false, + )), + _ => { + let pointee_metadata = type_metadata(cx, ty, usage_site_span); - Ok(MetadataCreationResult::new(pointer_type_metadata(cx, t, pointee_metadata), - false)) + if let Some(metadata) = + debug_context(cx).type_map.borrow().find_metadata_for_unique_id(unique_type_id) + { + return Err(metadata); } + + Ok(MetadataCreationResult::new(pointer_type_metadata(cx, t, pointee_metadata), false)) } }; let MetadataCreationResult { metadata, already_stored_in_typemap } = match t.kind { - ty::Never | - ty::Bool | - ty::Char | - ty::Int(_) | - ty::Uint(_) | - ty::Float(_) => { + ty::Never | ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_) => { MetadataCreationResult::new(basic_type_metadata(cx, t), false) } ty::Tuple(ref elements) if elements.is_empty() => { MetadataCreationResult::new(basic_type_metadata(cx, t), false) } - ty::Array(typ, _) | - ty::Slice(typ) => { + ty::Array(typ, _) | ty::Slice(typ) => { fixed_vec_metadata(cx, unique_type_id, t, typ, usage_site_span) } - ty::Str => { - fixed_vec_metadata(cx, unique_type_id, t, cx.tcx.types.i8, usage_site_span) - } + ty::Str => fixed_vec_metadata(cx, unique_type_id, t, cx.tcx.types.i8, usage_site_span), ty::Dynamic(..) => { - MetadataCreationResult::new( - trait_pointer_metadata(cx, t, None, unique_type_id), - false) + MetadataCreationResult::new(trait_pointer_metadata(cx, t, None, unique_type_id), false) } ty::Foreign(..) => { - MetadataCreationResult::new( - foreign_type_metadata(cx, t, unique_type_id), - false) - } - ty::RawPtr(ty::TypeAndMut{ty, ..}) | - ty::Ref(_, ty, _) => { - match ptr_metadata(ty) { - Ok(res) => res, - Err(metadata) => return metadata, - } - } - ty::Adt(def, _) if def.is_box() => { - match ptr_metadata(t.boxed_ty()) { - Ok(res) => res, - Err(metadata) => return metadata, - } + MetadataCreationResult::new(foreign_type_metadata(cx, t, unique_type_id), false) } + ty::RawPtr(ty::TypeAndMut { ty, .. }) | ty::Ref(_, ty, _) => match ptr_metadata(ty) { + Ok(res) => res, + Err(metadata) => return metadata, + }, + ty::Adt(def, _) if def.is_box() => match ptr_metadata(t.boxed_ty()) { + Ok(res) => res, + Err(metadata) => return metadata, + }, ty::FnDef(..) | ty::FnPtr(_) => { - - if let Some(metadata) = debug_context(cx).type_map - .borrow() - .find_metadata_for_unique_id(unique_type_id) + if let Some(metadata) = + debug_context(cx).type_map.borrow().find_metadata_for_unique_id(unique_type_id) { return metadata; } @@ -665,81 +645,63 @@ pub fn type_metadata( SmallCStr::new("<recur_type>").as_ptr(), size.bits(), align.bits() as u32, - DW_ATE_unsigned) + DW_ATE_unsigned, + ) } }; let type_map = &debug_context(cx).type_map; type_map.borrow_mut().register_type_with_metadata(t, temp_type); - let fn_metadata = subroutine_type_metadata(cx, - unique_type_id, - t.fn_sig(cx.tcx), - usage_site_span).metadata; + let fn_metadata = + subroutine_type_metadata(cx, unique_type_id, t.fn_sig(cx.tcx), usage_site_span) + .metadata; type_map.borrow_mut().remove_type(t); - // This is actually a function pointer, so wrap it in pointer DI. MetadataCreationResult::new(pointer_type_metadata(cx, t, fn_metadata), false) - } ty::Closure(def_id, substs) => { - let upvar_tys : Vec<_> = substs.as_closure().upvar_tys(def_id, cx.tcx).collect(); + let upvar_tys: Vec<_> = substs.as_closure().upvar_tys(def_id, cx.tcx).collect(); let containing_scope = get_namespace_for_item(cx, def_id); - prepare_tuple_metadata(cx, - t, - &upvar_tys, - unique_type_id, - usage_site_span, - Some(containing_scope)).finalize(cx) + prepare_tuple_metadata( + cx, + t, + &upvar_tys, + unique_type_id, + usage_site_span, + Some(containing_scope), + ) + .finalize(cx) } - ty::Generator(def_id, substs, _) => { - let upvar_tys : Vec<_> = substs - .as_generator().prefix_tys(def_id, cx.tcx).map(|t| { - cx.tcx.normalize_erasing_regions(ParamEnv::reveal_all(), t) - }).collect(); - prepare_enum_metadata(cx, - t, - def_id, - unique_type_id, - usage_site_span, - upvar_tys).finalize(cx) + ty::Generator(def_id, substs, _) => { + let upvar_tys: Vec<_> = substs + .as_generator() + .prefix_tys(def_id, cx.tcx) + .map(|t| cx.tcx.normalize_erasing_regions(ParamEnv::reveal_all(), t)) + .collect(); + prepare_enum_metadata(cx, t, def_id, unique_type_id, usage_site_span, upvar_tys) + .finalize(cx) } ty::Adt(def, ..) => match def.adt_kind() { AdtKind::Struct => { - prepare_struct_metadata(cx, - t, - unique_type_id, - usage_site_span).finalize(cx) + prepare_struct_metadata(cx, t, unique_type_id, usage_site_span).finalize(cx) } AdtKind::Union => { - prepare_union_metadata(cx, - t, - unique_type_id, - usage_site_span).finalize(cx) + prepare_union_metadata(cx, t, unique_type_id, usage_site_span).finalize(cx) } AdtKind::Enum => { - prepare_enum_metadata(cx, - t, - def.did, - unique_type_id, - usage_site_span, - vec![]).finalize(cx) + prepare_enum_metadata(cx, t, def.did, unique_type_id, usage_site_span, vec![]) + .finalize(cx) } }, ty::Tuple(ref elements) => { let tys: Vec<_> = elements.iter().map(|k| k.expect_ty()).collect(); - prepare_tuple_metadata(cx, - t, - &tys, - unique_type_id, - usage_site_span, - NO_SCOPE_METADATA).finalize(cx) - } - _ => { - bug!("debuginfo: unexpected type in type_metadata: {:?}", t) + prepare_tuple_metadata(cx, t, &tys, unique_type_id, usage_site_span, NO_SCOPE_METADATA) + .finalize(cx) } + _ => bug!("debuginfo: unexpected type in type_metadata: {:?}", t), }; { @@ -750,26 +712,30 @@ pub fn type_metadata( let metadata_for_uid = match type_map.find_metadata_for_unique_id(unique_type_id) { Some(metadata) => metadata, None => { - span_bug!(usage_site_span, - "expected type metadata for unique \ + span_bug!( + usage_site_span, + "expected type metadata for unique \ type ID '{}' to already be in \ the `debuginfo::TypeMap` but it \ was not. (Ty = {})", - type_map.get_unique_type_id_as_string(unique_type_id), - t); + type_map.get_unique_type_id_as_string(unique_type_id), + t + ); } }; match type_map.find_metadata_for_type(t) { Some(metadata) => { if metadata != metadata_for_uid { - span_bug!(usage_site_span, - "mismatch between `Ty` and \ + span_bug!( + usage_site_span, + "mismatch between `Ty` and \ `UniqueTypeId` maps in \ `debuginfo::TypeMap`. \ UniqueTypeId={}, Ty={}", - type_map.get_unique_type_id_as_string(unique_type_id), - t); + type_map.get_unique_type_id_as_string(unique_type_id), + t + ); } } None => { @@ -785,12 +751,12 @@ pub fn type_metadata( metadata } -pub fn file_metadata(cx: &CodegenCx<'ll, '_>, - file_name: &FileName, - defining_crate: CrateNum) -> &'ll DIFile { - debug!("file_metadata: file_name: {}, defining_crate: {}", - file_name, - defining_crate); +pub fn file_metadata( + cx: &CodegenCx<'ll, '_>, + file_name: &FileName, + defining_crate: CrateNum, +) -> &'ll DIFile { + debug!("file_metadata: file_name: {}, defining_crate: {}", file_name, defining_crate); let file_name = Some(file_name.to_string()); let directory = if defining_crate == LOCAL_CRATE { @@ -807,10 +773,11 @@ pub fn unknown_file_metadata(cx: &CodegenCx<'ll, '_>) -> &'ll DIFile { file_metadata_raw(cx, None, None) } -fn file_metadata_raw(cx: &CodegenCx<'ll, '_>, - file_name: Option<String>, - directory: Option<String>) - -> &'ll DIFile { +fn file_metadata_raw( + cx: &CodegenCx<'ll, '_>, + file_name: Option<String>, + directory: Option<String>, +) -> &'ll DIFile { let key = (file_name, directory); match debug_context(cx).created_files.borrow_mut().entry(key) { @@ -819,15 +786,16 @@ fn file_metadata_raw(cx: &CodegenCx<'ll, '_>, let (file_name, directory) = v.key(); debug!("file_metadata: file_name: {:?}, directory: {:?}", file_name, directory); - let file_name = SmallCStr::new( - if let Some(file_name) = file_name { &file_name } else { "<unknown>" }); - let directory = SmallCStr::new( - if let Some(directory) = directory { &directory } else { "" }); + let file_name = SmallCStr::new(if let Some(file_name) = file_name { + &file_name + } else { + "<unknown>" + }); + let directory = + SmallCStr::new(if let Some(directory) = directory { &directory } else { "" }); let file_metadata = unsafe { - llvm::LLVMRustDIBuilderCreateFile(DIB(cx), - file_name.as_ptr(), - directory.as_ptr()) + llvm::LLVMRustDIBuilderCreateFile(DIB(cx), file_name.as_ptr(), directory.as_ptr()) }; v.insert(file_metadata); @@ -841,20 +809,13 @@ fn basic_type_metadata(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>) -> &'ll DIType { let (name, encoding) = match t.kind { ty::Never => ("!", DW_ATE_unsigned), - ty::Tuple(ref elements) if elements.is_empty() => - ("()", DW_ATE_unsigned), + ty::Tuple(ref elements) if elements.is_empty() => ("()", DW_ATE_unsigned), ty::Bool => ("bool", DW_ATE_boolean), ty::Char => ("char", DW_ATE_unsigned_char), - ty::Int(int_ty) => { - (int_ty.name_str(), DW_ATE_signed) - }, - ty::Uint(uint_ty) => { - (uint_ty.name_str(), DW_ATE_unsigned) - }, - ty::Float(float_ty) => { - (float_ty.name_str(), DW_ATE_float) - }, - _ => bug!("debuginfo::basic_type_metadata - `t` is invalid type") + ty::Int(int_ty) => (int_ty.name_str(), DW_ATE_signed), + ty::Uint(uint_ty) => (uint_ty.name_str(), DW_ATE_unsigned), + ty::Float(float_ty) => (float_ty.name_str(), DW_ATE_float), + _ => bug!("debuginfo::basic_type_metadata - `t` is invalid type"), }; let (size, align) = cx.size_and_align_of(t); @@ -865,7 +826,8 @@ fn basic_type_metadata(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>) -> &'ll DIType { name.as_ptr(), size.bits(), align.bits() as u32, - encoding) + encoding, + ) }; return ty_metadata; @@ -896,7 +858,8 @@ fn pointer_type_metadata( pointee_type_metadata, pointer_size.bits(), pointer_align.bits() as u32, - name.as_ptr()) + name.as_ptr(), + ) } } @@ -920,10 +883,8 @@ pub fn compile_unit_metadata( } debug!("compile_unit_metadata: {:?}", name_in_debuginfo); - let rustc_producer = format!( - "rustc version {}", - option_env!("CFG_VERSION").expect("CFG_VERSION"), - ); + let rustc_producer = + format!("rustc version {}", option_env!("CFG_VERSION").expect("CFG_VERSION"),); // FIXME(#41252) Remove "clang LLVM" if we can get GDB and LLVM to play nice. let producer = format!("clang LLVM ({})", rustc_producer); @@ -954,7 +915,10 @@ pub fn compile_unit_metadata( unsafe { let file_metadata = llvm::LLVMRustDIBuilderCreateFile( - debug_context.builder, name_in_debuginfo.as_ptr(), work_dir.as_ptr()); + debug_context.builder, + name_in_debuginfo.as_ptr(), + work_dir.as_ptr(), + ); let unit_metadata = llvm::LLVMRustDIBuilderCreateCompileUnit( debug_context.builder, @@ -965,27 +929,36 @@ pub fn compile_unit_metadata( flags.as_ptr().cast(), 0, split_name.as_ptr().cast(), - kind); + kind, + ); if tcx.sess.opts.debugging_opts.profile { - let cu_desc_metadata = llvm::LLVMRustMetadataAsValue(debug_context.llcontext, - unit_metadata); + let cu_desc_metadata = + llvm::LLVMRustMetadataAsValue(debug_context.llcontext, unit_metadata); let gcov_cu_info = [ - path_to_mdstring(debug_context.llcontext, - &tcx.output_filenames(LOCAL_CRATE).with_extension("gcno")), - path_to_mdstring(debug_context.llcontext, - &tcx.output_filenames(LOCAL_CRATE).with_extension("gcda")), + path_to_mdstring( + debug_context.llcontext, + &tcx.output_filenames(LOCAL_CRATE).with_extension("gcno"), + ), + path_to_mdstring( + debug_context.llcontext, + &tcx.output_filenames(LOCAL_CRATE).with_extension("gcda"), + ), cu_desc_metadata, ]; - let gcov_metadata = llvm::LLVMMDNodeInContext(debug_context.llcontext, - gcov_cu_info.as_ptr(), - gcov_cu_info.len() as c_uint); + let gcov_metadata = llvm::LLVMMDNodeInContext( + debug_context.llcontext, + gcov_cu_info.as_ptr(), + gcov_cu_info.len() as c_uint, + ); let llvm_gcov_ident = const_cstr!("llvm.gcov"); - llvm::LLVMAddNamedMetadataOperand(debug_context.llmod, - llvm_gcov_ident.as_ptr(), - gcov_metadata); + llvm::LLVMAddNamedMetadataOperand( + debug_context.llmod, + llvm_gcov_ident.as_ptr(), + gcov_metadata, + ); } // Insert `llvm.ident` metadata on the wasm32 targets since that will @@ -1009,24 +982,23 @@ pub fn compile_unit_metadata( fn path_to_mdstring(llcx: &'ll llvm::Context, path: &Path) -> &'ll Value { let path_str = path_to_c_string(path); unsafe { - llvm::LLVMMDStringInContext(llcx, - path_str.as_ptr(), - path_str.as_bytes().len() as c_uint) + llvm::LLVMMDStringInContext( + llcx, + path_str.as_ptr(), + path_str.as_bytes().len() as c_uint, + ) } } } struct MetadataCreationResult<'ll> { metadata: &'ll DIType, - already_stored_in_typemap: bool + already_stored_in_typemap: bool, } impl MetadataCreationResult<'ll> { fn new(metadata: &'ll DIType, already_stored_in_typemap: bool) -> Self { - MetadataCreationResult { - metadata, - already_stored_in_typemap, - } + MetadataCreationResult { metadata, already_stored_in_typemap } } } @@ -1044,9 +1016,11 @@ struct MemberDescription<'ll> { } impl<'ll> MemberDescription<'ll> { - fn into_metadata(self, - cx: &CodegenCx<'ll, '_>, - composite_type_metadata: &'ll DIScope) -> &'ll DIType { + fn into_metadata( + self, + cx: &CodegenCx<'ll, '_>, + composite_type_metadata: &'ll DIScope, + ) -> &'ll DIType { let member_name = CString::new(self.name).unwrap(); unsafe { llvm::LLVMRustDIBuilderCreateVariantMemberType( @@ -1063,7 +1037,8 @@ impl<'ll> MemberDescription<'ll> { Some(value) => Some(cx.const_u64(value)), }, self.flags, - self.type_metadata) + self.type_metadata, + ) } } } @@ -1077,28 +1052,17 @@ enum MemberDescriptionFactory<'ll, 'tcx> { TupleMDF(TupleMemberDescriptionFactory<'tcx>), EnumMDF(EnumMemberDescriptionFactory<'ll, 'tcx>), UnionMDF(UnionMemberDescriptionFactory<'tcx>), - VariantMDF(VariantMemberDescriptionFactory<'ll, 'tcx>) + VariantMDF(VariantMemberDescriptionFactory<'ll, 'tcx>), } impl MemberDescriptionFactory<'ll, 'tcx> { - fn create_member_descriptions(&self, cx: &CodegenCx<'ll, 'tcx>) - -> Vec<MemberDescription<'ll>> { + fn create_member_descriptions(&self, cx: &CodegenCx<'ll, 'tcx>) -> Vec<MemberDescription<'ll>> { match *self { - StructMDF(ref this) => { - this.create_member_descriptions(cx) - } - TupleMDF(ref this) => { - this.create_member_descriptions(cx) - } - EnumMDF(ref this) => { - this.create_member_descriptions(cx) - } - UnionMDF(ref this) => { - this.create_member_descriptions(cx) - } - VariantMDF(ref this) => { - this.create_member_descriptions(cx) - } + StructMDF(ref this) => this.create_member_descriptions(cx), + TupleMDF(ref this) => this.create_member_descriptions(cx), + EnumMDF(ref this) => this.create_member_descriptions(cx), + UnionMDF(ref this) => this.create_member_descriptions(cx), + VariantMDF(ref this) => this.create_member_descriptions(cx), } } } @@ -1115,30 +1079,33 @@ struct StructMemberDescriptionFactory<'tcx> { } impl<'tcx> StructMemberDescriptionFactory<'tcx> { - fn create_member_descriptions(&self, cx: &CodegenCx<'ll, 'tcx>) - -> Vec<MemberDescription<'ll>> { + fn create_member_descriptions(&self, cx: &CodegenCx<'ll, 'tcx>) -> Vec<MemberDescription<'ll>> { let layout = cx.layout_of(self.ty); - self.variant.fields.iter().enumerate().map(|(i, f)| { - let name = if self.variant.ctor_kind == CtorKind::Fn { - format!("__{}", i) - } else { - f.ident.to_string() - }; - let field = layout.field(cx, i); - MemberDescription { - name, - type_metadata: type_metadata(cx, field.ty, self.span), - offset: layout.fields.offset(i), - size: field.size, - align: field.align.abi, - flags: DIFlags::FlagZero, - discriminant: None, - } - }).collect() + self.variant + .fields + .iter() + .enumerate() + .map(|(i, f)| { + let name = if self.variant.ctor_kind == CtorKind::Fn { + format!("__{}", i) + } else { + f.ident.to_string() + }; + let field = layout.field(cx, i); + MemberDescription { + name, + type_metadata: type_metadata(cx, field.ty, self.span), + offset: layout.fields.offset(i), + size: field.size, + align: field.align.abi, + flags: DIFlags::FlagZero, + discriminant: None, + } + }) + .collect() } } - fn prepare_struct_metadata( cx: &CodegenCx<'ll, 'tcx>, struct_type: Ty<'tcx>, @@ -1149,16 +1116,13 @@ fn prepare_struct_metadata( let (struct_def_id, variant) = match struct_type.kind { ty::Adt(def, _) => (def.did, def.non_enum_variant()), - _ => bug!("prepare_struct_metadata on a non-ADT") + _ => bug!("prepare_struct_metadata on a non-ADT"), }; let containing_scope = get_namespace_for_item(cx, struct_def_id); - let struct_metadata_stub = create_struct_stub(cx, - struct_type, - &struct_name, - unique_type_id, - Some(containing_scope)); + let struct_metadata_stub = + create_struct_stub(cx, struct_type, &struct_name, unique_type_id, Some(containing_scope)); create_and_register_recursive_type_forward_declaration( cx, @@ -1166,11 +1130,7 @@ fn prepare_struct_metadata( unique_type_id, struct_metadata_stub, struct_metadata_stub, - StructMDF(StructMemberDescriptionFactory { - ty: struct_type, - variant, - span, - }) + StructMDF(StructMemberDescriptionFactory { ty: struct_type, variant, span }), ) } @@ -1186,21 +1146,24 @@ struct TupleMemberDescriptionFactory<'tcx> { } impl<'tcx> TupleMemberDescriptionFactory<'tcx> { - fn create_member_descriptions(&self, cx: &CodegenCx<'ll, 'tcx>) - -> Vec<MemberDescription<'ll>> { + fn create_member_descriptions(&self, cx: &CodegenCx<'ll, 'tcx>) -> Vec<MemberDescription<'ll>> { let layout = cx.layout_of(self.ty); - self.component_types.iter().enumerate().map(|(i, &component_type)| { - let (size, align) = cx.size_and_align_of(component_type); - MemberDescription { - name: format!("__{}", i), - type_metadata: type_metadata(cx, component_type, self.span), - offset: layout.fields.offset(i), - size, - align, - flags: DIFlags::FlagZero, - discriminant: None, - } - }).collect() + self.component_types + .iter() + .enumerate() + .map(|(i, &component_type)| { + let (size, align) = cx.size_and_align_of(component_type); + MemberDescription { + name: format!("__{}", i), + type_metadata: type_metadata(cx, component_type, self.span), + offset: layout.fields.offset(i), + size, + align, + flags: DIFlags::FlagZero, + discriminant: None, + } + }) + .collect() } } @@ -1214,11 +1177,8 @@ fn prepare_tuple_metadata( ) -> RecursiveTypeDescription<'ll, 'tcx> { let tuple_name = compute_debuginfo_type_name(cx.tcx, tuple_type, false); - let struct_stub = create_struct_stub(cx, - tuple_type, - &tuple_name[..], - unique_type_id, - containing_scope); + let struct_stub = + create_struct_stub(cx, tuple_type, &tuple_name[..], unique_type_id, containing_scope); create_and_register_recursive_type_forward_declaration( cx, @@ -1230,7 +1190,7 @@ fn prepare_tuple_metadata( ty: tuple_type, component_types: component_types.to_vec(), span, - }) + }), ) } @@ -1245,20 +1205,24 @@ struct UnionMemberDescriptionFactory<'tcx> { } impl<'tcx> UnionMemberDescriptionFactory<'tcx> { - fn create_member_descriptions(&self, cx: &CodegenCx<'ll, 'tcx>) - -> Vec<MemberDescription<'ll>> { - self.variant.fields.iter().enumerate().map(|(i, f)| { - let field = self.layout.field(cx, i); - MemberDescription { - name: f.ident.to_string(), - type_metadata: type_metadata(cx, field.ty, self.span), - offset: Size::ZERO, - size: field.size, - align: field.align.abi, - flags: DIFlags::FlagZero, - discriminant: None, - } - }).collect() + fn create_member_descriptions(&self, cx: &CodegenCx<'ll, 'tcx>) -> Vec<MemberDescription<'ll>> { + self.variant + .fields + .iter() + .enumerate() + .map(|(i, f)| { + let field = self.layout.field(cx, i); + MemberDescription { + name: f.ident.to_string(), + type_metadata: type_metadata(cx, field.ty, self.span), + offset: Size::ZERO, + size: field.size, + align: field.align.abi, + flags: DIFlags::FlagZero, + discriminant: None, + } + }) + .collect() } } @@ -1272,16 +1236,13 @@ fn prepare_union_metadata( let (union_def_id, variant) = match union_type.kind { ty::Adt(def, _) => (def.did, def.non_enum_variant()), - _ => bug!("prepare_union_metadata on a non-ADT") + _ => bug!("prepare_union_metadata on a non-ADT"), }; let containing_scope = get_namespace_for_item(cx, union_def_id); - let union_metadata_stub = create_union_stub(cx, - union_type, - &union_name, - unique_type_id, - containing_scope); + let union_metadata_stub = + create_union_stub(cx, union_type, &union_name, unique_type_id, containing_scope); create_and_register_recursive_type_forward_declaration( cx, @@ -1289,11 +1250,7 @@ fn prepare_union_metadata( unique_type_id, union_metadata_stub, union_metadata_stub, - UnionMDF(UnionMemberDescriptionFactory { - layout: cx.layout_of(union_type), - variant, - span, - }) + UnionMDF(UnionMemberDescriptionFactory { layout: cx.layout_of(union_type), variant, span }), ) } @@ -1327,8 +1284,7 @@ fn generator_layout_and_saved_local_names( ) -> (&'tcx GeneratorLayout<'tcx>, IndexVec<mir::GeneratorSavedLocal, Option<ast::Name>>) { let body = tcx.optimized_mir(def_id); let generator_layout = body.generator_layout.as_ref().unwrap(); - let mut generator_saved_local_names = - IndexVec::from_elem(None, &generator_layout.field_tys); + let mut generator_saved_local_names = IndexVec::from_elem(None, &generator_layout.field_tys); let state_arg = mir::PlaceBase::Local(mir::Local::new(1)); for var in &body.var_debug_info { @@ -1372,8 +1328,7 @@ struct EnumMemberDescriptionFactory<'ll, 'tcx> { } impl EnumMemberDescriptionFactory<'ll, 'tcx> { - fn create_member_descriptions(&self, cx: &CodegenCx<'ll, 'tcx>) - -> Vec<MemberDescription<'ll>> { + fn create_member_descriptions(&self, cx: &CodegenCx<'ll, 'tcx>) -> Vec<MemberDescription<'ll>> { let generator_variant_info_data = match self.enum_type.kind { ty::Generator(def_id, ..) => { Some(generator_layout_and_saved_local_names(cx.tcx, def_id)) @@ -1381,21 +1336,19 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> { _ => None, }; - let variant_info_for = |index: VariantIdx| { - match self.enum_type.kind { - ty::Adt(adt, _) => VariantInfo::Adt(&adt.variants[index]), - ty::Generator(_, substs, _) => { - let (generator_layout, generator_saved_local_names) = - generator_variant_info_data.as_ref().unwrap(); - VariantInfo::Generator { - substs, - generator_layout: *generator_layout, - generator_saved_local_names, - variant_index: index, - } + let variant_info_for = |index: VariantIdx| match self.enum_type.kind { + ty::Adt(adt, _) => VariantInfo::Adt(&adt.variants[index]), + ty::Generator(_, substs, _) => { + let (generator_layout, generator_saved_local_names) = + generator_variant_info_data.as_ref().unwrap(); + VariantInfo::Generator { + substs, + generator_layout: *generator_layout, + generator_saved_local_names, + variant_index: index, } - _ => bug!(), } + _ => bug!(), }; // This will always find the metadata in the type map. @@ -1415,36 +1368,32 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> { } let variant_info = variant_info_for(index); - let (variant_type_metadata, member_description_factory) = - describe_enum_variant(cx, - self.layout, - variant_info, - NoDiscriminant, - self_metadata, - self.span); - - let member_descriptions = - member_description_factory.create_member_descriptions(cx); - - set_members_of_composite_type(cx, - self.enum_type, - variant_type_metadata, - member_descriptions); - vec![ - MemberDescription { - name: if fallback { - String::new() - } else { - variant_info.variant_name() - }, - type_metadata: variant_type_metadata, - offset: Size::ZERO, - size: self.layout.size, - align: self.layout.align.abi, - flags: DIFlags::FlagZero, - discriminant: None, - } - ] + let (variant_type_metadata, member_description_factory) = describe_enum_variant( + cx, + self.layout, + variant_info, + NoDiscriminant, + self_metadata, + self.span, + ); + + let member_descriptions = member_description_factory.create_member_descriptions(cx); + + set_members_of_composite_type( + cx, + self.enum_type, + variant_type_metadata, + member_descriptions, + ); + vec![MemberDescription { + name: if fallback { String::new() } else { variant_info.variant_name() }, + type_metadata: variant_type_metadata, + offset: Size::ZERO, + size: self.layout.size, + align: self.layout.align.abi, + flags: DIFlags::FlagZero, + discriminant: None, + }] } layout::Variants::Multiple { discr_kind: layout::DiscriminantKind::Tag, @@ -1455,54 +1404,58 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> { let discriminant_info = if fallback { RegularDiscriminant { discr_field: Field::from(discr_index), - discr_type_metadata: self.discriminant_type_metadata.unwrap() + discr_type_metadata: self.discriminant_type_metadata.unwrap(), } } else { // This doesn't matter in this case. NoDiscriminant }; - variants.iter_enumerated().map(|(i, _)| { - let variant = self.layout.for_variant(cx, i); - let variant_info = variant_info_for(i); - let (variant_type_metadata, member_desc_factory) = - describe_enum_variant(cx, - variant, - variant_info, - discriminant_info, - self_metadata, - self.span); - - let member_descriptions = member_desc_factory - .create_member_descriptions(cx); - - set_members_of_composite_type(cx, - self.enum_type, - variant_type_metadata, - member_descriptions); - - MemberDescription { - name: if fallback { - String::new() - } else { - variant_info.variant_name() - }, - type_metadata: variant_type_metadata, - offset: Size::ZERO, - size: self.layout.size, - align: self.layout.align.abi, - flags: DIFlags::FlagZero, - discriminant: Some( - self.layout.ty.discriminant_for_variant(cx.tcx, i).unwrap().val as u64 - ), - } - }).collect() + variants + .iter_enumerated() + .map(|(i, _)| { + let variant = self.layout.for_variant(cx, i); + let variant_info = variant_info_for(i); + let (variant_type_metadata, member_desc_factory) = describe_enum_variant( + cx, + variant, + variant_info, + discriminant_info, + self_metadata, + self.span, + ); + + let member_descriptions = + member_desc_factory.create_member_descriptions(cx); + + set_members_of_composite_type( + cx, + self.enum_type, + variant_type_metadata, + member_descriptions, + ); + + MemberDescription { + name: if fallback { + String::new() + } else { + variant_info.variant_name() + }, + type_metadata: variant_type_metadata, + offset: Size::ZERO, + size: self.layout.size, + align: self.layout.align.abi, + flags: DIFlags::FlagZero, + discriminant: Some( + self.layout.ty.discriminant_for_variant(cx.tcx, i).unwrap().val + as u64, + ), + } + }) + .collect() } layout::Variants::Multiple { - discr_kind: layout::DiscriminantKind::Niche { - ref niche_variants, - niche_start, - dataful_variant, - }, + discr_kind: + layout::DiscriminantKind::Niche { ref niche_variants, niche_start, dataful_variant }, ref discr, ref variants, discr_index, @@ -1510,32 +1463,37 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> { if fallback { let variant = self.layout.for_variant(cx, dataful_variant); // Create a description of the non-null variant. - let (variant_type_metadata, member_description_factory) = - describe_enum_variant(cx, - variant, - variant_info_for(dataful_variant), - OptimizedDiscriminant, - self.containing_scope, - self.span); + let (variant_type_metadata, member_description_factory) = describe_enum_variant( + cx, + variant, + variant_info_for(dataful_variant), + OptimizedDiscriminant, + self.containing_scope, + self.span, + ); let variant_member_descriptions = member_description_factory.create_member_descriptions(cx); - set_members_of_composite_type(cx, - self.enum_type, - variant_type_metadata, - variant_member_descriptions); + set_members_of_composite_type( + cx, + self.enum_type, + variant_type_metadata, + variant_member_descriptions, + ); // Encode the information about the null variant in the union // member's name. let mut name = String::from("RUST$ENCODED$ENUM$"); // Right now it's not even going to work for `niche_start > 0`, // and for multiple niche variants it only supports the first. - fn compute_field_path<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>, - name: &mut String, - layout: TyLayout<'tcx>, - offset: Size, - size: Size) { + fn compute_field_path<'a, 'tcx>( + cx: &CodegenCx<'a, 'tcx>, + name: &mut String, + layout: TyLayout<'tcx>, + offset: Size, + size: Size, + ) { for i in 0..layout.fields.count() { let field_offset = layout.fields.offset(i); if field_offset > offset { @@ -1549,70 +1507,78 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> { } } } - compute_field_path(cx, &mut name, - self.layout, - self.layout.fields.offset(discr_index), - self.layout.field(cx, discr_index).size); + compute_field_path( + cx, + &mut name, + self.layout, + self.layout.fields.offset(discr_index), + self.layout.field(cx, discr_index).size, + ); variant_info_for(*niche_variants.start()).map_struct_name(|variant_name| { name.push_str(variant_name); }); // Create the (singleton) list of descriptions of union members. - vec![ - MemberDescription { - name, - type_metadata: variant_type_metadata, - offset: Size::ZERO, - size: variant.size, - align: variant.align.abi, - flags: DIFlags::FlagZero, - discriminant: None, - } - ] + vec![MemberDescription { + name, + type_metadata: variant_type_metadata, + offset: Size::ZERO, + size: variant.size, + align: variant.align.abi, + flags: DIFlags::FlagZero, + discriminant: None, + }] } else { - variants.iter_enumerated().map(|(i, _)| { - let variant = self.layout.for_variant(cx, i); - let variant_info = variant_info_for(i); - let (variant_type_metadata, member_desc_factory) = - describe_enum_variant(cx, - variant, - variant_info, - OptimizedDiscriminant, - self_metadata, - self.span); - - let member_descriptions = member_desc_factory - .create_member_descriptions(cx); - - set_members_of_composite_type(cx, - self.enum_type, - variant_type_metadata, - member_descriptions); - - let niche_value = if i == dataful_variant { - None - } else { - let value = (i.as_u32() as u128) - .wrapping_sub(niche_variants.start().as_u32() as u128) - .wrapping_add(niche_start); - let value = truncate(value, discr.value.size(cx)); - // NOTE(eddyb) do *NOT* remove this assert, until - // we pass the full 128-bit value to LLVM, otherwise - // truncation will be silent and remain undetected. - assert_eq!(value as u64 as u128, value); - Some(value as u64) - }; - - MemberDescription { - name: variant_info.variant_name(), - type_metadata: variant_type_metadata, - offset: Size::ZERO, - size: self.layout.size, - align: self.layout.align.abi, - flags: DIFlags::FlagZero, - discriminant: niche_value, - } - }).collect() + variants + .iter_enumerated() + .map(|(i, _)| { + let variant = self.layout.for_variant(cx, i); + let variant_info = variant_info_for(i); + let (variant_type_metadata, member_desc_factory) = + describe_enum_variant( + cx, + variant, + variant_info, + OptimizedDiscriminant, + self_metadata, + self.span, + ); + + let member_descriptions = + member_desc_factory.create_member_descriptions(cx); + + set_members_of_composite_type( + cx, + self.enum_type, + variant_type_metadata, + member_descriptions, + ); + + let niche_value = if i == dataful_variant { + None + } else { + let value = (i.as_u32() as u128) + .wrapping_sub(niche_variants.start().as_u32() as u128) + .wrapping_add(niche_start); + let value = truncate(value, discr.value.size(cx)); + // NOTE(eddyb) do *NOT* remove this assert, until + // we pass the full 128-bit value to LLVM, otherwise + // truncation will be silent and remain undetected. + assert_eq!(value as u64 as u128, value); + Some(value as u64) + }; + + MemberDescription { + name: variant_info.variant_name(), + type_metadata: variant_type_metadata, + offset: Size::ZERO, + size: self.layout.size, + align: self.layout.align.abi, + flags: DIFlags::FlagZero, + discriminant: niche_value, + } + }) + .collect() } } } @@ -1629,37 +1595,40 @@ struct VariantMemberDescriptionFactory<'ll, 'tcx> { } impl VariantMemberDescriptionFactory<'ll, 'tcx> { - fn create_member_descriptions(&self, cx: &CodegenCx<'ll, 'tcx>) - -> Vec<MemberDescription<'ll>> { - self.args.iter().enumerate().map(|(i, &(ref name, ty))| { - let (size, align) = cx.size_and_align_of(ty); - MemberDescription { - name: name.to_string(), - type_metadata: if use_enum_fallback(cx) { - match self.discriminant_type_metadata { - // Discriminant is always the first field of our variant - // when using the enum fallback. - Some(metadata) if i == 0 => metadata, - _ => type_metadata(cx, ty, self.span) - } - } else { - type_metadata(cx, ty, self.span) - }, - offset: self.offsets[i], - size, - align, - flags: DIFlags::FlagZero, - discriminant: None, - } - }).collect() + fn create_member_descriptions(&self, cx: &CodegenCx<'ll, 'tcx>) -> Vec<MemberDescription<'ll>> { + self.args + .iter() + .enumerate() + .map(|(i, &(ref name, ty))| { + let (size, align) = cx.size_and_align_of(ty); + MemberDescription { + name: name.to_string(), + type_metadata: if use_enum_fallback(cx) { + match self.discriminant_type_metadata { + // Discriminant is always the first field of our variant + // when using the enum fallback. + Some(metadata) if i == 0 => metadata, + _ => type_metadata(cx, ty, self.span), + } + } else { + type_metadata(cx, ty, self.span) + }, + offset: self.offsets[i], + size, + align, + flags: DIFlags::FlagZero, + discriminant: None, + } + }) + .collect() } } #[derive(Copy, Clone)] enum EnumDiscriminantInfo<'ll> { - RegularDiscriminant{ discr_field: Field, discr_type_metadata: &'ll DIType }, + RegularDiscriminant { discr_field: Field, discr_type_metadata: &'ll DIType }, OptimizedDiscriminant, - NoDiscriminant + NoDiscriminant, } #[derive(Copy, Clone)] @@ -1677,8 +1646,9 @@ impl<'tcx> VariantInfo<'_, 'tcx> { fn map_struct_name<R>(&self, f: impl FnOnce(&str) -> R) -> R { match self { VariantInfo::Adt(variant) => f(&variant.ident.as_str()), - VariantInfo::Generator { substs, variant_index, .. } => - f(&substs.as_generator().variant_name(*variant_index)), + VariantInfo::Generator { substs, variant_index, .. } => { + f(&substs.as_generator().variant_name(*variant_index)) + } } } @@ -1697,16 +1667,18 @@ impl<'tcx> VariantInfo<'_, 'tcx> { fn field_name(&self, i: usize) -> String { let field_name = match *self { - VariantInfo::Adt(variant) if variant.ctor_kind != CtorKind::Fn => - Some(variant.fields[i].ident.name), + VariantInfo::Adt(variant) if variant.ctor_kind != CtorKind::Fn => { + Some(variant.fields[i].ident.name) + } VariantInfo::Generator { generator_layout, generator_saved_local_names, variant_index, .. - } => generator_saved_local_names[ - generator_layout.variant_fields[variant_index][i.into()] - ], + } => { + generator_saved_local_names + [generator_layout.variant_fields[variant_index][i.into()]] + } _ => None, }; field_name.map(|name| name.to_string()).unwrap_or_else(|| format!("__{}", i)) @@ -1726,17 +1698,11 @@ fn describe_enum_variant( span: Span, ) -> (&'ll DICompositeType, MemberDescriptionFactory<'ll, 'tcx>) { let metadata_stub = variant.map_struct_name(|variant_name| { - let unique_type_id = debug_context(cx).type_map - .borrow_mut() - .get_unique_type_id_of_enum_variant( - cx, - layout.ty, - &variant_name); - create_struct_stub(cx, - layout.ty, - &variant_name, - unique_type_id, - Some(containing_scope)) + let unique_type_id = debug_context(cx) + .type_map + .borrow_mut() + .get_unique_type_id_of_enum_variant(cx, layout.ty, &variant_name); + create_struct_stub(cx, layout.ty, &variant_name, unique_type_id, Some(containing_scope)) }); // Build an array of (field name, field type) pairs to be captured in the factory closure. @@ -1747,44 +1713,43 @@ fn describe_enum_variant( // We have the layout of an enum variant, we need the layout of the outer enum let enum_layout = cx.layout_of(layout.ty); let offset = enum_layout.fields.offset(discr_field.as_usize()); - let args = ( - "RUST$ENUM$DISR".to_owned(), - enum_layout.field(cx, discr_field.as_usize()).ty); + let args = + ("RUST$ENUM$DISR".to_owned(), enum_layout.field(cx, discr_field.as_usize()).ty); (Some(offset), Some(args)) } _ => (None, None), }; ( - discr_offset.into_iter().chain((0..layout.fields.count()).map(|i| { - layout.fields.offset(i) - })).collect(), - discr_arg.into_iter().chain((0..layout.fields.count()).map(|i| { - (variant.field_name(i), layout.field(cx, i).ty) - })).collect() + discr_offset + .into_iter() + .chain((0..layout.fields.count()).map(|i| layout.fields.offset(i))) + .collect(), + discr_arg + .into_iter() + .chain( + (0..layout.fields.count()) + .map(|i| (variant.field_name(i), layout.field(cx, i).ty)), + ) + .collect(), ) } else { ( - (0..layout.fields.count()).map(|i| { - layout.fields.offset(i) - }).collect(), - (0..layout.fields.count()).map(|i| { - (variant.field_name(i), layout.field(cx, i).ty) - }).collect() + (0..layout.fields.count()).map(|i| layout.fields.offset(i)).collect(), + (0..layout.fields.count()) + .map(|i| (variant.field_name(i), layout.field(cx, i).ty)) + .collect(), ) }; - let member_description_factory = - VariantMDF(VariantMemberDescriptionFactory { - offsets, - args, - discriminant_type_metadata: match discriminant_info { - RegularDiscriminant { discr_type_metadata, .. } => { - Some(discr_type_metadata) - } - _ => None - }, - span, - }); + let member_description_factory = VariantMDF(VariantMemberDescriptionFactory { + offsets, + args, + discriminant_type_metadata: match discriminant_info { + RegularDiscriminant { discr_type_metadata, .. } => Some(discr_type_metadata), + _ => None, + }, + span, + }); (metadata_stub, member_description_factory) } @@ -1820,7 +1785,8 @@ fn prepare_enum_metadata( DIB(cx), name.as_ptr(), // FIXME: what if enumeration has i128 discriminant? - discr.val as u64)) + discr.val as u64, + )) } }) .collect(), @@ -1834,7 +1800,8 @@ fn prepare_enum_metadata( DIB(cx), name.as_ptr(), // FIXME: what if enumeration has i128 discriminant? - variant_index.as_usize() as u64)) + variant_index.as_usize() as u64, + )) } }) .collect(), @@ -1842,14 +1809,12 @@ fn prepare_enum_metadata( }; let disr_type_key = (enum_def_id, discr); - let cached_discriminant_type_metadata = debug_context(cx).created_enum_disr_types - .borrow() - .get(&disr_type_key).cloned(); + let cached_discriminant_type_metadata = + debug_context(cx).created_enum_disr_types.borrow().get(&disr_type_key).cloned(); match cached_discriminant_type_metadata { Some(discriminant_type_metadata) => discriminant_type_metadata, None => { - let (discriminant_size, discriminant_align) = - (discr.size(cx), discr.align(cx)); + let (discriminant_size, discriminant_align) = (discr.size(cx), discr.align(cx)); let discriminant_base_type_metadata = type_metadata(cx, discr.to_ty(cx.tcx), syntax_pos::DUMMY_SP); @@ -1869,12 +1834,15 @@ fn prepare_enum_metadata( discriminant_size.bits(), discriminant_align.abi.bits() as u32, create_DIArray(DIB(cx), &enumerators_metadata), - discriminant_base_type_metadata, true) + discriminant_base_type_metadata, + true, + ) }; - debug_context(cx).created_enum_disr_types - .borrow_mut() - .insert(disr_type_key, discriminant_type_metadata); + debug_context(cx) + .created_enum_disr_types + .borrow_mut() + .insert(disr_type_key, discriminant_type_metadata); discriminant_type_metadata } @@ -1884,23 +1852,26 @@ fn prepare_enum_metadata( let layout = cx.layout_of(enum_type); match (&layout.abi, &layout.variants) { - (&layout::Abi::Scalar(_), &layout::Variants::Multiple { - discr_kind: layout::DiscriminantKind::Tag, - ref discr, - .. - }) => return FinalMetadata(discriminant_type_metadata(discr.value)), + ( + &layout::Abi::Scalar(_), + &layout::Variants::Multiple { + discr_kind: layout::DiscriminantKind::Tag, + ref discr, + .. + }, + ) => return FinalMetadata(discriminant_type_metadata(discr.value)), _ => {} } let enum_name = SmallCStr::new(&enum_name); let unique_type_id_str = SmallCStr::new( - debug_context(cx).type_map.borrow().get_unique_type_id_as_string(unique_type_id) + debug_context(cx).type_map.borrow().get_unique_type_id_as_string(unique_type_id), ); if use_enum_fallback(cx) { let discriminant_type_metadata = match layout.variants { - layout::Variants::Single { .. } | - layout::Variants::Multiple { + layout::Variants::Single { .. } + | layout::Variants::Multiple { discr_kind: layout::DiscriminantKind::Niche { .. }, .. } => None, @@ -1908,9 +1879,7 @@ fn prepare_enum_metadata( discr_kind: layout::DiscriminantKind::Tag, ref discr, .. - } => { - Some(discriminant_type_metadata(discr.value)) - } + } => Some(discriminant_type_metadata(discr.value)), }; let enum_metadata = unsafe { @@ -1925,7 +1894,8 @@ fn prepare_enum_metadata( DIFlags::FlagZero, None, 0, // RuntimeLang - unique_type_id_str.as_ptr()) + unique_type_id_str.as_ptr(), + ) }; return create_and_register_recursive_type_forward_declaration( @@ -1968,7 +1938,8 @@ fn prepare_enum_metadata( layout::F32 => Integer::I32, layout::F64 => Integer::I64, layout::Pointer => cx.data_layout().ptr_sized_integer(), - }.to_ty(cx.tcx, false); + } + .to_ty(cx.tcx, false); let discr_metadata = basic_type_metadata(cx, discr_type); unsafe { @@ -1982,9 +1953,10 @@ fn prepare_enum_metadata( align.abi.bits() as u32, layout.fields.offset(discr_index).bits(), DIFlags::FlagArtificial, - discr_metadata)) + discr_metadata, + )) } - }, + } layout::Variants::Multiple { discr_kind: layout::DiscriminantKind::Tag, @@ -2007,9 +1979,10 @@ fn prepare_enum_metadata( align.bits() as u32, layout.fields.offset(discr_index).bits(), DIFlags::FlagArtificial, - discr_metadata)) + discr_metadata, + )) } - }, + } }; let mut outer_fields = match layout.variants { @@ -2018,7 +1991,7 @@ fn prepare_enum_metadata( let tuple_mdf = TupleMemberDescriptionFactory { ty: enum_type, component_types: outer_field_tys, - span + span, }; tuple_mdf .create_member_descriptions(cx) @@ -2029,9 +2002,10 @@ fn prepare_enum_metadata( }; let variant_part_unique_type_id_str = SmallCStr::new( - debug_context(cx).type_map + debug_context(cx) + .type_map .borrow_mut() - .get_unique_type_id_str_of_enum_variant_part(unique_type_id) + .get_unique_type_id_str_of_enum_variant_part(unique_type_id), ); let empty_array = create_DIArray(DIB(cx), &[]); let variant_part = unsafe { @@ -2046,7 +2020,8 @@ fn prepare_enum_metadata( DIFlags::FlagZero, discriminator_metadata, empty_array, - variant_part_unique_type_id_str.as_ptr()) + variant_part_unique_type_id_str.as_ptr(), + ) }; outer_fields.push(Some(variant_part)); @@ -2066,7 +2041,8 @@ fn prepare_enum_metadata( type_array, 0, None, - unique_type_id_str.as_ptr()) + unique_type_id_str.as_ptr(), + ) }; return create_and_register_recursive_type_forward_declaration( @@ -2103,24 +2079,25 @@ fn composite_type_metadata( _definition_span: Span, ) -> &'ll DICompositeType { // Create the (empty) struct metadata node ... - let composite_type_metadata = create_struct_stub(cx, - composite_type, - composite_type_name, - composite_type_unique_id, - containing_scope); + let composite_type_metadata = create_struct_stub( + cx, + composite_type, + composite_type_name, + composite_type_unique_id, + containing_scope, + ); // ... and immediately create and add the member descriptions. - set_members_of_composite_type(cx, - composite_type, - composite_type_metadata, - member_descriptions); + set_members_of_composite_type(cx, composite_type, composite_type_metadata, member_descriptions); composite_type_metadata } -fn set_members_of_composite_type(cx: &CodegenCx<'ll, 'tcx>, - composite_type: Ty<'tcx>, - composite_type_metadata: &'ll DICompositeType, - member_descriptions: Vec<MemberDescription<'ll>>) { +fn set_members_of_composite_type( + cx: &CodegenCx<'ll, 'tcx>, + composite_type: Ty<'tcx>, + composite_type_metadata: &'ll DICompositeType, + member_descriptions: Vec<MemberDescription<'ll>>, +) { // In some rare cases LLVM metadata uniquing would lead to an existing type // description being used instead of a new one created in // create_struct_stub. This would cause a hard to trace assertion in @@ -2131,8 +2108,10 @@ fn set_members_of_composite_type(cx: &CodegenCx<'ll, 'tcx>, let mut composite_types_completed = debug_context(cx).composite_types_completed.borrow_mut(); if !composite_types_completed.insert(&composite_type_metadata) { - bug!("debuginfo::set_members_of_composite_type() - \ - Already completed forward declaration re-encountered."); + bug!( + "debuginfo::set_members_of_composite_type() - \ + Already completed forward declaration re-encountered." + ); } } @@ -2145,7 +2124,11 @@ fn set_members_of_composite_type(cx: &CodegenCx<'ll, 'tcx>, unsafe { let type_array = create_DIArray(DIB(cx), &member_metadata[..]); llvm::LLVMRustDICompositeTypeReplaceArrays( - DIB(cx), composite_type_metadata, Some(type_array), type_params); + DIB(cx), + composite_type_metadata, + Some(type_array), + type_params, + ); } } @@ -2155,40 +2138,42 @@ fn compute_type_parameters(cx: &CodegenCx<'ll, 'tcx>, ty: Ty<'tcx>) -> Option<&' if !substs.types().next().is_none() { let generics = cx.tcx.generics_of(def.did); let names = get_parameter_names(cx, generics); - let template_params: Vec<_> = substs.iter().zip(names).filter_map(|(kind, name)| { - if let GenericArgKind::Type(ty) = kind.unpack() { - let actual_type = cx.tcx.normalize_erasing_regions(ParamEnv::reveal_all(), ty); - let actual_type_metadata = - type_metadata(cx, actual_type, syntax_pos::DUMMY_SP); - let name = SmallCStr::new(&name.as_str()); - Some(unsafe { - - Some(llvm::LLVMRustDIBuilderCreateTemplateTypeParameter( - DIB(cx), - None, - name.as_ptr(), - actual_type_metadata, - unknown_file_metadata(cx), - 0, - 0, - )) - }) - } else { - None - } - }).collect(); + let template_params: Vec<_> = substs + .iter() + .zip(names) + .filter_map(|(kind, name)| { + if let GenericArgKind::Type(ty) = kind.unpack() { + let actual_type = + cx.tcx.normalize_erasing_regions(ParamEnv::reveal_all(), ty); + let actual_type_metadata = + type_metadata(cx, actual_type, syntax_pos::DUMMY_SP); + let name = SmallCStr::new(&name.as_str()); + Some(unsafe { + Some(llvm::LLVMRustDIBuilderCreateTemplateTypeParameter( + DIB(cx), + None, + name.as_ptr(), + actual_type_metadata, + unknown_file_metadata(cx), + 0, + 0, + )) + }) + } else { + None + } + }) + .collect(); return Some(create_DIArray(DIB(cx), &template_params[..])); } } return Some(create_DIArray(DIB(cx), &[])); - fn get_parameter_names(cx: &CodegenCx<'_, '_>, - generics: &ty::Generics) - -> Vec<Symbol> { - let mut names = generics.parent.map_or(vec![], |def_id| { - get_parameter_names(cx, cx.tcx.generics_of(def_id)) - }); + fn get_parameter_names(cx: &CodegenCx<'_, '_>, generics: &ty::Generics) -> Vec<Symbol> { + let mut names = generics + .parent + .map_or(vec![], |def_id| get_parameter_names(cx, cx.tcx.generics_of(def_id))); names.extend(generics.params.iter().map(|param| param.name)); names } @@ -2208,7 +2193,7 @@ fn create_struct_stub( let name = SmallCStr::new(struct_type_name); let unique_type_id = SmallCStr::new( - debug_context(cx).type_map.borrow().get_unique_type_id_as_string(unique_type_id) + debug_context(cx).type_map.borrow().get_unique_type_id_as_string(unique_type_id), ); let metadata_stub = unsafe { // `LLVMRustDIBuilderCreateStructType()` wants an empty array. A null @@ -2229,7 +2214,8 @@ fn create_struct_stub( empty_array, 0, None, - unique_type_id.as_ptr()) + unique_type_id.as_ptr(), + ) }; metadata_stub @@ -2246,7 +2232,7 @@ fn create_union_stub( let name = SmallCStr::new(union_type_name); let unique_type_id = SmallCStr::new( - debug_context(cx).type_map.borrow().get_unique_type_id_as_string(unique_type_id) + debug_context(cx).type_map.borrow().get_unique_type_id_as_string(unique_type_id), ); let metadata_stub = unsafe { // `LLVMRustDIBuilderCreateUnionType()` wants an empty array. A null @@ -2265,7 +2251,8 @@ fn create_union_stub( DIFlags::FlagZero, Some(empty_array), 0, // RuntimeLang - unique_type_id.as_ptr()) + unique_type_id.as_ptr(), + ) }; metadata_stub @@ -2274,11 +2261,7 @@ fn create_union_stub( /// Creates debug information for the given global variable. /// /// Adds the created metadata nodes directly to the crate's IR. -pub fn create_global_var_metadata( - cx: &CodegenCx<'ll, '_>, - def_id: DefId, - global: &'ll Value, -) { +pub fn create_global_var_metadata(cx: &CodegenCx<'ll, '_>, def_id: DefId, global: &'ll Value) { if cx.dbg_cx.is_none() { return; } @@ -2317,20 +2300,20 @@ pub fn create_global_var_metadata( let global_align = cx.align_of(variable_type); unsafe { - llvm::LLVMRustDIBuilderCreateStaticVariable(DIB(cx), - Some(var_scope), - var_name.as_ptr(), - // If null, linkage_name field is omitted, - // which is what we want for no_mangle statics - linkage_name.as_ref() - .map_or(ptr::null(), |name| name.as_ptr()), - file_metadata, - line_number, - type_metadata, - is_local_to_unit, - global, - None, - global_align.bytes() as u32, + llvm::LLVMRustDIBuilderCreateStaticVariable( + DIB(cx), + Some(var_scope), + var_name.as_ptr(), + // If null, linkage_name field is omitted, + // which is what we want for no_mangle statics + linkage_name.as_ref().map_or(ptr::null(), |name| name.as_ptr()), + file_metadata, + line_number, + type_metadata, + is_local_to_unit, + global, + None, + global_align.bytes() as u32, ); } } @@ -2370,20 +2353,22 @@ pub fn create_vtable_metadata(cx: &CodegenCx<'ll, 'tcx>, ty: Ty<'tcx>, vtable: & empty_array, 0, Some(type_metadata), - name.as_ptr() + name.as_ptr(), ); - llvm::LLVMRustDIBuilderCreateStaticVariable(DIB(cx), - NO_SCOPE_METADATA, - name.as_ptr(), - ptr::null(), - unknown_file_metadata(cx), - UNKNOWN_LINE_NUMBER, - vtable_type, - true, - vtable, - None, - 0); + llvm::LLVMRustDIBuilderCreateStaticVariable( + DIB(cx), + NO_SCOPE_METADATA, + name.as_ptr(), + ptr::null(), + unknown_file_metadata(cx), + UNKNOWN_LINE_NUMBER, + vtable_type, + true, + vtable, + None, + 0, + ); } } @@ -2395,10 +2380,5 @@ pub fn extend_scope_to_file( defining_crate: CrateNum, ) -> &'ll DILexicalBlock { let file_metadata = file_metadata(cx, &file.name, defining_crate); - unsafe { - llvm::LLVMRustDIBuilderCreateLexicalBlockFile( - DIB(cx), - scope_metadata, - file_metadata) - } + unsafe { llvm::LLVMRustDIBuilderCreateLexicalBlockFile(DIB(cx), scope_metadata, file_metadata) } } |
