Skip to content

Commit d042c51

Browse files
author
Ariel Ben-Yehuda
authored
Rollup merge of rust-lang#40348 - nrc:save-extern-fn, r=eddyb
Handle extern functions and statics in save-analysis r? @eddyb
2 parents cb1d4c9 + 0aceb99 commit d042c51

File tree

3 files changed

+97
-9
lines changed

3 files changed

+97
-9
lines changed

src/librustc_save_analysis/dump_visitor.rs

+37-9
Original file line numberDiff line numberDiff line change
@@ -1363,15 +1363,6 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll,
13631363
debug!("visit_expr {:?}", ex.node);
13641364
self.process_macro_use(ex.span, ex.id);
13651365
match ex.node {
1366-
ast::ExprKind::Call(ref _f, ref _args) => {
1367-
// Don't need to do anything for function calls,
1368-
// because just walking the callee path does what we want.
1369-
visit::walk_expr(self, ex);
1370-
}
1371-
ast::ExprKind::Path(_, ref path) => {
1372-
self.process_path(ex.id, path, None);
1373-
visit::walk_expr(self, ex);
1374-
}
13751366
ast::ExprKind::Struct(ref path, ref fields, ref base) => {
13761367
let hir_expr = self.save_ctxt.tcx.hir.expect_expr(ex.id);
13771368
let adt = match self.save_ctxt.tables.expr_ty_opt(&hir_expr) {
@@ -1469,6 +1460,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll,
14691460
self.visit_expr(element);
14701461
self.nest_tables(count.id, |v| v.visit_expr(count));
14711462
}
1463+
// In particular, we take this branch for call and path expressions,
1464+
// where we'll index the idents involved just by continuing to walk.
14721465
_ => {
14731466
visit::walk_expr(self, ex)
14741467
}
@@ -1567,4 +1560,39 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll,
15671560
walk_list!(self, visit_ty, &l.ty);
15681561
walk_list!(self, visit_expr, &l.init);
15691562
}
1563+
1564+
fn visit_foreign_item(&mut self, item: &'l ast::ForeignItem) {
1565+
match item.node {
1566+
ast::ForeignItemKind::Fn(ref decl, ref generics) => {
1567+
if let Some(fn_data) = self.save_ctxt.get_extern_item_data(item) {
1568+
down_cast_data!(fn_data, FunctionData, item.span);
1569+
if !self.span.filter_generated(Some(fn_data.span), item.span) {
1570+
self.dumper.function(fn_data.clone().lower(self.tcx));
1571+
}
1572+
1573+
self.nest_tables(item.id, |v| v.process_formals(&decl.inputs,
1574+
&fn_data.qualname));
1575+
self.process_generic_params(generics, item.span, &fn_data.qualname, item.id);
1576+
}
1577+
1578+
for arg in &decl.inputs {
1579+
self.visit_ty(&arg.ty);
1580+
}
1581+
1582+
if let ast::FunctionRetTy::Ty(ref ret_ty) = decl.output {
1583+
self.visit_ty(&ret_ty);
1584+
}
1585+
}
1586+
ast::ForeignItemKind::Static(ref ty, _) => {
1587+
if let Some(var_data) = self.save_ctxt.get_extern_item_data(item) {
1588+
down_cast_data!(var_data, VariableData, item.span);
1589+
if !self.span.filter_generated(Some(var_data.span), item.span) {
1590+
self.dumper.variable(var_data.lower(self.tcx));
1591+
}
1592+
}
1593+
1594+
self.visit_ty(ty);
1595+
}
1596+
}
1597+
}
15701598
}

src/librustc_save_analysis/lib.rs

+55
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,46 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
116116
result
117117
}
118118

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

779+
fn sig_base_extern(&self, item: &ast::ForeignItem) -> Signature {
780+
let text = self.span_utils.signature_string_for_span(item.span);
781+
let name = item.ident.to_string();
782+
let ident_start = text.find(&name).expect("Name not in signature?");
783+
let ident_end = ident_start + name.len();
784+
Signature {
785+
span: mk_sp(item.span.lo, item.span.lo + BytePos(text.len() as u32)),
786+
text: text,
787+
ident_start: ident_start,
788+
ident_end: ident_end,
789+
defs: vec![],
790+
refs: vec![],
791+
}
792+
}
793+
739794
#[inline]
740795
pub fn enclosing_scope(&self, id: NodeId) -> NodeId {
741796
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)