]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_utils/symbol_names_test.rs
Rollup merge of #58096 - h-michael:linkchecker-2018, r=Centril
[rust.git] / src / librustc_codegen_utils / symbol_names_test.rs
1 //! Walks the crate looking for items/impl-items/trait-items that have
2 //! either a `rustc_symbol_name` or `rustc_item_path` attribute and
3 //! generates an error giving, respectively, the symbol name or
4 //! item-path. This is used for unit testing the code that generates
5 //! paths etc in all kinds of annoying scenarios.
6
7 use rustc::hir;
8 use rustc::ty::TyCtxt;
9 use syntax::ast;
10
11 use rustc_mir::monomorphize::Instance;
12
13 const SYMBOL_NAME: &'static str = "rustc_symbol_name";
14 const ITEM_PATH: &'static str = "rustc_item_path";
15
16 pub fn report_symbol_names<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
17     // if the `rustc_attrs` feature is not enabled, then the
18     // attributes we are interested in cannot be present anyway, so
19     // skip the walk.
20     if !tcx.features().rustc_attrs {
21         return;
22     }
23
24     tcx.dep_graph.with_ignore(|| {
25         let mut visitor = SymbolNamesTest { tcx };
26         tcx.hir().krate().visit_all_item_likes(&mut visitor);
27     })
28 }
29
30 struct SymbolNamesTest<'a, 'tcx:'a> {
31     tcx: TyCtxt<'a, 'tcx, 'tcx>,
32 }
33
34 impl<'a, 'tcx> SymbolNamesTest<'a, 'tcx> {
35     fn process_attrs(&mut self,
36                      node_id: ast::NodeId) {
37         let tcx = self.tcx;
38         let def_id = tcx.hir().local_def_id(node_id);
39         for attr in tcx.get_attrs(def_id).iter() {
40             if attr.check_name(SYMBOL_NAME) {
41                 // for now, can only use on monomorphic names
42                 let instance = Instance::mono(tcx, def_id);
43                 let name = self.tcx.symbol_name(instance);
44                 tcx.sess.span_err(attr.span, &format!("symbol-name({})", name));
45             } else if attr.check_name(ITEM_PATH) {
46                 let path = tcx.item_path_str(def_id);
47                 tcx.sess.span_err(attr.span, &format!("item-path({})", path));
48             }
49
50             // (*) The formatting of `tag({})` is chosen so that tests can elect
51             // to test the entirety of the string, if they choose, or else just
52             // some subset.
53         }
54     }
55 }
56
57 impl<'a, 'tcx> hir::itemlikevisit::ItemLikeVisitor<'tcx> for SymbolNamesTest<'a, 'tcx> {
58     fn visit_item(&mut self, item: &'tcx hir::Item) {
59         self.process_attrs(item.id);
60     }
61
62     fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem) {
63         self.process_attrs(trait_item.id);
64     }
65
66     fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem) {
67         self.process_attrs(impl_item.id);
68     }
69 }