use super::super::item_head::{
BlockModule, GroupKind, attribute_path, cfg_attr_metas, cfg_written_before, is_cfg_if_arm,
is_outer_attribute, item_at_keyword, macro_group_kind, owner_start,
};
use super::super::path_vocab::canonical_segment;
use super::super::token_tree::{Delimiter, Kind, Node, TokenTree};
pub(super) struct DeclaredModule {
pub(super) name: String,
pub(super) body: Option<(usize, usize)>,
pub(super) direct_path: Option<Option<String>>,
pub(super) direct_path_is_conditional: bool,
pub(super) conditional_paths: Vec<String>,
pub(super) is_cfg_conditional: bool,
pub(super) declared_in_block: bool,
}
#[derive(Default)]
struct PathAttributes {
direct: Option<Option<String>>,
direct_after_candidate: bool,
conditional: Vec<(usize, String)>,
cfg_written: bool,
}
pub(super) fn declared_modules_in(
tree: &TokenTree,
range: std::ops::Range<usize>,
) -> Vec<DeclaredModule> {
let mut declared: Vec<(usize, DeclaredModule)> = Vec::new();
let mut work: Vec<(usize, usize)> = vec![(range.start, range.end.min(tree.len()))];
while let Some((from, to)) = work.pop() {
let mut i = from;
while i < to {
let node = tree.node_at(i);
if let Node::Macro { open, close, .. } = node {
if macro_group_kind(tree, open) == Some(GroupKind::CfgIf) {
work.extend(
cfg_if_arms(tree, open, close).map(|(open, close)| (open + 1, close)),
);
}
i = close + 1;
continue;
}
let head = (tree.kind(i) == Kind::Keyword && tree.text(i) == "mod")
.then(|| item_at_keyword(tree, i))
.flatten();
let Some((head, name)) = head.and_then(|head| head.name.map(|name| (head, name)))
else {
i = node.last() + 1;
continue;
};
let body = head.body.map(|open| (open, tree.partner(open)));
if body.is_none() && !tree.is(name + 1, ";") {
i = node.last() + 1;
continue;
}
let attributes = attributes_before(tree, head.start);
i = body.map_or(name + 2, |(_, close)| close + 1);
declared.push((
head.start,
DeclaredModule {
name: canonical_segment(tree.text(name)).to_string(),
body,
direct_path: attributes.direct,
direct_path_is_conditional: attributes.direct_after_candidate,
conditional_paths: attributes
.conditional
.into_iter()
.map(|(_, value)| value)
.collect(),
is_cfg_conditional: body.is_none()
&& (attributes.cfg_written || may_be_compiled_out(tree, head.start)),
declared_in_block: false,
},
));
}
}
declared.sort_by_key(|(at, _)| *at);
declared.into_iter().map(|(_, module)| module).collect()
}
fn cfg_if_arms<'t>(
tree: &'t TokenTree,
open: usize,
close: usize,
) -> impl Iterator<Item = (usize, usize)> + 't {
let mut k = open + 1;
std::iter::from_fn(move || {
while k < close {
let child = tree.node_at(k);
k = child.last() + 1;
if let Node::Group { open, close } = child {
if is_cfg_if_arm(tree, open) {
return Some((open, close));
}
}
}
None
})
}
fn may_be_compiled_out(tree: &TokenTree, at: usize) -> bool {
let mut group = tree.enclosing(at);
while let Some(open) = group {
let outer = tree.enclosing(open);
if is_cfg_if_arm(tree, open) || attributes_before(tree, owner_start(tree, open)).cfg_written
{
return true;
}
group = outer;
}
false
}
pub(super) fn block_path_modules(
tree: &TokenTree,
blocks: &[BlockModule],
range: std::ops::Range<usize>,
) -> Vec<DeclaredModule> {
let end = range.end.min(tree.len());
let owner = range.start.checked_sub(1).filter(|&open| {
tree.kind(open) == Kind::Open(Delimiter::Brace) && tree.partner(open) == end
});
let mut declared = Vec::new();
for BlockModule {
head,
name_at,
owner: declared_under,
label: block,
} in blocks
{
let name_at = *name_at;
if *declared_under != owner || !(range.start..end).contains(&head.keyword_at) {
continue;
}
let name = canonical_segment(tree.text(name_at)).to_string();
let body = head.body.map(|open| (open, tree.partner(open)));
if body.is_none() && !tree.is(name_at + 1, ";") {
continue;
}
let attributes = attributes_before(tree, head.start);
if body.is_none() && attributes.direct.is_none() && attributes.conditional.is_empty() {
continue;
}
declared.push(DeclaredModule {
name: format!("{block}::{name}"),
body,
direct_path: attributes.direct,
direct_path_is_conditional: attributes.direct_after_candidate,
conditional_paths: attributes
.conditional
.into_iter()
.map(|(_, value)| value)
.collect(),
is_cfg_conditional: body.is_none()
&& (attributes.cfg_written || may_be_compiled_out(tree, head.start)),
declared_in_block: true,
});
}
declared
}
fn attributes_before(tree: &TokenTree, start: usize) -> PathAttributes {
let mut attributes = Vec::new();
let mut k = start;
while let Some(node @ Node::Attribute { open, close, .. }) = tree.node_before(k) {
if !is_outer_attribute(node) {
break;
}
attributes.push((open + 1, close));
k = node.first();
}
let mut found = PathAttributes {
cfg_written: cfg_written_before(tree, start),
..PathAttributes::default()
};
let mut direct_at = None;
let mut metas: Vec<(usize, usize, bool)> = attributes
.into_iter()
.map(|(start, end)| (start, end, false))
.collect();
while let Some((start, end, applied)) = metas.pop() {
match attribute_path(tree, start, end) {
(Some("path"), after) if tree.is(after, "=") && !applied => {
if direct_at.is_none() {
direct_at = Some(start);
found.direct = Some(tree.string_value(after + 1));
}
}
(Some("path"), after) if tree.is(after, "=") => {
if let Some(value) = tree.string_value(after + 1) {
found.conditional.push((start, value));
}
}
(Some("cfg_attr"), after) if tree.kind(after) == Kind::Open(Delimiter::Parenthesis) => {
metas.extend(
cfg_attr_metas(tree, after)
.into_iter()
.rev()
.map(|(meta, end)| (meta, end, true)),
);
}
_ => {}
}
}
if let Some(direct_at) = direct_at {
found.conditional.retain(|(at, _)| *at < direct_at);
found.direct_after_candidate = !found.conditional.is_empty();
}
found.conditional.sort_by_key(|(at, _)| *at);
found
}
#[cfg(test)]
fn declared_modules_with_kind(source: &str) -> Vec<(String, bool)> {
let tree = TokenTree::lex(source, super::super::token_tree::Edition::Rust2018);
declared_modules_in(&tree, 0..tree.len())
.into_iter()
.map(|declared| (declared.name, declared.body.is_some()))
.collect()
}
#[cfg(test)]
pub(super) fn declared_modules(source: &str) -> Vec<String> {
declared_modules_with_kind(source)
.into_iter()
.map(|(name, _)| name)
.collect()
}
#[cfg(test)]
mod tests {
use super::super::super::token_tree::Edition;
use super::*;
fn attributes(source: &str) -> PathAttributes {
let tree = TokenTree::lex(source, Edition::Rust2018);
let at = (0..tree.len())
.rev()
.find(|&i| tree.is(i, "mod"))
.expect("a mod");
attributes_before(&tree, at)
}
#[test]
fn both_readers_take_the_attribute_name_from_one_position() {
for (prefix, remaps, cfg_written) in [
("#[path = \"x.rs\"]", true, false),
("#[cfg(unix)]", false, true),
("# [ path = \"x.rs\"]", true, false),
("# [ cfg(unix)]", false, true),
("#[r#path = \"x.rs\"]", true, false),
("#[r#cfg(unix)]", false, true),
("#[r#cfg_attr(unix, path = \"x.rs\")]", true, false),
("#[path]", false, false),
("#[path(\"x.rs\")]", false, false),
("##[path = \"x.rs\"]", true, false),
("#![cfg(unix)]", false, false),
("#![path = \"x.rs\"]", false, false),
("mod tests { #![cfg(test)]", false, false),
] {
let found = attributes(&format!("{prefix} mod m;"));
let remapped = found.direct.is_some() || !found.conditional.is_empty();
assert_eq!(remapped, remaps, "the path reader's verdict on {prefix}");
assert_eq!(
found.cfg_written, cfg_written,
"the cfg reader's verdict on {prefix}"
);
}
}
#[test]
fn deeply_nested_cfg_attr_paths_use_a_bounded_native_stack() {
const DEPTH: usize = 4096;
let mut nested = String::from("#[cfg_attr");
for _ in 0..DEPTH {
nested.push_str("(predicate, cfg_attr");
}
nested.push_str("(predicate, path = \"target.rs\")");
for _ in 0..=DEPTH {
nested.push(')');
}
nested.push_str("] mod m;");
let found = attributes(&nested);
assert_eq!(
found.conditional.len(),
1,
"the deepest path candidate remains observable"
);
assert_eq!(found.conditional[0].1, "target.rs");
}
#[test]
fn iterative_cfg_attr_collection_preserves_textual_candidate_order() {
let found = attributes(
"#[cfg_attr(a, cfg_attr(b, path = \"first.rs\"), path = \"second.rs\", cfg_attr(c, path = \"third.rs\"))] mod m;",
);
let values: Vec<&str> = found.conditional.iter().map(|(_, v)| v.as_str()).collect();
assert_eq!(values, ["first.rs", "second.rs", "third.rs"]);
}
#[test]
fn a_qualified_look_alike_is_not_the_built_in_cfg_attr() {
for (label, attr, expected) in [
(
"plain",
"#[cfg_attr(any(), foo::cfg_attr(a, path = \"bogus\"), path = \"real.rs\")]",
1,
),
(
"raw identifier",
"#[cfg_attr(any(), foo::r#cfg_attr(a, path = \"bogus\"), path = \"real.rs\")]",
1,
),
(
"nested plain",
"#[cfg_attr(any(), cfg_attr(a, path = \"nested.rs\"))]",
1,
),
(
"nested raw",
"#[cfg_attr(any(), r#cfg_attr(a, path = \"nested.rs\"))]",
1,
),
] {
let found = attributes(&format!("{attr} mod m;"));
assert_eq!(
found.conditional.len(),
expected,
"{label}: {:?}",
found.conditional
);
assert!(
found.conditional.iter().all(|(_, v)| v != "bogus"),
"{label}"
);
}
}
#[test]
fn an_unreadable_path_value_is_not_an_absent_path() {
assert_eq!(
attributes("#[path = concat!(\"a\")] mod m;").direct,
Some(None)
);
assert_eq!(attributes("#[path = \"a\\q\"] mod m;").direct, Some(None));
assert_eq!(
attributes("#[path = r#\"a.rs\"#] mod m;").direct,
Some(Some("a.rs".into()))
);
assert_eq!(
attributes("#[path = \"a\\\n b.rs\"] mod m;").direct,
Some(Some("ab.rs".into()))
);
assert_eq!(attributes("mod m;").direct, None);
}
}