Export attributes in save-analysis data

Some annotations like the "test" annotations might be of interest for
other projects, especially rls. Export all attributes in a new
attributes item.
This commit is contained in:
Jonas Bushart 2017-02-14 11:05:53 +01:00 committed by Alex Crichton
parent f573db4f80
commit a07c9a20b7
5 changed files with 171 additions and 6 deletions

View File

@ -15,7 +15,7 @@
use rustc::hir;
use rustc::hir::def_id::{CrateNum, DefId};
use syntax::ast::{self, NodeId};
use syntax::ast::{self, Attribute, NodeId};
use syntax_pos::Span;
pub struct CrateData {
@ -136,6 +136,7 @@ pub struct EnumData {
pub visibility: Visibility,
pub docs: String,
pub sig: Signature,
pub attributes: Vec<Attribute>,
}
/// Data for extern crates.
@ -171,6 +172,7 @@ pub struct FunctionData {
pub parent: Option<DefId>,
pub docs: String,
pub sig: Signature,
pub attributes: Vec<Attribute>,
}
/// Data about a function call.
@ -256,6 +258,7 @@ pub struct MethodData {
pub visibility: Visibility,
pub docs: String,
pub sig: Signature,
pub attributes: Vec<Attribute>,
}
/// Data for modules.
@ -271,6 +274,7 @@ pub struct ModData {
pub visibility: Visibility,
pub docs: String,
pub sig: Signature,
pub attributes: Vec<Attribute>,
}
/// Data for a reference to a module.
@ -295,6 +299,7 @@ pub struct StructData {
pub visibility: Visibility,
pub docs: String,
pub sig: Signature,
pub attributes: Vec<Attribute>,
}
#[derive(Debug, RustcEncodable)]
@ -309,6 +314,7 @@ pub struct StructVariantData {
pub parent: Option<DefId>,
pub docs: String,
pub sig: Signature,
pub attributes: Vec<Attribute>,
}
#[derive(Debug, RustcEncodable)]
@ -323,6 +329,7 @@ pub struct TraitData {
pub visibility: Visibility,
pub docs: String,
pub sig: Signature,
pub attributes: Vec<Attribute>,
}
#[derive(Debug, RustcEncodable)]
@ -337,6 +344,7 @@ pub struct TupleVariantData {
pub parent: Option<DefId>,
pub docs: String,
pub sig: Signature,
pub attributes: Vec<Attribute>,
}
/// Data for a typedef.
@ -351,6 +359,7 @@ pub struct TypeDefData {
pub parent: Option<DefId>,
pub docs: String,
pub sig: Option<Signature>,
pub attributes: Vec<Attribute>,
}
/// Data for a reference to a type or trait.
@ -396,6 +405,7 @@ pub struct VariableData {
pub visibility: Visibility,
pub docs: String,
pub sig: Option<Signature>,
pub attributes: Vec<Attribute>,
}
#[derive(Debug, RustcEncodable)]

View File

@ -47,7 +47,8 @@ use syntax::ptr::P;
use syntax::codemap::Spanned;
use syntax_pos::*;
use super::{escape, generated_code, SaveContext, PathCollector, docs_for_attrs};
use super::{escape, generated_code, SaveContext, PathCollector, docs_for_attrs,
remove_docs_from_attrs};
use super::data::*;
use super::dump::Dump;
use super::external_data::{Lower, make_def_id};
@ -373,6 +374,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
visibility: Visibility::Inherited,
docs: String::new(),
sig: None,
attributes: vec![],
}.lower(self.tcx));
}
}
@ -448,6 +450,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
visibility: vis,
docs: docs_for_attrs(attrs),
sig: method_data.sig,
attributes: remove_docs_from_attrs(attrs),
}.lower(self.tcx));
}
@ -519,6 +522,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
parent: None,
docs: String::new(),
sig: None,
attributes: vec![],
}.lower(self.tcx));
}
}
@ -592,6 +596,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
visibility: vis,
docs: docs_for_attrs(attrs),
sig: None,
attributes: remove_docs_from_attrs(attrs),
}.lower(self.tcx));
}
@ -636,6 +641,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
visibility: From::from(&item.vis),
docs: docs_for_attrs(&item.attrs),
sig: self.save_ctxt.sig_base(item),
attributes: remove_docs_from_attrs(&item.attrs),
}.lower(self.tcx));
}
@ -701,6 +707,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
parent: Some(make_def_id(item.id, &self.tcx.hir)),
docs: docs_for_attrs(&variant.node.attrs),
sig: sig,
attributes: remove_docs_from_attrs(&variant.node.attrs),
}.lower(self.tcx));
}
}
@ -727,6 +734,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
parent: Some(make_def_id(item.id, &self.tcx.hir)),
docs: docs_for_attrs(&variant.node.attrs),
sig: sig,
attributes: remove_docs_from_attrs(&variant.node.attrs),
}.lower(self.tcx));
}
}
@ -798,6 +806,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
visibility: From::from(&item.vis),
docs: docs_for_attrs(&item.attrs),
sig: self.save_ctxt.sig_base(item),
attributes: remove_docs_from_attrs(&item.attrs),
}.lower(self.tcx));
}
@ -1064,6 +1073,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
visibility: Visibility::Inherited,
docs: String::new(),
sig: None,
attributes: vec![],
}.lower(self.tcx));
}
}
@ -1305,6 +1315,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll,
parent: None,
docs: docs_for_attrs(&item.attrs),
sig: Some(self.save_ctxt.sig_base(item)),
attributes: remove_docs_from_attrs(&item.attrs),
}.lower(self.tcx));
}
@ -1527,6 +1538,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll,
visibility: Visibility::Inherited,
docs: String::new(),
sig: None,
attributes: vec![],
}.lower(self.tcx));
}
}

