Skip to content

Commit

Permalink
save-analysis: add parent info to api dumps
Browse files Browse the repository at this point in the history
The parent id is used for constructing rustdoc URLs by clients
  • Loading branch information
nrc committed Sep 1, 2016
1 parent 4e4306c commit 4dc7b58
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 70 deletions.
3 changes: 2 additions & 1 deletion src/librustc_driver/driver.rs
Expand Up @@ -248,7 +248,8 @@ fn keep_hygiene_data(sess: &Session) -> bool {
fn keep_ast(sess: &Session) -> bool {
sess.opts.debugging_opts.keep_ast ||
sess.opts.debugging_opts.save_analysis ||
sess.opts.debugging_opts.save_analysis_csv
sess.opts.debugging_opts.save_analysis_csv ||
sess.opts.debugging_opts.save_analysis_api
}

/// The name used for source code that doesn't originate in a file
Expand Down
9 changes: 7 additions & 2 deletions src/librustc_save_analysis/data.rs
Expand Up @@ -166,6 +166,7 @@ pub struct FunctionData {
pub scope: NodeId,
pub value: String,
pub visibility: Visibility,
pub parent: Option<NodeId>,
}

/// Data about a function call.
Expand Down Expand Up @@ -292,7 +293,8 @@ pub struct StructVariantData {
pub qualname: String,
pub type_value: String,
pub value: String,
pub scope: NodeId
pub scope: NodeId,
pub parent: Option<NodeId>,
}

#[derive(Debug, RustcEncodable)]
Expand All @@ -315,7 +317,8 @@ pub struct TupleVariantData {
pub qualname: String,
pub type_value: String,
pub value: String,
pub scope: NodeId
pub scope: NodeId,
pub parent: Option<NodeId>,
}

/// Data for a typedef.
Expand All @@ -327,6 +330,7 @@ pub struct TypeDefData {
pub qualname: String,
pub value: String,
pub visibility: Visibility,
pub parent: Option<NodeId>,
}

/// Data for a reference to a type or trait.
Expand Down Expand Up @@ -366,6 +370,7 @@ pub struct VariableData {
pub qualname: String,
pub span: Span,
pub scope: NodeId,
pub parent: Option<NodeId>,
pub value: String,
pub type_value: String,
pub visibility: Visibility,
Expand Down
131 changes: 71 additions & 60 deletions src/librustc_save_analysis/dump_visitor.rs
Expand Up @@ -365,6 +365,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
type_value: typ,
value: String::new(),
scope: 0,
parent: None,
visibility: Visibility::Inherited,
}.lower(self.tcx));
}
Expand Down Expand Up @@ -488,6 +489,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
qualname: qualname,
value: String::new(),
visibility: Visibility::Inherited,
parent: None,
}.lower(self.tcx));
}
}
Expand Down Expand Up @@ -531,13 +533,14 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
self.visit_expr(expr);
}

fn process_const(&mut self,
id: ast::NodeId,
name: ast::Name,
span: Span,
typ: &ast::Ty,
expr: &ast::Expr,
vis: Visibility) {
fn process_assoc_const(&mut self,
id: ast::NodeId,
name: ast::Name,
span: Span,
typ: &ast::Ty,
expr: &ast::Expr,
parent_id: NodeId,
vis: Visibility) {
let qualname = format!("::{}", self.tcx.node_path_str(id));

let sub_span = self.span.sub_span_after_keyword(span, keywords::Const);
Expand All @@ -552,6 +555,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
value: self.span.snippet(expr.span),
type_value: ty_to_string(&typ),
scope: self.cur_scope,
parent: Some(parent_id),
visibility: vis,
}.lower(self.tcx));
}
Expand Down Expand Up @@ -646,7 +650,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
qualname: qualname,
type_value: enum_data.qualname.clone(),
value: val,
scope: enum_data.scope
scope: enum_data.scope,
parent: Some(item.id),
}.lower(self.tcx));
}
}
Expand All @@ -669,7 +674,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
qualname: qualname,
type_value: enum_data.qualname.clone(),
value: val,
scope: enum_data.scope
scope: enum_data.scope,
parent: Some(item.id),
}.lower(self.tcx));
}
}
Expand Down Expand Up @@ -722,7 +728,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
}
self.process_generic_params(type_parameters, item.span, "", item.id);
for impl_item in impl_items {
self.visit_impl_item(impl_item);
self.process_impl_item(impl_item, item.id);
}
}

