]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/symbol_names_test.rs
Auto merge of #39418 - redox-os:redox_fs_ext, r=brson
[rust.git] / src / librustc_trans / symbol_names_test.rs
1 // Copyright 2012-2015 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.
4 //
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.
10
11 //! Walks the crate looking for items/impl-items/trait-items that have
12 //! either a `rustc_symbol_name` or `rustc_item_path` attribute and
13 //! generates an error giving, respectively, the symbol name or
14 //! item-path. This is used for unit testing the code that generates
15 //! paths etc in all kinds of annoying scenarios.
16
17 use rustc::hir;
18 use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
19 use syntax::ast;
20
21 use common::SharedCrateContext;
22 use monomorphize::Instance;
23
24 const SYMBOL_NAME: &'static str = "rustc_symbol_name";
25 const ITEM_PATH: &'static str = "rustc_item_path";
26
27 pub fn report_symbol_names(scx: &SharedCrateContext) {
28     // if the `rustc_attrs` feature is not enabled, then the
29     // attributes we are interested in cannot be present anyway, so
30     // skip the walk.
31     let tcx = scx.tcx();
32     if !tcx.sess.features.borrow().rustc_attrs {
33         return;
34     }
35
36     let _ignore = tcx.dep_graph.in_ignore();
37     let mut visitor = SymbolNamesTest { scx: scx };
38     // FIXME(#37712) could use ItemLikeVisitor if trait items were item-like
39     tcx.hir.krate().visit_all_item_likes(&mut visitor.as_deep_visitor());
40 }
41
42 struct SymbolNamesTest<'a, 'tcx:'a> {
43     scx: &'a SharedCrateContext<'a, 'tcx>,
44 }
45
46 impl<'a, 'tcx> SymbolNamesTest<'a, 'tcx> {
47     fn process_attrs(&mut self,
48                      node_id: ast::NodeId) {
49         let tcx = self.scx.tcx();
50         let def_id = tcx.hir.local_def_id(node_id);
51         for attr in tcx.get_attrs(def_id).iter() {
52             if attr.check_name(SYMBOL_NAME) {
53                 // for now, can only use on monomorphic names
54                 let instance = Instance::mono(self.scx, def_id);
55                 let name = instance.symbol_name(self.scx);
56                 tcx.sess.span_err(attr.span, &format!("symbol-name({})", name));
57             } else if attr.check_name(ITEM_PATH) {
58                 let path = tcx.item_path_str(def_id);
59                 tcx.sess.span_err(attr.span, &format!("item-path({})", path));
60             }
61
62             // (*) The formatting of `tag({})` is chosen so that tests can elect
63             // to test the entirety of the string, if they choose, or else just
64             // some subset.
65         }
66     }
67 }
68
69 impl<'a, 'tcx> Visitor<'tcx> for SymbolNamesTest<'a, 'tcx> {
70     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
71         NestedVisitorMap::None
72     }
73
74     fn visit_item(&mut self, item: &'tcx hir::Item) {
75         self.process_attrs(item.id);
76         intravisit::walk_item(self, item);
77     }
78
79     fn visit_trait_item(&mut self, ti: &'tcx hir::TraitItem) {
80         self.process_attrs(ti.id);
81         intravisit::walk_trait_item(self, ti)
82     }
83
84     fn visit_impl_item(&mut self, ii: &'tcx hir::ImplItem) {
85         self.process_attrs(ii.id);
86         intravisit::walk_impl_item(self, ii)
87     }
88 }
89