Skip to content

Commit ccdbb31

Browse files
committed
save-analysis: index extern blocks
1 parent 4143d40 commit ccdbb31

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

src/librustc_save_analysis/dump_visitor.rs

+35
Original file line numberDiff line numberDiff line change
@@ -1599,4 +1599,39 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll,
15991599
walk_list!(self, visit_ty, &l.ty);
16001600
walk_list!(self, visit_expr, &l.init);
16011601
}
1602+
1603+
fn visit_foreign_item(&mut self, item: &'l ast::ForeignItem) {
1604+
match item.node {
1605+
ast::ForeignItemKind::Fn(ref decl, ref generics) => {
1606+
if let Some(fn_data) = self.save_ctxt.get_extern_item_data(item) {
1607+
down_cast_data!(fn_data, FunctionData, item.span);
1608+
if !self.span.filter_generated(Some(fn_data.span), item.span) {
1609+
self.dumper.function(fn_data.clone().lower(self.tcx));
1610+
}
1611+
1612+
self.nest_tables(item.id, |v| v.process_formals(&decl.inputs,
1613+
&fn_data.qualname));
1614+
self.process_generic_params(generics, item.span, &fn_data.qualname, item.id);
1615+
}
1616+
1617+
for arg in &decl.inputs {
1618+
self.visit_ty(&arg.ty);
1619+
}
1620+
1621+
if let ast::FunctionRetTy::Ty(ref ret_ty) = decl.output {
1622+
self.visit_ty(&ret_ty);
1623+
}
1624+
}
1625+
ast::ForeignItemKind::Static(ref ty, _) => {
1626+
if let Some(var_data) = self.save_ctxt.get_extern_item_data(item) {
1627+
down_cast_data!(var_data, VariableData, item.span);
1628+
if !self.span.filter_generated(Some(var_data.span), item.span) {
1629+
self.dumper.variable(var_data.lower(self.tcx));
1630+
}
1631+
}
1632+
1633+
self.visit_ty(ty);
1634+
}
1635+
}
1636+
}
16021637
}

src/librustc_save_analysis/lib.rs

+55
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,46 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
120120
result
121121
}
122122

123+
pub fn get_extern_item_data(&self, item: &ast::ForeignItem) -> Option<Data> {
124+
let qualname = format!("::{}", self.tcx.node_path_str(item.id));
125+
match item.node {
126+
ast::ForeignItemKind::Fn(ref decl, ref generics) => {
127+
let sub_span = self.span_utils.sub_span_after_keyword(item.span, keywords::Fn);
128+
Some(Data::FunctionData(FunctionData {
129+
id: item.id,
130+
name: item.ident.to_string(),
131+
qualname: qualname,
132+
declaration: None,
133+
span: sub_span.unwrap(),
134+
scope: self.enclosing_scope(item.id),
135+
value: make_signature(decl, generics),
136+
visibility: From::from(&item.vis),
137+
parent: None,
138+
docs: docs_for_attrs(&item.attrs),
139+
sig: self.sig_base_extern(item),
140+
}))
141+
}
142+
ast::ForeignItemKind::Static(ref ty, m) => {
143+
let keyword = if m { keywords::Mut } else { keywords::Static };
144+
let sub_span = self.span_utils.sub_span_after_keyword(item.span, keyword);
145+
Some(Data::VariableData(VariableData {
146+
id: item.id,
147+
kind: VariableKind::Static,
148+
name: item.ident.to_string(),
149+
qualname: qualname,
150+
span: sub_span.unwrap(),
151+
scope: self.enclosing_scope(item.id),
152+
parent: None,
153+
value: String::new(),
154+
type_value: ty_to_string(ty),
155+
visibility: From::from(&item.vis),
156+
docs: docs_for_attrs(&item.attrs),
157+
sig: Some(self.sig_base_extern(item)),
158+
}))
159+
}
160+
}
161+
}
162+
123163
pub fn get_item_data(&self, item: &ast::Item) -> Option<Data> {
124164
match item.node {
125165
ast::ItemKind::Fn(ref decl, .., ref generics, _) => {
@@ -751,6 +791,21 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
751791
}
752792
}
753793

794+
fn sig_base_extern(&self, item: &ast::ForeignItem) -> Signature {
795+
let text = self.span_utils.signature_string_for_span(item.span);
796+
let name = item.ident.to_string();
797+
let ident_start = text.find(&name).expect("Name not in signature?");
798+
let ident_end = ident_start + name.len();
799+
Signature {
800+
span: mk_sp(item.span.lo, item.span.lo + BytePos(text.len() as u32)),
801+
text: text,
802+
ident_start: ident_start,
803+
ident_end: ident_end,
804+
defs: vec![],
805+
refs: vec![],
806+
}
807+
}
808+
754809
#[inline]
755810
pub fn enclosing_scope(&self, id: NodeId) -> NodeId {
756811
self.tcx.hir.get_enclosing_scope(id).unwrap_or(CRATE_NODE_ID)

src/test/run-make/save-analysis-fail/foo.rs

+5
Original file line numberDiff line numberDiff line change
@@ -448,3 +448,8 @@ fn test_format_args() {
448448
print!("{0} + {} = {}", x, y);
449449
print!("x is {}, y is {1}, name is {n}", x, y, n = name);
450450
}
451+
452+
extern {
453+
static EXTERN_FOO: u8;
454+
fn extern_foo(a: u8, b: i32) -> String;
455+
}

0 commit comments

Comments
 (0)