use std::cell::OnceCell;
use std::collections::{BTreeSet, HashMap};
use std::path::{Path, PathBuf};
use crate::finding::ModuleFact;
use super::glob_hazard::glob_reaches_prefix;
use super::item_head::ItemKeyword;
use super::occurrence::{Occurrence, occurrences};
use super::path_vocab::{
PathSite, SymbolPrefix, canonical_module_path, names_a_block_item, path_within,
};
use super::resolve::{CrateScopes, Named, Namespace, with_rest};
use super::scope_tree::{DeclKind, ScopeKind, ScopeTable};
use super::source_texts::SourceTexts;
use super::token_tree::{Edition, TokenTree};
use super::use_scan::{
ClassifiedLeaf, FileUse, ImportedPath, classify_uses, external_imports, file_uses,
internal_imports,
};
use super::use_tree::{UseLeaf, macro_use_statements, use_statements};
pub(crate) struct InlineFinding {
pub fact: ModuleFact,
pub file: String,
}
struct FileScan {
file: PathBuf,
module: String,
occurrences: Vec<Occurrence>,
uses: Vec<FileUse>,
classified: OnceCell<Result<Vec<ClassifiedLeaf>, String>>,
internal: OnceCell<Vec<(String, ImportedPath)>>,
external: OnceCell<Vec<(String, String)>>,
macro_uses: Vec<FileUse>,
}
impl FileScan {
fn read(
raw: &str,
file: &Path,
module: &str,
edition: Edition,
table: usize,
) -> (FileScan, ScopeTable) {
let tree = TokenTree::lex(raw, edition);
let table = ScopeTable::build(&tree, module, table);
let statements = use_statements(&tree);
let macro_statements: Vec<_> = macro_use_statements(&tree)
.into_iter()
.filter(|statement| statement.leaves.is_ok())
.collect();
let mut spans: Vec<(usize, usize)> = statements
.iter()
.chain(¯o_statements)
.map(|statement| (statement.at, statement.end))
.collect();
spans.sort_unstable();
let scan = FileScan {
file: file.to_path_buf(),
module: module.to_string(),
occurrences: occurrences(&tree, &spans),
uses: file_uses(statements, &table),
classified: OnceCell::new(),
internal: OnceCell::new(),
external: OnceCell::new(),
macro_uses: file_uses(macro_statements, &table),
};
(scan, table)
}
}
pub(crate) struct UnitScan {
files: Vec<FileScan>,
index: HashMap<(PathBuf, String), usize>,
#[cfg(test)]
classifications: std::cell::RefCell<HashMap<(PathBuf, String), usize>>,
scopes: CrateScopes,
}
impl UnitScan {
pub(crate) fn read(
sources: &SourceTexts,
all_files: &[(PathBuf, String)],
edition: Edition,
proc_macro: bool,
dependencies: BTreeSet<String>,
) -> Result<Self, String> {
let mut files = Vec::new();
let mut index = HashMap::new();
let mut tables = Vec::new();
for (file, module) in all_files {
let raw = sources
.text(file)
.map_err(|err| crate::errors::unreadable_governed_file_error(file, &err))?;
let (scan, table) = FileScan::read(&raw, file, module, edition, tables.len());
index
.entry((file.clone(), module.clone()))
.or_insert(files.len());
files.push(scan);
tables.push(table);
}
let scopes = CrateScopes::new(tables, edition).depending_on(dependencies);
Ok(UnitScan {
files,
index,
#[cfg(test)]
classifications: std::cell::RefCell::new(HashMap::new()),
scopes: if proc_macro {
scopes.in_a_proc_macro_crate()
} else {
scopes
},
})
}
pub(crate) fn imports(
&self,
file: &Path,
module: &str,
) -> Result<&[(String, ImportedPath)], String> {
let t = self.file_index(file, module)?;
let uses = self
.classified(t)
.map_err(|refusal| crate::errors::scan_refusal_in_file(file, refusal))?;
Ok(self.files[t]
.internal
.get_or_init(|| internal_imports(uses)))
}
pub(crate) fn external_imports(
&self,
file: &Path,
module: &str,
) -> Result<&[(String, String)], String> {
let t = self.file_index(file, module)?;
let uses = self
.classified(t)
.map_err(|refusal| crate::errors::scan_refusal_in_file(file, refusal))?;
Ok(self.files[t]
.external
.get_or_init(|| external_imports(uses)))
}
fn classified(&self, t: usize) -> Result<&[ClassifiedLeaf], &String> {
self.files[t]
.classified
.get_or_init(|| self.classify(t))
.as_ref()
.map(Vec::as_slice)
}
fn classify(&self, t: usize) -> Result<Vec<ClassifiedLeaf>, String> {
let scan = &self.files[t];
#[cfg(test)]
{
*self
.classifications
.borrow_mut()
.entry((scan.file.clone(), scan.module.clone()))
.or_default() += 1;
}
classify_uses(&self.scopes, t, &scan.uses)
}
#[cfg(test)]
pub(crate) fn classifications(&self) -> HashMap<(PathBuf, String), usize> {
self.classifications.borrow().clone()
}
fn every_table_judged(&self) -> Result<(), String> {
for (t, file) in self.files.iter().enumerate() {
if let Some(refusal) = self.scopes.table(t).refusal() {
return Err(crate::errors::scan_refusal_in_file(&file.file, refusal));
}
}
Ok(())
}
fn file_index(&self, file: &Path, module: &str) -> Result<usize, String> {
self.index
.get(&(file.to_path_buf(), module.to_string()))
.copied()
.ok_or_else(|| {
crate::errors::scan_refusal_in_file(
file,
&format!(
"it is governed as `{module}`, which the unit scan did not read it as"
),
)
})
}
pub(crate) fn value_items(&self, file: &Path, module: &str) -> Result<Vec<String>, String> {
let t = self.file_index(file, module)?;
let mut out = Vec::new();
for scope in &self.scopes.table(t).scopes {
if scope.kind != ScopeKind::Module {
continue;
}
for (name, declarations) in &scope.declarations {
let value_item = declarations.iter().any(|declaration| {
matches!(
declaration.kind,
DeclKind::Item(ItemKeyword::Fn | ItemKeyword::Const | ItemKeyword::Static)
)
});
if value_item {
out.push(format!("{}::{name}", scope.module));
}
}
}
Ok(out)
}
pub(crate) fn item_definitions(&self) -> BTreeSet<String> {
let mut items = BTreeSet::new();
for t in 0..self.files.len() {
for scope in &self.scopes.table(t).scopes {
if scope.kind != ScopeKind::Module || names_a_block_item(&scope.module) {
continue;
}
for name in scope.declarations.keys() {
items.insert(format!("{}::{name}", scope.module));
}
}
}
items
}
pub(crate) fn findings(
&self,
governed: &[(PathBuf, String)],
prefix: &SymbolPrefix,
ending_with: Option<&[String]>,
strict: bool,
external: bool,
dependency_names: &[String],
) -> Result<Vec<InlineFinding>, String> {
let judge = Judgement {
scopes: &self.scopes,
prefix: prefix.path.as_str(),
verbs: ending_with.map(|vs| vs.iter().map(|v| canonical_module_path(v)).collect()),
strict,
external_dependencies: external.then(|| dependency_names.iter().cloned().collect()),
};
self.every_table_judged()?;
let mut findings: Vec<InlineFinding> = Vec::new();
for (governed_file, governed_module) in governed {
let t = self.file_index(governed_file, governed_module)?;
let scan = &self.files[t];
judge
.file(t, scan, &mut findings)
.map_err(|refusal| crate::errors::scan_refusal_in_file(&scan.file, &refusal))?;
}
findings.sort_by(|a, b| a.fact.cmp(&b.fact).then(a.file.cmp(&b.file)));
findings.dedup_by(|a, b| a.fact == b.fact);
Ok(findings)
}
}
struct Judgement<'a> {
scopes: &'a CrateScopes,
prefix: &'a str,
verbs: Option<Vec<String>>,
strict: bool,
external_dependencies: Option<BTreeSet<String>>,
}
impl Judgement<'_> {
fn file(
&self,
t: usize,
scan: &FileScan,
findings: &mut Vec<InlineFinding>,
) -> Result<(), String> {
let table = self.scopes.table(t);
let file = scan.file.display().to_string();
let dependencies = self.external_dependencies.as_ref();
for (scope, written) in table.globs() {
self.glob(t, scope, written, scan, &file, findings)?;
}
for occurrence in &scan.occurrences {
if !is_judged(self.strict, occurrence.is_call) {
continue;
}
let scope = table.scope_at(occurrence.at);
let ns = if occurrence.is_call {
Namespace::Value
} else {
Namespace::Either
};
for resolved in resolve_written(
&occurrence.segments,
PathSite::Expr,
ns,
self.scopes,
t,
scope,
dependencies,
)? {
if should_react_on_occurrence(self.strict, self.verbs.as_deref(), &resolved) {
self.report(resolved, scan, &file, findings);
}
}
}
if self.strict {
for (file_use, in_macro) in scan
.uses
.iter()
.map(|file_use| (file_use, false))
.chain(scan.macro_uses.iter().map(|file_use| (file_use, true)))
{
for leaf in file_use.leaves.as_ref().map_err(Clone::clone)? {
let written = match leaf {
UseLeaf::Name { path, .. } => path,
UseLeaf::SelfLeaf { module, .. } | UseLeaf::Empty(module) => module,
UseLeaf::Glob(base) if in_macro => {
self.glob(t, file_use.scope, base, scan, &file, findings)?;
continue;
}
UseLeaf::Glob(_) => continue,
};
for resolved in resolve_written(
written,
PathSite::Use,
Namespace::Either,
self.scopes,
t,
file_use.scope,
dependencies,
)? {
self.report(resolved, scan, &file, findings);
}
}
}
}
Ok(())
}
fn glob(
&self,
t: usize,
scope: u32,
written: &str,
scan: &FileScan,
file: &str,
findings: &mut Vec<InlineFinding>,
) -> Result<(), String> {
let table = self.scopes.table(t);
for glob in resolve_written(
written,
PathSite::Use,
Namespace::Type,
self.scopes,
t,
scope,
self.external_dependencies.as_ref(),
)? {
if glob_reaches_prefix(
self.scopes,
&glob,
self.prefix,
&table.scopes[scope as usize].module,
)? {
findings.push(InlineFinding {
fact: ModuleFact::InlineGlob {
path: glob,
module: scan.module.clone(),
},
file: file.to_string(),
});
}
}
Ok(())
}
fn report(
&self,
resolved: String,
scan: &FileScan,
file: &str,
findings: &mut Vec<InlineFinding>,
) {
if path_within(&resolved, self.prefix) {
findings.push(InlineFinding {
fact: ModuleFact::InlinePath {
path: resolved,
module: scan.module.clone(),
},
file: file.to_string(),
});
}
}
}
fn resolve_written(
written: &str,
site: PathSite,
ns: Namespace,
scopes: &CrateScopes,
t: usize,
scope: u32,
external_dependencies: Option<&BTreeSet<String>>,
) -> Result<Vec<String>, String> {
match scopes.name(t, scope, written, site, ns) {
Named::Paths(paths) => Ok(paths),
Named::External(path) => Ok(external_dependencies.map(|_| path).into_iter().collect()),
Named::Unbound { head, rest, also } => {
let dependency =
external_dependencies.is_some_and(|dependencies| dependencies.contains(&head));
let path = with_rest(head, &rest);
Ok(also.into_iter().chain(dependency.then_some(path)).collect())
}
Named::Local | Named::Invalid => Ok(Vec::new()),
Named::PastCap(refusal) => Err(refusal),
}
}
fn is_judged(strict: bool, is_call: bool) -> bool {
strict || is_call
}
fn should_react_on_occurrence(strict: bool, verbs: Option<&[String]>, resolved: &str) -> bool {
if strict {
true
} else if let Some(verbs) = verbs {
let leaf = resolved
.rsplit_once("::")
.map_or(resolved, |(_, leaf)| leaf);
verbs.iter().any(|v| v == leaf)
} else {
true
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_file_cut_off_anywhere_is_read_to_its_end() {
let source = "\u{feff}#!/usr/bin/env run\n\
#![allow(unused)]\n\
use crate::a::{b, c::*, d as e, f::{self, g}};\n\
pub(in crate::a) type A<'a, T> = &'a mut (crate::x::Y<T>);\n\
type B = *const ::std::cell::Cell<u8>;\n\
#[cfg_attr(unix, path = \"u.rs\")] #[path = \"v.rs\"] mod m;\n\
#[cfg(any())] extern crate core as std;\n\
unsafe extern \"C\" { pub fn ext(x: i32) -> i32; }\n\
pub union U { a: u8, b: crate::y::Z }\n\
pub enum E { A(crate::y::Z), B { f: u8 } = 1 }\n\
pub trait Tr: for<'a> Fn(&'a u8) { const C: u8; fn f(&self) -> <Self as Tr>::X; }\n\
impl<T: crate::t::Tr> crate::t::Tr for S<T> where T: Copy {}\n\
macro_rules! m { ($x:expr) => { crate::q::f($x) }; }\n\
mod inner { use super::*; pub fn h() { if 1 << 2 < 3 { <u8 as crate::k::K>::k(); } } }\n\
pub fn run() { let r#ref = 1; crate::z::<u8>::f(&mut r#ref); std::process::id(); \
let _ = (b'x', b\"y\", r#\"z\"#, c\"w\", '\\u{1F600}'); /* outer /* nested */ still */ }\n\
const S: &str = \"unterminated";
for cut in (0..=source.len()).filter(|&cut| source.is_char_boundary(cut)) {
for edition in [Edition::Rust2015, Edition::Rust2018, Edition::Rust2021] {
FileScan::read(&source[..cut], Path::new("lib.rs"), "crate", edition, 0);
}
}
}
}