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