1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Output a CSV file containing the output from rustc's analysis. The data is
12 //! primarily designed to be used as input to the DXR tool, specifically its
13 //! Rust plugin. It could also be used by IDEs or other code browsing, search, or
14 //! cross-referencing tools.
16 //! Dumping the analysis is implemented by walking the AST and getting a bunch of
17 //! info out from all over the place. We use Def IDs to identify objects. The
18 //! tricky part is getting syntactic (span, source text) and semantic (reference
19 //! Def IDs) information for parts of expressions which the compiler has discarded.
20 //! E.g., in a path `foo::bar::baz`, the compiler only keeps a span for the whole
21 //! path and a reference to `baz`, but we want spans and references for all three
24 //! SpanUtils is used to manipulate spans. In particular, to extract sub-spans
25 //! from spans (e.g., the span for `bar` from the above example path).
26 //! Recorder is used for recording the output in csv format. FmtStrs separates
27 //! the format of the output away from extracting it from the compiler.
28 //! DxrVisitor walks the AST and processes it.
33 use middle::ty::{self, Ty};
36 use std::old_io::{self, File, fs};
39 use syntax::ast_util::{self, PostExpansionMethod};
40 use syntax::ast::{self, NodeId, DefId};
41 use syntax::ast_map::NodeItem;
43 use syntax::codemap::*;
44 use syntax::parse::token::{self, get_ident, keywords};
45 use syntax::owned_slice::OwnedSlice;
46 use syntax::visit::{self, Visitor};
47 use syntax::print::pprust::{path_to_string, ty_to_string};
50 use self::span_utils::SpanUtils;
51 use self::recorder::{Recorder, FmtStrs};
58 // Helper function to escape quotes in a string
59 fn escape(s: String) -> String {
60 s.replace("\"", "\"\"")
63 // If the expression is a macro expansion or other generated code, run screaming and don't index.
64 fn generated_code(span: Span) -> bool {
65 span.expn_id != NO_EXPANSION || span == DUMMY_SP
68 struct DxrVisitor<'l, 'tcx: 'l> {
70 analysis: &'l ty::CrateAnalysis<'tcx>,
72 collected_paths: Vec<(NodeId, ast::Path, bool, recorder::Row)>,
81 impl <'l, 'tcx> DxrVisitor<'l, 'tcx> {
82 fn nest<F>(&mut self, scope_id: NodeId, f: F) where
83 F: FnOnce(&mut DxrVisitor<'l, 'tcx>),
85 let parent_scope = self.cur_scope;
86 self.cur_scope = scope_id;
88 self.cur_scope = parent_scope;
91 fn dump_crate_info(&mut self, name: &str, krate: &ast::Crate) {
93 self.fmt.crate_str(krate.span, name);
95 // dump info about all the external crates referenced from this crate
96 self.sess.cstore.iter_crate_data(|n, cmd| {
97 self.fmt.external_crate_str(krate.span, &cmd.name[], n);
99 self.fmt.recorder.record("end_external_crates\n");
102 // Return all non-empty prefixes of a path.
103 // For each prefix, we return the span for the last segment in the prefix and
104 // a str representation of the entire prefix.
105 fn process_path_prefixes(&self, path: &ast::Path) -> Vec<(Span, String)> {
106 let spans = self.span.spans_for_path_segments(path);
108 // Paths to enums seem to not match their spans - the span includes all the
109 // variants too. But they seem to always be at the end, so I hope we can cope with
110 // always using the first ones. So, only error out if we don't have enough spans.
111 // What could go wrong...?
112 if spans.len() < path.segments.len() {
113 error!("Mis-calculated spans for path '{}'. \
114 Found {} spans, expected {}. Found spans:",
115 path_to_string(path), spans.len(), path.segments.len());
117 let loc = self.sess.codemap().lookup_char_pos(s.lo);
118 error!(" '{}' in {}, line {}",
119 self.span.snippet(*s), loc.file.name, loc.line);
124 let mut result: Vec<(Span, String)> = vec!();
126 let mut segs = vec!();
127 for (i, (seg, span)) in path.segments.iter().zip(spans.iter()).enumerate() {
128 segs.push(seg.clone());
129 let sub_path = ast::Path{span: *span, // span for the last segment
132 let qualname = if i == 0 && path.global {
133 format!("::{}", path_to_string(&sub_path))
135 path_to_string(&sub_path)
137 result.push((*span, qualname));
138 segs = sub_path.segments;
144 // The global arg allows us to override the global-ness of the path (which
145 // actually means 'does the path start with `::`', rather than 'is the path
146 // semantically global). We use the override for `use` imports (etc.) where
147 // the syntax is non-global, but the semantics are global.
148 fn write_sub_paths(&mut self, path: &ast::Path, global: bool) {
149 let sub_paths = self.process_path_prefixes(path);
150 for (i, &(ref span, ref qualname)) in sub_paths.iter().enumerate() {
151 let qualname = if i == 0 && global && !path.global {
152 format!("::{}", qualname)
156 self.fmt.sub_mod_ref_str(path.span,
163 // As write_sub_paths, but does not process the last ident in the path (assuming it
164 // will be processed elsewhere). See note on write_sub_paths about global.
165 fn write_sub_paths_truncated(&mut self, path: &ast::Path, global: bool) {
166 let sub_paths = self.process_path_prefixes(path);
167 let len = sub_paths.len();
172 let sub_paths = &sub_paths[..len-1];
173 for (i, &(ref span, ref qualname)) in sub_paths.iter().enumerate() {
174 let qualname = if i == 0 && global && !path.global {
175 format!("::{}", qualname)
179 self.fmt.sub_mod_ref_str(path.span,
186 // As write_sub_paths, but expects a path of the form module_path::trait::method
187 // Where trait could actually be a struct too.
188 fn write_sub_path_trait_truncated(&mut self, path: &ast::Path) {
189 let sub_paths = self.process_path_prefixes(path);
190 let len = sub_paths.len();
194 let sub_paths = &sub_paths[.. (len-1)];
196 // write the trait part of the sub-path
197 let (ref span, ref qualname) = sub_paths[len-2];
198 self.fmt.sub_type_ref_str(path.span,
202 // write the other sub-paths
206 let sub_paths = &sub_paths[..len-2];
207 for &(ref span, ref qualname) in sub_paths {
208 self.fmt.sub_mod_ref_str(path.span,
215 // looks up anything, not just a type
216 fn lookup_type_ref(&self, ref_id: NodeId) -> Option<DefId> {
217 if !self.analysis.ty_cx.def_map.borrow().contains_key(&ref_id) {
218 self.sess.bug(&format!("def_map has no key for {} in lookup_type_ref",
221 let def = (*self.analysis.ty_cx.def_map.borrow())[ref_id];
223 def::DefPrimTy(_) => None,
224 _ => Some(def.def_id()),
228 fn lookup_def_kind(&self, ref_id: NodeId, span: Span) -> Option<recorder::Row> {
229 let def_map = self.analysis.ty_cx.def_map.borrow();
230 if !def_map.contains_key(&ref_id) {
231 self.sess.span_bug(span, &format!("def_map has no key for {} in lookup_def_kind",
234 let def = (*def_map)[ref_id];
237 def::DefForeignMod(_) => Some(recorder::ModRef),
238 def::DefStruct(_) => Some(recorder::StructRef),
240 def::DefAssociatedTy(..) |
241 def::DefAssociatedPath(..) |
242 def::DefTrait(_) => Some(recorder::TypeRef),
243 def::DefStatic(_, _) |
246 def::DefVariant(_, _, _) |
247 def::DefUpvar(..) => Some(recorder::VarRef),
249 def::DefFn(..) => Some(recorder::FnRef),
253 def::DefTyParamBinder(_) |
255 def::DefStaticMethod(..) |
256 def::DefTyParam(..) |
259 def::DefPrimTy(_) => {
260 self.sess.span_bug(span, &format!("lookup_def_kind for unexpected item: {:?}",
266 fn process_formals(&mut self, formals: &Vec<ast::Arg>, qualname: &str) {
268 assert!(self.collected_paths.len() == 0 && !self.collecting);
269 self.collecting = true;
270 self.visit_pat(&*arg.pat);
271 self.collecting = false;
272 let span_utils = self.span.clone();
273 for &(id, ref p, _, _) in &self.collected_paths {
274 let typ = ppaux::ty_to_string(&self.analysis.ty_cx,
275 (*self.analysis.ty_cx.node_types.borrow())[id]);
276 // get the span only for the name of the variable (I hope the path is only ever a
277 // variable name, but who knows?)
278 self.fmt.formal_str(p.span,
279 span_utils.span_for_last_ident(p.span),
282 &path_to_string(p)[],
285 self.collected_paths.clear();
289 fn process_method(&mut self, method: &ast::Method) {
290 if generated_code(method.span) {
295 // The qualname for a method is the trait name or name of the struct in an impl in
296 // which the method is declared in, followed by the method's name.
297 let qualname = match ty::impl_of_method(&self.analysis.ty_cx,
298 ast_util::local_def(method.id)) {
299 Some(impl_id) => match self.analysis.ty_cx.map.get(impl_id.node) {
303 ast::ItemImpl(_, _, _, _, ref ty, _) => {
304 let mut result = String::from_str("<");
305 result.push_str(&ty_to_string(&**ty)[]);
307 match ty::trait_of_item(&self.analysis.ty_cx,
308 ast_util::local_def(method.id)) {
310 result.push_str(" as ");
312 ty::item_path_str(&self.analysis.ty_cx, def_id).as_slice());
316 result.push_str(">");
320 self.sess.span_bug(method.span,
321 &format!("Container {} for method {} not an impl?",
322 impl_id.node, method.id)[]);
327 self.sess.span_bug(method.span,
329 "Container {} for method {} is not a node item {:?}",
332 self.analysis.ty_cx.map.get(impl_id.node))[]);
335 None => match ty::trait_of_item(&self.analysis.ty_cx,
336 ast_util::local_def(method.id)) {
338 scope_id = def_id.node;
339 match self.analysis.ty_cx.map.get(def_id.node) {
341 format!("::{}", ty::item_path_str(&self.analysis.ty_cx, def_id))
344 self.sess.span_bug(method.span,
345 &format!("Could not find container {} for method {}",
346 def_id.node, method.id)[]);
351 self.sess.span_bug(method.span,
352 &format!("Could not find container for method {}",
358 let qualname = format!("{}::{}", qualname, get_ident(method.pe_ident()).get());
359 let qualname = &qualname[];
361 // record the decl for this def (if it has one)
362 let decl_id = ty::trait_item_of_item(&self.analysis.ty_cx,
363 ast_util::local_def(method.id))
366 ty::MethodTraitItemId(def_id) => {
367 def_id.node != 0 && def_id != ast_util::local_def(method.id)
369 ty::TypeTraitItemId(_) => false,
376 let decl_id = match decl_id {
378 Some(id) => Some(id.def_id()),
381 let sub_span = self.span.sub_span_after_keyword(method.span, keywords::Fn);
382 self.fmt.method_str(method.span,
389 self.process_formals(&method.pe_fn_decl().inputs, qualname);
391 // walk arg and return types
392 for arg in &method.pe_fn_decl().inputs {
393 self.visit_ty(&*arg.ty);
396 if let ast::Return(ref ret_ty) = method.pe_fn_decl().output {
397 self.visit_ty(&**ret_ty);
401 self.nest(method.id, |v| v.visit_block(&*method.pe_body()));
403 self.process_generic_params(method.pe_generics(),
409 fn process_trait_ref(&mut self,
410 trait_ref: &ast::TraitRef) {
411 match self.lookup_type_ref(trait_ref.ref_id) {
413 let sub_span = self.span.sub_span_for_type_name(trait_ref.path.span);
414 self.fmt.ref_str(recorder::TypeRef,
419 visit::walk_path(self, &trait_ref.path);
425 fn process_struct_field_def(&mut self,
426 field: &ast::StructField,
429 match field.node.kind {
430 ast::NamedField(ident, _) => {
431 let name = get_ident(ident);
432 let qualname = format!("{}::{}", qualname, name);
433 let typ = ppaux::ty_to_string(&self.analysis.ty_cx,
434 (*self.analysis.ty_cx.node_types.borrow())[field.node.id]);
435 match self.span.sub_span_before_token(field.span, token::Colon) {
436 Some(sub_span) => self.fmt.field_str(field.span,
443 None => self.sess.span_bug(field.span,
444 &format!("Could not find sub-span for field {}",
452 // Dump generic params bindings, then visit_generics
453 fn process_generic_params(&mut self,
454 generics:&ast::Generics,
458 // We can't only use visit_generics since we don't have spans for param
459 // bindings, so we reparse the full_span to get those sub spans.
460 // However full span is the entire enum/fn/struct block, so we only want
461 // the first few to match the number of generics we're looking for.
462 let param_sub_spans = self.span.spans_for_ty_params(full_span,
463 (generics.ty_params.len() as int));
464 for (param, param_ss) in generics.ty_params.iter().zip(param_sub_spans.iter()) {
465 // Append $id to name to make sure each one is unique
466 let name = format!("{}::{}${}",
468 escape(self.span.snippet(*param_ss)),
470 self.fmt.typedef_str(full_span,
476 self.visit_generics(generics);
479 fn process_fn(&mut self,
482 ty_params: &ast::Generics,
484 let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
486 let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Fn);
487 self.fmt.fn_str(item.span,
493 self.process_formals(&decl.inputs, &qualname[]);
495 // walk arg and return types
496 for arg in &decl.inputs {
497 self.visit_ty(&*arg.ty);
500 if let ast::Return(ref ret_ty) = decl.output {
501 self.visit_ty(&**ret_ty);
505 self.nest(item.id, |v| v.visit_block(&*body));
507 self.process_generic_params(ty_params, item.span, &qualname[], item.id);
510 fn process_static(&mut self,
516 let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
518 // If the variable is immutable, save the initialising expression.
519 let value = match mt {
520 ast::MutMutable => String::from_str("<mutable>"),
521 ast::MutImmutable => self.span.snippet(expr.span),
524 let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Static);
525 self.fmt.static_str(item.span,
528 get_ident(item.ident).get(),
531 &ty_to_string(&*typ)[],
534 // walk type and init value
535 self.visit_ty(&*typ);
536 self.visit_expr(expr);
539 fn process_const(&mut self,
544 let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
546 let sub_span = self.span.sub_span_after_keyword(item.span,
548 self.fmt.static_str(item.span,
551 get_ident(item.ident).get(),
554 &ty_to_string(&*typ)[],
557 // walk type and init value
558 self.visit_ty(&*typ);
559 self.visit_expr(expr);
562 fn process_struct(&mut self,
564 def: &ast::StructDef,
565 ty_params: &ast::Generics) {
566 let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
568 let ctor_id = match def.ctor_id {
569 Some(node_id) => node_id,
572 let val = self.span.snippet(item.span);
573 let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Struct);
574 self.fmt.struct_str(item.span,
583 for field in &def.fields {
584 self.process_struct_field_def(field, &qualname[], item.id);
585 self.visit_ty(&*field.node.ty);
588 self.process_generic_params(ty_params, item.span, &qualname[], item.id);
591 fn process_enum(&mut self,
593 enum_definition: &ast::EnumDef,
594 ty_params: &ast::Generics) {
595 let enum_name = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
596 let val = self.span.snippet(item.span);
597 match self.span.sub_span_after_keyword(item.span, keywords::Enum) {
598 Some(sub_span) => self.fmt.enum_str(item.span,
604 None => self.sess.span_bug(item.span,
605 &format!("Could not find subspan for enum {}",
608 for variant in &enum_definition.variants {
609 let name = get_ident(variant.node.name);
610 let name = name.get();
611 let mut qualname = enum_name.clone();
612 qualname.push_str("::");
613 qualname.push_str(name);
614 let val = self.span.snippet(variant.span);
615 match variant.node.kind {
616 ast::TupleVariantKind(ref args) => {
617 // first ident in span is the variant's name
618 self.fmt.tuple_variant_str(variant.span,
619 self.span.span_for_first_ident(variant.span),
627 self.visit_ty(&*arg.ty);
630 ast::StructVariantKind(ref struct_def) => {
631 let ctor_id = match struct_def.ctor_id {
632 Some(node_id) => node_id,
635 self.fmt.struct_variant_str(
637 self.span.span_for_first_ident(variant.span),
645 for field in &struct_def.fields {
646 self.process_struct_field_def(field, qualname.as_slice(), variant.node.id);
647 self.visit_ty(&*field.node.ty);
653 self.process_generic_params(ty_params, item.span, &enum_name[], item.id);
656 fn process_impl(&mut self,
658 type_parameters: &ast::Generics,
659 trait_ref: &Option<ast::TraitRef>,
661 impl_items: &Vec<ast::ImplItem>) {
662 let trait_id = trait_ref.as_ref().and_then(|tr| self.lookup_type_ref(tr.ref_id));
664 // Common case impl for a struct or something basic.
665 ast::TyPath(ref path, id) => {
666 match self.lookup_type_ref(id) {
668 let sub_span = self.span.sub_span_for_type_name(path.span);
669 self.fmt.ref_str(recorder::TypeRef,
674 self.fmt.impl_str(path.span,
685 // Less useful case, impl for a compound type.
686 self.visit_ty(&*typ);
688 let sub_span = self.span.sub_span_for_type_name(typ.span);
689 self.fmt.impl_str(typ.span,
699 Some(ref trait_ref) => self.process_trait_ref(trait_ref),
703 self.process_generic_params(type_parameters, item.span, "", item.id);
704 for impl_item in impl_items {
706 ast::MethodImplItem(ref method) => {
707 visit::walk_method_helper(self, &**method)
709 ast::TypeImplItem(ref typedef) => {
710 visit::walk_ty(self, &*typedef.typ)
716 fn process_trait(&mut self,
718 generics: &ast::Generics,
719 trait_refs: &OwnedSlice<ast::TyParamBound>,
720 methods: &Vec<ast::TraitItem>) {
721 let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
722 let val = self.span.snippet(item.span);
723 let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Trait);
724 self.fmt.trait_str(item.span,
732 for super_bound in &**trait_refs {
733 let trait_ref = match *super_bound {
734 ast::TraitTyParamBound(ref trait_ref, _) => {
737 ast::RegionTyParamBound(..) => {
742 let trait_ref = &trait_ref.trait_ref;
743 match self.lookup_type_ref(trait_ref.ref_id) {
745 let sub_span = self.span.sub_span_for_type_name(trait_ref.path.span);
746 self.fmt.ref_str(recorder::TypeRef,
751 self.fmt.inherit_str(trait_ref.path.span,
760 // walk generics and methods
761 self.process_generic_params(generics, item.span, &qualname[], item.id);
762 for method in methods {
763 self.visit_trait_item(method)
767 fn process_mod(&mut self,
768 item: &ast::Item, // The module in question, represented as an item.
770 let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
772 let cm = self.sess.codemap();
773 let filename = cm.span_to_filename(m.inner);
775 let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Mod);
776 self.fmt.mod_str(item.span,
783 self.nest(item.id, |v| visit::walk_mod(v, m));
786 fn process_path(&mut self,
790 ref_kind: Option<recorder::Row>) {
791 if generated_code(span) {
795 let def_map = self.analysis.ty_cx.def_map.borrow();
796 if !def_map.contains_key(&id) {
797 self.sess.span_bug(span,
798 format!("def_map has no key for {} in visit_expr", id).as_slice());
800 let def = &(*def_map)[id];
801 let sub_span = self.span.span_for_last_ident(span);
807 def::DefVariant(..) => self.fmt.ref_str(ref_kind.unwrap_or(recorder::VarRef),
812 def::DefStruct(def_id) => self.fmt.ref_str(recorder::StructRef,
817 def::DefTy(def_id, _) => self.fmt.ref_str(recorder::TypeRef,
822 def::DefStaticMethod(declid, provenence) |
823 def::DefMethod(declid, _, provenence) => {
824 let sub_span = self.span.sub_span_for_meth_name(span);
825 let defid = if declid.krate == ast::LOCAL_CRATE {
826 let ti = ty::impl_or_trait_item(&self.analysis.ty_cx,
829 def::FromTrait(def_id) => {
830 Some(ty::trait_items(&self.analysis.ty_cx,
834 mr.name() == ti.name()
839 def::FromImpl(def_id) => {
840 let impl_items = self.analysis
844 Some((*impl_items)[def_id]
847 ty::impl_or_trait_item(
848 &self.analysis.ty_cx,
850 ).name() == ti.name()
859 self.fmt.meth_call_str(span,
865 def::DefFn(def_id, _) => {
866 self.fmt.fn_call_str(span,
871 _ => self.sess.span_bug(span,
872 &format!("Unexpected def kind while looking \
873 up path in `{}`: `{:?}`",
874 self.span.snippet(span),
877 // modules or types in the path prefix
879 def::DefStaticMethod(..) => self.write_sub_path_trait_truncated(path),
881 def::DefStatic(_,_) |
884 def::DefVariant(..) |
885 def::DefFn(..) => self.write_sub_paths_truncated(path, false),
890 fn process_struct_lit(&mut self,
893 fields: &Vec<ast::Field>,
894 base: &Option<P<ast::Expr>>) {
895 if generated_code(path.span) {
899 self.write_sub_paths_truncated(path, false);
901 let ty = &ty::expr_ty_adjusted(&self.analysis.ty_cx, ex).sty;
902 let struct_def = match *ty {
903 ty::ty_struct(def_id, _) => {
904 let sub_span = self.span.span_for_last_ident(path.span);
905 self.fmt.ref_str(recorder::StructRef,
915 for field in fields {
917 Some(struct_def) => {
918 let fields = ty::lookup_struct_fields(&self.analysis.ty_cx, struct_def);
920 if generated_code(field.ident.span) {
923 if f.name == field.ident.node.name {
924 // We don't really need a sub-span here, but no harm done
925 let sub_span = self.span.span_for_last_ident(field.ident.span);
926 self.fmt.ref_str(recorder::VarRef,
937 self.visit_expr(&*field.expr)
939 visit::walk_expr_opt(self, base)
942 fn process_method_call(&mut self,
944 args: &Vec<P<ast::Expr>>) {
945 let method_map = self.analysis.ty_cx.method_map.borrow();
946 let method_callee = &(*method_map)[ty::MethodCall::expr(ex.id)];
947 let (def_id, decl_id) = match method_callee.origin {
948 ty::MethodStatic(def_id) |
949 ty::MethodStaticClosure(def_id) => {
950 // method invoked on an object with a concrete type (not a static method)
952 match ty::trait_item_of_item(&self.analysis.ty_cx,
955 Some(decl_id) => Some(decl_id.def_id()),
958 // This incantation is required if the method referenced is a
959 // trait's default implementation.
960 let def_id = match ty::impl_or_trait_item(&self.analysis
963 ty::MethodTraitItem(method) => {
964 method.provided_source.unwrap_or(def_id)
966 ty::TypeTraitItem(_) => def_id,
968 (Some(def_id), decl_id)
970 ty::MethodTypeParam(ref mp) => {
971 // method invoked on a type parameter
972 let trait_item = ty::trait_item(&self.analysis.ty_cx,
975 (None, Some(trait_item.def_id()))
977 ty::MethodTraitObject(ref mo) => {
978 // method invoked on a trait instance
979 let trait_item = ty::trait_item(&self.analysis.ty_cx,
982 (None, Some(trait_item.def_id()))
985 let sub_span = self.span.sub_span_for_meth_name(ex.span);
986 self.fmt.meth_call_str(ex.span,
992 // walk receiver and args
993 visit::walk_exprs(self, &args[]);
996 fn process_pat(&mut self, p:&ast::Pat) {
997 if generated_code(p.span) {
1002 ast::PatStruct(ref path, ref fields, _) => {
1003 self.collected_paths.push((p.id, path.clone(), false, recorder::StructRef));
1004 visit::walk_path(self, path);
1005 let struct_def = match self.lookup_type_ref(p.id) {
1008 self.sess.span_bug(p.span,
1009 &format!("Could not find struct_def for `{}`",
1010 self.span.snippet(p.span))[]);
1013 for &Spanned { node: ref field, span } in fields {
1014 let sub_span = self.span.span_for_first_ident(span);
1015 let fields = ty::lookup_struct_fields(&self.analysis.ty_cx, struct_def);
1017 if f.name == field.ident.name {
1018 self.fmt.ref_str(recorder::VarRef,
1026 self.visit_pat(&*field.pat);
1029 ast::PatEnum(ref path, _) => {
1030 self.collected_paths.push((p.id, path.clone(), false, recorder::VarRef));
1031 visit::walk_pat(self, p);
1033 ast::PatIdent(bm, ref path1, ref optional_subpattern) => {
1034 let immut = match bm {
1035 // Even if the ref is mut, you can't change the ref, only
1036 // the data pointed at, so showing the initialising expression
1037 // is still worthwhile.
1038 ast::BindByRef(_) => true,
1039 ast::BindByValue(mt) => {
1041 ast::MutMutable => false,
1042 ast::MutImmutable => true,
1046 // collect path for either visit_local or visit_arm
1047 let path = ast_util::ident_to_path(path1.span,path1.node);
1048 self.collected_paths.push((p.id, path, immut, recorder::VarRef));
1049 match *optional_subpattern {
1051 Some(ref subpattern) => self.visit_pat(&**subpattern)
1054 _ => visit::walk_pat(self, p)
1059 impl<'l, 'tcx, 'v> Visitor<'v> for DxrVisitor<'l, 'tcx> {
1060 fn visit_item(&mut self, item: &ast::Item) {
1061 if generated_code(item.span) {
1066 ast::ItemUse(ref use_item) => {
1067 match use_item.node {
1068 ast::ViewPathSimple(ident, ref path) => {
1069 let sub_span = self.span.span_for_last_ident(path.span);
1070 let mod_id = match self.lookup_type_ref(item.id) {
1072 match self.lookup_def_kind(item.id, path.span) {
1073 Some(kind) => self.fmt.ref_str(kind,
1085 // 'use' always introduces an alias, if there is not an explicit
1086 // one, there is an implicit one.
1088 match self.span.sub_span_after_keyword(use_item.span, keywords::As) {
1089 Some(sub_span) => Some(sub_span),
1093 self.fmt.use_alias_str(path.span,
1097 get_ident(ident).get(),
1099 self.write_sub_paths_truncated(path, true);
1101 ast::ViewPathGlob(ref path) => {
1102 // Make a comma-separated list of names of imported modules.
1103 let mut name_string = String::new();
1104 let glob_map = &self.analysis.glob_map;
1105 let glob_map = glob_map.as_ref().unwrap();
1106 if glob_map.contains_key(&item.id) {
1107 for n in &glob_map[item.id] {
1108 if name_string.len() > 0 {
1109 name_string.push_str(", ");
1111 name_string.push_str(n.as_str());
1115 let sub_span = self.span.sub_span_of_token(path.span,
1116 token::BinOp(token::Star));
1117 self.fmt.use_glob_str(path.span,
1120 name_string.as_slice(),
1122 self.write_sub_paths(path, true);
1124 ast::ViewPathList(ref path, ref list) => {
1127 ast::PathListIdent { id, .. } => {
1128 match self.lookup_type_ref(id) {
1130 match self.lookup_def_kind(id, plid.span) {
1135 def_id, self.cur_scope);
1142 ast::PathListMod { .. } => ()
1146 self.write_sub_paths(path, true);
1150 ast::ItemExternCrate(ref s) => {
1151 let name = get_ident(item.ident);
1152 let name = name.get();
1153 let location = match *s {
1154 Some((ref s, _)) => s.get().to_string(),
1155 None => name.to_string(),
1157 let alias_span = self.span.span_for_last_ident(item.span);
1158 let cnum = match self.sess.cstore.find_extern_mod_stmt_cnum(item.id) {
1162 self.fmt.extern_crate_str(item.span,
1170 ast::ItemFn(ref decl, _, _, ref ty_params, ref body) =>
1171 self.process_fn(item, &**decl, ty_params, &**body),
1172 ast::ItemStatic(ref typ, mt, ref expr) =>
1173 self.process_static(item, &**typ, mt, &**expr),
1174 ast::ItemConst(ref typ, ref expr) =>
1175 self.process_const(item, &**typ, &**expr),
1176 ast::ItemStruct(ref def, ref ty_params) => self.process_struct(item, &**def, ty_params),
1177 ast::ItemEnum(ref def, ref ty_params) => self.process_enum(item, def, ty_params),
1182 ref impl_items) => {
1183 self.process_impl(item,
1189 ast::ItemTrait(_, ref generics, ref trait_refs, ref methods) =>
1190 self.process_trait(item, generics, trait_refs, methods),
1191 ast::ItemMod(ref m) => self.process_mod(item, m),
1192 ast::ItemTy(ref ty, ref ty_params) => {
1193 let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
1194 let value = ty_to_string(&**ty);
1195 let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Type);
1196 self.fmt.typedef_str(item.span,
1202 self.visit_ty(&**ty);
1203 self.process_generic_params(ty_params, item.span, qualname.as_slice(), item.id);
1205 ast::ItemMac(_) => (),
1206 _ => visit::walk_item(self, item),
1210 fn visit_generics(&mut self, generics: &ast::Generics) {
1211 for param in &*generics.ty_params {
1212 for bound in &*param.bounds {
1213 if let ast::TraitTyParamBound(ref trait_ref, _) = *bound {
1214 self.process_trait_ref(&trait_ref.trait_ref);
1217 if let Some(ref ty) = param.default {
1218 self.visit_ty(&**ty);
1223 // We don't actually index functions here, that is done in visit_item/ItemFn.
1224 // Here we just visit methods.
1225 fn visit_fn(&mut self,
1226 fk: visit::FnKind<'v>,
1227 fd: &'v ast::FnDecl,
1231 if generated_code(s) {
1236 visit::FkMethod(_, _, method) => self.process_method(method),
1237 _ => visit::walk_fn(self, fk, fd, b, s),
1241 fn visit_trait_item(&mut self, tm: &ast::TraitItem) {
1243 ast::RequiredMethod(ref method_type) => {
1244 if generated_code(method_type.span) {
1249 let mut qualname = match ty::trait_of_item(&self.analysis.ty_cx,
1250 ast_util::local_def(method_type.id)) {
1252 scope_id = def_id.node;
1253 format!("::{}::", ty::item_path_str(&self.analysis.ty_cx, def_id))
1256 self.sess.span_bug(method_type.span,
1257 &format!("Could not find trait for method {}",
1262 qualname.push_str(get_ident(method_type.ident).get());
1263 let qualname = &qualname[];
1265 let sub_span = self.span.sub_span_after_keyword(method_type.span, keywords::Fn);
1266 self.fmt.method_decl_str(method_type.span,
1272 // walk arg and return types
1273 for arg in &method_type.decl.inputs {
1274 self.visit_ty(&*arg.ty);
1277 if let ast::Return(ref ret_ty) = method_type.decl.output {
1278 self.visit_ty(&**ret_ty);
1281 self.process_generic_params(&method_type.generics,
1286 ast::ProvidedMethod(ref method) => self.process_method(&**method),
1287 ast::TypeTraitItem(_) => {}
1291 fn visit_ty(&mut self, t: &ast::Ty) {
1292 if generated_code(t.span) {
1297 ast::TyPath(ref path, id) => {
1298 match self.lookup_type_ref(id) {
1300 let sub_span = self.span.sub_span_for_type_name(t.span);
1301 self.fmt.ref_str(recorder::TypeRef,
1310 self.write_sub_paths_truncated(path, false);
1312 visit::walk_path(self, path);
1314 _ => visit::walk_ty(self, t),
1318 fn visit_expr(&mut self, ex: &ast::Expr) {
1319 if generated_code(ex.span) {
1324 ast::ExprCall(ref _f, ref _args) => {
1325 // Don't need to do anything for function calls,
1326 // because just walking the callee path does what we want.
1327 visit::walk_expr(self, ex);
1329 ast::ExprPath(ref path) => {
1330 self.process_path(ex.id, path.span, path, None);
1331 visit::walk_path(self, path);
1333 ast::ExprQPath(ref qpath) => {
1334 let mut path = qpath.trait_ref.path.clone();
1335 path.segments.push(qpath.item_path.clone());
1336 self.process_path(ex.id, ex.span, &path, None);
1337 visit::walk_qpath(self, ex.span, &**qpath);
1339 ast::ExprStruct(ref path, ref fields, ref base) =>
1340 self.process_struct_lit(ex, path, fields, base),
1341 ast::ExprMethodCall(_, _, ref args) => self.process_method_call(ex, args),
1342 ast::ExprField(ref sub_ex, ident) => {
1343 if generated_code(sub_ex.span) {
1347 self.visit_expr(&**sub_ex);
1348 let ty = &ty::expr_ty_adjusted(&self.analysis.ty_cx, &**sub_ex).sty;
1350 ty::ty_struct(def_id, _) => {
1351 let fields = ty::lookup_struct_fields(&self.analysis.ty_cx, def_id);
1353 if f.name == ident.node.name {
1354 let sub_span = self.span.span_for_last_ident(ex.span);
1355 self.fmt.ref_str(recorder::VarRef,
1364 _ => self.sess.span_bug(ex.span,
1365 &format!("Expected struct type, found {:?}", ty)[]),
1368 ast::ExprTupField(ref sub_ex, idx) => {
1369 if generated_code(sub_ex.span) {
1373 self.visit_expr(&**sub_ex);
1375 let ty = &ty::expr_ty_adjusted(&self.analysis.ty_cx, &**sub_ex).sty;
1377 ty::ty_struct(def_id, _) => {
1378 let fields = ty::lookup_struct_fields(&self.analysis.ty_cx, def_id);
1379 for (i, f) in fields.iter().enumerate() {
1381 let sub_span = self.span.sub_span_after_token(ex.span, token::Dot);
1382 self.fmt.ref_str(recorder::VarRef,
1392 _ => self.sess.span_bug(ex.span,
1393 &format!("Expected struct or tuple \
1394 type, found {:?}", ty)[]),
1397 ast::ExprClosure(_, _, ref decl, ref body) => {
1398 if generated_code(body.span) {
1402 let mut id = String::from_str("$");
1403 id.push_str(&ex.id.to_string()[]);
1404 self.process_formals(&decl.inputs, &id[]);
1406 // walk arg and return types
1407 for arg in &decl.inputs {
1408 self.visit_ty(&*arg.ty);
1411 if let ast::Return(ref ret_ty) = decl.output {
1412 self.visit_ty(&**ret_ty);
1416 self.nest(ex.id, |v| v.visit_block(&**body));
1419 visit::walk_expr(self, ex)
1424 fn visit_mac(&mut self, _: &ast::Mac) {
1425 // Just stop, macros are poison to us.
1428 fn visit_pat(&mut self, p: &ast::Pat) {
1429 self.process_pat(p);
1430 if !self.collecting {
1431 self.collected_paths.clear();
1435 fn visit_arm(&mut self, arm: &ast::Arm) {
1436 assert!(self.collected_paths.len() == 0 && !self.collecting);
1437 self.collecting = true;
1438 for pattern in &arm.pats {
1439 // collect paths from the arm's patterns
1440 self.visit_pat(&**pattern);
1443 // This is to get around borrow checking, because we need mut self to call process_path.
1444 let mut paths_to_process = vec![];
1445 // process collected paths
1446 for &(id, ref p, ref immut, ref_kind) in &self.collected_paths {
1447 let def_map = self.analysis.ty_cx.def_map.borrow();
1448 if !def_map.contains_key(&id) {
1449 self.sess.span_bug(p.span,
1450 &format!("def_map has no key for {} in visit_arm",
1453 let def = &(*def_map)[id];
1455 def::DefLocal(id) => {
1456 let value = if *immut {
1457 self.span.snippet(p.span).to_string()
1459 "<mutable>".to_string()
1462 assert!(p.segments.len() == 1, "qualified path for local variable def in arm");
1463 self.fmt.variable_str(p.span,
1466 &path_to_string(p)[],
1470 def::DefVariant(..) | def::DefTy(..) | def::DefStruct(..) => {
1471 paths_to_process.push((id, p.clone(), Some(ref_kind)))
1473 // FIXME(nrc) what are these doing here?
1474 def::DefStatic(_, _) => {}
1475 def::DefConst(..) => {}
1476 _ => error!("unexpected definition kind when processing collected paths: {:?}",
1480 for &(id, ref path, ref_kind) in &paths_to_process {
1481 self.process_path(id, path.span, path, ref_kind);
1483 self.collecting = false;
1484 self.collected_paths.clear();
1485 visit::walk_expr_opt(self, &arm.guard);
1486 self.visit_expr(&*arm.body);
1489 fn visit_stmt(&mut self, s: &ast::Stmt) {
1490 if generated_code(s.span) {
1494 visit::walk_stmt(self, s)
1497 fn visit_local(&mut self, l: &ast::Local) {
1498 if generated_code(l.span) {
1502 // The local could declare multiple new vars, we must walk the
1503 // pattern and collect them all.
1504 assert!(self.collected_paths.len() == 0 && !self.collecting);
1505 self.collecting = true;
1506 self.visit_pat(&*l.pat);
1507 self.collecting = false;
1509 let value = self.span.snippet(l.span);
1511 for &(id, ref p, ref immut, _) in &self.collected_paths {
1512 let value = if *immut { value.to_string() } else { "<mutable>".to_string() };
1513 let types = self.analysis.ty_cx.node_types.borrow();
1514 let typ = ppaux::ty_to_string(&self.analysis.ty_cx, (*types)[id]);
1515 // Get the span only for the name of the variable (I hope the path
1516 // is only ever a variable name, but who knows?).
1517 let sub_span = self.span.span_for_last_ident(p.span);
1518 // Rust uses the id of the pattern for var lookups, so we'll use it too.
1519 self.fmt.variable_str(p.span,
1522 &path_to_string(p)[],
1526 self.collected_paths.clear();
1528 // Just walk the initialiser and type (don't want to walk the pattern again).
1529 visit::walk_ty_opt(self, &l.ty);
1530 visit::walk_expr_opt(self, &l.init);
1534 pub fn process_crate(sess: &Session,
1536 analysis: &ty::CrateAnalysis,
1537 odir: Option<&Path>) {
1538 if generated_code(krate.span) {
1542 assert!(analysis.glob_map.is_some());
1543 let cratename = match attr::find_crate_name(&krate.attrs[]) {
1544 Some(name) => name.get().to_string(),
1546 info!("Could not find crate name, using 'unknown_crate'");
1547 String::from_str("unknown_crate")
1551 info!("Dumping crate {}", cratename);
1553 // find a path to dump our data to
1554 let mut root_path = match env::var_string("DXR_RUST_TEMP_FOLDER") {
1555 Ok(val) => Path::new(val),
1556 Err(..) => match odir {
1557 Some(val) => val.join("dxr"),
1558 None => Path::new("dxr-temp"),
1562 match fs::mkdir_recursive(&root_path, old_io::USER_RWX) {
1563 Err(e) => sess.err(&format!("Could not create directory {}: {}",
1564 root_path.display(), e)[]),
1569 let disp = root_path.display();
1570 info!("Writing output to {}", disp);
1573 // Create output file.
1574 let mut out_name = cratename.clone();
1575 out_name.push_str(".csv");
1576 root_path.push(out_name);
1577 let output_file = match File::create(&root_path) {
1580 let disp = root_path.display();
1581 sess.fatal(&format!("Could not open {}: {}", disp, e)[]);
1586 let mut visitor = DxrVisitor {
1589 collected_paths: vec!(),
1591 fmt: FmtStrs::new(box Recorder {
1592 out: output_file as Box<Writer+'static>,
1597 err_count: Cell::new(0)
1601 err_count: Cell::new(0)
1606 visitor.dump_crate_info(&cratename[], krate);
1608 visit::walk_crate(&mut visitor, krate);