fn is_on(value: Option<std::ffi::OsString>) -> bool {
value.is_some_and(|value| !value.is_empty())
}
macro_rules! switch {
($($name:ident => $variable:literal, $documentation:literal;)*) => {
$(
#[doc = $documentation]
pub fn $name() -> bool {
static HELD: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*HELD.get_or_init(|| is_on(std::env::var_os($variable)))
}
)*
};
}
switch! {
cooking => "CPPLS_TRACE_COOKING", "What the cooker did to one unit: the directives it read and the facts it made.";
macro_ => "CPPLS_TRACE_MACRO", "One macro name, at every lookup, with everything the expander will see.";
guard => "CPPLS_TRACE_GUARD", "One condition, at every evaluation, on the walk's own path.";
lookup => "CPPLS_TRACE_LOOKUP", "One name, at every lookup, on the walk's environment.";
coordinates => "CPPLS_TRACE_COORDINATES", "Why a position resolved to the node it did.";
members => "CPPLS_TRACE_MEMBERS", "Why a member query answered what it answered.";
completion => "CPPLS_TRACE_COMPLETION", "What one completion request considered, region by region.";
attributes => "CPPLS_TRACE_ATTR", "Attribute questions, as the walk asks them.";
units => "CPPLS_TRACE_UNITS", "Which units the session walked, and how often each root.";
diagnostics => "CPPLS_TRACE_DIAGNOSTICS", "The stage table, printed when a run ends.";
timing => "CPPLS_TRACE_TIME", "Every type inference, and every file a question had to read to answer one.";
no_inference => "CPPLS_NO_INFERENCE", "Answer every type question `Unknown`, so that a rule's own walk can be measured apart from the inference.";
no_index_lookup => "CPPLS_NO_INDEX_LOOKUP", "Answer every declaration query `NotDeclaredHere`, so that a pull's cost can be split into the engine and the index — the sibling of `CPPLS_NO_INFERENCE`.";
inference => "CPPLS_TRACE_INFERENCE", "What a member answer was substituted with, and the type it became — the switch two editor reports needed.";
answers => "CPPLS_TRACE_ANSWERS", "What one name query found: how many declarations, of which kinds, in which files — the question a hover on a template parameter turned on (2288 `_Tp` answers, none of them a class).";
view => "CPPLS_TRACE_VIEW", "Which rendering a query was answered from, and whether the cache had one: the question that separated `not cooked` from `cooked and dropped`.";
types => "CPPLS_TRACE_TYPES", "One line per type question, at its depth — the only instrument that showed a recursion no depth limit bounded.";
visibility => "CPPLS_TRACE_VISIBILITY", "How many files a visibility question kept, after the ordering step.";
splice => "CPPLS_SPLICE_TRACE", "Every file the session entered and spliced, with what it contributed — the trace a missing standard-library fact needed.";
inlay => "CPPLS_TRACE_INLAY", "Where an inlay-hint request's milliseconds went, one line per call.";
time_checks => "CPPLS_TIME_CHECKS", "One line per rule per file — names the check that takes the forty-five seconds.";
walk_census => "CPPLS_WALK_CENSUS", "Every file the walk entered, spliced or fresh, and the conditions it mentions.";
code_action => "CPPLS_TRACE_CODE_ACTION", "Where a code-action request's milliseconds went.";
no_share => "CPPLS_NO_SHARE", "Turn summary sharing off, so one machine can be measured with and without it — an A/B switch, not a setting.";
}
pub fn no_guard_filter() -> bool {
static SEEDED: std::sync::OnceLock<()> = std::sync::OnceLock::new();
SEEDED.get_or_init(|| {
NO_GUARD_FILTER.store(
is_on(std::env::var_os("CPPLS_NO_GUARD_FILTER")),
std::sync::atomic::Ordering::Relaxed,
);
});
NO_GUARD_FILTER.load(std::sync::atomic::Ordering::Relaxed)
}
pub fn set_no_guard_filter(on: bool) {
let _ = no_guard_filter();
NO_GUARD_FILTER.store(on, std::sync::atomic::Ordering::Relaxed);
}
static NO_GUARD_FILTER: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
pub fn summaries() -> bool {
static HELD: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*HELD.get_or_init(|| is_on(std::env::var_os("CPPLS_TRACE_SUMMARIES")))
}
fn summary_reads() -> &'static std::sync::Mutex<SummaryReads> {
static HELD: std::sync::OnceLock<std::sync::Mutex<SummaryReads>> = std::sync::OnceLock::new();
HELD.get_or_init(|| std::sync::Mutex::new(SummaryReads::default()))
}
#[derive(Default)]
struct SummaryReads {
calls: usize,
distinct: std::collections::HashSet<String>,
}
pub fn a_summary_was_asked_for(path: &std::path::Path) {
if let Ok(mut held) = summary_reads().lock() {
held.calls += 1;
if held.distinct.len() < 100_000 {
held.distinct.insert(path.to_string_lossy().into_owned());
}
}
}
pub fn summary_read_counts() -> (usize, usize) {
summary_reads()
.lock()
.map(|held| (held.calls, held.distinct.len()))
.unwrap_or((0, 0))
}
pub fn completion_to_stderr() -> bool {
static HELD: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*HELD.get_or_init(|| is_on(std::env::var_os("CPPLS_TRACE_COMPLETION_STDERR")))
}
macro_rules! named_switch {
($($name:ident => $variable:literal, $documentation:literal;)*) => {
$(
#[doc = $documentation]
pub fn $name() -> Option<&'static str> {
static HELD: std::sync::OnceLock<Option<String>> = std::sync::OnceLock::new();
HELD.get_or_init(|| {
std::env::var_os($variable)
.filter(|value| !value.is_empty())
.map(|value| value.to_string_lossy().into_owned())
})
.as_deref()
}
)*
};
}
named_switch! {
macro_name => "CPPLS_TRACE_MACRO", "The one macro name to trace, at every lookup the expander makes.";
guard_name => "CPPLS_TRACE_GUARD", "The one name to trace through every condition evaluation.";
lookup_name => "CPPLS_TRACE_LOOKUP", "The one name to trace through the walk's macro environment.";
}
#[cfg(test)]
mod tests {
use super::is_on;
#[test]
fn an_unset_variable_is_off_and_so_is_an_empty_one() {
assert!(!is_on(None));
assert!(!is_on(Some(std::ffi::OsString::from(""))));
assert!(is_on(Some(std::ffi::OsString::from("1"))));
assert!(is_on(Some(std::ffi::OsString::from("0"))));
}
}