View File

@ -11,7 +11,7 @@
use rustc::hir::def_id::{CrateNum, DefId, DefIndex};
use rustc::hir::map::Map;
use rustc::ty::TyCtxt;
use syntax::ast::NodeId;
use syntax::ast::{self, LitKind, NodeId, StrStyle};
use syntax::codemap::CodeMap;
use syntax_pos::Span;
@ -64,6 +64,102 @@ impl SpanData {
}
}
/// Represent an arbitrary attribute on a code element
#[derive(Clone, Debug, RustcEncodable)]
pub struct Attribute {
value: AttributeItem,
span: SpanData,
}
impl Lower for ast::Attribute {
type Target = Attribute;
fn lower(self, tcx: TyCtxt) -> Attribute {
Attribute {
value: self.value.lower(tcx),
span: SpanData::from_span(self.span, tcx.sess.codemap()),
}
}
}
impl Lower for Vec<ast::Attribute> {
type Target = Vec<Attribute>;
fn lower(self, tcx: TyCtxt) -> Vec<Attribute> {
self.into_iter().map(|x| x.lower(tcx)).collect()
}
}
/// A single item as part of an attribute
#[derive(Clone, Debug, RustcEncodable)]
pub struct AttributeItem {
name: LitKind,
kind: AttributeItemKind,
span: SpanData,
}
impl Lower for ast::MetaItem {
type Target = AttributeItem;
fn lower(self, tcx: TyCtxt) -> AttributeItem {
AttributeItem {
name: LitKind::Str(self.name, StrStyle::Cooked),
kind: self.node.lower(tcx),
span: SpanData::from_span(self.span, tcx.sess.codemap()),
}
}
}
impl Lower for ast::NestedMetaItem {
type Target = AttributeItem;
fn lower(self, tcx: TyCtxt) -> AttributeItem {
match self.node {
ast::NestedMetaItemKind::MetaItem(item) => item.lower(tcx),
ast::NestedMetaItemKind::Literal(lit) => {
AttributeItem {
name: lit.node,
kind: AttributeItemKind::Literal,
span: SpanData::from_span(lit.span, tcx.sess.codemap()),
}
}
}
}
}
#[derive(Clone, Debug, RustcEncodable)]
pub enum AttributeItemKind {
/// Word meta item.
///
/// E.g. `test` as in `#[test]`
Literal,
/// Name value meta item.
///
/// E.g. `feature = "foo"` as in `#[feature = "foo"]`
NameValue(LitKind, SpanData),
/// List meta item.
///
/// E.g. the `derive(..)` as in `#[derive(..)]`
List(Vec<AttributeItem>),
}
impl Lower for ast::MetaItemKind {
type Target = AttributeItemKind;
fn lower(self, tcx: TyCtxt) -> AttributeItemKind {
match self {
ast::MetaItemKind::Word => AttributeItemKind::Literal,
ast::MetaItemKind::List(items) => {
AttributeItemKind::List(items.into_iter().map(|x| x.lower(tcx)).collect())
}
ast::MetaItemKind::NameValue(lit) => {
let span = SpanData::from_span(lit.span, tcx.sess.codemap());
AttributeItemKind::NameValue(lit.node, span)
}
}
}
}
#[derive(Debug, RustcEncodable)]
pub struct CratePreludeData {
pub crate_name: String,
@ -98,6 +194,7 @@ pub struct EnumData {
pub visibility: Visibility,
pub docs: String,
pub sig: Signature,
pub attributes: Vec<Attribute>,
}
impl Lower for data::EnumData {
@ -115,6 +212,7 @@ impl Lower for data::EnumData {
visibility: self.visibility,
docs: self.docs,
sig: self.sig.lower(tcx),
attributes: self.attributes.lower(tcx),
}
}
}
@ -179,6 +277,7 @@ pub struct FunctionData {
pub parent: Option<DefId>,
pub docs: String,
pub sig: Signature,
pub attributes: Vec<Attribute>,
}
impl Lower for data::FunctionData {
@ -197,6 +296,7 @@ impl Lower for data::FunctionData {
parent: self.parent,
docs: self.docs,
sig: self.sig.lower(tcx),
attributes: self.attributes.lower(tcx),
}
}
}
@ -346,6 +446,7 @@ pub struct MethodData {
pub parent: Option<DefId>,
pub docs: String,
pub sig: Signature,
pub attributes: Vec<Attribute>,
}
impl Lower for data::MethodData {
@ -364,6 +465,7 @@ impl Lower for data::MethodData {
parent: self.parent,
docs: self.docs,
sig: self.sig.lower(tcx),
attributes: self.attributes.lower(tcx),
}
}
}
@ -381,6 +483,7 @@ pub struct ModData {
pub visibility: Visibility,
pub docs: String,
pub sig: Signature,
pub attributes: Vec<Attribute>,
}
impl Lower for data::ModData {
@ -398,6 +501,7 @@ impl Lower for data::ModData {
visibility: self.visibility,
docs: self.docs,
sig: self.sig.lower(tcx),
attributes: self.attributes.lower(tcx),
}
}
}
@ -437,6 +541,7 @@ pub struct StructData {
pub visibility: Visibility,
pub docs: String,
pub sig: Signature,
pub attributes: Vec<Attribute>,
}
impl Lower for data::StructData {
@ -455,6 +560,7 @@ impl Lower for data::StructData {
visibility: self.visibility,
docs: self.docs,
sig: self.sig.lower(tcx),
attributes: self.attributes.lower(tcx),
}
}
}
@ -471,6 +577,7 @@ pub struct StructVariantData {
pub parent: Option<DefId>,
pub docs: String,
pub sig: Signature,
pub attributes: Vec<Attribute>,
}
impl Lower for data::StructVariantData {
@ -488,6 +595,7 @@ impl Lower for data::StructVariantData {
parent: self.parent,
docs: self.docs,
sig: self.sig.lower(tcx),
attributes: self.attributes.lower(tcx),
}
}
}
@ -504,6 +612,7 @@ pub struct TraitData {
pub visibility: Visibility,
pub docs: String,
pub sig: Signature,
pub attributes: Vec<Attribute>,
}
impl Lower for data::TraitData {
@ -521,6 +630,7 @@ impl Lower for data::TraitData {
visibility: self.visibility,
docs: self.docs,
sig: self.sig.lower(tcx),
attributes: self.attributes.lower(tcx),
}
}
}
@ -537,6 +647,7 @@ pub struct TupleVariantData {
pub parent: Option<DefId>,
pub docs: String,
pub sig: Signature,
pub attributes: Vec<Attribute>,
}
impl Lower for data::TupleVariantData {
@ -554,6 +665,7 @@ impl Lower for data::TupleVariantData {
parent: self.parent,
docs: self.docs,
sig: self.sig.lower(tcx),
attributes: self.attributes.lower(tcx),
}
}
}
@ -570,6 +682,7 @@ pub struct TypeDefData {
pub parent: Option<DefId>,
pub docs: String,
pub sig: Option<Signature>,
pub attributes: Vec<Attribute>,
}
impl Lower for data::TypeDefData {
@ -586,6 +699,7 @@ impl Lower for data::TypeDefData {
parent: self.parent,
docs: self.docs,
sig: self.sig.map(|s| s.lower(tcx)),
attributes: self.attributes.lower(tcx),
}
}
}
@ -675,6 +789,7 @@ pub struct VariableData {
pub visibility: Visibility,
pub docs: String,
pub sig: Option<Signature>,
pub attributes: Vec<Attribute>,
}
impl Lower for data::VariableData {
@ -694,6 +809,7 @@ impl Lower for data::VariableData {
visibility: self.visibility,
docs: self.docs,
sig: self.sig.map(|s| s.lower(tcx)),
attributes: self.attributes.lower(tcx),
}
}
}

