2019-06-08 18:47:18 +09:00
|
|
|
use std::borrow::Cow;
|
2017-03-21 16:00:33 -04:00
|
|
|
use std::collections::BTreeMap;
|
2017-12-24 00:28:58 +09:00
|
|
|
use std::path::{Path, PathBuf};
|
2015-07-26 12:55:25 +02:00
|
|
|
|
2020-03-27 22:29:12 -05:00
|
|
|
use rustc_ast::ast;
|
|
|
|
use rustc_ast::visit::Visitor;
|
2020-05-19 17:31:28 +09:00
|
|
|
use rustc_span::symbol::{self, sym, Symbol};
|
2015-07-26 12:55:25 +02:00
|
|
|
|
2019-06-09 09:20:39 +09:00
|
|
|
use crate::attr::MetaVisitor;
|
2019-02-04 13:30:43 +03:00
|
|
|
use crate::config::FileName;
|
2019-03-17 12:25:59 +09:00
|
|
|
use crate::items::is_mod_decl;
|
2020-03-26 21:25:34 -05:00
|
|
|
use crate::syntux::parser::{Directory, DirectoryOwnership, ModulePathSuccess, Parser};
|
|
|
|
use crate::syntux::session::ParseSess;
|
2019-02-04 13:30:43 +03:00
|
|
|
use crate::utils::contains_skip;
|
2017-07-13 18:42:14 +09:00
|
|
|
|
2019-06-08 18:47:18 +09:00
|
|
|
mod visitor;
|
|
|
|
|
2019-06-09 09:20:39 +09:00
|
|
|
type FileModMap<'ast> = BTreeMap<FileName, Cow<'ast, ast::Mod>>;
|
2019-03-17 12:25:59 +09:00
|
|
|
|
2020-03-26 21:25:34 -05:00
|
|
|
lazy_static! {
|
|
|
|
static ref CFG_IF: Symbol = Symbol::intern("cfg_if");
|
|
|
|
}
|
|
|
|
|
2019-03-17 12:25:59 +09:00
|
|
|
/// Maps each module to the corresponding file.
|
2019-06-08 18:47:18 +09:00
|
|
|
pub(crate) struct ModResolver<'ast, 'sess> {
|
|
|
|
parse_sess: &'sess ParseSess,
|
2019-03-17 12:25:59 +09:00
|
|
|
directory: Directory,
|
2019-06-08 18:47:18 +09:00
|
|
|
file_map: FileModMap<'ast>,
|
2019-06-07 14:55:41 +09:00
|
|
|
recursive: bool,
|
2019-03-17 12:25:59 +09:00
|
|
|
}
|
|
|
|
|
2019-06-09 09:20:39 +09:00
|
|
|
#[derive(Clone)]
|
|
|
|
enum SubModKind<'a, 'ast> {
|
2019-06-08 18:47:18 +09:00
|
|
|
/// `mod foo;`
|
2020-03-26 23:18:16 -05:00
|
|
|
External(PathBuf, DirectoryOwnership, Cow<'ast, ast::Mod>),
|
2019-06-09 09:20:39 +09:00
|
|
|
/// `mod foo;` with multiple sources.
|
|
|
|
MultiExternal(Vec<(PathBuf, DirectoryOwnership, Cow<'ast, ast::Mod>)>),
|
2019-06-08 18:47:18 +09:00
|
|
|
/// `#[path = "..."] mod foo {}`
|
|
|
|
InternalWithPath(PathBuf),
|
|
|
|
/// `mod foo {}`
|
2019-06-09 09:20:39 +09:00
|
|
|
Internal(&'a ast::Item),
|
2019-06-08 18:47:18 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
|
2019-03-17 12:25:59 +09:00
|
|
|
/// Creates a new `ModResolver`.
|
2019-05-09 20:37:51 +02:00
|
|
|
pub(crate) fn new(
|
2019-06-08 18:47:18 +09:00
|
|
|
parse_sess: &'sess ParseSess,
|
2019-03-17 12:25:59 +09:00
|
|
|
directory_ownership: DirectoryOwnership,
|
2019-06-07 14:55:41 +09:00
|
|
|
recursive: bool,
|
2019-03-17 12:25:59 +09:00
|
|
|
) -> Self {
|
|
|
|
ModResolver {
|
|
|
|
directory: Directory {
|
|
|
|
path: PathBuf::new(),
|
|
|
|
ownership: directory_ownership,
|
|
|
|
},
|
|
|
|
file_map: BTreeMap::new(),
|
2019-06-08 18:47:18 +09:00
|
|
|
parse_sess,
|
2019-06-07 14:55:41 +09:00
|
|
|
recursive,
|
2019-03-17 12:25:59 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a map that maps a file name to the module in AST.
|
2019-06-08 18:47:18 +09:00
|
|
|
pub(crate) fn visit_crate(
|
|
|
|
mut self,
|
|
|
|
krate: &'ast ast::Crate,
|
|
|
|
) -> Result<FileModMap<'ast>, String> {
|
2020-03-26 21:25:34 -05:00
|
|
|
let root_filename = self.parse_sess.span_to_filename(krate.span);
|
2019-03-17 12:25:59 +09:00
|
|
|
self.directory.path = match root_filename {
|
2020-03-26 21:25:34 -05:00
|
|
|
FileName::Real(ref p) => p.parent().unwrap_or(Path::new("")).to_path_buf(),
|
2019-03-17 12:25:59 +09:00
|
|
|
_ => PathBuf::new(),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Skip visiting sub modules when the input is from stdin.
|
2019-06-07 14:55:41 +09:00
|
|
|
if self.recursive {
|
2019-06-08 18:47:18 +09:00
|
|
|
self.visit_mod_from_ast(&krate.module)?;
|
2019-03-17 12:25:59 +09:00
|
|
|
}
|
|
|
|
|
2019-06-09 09:20:39 +09:00
|
|
|
self.file_map
|
2020-03-26 21:25:34 -05:00
|
|
|
.insert(root_filename, Cow::Borrowed(&krate.module));
|
2019-03-17 12:25:59 +09:00
|
|
|
Ok(self.file_map)
|
|
|
|
}
|
|
|
|
|
2019-06-09 09:20:39 +09:00
|
|
|
/// Visit `cfg_if` macro and look for module declarations.
|
|
|
|
fn visit_cfg_if(&mut self, item: Cow<'ast, ast::Item>) -> Result<(), String> {
|
2020-03-26 23:18:16 -05:00
|
|
|
let mut visitor = visitor::CfgIfVisitor::new(self.parse_sess);
|
2019-06-08 18:47:18 +09:00
|
|
|
visitor.visit_item(&item);
|
|
|
|
for module_item in visitor.mods() {
|
2019-10-05 23:40:24 +09:00
|
|
|
if let ast::ItemKind::Mod(ref sub_mod) = module_item.item.kind {
|
2019-09-02 04:36:51 -05:00
|
|
|
self.visit_sub_mod(&module_item.item, Cow::Owned(sub_mod.clone()))?;
|
2019-06-08 18:47:18 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Visit modules defined inside macro calls.
|
2019-06-09 09:20:39 +09:00
|
|
|
fn visit_mod_outside_ast(&mut self, module: ast::Mod) -> Result<(), String> {
|
|
|
|
for item in module.items {
|
|
|
|
if is_cfg_if(&item) {
|
|
|
|
self.visit_cfg_if(Cow::Owned(item.into_inner()))?;
|
|
|
|
continue;
|
2019-06-08 18:47:18 +09:00
|
|
|
}
|
|
|
|
|
2019-10-05 23:40:24 +09:00
|
|
|
if let ast::ItemKind::Mod(ref sub_mod) = item.kind {
|
2019-06-09 09:20:39 +09:00
|
|
|
self.visit_sub_mod(&item, Cow::Owned(sub_mod.clone()))?;
|
2019-03-17 12:25:59 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-06-08 18:47:18 +09:00
|
|
|
/// Visit modules from AST.
|
|
|
|
fn visit_mod_from_ast(&mut self, module: &'ast ast::Mod) -> Result<(), String> {
|
|
|
|
for item in &module.items {
|
2019-06-09 09:20:39 +09:00
|
|
|
if is_cfg_if(item) {
|
|
|
|
self.visit_cfg_if(Cow::Borrowed(item))?;
|
2019-06-08 18:47:18 +09:00
|
|
|
}
|
|
|
|
|
2019-10-05 23:40:24 +09:00
|
|
|
if let ast::ItemKind::Mod(ref sub_mod) = item.kind {
|
2019-06-09 09:20:39 +09:00
|
|
|
self.visit_sub_mod(item, Cow::Borrowed(sub_mod))?;
|
2019-06-08 18:47:18 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_sub_mod(
|
|
|
|
&mut self,
|
|
|
|
item: &'c ast::Item,
|
2019-06-09 09:20:39 +09:00
|
|
|
sub_mod: Cow<'ast, ast::Mod>,
|
2019-06-08 18:47:18 +09:00
|
|
|
) -> Result<(), String> {
|
2019-06-09 09:20:39 +09:00
|
|
|
let old_directory = self.directory.clone();
|
|
|
|
let sub_mod_kind = self.peek_sub_mod(item, &sub_mod)?;
|
|
|
|
if let Some(sub_mod_kind) = sub_mod_kind {
|
|
|
|
self.insert_sub_mod(sub_mod_kind.clone(), sub_mod.clone())?;
|
|
|
|
self.visit_sub_mod_inner(sub_mod, sub_mod_kind)?;
|
2019-06-08 18:47:18 +09:00
|
|
|
}
|
2019-06-09 09:20:39 +09:00
|
|
|
self.directory = old_directory;
|
2019-06-08 18:47:18 +09:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Inspect the given sub-module which we are about to visit and returns its kind.
|
2019-06-09 09:20:39 +09:00
|
|
|
fn peek_sub_mod(
|
|
|
|
&self,
|
|
|
|
item: &'c ast::Item,
|
|
|
|
sub_mod: &Cow<'ast, ast::Mod>,
|
|
|
|
) -> Result<Option<SubModKind<'c, 'ast>>, String> {
|
2019-06-08 18:47:18 +09:00
|
|
|
if contains_skip(&item.attrs) {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
|
|
|
if is_mod_decl(item) {
|
|
|
|
// mod foo;
|
|
|
|
// Look for an extern file.
|
2019-06-09 09:20:39 +09:00
|
|
|
self.find_external_module(item.ident, &item.attrs, sub_mod)
|
2019-06-08 18:47:18 +09:00
|
|
|
} else {
|
|
|
|
// An internal module (`mod foo { /* ... */ }`);
|
|
|
|
if let Some(path) = find_path_value(&item.attrs) {
|
2020-02-08 22:21:37 -06:00
|
|
|
let path = Path::new(&*path.as_str()).to_path_buf();
|
2019-06-08 18:47:18 +09:00
|
|
|
Ok(Some(SubModKind::InternalWithPath(path)))
|
|
|
|
} else {
|
2019-06-09 09:20:39 +09:00
|
|
|
Ok(Some(SubModKind::Internal(item)))
|
2019-06-08 18:47:18 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-09 09:20:39 +09:00
|
|
|
fn insert_sub_mod(
|
|
|
|
&mut self,
|
|
|
|
sub_mod_kind: SubModKind<'c, 'ast>,
|
2020-03-26 23:18:16 -05:00
|
|
|
_sub_mod: Cow<'ast, ast::Mod>,
|
2019-06-09 09:20:39 +09:00
|
|
|
) -> Result<(), String> {
|
|
|
|
match sub_mod_kind {
|
2020-03-26 23:18:16 -05:00
|
|
|
SubModKind::External(mod_path, _, sub_mod) => {
|
|
|
|
self.file_map
|
|
|
|
.entry(FileName::Real(mod_path))
|
|
|
|
.or_insert(sub_mod);
|
2019-06-09 09:20:39 +09:00
|
|
|
}
|
|
|
|
SubModKind::MultiExternal(mods) => {
|
|
|
|
for (mod_path, _, sub_mod) in mods {
|
2020-03-26 23:18:16 -05:00
|
|
|
self.file_map
|
|
|
|
.entry(FileName::Real(mod_path))
|
|
|
|
.or_insert(sub_mod);
|
2019-06-09 09:20:39 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_sub_mod_inner(
|
|
|
|
&mut self,
|
|
|
|
sub_mod: Cow<'ast, ast::Mod>,
|
|
|
|
sub_mod_kind: SubModKind<'c, 'ast>,
|
|
|
|
) -> Result<(), String> {
|
|
|
|
match sub_mod_kind {
|
2020-03-26 23:18:16 -05:00
|
|
|
SubModKind::External(mod_path, directory_ownership, sub_mod) => {
|
2019-06-09 09:20:39 +09:00
|
|
|
let directory = Directory {
|
|
|
|
path: mod_path.parent().unwrap().to_path_buf(),
|
|
|
|
ownership: directory_ownership,
|
|
|
|
};
|
|
|
|
self.visit_sub_mod_after_directory_update(sub_mod, Some(directory))
|
|
|
|
}
|
|
|
|
SubModKind::InternalWithPath(mod_path) => {
|
|
|
|
// All `#[path]` files are treated as though they are a `mod.rs` file.
|
|
|
|
let directory = Directory {
|
|
|
|
path: mod_path,
|
|
|
|
ownership: DirectoryOwnership::Owned { relative: None },
|
|
|
|
};
|
|
|
|
self.visit_sub_mod_after_directory_update(sub_mod, Some(directory))
|
|
|
|
}
|
|
|
|
SubModKind::Internal(ref item) => {
|
|
|
|
self.push_inline_mod_directory(item.ident, &item.attrs);
|
|
|
|
self.visit_sub_mod_after_directory_update(sub_mod, None)
|
|
|
|
}
|
|
|
|
SubModKind::MultiExternal(mods) => {
|
|
|
|
for (mod_path, directory_ownership, sub_mod) in mods {
|
|
|
|
let directory = Directory {
|
|
|
|
path: mod_path.parent().unwrap().to_path_buf(),
|
|
|
|
ownership: directory_ownership,
|
|
|
|
};
|
|
|
|
self.visit_sub_mod_after_directory_update(sub_mod, Some(directory))?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_sub_mod_after_directory_update(
|
|
|
|
&mut self,
|
|
|
|
sub_mod: Cow<'ast, ast::Mod>,
|
|
|
|
directory: Option<Directory>,
|
|
|
|
) -> Result<(), String> {
|
|
|
|
if let Some(directory) = directory {
|
|
|
|
self.directory = directory;
|
|
|
|
}
|
|
|
|
match sub_mod {
|
|
|
|
Cow::Borrowed(sub_mod) => self.visit_mod_from_ast(sub_mod),
|
|
|
|
Cow::Owned(sub_mod) => self.visit_mod_outside_ast(sub_mod),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-08 18:47:18 +09:00
|
|
|
/// Find a file path in the filesystem which corresponds to the given module.
|
2019-03-17 12:25:59 +09:00
|
|
|
fn find_external_module(
|
|
|
|
&self,
|
2020-05-19 17:31:28 +09:00
|
|
|
mod_name: symbol::Ident,
|
2019-03-17 12:25:59 +09:00
|
|
|
attrs: &[ast::Attribute],
|
2019-06-09 09:20:39 +09:00
|
|
|
sub_mod: &Cow<'ast, ast::Mod>,
|
2020-03-26 23:18:16 -05:00
|
|
|
) -> Result<Option<SubModKind<'c, 'ast>>, String> {
|
|
|
|
let relative = match self.directory.ownership {
|
|
|
|
DirectoryOwnership::Owned { relative } => relative,
|
|
|
|
DirectoryOwnership::UnownedViaBlock | DirectoryOwnership::UnownedViaMod => None,
|
|
|
|
};
|
2020-03-26 21:25:34 -05:00
|
|
|
if let Some(path) = Parser::submod_path_from_attr(attrs, &self.directory.path) {
|
2020-03-26 23:18:16 -05:00
|
|
|
if self.parse_sess.is_file_parsed(&path) {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
2020-03-27 21:47:03 -05:00
|
|
|
return match Parser::parse_file_as_module(self.parse_sess, &path, sub_mod.inner) {
|
2020-06-08 10:31:25 +09:00
|
|
|
Some((_, ref attrs)) if contains_skip(attrs) => Ok(None),
|
|
|
|
Some((m, _)) => Ok(Some(SubModKind::External(
|
2020-03-26 23:18:16 -05:00
|
|
|
path,
|
|
|
|
DirectoryOwnership::Owned { relative: None },
|
|
|
|
Cow::Owned(m),
|
|
|
|
))),
|
|
|
|
None => Err(format!(
|
|
|
|
"Failed to find module {} in {:?} {:?}",
|
|
|
|
mod_name, self.directory.path, relative,
|
|
|
|
)),
|
|
|
|
};
|
2019-03-17 12:25:59 +09:00
|
|
|
}
|
|
|
|
|
2019-06-09 09:20:39 +09:00
|
|
|
// Look for nested path, like `#[cfg_attr(feature = "foo", path = "bar.rs")]`.
|
2020-03-26 21:25:34 -05:00
|
|
|
let mut mods_outside_ast = self.find_mods_outside_of_ast(attrs, sub_mod);
|
2019-06-09 09:20:39 +09:00
|
|
|
|
2020-03-26 21:25:34 -05:00
|
|
|
match self
|
|
|
|
.parse_sess
|
|
|
|
.default_submod_path(mod_name, relative, &self.directory.path)
|
|
|
|
.result
|
2019-03-17 12:25:59 +09:00
|
|
|
{
|
2020-03-26 21:25:34 -05:00
|
|
|
Ok(ModulePathSuccess {
|
2020-03-26 23:18:16 -05:00
|
|
|
path, ownership, ..
|
|
|
|
}) => {
|
|
|
|
let outside_mods_empty = mods_outside_ast.is_empty();
|
|
|
|
let should_insert = !mods_outside_ast
|
|
|
|
.iter()
|
|
|
|
.any(|(outside_path, _, _)| outside_path == &path);
|
|
|
|
if self.parse_sess.is_file_parsed(&path) {
|
|
|
|
if outside_mods_empty {
|
|
|
|
return Ok(None);
|
|
|
|
} else {
|
|
|
|
if should_insert {
|
|
|
|
mods_outside_ast.push((path, ownership, sub_mod.clone()));
|
|
|
|
}
|
|
|
|
return Ok(Some(SubModKind::MultiExternal(mods_outside_ast)));
|
|
|
|
}
|
|
|
|
}
|
2020-03-27 21:47:03 -05:00
|
|
|
match Parser::parse_file_as_module(self.parse_sess, &path, sub_mod.inner) {
|
2020-06-08 10:31:25 +09:00
|
|
|
Some((_, ref attrs)) if contains_skip(attrs) => Ok(None),
|
|
|
|
Some((m, _)) if outside_mods_empty => {
|
2020-03-27 21:47:03 -05:00
|
|
|
Ok(Some(SubModKind::External(path, ownership, Cow::Owned(m))))
|
|
|
|
}
|
2020-06-08 10:31:25 +09:00
|
|
|
Some((m, _)) => {
|
2020-03-26 23:18:16 -05:00
|
|
|
mods_outside_ast.push((path.clone(), ownership, Cow::Owned(m)));
|
|
|
|
if should_insert {
|
|
|
|
mods_outside_ast.push((path, ownership, sub_mod.clone()));
|
|
|
|
}
|
|
|
|
Ok(Some(SubModKind::MultiExternal(mods_outside_ast)))
|
|
|
|
}
|
|
|
|
None if outside_mods_empty => Err(format!(
|
|
|
|
"Failed to find module {} in {:?} {:?}",
|
|
|
|
mod_name, self.directory.path, relative,
|
|
|
|
)),
|
|
|
|
None => {
|
|
|
|
if should_insert {
|
|
|
|
mods_outside_ast.push((path, ownership, sub_mod.clone()));
|
|
|
|
}
|
|
|
|
Ok(Some(SubModKind::MultiExternal(mods_outside_ast)))
|
2020-03-27 21:47:03 -05:00
|
|
|
}
|
2020-03-26 23:18:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(mut e) if !mods_outside_ast.is_empty() => {
|
|
|
|
e.cancel();
|
|
|
|
Ok(Some(SubModKind::MultiExternal(mods_outside_ast)))
|
|
|
|
}
|
|
|
|
Err(mut e) => {
|
|
|
|
e.cancel();
|
|
|
|
Err(format!(
|
|
|
|
"Failed to find module {} in {:?} {:?}",
|
|
|
|
mod_name, self.directory.path, relative,
|
|
|
|
))
|
2019-06-09 09:20:39 +09:00
|
|
|
}
|
2019-03-17 12:25:59 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 17:31:28 +09:00
|
|
|
fn push_inline_mod_directory(&mut self, id: symbol::Ident, attrs: &[ast::Attribute]) {
|
2019-03-17 12:25:59 +09:00
|
|
|
if let Some(path) = find_path_value(attrs) {
|
2020-02-08 22:21:37 -06:00
|
|
|
self.directory.path.push(&*path.as_str());
|
2019-03-17 12:25:59 +09:00
|
|
|
self.directory.ownership = DirectoryOwnership::Owned { relative: None };
|
|
|
|
} else {
|
|
|
|
// We have to push on the current module name in the case of relative
|
|
|
|
// paths in order to ensure that any additional module paths from inline
|
|
|
|
// `mod x { ... }` come after the relative extension.
|
|
|
|
//
|
|
|
|
// For example, a `mod z { ... }` inside `x/y.rs` should set the current
|
|
|
|
// directory path to `/x/y/z`, not `/x/z` with a relative offset of `y`.
|
|
|
|
if let DirectoryOwnership::Owned { relative } = &mut self.directory.ownership {
|
|
|
|
if let Some(ident) = relative.take() {
|
|
|
|
// remove the relative offset
|
2020-02-08 22:21:37 -06:00
|
|
|
self.directory.path.push(&*ident.as_str());
|
2019-03-17 12:25:59 +09:00
|
|
|
}
|
|
|
|
}
|
2020-02-08 22:21:37 -06:00
|
|
|
self.directory.path.push(&*id.as_str());
|
2019-03-17 12:25:59 +09:00
|
|
|
}
|
2017-12-08 14:16:47 +01:00
|
|
|
}
|
2019-06-09 09:20:39 +09:00
|
|
|
|
2020-03-26 21:25:34 -05:00
|
|
|
fn find_mods_outside_of_ast(
|
2019-06-09 09:20:39 +09:00
|
|
|
&self,
|
|
|
|
attrs: &[ast::Attribute],
|
|
|
|
sub_mod: &Cow<'ast, ast::Mod>,
|
|
|
|
) -> Vec<(PathBuf, DirectoryOwnership, Cow<'ast, ast::Mod>)> {
|
|
|
|
// Filter nested path, like `#[cfg_attr(feature = "foo", path = "bar.rs")]`.
|
|
|
|
let mut path_visitor = visitor::PathVisitor::default();
|
|
|
|
for attr in attrs.iter() {
|
|
|
|
if let Some(meta) = attr.meta() {
|
|
|
|
path_visitor.visit_meta_item(&meta)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let mut result = vec![];
|
|
|
|
for path in path_visitor.paths() {
|
|
|
|
let mut actual_path = self.directory.path.clone();
|
|
|
|
actual_path.push(&path);
|
|
|
|
if !actual_path.exists() {
|
|
|
|
continue;
|
|
|
|
}
|
2020-03-26 21:25:34 -05:00
|
|
|
if self.parse_sess.is_file_parsed(&actual_path) {
|
|
|
|
// If the specified file is already parsed, then we just use that.
|
2019-06-09 09:20:39 +09:00
|
|
|
result.push((
|
|
|
|
actual_path,
|
|
|
|
DirectoryOwnership::Owned { relative: None },
|
|
|
|
sub_mod.clone(),
|
|
|
|
));
|
|
|
|
continue;
|
|
|
|
}
|
2020-03-27 21:47:03 -05:00
|
|
|
let m = match Parser::parse_file_as_module(self.parse_sess, &actual_path, sub_mod.inner)
|
|
|
|
{
|
2020-06-08 10:31:25 +09:00
|
|
|
Some((_, ref attrs)) if contains_skip(attrs) => continue,
|
|
|
|
Some((m, _)) => m,
|
2020-03-26 21:25:34 -05:00
|
|
|
None => continue,
|
2019-06-09 09:20:39 +09:00
|
|
|
};
|
2020-03-26 21:25:34 -05:00
|
|
|
|
2019-06-09 09:20:39 +09:00
|
|
|
result.push((
|
|
|
|
actual_path,
|
|
|
|
DirectoryOwnership::Owned { relative: None },
|
|
|
|
Cow::Owned(m),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
2015-07-26 12:55:25 +02:00
|
|
|
}
|
|
|
|
|
2018-08-06 22:28:05 +09:00
|
|
|
fn path_value(attr: &ast::Attribute) -> Option<Symbol> {
|
2019-06-03 23:57:02 +09:00
|
|
|
if attr.check_name(sym::path) {
|
2018-08-06 22:28:05 +09:00
|
|
|
attr.value_str()
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-19 02:56:42 +00:00
|
|
|
// N.B., even when there are multiple `#[path = ...]` attributes, we just need to
|
2018-08-06 22:34:58 +09:00
|
|
|
// examine the first one, since rustc ignores the second and the subsequent ones
|
|
|
|
// as unused attributes.
|
2018-08-06 22:28:05 +09:00
|
|
|
fn find_path_value(attrs: &[ast::Attribute]) -> Option<Symbol> {
|
|
|
|
attrs.iter().flat_map(path_value).next()
|
|
|
|
}
|
2019-06-09 09:20:39 +09:00
|
|
|
|
|
|
|
fn is_cfg_if(item: &ast::Item) -> bool {
|
2019-10-05 23:40:24 +09:00
|
|
|
match item.kind {
|
2020-03-26 23:18:16 -05:00
|
|
|
ast::ItemKind::MacCall(ref mac) => {
|
2019-09-06 22:41:03 +09:00
|
|
|
if let Some(first_segment) = mac.path.segments.first() {
|
2020-03-26 21:25:34 -05:00
|
|
|
if first_segment.ident.name == *CFG_IF {
|
2019-09-02 04:36:51 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
2019-06-09 09:20:39 +09:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|