]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_utils/symbol_names_test.rs
Separate codepaths for fat and thin LTO in write.rs
[rust.git] / src / librustc_codegen_utils / 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::ty::TyCtxt;
19 use syntax::ast;
20
21 use rustc_mir::monomorphize::Instance;
22
23 const SYMBOL_NAME: &'static str = "rustc_symbol_name";
24 const ITEM_PATH: &'static str = "rustc_item_path";
25
26 pub fn report_symbol_names<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
27     // if the `rustc_attrs` feature is not enabled, then the
28     // attributes we are interested in cannot be present anyway, so
29     // skip the walk.
30     if !tcx.features().rustc_attrs {
31         return;
32     }
33
34     tcx.dep_graph.with_ignore(|| {
35         let mut visitor = SymbolNamesTest { tcx };
36         tcx.hir.krate().visit_all_item_likes(&mut visitor);
37     })
38 }
39
40 struct SymbolNamesTest<'a, 'tcx:'a> {
41     tcx: TyCtxt<'a, 'tcx, 'tcx>,
42 }
43
44 impl<'a, 'tcx> SymbolNamesTest<'a, 'tcx> {
45     fn process_attrs(&mut self,
46                      node_id: ast::NodeId) {
47         let tcx = self.tcx;
48         let def_id = tcx.hir.local_def_id(node_id);
49         for attr in tcx.get_attrs(def_id).iter() {
50             if attr.check_name(SYMBOL_NAME) {
51                 // for now, can only use on monomorphic names
52                 let instance = Instance::mono(tcx, def_id);
53                 let name = self.tcx.symbol_name(instance);
54                 tcx.sess.span_err(attr.span, &format!("symbol-name({})", name));
55             } else if attr.check_name(ITEM_PATH) {
56                 let path = tcx.item_path_str(def_id);
57                 tcx.sess.span_err(attr.span, &format!("item-path({})", path));
58             }
59
60             // (*) The formatting of `tag({})` is chosen so that tests can elect
61             // to test the entirety of the string, if they choose, or else just
62             // some subset.
63         }
64     }
65 }
66
67 impl<'a, 'tcx> hir::itemlikevisit::ItemLikeVisitor<'tcx> for SymbolNamesTest<'a, 'tcx> {
68     fn visit_item(&mut self, item: &'tcx hir::Item) {
69         self.process_attrs(item.id);
70     }
71
72     fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem) {
73         self.process_attrs(trait_item.id);
74     }
75
76     fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem) {
77         self.process_attrs(impl_item.id);
78     }
79 }