View File

@ -87,6 +87,7 @@ impl<'b, W: Write + 'b> Dump for JsonDumper<'b, W> {
decl_id: None,
docs: data.docs,
sig: Some(From::from(data.sig)),
attributes: data.attributes,
};
if def.span.file_name != def.value {
// If the module is an out-of-line defintion, then we'll make the
@ -232,6 +233,7 @@ struct Def {
decl_id: Option<Id>,
docs: String,
sig: Option<JsonSignature>,
attributes: Vec<Attribute>,
}
#[derive(Debug, RustcEncodable)]
@ -274,6 +276,7 @@ impl From<EnumData> for Def {
decl_id: None,
docs: data.docs,
sig: Some(From::from(data.sig)),
attributes: data.attributes,
}
}
}
@ -291,6 +294,7 @@ impl From<TupleVariantData> for Def {
decl_id: None,
docs: data.docs,
sig: Some(From::from(data.sig)),
attributes: data.attributes,
}
}
}
@ -307,6 +311,7 @@ impl From<StructVariantData> for Def {
decl_id: None,
docs: data.docs,
sig: Some(From::from(data.sig)),
attributes: data.attributes,
}
}
}
@ -323,6 +328,7 @@ impl From<StructData> for Def {
decl_id: None,
docs: data.docs,
sig: Some(From::from(data.sig)),
attributes: data.attributes,
}
}
}
@ -339,6 +345,7 @@ impl From<TraitData> for Def {
decl_id: None,
docs: data.docs,
sig: Some(From::from(data.sig)),
attributes: data.attributes,
}
}
}
@ -355,6 +362,7 @@ impl From<FunctionData> for Def {
decl_id: None,
docs: data.docs,
sig: Some(From::from(data.sig)),
attributes: data.attributes,
}
}
}
@ -371,6 +379,7 @@ impl From<MethodData> for Def {
decl_id: data.decl_id.map(|id| From::from(id)),
docs: data.docs,
sig: Some(From::from(data.sig)),
attributes: data.attributes,
}
}
}
@ -387,6 +396,7 @@ impl From<MacroData> for Def {
decl_id: None,
docs: data.docs,
sig: None,
attributes: vec![],
}
}
}
@ -403,6 +413,7 @@ impl From<TypeDefData> for Def {
decl_id: None,
docs: String::new(),
sig: data.sig.map(|s| From::from(s)),
attributes: data.attributes,
}
}
}
@ -424,6 +435,7 @@ impl From<VariableData> for Def {
decl_id: None,
docs: data.docs,
sig: None,
attributes: data.attributes,
}
}
}

