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, VfsFile};
use crate::file::view::{FileView, ParsedView, TokensOf};
use crate::include::config::{CompileCommands, CompilerConfig, project_config_from_flags};
use crate::include::toolchain::{self, 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};
pub(crate) fn the_seed_for(
config: &CompilerConfig,
macros: &crate::Marked,
file: &Path,
) -> MacroTable {
let mut seed = MacroTable::from_marked(macros);
if config.language_for(file).family() == Some(cpp_parser::features::Family::C) {
seed.undefine("__cplusplus", 0);
seed.undefine("_MSVC_LANG", 0);
}
seed
}
pub(crate) fn the_marked_seed_for(
config: &CompilerConfig,
macros: &crate::Marked,
root: &Path,
) -> crate::Marked {
let mut seed = macros.clone();
if config.language_for(root).family() == Some(cpp_parser::features::Family::C) {
seed.undefine("__cplusplus");
seed.undefine("_MSVC_LANG");
}
seed
}
#[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 SUMMARY_BUDGET_BYTES: usize = 1 << 30;
const MAX_COMPLETION_ITEMS: usize = 20_000;
const WALKS_WORTH_SHARING: usize = 4;
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 COOK_TWO_LEVELS_FURTHER: usize = 24;
const COOK_ONE_LEVEL_FURTHER: usize = 64;
const LAYOUT_MEMBER_BUDGET: 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>>>,
revision: Arc<std::sync::atomic::AtomicU64>,
}
impl OpenDocuments {
pub fn new() -> Self {
OpenDocuments::default()
}
pub fn open(&self, path: impl AsRef<Path>, text: impl Into<String>) {
let mut buffers = self
.buffers
.write()
.expect("the buffer map is not poisoned");
let key = normalize_path(path.as_ref(), cfg!(windows));
let text = text.into();
if buffers.get(&key) != Some(&text) {
buffers.insert(key, text);
self.revision
.fetch_add(1, std::sync::atomic::Ordering::Release);
}
}
pub fn close(&self, path: impl AsRef<Path>) -> bool {
let mut buffers = self
.buffers
.write()
.expect("the buffer map is not poisoned");
let removed = buffers
.remove(&normalize_path(path.as_ref(), cfg!(windows)))
.is_some();
if removed {
self.revision
.fetch_add(1, std::sync::atomic::Ordering::Release);
}
removed
}
fn revision(&self) -> u64 {
self.revision.load(std::sync::atomic::Ordering::Acquire)
}
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)]
pub(crate) enum Which {
FromMemory,
FromDisk,
Walked,
}
#[derive(Debug)]
pub(crate) struct PerFileConfig {
config: std::sync::Arc<CompilerConfig>,
language: cpp_parser::features::LanguageConfig,
cuda: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub(crate) struct ViewKey {
path: PathBuf,
rendering: Option<RenderingId>,
text: u64,
id: crate::file::paths::FileId,
cuda: bool,
language: Option<cpp_parser::features::LanguageConfig>,
open: bool,
environment: Option<EnvironmentId>,
}
fn a_rendering_key(path: &Path, text_hash: u64) -> (PathBuf, u64) {
(
PathBuf::from(crate::file::paths::normalize_path(path, cfg!(windows))),
text_hash,
)
}
fn buffer_view_key(
file: &VfsFile,
cuda: bool,
environment: Option<Arc<cpp_parser::MacroEnvironment>>,
) -> ViewKey {
ViewKey {
path: file.path.clone(),
rendering: None,
text: crate::cache::content_hash(&file.text),
id: file.id,
cuda,
language: None,
open: file.open,
environment: environment.map(EnvironmentId),
}
}
#[derive(Debug, Clone)]
struct RenderingId(std::sync::Arc<crate::preprocess::cooked::RenderedCooked>);
impl PartialEq for RenderingId {
fn eq(&self, other: &Self) -> bool {
std::sync::Arc::ptr_eq(&self.0, &other.0)
}
}
impl Eq for RenderingId {}
impl std::hash::Hash for RenderingId {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
std::sync::Arc::as_ptr(&self.0).hash(state);
}
}
#[derive(Debug, Clone)]
struct EnvironmentId(std::sync::Arc<cpp_parser::MacroEnvironment>);
impl PartialEq for EnvironmentId {
fn eq(&self, other: &Self) -> bool {
std::sync::Arc::ptr_eq(&self.0, &other.0)
}
}
impl Eq for EnvironmentId {}
impl std::hash::Hash for EnvironmentId {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
std::sync::Arc::as_ptr(&self.0).hash(state);
}
}
#[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 views: usize,
pub view_bytes: usize,
}
fn a_view_weighs(view: &FileView) -> usize {
view.source.len().saturating_mul(8) + view.tree.get_tokens().len().saturating_mul(16)
}
#[derive(Clone, Debug)]
pub struct SessionRevision {
session: Arc<()>,
changes: u64,
documents: u64,
}
pub struct Session<F: FileProvider = DiskFiles> {
root: PathBuf,
identity: Arc<()>,
changes: u64,
config: CompilerConfig,
toolchain: Option<Toolchain>,
discovery: ProjectDiscovery,
files: SessionFiles<F>,
documents: OpenDocuments,
vfs: Vfs<SessionFiles<F>>,
store: SummaryStore<SessionFiles<F>>,
filter: WatchFilter,
share_contributions: bool,
project: Vec<PathBuf>,
project_paths: HashMap<String, PathBuf>,
queue: Work,
parsed_since_the_last_pass: Vec<PathBuf>,
refined_on_request: std::sync::Mutex<std::collections::HashSet<PathBuf>>,
refinement_misses: std::sync::Mutex<std::collections::HashSet<(String, u64)>>,
refined_readings: std::sync::Mutex<std::collections::HashSet<String>>,
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>,
checks_memo: std::sync::Mutex<Option<(u64, Vec<crate::sema::check::Finding>)>>,
pull_hits: std::sync::atomic::AtomicU64,
pull_runs: std::sync::atomic::AtomicU64,
last_edit: std::time::Instant,
unit_index: std::sync::Mutex<Option<(u64, crate::IndexedUnit)>>,
rendered_unit: std::sync::Mutex<Option<(u64, crate::RenderedUnit)>>,
unit_roots: std::sync::Mutex<std::collections::HashMap<(PathBuf, u64), u64>>,
directive_signatures:
std::collections::HashMap<String, crate::preprocess::directive::DirectiveSignature>,
prefetched: HashMap<String, crate::index::store::Prepared>,
prefetched_order: std::collections::VecDeque<String>,
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>,
parsed_definitions: std::sync::Mutex<crate::ParsedDefinitions>,
config_for_file: std::sync::Mutex<HashMap<PathBuf, std::sync::Arc<PerFileConfig>>>,
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>,
>,
>,
views: std::sync::Mutex<
cache::cache_lru::Cache<ViewKey, std::sync::Arc<crate::file::view::ParsedView>>,
>,
rendering_views: std::sync::Mutex<
cache::cache_lru::Cache<ViewKey, std::sync::Arc<crate::file::view::ParsedView>>,
>,
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>>,
cooked_or_wanted: std::sync::Mutex<std::collections::HashSet<String>>,
renderings_asked: std::sync::Mutex<std::collections::HashSet<(std::path::PathBuf, u64)>>,
focused_document: std::sync::Mutex<Option<std::path::PathBuf>>,
stored_cooks: std::sync::atomic::AtomicUsize,
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>,
}
fn the_directive_defining(name: &str, text: &str, offset: usize) -> Option<String> {
let mut settled: Option<String> = None;
let mut at = 0usize;
for line in text.split_inclusive('\n') {
let start = at;
at += line.len();
if start > offset {
break;
}
let trimmed = line.trim_start();
let rest = if let Some(rest) = trimmed.strip_prefix("#define") {
rest.trim_start()
} else if let Some(rest) = trimmed.strip_prefix("#undef") {
rest.trim_start()
} else {
continue;
};
let end = rest
.find(|character: char| !(character.is_alphanumeric() || character == '_'))
.unwrap_or(rest.len());
if &rest[..end] != name {
continue;
}
if start > offset {
continue;
}
let mut directive = trimmed.trim_end().to_string();
while directive.ends_with('\\') {
let next_start = at;
let Some(next) = text[next_start..].split_inclusive('\n').next() else {
break;
};
at += next.len();
directive.pop();
directive.push_str(next.trim_end());
if next.trim_end().ends_with('\\') {
continue;
}
break;
}
settled = Some(directive.trim_end().to_string());
}
settled
}
fn the_standards_own_macro(
name: &str,
language: &cpp_parser::features::LanguageConfig,
) -> Option<String> {
use cpp_parser::features::{Family, Standard};
let family = language.family();
let c_standard = language
.standards()
.filter(|standard| standard.family() == Family::C)
.max();
let cpp_standard = language
.standards()
.filter(|standard| standard.family() == Family::Cpp)
.max();
match name {
"__STDC__" if family != Some(Family::Cpp) => Some("1".to_string()),
"__STDC_HOSTED__" if family != Some(Family::Cpp) => Some("1".to_string()),
"__STDC_VERSION__" if family != Some(Family::Cpp) => match c_standard? {
Standard::C89 => None,
Standard::C99 => Some("199901L".to_string()),
Standard::C11 => Some("201112L".to_string()),
Standard::C17 => Some("201710L".to_string()),
Standard::C23 => Some("202311L".to_string()),
_ => None,
},
"__cplusplus" if family != Some(Family::C) => match cpp_standard? {
Standard::Cpp11 => Some("201103L".to_string()),
Standard::Cpp14 => Some("201402L".to_string()),
Standard::Cpp17 => Some("201703L".to_string()),
Standard::Cpp20 => Some("202002L".to_string()),
Standard::Cpp23 => Some("202302L".to_string()),
_ => None,
},
_ => None,
}
}
fn a_macro_whose_value_depends_on_the_use(name: &str) -> bool {
matches!(
name,
"__LINE__" | "__FILE__" | "__DATE__" | "__TIME__" | "__COUNTER__"
)
}
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),
libraries: project_config.config.libraries.clone(),
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_cache = project_config
.config
.index
.cache_dir
.clone()
.map(std::path::PathBuf::from)
.unwrap_or_else(|| root.join(".cppls"))
.join("toolchain");
let commands = toolchain::CachedCommands::new(toolchain_cache);
let toolchain = toolchain::discover_with(
&files,
&commands,
&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 &project_config.config.unreal {
Some(unreal) => unreal.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,
revision: SessionRevision,
}
pub struct CookingMaterials {
pub path: PathBuf,
pub key: SummaryKey,
pub unit: Option<std::sync::Arc<crate::TranslationUnit>>,
pub configuration: Option<crate::preprocess::cooked::Configuration>,
pub text: String,
pub text_hash: u64,
pub seed: MacroTable,
pub already_declared: std::collections::HashSet<String>,
revision: SessionRevision,
}
pub struct DetachedCook<F: FileProvider> {
files: SessionFiles<F>,
config: CompilerConfig,
materials: CookingMaterials,
}
impl<F: FileProvider + Clone> DetachedCook<F> {
pub fn render(self) -> Option<RenderedCook> {
Session::<F>::render_cook(&self.files, &self.config, &self.materials)
}
}
#[derive(Default)]
pub struct PreparedWave {
pub prepared: Vec<(PathBuf, crate::index::store::Prepared)>,
pub units: Vec<std::sync::Arc<crate::TranslationUnit>>,
pub refinements: Vec<crate::FileSummary>,
revision: Option<SessionRevision>,
}
pub struct FileReadMaterials<F: FileProvider> {
path: PathBuf,
revision: SessionRevision,
input: crate::index::store::DetachedRead<F>,
view: Option<BufferViewMaterials>,
}
pub struct PreparedFile {
path: PathBuf,
revision: SessionRevision,
prepared: crate::index::store::Prepared,
view: Option<PreparedBufferView>,
}
#[derive(Clone)]
struct BufferViewMaterials {
file: VfsFile,
cuda: bool,
language: Option<cpp_parser::features::LanguageConfig>,
environment: Option<Arc<cpp_parser::MacroEnvironment>>,
}
struct PreparedBufferView {
key: ViewKey,
parsed: ParsedView,
weight: usize,
}
impl BufferViewMaterials {
fn parse(self) -> PreparedBufferView {
let key = buffer_view_key(&self.file, self.cuda, self.environment.clone());
let macros = self.environment.unwrap_or_default();
let view = FileView::parse_with_cuda(&self.file, macros.as_ref(), self.cuda, self.language);
let weight = a_view_weighs(&view);
PreparedBufferView {
key,
parsed: view.into_parsed(),
weight,
}
}
}
impl<F: FileProvider> FileReadMaterials<F> {
pub fn read(self) -> PreparedFile {
let (prepared, view) = match self.view {
Some(view)
if view.file.text.len() >= 16 * 1024
&& std::thread::available_parallelism().is_ok_and(|count| count.get() > 1) =>
{
std::thread::scope(|scope| {
let worker_view = view.clone();
let worker = std::thread::Builder::new()
.name("cpp-ls-buffer-view".to_string())
.stack_size(16 * 1024 * 1024)
.spawn_scoped(scope, move || worker_view.parse());
match worker {
Ok(worker) => {
let prepared = self.input.read();
let view = worker
.join()
.unwrap_or_else(|panic| std::panic::resume_unwind(panic));
(prepared, Some(view))
}
Err(error) => {
log::warn!("could not start a buffer-view worker: {error}");
(self.input.read(), Some(view.parse()))
}
}
})
}
Some(view) => (self.input.read(), Some(view.parse())),
None => (self.input.read(), None),
};
PreparedFile {
path: self.path,
revision: self.revision,
prepared,
view,
}
}
}
#[derive(Default)]
pub struct Drain {
files: Vec<PathBuf>,
units: Vec<std::sync::Arc<crate::TranslationUnit>>,
environments: Vec<PathBuf>,
revision: Option<SessionRevision>,
}
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>,
)>,
revision: Option<SessionRevision>,
retry_files: Vec<PathBuf>,
retry_environments: Vec<PathBuf>,
}
pub struct PreparedUnitReading {
root: PathBuf,
revision: SessionRevision,
program: u64,
indexed: crate::IndexedUnit,
walked: Vec<PathBuf>,
}
pub struct UnitReadMaterials<F: FileProvider> {
root: PathBuf,
revision: SessionRevision,
key: SummaryKey,
units_key: u64,
unit: Option<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 units_key(&self) -> u64 {
self.units_key
}
pub fn read_the_program(self) -> Option<PreparedUnitReading> {
if let Some(indexed) = self.known {
return Some(PreparedUnitReading {
root: self.root,
revision: self.revision,
program: self.program?,
indexed,
walked: self
.rendered
.as_ref()
.map(|r| r.walked.clone())
.unwrap_or_default(),
});
}
let stream = match self.rendered {
Some(rendered) => rendered,
None => {
let unit = self.unit.as_ref()?;
let definitions = unit.definitions();
let seed = the_seed_for(&self.config, &self.macros, &self.root);
let search = crate::preprocess::cooked::Search::new(&self.files, &self.config);
let cooking = std::time::Instant::now();
#[cfg(feature = "perf-probe")]
crate::perf::mark("before the cook");
let mut stream = unit.cook_the_unit(
&self.sources,
&definitions,
Some(&seed),
true,
Some(&search),
);
#[cfg(feature = "perf-probe")]
crate::perf::mark("cook");
if crate::probes::units() {
eprintln!(
"[unit] cook {:>6.1} ms ({} file(s), {} bytes)",
cooking.elapsed().as_secs_f64() * 1000.0,
stream.files.len(),
stream.text.len()
);
}
stream.walked = unit
.files()
.map(|path| path.to_path_buf())
.collect::<std::collections::BTreeSet<_>>()
.into_iter()
.collect();
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,
revision: self.revision,
program,
indexed: known,
walked: stream.walked.clone(),
});
}
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,
revision: self.revision,
program,
indexed,
walked: stream.walked.clone(),
})
}
}
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())
}
}
pub use cook::{AClaimedCook, AClaimedSlice};
mod calls;
pub mod completion;
mod constrained;
mod cook;
pub mod cuda;
pub mod definition;
mod diagnostics;
pub mod doc;
mod edits;
pub mod folding;
pub mod format;
mod indexing;
pub mod inlay;
mod lifecycle;
mod memory;
mod navigation;
mod pump;
mod queries;
pub mod semantic;
pub mod signature;
mod units;
mod views;
pub use queries::WrittenAttribute;
#[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 ModuleUnitTarget {
pub file: PathBuf,
pub name: String,
}
#[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>,
pub checks_withheld: bool,
}
#[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 = 8;
const MAX_UNIT_BYTES: usize = 64 * 1024 * 1024;
const MAX_WALKS_BY_CONTENT_ENTRIES: usize = 32;
const MAX_WALKS_BY_CONTENT_BYTES: usize = 16 * 1024 * 1024;
const CONFIG_FOR_FILE_ENTRIES: usize = 256;
const MAX_MODULES_READ_AT_ONCE: usize = 16;
#[derive(Default)]
pub(crate) 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)
}
fn timelines(&self) -> impl Iterator<Item = &std::sync::Arc<crate::TranslationUnit>> {
self.held.values().map(|held| &held.unit)
}
}
struct HeldUnit {
used: u64,
provisional: bool,
unit: std::sync::Arc<crate::TranslationUnit>,
weight: usize,
}
#[derive(Debug, Clone, Copy)]
pub(crate) 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 forget(&mut self, path: &Path) {
let key = queue_key(path);
if self.held.remove(&key) {
self.wanted.retain(|queued| queue_key(queued) != key);
}
}
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 library_files_to_read(
libraries: &[crate::project::LibrarySection],
workspace: &std::path::Path,
) -> Vec<PathBuf> {
const HEADER_EXTENSIONS: &[&str] = &["h", "hpp", "hxx", "h++", "inl", "ipp"];
const MAX_DEPTH: usize = 12;
const MAX_FILES: usize = 20_000;
let mut found: Vec<PathBuf> = Vec::new();
for library in libraries {
let mut roots: Vec<PathBuf> = library.include_directories(workspace);
roots.extend(library.module_directories(workspace));
let mut stack: Vec<(PathBuf, usize)> = roots.into_iter().map(|root| (root, 0)).collect();
while let Some((directory, depth)) = stack.pop() {
if depth > MAX_DEPTH || found.len() >= MAX_FILES {
continue;
}
let Ok(entries) = std::fs::read_dir(&directory) else {
continue;
};
for entry in entries.flatten() {
let path = entry.path();
let Ok(kind) = entry.file_type() else {
continue;
};
if kind.is_dir() {
stack.push((path, depth + 1));
continue;
}
let extension = path
.extension()
.and_then(|extension| extension.to_str())
.map(|extension| extension.to_lowercase());
let wanted = match extension.as_deref() {
Some("cppm") => true,
Some(other) => HEADER_EXTENSIONS.contains(&other),
None => false,
};
if wanted {
found.push(path);
}
}
}
}
found.sort();
found.dedup();
found
}
fn after_the_includes(text: &str) -> usize {
let mut end = 0usize;
let mut offset = 0usize;
for line in text.split_inclusive('\n') {
if line.trim_start().starts_with("#include") {
end = offset + line.len();
}
offset += line.len();
}
end
}
fn after_the_module_imports(text: &str) -> usize {
let mut end = 0usize;
let mut offset = 0usize;
for line in text.split_inclusive('\n') {
let trimmed = line.trim_start();
let imports = trimmed.starts_with("import ") && trimmed.trim_end().ends_with(';');
let declares_a_module = (trimmed.starts_with("module ")
|| trimmed.starts_with("export module "))
&& trimmed.trim_end().ends_with(';');
if imports || declares_a_module {
end = offset + line.len();
}
offset += line.len();
}
end
}
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);
}
if report
.config
.unreal
.as_ref()
.is_some_and(|unreal| unreal.is_on())
{
for pattern in [
"Intermediate/**",
"**/Intermediate/**",
"Binaries/**",
"Saved/**",
"DerivedDataCache/**",
] {
if let Ok(pattern) = crate::PathPattern::new(pattern) {
filter = filter.ignore_pattern(pattern);
}
}
}
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;