From 68d29cba95da88fafc5853c78b484f2708a305d8 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Wed, 7 Sep 2016 11:23:49 +1200 Subject: [PATCH] save-analysis: add docs data --- src/librustc_save_analysis/data.rs | 11 ++++++ src/librustc_save_analysis/dump_visitor.rs | 33 +++++++++++++---- src/librustc_save_analysis/external_data.rs | 24 +++++++++++- src/librustc_save_analysis/json_api_dumper.rs | 12 ++++++ src/librustc_save_analysis/json_dumper.rs | 12 ++++++ src/librustc_save_analysis/lib.rs | 37 ++++++++++++++++--- 6 files changed, 116 insertions(+), 13 deletions(-) diff --git a/src/librustc_save_analysis/data.rs b/src/librustc_save_analysis/data.rs index a58cce0745f..5f6e65e289f 100644 --- a/src/librustc_save_analysis/data.rs +++ b/src/librustc_save_analysis/data.rs @@ -134,6 +134,7 @@ pub struct EnumData { pub scope: NodeId, pub variants: Vec, pub visibility: Visibility, + pub docs: String, } /// Data for extern crates. @@ -167,6 +168,7 @@ pub struct FunctionData { pub value: String, pub visibility: Visibility, pub parent: Option, + pub docs: String, } /// Data about a function call. @@ -213,6 +215,7 @@ pub struct MacroData { pub span: Span, pub name: String, pub qualname: String, + pub docs: String, } /// Data about a macro use. @@ -248,6 +251,7 @@ pub struct MethodData { pub value: String, pub decl_id: Option, pub visibility: Visibility, + pub docs: String, } /// Data for modules. @@ -261,6 +265,7 @@ pub struct ModData { pub filename: String, pub items: Vec, pub visibility: Visibility, + pub docs: String, } /// Data for a reference to a module. @@ -283,6 +288,7 @@ pub struct StructData { pub value: String, pub fields: Vec, pub visibility: Visibility, + pub docs: String, } #[derive(Debug, RustcEncodable)] @@ -295,6 +301,7 @@ pub struct StructVariantData { pub value: String, pub scope: NodeId, pub parent: Option, + pub docs: String, } #[derive(Debug, RustcEncodable)] @@ -307,6 +314,7 @@ pub struct TraitData { pub value: String, pub items: Vec, pub visibility: Visibility, + pub docs: String, } #[derive(Debug, RustcEncodable)] @@ -319,6 +327,7 @@ pub struct TupleVariantData { pub value: String, pub scope: NodeId, pub parent: Option, + pub docs: String, } /// Data for a typedef. @@ -331,6 +340,7 @@ pub struct TypeDefData { pub value: String, pub visibility: Visibility, pub parent: Option, + pub docs: String, } /// Data for a reference to a type or trait. @@ -374,6 +384,7 @@ pub struct VariableData { pub value: String, pub type_value: String, pub visibility: Visibility, + pub docs: String, } #[derive(Debug, RustcEncodable)] diff --git a/src/librustc_save_analysis/dump_visitor.rs b/src/librustc_save_analysis/dump_visitor.rs index a695a070662..faf9cb2b0e3 100644 --- a/src/librustc_save_analysis/dump_visitor.rs +++ b/src/librustc_save_analysis/dump_visitor.rs @@ -36,7 +36,7 @@ use std::collections::HashSet; use std::hash::*; -use syntax::ast::{self, NodeId, PatKind}; +use syntax::ast::{self, NodeId, PatKind, Attribute}; use syntax::parse::token::{self, keywords}; use syntax::visit::{self, Visitor}; use syntax::print::pprust::{path_to_string, ty_to_string, bounds_to_string, generics_to_string}; @@ -44,7 +44,7 @@ use syntax::codemap::Spanned; use syntax_pos::*; -use super::{escape, generated_code, SaveContext, PathCollector}; +use super::{escape, generated_code, SaveContext, PathCollector, docs_for_attrs}; use super::data::*; use super::dump::Dump; use super::external_data::Lower; @@ -368,6 +368,7 @@ fn process_formals(&mut self, formals: &Vec, qualname: &str) { scope: 0, parent: None, visibility: Visibility::Inherited, + docs: String::new(), }.lower(self.tcx)); } } @@ -380,6 +381,7 @@ fn process_method(&mut self, id: ast::NodeId, name: ast::Name, vis: Visibility, + attrs: &[Attribute], span: Span) { debug!("process_method: {}:{}", id, name); @@ -421,6 +423,7 @@ fn process_method(&mut self, value: sig_str, decl_id: decl_id, visibility: vis, + docs: docs_for_attrs(attrs), }.lower(self.tcx)); } @@ -491,6 +494,7 @@ fn process_generic_params(&mut self, value: String::new(), visibility: Visibility::Inherited, parent: None, + docs: String::new(), }.lower(self.tcx)); } } @@ -541,7 +545,8 @@ fn process_assoc_const(&mut self, typ: &ast::Ty, expr: &ast::Expr, parent_id: NodeId, - vis: Visibility) { + vis: Visibility, + attrs: &[Attribute]) { let qualname = format!("::{}", self.tcx.node_path_str(id)); let sub_span = self.span.sub_span_after_keyword(span, keywords::Const); @@ -558,6 +563,7 @@ fn process_assoc_const(&mut self, scope: self.cur_scope, parent: Some(parent_id), visibility: vis, + docs: docs_for_attrs(attrs), }.lower(self.tcx)); } @@ -600,6 +606,7 @@ fn process_struct(&mut self, value: val, fields: fields, visibility: From::from(&item.vis), + docs: docs_for_attrs(&item.attrs), }.lower(self.tcx)); } @@ -653,6 +660,7 @@ fn process_enum(&mut self, value: val, scope: enum_data.scope, parent: Some(item.id), + docs: docs_for_attrs(&variant.node.attrs), }.lower(self.tcx)); } } @@ -677,6 +685,7 @@ fn process_enum(&mut self, value: val, scope: enum_data.scope, parent: Some(item.id), + docs: docs_for_attrs(&variant.node.attrs), }.lower(self.tcx)); } } @@ -759,6 +768,7 @@ fn process_trait(&mut self, value: val, items: methods.iter().map(|i| i.id).collect(), visibility: From::from(&item.vis), + docs: docs_for_attrs(&item.attrs), }.lower(self.tcx)); } @@ -1007,6 +1017,7 @@ fn process_var_decl(&mut self, p: &ast::Pat, value: String) { scope: 0, parent: None, visibility: Visibility::Inherited, + docs: String::new(), }.lower(self.tcx)); } } @@ -1036,7 +1047,9 @@ fn process_macro_use(&mut self, span: Span, id: NodeId) { self.dumper.macro_data(MacroData { span: sub_span, name: data.name.clone(), - qualname: qualname.clone() + qualname: qualname.clone(), + // FIXME where do macro docs come from? + docs: String::new(), }.lower(self.tcx)); } } @@ -1049,7 +1062,7 @@ fn process_macro_use(&mut self, span: Span, id: NodeId) { qualname: qualname, scope: data.scope, callee_span: data.callee_span, - imported: data.imported + imported: data.imported, }.lower(self.tcx)); } } @@ -1065,7 +1078,8 @@ fn process_trait_item(&mut self, trait_item: &ast::TraitItem, trait_id: NodeId) &ty, &expr, trait_id, - Visibility::Public); + Visibility::Public, + &trait_item.attrs); } ast::TraitItemKind::Method(ref sig, ref body) => { self.process_method(sig, @@ -1073,6 +1087,7 @@ fn process_trait_item(&mut self, trait_item: &ast::TraitItem, trait_id: NodeId) trait_item.id, trait_item.ident.name, Visibility::Public, + &trait_item.attrs, trait_item.span); } ast::TraitItemKind::Const(_, None) | @@ -1091,7 +1106,8 @@ fn process_impl_item(&mut self, impl_item: &ast::ImplItem, impl_id: NodeId) { &ty, &expr, impl_id, - From::from(&impl_item.vis)); + From::from(&impl_item.vis), + &impl_item.attrs); } ast::ImplItemKind::Method(ref sig, ref body) => { self.process_method(sig, @@ -1099,6 +1115,7 @@ fn process_impl_item(&mut self, impl_item: &ast::ImplItem, impl_id: NodeId) { impl_item.id, impl_item.ident.name, From::from(&impl_item.vis), + &impl_item.attrs, impl_item.span); } ast::ImplItemKind::Type(_) | @@ -1240,6 +1257,7 @@ fn visit_item(&mut self, item: &ast::Item) { value: value, visibility: From::from(&item.vis), parent: None, + docs: docs_for_attrs(&item.attrs), }.lower(self.tcx)); } @@ -1429,6 +1447,7 @@ fn visit_arm(&mut self, arm: &ast::Arm) { scope: 0, parent: None, visibility: Visibility::Inherited, + docs: String::new(), }.lower(self.tcx)); } } diff --git a/src/librustc_save_analysis/external_data.rs b/src/librustc_save_analysis/external_data.rs index 4333c6dd18e..3642346582b 100644 --- a/src/librustc_save_analysis/external_data.rs +++ b/src/librustc_save_analysis/external_data.rs @@ -93,6 +93,7 @@ pub struct EnumData { pub scope: DefId, pub variants: Vec, pub visibility: Visibility, + pub docs: String, } impl Lower for data::EnumData { @@ -108,6 +109,7 @@ fn lower(self, tcx: TyCtxt) -> EnumData { scope: make_def_id(self.scope, &tcx.map), variants: self.variants.into_iter().map(|id| make_def_id(id, &tcx.map)).collect(), visibility: self.visibility, + docs: self.docs, } } } @@ -170,6 +172,7 @@ pub struct FunctionData { pub value: String, pub visibility: Visibility, pub parent: Option, + pub docs: String, } impl Lower for data::FunctionData { @@ -186,6 +189,7 @@ fn lower(self, tcx: TyCtxt) -> FunctionData { value: self.value, visibility: self.visibility, parent: self.parent.map(|id| make_def_id(id, &tcx.map)), + docs: self.docs, } } } @@ -257,6 +261,7 @@ pub struct MacroData { pub span: SpanData, pub name: String, pub qualname: String, + pub docs: String, } impl Lower for data::MacroData { @@ -267,6 +272,7 @@ fn lower(self, tcx: TyCtxt) -> MacroData { span: SpanData::from_span(self.span, tcx.sess.codemap()), name: self.name, qualname: self.qualname, + docs: self.docs, } } } @@ -330,7 +336,8 @@ pub struct MethodData { pub value: String, pub decl_id: Option, pub visibility: Visibility, - pub parent: Option + pub parent: Option, + pub docs: String, } impl Lower for data::MethodData { @@ -347,6 +354,7 @@ fn lower(self, tcx: TyCtxt) -> MethodData { decl_id: self.decl_id, visibility: self.visibility, parent: Some(make_def_id(self.scope, &tcx.map)), + docs: self.docs, } } } @@ -362,6 +370,7 @@ pub struct ModData { pub filename: String, pub items: Vec, pub visibility: Visibility, + pub docs: String, } impl Lower for data::ModData { @@ -377,6 +386,7 @@ fn lower(self, tcx: TyCtxt) -> ModData { filename: self.filename, items: self.items.into_iter().map(|id| make_def_id(id, &tcx.map)).collect(), visibility: self.visibility, + docs: self.docs, } } } @@ -414,6 +424,7 @@ pub struct StructData { pub value: String, pub fields: Vec, pub visibility: Visibility, + pub docs: String, } impl Lower for data::StructData { @@ -430,6 +441,7 @@ fn lower(self, tcx: TyCtxt) -> StructData { value: self.value, fields: self.fields.into_iter().map(|id| make_def_id(id, &tcx.map)).collect(), visibility: self.visibility, + docs: self.docs, } } } @@ -444,6 +456,7 @@ pub struct StructVariantData { pub value: String, pub scope: DefId, pub parent: Option, + pub docs: String, } impl Lower for data::StructVariantData { @@ -459,6 +472,7 @@ fn lower(self, tcx: TyCtxt) -> StructVariantData { value: self.value, scope: make_def_id(self.scope, &tcx.map), parent: self.parent.map(|id| make_def_id(id, &tcx.map)), + docs: self.docs, } } } @@ -473,6 +487,7 @@ pub struct TraitData { pub value: String, pub items: Vec, pub visibility: Visibility, + pub docs: String, } impl Lower for data::TraitData { @@ -488,6 +503,7 @@ fn lower(self, tcx: TyCtxt) -> TraitData { value: self.value, items: self.items.into_iter().map(|id| make_def_id(id, &tcx.map)).collect(), visibility: self.visibility, + docs: self.docs, } } } @@ -502,6 +518,7 @@ pub struct TupleVariantData { pub value: String, pub scope: DefId, pub parent: Option, + pub docs: String, } impl Lower for data::TupleVariantData { @@ -517,6 +534,7 @@ fn lower(self, tcx: TyCtxt) -> TupleVariantData { value: self.value, scope: make_def_id(self.scope, &tcx.map), parent: self.parent.map(|id| make_def_id(id, &tcx.map)), + docs: self.docs, } } } @@ -531,6 +549,7 @@ pub struct TypeDefData { pub value: String, pub visibility: Visibility, pub parent: Option, + pub docs: String, } impl Lower for data::TypeDefData { @@ -545,6 +564,7 @@ fn lower(self, tcx: TyCtxt) -> TypeDefData { value: self.value, visibility: self.visibility, parent: self.parent.map(|id| make_def_id(id, &tcx.map)), + docs: self.docs, } } } @@ -632,6 +652,7 @@ pub struct VariableData { pub type_value: String, pub parent: Option, pub visibility: Visibility, + pub docs: String, } impl Lower for data::VariableData { @@ -649,6 +670,7 @@ fn lower(self, tcx: TyCtxt) -> VariableData { type_value: self.type_value, parent: self.parent.map(|id| make_def_id(id, &tcx.map)), visibility: self.visibility, + docs: self.docs, } } } diff --git a/src/librustc_save_analysis/json_api_dumper.rs b/src/librustc_save_analysis/json_api_dumper.rs index 874babb907e..751c2d06ee7 100644 --- a/src/librustc_save_analysis/json_api_dumper.rs +++ b/src/librustc_save_analysis/json_api_dumper.rs @@ -168,6 +168,7 @@ struct Def { parent: Option, children: Vec, decl_id: Option, + docs: String, } #[derive(Debug, RustcEncodable)] @@ -209,6 +210,7 @@ fn from(data: EnumData) -> Option { parent: None, children: data.variants.into_iter().map(|id| From::from(id)).collect(), decl_id: None, + docs: data.docs, }), _ => None, } @@ -227,6 +229,7 @@ fn from(data: TupleVariantData) -> Option { parent: data.parent.map(|id| From::from(id)), children: vec![], decl_id: None, + docs: data.docs, }) } } @@ -242,6 +245,7 @@ fn from(data: StructVariantData) -> Option { parent: data.parent.map(|id| From::from(id)), children: vec![], decl_id: None, + docs: data.docs, }) } } @@ -258,6 +262,7 @@ fn from(data: StructData) -> Option { parent: None, children: data.fields.into_iter().map(|id| From::from(id)).collect(), decl_id: None, + docs: data.docs, }), _ => None, } @@ -276,6 +281,7 @@ fn from(data: TraitData) -> Option { children: data.items.into_iter().map(|id| From::from(id)).collect(), parent: None, decl_id: None, + docs: data.docs, }), _ => None, } @@ -294,6 +300,7 @@ fn from(data: FunctionData) -> Option { children: vec![], parent: data.parent.map(|id| From::from(id)), decl_id: None, + docs: data.docs, }), _ => None, } @@ -312,6 +319,7 @@ fn from(data: MethodData) -> Option { children: vec![], parent: data.parent.map(|id| From::from(id)), decl_id: data.decl_id.map(|id| From::from(id)), + docs: data.docs, }), _ => None, } @@ -329,6 +337,7 @@ fn from(data: MacroData) -> Option { children: vec![], parent: None, decl_id: None, + docs: data.docs, }) } } @@ -345,6 +354,7 @@ fn from(data:ModData) -> Option { children: data.items.into_iter().map(|id| From::from(id)).collect(), parent: None, decl_id: None, + docs: data.docs, }), _ => None, } @@ -363,6 +373,7 @@ fn from(data: TypeDefData) -> Option { children: vec![], parent: data.parent.map(|id| From::from(id)), decl_id: None, + docs: String::new(), }), _ => None, } @@ -386,6 +397,7 @@ fn from(data: VariableData) -> Option { children: vec![], parent: data.parent.map(|id| From::from(id)), decl_id: None, + docs: data.docs, }), _ => None, } diff --git a/src/librustc_save_analysis/json_dumper.rs b/src/librustc_save_analysis/json_dumper.rs index b1955cbd7b8..3000376e724 100644 --- a/src/librustc_save_analysis/json_dumper.rs +++ b/src/librustc_save_analysis/json_dumper.rs @@ -183,6 +183,7 @@ struct Def { value: String, children: Vec, decl_id: Option, + docs: String, } #[derive(Debug, RustcEncodable)] @@ -223,6 +224,7 @@ fn from(data: EnumData) -> Def { value: data.value, children: data.variants.into_iter().map(|id| From::from(id)).collect(), decl_id: None, + docs: data.docs, } } } @@ -238,6 +240,7 @@ fn from(data: TupleVariantData) -> Def { value: data.value, children: vec![], decl_id: None, + docs: data.docs, } } } @@ -252,6 +255,7 @@ fn from(data: StructVariantData) -> Def { value: data.value, children: vec![], decl_id: None, + docs: data.docs, } } } @@ -266,6 +270,7 @@ fn from(data: StructData) -> Def { value: data.value, children: data.fields.into_iter().map(|id| From::from(id)).collect(), decl_id: None, + docs: data.docs, } } } @@ -280,6 +285,7 @@ fn from(data: TraitData) -> Def { value: data.value, children: data.items.into_iter().map(|id| From::from(id)).collect(), decl_id: None, + docs: data.docs, } } } @@ -294,6 +300,7 @@ fn from(data: FunctionData) -> Def { value: data.value, children: vec![], decl_id: None, + docs: data.docs, } } } @@ -308,6 +315,7 @@ fn from(data: MethodData) -> Def { value: data.value, children: vec![], decl_id: data.decl_id.map(|id| From::from(id)), + docs: data.docs, } } } @@ -322,6 +330,7 @@ fn from(data: MacroData) -> Def { value: String::new(), children: vec![], decl_id: None, + docs: data.docs, } } } @@ -336,6 +345,7 @@ fn from(data:ModData) -> Def { value: data.filename, children: data.items.into_iter().map(|id| From::from(id)).collect(), decl_id: None, + docs: data.docs, } } } @@ -350,6 +360,7 @@ fn from(data: TypeDefData) -> Def { value: data.value, children: vec![], decl_id: None, + docs: String::new(), } } } @@ -369,6 +380,7 @@ fn from(data: VariableData) -> Def { value: data.value, children: vec![], decl_id: None, + docs: data.docs, } } } diff --git a/src/librustc_save_analysis/lib.rs b/src/librustc_save_analysis/lib.rs index f32baf30ff4..eeb8d02429c 100644 --- a/src/librustc_save_analysis/lib.rs +++ b/src/librustc_save_analysis/lib.rs @@ -29,6 +29,7 @@ extern crate serialize as rustc_serialize; extern crate syntax_pos; + mod csv_dumper; mod json_api_dumper; mod json_dumper; @@ -50,8 +51,8 @@ use std::fs::{self, File}; use std::path::{Path, PathBuf}; -use syntax::ast::{self, NodeId, PatKind}; -use syntax::parse::token::{self, keywords}; +use syntax::ast::{self, NodeId, PatKind, Attribute}; +use syntax::parse::token::{self, keywords, InternedString}; use syntax::visit::{self, Visitor}; use syntax::print::pprust::{ty_to_string, arg_to_string}; use syntax::codemap::MacroAttribute; @@ -142,6 +143,7 @@ pub fn get_item_data(&self, item: &ast::Item) -> Option { value: make_signature(decl, generics), visibility: From::from(&item.vis), parent: None, + docs: docs_for_attrs(&item.attrs), })) } ast::ItemKind::Static(ref typ, mt, ref expr) => { @@ -168,6 +170,7 @@ pub fn get_item_data(&self, item: &ast::Item) -> Option { value: value, type_value: ty_to_string(&typ), visibility: From::from(&item.vis), + docs: docs_for_attrs(&item.attrs), })) } ast::ItemKind::Const(ref typ, ref expr) => { @@ -185,6 +188,7 @@ pub fn get_item_data(&self, item: &ast::Item) -> Option { value: self.span_utils.snippet(expr.span), type_value: ty_to_string(&typ), visibility: From::from(&item.vis), + docs: docs_for_attrs(&item.attrs), })) } ast::ItemKind::Mod(ref m) => { @@ -204,6 +208,7 @@ pub fn get_item_data(&self, item: &ast::Item) -> Option { filename: filename, items: m.items.iter().map(|i| i.id).collect(), visibility: From::from(&item.vis), + docs: docs_for_attrs(&item.attrs), })) } ast::ItemKind::Enum(ref def, _) => { @@ -225,6 +230,7 @@ pub fn get_item_data(&self, item: &ast::Item) -> Option { scope: self.enclosing_scope(item.id), variants: def.variants.iter().map(|v| v.node.data.id()).collect(), visibility: From::from(&item.vis), + docs: docs_for_attrs(&item.attrs), })) } ast::ItemKind::Impl(_, _, _, ref trait_ref, ref typ, _) => { @@ -291,6 +297,7 @@ pub fn get_field_data(&self, field: &ast::StructField, value: "".to_owned(), type_value: typ, visibility: From::from(&field.vis), + docs: docs_for_attrs(&field.attrs), }) } else { None @@ -303,7 +310,7 @@ pub fn get_method_data(&self, id: ast::NodeId, name: ast::Name, span: Span) -> Option { // The qualname for a method is the trait name or name of the struct in an impl in // which the method is declared in, followed by the method's name. - let (qualname, vis) = match self.tcx.impl_of_method(self.tcx.map.local_def_id(id)) { + let (qualname, vis, docs) = match self.tcx.impl_of_method(self.tcx.map.local_def_id(id)) { Some(impl_id) => match self.tcx.map.get_if_local(impl_id) { Some(NodeItem(item)) => { match item.node { @@ -316,7 +323,7 @@ pub fn get_method_data(&self, id: ast::NodeId, result.push_str(&self.tcx.item_path_str(def_id)); } result.push_str(">"); - (result, From::from(&item.vis)) + (result, From::from(&item.vis), docs_for_attrs(&item.attrs)) } _ => { span_bug!(span, @@ -338,7 +345,9 @@ pub fn get_method_data(&self, id: ast::NodeId, Some(def_id) => { match self.tcx.map.get_if_local(def_id) { Some(NodeItem(item)) => { - (format!("::{}", self.tcx.item_path_str(def_id)), From::from(&item.vis)) + (format!("::{}", self.tcx.item_path_str(def_id)), + From::from(&item.vis), + docs_for_attrs(&item.attrs)) } r => { span_bug!(span, @@ -382,6 +391,7 @@ pub fn get_method_data(&self, id: ast::NodeId, value: String::new(), visibility: vis, parent: Some(parent_scope), + docs: docs, }) } @@ -739,6 +749,23 @@ fn visit_pat(&mut self, p: &ast::Pat) { } } + +fn docs_for_attrs(attrs: &[Attribute]) -> String { + let doc = InternedString::new("doc"); + let mut result = String::new(); + + for attr in attrs { + if attr.name() == doc { + if let Some(ref val) = attr.value_str() { + result.push_str(val); + result.push('\n'); + } + } + } + + result +} + #[derive(Clone, Copy, Debug)] pub enum Format { Csv,