View File

@ -136,6 +136,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
parent: None,
docs: docs_for_attrs(&item.attrs),
sig: self.sig_base(item),
attributes: remove_docs_from_attrs(&item.attrs),
}))
}
ast::ItemKind::Static(ref typ, mt, ref expr) => {
@ -164,6 +165,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
visibility: From::from(&item.vis),
docs: docs_for_attrs(&item.attrs),
sig: Some(self.sig_base(item)),
attributes: remove_docs_from_attrs(&item.attrs),
}))
}
ast::ItemKind::Const(ref typ, ref expr) => {
@ -183,6 +185,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
visibility: From::from(&item.vis),
docs: docs_for_attrs(&item.attrs),
sig: Some(self.sig_base(item)),
attributes: remove_docs_from_attrs(&item.attrs),
}))
}
ast::ItemKind::Mod(ref m) => {
@ -205,6 +208,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
visibility: From::from(&item.vis),
docs: docs_for_attrs(&item.attrs),
sig: self.sig_base(item),
attributes: remove_docs_from_attrs(&item.attrs),
}))
}
ast::ItemKind::Enum(ref def, _) => {
@ -228,6 +232,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
visibility: From::from(&item.vis),
docs: docs_for_attrs(&item.attrs),
sig: self.sig_base(item),
attributes: remove_docs_from_attrs(&item.attrs),
}))
}
ast::ItemKind::Impl(.., ref trait_ref, ref typ, _) => {
@ -315,6 +320,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
visibility: From::from(&field.vis),
docs: docs_for_attrs(&field.attrs),
sig: Some(sig),
attributes: remove_docs_from_attrs(&field.attrs),
})
} else {
None
@ -327,7 +333,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
name: ast::Name, span: Span) -> Option<FunctionData> {
// 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, parent_scope, decl_id, vis, docs) =
let (qualname, parent_scope, decl_id, vis, docs, attributes) =
match self.tcx.impl_of_method(self.tcx.hir.local_def_id(id)) {
Some(impl_id) => match self.tcx.hir.get_if_local(impl_id) {
Some(Node::NodeItem(item)) => {
@ -349,7 +355,8 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
(result, trait_id, decl_id,
From::from(&item.vis),
docs_for_attrs(&item.attrs))
docs_for_attrs(&item.attrs),
remove_docs_from_attrs(&item.attrs))
}
_ => {
span_bug!(span,
@ -374,7 +381,8 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
(format!("::{}", self.tcx.item_path_str(def_id)),
Some(def_id), None,
From::from(&item.vis),
docs_for_attrs(&item.attrs))
docs_for_attrs(&item.attrs),
remove_docs_from_attrs(&item.attrs))
}
r => {
span_bug!(span,
@ -423,6 +431,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
parent: parent_scope,
docs: docs,
sig: sig,
attributes: attributes,
})
}
@ -836,6 +845,12 @@ fn docs_for_attrs(attrs: &[Attribute]) -> String {
result
}
/// Remove all attributes which are docs
fn remove_docs_from_attrs(attrs: &[Attribute]) -> Vec<Attribute> {
let doc = Symbol::intern("doc");
attrs.iter().cloned().filter(|attr| attr.name() != doc).collect()
}
#[derive(Clone, Copy, Debug, RustcEncodable)]
pub enum Format {
Csv,