1 // Copyright 2017 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.
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.
11 use rustc::hir::itemlikevisit::ItemLikeVisitor;
13 use rustc::ty::TyCtxt;
14 use rustc_target::spec::abi::Abi;
16 pub fn collect<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Vec<String> {
17 let mut collector = Collector {
20 tcx.hir.krate().visit_all_item_likes(&mut collector);
22 for attr in tcx.hir.krate().attrs.iter() {
23 if attr.path == "link_args" {
24 if let Some(linkarg) = attr.value_str() {
25 collector.add_link_args(&linkarg.as_str());
37 impl<'tcx> ItemLikeVisitor<'tcx> for Collector {
38 fn visit_item(&mut self, it: &'tcx hir::Item) {
39 let fm = match it.node {
40 hir::ItemKind::ForeignMod(ref fm) => fm,
43 if fm.abi == Abi::Rust ||
44 fm.abi == Abi::RustIntrinsic ||
45 fm.abi == Abi::PlatformIntrinsic {
49 // First, add all of the custom #[link_args] attributes
50 for m in it.attrs.iter().filter(|a| a.check_name("link_args")) {
51 if let Some(linkarg) = m.value_str() {
52 self.add_link_args(&linkarg.as_str());
57 fn visit_trait_item(&mut self, _it: &'tcx hir::TraitItem) {}
58 fn visit_impl_item(&mut self, _it: &'tcx hir::ImplItem) {}
62 fn add_link_args(&mut self, args: &str) {
63 self.args.extend(args.split(' ').filter(|s| !s.is_empty()).map(|s| s.to_string()))