@@ -85,6 +85,43 @@ continuation, storing all state needed to continue traversal at the type members
8585been registered with the cache. (This implementation approach might be a tad over-engineered and
8686may change in the future)
8787
88+
89+ ## Source Locations and Line Information
90+ In addition to data type descriptions the debugging information must also allow to map machine code
91+ locations back to source code locations in order to be useful. This functionality is also handled in
92+ this module. The following functions allow to control source mappings:
93+
94+ + set_source_location()
95+ + clear_source_location()
96+ + start_emitting_source_locations()
97+
98+ `set_source_location()` allows to set the current source location. All IR instructions created after
99+ a call to this function will be linked to the given source location, until another location is
100+ specified with `set_source_location()` or the source location is cleared with
101+ `clear_source_location()`. In the later case, subsequent IR instruction will not be linked to any
102+ source location. As you can see, this is a stateful API (mimicking the one in LLVM), so be careful
103+ with source locations set by previous calls. It's probably best to not rely on any specific state
104+ being present at a given point in code.
105+
106+ One topic that deserves some extra attention is *function prologues*. At the beginning of a
107+ function's machine code there are typically a few instructions for loading argument values into
108+ allocas and checking if there's enough stack space for the function to execute. This *prologue* is
109+ not visible in the source code and LLVM puts a special PROLOGUE END marker into the line table at
110+ the first non-prologue instruction of the function. In order to find out where the prologue ends,
111+ LLVM looks for the first instruction in the function body that is linked to a source location. So,
112+ when generating prologue instructions we have to make sure that we don't emit source location
113+ information until the 'real' function body begins. For this reason, source location emission is
114+ disabled by default for any new function being translated and is only activated after a call to the
115+ third function from the list above, `start_emitting_source_locations()`. This function should be
116+ called right before regularly starting to translate the top-level block of the given function.
117+
118+ There is one exception to the above rule: `llvm.dbg.declare` instruction must be linked to the
119+ source location of the variable being declared. For function parameters these `llvm.dbg.declare`
120+ instructions typically occur in the middle of the prologue, however, they are ignored by LLVM's
121+ prologue detection. The `create_argument_metadata()` and related functions take care of linking the
122+ `llvm.dbg.declare` instructions to the correct source locations even while source location emission
123+ is still disabled, so there is no need to do anything special with source location handling here.
124+
88125*/
89126
90127
@@ -651,7 +688,16 @@ pub fn create_function_debug_context(cx: &mut CrateContext,
651688 ( function_name. clone ( ) , file_metadata)
652689 } ;
653690
654- let scope_line = get_scope_line ( cx, top_level_block, loc. line ) ;
691+ // Clang sets this parameter to the opening brace of the function's block, so let's do this too.
692+ let scope_line = span_start ( cx, top_level_block. span ) . line ;
693+
694+ // The is_local_to_unit flag indicates whether a function is local to the current compilation
695+ // unit (i.e. if it is *static* in the C-sense). The *reachable* set should provide a good
696+ // approximation of this, as it contains everything that might leak out of the current crate
697+ // (by being externally visible or by being inlined into something externally visible). It might
698+ // better to use the `exported_items` set from `driver::CrateAnalysis` in the future, but (atm)
699+ // this set is not available in the translation pass.
700+ let is_local_to_unit = !cx. reachable . contains ( & fn_ast_id) ;
655701
656702 let fn_metadata = function_name. with_c_str ( |function_name| {
657703 linkage_name. with_c_str ( |linkage_name| {
@@ -664,7 +710,7 @@ pub fn create_function_debug_context(cx: &mut CrateContext,
664710 file_metadata,
665711 loc. line as c_uint ,
666712 function_type_metadata,
667- false ,
713+ is_local_to_unit ,
668714 true ,
669715 scope_line as c_uint ,
670716 FlagPrototyped as c_uint ,
@@ -687,6 +733,9 @@ pub fn create_function_debug_context(cx: &mut CrateContext,
687733 let arg_pats = fn_decl. inputs . map ( |arg_ref| arg_ref. pat ) ;
688734 populate_scope_map ( cx, arg_pats, top_level_block, fn_metadata, & mut fn_debug_context. scope_map ) ;
689735
736+ // Clear the debug location so we don't assign them in the function prelude
737+ set_debug_location ( cx, UnknownLocation ) ;
738+
690739 return FunctionDebugContext ( fn_debug_context) ;
691740
692741 fn get_function_signature ( cx : & mut CrateContext ,
@@ -837,21 +886,6 @@ pub fn create_function_debug_context(cx: &mut CrateContext,
837886
838887 return create_DIArray ( DIB ( cx) , template_params) ;
839888 }
840-
841- fn get_scope_line ( cx : & CrateContext ,
842- top_level_block : & ast:: Block ,
843- default : uint )
844- -> uint {
845- match * top_level_block {
846- ast:: Block { stmts : ref statements, .. } if statements. len ( ) > 0 => {
847- span_start ( cx, statements[ 0 ] . span ) . line
848- }
849- ast:: Block { expr : Some ( @ref expr) , .. } => {
850- span_start ( cx, expr. span ) . line
851- }
852- _ => default
853- }
854- }
855889}
856890
857891//=-------------------------------------------------------------------------------------------------
@@ -2128,7 +2162,8 @@ fn set_debug_location(cx: &mut CrateContext, debug_location: DebugLocation) {
21282162 let metadata_node;
21292163
21302164 match debug_location {
2131- KnownLocation { scope, line, col } => {
2165+ KnownLocation { scope, line, .. } => {
2166+ let col = 0 ; // Always set the column to zero like Clang and GCC
21322167 debug ! ( "setting debug location to {} {}" , line, col) ;
21332168 let elements = [ C_i32 ( line as i32 ) , C_i32 ( col as i32 ) , scope, ptr:: null ( ) ] ;
21342169 unsafe {
@@ -2244,7 +2279,14 @@ fn populate_scope_map(cx: &mut CrateContext,
22442279 } )
22452280 }
22462281
2247- walk_block ( cx, fn_entry_block, & mut scope_stack, scope_map) ;
2282+ // Clang creates a separate scope for function bodies, so let's do this too
2283+ with_new_scope ( cx,
2284+ fn_entry_block. span ,
2285+ & mut scope_stack,
2286+ scope_map,
2287+ |cx, scope_stack, scope_map| {
2288+ walk_block ( cx, fn_entry_block, scope_stack, scope_map) ;
2289+ } ) ;
22482290
22492291 // local helper functions for walking the AST.
22502292 fn with_new_scope ( cx : & mut CrateContext ,
0 commit comments