Expand Down Expand Up @@ -792,7 +798,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
// walk generics and methods
self.process_generic_params(generics, item.span, &qualname, item.id);
for method in methods {
self.visit_trait_item(method)
self.process_trait_item(method, item.id)
}
}

Expand Down Expand Up @@ -998,6 +1004,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
value: value,
type_value: typ,
scope: 0,
parent: None,
visibility: Visibility::Inherited,
}.lower(self.tcx));
}
Expand Down Expand Up @@ -1046,6 +1053,57 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
}
}
}

fn process_trait_item(&mut self, trait_item: &ast::TraitItem, trait_id: NodeId) {
self.process_macro_use(trait_item.span, trait_item.id);
match trait_item.node {
ast::TraitItemKind::Const(ref ty, Some(ref expr)) => {
self.process_assoc_const(trait_item.id,
trait_item.ident.name,
trait_item.span,
&ty,
&expr,
trait_id,
Visibility::Public);
}
ast::TraitItemKind::Method(ref sig, ref body) => {
self.process_method(sig,
body.as_ref().map(|x| &**x),
trait_item.id,
trait_item.ident.name,
Visibility::Public,
trait_item.span);
}
ast::TraitItemKind::Const(_, None) |
ast::TraitItemKind::Type(..) |
ast::TraitItemKind::Macro(_) => {}
}
}

fn process_impl_item(&mut self, impl_item: &ast::ImplItem, impl_id: NodeId) {
self.process_macro_use(impl_item.span, impl_item.id);
match impl_item.node {
ast::ImplItemKind::Const(ref ty, ref expr) => {
self.process_assoc_const(impl_item.id,
impl_item.ident.name,
impl_item.span,
&ty,
&expr,
impl_id,
From::from(&impl_item.vis));
}
ast::ImplItemKind::Method(ref sig, ref body) => {
self.process_method(sig,
Some(body),
impl_item.id,
impl_item.ident.name,
From::from(&impl_item.vis),
impl_item.span);
}
ast::ImplItemKind::Type(_) |
ast::ImplItemKind::Macro(_) => {}
}
}
}

impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor for DumpVisitor<'l, 'tcx, 'll, D> {
Expand Down Expand Up @@ -1180,6 +1238,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor for DumpVisitor<'l, 'tcx, 'll, D>
qualname: qualname.clone(),
value: value,
visibility: From::from(&item.vis),
parent: None,
}.lower(self.tcx));
}

Expand All @@ -1204,55 +1263,6 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor for DumpVisitor<'l, 'tcx, 'll, D>
}
}

fn visit_trait_item(&mut self, trait_item: &ast::TraitItem) {
self.process_macro_use(trait_item.span, trait_item.id);
match trait_item.node {
ast::TraitItemKind::Const(ref ty, Some(ref expr)) => {
self.process_const(trait_item.id,
trait_item.ident.name,
trait_item.span,
&ty,
&expr,
Visibility::Public);
}
ast::TraitItemKind::Method(ref sig, ref body) => {
self.process_method(sig,
body.as_ref().map(|x| &**x),
trait_item.id,
trait_item.ident.name,
Visibility::Public,
trait_item.span);
}
ast::TraitItemKind::Const(_, None) |
ast::TraitItemKind::Type(..) |
ast::TraitItemKind::Macro(_) => {}
}
}

