From 203d22762ddb215af583108d79a521a8441693c2 Mon Sep 17 00:00:00 2001 From: Jonas Bushart Date: Thu, 23 Feb 2017 23:24:12 +0100 Subject: [PATCH] Store attributes as strings Remove the AST structure --- src/librustc_save_analysis/external_data.rs | 85 +++------------------ 1 file changed, 11 insertions(+), 74 deletions(-) diff --git a/src/librustc_save_analysis/external_data.rs b/src/librustc_save_analysis/external_data.rs index 0cfa71a3499..38d1df2abb8 100644 --- a/src/librustc_save_analysis/external_data.rs +++ b/src/librustc_save_analysis/external_data.rs @@ -11,8 +11,9 @@ use rustc::hir::def_id::{CrateNum, DefId, DefIndex}; use rustc::hir::map::Map; use rustc::ty::TyCtxt; -use syntax::ast::{self, LitKind, NodeId, StrStyle}; +use syntax::ast::{self, NodeId}; use syntax::codemap::CodeMap; +use syntax::print::pprust; use syntax_pos::Span; use data::{self, Visibility, SigElement}; @@ -67,16 +68,22 @@ impl SpanData { /// Represent an arbitrary attribute on a code element #[derive(Clone, Debug, RustcEncodable)] pub struct Attribute { - value: AttributeItem, + value: String, span: SpanData, } impl Lower for ast::Attribute { type Target = Attribute; - fn lower(self, tcx: TyCtxt) -> Attribute { + fn lower(mut self, tcx: TyCtxt) -> Attribute { + // strip #[] and #![] from the original attributes + self.style = ast::AttrStyle::Outer; + let value = pprust::attribute_to_string(&self); + // #[] are all ASCII which makes this slice save + let value = value[2..value.len()-1].to_string(); + Attribute { - value: self.value.lower(tcx), + value: value, span: SpanData::from_span(self.span, tcx.sess.codemap()), } } @@ -90,76 +97,6 @@ impl Lower for Vec { } } -/// 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), -} - -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,