Skip to content

Commit 3fe451b

Browse files
committed
Auto merge of #40348 - nrc:save-extern-fn, r=eddyb
Handle extern functions and statics in save-analysis r? @eddyb
2 parents 2564711 + d76daf5 commit 3fe451b

File tree

3 files changed

+101
-9
lines changed

3 files changed

+101
-9
lines changed

src/librustc_save_analysis/dump_visitor.rs

+37-9
Original file line numberDiff line numberDiff line change
@@ -1401,15 +1401,6 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll,
14011401
debug!("visit_expr {:?}", ex.node);
14021402
self.process_macro_use(ex.span, ex.id);
14031403
match ex.node {
1404-
ast::ExprKind::Call(ref _f, ref _args) => {
1405-
// Don't need to do anything for function calls,
1406-
// because just walking the callee path does what we want.
1407-
visit::walk_expr(self, ex);
1408-
}
1409-
ast::ExprKind::Path(_, ref path) => {
1410-
self.process_path(ex.id, path, None);
1411-
visit::walk_expr(self, ex);
1412-
}
14131404
ast::ExprKind::Struct(ref path, ref fields, ref base) => {
14141405
let hir_expr = self.save_ctxt.tcx.hir.expect_expr(ex.id);
14151406
let adt = match self.save_ctxt.tables.expr_ty_opt(&hir_expr) {
@@ -1507,6 +1498,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll,
15071498
self.visit_expr(element);
15081499
self.nest_tables(count.id, |v| v.visit_expr(count));
15091500
}
1501+
// In particular, we take this branch for call and path expressions,
1502+
// where we'll index the idents involved just by continuing to walk.
15101503
_ => {
15111504
visit::walk_expr(self, ex)
15121505
}
@@ -1606,4 +1599,39 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll,
16061599
walk_list!(self, visit_ty, &l.ty);
16071600
walk_list!(self, visit_expr, &l.init);
16081601
}
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+
}
16091637
}

src/librustc_save_analysis/lib.rs

+59
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,50 @@ 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+
filter!(self.span_utils, sub_span, item.span, None);
129+
Some(Data::FunctionData(FunctionData {
130+
id: item.id,
131+
name: item.ident.to_string(),
132+
qualname: qualname,
133+
declaration: None,
134+
span: sub_span.unwrap(),
135+
scope: self.enclosing_scope(item.id),
136+
value: make_signature(decl, generics),
137+
visibility: From::from(&item.vis),
138+
parent: None,
139+
docs: docs_for_attrs(&item.attrs),
140+
sig: self.sig_base_extern(item),
141+
attributes: item.attrs.clone(),
142+
}))
143+
}
144+
ast::ForeignItemKind::Static(ref ty, m) => {
145+
let keyword = if m { keywords::Mut } else { keywords::Static };
146+
let sub_span = self.span_utils.sub_span_after_keyword(item.span, keyword);
147+
filter!(self.span_utils, sub_span, item.span, None);
148+
Some(Data::VariableData(VariableData {
149+
id: item.id,
150+
kind: VariableKind::Static,
151+
name: item.ident.to_string(),
152+
qualname: qualname,
153+
span: sub_span.unwrap(),
154+
scope: self.enclosing_scope(item.id),
155+
parent: None,
156+
value: String::new(),
157+
type_value: ty_to_string(ty),
158+
visibility: From::from(&item.vis),
159+
docs: docs_for_attrs(&item.attrs),
160+
sig: Some(self.sig_base_extern(item)),
161+
attributes: item.attrs.clone(),
162+
}))
163+
}
164+
}
165+
}
166+
123167
pub fn get_item_data(&self, item: &ast::Item) -> Option<Data> {
124168
match item.node {
125169
ast::ItemKind::Fn(ref decl, .., ref generics, _) => {
@@ -751,6 +795,21 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
751795
}
752796
}
753797

798+
fn sig_base_extern(&self, item: &ast::ForeignItem) -> Signature {
799+
let text = self.span_utils.signature_string_for_span(item.span);
800+
let name = item.ident.to_string();
801+
let ident_start = text.find(&name).expect("Name not in signature?");
802+
let ident_end = ident_start + name.len();
803+
Signature {
804+
span: Span { hi: item.span.lo + BytePos(text.len() as u32), ..item.span },
805+
text: text,
806+
ident_start: ident_start,
807+
ident_end: ident_end,
808+
defs: vec![],
809+
refs: vec![],
810+
}
811+
}
812+
754813
#[inline]
755814
pub fn enclosing_scope(&self, id: NodeId) -> NodeId {
756815
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)