Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

save-analysis: JSON mk2 #33568

Merged
merged 3 commits into from
May 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
92 changes: 71 additions & 21 deletions src/librustc_save_analysis/dump_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use syntax::ast::{self, NodeId, PatKind};
use syntax::codemap::*;
use syntax::parse::token::{self, keywords};
use syntax::visit::{self, Visitor};
use syntax::print::pprust::{path_to_string, ty_to_string};
use syntax::print::pprust::{path_to_string, ty_to_string, bounds_to_string, generics_to_string};
use syntax::ptr::P;

use super::{escape, generated_code, SaveContext, PathCollector};
Expand Down Expand Up @@ -388,18 +388,23 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {

if let Some(method_data) = self.save_ctxt.get_method_data(id, name, span) {

let sig_str = ::make_signature(&sig.decl, &sig.generics);
if body.is_some() {
if !self.span.filter_generated(Some(method_data.span), span) {
self.dumper.function(method_data.clone().lower(self.tcx));
let mut data = method_data.clone();
data.value = sig_str;
self.dumper.function(data.lower(self.tcx));
}
self.process_formals(&sig.decl.inputs, &method_data.qualname);
} else {
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: sig_str,
}.lower(self.tcx));
}
}
Expand Down Expand Up @@ -455,16 +460,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 @@ -536,18 +543,33 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
self.visit_expr(expr);
}

// FIXME tuple structs should generate tuple-specific data.
fn process_struct(&mut self,
item: &ast::Item,
def: &ast::VariantData,
ty_params: &ast::Generics) {
let name = item.ident.to_string();
let qualname = format!("::{}", self.tcx.node_path_str(item.id));

let val = self.span.snippet(item.span);
let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Struct);
let val = if let ast::ItemKind::Struct(ast::VariantData::Struct(ref fields, _), _) =
item.node {
let fields_str = fields.iter()
.enumerate()
.map(|(i, f)| f.ident.map(|i| i.to_string())
.unwrap_or(i.to_string()))
.collect::<Vec<_>>()
.join(", ");
format!("{} {{ {} }}", name, fields_str)
} else {
String::new()
};

if !self.span.filter_generated(sub_span, item.span) {
self.dumper.struct_data(StructData {
span: sub_span.expect("No span found for struct"),
id: item.id,
name: name,
ctor_id: def.id(),
qualname: qualname.clone(),
scope: self.cur_scope,
Expand Down Expand Up @@ -580,33 +602,49 @@ 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);
let val = self.span.snippet(variant.span);
qualname.push_str(&name);

match variant.node.data {
ast::VariantData::Struct(..) => {
ast::VariantData::Struct(ref fields, _) => {
let sub_span = self.span.span_for_first_ident(variant.span);
let fields_str = fields.iter()
.enumerate()
.map(|(i, f)| f.ident.map(|i| i.to_string())
.unwrap_or(i.to_string()))
.collect::<Vec<_>>()
.join(", ");
let val = format!("{}::{} {{ {} }}", enum_data.name, name, fields_str);
if !self.span.filter_generated(sub_span, variant.span) {
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,
scope: enum_data.scope
}.lower(self.tcx));
}
}
_ => {
ref v => {
let sub_span = self.span.span_for_first_ident(variant.span);
let mut val = format!("{}::{}", enum_data.name, name);
if let &ast::VariantData::Tuple(ref fields, _) = v {
val.push('(');
val.push_str(&fields.iter()
.map(|f| ty_to_string(&f.ty))
.collect::<Vec<_>>()
.join(", "));
val.push(')');
}
if !self.span.filter_generated(sub_span, variant.span) {
self.dumper.tuple_variant(TupleVariantData {
span: sub_span.expect("No span found for tuple variant"),
id: variant.node.data.id(),
name: name.to_string(),
name: name,
qualname: qualname,
type_value: enum_data.qualname.clone(),
value: val,
Expand Down Expand Up @@ -672,13 +710,22 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
generics: &ast::Generics,
trait_refs: &ast::TyParamBounds,
methods: &[ast::TraitItem]) {
let name = item.ident.to_string();
let qualname = format!("::{}", self.tcx.node_path_str(item.id));
let val = self.span.snippet(item.span);
let mut val = name.clone();
if !generics.lifetimes.is_empty() || !generics.ty_params.is_empty() {
val.push_str(&generics_to_string(generics));
}
if !trait_refs.is_empty() {
val.push_str(": ");
val.push_str(&bounds_to_string(trait_refs));
}
let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Trait);
if !self.span.filter_generated(sub_span, item.span) {
self.dumper.trait_data(TraitData {
span: sub_span.expect("No span found for trait"),
id: item.id,
name: name,
qualname: qualname.clone(),
scope: self.cur_scope,
value: val
Expand Down Expand Up @@ -908,13 +955,15 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
self.visit_pat(&p);

for &(id, ref p, immut, _) in &collector.collected_paths {
let value = if immut == ast::Mutability::Immutable {
let mut value = if immut == ast::Mutability::Immutable {
value.to_string()
} else {
"<mutable>".to_string()
};
let types = self.tcx.node_types();
let typ = types.get(&id).map(|t| t.to_string()).unwrap_or(String::new());
value.push_str(": ");
value.push_str(&typ);
// Get the span only for the name of the variable (I hope the path
// is only ever a variable name, but who knows?).
let sub_span = self.span.span_for_last_ident(p.span);
Expand Down Expand Up @@ -1107,8 +1156,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 Expand Up @@ -1275,13 +1325,13 @@ impl<'v, 'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'v> for DumpVisitor<'l, 'tcx,
}
ast::ExprKind::ForLoop(ref pattern, ref subexpression, ref block, _) |
ast::ExprKind::WhileLet(ref pattern, ref subexpression, ref block, _) => {
let value = self.span.snippet(mk_sp(ex.span.lo, subexpression.span.hi));
let value = self.span.snippet(subexpression.span);
self.process_var_decl(pattern, value);
visit::walk_expr(self, subexpression);
visit::walk_block(self, block);
}
ast::ExprKind::IfLet(ref pattern, ref subexpression, ref block, ref opt_else) => {
let value = self.span.snippet(mk_sp(ex.span.lo, subexpression.span.hi));
let value = self.span.snippet(subexpression.span);
self.process_var_decl(pattern, value);
visit::walk_expr(self, subexpression);
visit::walk_block(self, block);
Expand Down Expand Up @@ -1371,7 +1421,7 @@ impl<'v, 'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'v> for DumpVisitor<'l, 'tcx,

fn visit_local(&mut self, l: &ast::Local) {
self.process_macro_use(l.span, l.id);
let value = self.span.snippet(l.span);
let value = l.init.as_ref().map(|i| self.span.snippet(i.span)).unwrap_or(String::new());
self.process_var_decl(&l.pat, value);

// Just walk the initialiser and type (don't want to walk the pattern again).
Expand Down
Loading