use std::collections::{HashMap, HashSet, VecDeque};
use std::path::{Path, PathBuf};
use std::sync::{Arc, RwLock};
use crate::cache::SummaryKey;
use crate::file::coordinates::{FileOffset, ReadingOffset};
use crate::file::paths::{DiskFiles, FileProvider, OverlayFiles, normalize_path};
use crate::file::vfs::Vfs;
use crate::file::view::{FileView, TokensOf};
use crate::include::config::{CompileCommands, CompilerConfig, project_config_from_flags};
use crate::include::toolchain::{self, DiskCommands, Environment, Toolchain};
use crate::index::project::{
MemberCompletions, MemberList, NameCompletions, ProjectDefinition, ProjectDefinitions,
ProjectIndex, ProjectMacro, substituted_members,
};
use crate::index::references::{MacroReferences, ReferenceBudget, macro_references};
use crate::index::store::{StoreStats, SummaryStore};
use crate::index::watch::{ChangeBatch, FileEvent, Response, WatchFilter};
use crate::index::worklist::StepOutcome;
use crate::index::worklist::{Priority, Step, outcome_of};
use crate::index::{
FileIndexer, definition_across_files, definitions_across_files, macro_across_files,
member_completions_at, members_of,
};
use crate::inlay::ParameterHint;
use crate::macros::MacroTable;
use crate::project::{ConfigReport, ProjectDiscovery};
use crate::sema::modules::{ImportOutcome, ModuleScanner};
use crate::summary::{FileSummary, OutlineSymbol, UnitReading};
use crate::symbol::{Known, UnknownReason};
use crate::{PathInterner, cache};
#[allow(dead_code)]
const SHARING_FILE: &str = "sharing.bin";
const COOK_SLICE: usize = 4;
pub struct DiagTrace(Option<(&'static str, std::time::Instant)>);
impl DiagTrace {
pub fn new(what: &'static str) -> DiagTrace {
DiagTrace(crate::probes::diagnostics().then(|| (what, std::time::Instant::now())))
}
}
impl Drop for DiagTrace {
fn drop(&mut self) {
if let Some((what, started)) = self.0 {
eprintln!(
" [diagnostics] {what:<28} {:>7} ms",
started.elapsed().as_millis()
);
}
}
}
const RE_READ_SLICE: usize = 16;
const MACRO_SLICE: usize = 1;
const UNTIL_A_UNIT_MAY_BE_READ: std::time::Duration = std::time::Duration::from_secs(1);
const PROGRAM_READ_ATTEMPTS: u8 = 3;
const THE_LARGEST_PROGRAM_READ_AT_ONCE: usize = 8 * 1024 * 1024;
const COOK_TWO_LEVELS_FURTHER: usize = 24;
const COOK_ONE_LEVEL_FURTHER: usize = 64;
const MAX_PROJECT_FILES: usize = 10_000;
const SOURCE_EXTENSIONS: &[&str] = &[
"c", "cc", "cpp", "cxx", "c++", "h", "hh", "hpp", "hxx", "h++", "ipp", "inl", "tpp", "tcc",
"cppm", "ixx",
];
pub type SessionFiles<F = DiskFiles> = OverlayFiles<OpenDocuments, F>;
#[derive(Debug, Clone, Default)]
pub struct OpenDocuments {
buffers: Arc<RwLock<HashMap<String, String>>>,
}
impl OpenDocuments {
pub fn new() -> Self {
OpenDocuments::default()
}
pub fn open(&self, path: impl AsRef<Path>, text: impl Into<String>) {
self.buffers
.write()
.expect("the buffer map is not poisoned")
.insert(normalize_path(path.as_ref(), cfg!(windows)), text.into());
}
pub fn close(&self, path: impl AsRef<Path>) -> bool {
self.buffers
.write()
.expect("the buffer map is not poisoned")
.remove(&normalize_path(path.as_ref(), cfg!(windows)))
.is_some()
}
pub fn text(&self, path: impl AsRef<Path>) -> Option<String> {
self.buffers
.read()
.expect("the buffer map is not poisoned")
.get(&normalize_path(path.as_ref(), cfg!(windows)))
.cloned()
}
pub fn is_open(&self, path: impl AsRef<Path>) -> bool {
self.buffers
.read()
.expect("the buffer map is not poisoned")
.contains_key(&normalize_path(path.as_ref(), cfg!(windows)))
}
pub fn paths(&self) -> Vec<PathBuf> {
self.buffers
.read()
.expect("the buffer map is not poisoned")
.keys()
.map(PathBuf::from)
.collect()
}
pub fn len(&self) -> usize {
self.buffers
.read()
.expect("the buffer map is not poisoned")
.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
impl FileProvider for OpenDocuments {
fn read(&self, path: &Path) -> Option<String> {
self.text(path)
}
fn exists(&self, path: &Path) -> bool {
self.is_open(path)
}
fn is_open_buffer(&self, path: &Path) -> bool {
self.is_open(path)
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct UnitStats {
pub from_memory: usize,
pub from_disk: usize,
pub walked: usize,
}
#[derive(Debug, Clone, Copy)]
enum Which {
FromMemory,
FromDisk,
Walked,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct CacheSizes {
pub renderings: usize,
pub rendering_bytes: usize,
pub units: usize,
pub summaries: usize,
pub facts: usize,
pub macros: usize,
pub cooked_files: usize,
pub cooked_declarations: usize,
}
pub struct Session<F: FileProvider = DiskFiles> {
root: PathBuf,
config: CompilerConfig,
toolchain: Option<Toolchain>,
discovery: ProjectDiscovery,
files: SessionFiles<F>,
documents: OpenDocuments,
vfs: Vfs<SessionFiles<F>>,
store: SummaryStore<SessionFiles<F>>,
filter: WatchFilter,
project: Vec<PathBuf>,
queue: Work,
parsed_since_the_last_pass: Vec<PathBuf>,
cooking: Cooking,
definitions: std::sync::Mutex<crate::MacroDefinitions>,
units_read: std::collections::HashSet<String>,
programs_wanted: std::collections::HashSet<String>,
units_attempted: std::collections::HashMap<String, u8>,
last_edit: std::time::Instant,
unit_index: std::sync::Mutex<Option<(u64, crate::IndexedUnit)>>,
rendered_unit: std::sync::Mutex<Option<(u64, crate::RenderedUnit)>>,
directive_signatures:
std::collections::HashMap<String, crate::preprocess::directive::DirectiveSignature>,
prefetched: HashMap<String, crate::index::store::Prepared>,
units: std::sync::Mutex<UnitTable>,
walked_by_content: std::sync::Mutex<
cache::cache_lru::Cache<(u64, u64), std::sync::Arc<crate::TranslationUnit>>,
>,
headers: crate::SharedHeaders,
macro_environments: std::sync::Mutex<
cache::cache_lru::Cache<(PathBuf, u64), std::sync::Arc<cpp_parser::MacroEnvironment>>,
>,
macro_work: std::sync::Mutex<Cooking>,
surfaces: std::sync::Mutex<crate::effect::Surfaces>,
effects: std::sync::Mutex<HashMap<u64, crate::effect::Effect>>,
renderings: std::sync::Mutex<
cache::cache_lru::Cache<
(PathBuf, u64),
std::sync::Arc<crate::preprocess::cooked::RenderedCooked>,
>,
>,
rendering_summaries: std::sync::Mutex<
cache::cache_lru::Cache<(PathBuf, u64), std::sync::Arc<crate::FileSummary>>,
>,
cooking_refused: std::sync::Mutex<std::collections::HashSet<String>>,
unit_from_memory: std::sync::atomic::AtomicUsize,
unit_from_disk: std::sync::atomic::AtomicUsize,
unit_walked: std::sync::atomic::AtomicUsize,
walks_by_root: std::sync::Mutex<std::collections::HashMap<String, u64>>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModuleImport {
pub name: String,
pub range: cpp_parser::SourceRange,
pub resolved: Option<PathBuf>,
}
impl Session<DiskFiles> {
pub fn open(
root: impl Into<PathBuf>,
files: SessionFiles<DiskFiles>,
filter: WatchFilter,
) -> Session<DiskFiles> {
Session::open_with_config_file(root.into(), files, filter, None)
}
pub fn open_with_config_file(
root: PathBuf,
files: SessionFiles<DiskFiles>,
filter: WatchFilter,
config_file: Option<&Path>,
) -> Session<DiskFiles> {
let discovery = crate::project::discover(&files, &root, config_file);
let project_config = discovery.config.clone();
if let Some(locale) = project_config.config.locale.as_deref() {
cpp_parser::set_locale(locale);
}
let filter = apply_project_config(filter, &project_config);
let for_file = discovery
.database
.as_ref()
.and_then(|database| database.commands.commands.first())
.map(|command| command.file.clone())
.unwrap_or_else(|| root.clone());
let base = project_config_from_flags(
&discovery.flags(),
discovery.working_directory(),
&project_config.config.compile,
);
let base = CompilerConfig {
root: Some(root.to_path_buf()),
fuzzy_includes: project_config
.config
.compile
.fuzzy_includes
.unwrap_or(discovery.database.is_none()),
targets: crate::project::targets_for(&project_config.config.compile),
database: discovery
.database
.as_ref()
.map(|database| std::sync::Arc::new(database.commands.clone())),
remove_args: project_config.config.compile.remove_args.clone(),
extra_args: project_config.config.compile.extra_args.clone(),
..base
};
let base = match (&project_config.config.compile.standard, &base.standard) {
(Some(stated), _) => base.with_standard(stated.clone()),
(None, _) => base,
};
let base = match (base.standard.clone(), discovery.standard()) {
(None, Some(standard)) => base.with_standard(standard),
_ => base,
};
let environment = Environment::current();
let layout = crate::include::msvc::WindowsLayout::current();
let named = discovery.named_compilers();
let toolchain = toolchain::discover_with(
&files,
&DiskCommands,
&named,
toolchain::BuildStatement {
commands: discovery
.database
.as_ref()
.map(|database| &database.commands),
standard: base.standard.as_deref(),
},
&for_file,
&environment,
&layout,
);
let config = match &toolchain {
Some(found) => found.config(&base),
None => base,
};
let config = match &project_config.config.compile.cuda {
Some(cuda) => cuda.applied_to(config, &root),
None => config,
};
let config = match toolchain.as_ref().and_then(|found| found.dialect) {
Some(dialect) => config.with_dialect(dialect),
None => config,
};
let session = Session::assemble(root, files, filter, config, toolchain, discovery);
session
}
}
pub struct RenderedCook {
pub text_hash: u64,
pub indexed: crate::IndexedRendering,
pub reading: CookedReading,
}
pub struct CookingMaterials {
pub path: PathBuf,
pub key: SummaryKey,
pub unit: std::sync::Arc<crate::TranslationUnit>,
pub text: String,
pub text_hash: u64,
pub seed: MacroTable,
pub already_declared: std::collections::HashSet<String>,
}
#[derive(Default)]
pub struct PreparedWave {
pub prepared: Vec<(PathBuf, crate::index::store::Prepared)>,
pub units: Vec<std::sync::Arc<crate::TranslationUnit>>,
}
#[derive(Default)]
pub struct Drain {
files: Vec<PathBuf>,
units: Vec<std::sync::Arc<crate::TranslationUnit>>,
environments: Vec<PathBuf>,
}
impl Drain {
pub fn is_empty(&self) -> bool {
self.files.is_empty() && self.environments.is_empty()
}
}
#[derive(Default)]
pub struct PreparedDrain {
re_read: Option<crate::index::store::ReRead>,
rendered: Vec<(
(PathBuf, u64),
std::sync::Arc<crate::preprocess::cooked::RenderedCooked>,
)>,
}
pub struct PreparedUnitReading {
root: PathBuf,
program: u64,
indexed: crate::IndexedUnit,
}
pub struct UnitReadMaterials<F: FileProvider> {
root: PathBuf,
key: SummaryKey,
units_key: u64,
unit: std::sync::Arc<crate::TranslationUnit>,
sources: HashMap<PathBuf, String>,
macros: crate::Marked,
config: CompilerConfig,
files: F,
database: Option<std::sync::Arc<cache::cache_db::SummaryCache>>,
program: Option<u64>,
rendered: Option<crate::RenderedUnit>,
known: Option<crate::IndexedUnit>,
}
impl<F: FileProvider> UnitReadMaterials<F> {
pub fn root(&self) -> &Path {
&self.root
}
pub fn read_the_program(self) -> Option<PreparedUnitReading> {
if let Some(indexed) = self.known {
return Some(PreparedUnitReading {
root: self.root,
program: self.program?,
indexed,
});
}
let stream = match self.rendered {
Some(rendered) => rendered,
None => {
let definitions = self.unit.definitions();
let seed = MacroTable::from_marked(&self.macros);
let search = crate::preprocess::cooked::Search::new(&self.files, &self.config);
let stream = self.unit.cook_the_unit(
&self.sources,
&definitions,
Some(&seed),
true,
Some(&search),
);
if let Some(database) = &self.database {
let _ = database.put_unit(self.units_key, &stream);
}
stream
}
};
let program = crate::cache::content_hash(&stream.text);
if let Some(database) = &self.database
&& let Ok(Some(known)) = database.indexed_unit(program)
{
return Some(PreparedUnitReading {
root: self.root,
program,
indexed: known,
});
}
let indexer = FileIndexer::new(&self.files, &self.config);
let indexed = indexer.index_unit_rendering(&self.root, &stream, self.key);
if let Some(database) = &self.database {
let _ = database.put_indexed_unit(program, &indexed);
}
Some(PreparedUnitReading {
root: self.root,
program,
indexed,
})
}
}
struct SessionReads<'a, F: FileProvider> {
files: &'a F,
index: &'a crate::ProjectIndex,
}
impl<F: FileProvider> crate::effect::Reads for SessionReads<'_, F> {
fn read(&self, path: &std::path::Path) -> Option<String> {
self.files.read(path)
}
fn resolve(&self, from: &std::path::Path, spelling: &str) -> Option<PathBuf> {
let wanted = spelling.trim_matches(|c| c == '"' || c == '<' || c == '>');
let summary = self.index.summary(from)?;
summary
.includes
.iter()
.find(|include| include.spelling.trim() == wanted)
.and_then(|include| include.resolved.clone())
}
}
impl<F: FileProvider + Clone> Session<F> {
pub fn with_config(
root: impl Into<PathBuf>,
files: SessionFiles<F>,
filter: WatchFilter,
config: CompilerConfig,
) -> Session<F> {
let root = root.into();
let config_report = crate::project::load_config(&files, &root, None);
let filter = apply_project_config(filter, &config_report);
let discovery = ProjectDiscovery {
root: root.clone(),
config: config_report,
database: None,
problems: Vec::new(),
};
Session::assemble(root, files, filter, config, None, discovery)
}
fn assemble(
root: PathBuf,
files: SessionFiles<F>,
filter: WatchFilter,
config: CompilerConfig,
toolchain: Option<Toolchain>,
discovery: ProjectDiscovery,
) -> Session<F> {
let project_config = discovery.config.clone();
let database = discovery
.database
.as_ref()
.map(|database| &database.commands);
let documents = files.overlay.clone();
let mut extra_extensions = project_config.config.workspace.source_extensions.clone();
if config.cuda() {
extra_extensions.extend(["cu".to_string(), "cuh".to_string()]);
}
let project = project_files(
&files,
&filter,
&root,
database,
&extra_extensions,
project_config
.config
.index
.max_files
.unwrap_or(MAX_PROJECT_FILES),
);
let configured = the_compilation_is_known(toolchain.as_ref());
let mut store = SummaryStore::with_provider(root.clone(), config.clone(), files.clone())
.with_macros(crate::index::environment::compilation_environment(
&config,
toolchain.as_ref(),
configured,
));
if let Some(cache_dir) = &project_config.config.index.cache_dir {
store = store.with_cache_directory(cache_dir);
}
let headers = crate::completion::header_index(&config, &root, project.clone());
let mut session = Session {
root: root.clone(),
config,
toolchain,
discovery,
vfs: Vfs::new(files.clone()),
files,
documents,
store,
filter,
project: project.clone(),
queue: Work::default(),
parsed_since_the_last_pass: Vec::new(),
cooking: Cooking::default(),
definitions: std::sync::Mutex::new(crate::MacroDefinitions::default()),
units: std::sync::Mutex::new(UnitTable::default()),
walked_by_content: std::sync::Mutex::new(cache::cache_lru::Cache::new(
MAX_WALKS_BY_CONTENT_ENTRIES,
MAX_WALKS_BY_CONTENT_BYTES,
)),
units_read: std::collections::HashSet::new(),
programs_wanted: std::collections::HashSet::new(),
units_attempted: std::collections::HashMap::new(),
unit_index: std::sync::Mutex::new(None),
rendered_unit: std::sync::Mutex::new(None),
last_edit: std::time::Instant::now(),
directive_signatures: std::collections::HashMap::new(),
prefetched: HashMap::new(),
headers,
macro_work: std::sync::Mutex::new(Cooking::default()),
macro_environments: std::sync::Mutex::new(cache::cache_lru::Cache::macro_environments()),
surfaces: std::sync::Mutex::new(crate::effect::Surfaces::new()),
effects: std::sync::Mutex::new(HashMap::new()),
renderings: std::sync::Mutex::new(cache::cache_lru::Cache::renderings()),
rendering_summaries: std::sync::Mutex::new(cache::cache_lru::Cache::new(
RENDERING_SUMMARY_ENTRIES,
RENDERING_SUMMARY_BYTES,
)),
cooking_refused: std::sync::Mutex::new(std::collections::HashSet::new()),
unit_from_memory: std::sync::atomic::AtomicUsize::new(0),
unit_from_disk: std::sync::atomic::AtomicUsize::new(0),
unit_walked: std::sync::atomic::AtomicUsize::new(0),
walks_by_root: std::sync::Mutex::new(std::collections::HashMap::new()),
};
for path in session.project.clone() {
session.queue.add(path, Priority::Rest, 0);
}
session
}
pub fn root(&self) -> &Path {
&self.root
}
pub fn config_for(&self, file: &std::path::Path) -> CompilerConfig {
self.config().for_file(file)
}
pub fn config(&self) -> &CompilerConfig {
&self.config
}
pub fn project_config(&self) -> &ConfigReport {
&self.discovery.config
}
pub fn discovery(&self) -> &ProjectDiscovery {
&self.discovery
}
pub fn toolchain(&self) -> Option<&Toolchain> {
self.toolchain.as_ref()
}
pub fn compile_database(&self) -> Option<&CompileCommands> {
self.discovery
.database
.as_ref()
.map(|database| &database.commands)
}
pub fn documents(&self) -> &OpenDocuments {
&self.documents
}
pub fn index(&self) -> &ProjectIndex {
self.store.index()
}
pub fn directive_summary(&self, path: &Path) -> Option<crate::FileSummary> {
self.store
.directive_summary(path)
.map(|summary| (*summary).clone())
}
pub fn parsed_summary(&self, path: &Path) -> Option<crate::FileSummary> {
self.store.parsed_summary(path)
}
pub fn summary_database_sizes(&self) -> Option<cache::cache_db::DatabaseSizes> {
self.store.summary_database_sizes()
}
pub fn collect_the_cache(&mut self) -> Option<cache::cache_db::Collected> {
self.store.collect_the_cache()
}
pub fn flush_summaries(&self) {
self.store.flush_summaries();
}
pub fn cache_sizes(&self) -> CacheSizes {
let (renderings, rendering_bytes) = self
.renderings
.lock()
.map(|known| {
let bytes = known
.values()
.map(|rendering| rendering.text.len())
.sum::<usize>();
(known.len(), bytes)
})
.unwrap_or((0, 0));
let summaries = self.store.index().summaries().count();
let facts = self
.store
.index()
.summaries()
.map(|summary| summary.declarations.len())
.sum();
let macros = self
.store
.index()
.summaries()
.map(|summary| summary.macros.len())
.sum();
let (cooked_files, cooked_declarations) = self.store.index().cooked_size();
CacheSizes {
renderings,
rendering_bytes,
units: self.held_units().held.len(),
summaries,
facts,
macros,
cooked_files,
cooked_declarations,
}
}
pub fn walk_contributions_held(&self) -> usize {
self.store.contributions().held()
}
pub fn memory_report(&self) -> crate::MemoryReport {
let (vfs_files, vfs_text_bytes, vfs_line_index_bytes) = self.vfs.accounting();
let (summaries, summary_bytes, cooked_files, cooked_bytes) =
self.store.index().accounting();
let (definitions, definition_bytes) = self
.definitions
.lock()
.map(|definitions| definitions.accounting())
.unwrap_or((0, 0));
let (units, unit_bytes) = self.held_units().accounting();
let (renderings, rendering_bytes) = self
.renderings
.lock()
.map(|known| {
let bytes = known
.values()
.map(|rendering| rendering.text.len())
.sum::<usize>();
(known.len(), bytes)
})
.unwrap_or((0, 0));
let (dead_macro_files, dead_macro_offsets) = self.dead_macro_decisions();
let (walk_cache_entries, walk_cache_bytes) = self.store.contributions().accounting();
let (walks_by_content_entries, walks_by_content_bytes) = self
.walked_by_content
.lock()
.map(|held| (held.len(), held.weight()))
.unwrap_or((0, 0));
crate::MemoryReport {
vfs_files,
vfs_text_bytes,
vfs_line_index_bytes,
summaries,
summary_bytes,
cooked_files,
cooked_bytes,
definitions,
definition_bytes,
units,
unit_bytes,
renderings,
rendering_bytes,
dead_macro_decisions: dead_macro_files,
dead_macro_bytes: dead_macro_offsets * std::mem::size_of::<usize>(),
directive_signatures: self.directive_signatures.len(),
include_searches: self.store.include_search_count().unwrap_or(0),
units_read: self.units_read.len(),
walk_cache_entries,
walk_cache_bytes,
walks_by_content_entries,
walks_by_content_bytes,
}
}
pub fn macro_at(&mut self, file: &Path, name: &str, offset: usize) -> Option<crate::MacroAt> {
let mut candidates: Vec<PathBuf> = Vec::new();
if self.project.iter().any(|root| root == file) {
candidates.push(file.to_path_buf());
}
candidates.extend(self.project.iter().filter(|root| *root != file).cloned());
for root in candidates {
let Some(unit) = self.translation_unit_of(&root) else {
continue;
};
if let Some(answer) = unit.macro_at(file, name, offset) {
return Some(answer);
}
}
None
}
pub fn stats(&self) -> StoreStats {
self.store.stats()
}
pub fn dead_macro_decisions(&self) -> (usize, usize) {
self.store.dead_macro_decisions()
}
pub fn cache_directory(&self) -> &Path {
self.store.cache_directory()
}
pub fn project_files(&self) -> &[PathBuf] {
&self.project
}
pub fn add_project_files(&mut self, paths: impl IntoIterator<Item = PathBuf>) -> usize {
let mut held: HashSet<String> = self.project.iter().map(|path| queue_key(path)).collect();
let mut added = 0;
for path in paths {
if held.insert(queue_key(&path)) {
self.project.push(path.clone());
added += 1;
}
self.queue.add(path, Priority::Rest, 0);
}
if added > 0
&& let Ok(mut headers) = self.headers.write()
{
*headers = headers.clone().with_project(self.project.clone());
}
added
}
pub fn did_open(&mut self, path: impl AsRef<Path>, text: &str) {
self.buffer_changed(path.as_ref(), text);
}
pub fn did_change(&mut self, path: impl AsRef<Path>, text: &str) {
self.buffer_changed(path.as_ref(), text);
}
pub fn did_save(&mut self, path: impl AsRef<Path>, text: Option<&str>) {
if let Some(text) = text {
self.buffer_changed(path.as_ref(), text);
}
}
pub fn did_close(&mut self, path: impl AsRef<Path>) {
let path = path.as_ref();
self.documents.close(path);
self.vfs.close(path);
self.store.forget(path);
self.prefetched.clear();
self.directive_signatures.remove(&queue_key(path));
self.units
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
.clear();
self.units_read.clear();
self.queue.again(path.to_path_buf(), Priority::Rest, 0);
}
pub fn changed(&mut self, events: impl IntoIterator<Item = FileEvent>) -> Response {
let mut batch = ChangeBatch::new(self.filter.clone());
batch.extend(events);
self.respond(&batch)
}
pub fn respond(&mut self, batch: &ChangeBatch) -> Response {
self.prefetched.clear();
for (path, _) in batch.changed() {
let text = self.files.read(path);
let moved = self.directives_moved(path, text.as_deref());
if moved.environment {
self.invalidate_dependents(path);
}
self.held_definitions().forget(path);
if moved.layout {
self.units
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
.clear();
self.units_read.clear();
}
}
let response = self.store.respond(batch);
if response.everything {
let project = self.project.clone();
for path in project {
self.queue.requeue(path, Priority::Rest, 0);
}
for path in self.documents.paths() {
self.queue.again(path, Priority::Open, 0);
}
} else {
for path in &response.reindex {
let priority = if self.documents.is_open(path) {
Priority::Open
} else {
Priority::Rest
};
self.queue.again(path.clone(), priority, 0);
}
}
response
}
fn buffer_changed(&mut self, path: &Path, text: &str) {
self.last_edit = std::time::Instant::now();
if !self.directive_signatures.contains_key(&queue_key(path))
&& let Some(held) = self.vfs.held(path)
{
let before = crate::preprocess::directive::directive_signature(&held.text);
self.directive_signatures.insert(queue_key(path), before);
}
let moved = self.directives_moved(path, Some(text));
self.prefetched.clear();
self.documents.open(path, text);
self.vfs.insert(path, text, true);
if moved.environment {
self.invalidate_dependents(path);
}
self.store.forget(path);
self.held_definitions().forget(path);
if moved.layout {
self.held_units().remove(&queue_key(path));
self.units_read.remove(&queue_key(path));
}
if let Ok(mut refused) = self.cooking_refused.lock() {
refused.remove(&queue_key(path));
}
self.cooking.want(path);
self.queue.again(path.to_path_buf(), Priority::Open, 0);
}
fn directives_moved(&mut self, path: &Path, text: Option<&str>) -> DirectivesMoved {
let key = queue_key(path);
let Some(text) = text else {
self.directive_signatures.remove(&key);
return DirectivesMoved {
environment: true,
layout: true,
};
};
let now = crate::preprocess::directive::directive_signature(text);
match self.directive_signatures.insert(key, now) {
Some(before) => DirectivesMoved {
environment: before.environment != now.environment,
layout: before.layout != now.layout,
},
None => DirectivesMoved {
environment: true,
layout: true,
},
}
}
fn invalidate_dependents(&mut self, path: &Path) {
let dependents = self.store.index().dependents_of(path);
for dependent in dependents {
self.held_units().remove(&queue_key(&dependent));
let was_indexed = self.store.forget(&dependent);
if was_indexed {
self.queue.again(dependent.clone(), Priority::Rest, 0);
}
if let Ok(mut known) = self.renderings.lock() {
known.retain(|(held, _)| held != &dependent);
}
}
}
fn want_the_closure_cooked(&mut self, root: &Path) {
let already_read =
|session: &Self, path: &Path| session.store.index().cooked_declarations(path).is_some();
let can_be_cooked =
|session: &Self, path: &Path| session.store.index().summary(path).is_some();
let direct = self.direct_includes_of(root);
let refused = |session: &Self, path: &Path| {
session
.cooking_refused
.lock()
.map(|refused| refused.contains(&queue_key(path)))
.unwrap_or(false)
};
for included in &direct {
if !already_read(self, included)
&& can_be_cooked(self, included)
&& !refused(self, included)
{
if crate::probes::cooking() {
eprintln!(
"want_the_closure_cooked({}): queued {} (not read, cookable, not refused)",
root.display(),
included.display()
);
}
self.cooking.want(included);
}
}
let mut budget = COOK_ONE_LEVEL_FURTHER;
let mut nested_files: Vec<PathBuf> = Vec::new();
for included in &direct {
for nested in self.direct_includes_of(included) {
if budget == 0 {
break;
}
if already_read(self, &nested) || refused(self, &nested) {
continue;
}
if !nested_files.contains(&nested) {
nested_files.push(nested.clone());
}
self.cooking.want(&nested);
budget -= 1;
}
}
let mut budget = COOK_TWO_LEVELS_FURTHER;
for nested in &nested_files {
for deeper in self.direct_includes_of(nested) {
if budget == 0 {
break;
}
if already_read(self, &deeper) || refused(self, &deeper) {
continue;
}
self.cooking.want(&deeper);
budget -= 1;
}
}
if !already_read(self, root) {
self.cooking.want(root);
}
}
fn direct_includes_of(&self, path: &Path) -> Vec<PathBuf> {
self.store
.index()
.summary(path)
.map(|summary| {
summary
.includes
.iter()
.filter_map(|include| include.resolved.clone())
.collect()
})
.unwrap_or_default()
}
pub fn diagnose_everything(
&mut self,
up_to: usize,
) -> Vec<(std::path::PathBuf, FileDiagnostics)> {
let paths: Vec<std::path::PathBuf> = self
.store
.index()
.summaries()
.map(|summary| summary.path.clone())
.take(up_to.max(1))
.collect();
self.want_everything_cooked();
let mut waves = 0usize;
while self.pending() > 0 {
if self.advance(64).is_empty() {
break;
}
waves += 1;
if waves > 100_000 {
break;
}
}
let mut cooked = 0usize;
loop {
let claimed = self.claim_cooking(64);
if claimed.is_empty() {
break;
}
let materials = self.materials_for(claimed);
let rendered = self.render_them(materials);
cooked += self.commit_them(rendered).len();
if cooked > up_to.max(1) * 2 + 1024 {
break;
}
}
paths
.into_iter()
.filter_map(|path| self.diagnostics(&path).map(|found| (path, found)))
.collect()
}
pub fn want_cooked_reading(&mut self, path: &Path) {
let refused = self
.cooking_refused
.lock()
.map(|refused| refused.contains(&queue_key(path)))
.unwrap_or(false);
if refused {
return;
}
if self.store.index().summary(path).is_none() {
self.queue.add(path.to_path_buf(), Priority::Open, 0);
}
if self.store.index().cooked_declarations(path).is_some() {
return;
}
self.cooking.want(path);
}
pub fn is_ready_for_a_request_about(&self, path: &Path) -> bool {
self.vfs.held(path).is_some()
&& (self.store.index().cooked_declarations(path).is_some() || self.cooking.wants(path))
}
pub fn want_everything_cooked(&mut self) {
let wanted: Vec<PathBuf> = self
.store
.index()
.summaries()
.filter(|summary| {
self.store
.index()
.cooked_declarations(&summary.path)
.is_none()
})
.map(|summary| summary.path.clone())
.collect();
for path in wanted {
self.cooking.want(&path);
}
}
pub fn advance(&mut self, steps: usize) -> Vec<Step> {
let mut done = Vec::new();
for taken in 0..steps {
let Some((path, priority, depth)) = self.queue.pop() else {
break;
};
let key = queue_key(&path);
let left = steps - taken;
let wave_would_parse_it_as_text = left > 1
&& !self.will_read_it_as_the_program(&path)
&& !self.prefetched.contains_key(&key);
if wave_would_parse_it_as_text {
let mut wave = vec![path.clone()];
wave.extend(
self.queue
.peek(left - 1)
.into_iter()
.filter(|queued| !self.prefetched.contains_key(&queue_key(queued))),
);
let room = (steps * 4).saturating_sub(self.prefetched.len());
let made = {
let queue = &self.queue;
let prefetched = &self.prefetched;
self.store.prepare_closure(
&wave,
|target| {
!queue.is_worked(target) && !prefetched.contains_key(&queue_key(target))
},
room.min(steps * 2).max(wave.len()),
)
};
for (path, made) in made {
self.prefetched.insert(queue_key(&path), made);
}
}
let ready = self.prefetched.remove(&key);
done.push(self.index_one(path, priority, depth, ready));
}
if self.queue.pending() == 0 {
self.prefetched.clear();
}
let units = Vec::new();
if crate::probes::units() {
eprintln!(
"ADVANCE steps={} done={:?} closure(api.h)={:?}",
steps,
done.iter()
.map(|step| step.path.clone())
.collect::<Vec<_>>(),
self.closure_with_text(Path::new("/p/api.h"))
.0
.iter()
.map(|(path, _)| path.clone())
.collect::<Vec<_>>()
);
}
let drain = self.finish_a_slice(&done, units);
let prepared = self.prepare_the_drain(drain);
self.commit_the_drain(prepared);
if self.is_idle() && self.pending_cooking() == 0 && self.re_read_backlog() == 0 {
self.read_a_program_now();
}
done
}
pub fn prepare_a_wave(&self, steps: usize) -> PreparedWave {
let wave: Vec<PathBuf> = self
.queue
.peek(steps)
.into_iter()
.filter(|path| {
let key = queue_key(path);
!self.prefetched.contains_key(&key) && !self.queue.is_worked(path)
})
.filter(|path| !self.will_read_it_as_the_program(path))
.collect();
let slice: Vec<PathBuf> = self
.parsed_since_the_last_pass
.iter()
.take(RE_READ_SLICE)
.cloned()
.collect();
let units = self.units_for_the_pass(&slice);
if wave.is_empty() {
return PreparedWave {
prepared: Vec::new(),
units,
};
}
let room = (steps * 4)
.saturating_sub(self.prefetched.len())
.max(wave.len());
let queue = &self.queue;
let prefetched = &self.prefetched;
PreparedWave {
prepared: self.store.prepare_closure(
&wave,
|target| !queue.is_worked(target) && !prefetched.contains_key(&queue_key(target)),
room,
),
units,
}
}
pub fn commit_a_wave(&mut self, wave: PreparedWave, steps: usize) -> (Vec<Step>, Drain) {
let PreparedWave { prepared, units } = wave;
for (path, made) in prepared {
self.prefetched.insert(queue_key(&path), made);
}
let mut done = Vec::new();
for _ in 0..steps {
let Some((path, priority, depth)) = self.queue.pop() else {
break;
};
let ready = self.prefetched.remove(&queue_key(&path));
done.push(self.index_one(path, priority, depth, ready));
}
if self.queue.pending() == 0 {
self.prefetched.clear();
}
let drain = self.finish_a_slice(&done, units);
(done, drain)
}
fn finish_a_slice(
&mut self,
done: &[Step],
mut units: Vec<std::sync::Arc<crate::TranslationUnit>>,
) -> Drain {
self.parsed_since_the_last_pass.extend(
done.iter()
.filter(|step| {
matches!(
step.outcome,
StepOutcome::Built | StepOutcome::Unstored | StepOutcome::Reused
)
})
.filter(|step| !step.with_the_environment)
.map(|step| step.path.clone()),
);
let files = if self.is_idle() {
let take = self.parsed_since_the_last_pass.len().min(RE_READ_SLICE);
self.parsed_since_the_last_pass.drain(..take).collect()
} else {
Vec::new()
};
if units.is_empty() && !files.is_empty() {
units = self.units_for_the_pass(&files);
}
let mut drain = Drain {
files,
units,
environments: Vec::new(),
};
if self.is_idle() {
for open in self.documents.paths() {
self.want_the_closure_cooked(&open);
}
while let Some(path) = self
.macro_work
.lock()
.ok()
.and_then(|mut work| work.take(1).into_iter().next())
{
self.cooking.want(&path);
}
for _ in 0..MACRO_SLICE {
let Some(path) = self
.macro_work
.lock()
.ok()
.and_then(|mut work| work.take(1).into_iter().next())
else {
break;
};
drain.environments.push(path);
}
}
drain
}
pub fn read_a_program_now(&mut self) -> Option<UnitReading> {
let root = self.the_program_to_read()?;
let materials = self.gather_a_unit_read(&root);
let prepared = materials.and_then(UnitReadMaterials::read_the_program);
self.commit_a_program(&root, prepared)
}
pub fn commit_a_program(
&mut self,
root: &Path,
prepared: Option<PreparedUnitReading>,
) -> Option<UnitReading> {
let Some(prepared) = prepared else {
let key = queue_key(root);
let attempts = self.units_attempted.entry(key).or_default();
*attempts += 1;
if *attempts >= PROGRAM_READ_ATTEMPTS {
self.units_read.insert(queue_key(root));
}
return None;
};
self.units_read.insert(queue_key(root));
Some(self.commit_a_unit_reading(prepared))
}
pub fn prepare_the_drain(&self, drain: Drain) -> PreparedDrain {
let Drain {
files,
units,
environments,
} = drain;
let tracing = crate::probes::cooking();
let started = std::time::Instant::now();
let re_read =
(!files.is_empty()).then(|| self.store.prepare_the_re_read(&files, &units, false, true));
if tracing && !files.is_empty() {
eprintln!(
"drain: re-read {} file(s) in {} ms",
files.len(),
started.elapsed().as_millis()
);
}
let mut rendered = Vec::new();
for path in environments {
let step = std::time::Instant::now();
let Some(reading) = self.rendering_of(&path) else {
continue;
};
let Some(text) = self.text(&path) else {
continue;
};
let _ = self.the_macros_of(&path, &text);
if tracing {
eprintln!(
"drain: environment for {} in {} ms",
path.display(),
step.elapsed().as_millis()
);
}
rendered.push((
(path, crate::cache::content_hash(&text)),
std::sync::Arc::new(reading),
));
}
PreparedDrain { re_read, rendered }
}
pub fn commit_the_drain(&mut self, prepared: PreparedDrain) -> usize {
for (key, rendered) in prepared.rendered {
if let Ok(mut known) = self.renderings.lock() {
let weight = rendered.weight();
known.insert(key, rendered, weight);
}
}
let Some(plan) = prepared.re_read else {
return 0;
};
let waiting: Vec<PathBuf> = plan.waiting_for_the_index().to_vec();
let re_read = self.store.commit_the_re_read(plan);
if !waiting.is_empty() && self.pending_work() > 0 {
if crate::probes::cooking() {
eprintln!(
"re-read: {} file(s) go back on the queue — their closures are still filling",
waiting.len()
);
}
for path in waiting {
self.queue.again(path, Priority::Rest, 0);
}
}
re_read
}
fn units_for_the_pass(&self, files: &[PathBuf]) -> Vec<std::sync::Arc<crate::TranslationUnit>> {
let mut candidates: Vec<PathBuf> = Vec::new();
let mut seen: std::collections::HashSet<PathBuf> = std::collections::HashSet::new();
for path in files.iter() {
let path = self
.store
.index()
.summary(path)
.map(|summary| summary.path.clone())
.unwrap_or_else(|| path.clone());
if seen.insert(path.clone()) {
candidates.push(path);
}
}
let mut units: Vec<std::sync::Arc<crate::TranslationUnit>> = Vec::new();
let held: Vec<std::sync::Arc<crate::TranslationUnit>> = {
let table = self.held_units();
table
.held
.values()
.filter(|held| !held.provisional)
.map(|held| std::sync::Arc::clone(&held.unit))
.collect()
};
let mut still_to_walk: Vec<PathBuf> = Vec::new();
for candidate in candidates {
if let Some(unit) = held
.iter()
.find(|unit| unit.environment_of(&candidate).is_some())
{
if !units
.iter()
.any(|known| std::sync::Arc::ptr_eq(known, unit))
{
units.push(std::sync::Arc::clone(unit));
}
continue;
}
still_to_walk.push(candidate);
}
for (candidate, unit) in self.walk_the_timelines(still_to_walk) {
if units
.iter()
.any(|unit| unit.environment_of(&candidate).is_some())
{
continue;
}
if let Some(unit) = unit {
units.push(unit);
}
}
units
}
fn walk_the_timelines(
&self,
roots: Vec<PathBuf>,
) -> Vec<(PathBuf, Option<std::sync::Arc<crate::TranslationUnit>>)> {
roots
.into_iter()
.map(|root| {
let unit = self.translation_unit_of(&root);
(root, unit)
})
.collect()
}
fn index_one(
&mut self,
path: PathBuf,
priority: Priority,
depth: usize,
ready: Option<crate::index::store::Prepared>,
) -> Step {
{
self.vfs.load(&path);
}
let before = self.store.stats();
let spelled = self
.store
.index()
.summary(&path)
.map(|summary| summary.path.clone())
.unwrap_or_else(|| path.clone());
let unit = self.the_timeline_to_read_through(&spelled);
let has_a_timeline = unit
.as_ref()
.is_some_and(|unit| unit.environment_of(&spelled).is_some());
let read_here = ready.is_none();
let summary = match ready {
Some(ready) => self.store.commit(&path, ready),
None => match &unit {
Some(unit) => {
let prepared = self.store.prepare_with_the_timeline(&path, unit);
self.store.commit(&path, prepared)
}
None => self.store.get(&path),
},
};
let includes: Vec<PathBuf> = summary
.map(|summary| {
summary
.includes
.iter()
.filter_map(|include| include.resolved.clone())
.collect()
})
.unwrap_or_default();
let after = self.store.stats();
let outcome = outcome_of(before, after);
for include in includes {
self.queue.add(include, priority, depth + 1);
}
Step {
path,
priority,
depth,
outcome,
with_the_environment: read_here
&& has_a_timeline
&& matches!(outcome, StepOutcome::Built | StepOutcome::Unstored),
}
}
pub fn catch_up(&mut self, path: &Path) {
if self.queue.forget(path) {
self.index_one(path.to_path_buf(), Priority::Open, 0, None);
}
for include in self.unread_includes_of(path) {
self.index_one(include, Priority::Open, 1, None);
}
}
fn unread_includes_of(&self, path: &Path) -> Vec<PathBuf> {
let Some(summary) = self.store.index().summary(path) else {
return Vec::new();
};
summary
.includes
.iter()
.filter_map(|include| include.resolved.as_ref())
.filter(|resolved| self.store.index().summary(resolved).is_none())
.cloned()
.collect()
}
pub fn needs_catching_up(&self, path: &Path) -> bool {
self.queue.is_queued(path) || !self.unread_includes_of(path).is_empty()
}
pub fn is_closure_ready(&self, path: &Path) -> bool {
if !self.store.index().summary(path).is_some() {
return false;
}
self.store
.index()
.visible_files(path)
.iter()
.all(|(file, visibility)| {
*visibility != crate::IncludeVisibility::Unconditional
|| self.store.index().summary(Path::new(file)).is_some()
})
}
pub fn is_locally_ready(&self, path: &Path) -> bool {
self.is_indexed(path) && !self.needs_catching_up(path)
}
pub fn begin_the_cache_generation(&mut self) {
self.store.begin_generation();
}
pub fn index_everything(&mut self) -> usize {
let mut total = 0;
let tracing = crate::probes::cooking();
loop {
let read = self.advance(64).len();
total += read;
let cooked = {
let claimed = self.claim_cooking(COOK_SLICE);
if tracing && !claimed.is_empty() {
eprintln!("cooking: claimed {} file(s) {:?}", claimed.len(), claimed);
}
let materials = self.materials_for(claimed);
let wanted = materials.len();
let rendered = self.render_them(materials);
let built = rendered.len();
let committed = self.commit_them(rendered).len();
if tracing && (built != wanted || committed != built) {
eprintln!(
"cooking: rendered {built} of {wanted} claimed, committed {committed} \
(cooking backlog {})",
self.pending_cooking()
);
}
committed
};
if read == 0 && cooked == 0 {
if tracing {
let cached: Vec<String> = self
.renderings
.lock()
.map(|known| {
known
.keys()
.map(|(path, hash)| format!("{}#{hash}", path.display()))
.collect()
})
.unwrap_or_default();
eprintln!(
"cooking: stopping with read 0, cooked 0, backlog {} | cache {cached:?}",
self.pending_cooking()
);
}
if let Some(collected) = self.collect_the_cache() {
log::debug!(
"cache: collected {} summary(ies), {} rendering(s), {} projection(s)",
collected.summaries,
collected.renderings,
collected.projections
);
}
return total;
}
}
}
pub fn pending(&self) -> usize {
self.queue.pending()
}
pub fn pending_work(&self) -> usize {
self.queue.pending() + self.cooking.len() + self.parsed_since_the_last_pass.len()
}
pub fn is_idle(&self) -> bool {
self.queue.pending() == 0
}
pub fn pending_cooking(&self) -> usize {
self.cooking.len()
}
pub fn re_read_backlog(&self) -> usize {
self.parsed_since_the_last_pass.len()
}
pub fn is_owed_a_re_read(&self, path: &Path) -> bool {
let key = crate::file::paths::normalize_path(path, cfg!(windows));
self.parsed_since_the_last_pass
.iter()
.any(|queued| crate::file::paths::normalize_path(queued, cfg!(windows)) == key)
}
pub fn its_closure_is_owed(&self, path: &Path) -> bool {
if self.parsed_since_the_last_pass.is_empty() {
return false;
}
if self.is_owed_a_re_read(path) {
return true;
}
self.store
.index()
.visible_files(path)
.iter()
.any(|(file, _)| self.is_owed_a_re_read(Path::new(file)))
}
pub fn abandon_the_cooking_queue(&mut self) -> usize {
self.cooking.abandon()
}
pub fn save_the_sharing(&self) -> std::io::Result<usize> {
use crate::index::walk_cache_codec;
let entries = self.store.contributions().saved();
let written = entries.len();
std::fs::write(
self.store.cache_directory().join(SHARING_FILE),
walk_cache_codec::encode(&entries),
)?;
Ok(written)
}
pub fn load_the_sharing(&self) -> usize {
use crate::index::walk_cache_codec;
let Ok(bytes) = std::fs::read(self.store.cache_directory().join(SHARING_FILE)) else {
return 0;
};
let Some(entries) = walk_cache_codec::decode(&bytes) else {
return 0;
};
let restored = entries.len();
self.store.contributions().restore(entries);
restored
}
pub fn is_indexed(&self, path: impl AsRef<Path>) -> bool {
self.store.index().summary(path.as_ref()).is_some()
}
pub fn tokens_of(&self, path: impl AsRef<Path>) -> Option<TokensOf> {
let file = self.vfs.held(path)?;
Some(TokensOf {
path: file.path.clone(),
source: file.text.clone(),
line_index: file.line_index.clone(),
open: file.open,
tokens: cpp_parser::lex(&file.text, &cpp_parser::LexerConfig::default()).0,
})
}
pub fn view(&self, path: impl AsRef<Path>) -> Option<FileView> {
let requested = path.as_ref();
let key = self
.store
.index()
.summary(requested)
.map(|summary| summary.path.clone())
.unwrap_or_else(|| requested.to_path_buf());
if crate::probes::view() {
eprintln!(
"view: asked {:?} key {:?} summary {} held {}",
requested,
key,
self.store.index().summary(requested).is_some(),
self.vfs.held(&key).is_some()
);
}
let file = self.vfs.held(&key)?;
if crate::probes::view() {
eprintln!(
"view: file.path {:?} hash {:?}",
file.path,
crate::cache::content_hash(&file.text)
);
}
match self.known_rendering_of(&file.path, &file.text) {
Some(rendered) => Some(FileView::parse_rendering_in(
file,
&rendered,
&self.config_for(&file.path),
)),
None => {
if crate::probes::cooking() {
eprintln!(
"view: no rendering for {} | hash {} | {} entr(ies) cached | open {}",
file.path.display(),
crate::cache::content_hash(&file.text),
self.renderings.lock().map(|known| known.len()).unwrap_or(0),
file.open
);
}
if let Ok(mut work) = self.macro_work.lock() {
work.want(&file.path);
}
None
}
}
}
pub fn view_of_the_file(&self, path: impl AsRef<Path>) -> Option<FileView> {
let requested = path.as_ref();
let file = self.vfs.held(requested)?;
if self.known_rendering_of(&file.path, &file.text).is_none()
&& let Ok(mut work) = self.macro_work.lock()
{
work.want(&file.path);
}
let macros = self
.known_macros_of(&file.path, &file.text)
.unwrap_or_default();
Some(FileView::parse_with_cuda(
file,
macros.as_ref(),
self.config.cuda(),
))
}
pub fn view_of_the_rendering(&mut self, path: impl AsRef<Path>) -> Option<FileView> {
let file = self.vfs.held(path)?.clone();
let rendered = self.rendering_of(&file.path)?;
self.known_rendering_of(&file.path, &file.text);
Some(FileView::parse_rendering_in(
&file,
&rendered,
&self.config_for(&file.path),
))
}
fn known_rendering_of(
&self,
path: &Path,
text: &str,
) -> Option<std::sync::Arc<crate::preprocess::cooked::RenderedCooked>> {
let key = (path.to_path_buf(), crate::cache::content_hash(text));
self.renderings.lock().ok()?.get(&key).cloned()
}
fn known_macros_of(
&self,
path: &Path,
text: &str,
) -> Option<std::sync::Arc<cpp_parser::MacroEnvironment>> {
let key = (path.to_path_buf(), crate::cache::content_hash(text));
self.macro_environments.lock().ok()?.get(&key).cloned()
}
fn the_macros_of(
&self,
path: &Path,
text: &str,
) -> Option<std::sync::Arc<cpp_parser::MacroEnvironment>> {
if let Some(known) = self.known_macros_of(path, text) {
return Some(known);
}
let summary = self.store.index().summary(path)?;
let (closure, by_key) = self.closure_with_text(path);
let mut definitions = crate::summary::MacroDefinitions::default();
let evidence = crate::summary::macros_from_the_closure_with_bodies(
summary,
|wanted| {
let key = normalize_path(wanted, cfg!(windows));
let (defined_in, text) = closure.get(*by_key.get(&key)?)?;
Some((self.store.index().summary(defined_in)?, text.as_str()))
},
self.store.index().macros(),
&mut definitions,
);
let made = std::sync::Arc::new(
cpp_parser::MacroEnvironment::from_included_macros(evidence.macros)
.with_bodies_in_force(evidence.conditional_bodies),
);
let weight = cpp_parser::MacroFacts::len(made.as_ref());
if let Ok(mut known) = self.macro_environments.lock() {
known.insert(
(path.to_path_buf(), crate::cache::content_hash(text)),
made.clone(),
weight,
);
}
Some(made)
}
pub fn notes_about_the_modules(&self, view: &crate::FileView) -> Vec<ModuleNote> {
let info = crate::ModuleInfo::from_tree(&view.root);
let own_module = info.module_name.clone();
let mut notes = Vec::new();
for declaration in &info.imports {
let start = view
.file_offset_of(ReadingOffset::new(declaration.range.start_offset))
.map_or(declaration.range.start_offset, |at| at.get());
let end = view
.file_offset_of(ReadingOffset::new(declaration.range.end_offset()))
.map_or(declaration.range.end_offset(), |at| at.get());
let range = (start, end);
if let Some(partition) = declaration.target.partition_name() {
let Some(module) = own_module.as_deref() else {
continue;
};
if self.the_partition_is_read(module, partition) {
continue;
}
notes.push(ModuleNote {
message: match &self.toolchain {
Some(toolchain) => toolchain.partition_note(module, partition),
None => format!(
"no file this project contains declares partition `:{partition}` of module `{module}` \
— a partition is not a module, and until its file is read the names it exports are \
unknown rather than absent"
),
},
start: range.0,
end: range.1,
});
continue;
}
let Some(module) = declaration.target.module_name() else {
continue;
};
if self.store.index().interface_unit_of(module).is_some() {
continue;
}
let message = match &self.toolchain {
Some(toolchain) => toolchain.module_note(module),
None => format!(
"no file this project contains declares module `{module}` — a module outside the project is \
one only the compiler can see, and until then the names it exports are unknown rather than \
absent"
),
};
notes.push(ModuleNote {
message,
start: range.0,
end: range.1,
});
}
notes
}
fn the_partition_is_read(&self, module: &str, partition: &str) -> bool {
self.store.index().summaries().any(|summary| {
summary.modules.module.as_deref() == Some(module)
&& summary.modules.partition.as_deref() == Some(partition)
&& summary.modules.is_interface
})
}
pub fn diagnostics(&self, path: impl AsRef<Path>) -> Option<FileDiagnostics> {
let path = path.as_ref();
let _whole = DiagTrace::new("diagnostics");
let key = self
.store
.index()
.summary(path)
.map(|summary| summary.path.clone())
.unwrap_or_else(|| path.to_path_buf());
let mut notes = Vec::new();
if let Some(cooked) = self.store.index().cooked_reading(&key) {
let view = {
let _t = DiagTrace::new("the file's tree (view)");
self.view(path)
};
if let Some(view) = &view {
let _t = DiagTrace::new("module notes");
notes = if self.pending_work() == 0 {
self.notes_about_the_modules(view)
} else {
Vec::new()
};
}
let checks = match &view {
Some(view) => self.checks_about(&key, view),
None => Vec::new(),
};
return Some(FileDiagnostics {
reading: DiagnosticReading::Cooked,
unplaced: cooked.unplaced,
errors: match view.as_ref().and_then(Self::a_fragment_and_its_message) {
Some((start, end, message)) => vec![FileDiagnostic {
start,
end,
message,
code: crate::sema::check::DiagnosticCode::SyntaxError,
}],
None => cooked
.diagnostics
.iter()
.map(|error| FileDiagnostic {
start: error.range.start_offset,
end: error.range.end_offset(),
message: error.message.clone(),
code: crate::sema::check::DiagnosticCode::SyntaxError,
})
.collect(),
},
notes,
checks,
});
}
None
}
fn a_fragment_and_its_message(view: &crate::FileView) -> Option<(usize, usize, String)> {
let written = view.written_text().unwrap_or(&view.source);
let (start, end, message) = Self::error_directive_line(written)?;
let first = view
.root
.descendants_with_tokens()
.filter_map(|element| element.into_token())
.map(|token| cpp_parser::CppTokenKind::from(token.kind()))
.find(|kind| {
!matches!(
kind,
cpp_parser::CppTokenKind::Whitespace | cpp_parser::CppTokenKind::Newline
)
})?;
if !matches!(
first,
cpp_parser::CppTokenKind::LeftBrace | cpp_parser::CppTokenKind::RightBrace
) {
return None;
}
Some((start, end, message))
}
fn error_directive_line(text: &str) -> Option<(usize, usize, String)> {
let mut at = 0;
for line in text.split_inclusive('\n') {
let trimmed = line.trim_start();
let indent = line.len() - trimmed.len();
if let Some(rest) = trimmed.strip_prefix('#') {
let rest = rest.trim_start();
if let Some(message) = rest.strip_prefix("error") {
let message = message.trim().trim_matches('"').trim();
return Some((at + indent, at + line.trim_end().len(), message.to_string()));
}
}
at += line.len();
}
None
}
fn checks_about(&self, key: &Path, view: &crate::FileView) -> Vec<crate::sema::check::Finding> {
let index = self.store.index();
let Some(summary) = index.summary(key) else {
return Vec::new();
};
let model = self.semantic_model(view);
let config = crate::sema::check::CheckConfig::from_names(
&self.project_config().config.diagnostics.enable,
&self.project_config().config.diagnostics.disable,
);
for name in config.unknown() {
log::warn!("`[diagnostics]` names a rule this build does not have: `{name}`");
}
crate::sema::check::run(crate::sema::check::CheckContext::new(
key,
summary,
model,
&view.source,
view.written_text().unwrap_or(&view.source),
view.tree.get_errors(),
view,
config,
self.config_for(key).language_for(key),
))
}
pub fn semantic_model<'s>(
&'s self,
view: &'s FileView,
) -> crate::sema::model::SemanticModel<'s> {
let session: &'s Session<F> = self;
let index: &'s crate::ProjectIndex = session.store.index();
let seen: std::cell::RefCell<std::collections::HashMap<PathBuf, Option<FileView>>> =
std::cell::RefCell::new(std::collections::HashMap::new());
let resolver = Box::new(move |path: &Path| -> Option<FileView> {
if let Some(known) = seen.borrow().get(path) {
return known.clone();
}
let started = std::time::Instant::now();
let made = session.view_of_the_file(path);
if crate::probes::timing() {
eprintln!(
"[resolver] {:>5} ms {} ({})",
started.elapsed().as_millis(),
path.file_name()
.map(|name| name.to_string_lossy().to_string())
.unwrap_or_else(|| path.display().to_string()),
if made.is_some() { "read" } else { "none" }
);
}
seen.borrow_mut().insert(path.to_path_buf(), made.clone());
made
});
crate::sema::model::SemanticModel::new(
index,
&view.root,
&view.scopes,
&view.source,
&view.path,
resolver,
)
}
pub fn text(&self, path: impl AsRef<Path>) -> Option<String> {
Some(self.vfs.held(path)?.text.to_string())
}
pub fn load(&mut self, path: impl AsRef<Path>) -> Option<crate::FileId> {
self.vfs.load(path)
}
pub fn files(&self) -> &Vfs<SessionFiles<F>> {
&self.vfs
}
fn translation_unit_of(&self, path: &Path) -> Option<std::sync::Arc<crate::TranslationUnit>> {
self.unit_and_state_of(path).map(|(unit, _)| unit)
}
fn unit_and_state_of(
&self,
path: &Path,
) -> Option<(std::sync::Arc<crate::TranslationUnit>, bool)> {
let key = queue_key(path);
let held = self.held_units().get(&key);
if let Some((provisional, unit)) = held {
if !provisional || self.queue.pending() > 0 {
self.note_a_unit(Which::FromMemory);
return Some((unit, provisional));
}
self.held_units().remove(&key);
}
let context = self.store.context_hash(path);
let cached: Option<crate::TranslationUnit> = None;
let (unit, provisional) = match cached {
Some(unit) => {
self.note_a_unit(Which::FromDisk);
(std::sync::Arc::new(unit), false)
}
None => {
let owned_root;
let index = self.store.index();
let root = match index.summary(path) {
Some(summary) => summary,
None if self.documents.is_open(path) => {
owned_root = self.store.directive_summary(path)?;
self.the_closure_of_the_directives(path, &owned_root)?;
owned_root.as_ref()
}
None => return None,
};
let read_started = std::time::Instant::now();
let (closure, closure_by_key) = { self.closure_with_text_from(path, Some(root)) };
let read_took = read_started.elapsed();
let closure_bytes: usize = closure.iter().map(|(_, text)| text.len()).sum();
let walk_started = std::time::Instant::now();
let mut definitions = self.held_definitions();
let identity = self.closure_identity(&closure, context);
let shared = self
.walked_by_content
.lock()
.ok()
.and_then(|mut held| held.get(&identity).cloned());
let (unit, provisional) = if let Some(shared) = shared {
self.note_a_unit(Which::FromMemory);
(shared, false)
} else {
let provisional = self.closure_is_still_growing(&closure, &closure_by_key);
let walked = std::sync::Arc::new({
let root_key = queue_key(path);
crate::TranslationUnit::walk(
root,
|wanted| {
let (held, text) =
closure.get(*closure_by_key.get(&queue_key(wanted))?)?;
let summary = if queue_key(held) == root_key {
root
} else {
index.summary(held)?
};
Some((summary, text.as_str()))
},
index.macros(),
&mut definitions,
!provisional && !crate::probes::no_share(),
self.store.contributions(),
)
});
let walk_took = walk_started.elapsed();
let mut put_took = std::time::Duration::ZERO;
if !provisional {
let put_started = std::time::Instant::now();
put_took = put_started.elapsed();
let weight = walked.heap_bytes();
if let Ok(mut held) = self.walked_by_content.lock() {
held.insert(identity, std::sync::Arc::clone(&walked), weight);
}
}
self.note_a_unit(Which::Walked);
if let Ok(mut walks) = self.walks_by_root.lock() {
*walks.entry(queue_key(path)).or_insert(0) += 1;
}
if crate::probes::timing() {
eprintln!(
"[unit] read {:>4} ms, walk {:>4} ms, put {:>4} ms, {} file(s) / {} bytes {}",
read_took.as_millis(),
walk_took.as_millis(),
put_took.as_millis(),
closure.len(),
closure_bytes,
path.file_name()
.map(|name| name.to_string_lossy().to_string())
.unwrap_or_else(|| path.display().to_string())
);
}
(walked, provisional)
};
(unit, provisional)
}
};
self.held_units()
.insert(key, std::sync::Arc::clone(&unit), provisional);
Some((unit, provisional))
}
fn holds_an_environment_for(&self, path: &Path) -> bool {
let spelled = self
.store
.index()
.summary(path)
.map(|summary| summary.path.clone())
.unwrap_or_else(|| path.to_path_buf());
let held = self.held_units().get(&queue_key(&spelled));
held.is_some_and(|(provisional, _)| !provisional)
}
fn will_read_it_as_the_program(&self, path: &Path) -> bool {
self.holds_an_environment_for(path) || self.a_timeline_can_be_walked_for(path)
}
fn a_timeline_can_be_walked_for(&self, path: &Path) -> bool {
if !self.documents.is_open(path) {
return false;
}
match self.store.index().summary(path) {
Some(_) => self.is_closure_ready(path),
None => self
.store
.directive_summary(path)
.is_some_and(|directives| {
self.the_closure_of_the_directives(path, &directives)
.is_some()
}),
}
}
fn the_timeline_to_read_through(
&self,
path: &Path,
) -> Option<std::sync::Arc<crate::TranslationUnit>> {
if let Some((false, unit)) = self.held_units().get(&queue_key(path))
&& unit.environment_of(path).is_some()
{
return Some(unit);
}
if !self.a_timeline_can_be_walked_for(path) {
return None;
}
let (unit, provisional) = self.unit_and_state_of(path)?;
(!provisional && unit.environment_of(path).is_some()).then_some(unit)
}
fn closure_is_still_growing(
&self,
closure: &[(PathBuf, String)],
in_closure: &HashMap<String, usize>,
) -> bool {
let index = self.store.index();
closure.iter().any(|(path, _)| {
index.summary(path).is_some_and(|summary| {
summary.includes.iter().any(|include| {
include.resolved.as_ref().is_some_and(|target| {
!in_closure.contains_key(&queue_key(target)) && self.queue.is_queued(target)
})
})
})
})
}
fn note_a_unit(&self, which: Which) {
use std::sync::atomic::Ordering::Relaxed;
let counter = match which {
Which::FromMemory => &self.unit_from_memory,
Which::FromDisk => &self.unit_from_disk,
Which::Walked => &self.unit_walked,
};
counter.fetch_add(1, Relaxed);
}
pub fn unit_stats(&self) -> UnitStats {
use std::sync::atomic::Ordering::Relaxed;
UnitStats {
from_memory: self.unit_from_memory.load(Relaxed),
from_disk: self.unit_from_disk.load(Relaxed),
walked: self.unit_walked.load(Relaxed),
}
}
pub fn walks_by_root(&self) -> Vec<(PathBuf, u64)> {
let Ok(walks) = self.walks_by_root.lock() else {
return Vec::new();
};
let mut walked: Vec<(PathBuf, u64)> = walks
.iter()
.map(|(path, count)| (PathBuf::from(path), *count))
.collect();
walked.sort_by(|left, right| right.1.cmp(&left.1).then_with(|| left.0.cmp(&right.0)));
walked
}
fn closure_identity(&self, closure: &[(PathBuf, String)], context: u64) -> (u64, u64) {
let mut contents: Vec<u64> = closure
.iter()
.map(|(_, text)| crate::cache::content_hash(text))
.collect();
contents.sort_unstable();
let mut identity: u64 = 0xcbf2_9ce4_8422_2325;
for hash in &contents {
identity ^= *hash;
identity = identity.wrapping_mul(0x0000_0100_0000_01b3);
}
identity ^= (contents.len() as u64).wrapping_mul(0x9e37_79b9_7f4a_7c15);
(context, identity)
}
fn held_units(&self) -> std::sync::MutexGuard<'_, UnitTable> {
self.units
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
}
fn held_definitions(&self) -> std::sync::MutexGuard<'_, crate::MacroDefinitions> {
self.definitions
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
}
pub fn effect_walk(
&self,
root: &Path,
) -> Option<(HashMap<PathBuf, u64>, crate::effect::Walked)> {
self.store.index().summary(root)?;
let reads = SessionReads {
files: &self.files,
index: self.store.index(),
};
let mut cache = self
.effects
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
if cache.len() >= cache::cache_lru::EFFECT_ENTRIES {
cache.clear();
}
let seed = crate::effect::Macros::new();
let surfaces = self
.surfaces
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
let (signatures, walked) = crate::effect::Chain::new(&reads, &mut cache)
.with_surfaces(&surfaces)
.at_signatures(root, &seed);
Some((signatures, walked))
}
pub fn render_the_unit(&self, root: &Path) -> Option<crate::RenderedUnit> {
let unit = self.translation_unit_of(root)?;
let (sources, key) = self.the_units_inputs(&unit);
if let Ok(known) = self.rendered_unit.lock()
&& let Some((seen, cached)) = known.as_ref()
&& *seen == key
{
return Some(cached.clone());
}
if let Some(cached) = self.store.cached_unit(key) {
if let Ok(mut known) = self.rendered_unit.lock() {
*known = Some((key, cached.clone()));
}
return Some(cached);
}
let definitions = unit.definitions();
let seed = MacroTable::from_marked(self.store.index().macros());
let search = crate::preprocess::cooked::Search::new(&self.files, &self.config);
let stream =
{ unit.cook_the_unit(&sources, &definitions, Some(&seed), true, Some(&search)) };
self.store.remember_unit(key, &stream);
if let Ok(mut known) = self.rendered_unit.lock() {
*known = Some((key, stream.clone()));
}
Some(stream)
}
fn the_units_inputs(&self, unit: &crate::TranslationUnit) -> (HashMap<PathBuf, String>, u64) {
let mut sources: HashMap<PathBuf, String> = HashMap::new();
for path in unit.files() {
if let Some(text) = self.files.read(path) {
sources.insert(path.to_path_buf(), text);
}
}
let mut inputs: Vec<(&std::path::Path, u64)> = sources
.iter()
.map(|(path, text)| (path.as_path(), crate::cache::content_hash(text)))
.collect();
inputs.sort_unstable_by(|left, right| left.0.cmp(right.0));
let key = {
let mut folded = String::new();
for (path, hash) in &inputs {
folded.push_str(&path.to_string_lossy());
folded.push('|');
folded.push_str(&hash.to_string());
folded.push(';');
}
crate::cache::content_hash(&folded)
};
(sources, key)
}
pub fn gather_a_unit_read(&self, root: &Path) -> Option<UnitReadMaterials<SessionFiles<F>>> {
let root = self.store.index().summary(root)?.path.clone();
let unit = self.translation_unit_of(&root)?;
let (sources, units_key) = self.the_units_inputs(&unit);
let key = SummaryKey::new(0, self.store.context_hash(&root));
let rendered = self
.rendered_unit
.lock()
.ok()
.and_then(|known| {
known
.as_ref()
.filter(|(seen, _)| *seen == units_key)
.map(|(_, cached)| cached.clone())
})
.or_else(|| self.store.cached_unit(units_key));
let (program, known) = match &rendered {
Some(rendered) => {
let program = crate::cache::content_hash(&rendered.text);
let known = self
.unit_index
.lock()
.ok()
.and_then(|memo| {
memo.as_ref()
.filter(|(seen, _)| *seen == program)
.map(|(_, cached)| cached.clone())
})
.or_else(|| self.store.cached_indexed_unit(program));
(Some(program), known)
}
None => (None, None),
};
Some(UnitReadMaterials {
root,
key,
units_key,
unit,
sources,
macros: self.store.index().macros().clone(),
config: self.config.clone(),
files: self.files.clone(),
database: self.store.database(),
program,
rendered,
known,
})
}
pub fn prepare_a_unit_reading(&self, root: &Path) -> Option<PreparedUnitReading> {
self.gather_a_unit_read(root)?.read_the_program()
}
fn remember_the_unit_index(&self, program: u64, indexed: &crate::IndexedUnit) {
if let Ok(mut memo) = self.unit_index.lock() {
*memo = Some((program, indexed.clone()));
}
}
pub fn commit_a_unit_reading(&mut self, prepared: PreparedUnitReading) -> UnitReading {
let PreparedUnitReading {
root,
program,
indexed,
} = prepared;
self.remember_the_unit_index(program, &indexed);
let mut placed = 0usize;
for (path, cooked) in indexed.files {
placed += 1;
self.queue.mark_worked(&path);
self.store
.index_mut()
.insert_cooked_in_a_program(&path, program, cooked);
}
UnitReading {
root,
files: placed,
declared: 0,
tokens: indexed.tokens,
files_with_tokens: indexed.files_with_tokens,
missing: indexed.missing,
unplaced: indexed.unplaced,
unbalanced: indexed.unbalanced,
braces: indexed.braces,
errors: indexed.errors,
}
}
pub fn read_the_unit(&mut self, root: &Path) -> Option<UnitReading> {
let prepared = self.prepare_a_unit_reading(root)?;
Some(self.commit_a_unit_reading(prepared))
}
fn a_unit_may_be_read_now(&self) -> bool {
self.last_edit.elapsed() >= UNTIL_A_UNIT_MAY_BE_READ
}
#[cfg(test)]
pub(crate) fn pretend_a_pause(&mut self, for_how_long: std::time::Duration) {
self.last_edit = std::time::Instant::now()
.checked_sub(for_how_long)
.unwrap_or_else(std::time::Instant::now);
}
pub fn the_wait_before_a_program_may_be_read(&self) -> Option<std::time::Duration> {
self.the_next_unit_to_read()
.map(|_| UNTIL_A_UNIT_MAY_BE_READ.saturating_sub(self.last_edit.elapsed()))
}
pub fn the_program_to_read(&self) -> Option<PathBuf> {
let open = self.the_next_unit_to_read()?;
self.a_unit_may_be_read_now().then_some(open)
}
fn the_next_unit_to_read(&self) -> Option<PathBuf> {
let mut wanted: Vec<PathBuf> = self
.programs_wanted
.iter()
.filter(|key| !self.units_read.contains(*key))
.map(PathBuf::from)
.collect();
wanted.sort();
if let Some(unit) = wanted.into_iter().next() {
return Some(unit);
}
let open = self.documents.paths().into_iter().find(|path| {
!self.units_read.contains(&queue_key(path))
&& !self.the_program_is_too_large_to_read_at_once(path)
&& self
.store
.index()
.summary(path)
.is_none_or(|summary| summary.modules.imports.is_empty())
});
if open.is_some() {
return open;
}
let mut wanted: Vec<PathBuf> = self
.programs_wanted
.iter()
.filter(|key| !self.units_read.contains(*key))
.map(PathBuf::from)
.collect();
wanted.sort();
wanted.into_iter().next()
}
fn the_program_is_too_large_to_read_at_once(&self, root: &Path) -> bool {
let bytes: usize = self
.closure_paths_from(root, None)
.iter()
.map(|path| self.text(path).map_or(0, |text| text.len()))
.sum();
bytes > THE_LARGEST_PROGRAM_READ_AT_ONCE
}
pub fn read_a_unit_of_the_project(&mut self) -> Option<UnitReading> {
let root = self
.project
.iter()
.find(|root| !self.units_read.contains(&queue_key(root)))
.cloned()?;
let reading = self.read_the_unit(&root);
self.units_read.insert(queue_key(&root));
reading
}
pub fn a_module_unit_is_waiting_for_its_program(&self) -> bool {
self.programs_wanted
.iter()
.any(|key| !self.units_read.contains(key.as_str()))
}
pub fn the_class_after_the_operator(
&self,
view: &crate::FileView,
written: &str,
arrow: bool,
) -> Option<String> {
let written = crate::sema::types::parse_type_spelling(written);
crate::completion::the_class_after_the_operator(
self.store.index(),
&view.scopes,
&view.root,
&view.path,
&written,
arrow,
)
.map(|(class, _)| class)
}
pub fn read_a_module_unit(&mut self) -> bool {
let Some(key) = self
.programs_wanted
.iter()
.find(|key| !self.units_read.contains(*key))
.cloned()
else {
return false;
};
let path = std::path::PathBuf::from(&key);
if self.store.index().summary(&path).is_none() {
self.load(&path);
}
self.want_the_closure_cooked(&path);
let mut rounds = 0;
while self.pending() > 0 && rounds < 2_000 {
self.advance(256);
rounds += 1;
}
let reading = self.read_the_unit(&path);
if reading.is_some() {
self.units_read.insert(key);
}
reading.is_some()
}
pub fn read_a_looked_at_unit(&mut self) -> Option<UnitReading> {
let root = self.the_next_unit_to_read()?;
let reading = self.read_the_unit(&root);
let key = queue_key(&root);
let attempts = self.units_attempted.entry(key.clone()).or_insert(0);
*attempts += 1;
if reading.is_some() || *attempts >= 3 {
self.units_read.insert(key);
}
reading
}
pub fn queue_the_modules_the_open_files_import(&mut self) -> usize {
let mut queued = 0;
for path in self.documents.paths() {
let wanted = self.modules_the_index_cannot_name(&path);
if wanted.is_empty() {
continue;
}
for unit in self.module_interface_units_of(&wanted, &path) {
self.add_project_files([unit.clone()]);
self.programs_wanted.insert(queue_key(&unit));
self.want_cooked_reading(&unit);
queued += 1;
}
}
queued
}
pub fn queue_the_modules_a_file_imports(&mut self, path: &Path) -> usize {
let wanted = self.modules_the_index_cannot_name(path);
if wanted.is_empty() {
return 0;
}
let found = self.module_interface_units_of(&wanted, path);
for unit in &found {
self.add_project_files([unit.clone()]);
self.want_cooked_reading(unit);
self.programs_wanted.insert(queue_key(unit));
self.queue.again(unit.clone(), Priority::Open, 0);
}
found.len()
}
pub fn read_the_modules_a_file_imports(&mut self, path: &Path) -> usize {
let mut read_in = 0;
for _ in 0..MAX_MODULES_READ_AT_ONCE {
let wanted = self.modules_the_index_cannot_name(path);
if wanted.is_empty() {
break;
}
let found = self.module_interface_units_of(&wanted, path);
if found.is_empty() {
break;
}
read_in += found.len();
self.add_project_files(found.clone());
for unit in found {
self.programs_wanted.insert(queue_key(&unit));
}
self.index_everything();
}
read_in
}
fn modules_the_index_cannot_name(&self, path: &Path) -> Vec<String> {
self.modules_in_view_of(path)
.into_iter()
.filter(|module| self.store.index().interface_unit_of(module).is_none())
.collect()
}
pub fn imports_a_module_that_is_not_read(&self, path: &Path) -> bool {
!self.modules_the_index_cannot_name(path).is_empty()
}
fn module_interface_units_of(&self, wanted: &[String], importing: &Path) -> Vec<PathBuf> {
let mut found: Vec<PathBuf> = Vec::new();
let mut interner = PathInterner::new(self.files.is_case_insensitive());
let mut scanner = ModuleScanner::new(&self.files, &self.config);
let directory =
crate::paths::parent_normalized(importing, self.files.is_case_insensitive());
for module in wanted {
if let ImportOutcome::Resolved(unit) =
scanner.resolve_module(module, &directory, &mut interner)
&& let Some(entry) = scanner.units().iter().find(|entry| entry.file == unit)
{
found.push(entry.path.clone());
}
}
found.retain(|unit| self.store.index().summary(unit).is_none());
found.sort();
found.dedup();
found
}
fn modules_in_view_of(&self, path: &Path) -> Vec<String> {
let mut wanted: Vec<String> = Vec::new();
let mut pending: Vec<PathBuf> = vec![path.to_path_buf()];
let mut seen: std::collections::HashSet<String> = std::collections::HashSet::new();
while let Some(current) = pending.pop() {
if !seen.insert(queue_key(¤t)) {
continue;
}
let Some(summary) = self.store.index().summary(¤t) else {
continue;
};
for imported in &summary.modules.imports {
if !wanted.iter().any(|held| held == imported.as_ref()) {
wanted.push(imported.to_string());
}
if let Some(interface) = self.store.index().interface_unit_of(imported) {
pending.push(PathBuf::from(interface));
}
}
if !summary.modules.is_interface
&& summary.modules.partition.is_none()
&& let Some(module) = &summary.modules.module
&& !wanted.iter().any(|held| held == module.as_ref())
{
wanted.push(module.to_string());
if let Some(interface) = self.store.index().interface_unit_of(module) {
pending.push(PathBuf::from(interface));
}
}
}
wanted
}
pub fn unread_units(&self) -> usize {
self.documents
.paths()
.iter()
.filter(|path| !self.units_read.contains(&queue_key(path)))
.count()
}
pub fn cook(&mut self, path: impl AsRef<Path>) -> Option<CookedReading> {
let materials = self.cooking_materials(path.as_ref())?;
let rendered = self.render_a_cooked(&materials)?;
self.commit_a_cooked(rendered)
}
pub fn cooking_materials(&self, path: &Path) -> Option<CookingMaterials> {
let path = self.store.index().summary(path)?.path.clone();
let key = SummaryKey::new(0, self.store.context_hash(&path));
let already_declared: std::collections::HashSet<String> = self
.store
.index()
.summary(&path)
.into_iter()
.flat_map(|summary| summary.declarations.iter())
.map(crate::DeclFact::qualified_name)
.collect();
let mut unit = None;
{
let held_units = self.held_units();
for held in held_units.values() {
if held.provisional {
continue;
}
if held.unit.environment_of(&path).is_some() {
unit = Some(std::sync::Arc::clone(&held.unit));
break;
}
}
}
let unit = match unit {
Some(unit) => unit,
None => {
let included = !self.store.index().includers_of(&path).is_empty();
let has_a_command = self
.config
.database
.as_ref()
.is_some_and(|database| database.command_for(&path).is_some());
if included && !has_a_command && !self.documents.is_open(&path) {
return None;
}
self.unit_and_state_of(&path)?.0
}
};
unit.environment_of(&path)?;
let text = self.files.read(&path)?;
let text_hash = crate::cache::content_hash(&text);
let seed = MacroTable::from_marked(self.store.index().macros());
Some(CookingMaterials {
path,
key,
unit,
text,
text_hash,
seed,
already_declared,
})
}
pub fn render_a_cooked(&self, materials: &CookingMaterials) -> Option<RenderedCook> {
let CookingMaterials {
path,
key,
unit,
text,
text_hash,
seed,
already_declared,
} = materials;
let (tokens, _) = { cpp_parser::lex(text, &cpp_parser::LexerConfig::default()) };
let unit_definitions = unit.definitions();
let macros = {
crate::preprocess::cooked::FileMacros::new(
unit.environment_of(path)?,
&unit_definitions,
Some(seed),
true,
)
};
let rendered = { crate::preprocess::cooked::cook_with(text, &tokens, ¯os).render() };
let indexer = FileIndexer::new(&self.files, &self.config);
let indexed = indexer.index_rendering(path, &rendered, *key);
let reading = CookedReading {
declarations: indexed.summary.declarations.len(),
only_after_expansion: indexed
.summary
.declarations
.iter()
.filter(|fact| !already_declared.contains(&fact.qualified_name()))
.count(),
diagnostics: indexed.diagnostics.len(),
unplaced: indexed.unplaced,
mapped: indexed.mapped,
};
Some(RenderedCook {
text_hash: *text_hash,
indexed,
reading,
})
}
pub fn commit_a_cooked(&mut self, rendered: RenderedCook) -> Option<CookedReading> {
let RenderedCook {
text_hash,
indexed,
reading,
} = rendered;
let path = indexed.summary.path.clone();
let current = self
.text(&path)
.map(|text| crate::cache::content_hash(&text));
if current != Some(text_hash) {
if crate::probes::cooking() {
eprintln!(
"commit_a_cooked: dropped {} | the text changed while it was being rendered",
path.display()
);
}
self.want_cooked_reading(&path);
return None;
}
if let Ok(mut known) = self.renderings.lock() {
let key = (path.clone(), text_hash);
if crate::probes::view() {
eprintln!("commit: path {:?} hash {:?}", path, text_hash);
}
let before = known.len();
let weight = indexed.rendered.weight();
known.insert(key, std::sync::Arc::clone(&indexed.rendered), weight);
if crate::probes::cooking() {
eprintln!(
"commit_a_cooked: inserted {} | cache {} -> {}",
path.display(),
before,
known.len()
);
}
} else if crate::probes::cooking() {
eprintln!(
"commit_a_cooked: NOT inserted {} | lock {}",
path.display(),
self.renderings.lock().is_ok()
);
}
{
self.store.index_mut().insert_cooked(&path, indexed.into());
}
Some(reading)
}
pub fn claim_cooking(&mut self, how_many: usize) -> Vec<PathBuf> {
self.cooking.take(how_many)
}
pub fn refuse_the_cooks_that_built_nothing(&mut self, asked: &[PathBuf], built: &[PathBuf]) {
let Ok(mut refused) = self.cooking_refused.lock() else {
return;
};
for path in asked {
if !built.iter().any(|made| made == path) {
refused.insert(queue_key(path));
}
}
}
pub fn materials_for(&self, paths: Vec<PathBuf>) -> Vec<CookingMaterials> {
paths
.into_iter()
.filter_map(|path| {
let built = self.cooking_materials(&path);
if built.is_none() && crate::probes::cooking() {
let indexed = self.store.index().summary(&path).is_some();
let (unit, provisional) = match self.unit_and_state_of(&path) {
Some((_, provisional)) => ("held", provisional),
None => ("none", false),
};
eprintln!(
"cooking: {} yields no materials | summary indexed {indexed} | unit {unit} \
provisional {provisional}",
path.display()
);
}
built
})
.collect()
}
pub fn render_them(&self, materials: Vec<CookingMaterials>) -> Vec<RenderedCook> {
let tracing = crate::probes::cooking();
materials
.into_iter()
.filter_map(|materials| {
let path = materials.path.clone();
let built = self.render_a_cooked(&materials);
if built.is_none() && tracing {
eprintln!("cooking: {} did not render", path.display());
}
built
})
.collect()
}
pub fn commit_them(&mut self, rendered: Vec<RenderedCook>) -> Vec<CookedReading> {
rendered
.into_iter()
.filter_map(|reading| self.commit_a_cooked(reading))
.collect()
}
pub fn rendered_text_of(&self, path: impl AsRef<Path>) -> Option<String> {
Some(self.rendering_of(path)?.text)
}
pub fn rendering_of(
&self,
path: impl AsRef<Path>,
) -> Option<crate::preprocess::cooked::RenderedCooked> {
let path = self.store.index().summary(path.as_ref())?.path.clone();
let unit = self.translation_unit_of(&path)?;
let text = self.files.read(&path)?;
let seed = MacroTable::from_marked(self.store.index().macros());
let (tokens, _) = cpp_parser::lex(&text, &cpp_parser::LexerConfig::default());
let unit_definitions = unit.definitions();
let macros = crate::preprocess::cooked::FileMacros::new(
unit.environment_of(&path)?,
&unit_definitions,
Some(&seed),
true,
);
Some(crate::preprocess::cooked::cook_with(&text, &tokens, ¯os).render())
}
pub fn index_the_rendering(&mut self, path: impl AsRef<Path>) -> Option<crate::FileSummary> {
let path = self.store.index().summary(path.as_ref())?.path.clone();
let unit = self.translation_unit_of(&path)?;
let environment = unit.environment_of(&path)?;
let written = self.files.read(&path)?;
let rendered = match self.known_rendering_of(&path, &written) {
Some(rendered) => rendered,
None => std::sync::Arc::new(self.rendering_of(&path)?),
};
self.store
.index_the_rendering(&path, &rendered, Some(&environment))
.cloned()
}
pub fn fact_from_the_rendering(
&self,
path: impl AsRef<Path>,
name: &str,
scope: Option<&str>,
) -> Option<crate::DeclFact> {
let path = self.store.index().summary(path.as_ref())?.path.clone();
let text = self.files.read(&path)?;
let rendered = match self.known_rendering_of(&path, &text) {
Some(rendered) => rendered,
None => {
let built = std::sync::Arc::new(self.rendering_of(&path)?);
if let Ok(mut known) = self.renderings.lock() {
let weight = built.weight();
known.insert(
(path.clone(), crate::cache::content_hash(&text)),
built.clone(),
weight,
);
}
built
}
};
let key = (path.clone(), crate::cache::content_hash(&rendered.text));
let remembered = self
.rendering_summaries
.lock()
.ok()
.and_then(|mut known| known.get(&key).cloned());
let summary = match remembered {
Some(summary) => summary,
None => {
let built = std::sync::Arc::new(crate::summarize(
&path,
&rendered.text,
crate::SummaryKey::new(0, 0),
));
if let Ok(mut known) = self.rendering_summaries.lock() {
let weight = built.declarations.len() + built.macros.len();
known.insert(key, built.clone(), weight);
}
built
}
};
summary
.declarations
.iter()
.find(|fact| {
fact.name == name && fact.scope.as_deref() == scope && !fact.split_by_a_directive
})
.cloned()
}
pub fn cook_the_open_files(&mut self) -> Vec<(PathBuf, CookedReading)> {
let open = self.documents.paths();
let mut done = Vec::new();
for path in open {
if let Some(reading) = self.cook(&path) {
done.push((path, reading));
}
}
done
}
fn closure_with_text(&self, root: &Path) -> (Vec<(PathBuf, String)>, HashMap<String, usize>) {
let seed = self.store.index().summary(root);
self.closure_with_text_from(root, seed)
}
fn closure_with_text_from(
&self,
root: &Path,
seed: Option<&FileSummary>,
) -> (Vec<(PathBuf, String)>, HashMap<String, usize>) {
let mut out: Vec<(PathBuf, String)> = Vec::new();
let mut by_key: HashMap<String, usize> = HashMap::new();
for path in self.closure_paths_from(root, seed) {
if let Some(text) = self.text(&path) {
by_key.insert(queue_key(&path), out.len());
out.push((path, text));
}
}
(out, by_key)
}
fn closure_paths_from(&self, root: &Path, seed: Option<&FileSummary>) -> Vec<PathBuf> {
let mut out: Vec<PathBuf> = Vec::new();
let mut seen: HashSet<String> = HashSet::new();
let mut queue: std::collections::VecDeque<PathBuf> = std::collections::VecDeque::new();
queue.push_back(root.to_path_buf());
let mut edges: Option<&FileSummary> = seed;
while let Some(path) = queue.pop_front() {
if !seen.insert(queue_key(&path)) {
continue;
}
out.push(path.clone());
let summary = match edges.take() {
Some(seed) => Some(seed),
None => self.store.index().summary(&path),
};
if let Some(summary) = summary {
for include in &summary.includes {
if let Some(resolved) = &include.resolved {
queue.push_back(resolved.clone());
}
}
}
}
out
}
fn the_closure_of_the_directives(
&self,
root: &Path,
directives: &FileSummary,
) -> Option<Vec<PathBuf>> {
let index = self.store.index();
let root_key = queue_key(root);
let reached = self.closure_paths_from(root, Some(directives));
reached
.iter()
.all(|path| queue_key(path) == root_key || index.summary(path).is_some())
.then_some(reached)
}
pub fn definition(&self, view: &FileView, offset: ReadingOffset) -> Known<ProjectDefinition> {
definition_across_files(
self.store.index(),
&mut |path| self.view(path),
&view.scopes,
&view.root,
&view.path,
offset.get(),
)
}
pub fn definitions(&self, view: &FileView, offset: ReadingOffset) -> Known<ProjectDefinitions> {
definitions_across_files(
self.store.index(),
&mut |path| self.view(path),
&view.scopes,
&view.root,
&view.path,
offset.get(),
)
}
pub fn symbol_references(
&self,
view: &FileView,
offset: ReadingOffset,
budget: crate::ReferenceBudget,
) -> Known<crate::SymbolReferences> {
let Known::Yes(found) = self.definitions(view, offset) else {
let written = crate::sema::resolve::name_position_at(&view.root, offset.get())
.map(|name| name.written.clone())
.unwrap_or_default();
return Known::Unknown(UnknownReason::NotDeclaredHere(Box::from(written)));
};
let Some(first) = found.found.first() else {
return Known::Unknown(UnknownReason::NotDeclaredHere(Box::from("")));
};
let symbol = crate::SymbolToFind::of(&first.fact, first.file.clone());
crate::index::references::symbol_references(
self.store.index(),
&self.files,
&symbol,
budget,
)
}
pub fn macro_at_a_file_offset(
&self,
view: &FileView,
offset: crate::file::coordinates::FileOffset,
) -> Known<ProjectMacro> {
let Some(written) = view.written_text() else {
return Known::Unknown(UnknownReason::NotDeclaredHere(Box::from("no written text")));
};
let at = offset.get();
let is_a_word = |byte: u8| byte.is_ascii_alphanumeric() || byte == b'_';
let bytes = written.as_bytes();
let mut start = at.min(bytes.len());
while start > 0 && is_a_word(bytes[start - 1]) {
start -= 1;
}
let mut end = at.min(bytes.len());
while end < bytes.len() && is_a_word(bytes[end]) {
end += 1;
}
let name = &written[start..end];
if name.is_empty() {
return Known::Unknown(UnknownReason::UnparsableName);
}
self.store.index().macro_definition(name, &view.path, at)
}
pub fn module_names(&self) -> Vec<(String, PathBuf)> {
let mut found: Vec<(String, PathBuf)> = self
.store
.index()
.interface_units()
.into_iter()
.map(|(name, unit)| (name, PathBuf::from(unit)))
.collect();
const A_MODULE_FILE: &[&str] = &["cppm", "ixx", "ccm", "cxxm"];
for path in &self.project {
let Some(extension) = path.extension().and_then(|extension| extension.to_str()) else {
continue;
};
if !A_MODULE_FILE
.iter()
.any(|known| known.eq_ignore_ascii_case(extension))
{
continue;
}
let Some(name) = path
.file_stem()
.and_then(|stem| stem.to_str())
.filter(|stem| !stem.is_empty())
else {
continue;
};
found.push((name.to_string(), path.clone()));
}
found.sort_by(|left, right| left.0.cmp(&right.0));
found.dedup_by(|later, earlier| later.0 == earlier.0);
found
}
fn module_completions(
&self,
written: &str,
range: cpp_parser::SourceRange,
) -> crate::CompletionSet {
let mut items: Vec<crate::completion::CompletionItem> = Vec::new();
for (name, unit) in self.module_names() {
if !written.is_empty()
&& !name
.to_ascii_lowercase()
.starts_with(&written.to_ascii_lowercase())
{
continue;
}
items.push(crate::completion::CompletionItem {
label: name.clone(),
insert: name.clone(),
snippet: false,
kind: crate::completion::ItemKind::Namespace,
detail: Some(format!("module {}", unit.display())),
identity: None,
filter: None,
fact: None,
replace: range,
});
}
crate::CompletionSet {
scope: String::new(),
prefix: written.to_string(),
replace: range,
truncated: false,
incomplete: false,
items,
}
}
pub fn constant_value_at(&self, view: &FileView, offset: ReadingOffset) -> Option<i64> {
let path = view.path.clone();
crate::sema::constant::value_at(view, offset.get(), &|name| {
self.macro_body_at(&path, offset.get(), name)
})
}
pub fn macro_body_at(&self, path: &Path, offset: usize, name: &str) -> Option<String> {
let environment = self.store.index().macro_environment(name, path);
let Known::Yes(found) = environment.at(offset) else {
return None;
};
let fact = found.fact();
if fact.function_like {
return None;
}
let range = fact.body_range?;
let text = self.text(found.file())?;
let body = text.get(range.start_offset..range.end_offset())?;
(!body.trim().is_empty()).then(|| body.trim().to_string())
}
pub fn macro_definition(&self, view: &FileView, offset: ReadingOffset) -> Known<ProjectMacro> {
macro_across_files(self.store.index(), &view.root, &view.path, offset.get())
}
pub fn module_imports_of(&self, view: &FileView) -> Vec<ModuleImport> {
let mut imports: Vec<ModuleImport> = Vec::new();
let directory =
crate::paths::parent_normalized(&view.path, self.files.is_case_insensitive());
let mut interner = PathInterner::new(self.files.is_case_insensitive());
let mut scanner: Option<ModuleScanner<'_, OverlayFiles<OpenDocuments, F>>> = None;
for node in view.root.descendants() {
if cpp_parser::CppSyntaxKind::from(node.kind()) != cpp_parser::CppSyntaxKind::ImportDecl
{
continue;
}
let Some(name_node) = node.descendants().find(|inner| {
cpp_parser::CppSyntaxKind::from(inner.kind())
== cpp_parser::CppSyntaxKind::ModuleName
}) else {
continue;
};
let name = name_node.text().to_string().trim().to_string();
if name.is_empty() {
continue;
}
let range = cpp_parser::source_range(name_node.text_range());
let resolved = match self.store.index().interface_unit_of(&name) {
Some(path) => Some(PathBuf::from(path)),
None => {
let scanner = scanner
.get_or_insert_with(|| ModuleScanner::new(&self.files, &self.config));
match scanner.resolve_module(&name, &directory, &mut interner) {
ImportOutcome::Resolved(unit) => scanner
.units()
.iter()
.find(|entry| entry.file == unit)
.map(|entry| entry.path.clone()),
_ => None,
}
}
};
imports.push(ModuleImport {
name,
range,
resolved,
});
}
imports
}
pub fn header_at(&self, view: &FileView, offset: FileOffset) -> Known<crate::HeaderTarget> {
let index = self.store.index();
let Some(written) = view.written_text() else {
return crate::header_at(index, &view.root, &view.path, offset.get());
};
let tree = cpp_parser::CppParser::parse(written, cpp_parser::ParserConfig::default());
crate::header_at(index, &tree.get_red_root(), &view.path, offset.get())
}
pub fn macro_references(
&self,
view: &FileView,
offset: ReadingOffset,
) -> Known<MacroReferences> {
let Some((name, _)) =
crate::sema::resolve::name_at_including_directives(&view.root, offset.get())
else {
return Known::Unknown(UnknownReason::UnparsableName);
};
macro_references(
self.store.index(),
&self.files,
&name,
ReferenceBudget::default(),
)
}
pub fn member_completions(
&self,
view: &FileView,
offset: FileOffset,
) -> Known<MemberCompletions> {
let Some(offset) = view.reading_offset_of(offset) else {
return Known::Unknown(UnknownReason::NotDeclaredHere(Box::from("")));
};
member_completions_at(
self.store.index(),
&mut |path| self.view(path),
&view.scopes,
&view.root,
&view.path,
offset.get(),
)
}
pub fn name_completions(&self, view: &FileView, offset: FileOffset) -> Known<NameCompletions> {
if let Some(written) = view.written_text()
&& crate::file::view::in_prose(written, offset.get())
{
return Known::Unknown(UnknownReason::NotDeclaredHere(Box::from("")));
}
let Some(offset) = view.reading_offset_of(offset) else {
return Known::Unknown(UnknownReason::NotDeclaredHere(Box::from("")));
};
crate::index::project::name_completions_traced(
self.store.index(),
&view.scopes,
&view.root,
&view.path,
offset.get(),
)
}
pub fn completions(&self, view: &FileView, offset: FileOffset) -> crate::CompletionSet {
{
let source: &str = view.written_text().unwrap_or(&view.source);
if let Some((written, range)) =
crate::completion::context::module_name_from_the_text(source, offset.get())
{
return self.module_completions(&written, range);
}
}
if let Some(written) = view.written_text() {
let tree = cpp_parser::CppParser::parse(written, cpp_parser::ParserConfig::default());
let written_root = tree.get_red_root();
if let Some(context @ crate::completion::CompletionContext::DocComment { .. }) =
crate::completion::doc_comment_at(&written_root, offset.get())
{
return crate::completion::completion_with(
self.store.index(),
&view.scopes,
&view.root,
&view.path,
offset.get(),
&self
.headers
.read()
.map(|headers| headers.clone())
.unwrap_or_default(),
context,
self.config.cuda(),
);
}
if crate::file::view::in_prose(written, offset.get()) {
return crate::CompletionSet::default();
}
let context = crate::completion::context_at(&written_root, offset.get());
match context {
crate::completion::CompletionContext::DirectiveArgument { .. }
| crate::completion::CompletionContext::Qualified(_) => {
let in_the_reading = view
.reading_offset_of(offset)
.map_or(view.source.len(), |at| at.get());
return crate::completion::completion_with(
self.store.index(),
&view.scopes,
&view.root,
&view.path,
in_the_reading,
&self
.headers
.read()
.map(|headers| headers.clone())
.unwrap_or_default(),
context,
self.config.cuda(),
);
}
context @ (crate::completion::CompletionContext::DirectiveName { .. }
| crate::completion::CompletionContext::Include { .. }
| crate::completion::CompletionContext::MacroBody { .. }
| crate::completion::CompletionContext::Condition { .. }) => {
return crate::completion::completion_with(
self.store.index(),
&view.scopes,
&view.root,
&view.path,
offset.get(),
&self
.headers
.read()
.map(|headers| headers.clone())
.unwrap_or_default(),
context,
self.config.cuda(),
);
}
crate::completion::CompletionContext::Nothing => {
return crate::CompletionSet::default();
}
_ => {}
}
}
let Some(offset) = view.reading_offset_of(offset) else {
return crate::CompletionSet::default();
};
let headers = self
.headers
.read()
.map(|headers| headers.clone())
.unwrap_or_default();
crate::completion::completion_at(
self.store.index(),
&view.scopes,
&view.root,
&view.path,
offset.get(),
&headers,
self.config.cuda(),
)
}
pub fn headers(&self) -> crate::HeaderIndex {
self.headers
.read()
.map(|headers| headers.clone())
.unwrap_or_default()
}
pub fn documentation(
&self,
view: &FileView,
file: &Path,
offset: usize,
) -> Option<cpp_parser::CppDocComment> {
if file == view.path {
let Some(written) = view.written_text() else {
return crate::file::view::documentation_at(&view.root, offset);
};
if !crate::file::view::might_be_documented(written, offset) {
return None;
}
let tree = cpp_parser::CppParser::parse(written, cpp_parser::ParserConfig::default());
return crate::file::view::documentation_at(&tree.get_red_root(), offset);
}
let declared_in = self.vfs.held(file)?;
if !crate::file::view::might_be_documented(&declared_in.text, offset) {
return None;
}
let tree =
cpp_parser::CppParser::parse(&declared_in.text, cpp_parser::ParserConfig::default());
crate::file::view::documentation_at(&tree.get_red_root(), offset)
}
pub fn type_at(&self, view: &FileView, offset: ReadingOffset) -> Known<ExpressionType> {
let Some(expression) = crate::sema::resolve::expression_at(&view.root, offset.get()) else {
return Known::No;
};
let written = expression.text().to_string().trim().to_string();
match crate::index::project::type_of_expression(
self.store.index(),
&mut |path| self.view(path),
&view.scopes,
&view.root,
&view.path,
&expression,
0,
) {
Known::Yes((type_of, file)) => Known::Yes(ExpressionType {
expression: written,
type_of: type_of.to_string(),
file,
}),
Known::Unknown(reason) => Known::Unknown(reason),
Known::No => Known::No,
}
}
pub fn inlay_hints(
&self,
view: &FileView,
range: cpp_parser::SourceRange,
) -> Vec<ParameterHint> {
let mut hints = crate::inlay::parameter_hints(self.store.index(), view, range, |path| {
self.view_of_the_file(path)
});
for hint in &mut hints {
let Some(placed) = view.file_offset_of(ReadingOffset::new(hint.offset)) else {
continue;
};
hint.offset = placed.get();
}
hints
}
pub fn classified_names(&self, view: &FileView) -> Vec<crate::semantic::Name> {
let mut names = crate::semantic::classified_names(self.store.index(), view);
let mut placed = Vec::with_capacity(names.len());
for mut name in names {
let Some(start) = view.file_offset_of(ReadingOffset::new(name.range.start_offset))
else {
continue;
};
let end = view
.file_offset_of(ReadingOffset::new(name.range.end_offset()))
.unwrap_or(start);
name.range =
cpp_parser::SourceRange::new(start.get(), end.get().saturating_sub(start.get()));
placed.push(name);
}
names = placed;
if view
.written_text()
.is_some_and(|written| written != &*view.source)
&& let Some(own) = self.view_of_the_file(&view.path)
{
let mut at_offset: std::collections::HashMap<usize, usize> = names
.iter()
.enumerate()
.map(|(index, name)| (name.range.start_offset, index))
.collect();
for name in crate::semantic::classified_names(self.store.index(), &own) {
match at_offset.get(&name.range.start_offset) {
Some(&index) => names[index] = name,
None => {
at_offset.insert(name.range.start_offset, names.len());
names.push(name);
}
}
}
}
names.sort_by_key(|name| (name.range.start_offset, name.range.end_offset()));
names
}
pub fn format_literals(
&self,
view: &FileView,
config: &crate::format::FormatConfig,
) -> Vec<crate::format::FormatLiteral> {
if view.written_text().is_some() {
return Vec::new();
}
let mut qualified_name = |offset: usize| -> Option<String> {
let reading = view.reading_offset_of(FileOffset::new(offset))?;
let Known::Yes(binding) =
crate::sema::resolve::definition_at(&view.scopes, &view.root, reading.get())
else {
return None;
};
let name = binding.name.identifier_text()?;
Some(match view.scopes.qualified_name_of(binding.scope) {
Some(scope) if !scope.is_empty() => format!("{scope}::{name}"),
_ => name.to_string(),
})
};
crate::format::literals_in(
&view.source,
view.tree.get_tokens(),
config,
&mut qualified_name,
)
}
pub fn selection_chain(
&self,
view: &FileView,
offset: FileOffset,
) -> Vec<cpp_parser::SourceRange> {
let Some(written) = view.written_text() else {
return view.selection_chain(ReadingOffset::new(offset.get()));
};
let tree = cpp_parser::CppParser::parse(written, cpp_parser::ParserConfig::default());
crate::file::view::selection_chain_of(&tree.get_red_root(), offset.get())
}
pub fn folding_ranges(&self, view: &FileView) -> Vec<crate::folding::Fold> {
let Some(written) = view.written_text() else {
return crate::folding::folding_ranges(&view.source, view.tree.get_tokens());
};
let (tokens, _) = cpp_parser::lex(written, &cpp_parser::LexerConfig::default());
crate::folding::folding_ranges(written, &tokens)
}
pub fn signatures_at(
&self,
view: &FileView,
offset: ReadingOffset,
) -> Vec<crate::signature::CallSignature> {
let mut signatures =
crate::signature::signatures_at(self.store.index(), view, offset.get(), |path| {
self.view(path)
});
for signature in &mut signatures {
let declared = self.view(&signature.declared_in);
let Some(in_the_file) = declared.as_ref().and_then(|declared| {
declared.file_offset_of(ReadingOffset::new(signature.declared_at))
}) else {
continue;
};
signature.documentation =
self.documentation(view, &signature.declared_in, in_the_file.get());
}
signatures
}
pub fn the_declaration_a_call_means(
&self,
view: &FileView,
offset: ReadingOffset,
parameter_lists: &[Option<String>],
template_parameters: &[Vec<String>],
) -> Option<usize> {
let token = cpp_parser::token_at(&view.root, offset.get())?;
let call = token.parent_ancestors().find(|node| {
cpp_parser::CppSyntaxKind::from(node.kind()) == cpp_parser::CppSyntaxKind::CallExpr
})?;
let arguments = crate::inlay::arguments_of(&call)?;
let session: &Session<F> = self;
let index: &crate::ProjectIndex = session.store.index();
let resolver =
Box::new(move |path: &Path| -> Option<FileView> { session.view_of_the_file(path) });
let model = crate::sema::model::SemanticModel::new(
index,
&view.root,
&view.scopes,
&view.source,
&view.path,
resolver,
);
let types: Vec<crate::Known<String>> = arguments
.iter()
.map(|argument| match model.type_of(argument) {
crate::Known::Yes((a_type, _)) => crate::Known::Yes(a_type.to_string()),
crate::Known::No => crate::Known::No,
crate::Known::Unknown(reason) => crate::Known::Unknown(reason),
})
.collect();
let candidates: Vec<crate::sema::overload::Candidate> = parameter_lists
.iter()
.enumerate()
.map(|(at, parameters)| {
let (declared, variadic) = parameters
.as_deref()
.map(crate::sema::overload::parameters_in)
.unwrap_or_default();
let names = template_parameters.get(at).cloned().unwrap_or_default();
let declared_for_ordering = declared.clone();
let (types, is_a_template) =
match crate::sema::deduction::deduce(&names, &declared, &types) {
Some(arguments) => (
declared
.iter()
.map(|spelling| {
crate::sema::types::substituted_spelling(
spelling, &names, &arguments,
)
})
.collect(),
true,
),
None => (declared, !names.is_empty()),
};
let candidate = crate::sema::overload::Candidate::new(
types,
variadic,
format!("declaration {at}"),
);
if is_a_template {
candidate.with_declaration(declared_for_ordering, names.clone())
} else {
candidate
}
})
.collect();
match crate::sema::overload::choose(&types, &candidates) {
crate::sema::overload::Choice::One(chosen) => Some(chosen),
_ => None,
}
}
pub fn the_deduced_arguments(
&self,
view: &FileView,
offset: ReadingOffset,
template_parameters: &[String],
parameters: &[String],
) -> Option<Vec<String>> {
if template_parameters.is_empty() {
return None;
}
let token = cpp_parser::token_at(&view.root, offset.get())?;
let call = token.parent_ancestors().find(|node| {
cpp_parser::CppSyntaxKind::from(node.kind()) == cpp_parser::CppSyntaxKind::CallExpr
})?;
let arguments = crate::inlay::arguments_of(&call)?;
let session: &Session<F> = self;
let index: &crate::ProjectIndex = session.store.index();
let resolver =
Box::new(move |path: &Path| -> Option<FileView> { session.view_of_the_file(path) });
let model = crate::sema::model::SemanticModel::new(
index,
&view.root,
&view.scopes,
&view.source,
&view.path,
resolver,
);
let types: Vec<crate::Known<String>> = arguments
.iter()
.map(|argument| match model.type_of(argument) {
crate::Known::Yes((a_type, _)) => crate::Known::Yes(a_type.to_string()),
crate::Known::No => crate::Known::No,
crate::Known::Unknown(reason) => crate::Known::Unknown(reason),
})
.collect();
crate::sema::deduction::deduce(template_parameters, parameters, &types)
}
pub fn members_of(&self, view: &FileView, written_type: &str) -> Known<MemberList> {
let listed = members_of(
self.store.index(),
&view.scopes,
&view.root,
&view.path,
written_type,
);
substituted_members(self.store.index(), &view.path, written_type, listed)
}
pub fn definition_of_a_written_type(
&self,
view: &FileView,
written: &str,
in_scope: Option<&str>,
at: usize,
) -> Known<ProjectDefinition> {
let index = self.store.index();
let enclosing = match in_scope {
Some(scope) => Some(scope.to_string()),
None => enclosing_namespace_at(view, at),
};
let direct = index.definition_where_written(written, enclosing.as_deref(), &view.path);
if !matches!(direct, Known::Unknown(UnknownReason::NotDeclaredHere(_))) {
return direct;
}
let in_scope = enclosing.as_deref();
let Some((class, member)) = written.rsplit_once("::") else {
return direct;
};
let mut tried: Vec<String> = Vec::new();
if let Some(scope) = in_scope.filter(|_| !class.starts_with("::") && !class.contains("::"))
{
tried.push(format!("{scope}::{class}"));
}
tried.push(class.to_string());
let mut list = None;
for spelling in &tried {
if let Known::Yes(found) = self.members_of(view, spelling) {
list = Some(found);
break;
}
}
let Some(list) = list else {
return direct;
};
let mut found = list
.members
.iter()
.filter(|found| found.fact.name == member);
let Some(only) = found.next() else {
return direct;
};
if found.next().is_some() || only.ambiguous {
return Known::Unknown(UnknownReason::Ambiguous(Box::from(written)));
}
Known::Yes(ProjectDefinition {
file: only.file.clone(),
fact: only.fact.clone(),
})
}
pub fn outline(&self, view: &FileView) -> Vec<OutlineSymbol> {
let preprocessing = crate::preprocess(&view.source, view.tree.get_tokens());
let errors: Vec<cpp_parser::SourceRange> = view
.tree
.get_errors()
.iter()
.map(|error| cpp_parser::source_range(error.range))
.collect();
let (facts, _guards) =
crate::build_facts(&view.scopes, &preprocessing, &view.root, &errors);
crate::outline_of(&facts)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DiagnosticReading {
Cooked,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExpressionType {
pub expression: String,
pub type_of: String,
pub file: PathBuf,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FileDiagnostic {
pub start: usize,
pub end: usize,
pub message: String,
pub code: crate::sema::check::DiagnosticCode,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModuleNote {
pub message: String,
pub start: usize,
pub end: usize,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FileDiagnostics {
pub reading: DiagnosticReading,
pub unplaced: usize,
pub errors: Vec<FileDiagnostic>,
pub notes: Vec<ModuleNote>,
pub checks: Vec<crate::sema::check::Finding>,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct CookedReading {
pub declarations: usize,
pub only_after_expansion: usize,
pub diagnostics: usize,
pub unplaced: usize,
pub mapped: crate::MapReport,
}
const MAX_UNITS: usize = 16;
const MAX_UNIT_BYTES: usize = 128 * 1024 * 1024;
const MAX_WALKS_BY_CONTENT_ENTRIES: usize = 512;
const MAX_WALKS_BY_CONTENT_BYTES: usize = 64 * 1024 * 1024;
const MAX_MODULES_READ_AT_ONCE: usize = 16;
#[derive(Default)]
struct UnitTable {
held: std::collections::HashMap<String, HeldUnit>,
clock: u64,
bytes: usize,
}
impl UnitTable {
fn values(&self) -> impl Iterator<Item = &HeldUnit> {
self.held.values()
}
fn get(&mut self, key: &str) -> Option<(bool, std::sync::Arc<crate::TranslationUnit>)> {
self.clock += 1;
let held = self.held.get_mut(key)?;
held.used = self.clock;
Some((held.provisional, held.unit.clone()))
}
fn insert(
&mut self,
key: String,
unit: std::sync::Arc<crate::TranslationUnit>,
provisional: bool,
) {
self.clock += 1;
let weight = unit.heap_bytes();
if let Some(replaced) = self.held.remove(&key) {
self.bytes = self.bytes.saturating_sub(replaced.weight);
}
while self.held.len() + 1 > MAX_UNITS || self.bytes + weight > MAX_UNIT_BYTES {
if self.held.is_empty() {
break;
}
let smallest = self
.held
.iter()
.min_by_key(|(_, held)| (held.weight, held.used))
.map(|(key, _)| key.clone());
let Some(smallest) = smallest else {
break;
};
if let Some(removed) = self.held.remove(&smallest) {
self.bytes = self.bytes.saturating_sub(removed.weight);
}
}
self.bytes = self.bytes.saturating_add(weight);
self.held.insert(
key,
HeldUnit {
used: self.clock,
provisional,
unit,
weight,
},
);
}
fn remove(&mut self, key: &str) {
if let Some(removed) = self.held.remove(key) {
self.bytes = self.bytes.saturating_sub(removed.weight);
}
}
fn clear(&mut self) {
self.held.clear();
self.bytes = 0;
}
fn accounting(&self) -> (usize, usize) {
(self.held.len(), self.bytes)
}
}
struct HeldUnit {
used: u64,
provisional: bool,
unit: std::sync::Arc<crate::TranslationUnit>,
weight: usize,
}
#[derive(Debug, Clone, Copy)]
struct DirectivesMoved {
environment: bool,
layout: bool,
}
#[derive(Debug, Default)]
struct Cooking {
wanted: VecDeque<PathBuf>,
held: HashSet<String>,
}
impl Cooking {
fn want(&mut self, path: &Path) -> bool {
if !self.held.insert(queue_key(path)) {
return false;
}
self.wanted.push_front(path.to_path_buf());
true
}
fn wants(&self, path: &Path) -> bool {
self.held.contains(&queue_key(path))
}
fn take(&mut self, how_many: usize) -> Vec<PathBuf> {
let mut taken = Vec::new();
for _ in 0..how_many {
let Some(path) = self.wanted.pop_front() else {
break;
};
self.held.remove(&queue_key(&path));
taken.push(path);
}
taken
}
fn len(&self) -> usize {
self.wanted.len()
}
fn abandon(&mut self) -> usize {
let held = self.wanted.len();
self.wanted.clear();
self.held.clear();
held
}
}
#[derive(Debug, Default)]
struct Work {
open: VecDeque<(PathBuf, usize)>,
rest: VecDeque<(PathBuf, usize)>,
standing: HashMap<String, Standing>,
queued: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Standing {
Queued(Priority),
Worked,
}
impl Work {
fn add(&mut self, path: PathBuf, priority: Priority, depth: usize) -> bool {
let key = queue_key(&path);
match self.standing.get(&key) {
None => {
self.standing.insert(key, Standing::Queued(priority));
self.push(path, priority, depth);
self.queued += 1;
true
}
Some(Standing::Queued(Priority::Rest)) if priority == Priority::Open => {
self.standing.insert(key, Standing::Queued(Priority::Open));
self.push(path, priority, depth);
true
}
Some(_) => false,
}
}
fn again(&mut self, path: PathBuf, priority: Priority, depth: usize) {
self.rearm(path, priority, depth, true);
}
fn requeue(&mut self, path: PathBuf, priority: Priority, depth: usize) {
self.rearm(path, priority, depth, false);
}
fn rearm(&mut self, path: PathBuf, priority: Priority, depth: usize, urgent: bool) {
let key = queue_key(&path);
if !matches!(self.standing.get(&key), Some(Standing::Queued(_))) {
self.queued += 1;
}
self.standing.insert(key, Standing::Queued(priority));
match (priority, urgent) {
(Priority::Open, true) => self.open.push_front((path, depth)),
(Priority::Open, false) => self.open.push_back((path, depth)),
(Priority::Rest, true) => self.rest.push_front((path, depth)),
(Priority::Rest, false) => self.rest.push_back((path, depth)),
}
}
fn forget(&mut self, path: &Path) -> bool {
if !self.is_queued(path) {
return false;
}
self.standing.insert(queue_key(path), Standing::Worked);
self.queued -= 1;
true
}
fn is_queued(&self, path: &Path) -> bool {
matches!(
self.standing.get(&queue_key(path)),
Some(Standing::Queued(_))
)
}
fn pop(&mut self) -> Option<(PathBuf, Priority, usize)> {
loop {
let (path, priority, depth) = match self.open.pop_front() {
Some((path, depth)) => (path, Priority::Open, depth),
None => {
let (path, depth) = self.rest.pop_front()?;
(path, Priority::Rest, depth)
}
};
let key = queue_key(&path);
match self.standing.get(&key) {
Some(Standing::Queued(held)) if *held == priority => {
self.standing.insert(key, Standing::Worked);
self.queued -= 1;
return Some((path, priority, depth));
}
_ => continue,
}
}
}
fn push(&mut self, path: PathBuf, priority: Priority, depth: usize) {
match priority {
Priority::Open => self.open.push_back((path, depth)),
Priority::Rest => self.rest.push_back((path, depth)),
}
}
fn pending(&self) -> usize {
self.queued
}
fn is_worked(&self, path: &Path) -> bool {
matches!(self.standing.get(&queue_key(path)), Some(Standing::Worked))
}
fn mark_worked(&mut self, path: &Path) {
let key = queue_key(path);
match self.standing.get(&key) {
Some(Standing::Queued(_)) => {
self.standing.insert(key, Standing::Worked);
self.queued -= 1;
}
Some(Standing::Worked) => {}
None => {
self.standing.insert(key, Standing::Worked);
}
}
}
fn peek(&self, how_many: usize) -> Vec<PathBuf> {
let mut seen: std::collections::HashSet<String> = std::collections::HashSet::new();
let mut ahead = Vec::new();
let halves = [(&self.open, Priority::Open), (&self.rest, Priority::Rest)];
for (half, priority) in halves {
for (path, _) in half {
if ahead.len() >= how_many {
return ahead;
}
let key = queue_key(path);
let queued_here = matches!(
self.standing.get(&key),
Some(Standing::Queued(held)) if *held == priority
);
if queued_here && seen.insert(key) {
ahead.push(path.clone());
}
}
}
ahead
}
}
fn enclosing_namespace_at(view: &FileView, at: usize) -> Option<String> {
let scope = view.scopes.scope_at(at)?;
let mut segments: Vec<&str> = Vec::new();
for id in view.scopes.scope_chain(scope) {
let Some(data) = view.scopes.scope(id) else {
continue;
};
if data.kind == crate::ScopeKind::Namespace
&& let Some(name) = data.name.as_deref()
{
segments.push(name);
}
}
if segments.is_empty() {
return None;
}
segments.reverse();
Some(segments.join("::"))
}
fn queue_key(path: &Path) -> String {
normalize_path(path, cfg!(windows))
}
fn the_compilation_is_known(toolchain: Option<&Toolchain>) -> bool {
toolchain.is_some_and(|found| !found.builtin_macros.is_empty())
}
fn apply_project_config(filter: WatchFilter, report: &ConfigReport) -> WatchFilter {
let mut filter = filter;
for pattern in &report.config.workspace.exclude {
if let Ok(pattern) = crate::PathPattern::new(pattern) {
filter = filter.ignore_pattern(pattern);
}
}
if let Some(cache_dir) = &report.config.index.cache_dir {
filter = filter.with_cache_directory(cache_dir);
}
filter
}
fn project_files(
files: &impl FileProvider,
filter: &WatchFilter,
root: &Path,
database: Option<&CompileCommands>,
extra_extensions: &[String],
max_files: usize,
) -> Vec<PathBuf> {
let mut found = Vec::new();
if let Some(database) = database {
for command in &database.commands {
let path = match (&command.directory, command.file.is_relative()) {
(Some(directory), true) => directory.join(&command.file),
_ => command.file.clone(),
};
if files.exists(&path) && !filter.is_ignored(&path) {
found.push(path);
}
}
}
if found.is_empty() {
scan(root, filter, &mut found, extra_extensions, max_files);
}
found.sort();
found.dedup();
found
}
fn scan(
root: &Path,
filter: &WatchFilter,
found: &mut Vec<PathBuf>,
extensions: &[String],
max_files: usize,
) {
let mut pending = vec![root.to_path_buf()];
while let Some(directory) = pending.pop() {
if found.len() >= max_files {
return;
}
let Ok(entries) = std::fs::read_dir(&directory) else {
continue;
};
for entry in entries.flatten() {
if found.len() >= max_files {
return;
}
let path = entry.path();
if filter.is_ignored(&path) {
continue;
}
let Ok(kind) = entry.file_type() else {
continue;
};
if kind.is_symlink() {
continue;
}
if kind.is_dir() {
pending.push(path);
} else if kind.is_file() && is_a_source_file(&path, extensions) {
found.push(path);
}
}
}
}
fn is_a_source_file(path: &Path, extra: &[String]) -> bool {
path.extension()
.and_then(|extension| extension.to_str())
.is_some_and(|extension| {
let lower = extension.to_ascii_lowercase();
SOURCE_EXTENSIONS.contains(&lower.as_str())
|| extra
.iter()
.any(|listed| listed.trim_start_matches('.').eq_ignore_ascii_case(&lower))
})
}
#[cfg(test)]
mod tests;
const RENDERING_SUMMARY_ENTRIES: usize = 16;
const RENDERING_SUMMARY_BYTES: usize = 40_000;