|
| 1 | +use rustc::hir::def_id::CrateNum; |
| 2 | +use rustc::mir; |
| 3 | +use crate::traits::*; |
| 4 | + |
| 5 | +use syntax_pos::{DUMMY_SP, BytePos, Span}; |
| 6 | + |
| 7 | +use super::FunctionCx; |
| 8 | + |
| 9 | +pub enum FunctionDebugContext<D> { |
| 10 | + RegularContext(FunctionDebugContextData<D>), |
| 11 | + DebugInfoDisabled, |
| 12 | + FunctionWithoutDebugInfo, |
| 13 | +} |
| 14 | + |
| 15 | +impl<D> FunctionDebugContext<D> { |
| 16 | + pub fn get_ref(&self, span: Span) -> &FunctionDebugContextData<D> { |
| 17 | + match *self { |
| 18 | + FunctionDebugContext::RegularContext(ref data) => data, |
| 19 | + FunctionDebugContext::DebugInfoDisabled => { |
| 20 | + span_bug!( |
| 21 | + span, |
| 22 | + "debuginfo: Error trying to access FunctionDebugContext \ |
| 23 | + although debug info is disabled!", |
| 24 | + ); |
| 25 | + } |
| 26 | + FunctionDebugContext::FunctionWithoutDebugInfo => { |
| 27 | + span_bug!( |
| 28 | + span, |
| 29 | + "debuginfo: Error trying to access FunctionDebugContext \ |
| 30 | + for function that should be ignored by debug info!", |
| 31 | + ); |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +/// Enables emitting source locations for the given functions. |
| 38 | +/// |
| 39 | +/// Since we don't want source locations to be emitted for the function prelude, |
| 40 | +/// they are disabled when beginning to codegen a new function. This functions |
| 41 | +/// switches source location emitting on and must therefore be called before the |
| 42 | +/// first real statement/expression of the function is codegened. |
| 43 | +pub fn start_emitting_source_locations<D>(dbg_context: &mut FunctionDebugContext<D>) { |
| 44 | + match *dbg_context { |
| 45 | + FunctionDebugContext::RegularContext(ref mut data) => { |
| 46 | + data.source_locations_enabled = true; |
| 47 | + }, |
| 48 | + _ => { /* safe to ignore */ } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +pub struct FunctionDebugContextData<D> { |
| 53 | + pub fn_metadata: D, |
| 54 | + pub source_locations_enabled: bool, |
| 55 | + pub defining_crate: CrateNum, |
| 56 | +} |
| 57 | + |
| 58 | +pub enum VariableAccess<'a, V> { |
| 59 | + // The llptr given is an alloca containing the variable's value |
| 60 | + DirectVariable { alloca: V }, |
| 61 | + // The llptr given is an alloca containing the start of some pointer chain |
| 62 | + // leading to the variable's content. |
| 63 | + IndirectVariable { alloca: V, address_operations: &'a [i64] } |
| 64 | +} |
| 65 | + |
| 66 | +pub enum VariableKind { |
| 67 | + ArgumentVariable(usize /*index*/), |
| 68 | + LocalVariable, |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | +#[derive(Clone, Copy, Debug)] |
| 73 | +pub struct DebugScope<D> { |
| 74 | + pub scope_metadata: Option<D>, |
| 75 | + // Start and end offsets of the file to which this DIScope belongs. |
| 76 | + // These are used to quickly determine whether some span refers to the same file. |
| 77 | + pub file_start_pos: BytePos, |
| 78 | + pub file_end_pos: BytePos, |
| 79 | +} |
| 80 | + |
| 81 | +impl<D> DebugScope<D> { |
| 82 | + pub fn is_valid(&self) -> bool { |
| 83 | + !self.scope_metadata.is_none() |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { |
| 88 | + pub fn set_debug_loc( |
| 89 | + &mut self, |
| 90 | + bx: &mut Bx, |
| 91 | + source_info: mir::SourceInfo |
| 92 | + ) { |
| 93 | + let (scope, span) = self.debug_loc(source_info); |
| 94 | + bx.set_source_location(&mut self.debug_context, scope, span); |
| 95 | + } |
| 96 | + |
| 97 | + pub fn debug_loc(&self, source_info: mir::SourceInfo) -> (Option<Bx::DIScope>, Span) { |
| 98 | + // Bail out if debug info emission is not enabled. |
| 99 | + match self.debug_context { |
| 100 | + FunctionDebugContext::DebugInfoDisabled | |
| 101 | + FunctionDebugContext::FunctionWithoutDebugInfo => { |
| 102 | + return (self.scopes[source_info.scope].scope_metadata, source_info.span); |
| 103 | + } |
| 104 | + FunctionDebugContext::RegularContext(_) =>{} |
| 105 | + } |
| 106 | + |
| 107 | + // In order to have a good line stepping behavior in debugger, we overwrite debug |
| 108 | + // locations of macro expansions with that of the outermost expansion site |
| 109 | + // (unless the crate is being compiled with `-Z debug-macros`). |
| 110 | + if !source_info.span.from_expansion() || |
| 111 | + self.cx.sess().opts.debugging_opts.debug_macros { |
| 112 | + let scope = self.scope_metadata_for_loc(source_info.scope, source_info.span.lo()); |
| 113 | + (scope, source_info.span) |
| 114 | + } else { |
| 115 | + // Walk up the macro expansion chain until we reach a non-expanded span. |
| 116 | + // We also stop at the function body level because no line stepping can occur |
| 117 | + // at the level above that. |
| 118 | + let span = syntax_pos::hygiene::walk_chain(source_info.span, self.mir.span.ctxt()); |
| 119 | + let scope = self.scope_metadata_for_loc(source_info.scope, span.lo()); |
| 120 | + // Use span of the outermost expansion site, while keeping the original lexical scope. |
| 121 | + (scope, span) |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + // DILocations inherit source file name from the parent DIScope. Due to macro expansions |
| 126 | + // it may so happen that the current span belongs to a different file than the DIScope |
| 127 | + // corresponding to span's containing source scope. If so, we need to create a DIScope |
| 128 | + // "extension" into that file. |
| 129 | + fn scope_metadata_for_loc(&self, scope_id: mir::SourceScope, pos: BytePos) |
| 130 | + -> Option<Bx::DIScope> { |
| 131 | + let scope_metadata = self.scopes[scope_id].scope_metadata; |
| 132 | + if pos < self.scopes[scope_id].file_start_pos || |
| 133 | + pos >= self.scopes[scope_id].file_end_pos { |
| 134 | + let sm = self.cx.sess().source_map(); |
| 135 | + let defining_crate = self.debug_context.get_ref(DUMMY_SP).defining_crate; |
| 136 | + Some(self.cx.extend_scope_to_file( |
| 137 | + scope_metadata.unwrap(), |
| 138 | + &sm.lookup_char_pos(pos).file, |
| 139 | + defining_crate |
| 140 | + )) |
| 141 | + } else { |
| 142 | + scope_metadata |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments