Skip to content

Commit

Permalink
save-analysis: attempt number 2 at a JSON version
Browse files Browse the repository at this point in the history
  • Loading branch information
nrc committed May 11, 2016
1 parent e37f859 commit a0a9877
Show file tree
Hide file tree
Showing 7 changed files with 423 additions and 61 deletions.
2 changes: 1 addition & 1 deletion src/librustc_save_analysis/csv_dumper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ impl<'b, W: Write + 'b> Dump for CsvDumper<'b, W> {
self.record("type_ref", data.span, values);
}

fn typedef(&mut self, data: TypedefData) {
fn typedef(&mut self, data: TypeDefData) {
let id = data.id.index.as_u32().to_string();
let values = make_values_str(&[
("id", &id),
Expand Down
12 changes: 10 additions & 2 deletions src/librustc_save_analysis/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub enum Data {
/// Data for a tuple variant.
TupleVariantData(TupleVariantData),
/// Data for a typedef.
TypeDefData(TypedefData),
TypeDefData(TypeDefData),
/// Data for a reference to a type or trait.
TypeRefData(TypeRefData),
/// Data for a use statement.
Expand Down Expand Up @@ -97,6 +97,7 @@ pub struct ExternalCrateData {
#[derive(Clone, Debug, RustcEncodable)]
pub struct EnumData {
pub id: NodeId,
pub name: String,
pub value: String,
pub qualname: String,
pub span: Span,
Expand Down Expand Up @@ -131,6 +132,7 @@ pub struct FunctionData {
pub declaration: Option<DefId>,
pub span: Span,
pub scope: NodeId,
pub value: String,
}

/// Data about a function call.
Expand Down Expand Up @@ -205,9 +207,11 @@ pub struct MethodCallData {
#[derive(Clone, Debug, RustcEncodable)]
pub struct MethodData {
pub id: NodeId,
pub name: String,
pub qualname: String,
pub span: Span,
pub scope: NodeId,
pub value: String,
}

/// Data for modules.
Expand All @@ -233,6 +237,7 @@ pub struct ModRefData {
#[derive(Debug, RustcEncodable)]
pub struct StructData {
pub span: Span,
pub name: String,
pub id: NodeId,
pub ctor_id: NodeId,
pub qualname: String,
Expand All @@ -243,6 +248,7 @@ pub struct StructData {
#[derive(Debug, RustcEncodable)]
pub struct StructVariantData {
pub span: Span,
pub name: String,
pub id: NodeId,
pub qualname: String,
pub type_value: String,
Expand All @@ -254,6 +260,7 @@ pub struct StructVariantData {
pub struct TraitData {
pub span: Span,
pub id: NodeId,
pub name: String,
pub qualname: String,
pub scope: NodeId,
pub value: String
Expand All @@ -272,8 +279,9 @@ pub struct TupleVariantData {

/// Data for a typedef.
#[derive(Debug, RustcEncodable)]
pub struct TypedefData {
pub struct TypeDefData {
pub id: NodeId,
pub name: String,
pub span: Span,
pub qualname: String,
pub value: String,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_save_analysis/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub trait Dump {
fn trait_data(&mut self, TraitData) {}
fn tuple_variant(&mut self, TupleVariantData) {}
fn type_ref(&mut self, TypeRefData) {}
fn typedef(&mut self, TypedefData) {}
fn typedef(&mut self, TypeDefData) {}
fn use_data(&mut self, UseData) {}
fn use_glob(&mut self, UseGlobData) {}
fn variable(&mut self, VariableData) {}
Expand Down
26 changes: 17 additions & 9 deletions src/librustc_save_analysis/dump_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,11 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
if !self.span.filter_generated(Some(method_data.span), span) {
self.dumper.method(MethodData {
id: method_data.id,
name: method_data.name,
span: method_data.span,
scope: method_data.scope,
qualname: method_data.qualname.clone(),
value: String::new(), // TODO
}.lower(self.tcx));
}
}
Expand Down Expand Up @@ -455,16 +457,18 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
let param_sub_spans = self.span.spans_for_ty_params(full_span,
(generics.ty_params.len() as isize));
for (param, param_ss) in generics.ty_params.iter().zip(param_sub_spans) {
let name = escape(self.span.snippet(param_ss));
// Append $id to name to make sure each one is unique
let name = format!("{}::{}${}",
prefix,
escape(self.span.snippet(param_ss)),
id);
let qualname = format!("{}::{}${}",
prefix,
name,
id);
if !self.span.filter_generated(Some(param_ss), full_span) {
self.dumper.typedef(TypedefData {
self.dumper.typedef(TypeDefData {
span: param_ss,
name: name,
id: param.id,
qualname: name,
qualname: qualname,
value: String::new()
}.lower(self.tcx));
}
Expand Down Expand Up @@ -548,6 +552,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
self.dumper.struct_data(StructData {
span: sub_span.expect("No span found for struct"),
id: item.id,
name: item.ident.to_string(),
ctor_id: def.id(),
qualname: qualname.clone(),
scope: self.cur_scope,
Expand Down Expand Up @@ -580,10 +585,10 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
}

for variant in &enum_definition.variants {
let name = &variant.node.name.name.as_str();
let name = variant.node.name.name.to_string();
let mut qualname = enum_data.qualname.clone();
qualname.push_str("::");
qualname.push_str(name);
qualname.push_str(&name);
let val = self.span.snippet(variant.span);

match variant.node.data {
Expand All @@ -593,6 +598,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
self.dumper.struct_variant(StructVariantData {
span: sub_span.expect("No span found for struct variant"),
id: variant.node.data.id(),
name: name,
qualname: qualname,
type_value: enum_data.qualname.clone(),
value: val,
Expand Down Expand Up @@ -679,6 +685,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
self.dumper.trait_data(TraitData {
span: sub_span.expect("No span found for trait"),
id: item.id,
name: item.ident.to_string(),
qualname: qualname.clone(),
scope: self.cur_scope,
value: val
Expand Down Expand Up @@ -1107,8 +1114,9 @@ impl<'v, 'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'v> for DumpVisitor<'l, 'tcx,
let value = ty_to_string(&ty);
let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Type);
if !self.span.filter_generated(sub_span, item.span) {
self.dumper.typedef(TypedefData {
self.dumper.typedef(TypeDefData {
span: sub_span.expect("No span found for typedef"),
name: item.ident.to_string(),
id: item.id,
qualname: qualname.clone(),
value: value
Expand Down
28 changes: 21 additions & 7 deletions src/librustc_save_analysis/external_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ impl Lower for data::CratePreludeData {
pub struct EnumData {
pub id: DefId,
pub value: String,
pub name: String,
pub qualname: String,
pub span: SpanData,
pub scope: DefId,
Expand All @@ -97,6 +98,7 @@ impl Lower for data::EnumData {
fn lower(self, tcx: TyCtxt) -> EnumData {
EnumData {
id: make_def_id(self.id, &tcx.map),
name: self.name,
value: self.value,
qualname: self.qualname,
span: SpanData::from_span(self.span, tcx.sess.codemap()),
Expand Down Expand Up @@ -160,6 +162,7 @@ pub struct FunctionData {
pub declaration: Option<DefId>,
pub span: SpanData,
pub scope: DefId,
pub value: String,
}

impl Lower for data::FunctionData {
Expand All @@ -173,6 +176,7 @@ impl Lower for data::FunctionData {
declaration: self.declaration,
span: SpanData::from_span(self.span, tcx.sess.codemap()),
scope: make_def_id(self.scope, &tcx.map),
value: self.value,
}
}
}
Expand Down Expand Up @@ -268,7 +272,6 @@ pub struct MacroUseData {
// we use the callee span to reference the associated macro definition.
pub callee_span: SpanData,
pub scope: DefId,
pub imported: bool,
}

impl Lower for data::MacroUseData {
Expand All @@ -281,7 +284,6 @@ impl Lower for data::MacroUseData {
qualname: self.qualname,
callee_span: SpanData::from_span(self.callee_span, tcx.sess.codemap()),
scope: make_def_id(self.scope, &tcx.map),
imported: self.imported,
}
}
}
Expand Down Expand Up @@ -312,9 +314,11 @@ impl Lower for data::MethodCallData {
#[derive(Clone, Debug, RustcEncodable)]
pub struct MethodData {
pub id: DefId,
pub name: String,
pub qualname: String,
pub span: SpanData,
pub scope: DefId,
pub value: String,
}

impl Lower for data::MethodData {
Expand All @@ -323,9 +327,11 @@ impl Lower for data::MethodData {
fn lower(self, tcx: TyCtxt) -> MethodData {
MethodData {
span: SpanData::from_span(self.span, tcx.sess.codemap()),
name: self.name,
scope: make_def_id(self.scope, &tcx.map),
id: make_def_id(self.id, &tcx.map),
qualname: self.qualname,
value: self.value,
}
}
}
Expand Down Expand Up @@ -381,6 +387,7 @@ impl Lower for data::ModRefData {
#[derive(Debug, RustcEncodable)]
pub struct StructData {
pub span: SpanData,
pub name: String,
pub id: DefId,
pub ctor_id: DefId,
pub qualname: String,
Expand All @@ -394,6 +401,7 @@ impl Lower for data::StructData {
fn lower(self, tcx: TyCtxt) -> StructData {
StructData {
span: SpanData::from_span(self.span, tcx.sess.codemap()),
name: self.name,
id: make_def_id(self.id, &tcx.map),
ctor_id: make_def_id(self.ctor_id, &tcx.map),
qualname: self.qualname,
Expand All @@ -406,6 +414,7 @@ impl Lower for data::StructData {
#[derive(Debug, RustcEncodable)]
pub struct StructVariantData {
pub span: SpanData,
pub name: String,
pub id: DefId,
pub qualname: String,
pub type_value: String,
Expand All @@ -419,6 +428,7 @@ impl Lower for data::StructVariantData {
fn lower(self, tcx: TyCtxt) -> StructVariantData {
StructVariantData {
span: SpanData::from_span(self.span, tcx.sess.codemap()),
name: self.name,
id: make_def_id(self.id, &tcx.map),
qualname: self.qualname,
type_value: self.type_value,
Expand All @@ -431,6 +441,7 @@ impl Lower for data::StructVariantData {
#[derive(Debug, RustcEncodable)]
pub struct TraitData {
pub span: SpanData,
pub name: String,
pub id: DefId,
pub qualname: String,
pub scope: DefId,
Expand All @@ -443,6 +454,7 @@ impl Lower for data::TraitData {
fn lower(self, tcx: TyCtxt) -> TraitData {
TraitData {
span: SpanData::from_span(self.span, tcx.sess.codemap()),
name: self.name,
id: make_def_id(self.id, &tcx.map),
qualname: self.qualname,
scope: make_def_id(self.scope, &tcx.map),
Expand Down Expand Up @@ -480,19 +492,21 @@ impl Lower for data::TupleVariantData {

/// Data for a typedef.
#[derive(Debug, RustcEncodable)]
pub struct TypedefData {
pub struct TypeDefData {
pub id: DefId,
pub name: String,
pub span: SpanData,
pub qualname: String,
pub value: String,
}

impl Lower for data::TypedefData {
type Target = TypedefData;
impl Lower for data::TypeDefData {
type Target = TypeDefData;

fn lower(self, tcx: TyCtxt) -> TypedefData {
TypedefData {
fn lower(self, tcx: TyCtxt) -> TypeDefData {
TypeDefData {
id: make_def_id(self.id, &tcx.map),
name: self.name,
span: SpanData::from_span(self.span, tcx.sess.codemap()),
qualname: self.qualname,
value: self.value,
Expand Down
Loading

0 comments on commit a0a9877

Please sign in to comment.