fn visit_impl_item(&mut self, impl_item: &ast::ImplItem) {
self.process_macro_use(impl_item.span, impl_item.id);
match impl_item.node {
ast::ImplItemKind::Const(ref ty, ref expr) => {
self.process_const(impl_item.id,
impl_item.ident.name,
impl_item.span,
&ty,
&expr,
From::from(&impl_item.vis));
}
ast::ImplItemKind::Method(ref sig, ref body) => {
self.process_method(sig,
Some(body),
impl_item.id,
impl_item.ident.name,
From::from(&impl_item.vis),
impl_item.span);
}
ast::ImplItemKind::Type(_) |
ast::ImplItemKind::Macro(_) => {}
}
}

fn visit_ty(&mut self, t: &ast::Ty) {
self.process_macro_use(t.span, t.id);
match t.node {
Expand Down Expand Up @@ -1416,6 +1426,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor for DumpVisitor<'l, 'tcx, 'll, D>
value: value,
type_value: String::new(),
scope: 0,
parent: None,
visibility: Visibility::Inherited,
}.lower(self.tcx));
}
Expand Down
14 changes: 13 additions & 1 deletion src/librustc_save_analysis/external_data.rs
Expand Up @@ -169,6 +169,7 @@ pub struct FunctionData {
pub scope: DefId,
pub value: String,
pub visibility: Visibility,
pub parent: Option<DefId>,
}

impl Lower for data::FunctionData {
Expand All @@ -184,6 +185,7 @@ impl Lower for data::FunctionData {
scope: make_def_id(self.scope, &tcx.map),
value: self.value,
visibility: self.visibility,
parent: self.parent.map(|id| make_def_id(id, &tcx.map)),
}
}
}
Expand Down Expand Up @@ -328,6 +330,7 @@ pub struct MethodData {
pub value: String,
pub decl_id: Option<DefId>,
pub visibility: Visibility,
pub parent: Option<DefId>
}

impl Lower for data::MethodData {
Expand All @@ -343,6 +346,7 @@ impl Lower for data::MethodData {
value: self.value,
decl_id: self.decl_id,
visibility: self.visibility,
parent: Some(make_def_id(self.scope, &tcx.map)),
}
}
}
Expand Down Expand Up @@ -438,7 +442,8 @@ pub struct StructVariantData {
pub qualname: String,
pub type_value: String,
pub value: String,
pub scope: DefId
pub scope: DefId,
pub parent: Option<DefId>,
}

impl Lower for data::StructVariantData {
Expand All @@ -453,6 +458,7 @@ impl Lower for data::StructVariantData {
type_value: self.type_value,
value: self.value,
scope: make_def_id(self.scope, &tcx.map),
parent: self.parent.map(|id| make_def_id(id, &tcx.map)),
}
}
}
Expand Down Expand Up @@ -495,6 +501,7 @@ pub struct TupleVariantData {
pub type_value: String,
pub value: String,
pub scope: DefId,
pub parent: Option<DefId>,
}

impl Lower for data::TupleVariantData {
Expand All @@ -509,6 +516,7 @@ impl Lower for data::TupleVariantData {
type_value: self.type_value,
value: self.value,
scope: make_def_id(self.scope, &tcx.map),
parent: self.parent.map(|id| make_def_id(id, &tcx.map)),
}
}
}
Expand All @@ -522,6 +530,7 @@ pub struct TypeDefData {
pub qualname: String,
pub value: String,
pub visibility: Visibility,
pub parent: Option<DefId>,
}

impl Lower for data::TypeDefData {
Expand All @@ -535,6 +544,7 @@ impl Lower for data::TypeDefData {
qualname: self.qualname,
value: self.value,
visibility: self.visibility,
parent: self.parent.map(|id| make_def_id(id, &tcx.map)),
}
}
}
Expand Down Expand Up @@ -620,6 +630,7 @@ pub struct VariableData {
pub scope: DefId,
pub value: String,
pub type_value: String,
pub parent: Option<DefId>,
pub visibility: Visibility,
}

Expand All @@ -636,6 +647,7 @@ impl Lower for data::VariableData {
scope: make_def_id(self.scope, &tcx.map),
value: self.value,
type_value: self.type_value,
parent: self.parent.map(|id| make_def_id(id, &tcx.map)),
visibility: self.visibility,
}
}
Expand Down

0 comments on commit 4dc7b58

Please sign in to comment.