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-02-08 22:54:37 -06:00
|
|
|
use rustc_errors::PResult;
|
2020-02-08 22:21:37 -06:00
|
|
|
use rustc_parse::{new_sub_parser_from_file, parser, DirectoryOwnership};
|
|
|
|
use rustc_session::parse::ParseSess;
|
|
|
|
use rustc_span::symbol::{sym, Symbol};
|
|
|
|
use rustc_span::{source_map, Span, DUMMY_SP};
|
2015-07-26 12:55:25 +02:00
|
|
|
use syntax::ast;
|
2020-02-08 22:21:37 -06:00
|
|
|
use syntax::token::TokenKind;
|
2019-06-08 18:47:18 +09:00
|
|
|
use syntax::visit::Visitor;
|
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;
|
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
|
|
|
|
|
|
|
/// 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
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct Directory {
|
|
|
|
path: PathBuf,
|
|
|
|
ownership: DirectoryOwnership,
|
|
|
|
}
|
|
|
|
|
2019-06-08 18:47:18 +09:00
|
|
|
impl<'a> Directory {
|
2020-03-26 15:26:58 -05:00
|
|
|
fn to_syntax_directory(&'a self) -> rustc_parse::Directory {
|
2020-02-08 22:21:37 -06:00
|
|
|
rustc_parse::Directory {
|
2020-03-26 15:26:58 -05:00
|
|
|
path: self.path.clone(),
|
2019-06-08 18:47:18 +09:00
|
|
|
ownership: self.ownership.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-09 09:20:39 +09:00
|
|
|
#[derive(Clone)]
|
|
|
|
enum SubModKind<'a, 'ast> {
|
2019-06-08 18:47:18 +09:00
|
|
|
/// `mod foo;`
|
|
|
|
External(PathBuf, DirectoryOwnership),
|
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> {
|
|
|
|
let root_filename = self.parse_sess.source_map().span_to_filename(krate.span);
|
2019-03-17 12:25:59 +09:00
|
|
|
self.directory.path = match root_filename {
|
|
|
|
source_map::FileName::Real(ref path) => path
|
|
|
|
.parent()
|
|
|
|
.expect("Parent directory should exists")
|
|
|
|
.to_path_buf(),
|
|
|
|
_ => 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
|
|
|
|
.insert(root_filename.into(), 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> {
|
2019-06-08 18:47:18 +09:00
|
|
|
let mut visitor =
|
|
|
|
visitor::CfgIfVisitor::new(self.parse_sess, self.directory.to_syntax_directory());
|
|
|
|
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)
|
|
|
|
.map(Some)
|
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>,
|
|
|
|
sub_mod: Cow<'ast, ast::Mod>,
|
|
|
|
) -> Result<(), String> {
|
|
|
|
match sub_mod_kind {
|
|
|
|
SubModKind::External(mod_path, _) => {
|
|
|
|
self.file_map.insert(FileName::Real(mod_path), sub_mod);
|
|
|
|
}
|
|
|
|
SubModKind::MultiExternal(mods) => {
|
|
|
|
for (mod_path, _, sub_mod) in mods {
|
|
|
|
self.file_map.insert(FileName::Real(mod_path), sub_mod);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
SubModKind::External(mod_path, directory_ownership) => {
|
|
|
|
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,
|
|
|
|
mod_name: ast::Ident,
|
|
|
|
attrs: &[ast::Attribute],
|
2019-06-09 09:20:39 +09:00
|
|
|
sub_mod: &Cow<'ast, ast::Mod>,
|
|
|
|
) -> Result<SubModKind<'c, 'ast>, String> {
|
2019-03-17 12:25:59 +09:00
|
|
|
if let Some(path) = parser::Parser::submod_path_from_attr(attrs, &self.directory.path) {
|
2019-06-09 09:20:39 +09:00
|
|
|
return Ok(SubModKind::External(
|
|
|
|
path,
|
|
|
|
DirectoryOwnership::Owned { relative: None },
|
|
|
|
));
|
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")]`.
|
|
|
|
let mut mods_outside_ast = self
|
|
|
|
.find_mods_ouside_of_ast(attrs, sub_mod)
|
|
|
|
.unwrap_or(vec![]);
|
|
|
|
|
2019-03-17 12:25:59 +09:00
|
|
|
let relative = match self.directory.ownership {
|
|
|
|
DirectoryOwnership::Owned { relative } => relative,
|
2020-02-08 22:21:37 -06:00
|
|
|
DirectoryOwnership::UnownedViaBlock | DirectoryOwnership::UnownedViaMod => None,
|
2017-12-08 14:16:47 +01:00
|
|
|
};
|
2019-03-17 12:25:59 +09:00
|
|
|
match parser::Parser::default_submod_path(
|
|
|
|
mod_name,
|
|
|
|
relative,
|
|
|
|
&self.directory.path,
|
2019-06-08 18:47:18 +09:00
|
|
|
self.parse_sess.source_map(),
|
2019-03-17 12:25:59 +09:00
|
|
|
)
|
|
|
|
.result
|
|
|
|
{
|
|
|
|
Ok(parser::ModulePathSuccess {
|
|
|
|
path,
|
|
|
|
directory_ownership,
|
|
|
|
..
|
2019-06-09 09:20:39 +09:00
|
|
|
}) => Ok(if mods_outside_ast.is_empty() {
|
|
|
|
SubModKind::External(path, directory_ownership)
|
|
|
|
} else {
|
|
|
|
mods_outside_ast.push((path, directory_ownership, sub_mod.clone()));
|
|
|
|
SubModKind::MultiExternal(mods_outside_ast)
|
|
|
|
}),
|
|
|
|
Err(_) if !mods_outside_ast.is_empty() => {
|
|
|
|
Ok(SubModKind::MultiExternal(mods_outside_ast))
|
|
|
|
}
|
2019-03-17 12:25:59 +09:00
|
|
|
Err(_) => Err(format!(
|
|
|
|
"Failed to find module {} in {:?} {:?}",
|
|
|
|
mod_name, self.directory.path, relative,
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn push_inline_mod_directory(&mut self, id: ast::Ident, attrs: &[ast::Attribute]) {
|
|
|
|
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
|
|
|
|
|
|
|
fn find_mods_ouside_of_ast(
|
|
|
|
&self,
|
|
|
|
attrs: &[ast::Attribute],
|
|
|
|
sub_mod: &Cow<'ast, ast::Mod>,
|
|
|
|
) -> Option<Vec<(PathBuf, DirectoryOwnership, Cow<'ast, ast::Mod>)>> {
|
|
|
|
use std::panic::{catch_unwind, AssertUnwindSafe};
|
|
|
|
Some(
|
|
|
|
catch_unwind(AssertUnwindSafe(|| {
|
|
|
|
self.find_mods_ouside_of_ast_inner(attrs, sub_mod)
|
|
|
|
}))
|
|
|
|
.ok()?,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn find_mods_ouside_of_ast_inner(
|
|
|
|
&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-02-08 22:21:37 -06:00
|
|
|
let file_name = rustc_span::FileName::Real(actual_path.clone());
|
2019-06-09 09:20:39 +09:00
|
|
|
if self
|
|
|
|
.parse_sess
|
|
|
|
.source_map()
|
|
|
|
.get_source_file(&file_name)
|
|
|
|
.is_some()
|
|
|
|
{
|
|
|
|
// If the specfied file is already parsed, then we just use that.
|
|
|
|
result.push((
|
|
|
|
actual_path,
|
|
|
|
DirectoryOwnership::Owned { relative: None },
|
|
|
|
sub_mod.clone(),
|
|
|
|
));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let mut parser = new_sub_parser_from_file(
|
|
|
|
self.parse_sess,
|
|
|
|
&actual_path,
|
|
|
|
self.directory.ownership,
|
|
|
|
None,
|
|
|
|
DUMMY_SP,
|
|
|
|
);
|
|
|
|
parser.cfg_mods = false;
|
2019-06-09 21:28:57 +09:00
|
|
|
let lo = parser.token.span;
|
2019-06-09 09:20:39 +09:00
|
|
|
// FIXME(topecongiro) Format inner attributes (#3606).
|
|
|
|
let _mod_attrs = match parse_inner_attributes(&mut parser) {
|
|
|
|
Ok(attrs) => attrs,
|
|
|
|
Err(mut e) => {
|
|
|
|
e.cancel();
|
|
|
|
parser.sess.span_diagnostic.reset_err_count();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let m = match parse_mod_items(&mut parser, lo) {
|
|
|
|
Ok(m) => m,
|
|
|
|
Err(mut e) => {
|
|
|
|
e.cancel();
|
|
|
|
parser.sess.span_diagnostic.reset_err_count();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
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
|
|
|
|
|
|
|
// FIXME(topecongiro) Use the method from libsyntax[1] once it become public.
|
|
|
|
//
|
|
|
|
// [1] https://github.com/rust-lang/rust/blob/master/src/libsyntax/parse/attr.rs
|
|
|
|
fn parse_inner_attributes<'a>(parser: &mut parser::Parser<'a>) -> PResult<'a, Vec<ast::Attribute>> {
|
|
|
|
let mut attrs: Vec<ast::Attribute> = vec![];
|
|
|
|
loop {
|
2019-06-09 21:28:57 +09:00
|
|
|
match parser.token.kind {
|
|
|
|
TokenKind::Pound => {
|
2019-06-09 09:20:39 +09:00
|
|
|
// Don't even try to parse if it's not an inner attribute.
|
2019-06-09 21:28:57 +09:00
|
|
|
if !parser.look_ahead(1, |t| t == &TokenKind::Not) {
|
2019-06-09 09:20:39 +09:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
let attr = parser.parse_attribute(true)?;
|
|
|
|
assert_eq!(attr.style, ast::AttrStyle::Inner);
|
|
|
|
attrs.push(attr);
|
|
|
|
}
|
2019-06-09 21:28:57 +09:00
|
|
|
TokenKind::DocComment(s) => {
|
2019-06-09 09:20:39 +09:00
|
|
|
// we need to get the position of this token before we bump.
|
2020-02-08 22:21:37 -06:00
|
|
|
let attr = syntax::attr::mk_doc_comment(
|
|
|
|
syntax::util::comments::doc_comment_style(&s.as_str()),
|
|
|
|
s,
|
|
|
|
parser.token.span,
|
|
|
|
);
|
2019-06-09 09:20:39 +09:00
|
|
|
if attr.style == ast::AttrStyle::Inner {
|
|
|
|
attrs.push(attr);
|
|
|
|
parser.bump();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(attrs)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_mod_items<'a>(parser: &mut parser::Parser<'a>, inner_lo: Span) -> PResult<'a, ast::Mod> {
|
|
|
|
let mut items = vec![];
|
|
|
|
while let Some(item) = parser.parse_item()? {
|
|
|
|
items.push(item);
|
|
|
|
}
|
|
|
|
|
2019-06-09 21:28:57 +09:00
|
|
|
let hi = if parser.token.span.is_dummy() {
|
2019-06-09 09:20:39 +09:00
|
|
|
inner_lo
|
|
|
|
} else {
|
2020-03-26 17:20:24 -05:00
|
|
|
parser.prev_token.span
|
2019-06-09 09:20:39 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(ast::Mod {
|
|
|
|
inner: inner_lo.to(hi),
|
|
|
|
items,
|
|
|
|
inline: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_cfg_if(item: &ast::Item) -> bool {
|
2019-10-05 23:40:24 +09:00
|
|
|
match item.kind {
|
2019-09-02 04:36:51 -05:00
|
|
|
ast::ItemKind::Mac(ref mac) => {
|
2019-09-06 22:41:03 +09:00
|
|
|
if let Some(first_segment) = mac.path.segments.first() {
|
2019-09-02 04:36:51 -05:00
|
|
|
if first_segment.ident.name == Symbol::intern("cfg_if") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
2019-06-09 09:20:39 +09:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|