]> git.lizzy.rs Git - rust.git/blob - src/librustc_driver/derive_registrar.rs
Rollup merge of #53545 - FelixMcFelix:fix-50865-beta, r=petrochenkov
[rust.git] / src / librustc_driver / derive_registrar.rs
1 // Copyright 2016 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 use rustc::hir::itemlikevisit::ItemLikeVisitor;
12 use rustc::hir::map::Map;
13 use rustc::hir;
14 use syntax::ast;
15 use syntax::attr;
16
17 pub fn find(hir_map: &Map) -> Option<ast::NodeId> {
18     let krate = hir_map.krate();
19
20     let mut finder = Finder { registrar: None };
21     krate.visit_all_item_likes(&mut finder);
22     finder.registrar
23 }
24
25 struct Finder {
26     registrar: Option<ast::NodeId>,
27 }
28
29 impl<'v> ItemLikeVisitor<'v> for Finder {
30     fn visit_item(&mut self, item: &hir::Item) {
31         if attr::contains_name(&item.attrs, "rustc_derive_registrar") {
32             self.registrar = Some(item.id);
33         }
34     }
35
36     fn visit_trait_item(&mut self, _trait_item: &hir::TraitItem) {
37     }
38
39     fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem) {
40     }
41 }
42