]> git.proxmox.com Git - rustc.git/blob - src/librustc_trans/debuginfo/utils.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / librustc_trans / debuginfo / utils.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Utility Functions.
12
13 use super::{FunctionDebugContext, CrateDebugContext};
14 use super::namespace::namespace_for_item;
15
16 use rustc::hir::def_id::DefId;
17
18 use llvm;
19 use llvm::debuginfo::{DIScope, DIBuilderRef, DIDescriptor, DIArray};
20 use machine;
21 use common::{CrateContext, FunctionContext};
22 use type_::Type;
23
24 use syntax::codemap::Span;
25 use syntax::{ast, codemap};
26
27 pub fn is_node_local_to_unit(cx: &CrateContext, node_id: ast::NodeId) -> bool
28 {
29 // The is_local_to_unit flag indicates whether a function is local to the
30 // current compilation unit (i.e. if it is *static* in the C-sense). The
31 // *reachable* set should provide a good approximation of this, as it
32 // contains everything that might leak out of the current crate (by being
33 // externally visible or by being inlined into something externally
34 // visible). It might better to use the `exported_items` set from
35 // `driver::CrateAnalysis` in the future, but (atm) this set is not
36 // available in the translation pass.
37 !cx.reachable().contains(&node_id)
38 }
39
40 #[allow(non_snake_case)]
41 pub fn create_DIArray(builder: DIBuilderRef, arr: &[DIDescriptor]) -> DIArray {
42 return unsafe {
43 llvm::LLVMDIBuilderGetOrCreateArray(builder, arr.as_ptr(), arr.len() as u32)
44 };
45 }
46
47 pub fn contains_nodebug_attribute(attributes: &[ast::Attribute]) -> bool {
48 attributes.iter().any(|attr| {
49 let meta_item: &ast::MetaItem = &attr.node.value;
50 match meta_item.node {
51 ast::MetaItemKind::Word(ref value) => &value[..] == "no_debug",
52 _ => false
53 }
54 })
55 }
56
57 /// Return codemap::Loc corresponding to the beginning of the span
58 pub fn span_start(cx: &CrateContext, span: Span) -> codemap::Loc {
59 cx.sess().codemap().lookup_char_pos(span.lo)
60 }
61
62 pub fn size_and_align_of(cx: &CrateContext, llvm_type: Type) -> (u64, u64) {
63 (machine::llsize_of_alloc(cx, llvm_type), machine::llalign_of_min(cx, llvm_type) as u64)
64 }
65
66 pub fn bytes_to_bits(bytes: u64) -> u64 {
67 bytes * 8
68 }
69
70 #[inline]
71 pub fn debug_context<'a, 'tcx>(cx: &'a CrateContext<'a, 'tcx>)
72 -> &'a CrateDebugContext<'tcx> {
73 let debug_context: &'a CrateDebugContext<'tcx> = cx.dbg_cx().as_ref().unwrap();
74 debug_context
75 }
76
77 #[inline]
78 #[allow(non_snake_case)]
79 pub fn DIB(cx: &CrateContext) -> DIBuilderRef {
80 cx.dbg_cx().as_ref().unwrap().builder
81 }
82
83 pub fn fn_should_be_ignored(fcx: &FunctionContext) -> bool {
84 match fcx.debug_context {
85 FunctionDebugContext::RegularContext(_) => false,
86 _ => true
87 }
88 }
89
90 pub fn assert_type_for_node_id(cx: &CrateContext,
91 node_id: ast::NodeId,
92 error_reporting_span: Span) {
93 if !cx.tcx().node_types().contains_key(&node_id) {
94 span_bug!(error_reporting_span,
95 "debuginfo: Could not find type for node id!");
96 }
97 }
98
99 pub fn get_namespace_and_span_for_item(cx: &CrateContext, def_id: DefId)
100 -> (DIScope, Span) {
101 let containing_scope = namespace_for_item(cx, def_id).scope;
102 let definition_span = cx.tcx().map.def_id_span(def_id, codemap::DUMMY_SP /* (1) */ );
103
104 // (1) For external items there is no span information
105
106 (containing_scope, definition_span)
107 }