2015-07-26 05:55:25 -05:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2017-03-21 15:00:33 -05:00
|
|
|
use std::collections::BTreeMap;
|
2017-07-13 04:42:14 -05:00
|
|
|
use std::path::{Path, PathBuf};
|
2015-07-26 05:55:25 -05:00
|
|
|
|
|
|
|
use syntax::ast;
|
|
|
|
use syntax::codemap;
|
2015-07-26 07:05:43 -05:00
|
|
|
use syntax::parse::parser;
|
2015-07-26 05:55:25 -05:00
|
|
|
|
2017-07-13 04:42:14 -05:00
|
|
|
use utils::contains_skip;
|
|
|
|
|
2015-07-26 05:55:25 -05:00
|
|
|
|
|
|
|
/// List all the files containing modules of a crate.
|
|
|
|
/// If a file is used twice in a crate, it appears only once.
|
2017-06-11 22:58:58 -05:00
|
|
|
pub fn list_files<'a>(
|
|
|
|
krate: &'a ast::Crate,
|
|
|
|
codemap: &codemap::CodeMap,
|
|
|
|
) -> BTreeMap<PathBuf, &'a ast::Mod> {
|
2017-03-21 15:00:33 -05:00
|
|
|
let mut result = BTreeMap::new(); // Enforce file order determinism
|
2015-07-26 05:55:25 -05:00
|
|
|
let root_filename: PathBuf = codemap.span_to_filename(krate.span).into();
|
2017-06-11 22:58:58 -05:00
|
|
|
list_submodules(
|
|
|
|
&krate.module,
|
|
|
|
root_filename.parent().unwrap(),
|
|
|
|
codemap,
|
|
|
|
&mut result,
|
|
|
|
);
|
2015-07-26 05:55:25 -05:00
|
|
|
result.insert(root_filename, &krate.module);
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Recursively list all external modules included in a module.
|
2017-06-11 22:58:58 -05:00
|
|
|
fn list_submodules<'a>(
|
|
|
|
module: &'a ast::Mod,
|
|
|
|
search_dir: &Path,
|
|
|
|
codemap: &codemap::CodeMap,
|
|
|
|
result: &mut BTreeMap<PathBuf, &'a ast::Mod>,
|
|
|
|
) {
|
2015-07-26 05:55:25 -05:00
|
|
|
debug!("list_submodules: search_dir: {:?}", search_dir);
|
2015-09-04 16:39:33 -05:00
|
|
|
for item in &module.items {
|
2016-03-01 16:27:19 -06:00
|
|
|
if let ast::ItemKind::Mod(ref sub_mod) = item.node {
|
2017-07-13 04:42:14 -05:00
|
|
|
if !contains_skip(&item.attrs) {
|
2017-07-05 04:31:37 -05:00
|
|
|
let is_internal =
|
|
|
|
codemap.span_to_filename(item.span) == codemap.span_to_filename(sub_mod.inner);
|
2015-07-26 05:55:25 -05:00
|
|
|
let dir_path = if is_internal {
|
2015-07-26 07:05:43 -05:00
|
|
|
search_dir.join(&item.ident.to_string())
|
2015-07-26 05:55:25 -05:00
|
|
|
} else {
|
|
|
|
let mod_path = module_file(item.ident, &item.attrs, search_dir, codemap);
|
|
|
|
let dir_path = mod_path.parent().unwrap().to_owned();
|
|
|
|
result.insert(mod_path, sub_mod);
|
|
|
|
dir_path
|
|
|
|
};
|
|
|
|
list_submodules(sub_mod, &dir_path, codemap, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Find the file corresponding to an external mod
|
2017-06-11 22:58:58 -05:00
|
|
|
fn module_file(
|
|
|
|
id: ast::Ident,
|
|
|
|
attrs: &[ast::Attribute],
|
|
|
|
dir_path: &Path,
|
|
|
|
codemap: &codemap::CodeMap,
|
|
|
|
) -> PathBuf {
|
2016-08-23 09:14:45 -05:00
|
|
|
if let Some(path) = parser::Parser::submod_path_from_attr(attrs, dir_path) {
|
2015-07-26 05:55:25 -05:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2017-05-25 02:08:08 -05:00
|
|
|
match parser::Parser::default_submod_path(id, dir_path, codemap).result {
|
2015-07-26 05:55:25 -05:00
|
|
|
Ok(parser::ModulePathSuccess { path, .. }) => path,
|
2017-10-26 02:55:48 -05:00
|
|
|
Err(_) => unreachable!("Couldn't find module {}", id),
|
2015-07-26 05:55:25 -05:00
|
|
|
}
|
|
|
|
}
|