[go: up one dir, main page]

rustc_codegen_llvm/debuginfo/
mod.rs

1#![doc = include_str!("doc.md")]
2
3use std::cell::{OnceCell, RefCell};
4use std::ops::Range;
5use std::sync::Arc;
6use std::{iter, ptr};
7
8use libc::c_uint;
9use metadata::create_subroutine_type;
10use rustc_abi::Size;
11use rustc_codegen_ssa::debuginfo::type_names;
12use rustc_codegen_ssa::mir::debuginfo::VariableKind::*;
13use rustc_codegen_ssa::mir::debuginfo::{DebugScope, FunctionDebugContext, VariableKind};
14use rustc_codegen_ssa::traits::*;
15use rustc_data_structures::unord::UnordMap;
16use rustc_hir::def_id::{DefId, DefIdMap};
17use rustc_index::IndexVec;
18use rustc_middle::mir;
19use rustc_middle::ty::layout::{HasTypingEnv, LayoutOf};
20use rustc_middle::ty::{self, GenericArgsRef, Instance, Ty, TypeVisitableExt};
21use rustc_session::Session;
22use rustc_session::config::{self, DebugInfo};
23use rustc_span::{
24    BytePos, Pos, SourceFile, SourceFileAndLine, SourceFileHash, Span, StableSourceFileId, Symbol,
25};
26use rustc_target::callconv::FnAbi;
27use rustc_target::spec::DebuginfoKind;
28use smallvec::SmallVec;
29use tracing::debug;
30
31use self::metadata::{UNKNOWN_COLUMN_NUMBER, UNKNOWN_LINE_NUMBER, file_metadata, type_di_node};
32use self::namespace::mangled_name_of_instance;
33use self::utils::{DIB, create_DIArray, is_node_local_to_unit};
34use crate::builder::Builder;
35use crate::common::{AsCCharPtr, CodegenCx};
36use crate::llvm;
37use crate::llvm::debuginfo::{
38    DIArray, DIBuilderBox, DIFile, DIFlags, DILexicalBlock, DILocation, DISPFlags, DIScope,
39    DITemplateTypeParameter, DIType, DIVariable,
40};
41use crate::value::Value;
42
43mod create_scope_map;
44mod dwarf_const;
45mod gdb;
46pub(crate) mod metadata;
47mod namespace;
48mod utils;
49
50use self::create_scope_map::compute_mir_scopes;
51pub(crate) use self::metadata::build_global_var_di_node;
52
53// FIXME(Zalathar): These `DW_TAG_*` constants are fake values that were
54// removed from LLVM in 2015, and are only used by our own `RustWrapper.cpp`
55// to decide which C++ API to call. Instead, we should just have two separate
56// FFI functions and choose the correct one on the Rust side.
57#[allow(non_upper_case_globals)]
58const DW_TAG_auto_variable: c_uint = 0x100;
59#[allow(non_upper_case_globals)]
60const DW_TAG_arg_variable: c_uint = 0x101;
61
62/// A context object for maintaining all state needed by the debuginfo module.
63pub(crate) struct CodegenUnitDebugContext<'ll, 'tcx> {
64    llmod: &'ll llvm::Module,
65    builder: DIBuilderBox<'ll>,
66    created_files: RefCell<UnordMap<Option<(StableSourceFileId, SourceFileHash)>, &'ll DIFile>>,
67
68    type_map: metadata::TypeMap<'ll, 'tcx>,
69    adt_stack: RefCell<Vec<(DefId, GenericArgsRef<'tcx>)>>,
70    namespace_map: RefCell<DefIdMap<&'ll DIScope>>,
71    recursion_marker_type: OnceCell<&'ll DIType>,
72}
73
74impl<'ll, 'tcx> CodegenUnitDebugContext<'ll, 'tcx> {
75    pub(crate) fn new(llmod: &'ll llvm::Module) -> Self {
76        debug!("CodegenUnitDebugContext::new");
77        let builder = DIBuilderBox::new(llmod);
78        // DIBuilder inherits context from the module, so we'd better use the same one
79        CodegenUnitDebugContext {
80            llmod,
81            builder,
82            created_files: Default::default(),
83            type_map: Default::default(),
84            adt_stack: Default::default(),
85            namespace_map: RefCell::new(Default::default()),
86            recursion_marker_type: OnceCell::new(),
87        }
88    }
89
90    pub(crate) fn finalize(&self, sess: &Session) {
91        unsafe { llvm::LLVMDIBuilderFinalize(self.builder.as_ref()) };
92
93        match sess.target.debuginfo_kind {
94            DebuginfoKind::Dwarf | DebuginfoKind::DwarfDsym => {
95                // Debuginfo generation in LLVM by default uses a higher
96                // version of dwarf than macOS currently understands. We can
97                // instruct LLVM to emit an older version of dwarf, however,
98                // for macOS to understand. For more info see #11352
99                // This can be overridden using --llvm-opts -dwarf-version,N.
100                // Android has the same issue (#22398)
101                llvm::add_module_flag_u32(
102                    self.llmod,
103                    // In the case where multiple CGUs with different dwarf version
104                    // values are being merged together, such as with cross-crate
105                    // LTO, then we want to use the highest version of dwarf
106                    // we can. This matches Clang's behavior as well.
107                    llvm::ModuleFlagMergeBehavior::Max,
108                    "Dwarf Version",
109                    sess.dwarf_version(),
110                );
111            }
112            DebuginfoKind::Pdb => {
113                // Indicate that we want CodeView debug information
114                llvm::add_module_flag_u32(
115                    self.llmod,
116                    llvm::ModuleFlagMergeBehavior::Warning,
117                    "CodeView",
118                    1,
119                );
120            }
121        }
122
123        // Prevent bitcode readers from deleting the debug info.
124        llvm::add_module_flag_u32(
125            self.llmod,
126            llvm::ModuleFlagMergeBehavior::Warning,
127            "Debug Info Version",
128            unsafe { llvm::LLVMRustDebugMetadataVersion() },
129        );
130    }
131}
132
133/// Creates any deferred debug metadata nodes
134pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) {
135    if let Some(dbg_cx) = &cx.dbg_cx {
136        debug!("finalize");
137
138        if gdb::needs_gdb_debug_scripts_section(cx) {
139            // Add a .debug_gdb_scripts section to this compile-unit. This will
140            // cause GDB to try and load the gdb_load_rust_pretty_printers.py file,
141            // which activates the Rust pretty printers for binary this section is
142            // contained in.
143            gdb::get_or_insert_gdb_debug_scripts_section_global(cx);
144        }
145
146        dbg_cx.finalize(cx.sess());
147    }
148}
149
150impl<'ll> Builder<'_, 'll, '_> {
151    pub(crate) fn get_dbg_loc(&self) -> Option<&'ll DILocation> {
152        unsafe { llvm::LLVMGetCurrentDebugLocation2(self.llbuilder) }
153    }
154}
155
156impl<'ll> DebugInfoBuilderMethods for Builder<'_, 'll, '_> {
157    // FIXME(eddyb) find a common convention for all of the debuginfo-related
158    // names (choose between `dbg`, `debug`, `debuginfo`, `debug_info` etc.).
159    fn dbg_var_addr(
160        &mut self,
161        dbg_var: &'ll DIVariable,
162        dbg_loc: &'ll DILocation,
163        variable_alloca: Self::Value,
164        direct_offset: Size,
165        indirect_offsets: &[Size],
166        fragment: Option<Range<Size>>,
167    ) {
168        use dwarf_const::{DW_OP_LLVM_fragment, DW_OP_deref, DW_OP_plus_uconst};
169
170        // Convert the direct and indirect offsets and fragment byte range to address ops.
171        let mut addr_ops = SmallVec::<[u64; 8]>::new();
172
173        if direct_offset.bytes() > 0 {
174            addr_ops.push(DW_OP_plus_uconst);
175            addr_ops.push(direct_offset.bytes() as u64);
176        }
177        for &offset in indirect_offsets {
178            addr_ops.push(DW_OP_deref);
179            if offset.bytes() > 0 {
180                addr_ops.push(DW_OP_plus_uconst);
181                addr_ops.push(offset.bytes() as u64);
182            }
183        }
184        if let Some(fragment) = fragment {
185            // `DW_OP_LLVM_fragment` takes as arguments the fragment's
186            // offset and size, both of them in bits.
187            addr_ops.push(DW_OP_LLVM_fragment);
188            addr_ops.push(fragment.start.bits() as u64);
189            addr_ops.push((fragment.end - fragment.start).bits() as u64);
190        }
191
192        unsafe {
193            // FIXME(eddyb) replace `llvm.dbg.declare` with `llvm.dbg.addr`.
194            llvm::LLVMRustDIBuilderInsertDeclareAtEnd(
195                DIB(self.cx()),
196                variable_alloca,
197                dbg_var,
198                addr_ops.as_ptr(),
199                addr_ops.len() as c_uint,
200                dbg_loc,
201                self.llbb(),
202            );
203        }
204    }
205
206    fn set_dbg_loc(&mut self, dbg_loc: &'ll DILocation) {
207        unsafe {
208            llvm::LLVMSetCurrentDebugLocation2(self.llbuilder, dbg_loc);
209        }
210    }
211
212    fn clear_dbg_loc(&mut self) {
213        unsafe {
214            llvm::LLVMSetCurrentDebugLocation2(self.llbuilder, ptr::null());
215        }
216    }
217
218    fn insert_reference_to_gdb_debug_scripts_section_global(&mut self) {
219        gdb::insert_reference_to_gdb_debug_scripts_section_global(self)
220    }
221
222    fn set_var_name(&mut self, value: &'ll Value, name: &str) {
223        // Avoid wasting time if LLVM value names aren't even enabled.
224        if self.sess().fewer_names() {
225            return;
226        }
227
228        // Only function parameters and instructions are local to a function,
229        // don't change the name of anything else (e.g. globals).
230        let param_or_inst = unsafe {
231            llvm::LLVMIsAArgument(value).is_some() || llvm::LLVMIsAInstruction(value).is_some()
232        };
233        if !param_or_inst {
234            return;
235        }
236
237        // Avoid replacing the name if it already exists.
238        // While we could combine the names somehow, it'd
239        // get noisy quick, and the usefulness is dubious.
240        if llvm::get_value_name(value).is_empty() {
241            llvm::set_value_name(value, name.as_bytes());
242        }
243    }
244}
245
246/// A source code location used to generate debug information.
247// FIXME(eddyb) rename this to better indicate it's a duplicate of
248// `rustc_span::Loc` rather than `DILocation`, perhaps by making
249// `lookup_char_pos` return the right information instead.
250struct DebugLoc {
251    /// Information about the original source file.
252    file: Arc<SourceFile>,
253    /// The (1-based) line number.
254    line: u32,
255    /// The (1-based) column number.
256    col: u32,
257}
258
259impl<'ll> CodegenCx<'ll, '_> {
260    /// Looks up debug source information about a `BytePos`.
261    // FIXME(eddyb) rename this to better indicate it's a duplicate of
262    // `lookup_char_pos` rather than `dbg_loc`, perhaps by making
263    // `lookup_char_pos` return the right information instead.
264    fn lookup_debug_loc(&self, pos: BytePos) -> DebugLoc {
265        let (file, line, col) = match self.sess().source_map().lookup_line(pos) {
266            Ok(SourceFileAndLine { sf: file, line }) => {
267                let line_pos = file.lines()[line];
268
269                // Use 1-based indexing.
270                let line = (line + 1) as u32;
271                let col = (file.relative_position(pos) - line_pos).to_u32() + 1;
272
273                (file, line, col)
274            }
275            Err(file) => (file, UNKNOWN_LINE_NUMBER, UNKNOWN_COLUMN_NUMBER),
276        };
277
278        // For MSVC, omit the column number.
279        // Otherwise, emit it. This mimics clang behaviour.
280        // See discussion in https://github.com/rust-lang/rust/issues/42921
281        if self.sess().target.is_like_msvc {
282            DebugLoc { file, line, col: UNKNOWN_COLUMN_NUMBER }
283        } else {
284            DebugLoc { file, line, col }
285        }
286    }
287
288    fn create_template_type_parameter(
289        &self,
290        name: &str,
291        actual_type_metadata: &'ll DIType,
292    ) -> &'ll DITemplateTypeParameter {
293        unsafe {
294            llvm::LLVMRustDIBuilderCreateTemplateTypeParameter(
295                DIB(self),
296                None,
297                name.as_c_char_ptr(),
298                name.len(),
299                actual_type_metadata,
300            )
301        }
302    }
303}
304
305impl<'ll, 'tcx> DebugInfoCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
306    fn create_function_debug_context(
307        &self,
308        instance: Instance<'tcx>,
309        fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
310        llfn: &'ll Value,
311        mir: &mir::Body<'tcx>,
312    ) -> Option<FunctionDebugContext<'tcx, &'ll DIScope, &'ll DILocation>> {
313        if self.sess().opts.debuginfo == DebugInfo::None {
314            return None;
315        }
316
317        // Initialize fn debug context (including scopes).
318        let empty_scope = DebugScope {
319            dbg_scope: self.dbg_scope_fn(instance, fn_abi, Some(llfn)),
320            inlined_at: None,
321            file_start_pos: BytePos(0),
322            file_end_pos: BytePos(0),
323        };
324        let mut fn_debug_context = FunctionDebugContext {
325            scopes: IndexVec::from_elem(empty_scope, &mir.source_scopes),
326            inlined_function_scopes: Default::default(),
327        };
328
329        // Fill in all the scopes, with the information from the MIR body.
330        compute_mir_scopes(self, instance, mir, &mut fn_debug_context);
331
332        Some(fn_debug_context)
333    }
334
335    fn dbg_scope_fn(
336        &self,
337        instance: Instance<'tcx>,
338        fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
339        maybe_definition_llfn: Option<&'ll Value>,
340    ) -> &'ll DIScope {
341        let tcx = self.tcx;
342
343        let def_id = instance.def_id();
344        let (containing_scope, is_method) = get_containing_scope(self, instance);
345        let span = tcx.def_span(def_id);
346        let loc = self.lookup_debug_loc(span.lo());
347        let file_metadata = file_metadata(self, &loc.file);
348
349        let function_type_metadata =
350            create_subroutine_type(self, get_function_signature(self, fn_abi));
351
352        let mut name = String::with_capacity(64);
353        type_names::push_item_name(tcx, def_id, false, &mut name);
354
355        // Find the enclosing function, in case this is a closure.
356        let enclosing_fn_def_id = tcx.typeck_root_def_id(def_id);
357
358        // We look up the generics of the enclosing function and truncate the args
359        // to their length in order to cut off extra stuff that might be in there for
360        // closures or coroutines.
361        let generics = tcx.generics_of(enclosing_fn_def_id);
362        let args = instance.args.truncate_to(tcx, generics);
363
364        type_names::push_generic_params(
365            tcx,
366            tcx.normalize_erasing_regions(self.typing_env(), args),
367            &mut name,
368        );
369
370        let template_parameters = get_template_parameters(self, generics, args);
371
372        let linkage_name = &mangled_name_of_instance(self, instance).name;
373        // Omit the linkage_name if it is the same as subprogram name.
374        let linkage_name = if &name == linkage_name { "" } else { linkage_name };
375
376        // FIXME(eddyb) does this need to be separate from `loc.line` for some reason?
377        let scope_line = loc.line;
378
379        let mut flags = DIFlags::FlagPrototyped;
380
381        if fn_abi.ret.layout.is_uninhabited() {
382            flags |= DIFlags::FlagNoReturn;
383        }
384
385        let mut spflags = DISPFlags::SPFlagDefinition;
386        if is_node_local_to_unit(self, def_id) {
387            spflags |= DISPFlags::SPFlagLocalToUnit;
388        }
389        if self.sess().opts.optimize != config::OptLevel::No {
390            spflags |= DISPFlags::SPFlagOptimized;
391        }
392        if let Some((id, _)) = tcx.entry_fn(()) {
393            if id == def_id {
394                spflags |= DISPFlags::SPFlagMainSubprogram;
395            }
396        }
397
398        // When we're adding a method to a type DIE, we only want a DW_AT_declaration there, because
399        // LLVM LTO can't unify type definitions when a child DIE is a full subprogram definition.
400        // When we use this `decl` below, the subprogram definition gets created at the CU level
401        // with a DW_AT_specification pointing back to the type's declaration.
402        let decl = is_method.then(|| unsafe {
403            llvm::LLVMRustDIBuilderCreateMethod(
404                DIB(self),
405                containing_scope,
406                name.as_c_char_ptr(),
407                name.len(),
408                linkage_name.as_c_char_ptr(),
409                linkage_name.len(),
410                file_metadata,
411                loc.line,
412                function_type_metadata,
413                flags,
414                spflags & !DISPFlags::SPFlagDefinition,
415                template_parameters,
416            )
417        });
418
419        return unsafe {
420            llvm::LLVMRustDIBuilderCreateFunction(
421                DIB(self),
422                containing_scope,
423                name.as_c_char_ptr(),
424                name.len(),
425                linkage_name.as_c_char_ptr(),
426                linkage_name.len(),
427                file_metadata,
428                loc.line,
429                function_type_metadata,
430                scope_line,
431                flags,
432                spflags,
433                maybe_definition_llfn,
434                template_parameters,
435                decl,
436            )
437        };
438
439        fn get_function_signature<'ll, 'tcx>(
440            cx: &CodegenCx<'ll, 'tcx>,
441            fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
442        ) -> &'ll DIArray {
443            if cx.sess().opts.debuginfo != DebugInfo::Full {
444                return create_DIArray(DIB(cx), &[]);
445            }
446
447            let mut signature = Vec::with_capacity(fn_abi.args.len() + 1);
448
449            // Return type -- llvm::DIBuilder wants this at index 0
450            signature.push(if fn_abi.ret.is_ignore() {
451                None
452            } else {
453                Some(type_di_node(cx, fn_abi.ret.layout.ty))
454            });
455
456            // Arguments types
457            if cx.sess().target.is_like_msvc {
458                // FIXME(#42800):
459                // There is a bug in MSDIA that leads to a crash when it encounters
460                // a fixed-size array of `u8` or something zero-sized in a
461                // function-type (see #40477).
462                // As a workaround, we replace those fixed-size arrays with a
463                // pointer-type. So a function `fn foo(a: u8, b: [u8; 4])` would
464                // appear as `fn foo(a: u8, b: *const u8)` in debuginfo,
465                // and a function `fn bar(x: [(); 7])` as `fn bar(x: *const ())`.
466                // This transformed type is wrong, but these function types are
467                // already inaccurate due to ABI adjustments (see #42800).
468                signature.extend(fn_abi.args.iter().map(|arg| {
469                    let t = arg.layout.ty;
470                    let t = match t.kind() {
471                        ty::Array(ct, _)
472                            if (*ct == cx.tcx.types.u8) || cx.layout_of(*ct).is_zst() =>
473                        {
474                            Ty::new_imm_ptr(cx.tcx, *ct)
475                        }
476                        _ => t,
477                    };
478                    Some(type_di_node(cx, t))
479                }));
480            } else {
481                signature
482                    .extend(fn_abi.args.iter().map(|arg| Some(type_di_node(cx, arg.layout.ty))));
483            }
484
485            create_DIArray(DIB(cx), &signature[..])
486        }
487
488        fn get_template_parameters<'ll, 'tcx>(
489            cx: &CodegenCx<'ll, 'tcx>,
490            generics: &ty::Generics,
491            args: GenericArgsRef<'tcx>,
492        ) -> &'ll DIArray {
493            if args.types().next().is_none() {
494                return create_DIArray(DIB(cx), &[]);
495            }
496
497            // Again, only create type information if full debuginfo is enabled
498            let template_params: Vec<_> = if cx.sess().opts.debuginfo == DebugInfo::Full {
499                let names = get_parameter_names(cx, generics);
500                iter::zip(args, names)
501                    .filter_map(|(kind, name)| {
502                        kind.as_type().map(|ty| {
503                            let actual_type = cx.tcx.normalize_erasing_regions(cx.typing_env(), ty);
504                            let actual_type_metadata = type_di_node(cx, actual_type);
505                            Some(cx.create_template_type_parameter(
506                                name.as_str(),
507                                actual_type_metadata,
508                            ))
509                        })
510                    })
511                    .collect()
512            } else {
513                vec![]
514            };
515
516            create_DIArray(DIB(cx), &template_params)
517        }
518
519        fn get_parameter_names(cx: &CodegenCx<'_, '_>, generics: &ty::Generics) -> Vec<Symbol> {
520            let mut names = generics.parent.map_or_else(Vec::new, |def_id| {
521                get_parameter_names(cx, cx.tcx.generics_of(def_id))
522            });
523            names.extend(generics.own_params.iter().map(|param| param.name));
524            names
525        }
526
527        /// Returns a scope, plus `true` if that's a type scope for "class" methods,
528        /// otherwise `false` for plain namespace scopes.
529        fn get_containing_scope<'ll, 'tcx>(
530            cx: &CodegenCx<'ll, 'tcx>,
531            instance: Instance<'tcx>,
532        ) -> (&'ll DIScope, bool) {
533            // First, let's see if this is a method within an inherent impl. Because
534            // if yes, we want to make the result subroutine DIE a child of the
535            // subroutine's self-type.
536            if let Some(impl_def_id) = cx.tcx.impl_of_method(instance.def_id()) {
537                // If the method does *not* belong to a trait, proceed
538                if cx.tcx.trait_id_of_impl(impl_def_id).is_none() {
539                    let impl_self_ty = cx.tcx.instantiate_and_normalize_erasing_regions(
540                        instance.args,
541                        cx.typing_env(),
542                        cx.tcx.type_of(impl_def_id),
543                    );
544
545                    // Only "class" methods are generally understood by LLVM,
546                    // so avoid methods on other types (e.g., `<*mut T>::null`).
547                    if let ty::Adt(def, ..) = impl_self_ty.kind()
548                        && !def.is_box()
549                    {
550                        // Again, only create type information if full debuginfo is enabled
551                        if cx.sess().opts.debuginfo == DebugInfo::Full && !impl_self_ty.has_param()
552                        {
553                            return (type_di_node(cx, impl_self_ty), true);
554                        } else {
555                            return (namespace::item_namespace(cx, def.did()), false);
556                        }
557                    }
558                } else {
559                    // For trait method impls we still use the "parallel namespace"
560                    // strategy
561                }
562            }
563
564            let scope = namespace::item_namespace(
565                cx,
566                DefId {
567                    krate: instance.def_id().krate,
568                    index: cx
569                        .tcx
570                        .def_key(instance.def_id())
571                        .parent
572                        .expect("get_containing_scope: missing parent?"),
573                },
574            );
575            (scope, false)
576        }
577    }
578
579    fn dbg_loc(
580        &self,
581        scope: &'ll DIScope,
582        inlined_at: Option<&'ll DILocation>,
583        span: Span,
584    ) -> &'ll DILocation {
585        // When emitting debugging information, DWARF (i.e. everything but MSVC)
586        // treats line 0 as a magic value meaning that the code could not be
587        // attributed to any line in the source. That's also exactly what dummy
588        // spans are. Make that equivalence here, rather than passing dummy spans
589        // to lookup_debug_loc, which will return line 1 for them.
590        let (line, col) = if span.is_dummy() && !self.sess().target.is_like_msvc {
591            (0, 0)
592        } else {
593            let DebugLoc { line, col, .. } = self.lookup_debug_loc(span.lo());
594            (line, col)
595        };
596
597        unsafe { llvm::LLVMDIBuilderCreateDebugLocation(self.llcx, line, col, scope, inlined_at) }
598    }
599
600    fn create_vtable_debuginfo(
601        &self,
602        ty: Ty<'tcx>,
603        trait_ref: Option<ty::ExistentialTraitRef<'tcx>>,
604        vtable: Self::Value,
605    ) {
606        metadata::create_vtable_di_node(self, ty, trait_ref, vtable)
607    }
608
609    fn extend_scope_to_file(
610        &self,
611        scope_metadata: &'ll DIScope,
612        file: &rustc_span::SourceFile,
613    ) -> &'ll DILexicalBlock {
614        metadata::extend_scope_to_file(self, scope_metadata, file)
615    }
616
617    fn debuginfo_finalize(&self) {
618        finalize(self)
619    }
620
621    // FIXME(eddyb) find a common convention for all of the debuginfo-related
622    // names (choose between `dbg`, `debug`, `debuginfo`, `debug_info` etc.).
623    fn create_dbg_var(
624        &self,
625        variable_name: Symbol,
626        variable_type: Ty<'tcx>,
627        scope_metadata: &'ll DIScope,
628        variable_kind: VariableKind,
629        span: Span,
630    ) -> &'ll DIVariable {
631        let loc = self.lookup_debug_loc(span.lo());
632        let file_metadata = file_metadata(self, &loc.file);
633
634        let type_metadata = type_di_node(self, variable_type);
635
636        let (argument_index, dwarf_tag) = match variable_kind {
637            ArgumentVariable(index) => (index as c_uint, DW_TAG_arg_variable),
638            LocalVariable => (0, DW_TAG_auto_variable),
639        };
640        let align = self.align_of(variable_type);
641
642        let name = variable_name.as_str();
643        unsafe {
644            llvm::LLVMRustDIBuilderCreateVariable(
645                DIB(self),
646                dwarf_tag,
647                scope_metadata,
648                name.as_c_char_ptr(),
649                name.len(),
650                file_metadata,
651                loc.line,
652                type_metadata,
653                true,
654                DIFlags::FlagZero,
655                argument_index,
656                align.bits() as u32,
657            )
658        }
659    }
660}