]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/trans/symbol_names_test.rs
unit-test symbol-names and item-paths
[rust.git] / src / librustc_trans / 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 back::symbol_names;
18 use rustc::middle::ty::TyCtxt;
19 use rustc_front::hir;
20 use rustc_front::intravisit::{self, Visitor};
21 use syntax::ast;
22 use syntax::attr::AttrMetaMethods;
23 use trans::common::CrateContext;
24
25 const SYMBOL_NAME: &'static str = "rustc_symbol_name";
26 const ITEM_PATH: &'static str = "rustc_item_path";
27
28 pub fn report_symbol_names(ccx: &CrateContext) {
29     // if the `rustc_attrs` feature is not enabled, then the
30     // attributes we are interested in cannot be present anyway, so
31     // skip the walk.
32     let tcx = ccx.tcx();
33     if !tcx.sess.features.borrow().rustc_attrs {
34         return;
35     }
36
37     let _ignore = tcx.dep_graph.in_ignore();
38     let mut visitor = SymbolNamesTest { ccx: ccx, tcx: tcx };
39     tcx.map.krate().visit_all_items(&mut visitor);
40 }
41
42 struct SymbolNamesTest<'a, 'tcx:'a> {
43     ccx: &'a CrateContext<'a, 'tcx>,
44     tcx: &'a TyCtxt<'tcx>,
45 }
46
47 impl<'a, 'tcx> SymbolNamesTest<'a, 'tcx> {
48     fn process_attrs(&mut self,
49                      node_id: ast::NodeId) {
50         let def_id = self.tcx.map.local_def_id(node_id);
51         for attr in self.tcx.get_attrs(def_id).iter() {
52             if attr.check_name(SYMBOL_NAME) {
53                 // for now, just monomorphic names
54                 let name = symbol_names::exported_name(self.ccx, def_id, &[]);
55                 self.tcx.sess.span_err(attr.span, &format!("symbol-name({})", name));
56             } else if attr.check_name(ITEM_PATH) {
57                 let path = self.tcx.item_path_str(def_id);
58                 self.tcx.sess.span_err(attr.span, &format!("item-path({})", path));
59             }
60
61             // (*) The formatting of `tag({})` is chosen so that tests can elect
62             // to test the entirety of the string, if they choose, or else just
63             // some subset.
64         }
65     }
66 }
67
68 impl<'a, 'tcx> Visitor<'tcx> for SymbolNamesTest<'a, 'tcx> {
69     fn visit_item(&mut self, item: &'tcx hir::Item) {
70         self.process_attrs(item.id);
71         intravisit::walk_item(self, item);
72     }
73
74     fn visit_trait_item(&mut self, ti: &'tcx hir::TraitItem) {
75         self.process_attrs(ti.id);
76         intravisit::walk_trait_item(self, ti)
77     }
78
79     fn visit_impl_item(&mut self, ii: &'tcx hir::ImplItem) {
80         self.process_attrs(ii.id);
81         intravisit::walk_impl_item(self, ii)
82     }
83 }
84