]> git.proxmox.com Git - rustc.git/blame - src/librustc_codegen_utils/symbol_names_test.rs
New upstream version 1.39.0+dfsg1
[rustc.git] / src / librustc_codegen_utils / symbol_names_test.rs
CommitLineData
54a0048b 1//! Walks the crate looking for items/impl-items/trait-items that have
532ac7d7 2//! either a `rustc_symbol_name` or `rustc_def_path` attribute and
54a0048b 3//! generates an error giving, respectively, the symbol name or
532ac7d7 4//! def-path. This is used for unit testing the code that generates
54a0048b
SL
5//! paths etc in all kinds of annoying scenarios.
6
54a0048b 7use rustc::hir;
dc9dc135 8use rustc::ty::{TyCtxt, Instance};
48663c56 9use syntax::symbol::{Symbol, sym};
54a0048b 10
48663c56
XL
11const SYMBOL_NAME: Symbol = sym::rustc_symbol_name;
12const DEF_PATH: Symbol = sym::rustc_def_path;
54a0048b 13
416331ca 14pub fn report_symbol_names(tcx: TyCtxt<'_>) {
54a0048b
SL
15 // if the `rustc_attrs` feature is not enabled, then the
16 // attributes we are interested in cannot be present anyway, so
17 // skip the walk.
0531ce1d 18 if !tcx.features().rustc_attrs {
54a0048b
SL
19 return;
20 }
21
2c00a5a8 22 tcx.dep_graph.with_ignore(|| {
a1dfa0c6 23 let mut visitor = SymbolNamesTest { tcx };
0731742a 24 tcx.hir().krate().visit_all_item_likes(&mut visitor);
2c00a5a8 25 })
54a0048b
SL
26}
27
dc9dc135
XL
28struct SymbolNamesTest<'tcx> {
29 tcx: TyCtxt<'tcx>,
54a0048b
SL
30}
31
dc9dc135 32impl SymbolNamesTest<'tcx> {
54a0048b 33 fn process_attrs(&mut self,
532ac7d7 34 hir_id: hir::HirId) {
cc61c64b 35 let tcx = self.tcx;
416331ca 36 let def_id = tcx.hir().local_def_id(hir_id);
54a0048b
SL
37 for attr in tcx.get_attrs(def_id).iter() {
38 if attr.check_name(SYMBOL_NAME) {
39 // for now, can only use on monomorphic names
cc61c64b 40 let instance = Instance::mono(tcx, def_id);
dc9dc135
XL
41 let mangled = self.tcx.symbol_name(instance);
42 tcx.sess.span_err(attr.span, &format!("symbol-name({})", mangled));
e1599b0c 43 if let Ok(demangling) = rustc_demangle::try_demangle(&mangled.name.as_str()) {
dc9dc135
XL
44 tcx.sess.span_err(attr.span, &format!("demangling({})", demangling));
45 tcx.sess.span_err(attr.span, &format!("demangling-alt({:#})", demangling));
46 }
532ac7d7
XL
47 } else if attr.check_name(DEF_PATH) {
48 let path = tcx.def_path_str(def_id);
49 tcx.sess.span_err(attr.span, &format!("def-path({})", path));
54a0048b
SL
50 }
51
52 // (*) The formatting of `tag({})` is chosen so that tests can elect
53 // to test the entirety of the string, if they choose, or else just
54 // some subset.
55 }
56 }
57}
58
dc9dc135 59impl hir::itemlikevisit::ItemLikeVisitor<'tcx> for SymbolNamesTest<'tcx> {
54a0048b 60 fn visit_item(&mut self, item: &'tcx hir::Item) {
532ac7d7 61 self.process_attrs(item.hir_id);
54a0048b
SL
62 }
63
0531ce1d 64 fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem) {
532ac7d7 65 self.process_attrs(trait_item.hir_id);
54a0048b
SL
66 }
67
0531ce1d 68 fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem) {
532ac7d7 69 self.process_attrs(impl_item.hir_id);
54a0048b
SL
70 }
71}