use cpp_code_analysis::{
CompilerConfig, FileProvider, ImportEdge, ImportOutcome, ImportTarget, MAX_IMPORT_DEPTH,
MemoryFiles, ModuleGraph, ModuleUnit, PathInterner, Session, scan_imports,
};
use cpp_parser::{CppParser, ParserConfig};
use std::path::Path;
fn scan(files: &MemoryFiles, root: &str) -> (ModuleGraph, PathInterner) {
scan_with(files, root, &CompilerConfig::new())
}
fn scan_with(
files: &MemoryFiles,
root: &str,
config: &CompilerConfig,
) -> (ModuleGraph, PathInterner) {
let source = files.read(Path::new(root)).expect("the root exists");
let tree = CppParser::parse(&source, ParserConfig::default());
let mut interner = PathInterner::new(files.is_case_insensitive());
let graph = scan_imports(&tree, Path::new(root), files, config, &mut interner);
(graph, interner)
}
fn analysed(graph: &ModuleGraph, interner: &PathInterner) -> Vec<String> {
graph
.analysed
.iter()
.filter_map(|file| interner.path(*file))
.map(|path| path.to_string_lossy().replace('\\', "/"))
.collect()
}
fn imports_of<'a>(
graph: &'a ModuleGraph,
interner: &PathInterner,
file: &str,
) -> Vec<&'a cpp_code_analysis::ImportEdge> {
let id = interner.get(Path::new(file)).unwrap_or_else(|| {
panic!(
"{file} is not in the graph; analysed: {:?}",
analysed(graph, interner)
)
});
graph.imports_of(id).collect()
}
fn outcome_of(graph: &ModuleGraph, file: &str, interner: &PathInterner) -> String {
let id = interner.get(Path::new(file)).unwrap_or_else(|| {
panic!(
"{file} is not in the graph; analysed: {:?}",
analysed(graph, interner)
)
});
let edge = graph
.imports_of(id)
.next()
.unwrap_or_else(|| panic!("{file} imports nothing; edges: {:?}", graph.edges));
match &edge.outcome {
ImportOutcome::Resolved(target) => format!(
"resolved to {}",
interner
.path(*target)
.map(|path| path.to_string_lossy().replace('\\', "/"))
.unwrap_or_default()
),
other => other.describe(),
}
}
#[test]
fn an_import_resolves_through_the_naming_convention() {
let files = MemoryFiles::new()
.with_file("main.cpp", "import m;\nint main() {}\n")
.with_file("m.cppm", "export module m;\nexport int f();\n");
let (graph, interner) = scan(&files, "main.cpp");
assert_eq!(
outcome_of(&graph, "main.cpp", &interner),
"resolved to m.cppm"
);
assert_eq!(analysed(&graph, &interner), vec!["main.cpp", "m.cppm"]);
}
#[test]
fn a_dotted_module_name_resolves_by_path() {
for layout in ["my/mod.cppm", "my.mod.cppm"] {
let files = MemoryFiles::new()
.with_file("main.cpp", "import my.mod;\n")
.with_file(layout, "export module my.mod;\n");
let (graph, interner) = scan(&files, "main.cpp");
assert_eq!(
outcome_of(&graph, "main.cpp", &interner),
format!("resolved to {layout}"),
"layout {layout:?}"
);
}
}
#[test]
fn imports_are_followed_transitively() {
let files = MemoryFiles::new()
.with_file("main.cpp", "import a;\n")
.with_file("a.cppm", "export module a;\nimport b;\nexport int f();\n")
.with_file("b.cppm", "export module b;\nexport int g();\n");
let (graph, interner) = scan(&files, "main.cpp");
assert_eq!(
analysed(&graph, &interner),
vec!["main.cpp", "a.cppm", "b.cppm"]
);
assert_eq!(graph.units.len(), 2, "two module units: {:?}", graph.units);
}
#[test]
fn units_carry_their_module_and_partition() {
let files = MemoryFiles::new()
.with_file("main.cpp", "import m;\n")
.with_file("m.cppm", "export module m;\nimport :part;\n")
.with_file("m-part.cppm", "export module m:part;\n");
let (graph, _) = scan(&files, "main.cpp");
let names: Vec<String> = graph
.units
.iter()
.map(|unit| format!("{} ({})", unit.qualified_name(), unit.unit.describe()))
.collect();
assert_eq!(
names,
vec![
"m (module interface unit)".to_string(),
"m:part (module partition interface unit)".to_string(),
]
);
}
#[test]
fn a_partition_resolves_under_either_file_naming() {
for layout in ["m-part.cppm", "m.part.cppm"] {
let files = MemoryFiles::new()
.with_file("m.cppm", "export module m;\nimport :part;\n")
.with_file(layout, "export module m:part;\n");
let (graph, interner) = scan(&files, "m.cppm");
assert_eq!(
outcome_of(&graph, "m.cppm", &interner),
format!("resolved to {layout}"),
"layout {layout:?}"
);
}
}
#[test]
fn a_partition_import_resolves_to_the_interface_not_the_implementation() {
let files = MemoryFiles::new()
.with_file("m.cppm", "export module m;\nimport :part;\n")
.with_file("m-part.cppm", "export module m:part;\nexport int f();\n")
.with_file("m-part-impl.cpp", "module m:part;\nint g();\n");
let (graph, interner) = scan(&files, "m.cppm");
assert_eq!(
outcome_of(&graph, "m.cppm", &interner),
"resolved to m-part.cppm",
"the interface unit"
);
assert!(
!analysed(&graph, &interner).contains(&"m-part-impl.cpp".to_string()),
"the implementation unit was not pulled in: {:?}",
analysed(&graph, &interner)
);
}
#[test]
fn importers_of_is_the_reverse_edge() {
let files = MemoryFiles::new()
.with_file("one.cpp", "import shared;\n")
.with_file("two.cpp", "import shared;\n")
.with_file("three.cpp", "int x;\n")
.with_file("shared.cppm", "export module shared;\nexport int f();\n");
let mut interner = PathInterner::new(false);
let mut graphs = Vec::new();
for unit in ["one.cpp", "two.cpp", "three.cpp"] {
let source = files.read(Path::new(unit)).expect("the unit");
let tree = CppParser::parse(&source, ParserConfig::default());
graphs.push(scan_imports(
&tree,
Path::new(unit),
&files,
&CompilerConfig::new(),
&mut interner,
));
}
let shared = interner.get(Path::new("shared.cppm")).expect("shared.cppm");
let affected: std::collections::HashSet<String> = graphs
.iter()
.flat_map(|graph| graph.importers_of(shared))
.filter_map(|file| interner.path(file))
.map(|path| path.to_string_lossy().replace('\\', "/"))
.collect();
assert_eq!(
affected,
["one.cpp", "two.cpp"]
.into_iter()
.map(String::from)
.collect(),
"the third file does not import it"
);
}
#[test]
fn dependents_of_follows_the_chain() {
let files = MemoryFiles::new()
.with_file("main.cpp", "import a;\n")
.with_file("a.cppm", "export module a;\nimport b;\n")
.with_file("b.cppm", "export module b;\nimport c;\n")
.with_file("c.cppm", "export module c;\n");
let (graph, interner) = scan(&files, "main.cpp");
let c = interner.get(Path::new("c.cppm")).expect("c.cppm");
let mut affected: Vec<String> = graph
.dependents_of(c)
.iter()
.filter_map(|file| interner.path(*file))
.map(|path| path.to_string_lossy().replace('\\', "/"))
.collect();
affected.sort();
assert_eq!(
affected,
vec!["a.cppm", "b.cppm", "main.cpp"],
"everything above c, transitively"
);
}
#[test]
fn a_root_has_no_importers() {
let files = MemoryFiles::new()
.with_file("main.cpp", "import m;\n")
.with_file("m.cppm", "export module m;\n");
let (graph, interner) = scan(&files, "main.cpp");
let main = interner.get(Path::new("main.cpp")).expect("main.cpp");
assert_eq!(
graph.imports_of(main).count(),
1,
"the root is where the dependency starts"
);
assert!(graph.importers_of(main).is_empty(), "nothing imports it");
assert!(graph.dependents_of(main).is_empty());
}
#[test]
fn a_file_with_no_imports_has_no_needs() {
let files = MemoryFiles::new().with_file("main.cpp", "#include <vector>\nint main() {}\n");
let (graph, interner) = scan(&files, "main.cpp");
let main = interner.get(Path::new("main.cpp")).expect("main.cpp");
assert!(graph.imports_of(main).next().is_none());
assert!(graph.needs_of(main).is_empty());
}
#[test]
fn an_unknown_module_is_unknown_rather_than_absent() {
let files = MemoryFiles::new().with_file("main.cpp", "import std;\n");
let (graph, interner) = scan(&files, "main.cpp");
assert_eq!(
imports_of(&graph, &interner, "main.cpp")[0].outcome,
ImportOutcome::UnknownModule { name: "std".into() }
);
let unresolved = graph.unresolved_imports();
assert_eq!(unresolved.len(), 1);
assert!(
!unresolved[0].outcome.may_provide_names(),
"the names are unknown, so nothing may be concluded about them"
);
assert!(
unresolved[0]
.outcome
.describe()
.contains("no file in this project"),
"and the message says what was actually established: {}",
unresolved[0].outcome.describe()
);
}
#[test]
fn a_file_that_declares_something_else_is_a_mismatch() {
let files = MemoryFiles::new()
.with_file("main.cpp", "import m;\n")
.with_file("m.cppm", "export module other;\n");
let (graph, interner) = scan(&files, "main.cpp");
let edge = imports_of(&graph, &interner, "main.cpp")[0];
let edge = edge.clone();
assert_eq!(
edge.outcome,
ImportOutcome::NotDeclaredHere {
name: "m".into(),
tried: std::path::PathBuf::from("m.cppm"),
}
);
assert!(!edge.outcome.may_provide_names());
assert!(
edge.outcome.describe().contains("does not declare"),
"{}",
edge.outcome.describe()
);
}
#[test]
fn a_missing_partition_is_reported_against_its_module() {
let files = MemoryFiles::new().with_file("m.cppm", "export module m;\nimport :missing;\n");
let (graph, interner) = scan(&files, "m.cppm");
let edge = imports_of(&graph, &interner, "m.cppm")[0].clone();
assert_eq!(
edge.outcome,
ImportOutcome::UnknownPartition {
module: "m".into(),
partition: "missing".into(),
}
);
assert!(
edge.outcome.describe().contains("declares no partition"),
"{}",
edge.outcome.describe()
);
}
#[test]
fn a_partition_import_without_a_module_is_reported() {
let files = MemoryFiles::new().with_file("main.cpp", "import :part;\n");
let (graph, interner) = scan(&files, "main.cpp");
let edge = imports_of(&graph, &interner, "main.cpp")[0].clone();
assert_eq!(edge.outcome, ImportOutcome::NoModuleToPartition);
assert!(!edge.outcome.may_provide_names());
}
#[test]
fn a_header_unit_uses_the_include_search_order() {
let files = MemoryFiles::new()
.with_file("main.cpp", "import \"local.h\";\nimport <vector>;\n")
.with_file("local.h", "int x;\n");
let (graph, interner) = scan(&files, "main.cpp");
let edges = imports_of(&graph, &interner, "main.cpp");
assert_eq!(
edges[0].outcome,
ImportOutcome::Resolved(interner.get(Path::new("local.h")).unwrap()),
"the quoted form found the project header"
);
match &edges[1].outcome {
ImportOutcome::UnknownHeaderUnit { name, searched } => {
assert_eq!(&**name, "vector");
assert!(
searched.is_empty(),
"an angle import with no include paths has nowhere to look: {searched:?}"
);
assert!(
edges[1]
.outcome
.describe()
.contains("no include directories are configured"),
"and the message says that rather than blaming the header: {}",
edges[1].outcome.describe()
);
}
other => panic!("expected an unresolvable header unit, got {other:?}"),
}
}
#[test]
fn a_partition_file_declaring_another_partition_is_a_mismatch() {
let files = MemoryFiles::new()
.with_file("m.cppm", "export module m;\nimport :part;\n")
.with_file("m-part.cppm", "export module m:other;\n");
let (graph, interner) = scan(&files, "m.cppm");
let edge = imports_of(&graph, &interner, "m.cppm")[0].clone();
assert_eq!(
edge.outcome,
ImportOutcome::PartitionMismatch {
module: "m".into(),
partition: "part".into(),
tried: std::path::PathBuf::from("m-part.cppm"),
}
);
assert!(!edge.outcome.may_provide_names());
assert!(
edge.outcome.describe().contains("is not partition"),
"{}",
edge.outcome.describe()
);
}
#[test]
fn a_partition_file_without_a_partition_is_a_mismatch() {
let files = MemoryFiles::new()
.with_file("m.cppm", "export module m;\nimport :part;\n")
.with_file("m-part.cppm", "export module m;\n");
let (graph, interner) = scan(&files, "m.cppm");
let edge = imports_of(&graph, &interner, "m.cppm")[0].clone();
assert!(
matches!(edge.outcome, ImportOutcome::PartitionMismatch { .. }),
"{:?}",
edge.outcome
);
}
#[test]
fn a_header_unit_resolves_along_an_include_path() {
let files = MemoryFiles::new()
.with_file("src/main.cpp", "#include \"local.h\"\nimport <vector>;\n")
.with_file("include/vector", "int v;\n");
let config = CompilerConfig::new().with_include_path("include");
let (graph, interner) = scan_with(&files, "src/main.cpp", &config);
let edges = imports_of(&graph, &interner, "src/main.cpp");
assert_eq!(
edges[0].outcome,
ImportOutcome::Resolved(interner.get(Path::new("include/vector")).unwrap()),
"the angle form searched the configured path"
);
}
#[test]
fn a_module_available_only_as_a_bmi_is_unknown() {
let files = MemoryFiles::new()
.with_file(
"main.cpp",
"import std.compat;\nimport <vector>;\nimport third_party.lib;\nint main() {}\n",
)
.with_file("include/vector", "int v;\n");
let config = CompilerConfig::new().with_include_path("include");
let (graph, interner) = scan_with(&files, "main.cpp", &config);
let edges = imports_of(&graph, &interner, "main.cpp");
assert_eq!(
edges[0].outcome,
ImportOutcome::UnknownModule {
name: "std.compat".into()
}
);
assert!(
edges[1].outcome.is_resolved(),
"the header unit resolved: {:?}",
edges[1].outcome
);
assert_eq!(
edges[2].outcome,
ImportOutcome::UnknownModule {
name: "third_party.lib".into()
}
);
for edge in graph.unresolved_imports() {
assert!(
!edge.outcome.may_provide_names(),
"nothing may be concluded about names from an unresolved import"
);
assert!(
!edge.outcome.describe().is_empty(),
"and the outcome can explain itself"
);
}
}
#[test]
fn an_unresolved_import_is_not_a_dependency() {
let files = MemoryFiles::new()
.with_file("main.cpp", "import present;\nimport absent;\n")
.with_file("present.cppm", "export module present;\n");
let (graph, interner) = scan(&files, "main.cpp");
let main = interner.get(Path::new("main.cpp")).expect("main.cpp");
let needs: Vec<String> = graph
.needs_of(main)
.iter()
.filter_map(|file| interner.path(*file))
.map(|path| path.to_string_lossy().replace('\\', "/"))
.collect();
assert_eq!(
needs,
vec!["present.cppm"],
"only what resolved is a dependency"
);
assert_eq!(graph.imports_of(main).count(), 2);
assert_eq!(graph.unresolved_imports().len(), 1);
}
#[test]
fn a_header_is_not_a_module() {
let files = MemoryFiles::new()
.with_file("main.cpp", "import util;\n")
.with_file("util.h", "int helper();\n");
let (graph, interner) = scan(&files, "main.cpp");
let edge = imports_of(&graph, &interner, "main.cpp")[0].clone();
assert!(
!edge.outcome.is_resolved(),
"a header is not a module unit: {:?}",
edge.outcome
);
assert!(!analysed(&graph, &interner).contains(&"util.h".to_string()));
}
#[test]
fn an_import_cycle_terminates_and_is_recorded() {
let files = MemoryFiles::new()
.with_file("a.cppm", "export module a;\nimport b;\n")
.with_file("b.cppm", "export module b;\nimport a;\n");
let (graph, interner) = scan(&files, "a.cppm");
assert_eq!(analysed(&graph, &interner), vec!["a.cppm", "b.cppm"]);
assert_eq!(graph.edges.len(), 2, "both edges: {:?}", graph.edges);
assert!(
graph.edges.iter().all(|edge| edge.outcome.is_resolved()),
"both resolved — it is the layout that is cyclic, not the resolution"
);
}
#[test]
fn a_self_import_terminates() {
let files = MemoryFiles::new().with_file("a.cppm", "export module a;\nimport a;\n");
let (graph, _) = scan(&files, "a.cppm");
assert_eq!(graph.analysed.len(), 1);
assert_eq!(graph.edges.len(), 1);
}
#[test]
fn a_diamond_reads_the_shared_module_once() {
let files = MemoryFiles::new()
.with_file("main.cpp", "import a;\nimport b;\n")
.with_file("a.cppm", "export module a;\nimport shared;\n")
.with_file("b.cppm", "export module b;\nimport shared;\n")
.with_file("shared.cppm", "export module shared;\n");
let (graph, interner) = scan(&files, "main.cpp");
let shared_count = analysed(&graph, &interner)
.iter()
.filter(|path| *path == "shared.cppm")
.count();
assert_eq!(
shared_count,
1,
"read once: {:?}",
analysed(&graph, &interner)
);
assert_eq!(graph.units.len(), 3, "one unit each: {:?}", graph.units);
}
#[test]
fn a_chain_deeper_than_the_limit_stops() {
let mut files = MemoryFiles::new().with_file("m0.cppm", "export module m0;\nimport m1;\n");
for depth in 1..MAX_IMPORT_DEPTH + 5 {
files.insert(
format!("m{depth}.cppm"),
format!("export module m{depth};\nimport m{};\n", depth + 1),
);
}
let (graph, _) = scan(&files, "m0.cppm");
assert!(
graph.analysed.len() <= MAX_IMPORT_DEPTH + 2,
"the scan stopped: {} files",
graph.analysed.len()
);
}
#[test]
fn the_scan_never_panics() {
let cases: Vec<MemoryFiles> = vec![
MemoryFiles::new(),
MemoryFiles::new().with_file("a.cppm", ""),
MemoryFiles::new().with_file("a.cppm", "export module a;\nimport a;\n"),
MemoryFiles::new()
.with_file("a.cppm", "export module a;\nimport b;\n")
.with_file("b.cppm", "export module b;\nimport c;\n")
.with_file("c.cppm", "export module c;\nimport a;\n"),
MemoryFiles::new().with_file("a.cppm", "export module a;\nimport ;\n"),
MemoryFiles::new().with_file("a.cppm", "export module ;\n"),
MemoryFiles::new().with_file("a.cppm", "import;\nimport\n"),
MemoryFiles::new().with_file("a.cppm", "export module a;\nimport :;\n"),
MemoryFiles::new().with_file("a.cppm", "module : private;\n"),
MemoryFiles::new().with_file("a.cppm", "export module a;\nimport m.b.c.d.e.f;\n"),
MemoryFiles::new().with_file("a.cppm", "export module a;\nimport <;\n"),
MemoryFiles::new().with_file("a.cppm", "export module a;\nimport \"unterminated\n"),
MemoryFiles::new().with_file("a.cppm", "export module a;\nimport \u{1F600};\n"),
];
for files in cases {
let mut interner = PathInterner::new(false);
let source = files.read(Path::new("a.cppm")).unwrap_or_default();
let tree = CppParser::parse(&source, ParserConfig::default());
let graph = scan_imports(
&tree,
Path::new("a.cppm"),
&files,
&CompilerConfig::new(),
&mut interner,
);
for file in graph.analysed.clone() {
let _ = graph.importers_of(file);
let _ = graph.dependents_of(file);
let _ = graph.needs_of(file);
let _ = graph.imports_of(file);
}
let _ = graph.unresolved_imports();
let _ = graph.interface_unit("a");
let _ = graph.partition_unit("a", "b");
let _ = graph.units_of("a");
}
}
#[test]
fn a_plain_translation_unit_has_no_unit() {
let files = MemoryFiles::new().with_file("main.cpp", "#include <vector>\nint main() {}\n");
let (graph, interner) = scan(&files, "main.cpp");
let main = interner.get(Path::new("main.cpp")).expect("main.cpp");
assert!(graph.units.is_empty(), "{:?}", graph.units);
assert!(graph.unit_of(main).is_none());
assert!(graph.edges.is_empty());
}
#[test]
fn an_import_edge_has_no_macro_environment() {
let files = MemoryFiles::new()
.with_file("main.cpp", "#define FROM_MAIN 1\nimport body;\n")
.with_file("body.cppm", "export module body;\n");
let (modules, interner) = scan(&files, "main.cpp");
let edge = modules
.imports_of(interner.get(Path::new("main.cpp")).expect("main.cpp"))
.next()
.expect("the import");
assert!(edge.outcome.is_resolved(), "{:?}", edge.outcome);
let ImportEdge {
from: _,
from_module: _,
target: _,
is_reexport: _,
outcome: _,
range: _,
} = edge;
assert!(
analysed(&modules, &interner).contains(&"body.cppm".to_string()),
"a resolved import is followed: {:?}",
analysed(&modules, &interner)
);
}
#[test]
fn a_re_export_is_recorded() {
let files = MemoryFiles::new()
.with_file(
"m.cppm",
"export module m;\nexport import other;\nimport plain;\n",
)
.with_file("other.cppm", "export module other;\n")
.with_file("plain.cppm", "export module plain;\n");
let (graph, interner) = scan(&files, "m.cppm");
let edges = imports_of(&graph, &interner, "m.cppm");
assert_eq!(edges.len(), 2);
assert!(edges[0].is_reexport, "`export import other;`");
assert!(!edges[1].is_reexport, "plain `import plain;`");
assert_eq!(
edges[0].target,
ImportTarget::Module("other".into()),
"and the target is still recorded"
);
}
#[test]
fn the_scan_is_deterministic() {
let files = MemoryFiles::new()
.with_file("main.cpp", "import a;\nimport b;\nimport missing;\n")
.with_file("a.cppm", "export module a;\nimport shared;\n")
.with_file("b.cppm", "export module b;\nimport shared;\n")
.with_file("shared.cppm", "export module shared;\n");
let (first, interner) = scan(&files, "main.cpp");
let (second, _) = scan(&files, "main.cpp");
assert_eq!(
analysed(&first, &interner),
analysed(&second, &interner),
"the same files in the same order"
);
assert_eq!(
first.edges.len(),
second.edges.len(),
"and the same number of edges"
);
assert_eq!(
first
.units
.iter()
.map(|u| u.qualified_name())
.collect::<Vec<_>>(),
second
.units
.iter()
.map(|u| u.qualified_name())
.collect::<Vec<_>>()
);
}
#[test]
fn interface_units_are_found_by_module_name() {
let files = MemoryFiles::new()
.with_file("main.cpp", "import m;\nimport m.part;\n")
.with_file("m.cppm", "export module m;\nimport :part;\n")
.with_file("m-impl.cpp", "module m;\nint x;\n")
.with_file("m-part.cppm", "export module m:part;\n");
let (graph, interner) = scan(&files, "main.cpp");
let interface = graph.interface_unit("m").expect("an interface unit");
assert_eq!(interface.unit, ModuleUnit::InterfaceUnit);
assert_eq!(
interface.path.to_string_lossy().replace('\\', "/"),
"m.cppm",
"the implementation unit is not the one importers see"
);
let all: Vec<String> = graph
.units_of("m")
.iter()
.map(|unit| unit.qualified_name())
.collect();
assert!(
all.contains(&"m:part".to_string()),
"the partition belongs to `m`: {all:?}"
);
assert!(
graph
.edges
.iter()
.any(|edge| edge.target == ImportTarget::Module("m.part".into())
&& !edge.outcome.is_resolved()),
"a dotted name is not a partition: {:?}",
graph.edges
);
let _ = interner;
}
#[test]
fn a_module_beside_the_importing_file_is_found_from_that_file() {
let files = MemoryFiles::new()
.with_file("src/main.cpp", "import mathlib;\n")
.with_file(
"src/mathlib.ixx",
"export module mathlib;\nexport int add(int, int);\n",
);
let (graph, _interner) = scan(&files, "src/main.cpp");
let interface = graph
.interface_unit("mathlib")
.expect("`mathlib.ixx` is beside the file that imports it");
assert_eq!(
interface.path.to_string_lossy().replace('\\', "/"),
"src/mathlib.ixx",
"and it was found *there* rather than relative to the process"
);
}
#[test]
fn a_header_unit_makes_the_headers_declarations_visible() {
use cpp_code_analysis::{DiskFiles, Known, OpenDocuments, Session, SessionFiles, WatchFilter};
let memory = MemoryFiles::new()
.with_file(
"local_math.h",
"#pragma once\ninline int twice(int value) { return value * 2; }\n",
)
.with_file(
"main.cpp",
"import \"local_math.h\";\nint f() { return twice(21); }\n",
);
let providers = SessionFiles::new(OpenDocuments::new(), memory);
let mut session = Session::with_config(
".",
providers,
WatchFilter::new("."),
CompilerConfig::default(),
);
session.add_project_files([
std::path::PathBuf::from("local_math.h"),
std::path::PathBuf::from("main.cpp"),
]);
session.index_everything();
let found = session.index().definition("twice", Path::new("main.cpp"));
assert!(
matches!(found, Known::Yes(_)),
"`main.cpp` imports the header as a unit, so `twice` is in view: {found:?}"
);
let _ = DiskFiles;
}
#[test]
fn only_exported_names_are_visible_through_an_import() {
use cpp_code_analysis::{DiskFiles, Known, OpenDocuments, Session, SessionFiles, WatchFilter};
let memory = MemoryFiles::new()
.with_file(
"mathlib.ixx",
"export module mathlib;\n\
export namespace mathlib { int add(int, int); }\n\
namespace mathlib { int hidden_helper(int); }\n",
)
.with_file(
"main.cpp",
"import mathlib;\nint f() { return mathlib::add(1, 2); }\n",
);
let providers = SessionFiles::new(OpenDocuments::new(), memory);
let mut session = Session::with_config(
".",
providers,
WatchFilter::new("."),
CompilerConfig::default(),
);
session.add_project_files([
std::path::PathBuf::from("mathlib.ixx"),
std::path::PathBuf::from("main.cpp"),
]);
session.index_everything();
let exported = session
.index()
.definition("mathlib::add", Path::new("main.cpp"));
assert!(
matches!(exported, Known::Yes(_)),
"`add` is inside an exported namespace: {exported:?}"
);
let hidden = session
.index()
.definition("mathlib::hidden_helper", Path::new("main.cpp"));
assert!(
!matches!(hidden, Known::Yes(_)),
"`hidden_helper` is written outside every `export`, so an importer may not name it: {hidden:?}"
);
let _ = DiskFiles;
}
#[test]
fn a_module_whose_interface_unit_is_outside_the_project_is_read_in() {
use cpp_code_analysis::{DiskFiles, Known, OpenDocuments, Session, SessionFiles, WatchFilter};
let files = MemoryFiles::new()
.with_file(
"lib/modules/std.ixx",
"export module std;\n#include <string>\n",
)
.with_file("lib/include/string", "namespace std { class string {}; }\n")
.with_file("src/main.cpp", "import std;\nstd::string greeting;\n");
let config = CompilerConfig::new().with_include_path("lib/include");
let providers = SessionFiles::new(OpenDocuments::new(), files);
let mut session = Session::with_config(".", providers, WatchFilter::new("."), config);
session.add_project_files([std::path::PathBuf::from("src/main.cpp")]);
session.index_everything();
let before = session
.index()
.definition("std::string", Path::new("src/main.cpp"));
assert!(
!matches!(before, Known::Yes(_)),
"no summary of `lib/modules/std.ixx` exists yet, so nothing can answer for `std`: {before:?}"
);
let read_in = session.read_the_modules_a_file_imports(Path::new("src/main.cpp"));
assert_eq!(
read_in, 1,
"one interface unit: `std.ixx`, and nothing else"
);
assert_eq!(
session.index().interface_unit_of("std"),
Some("lib/modules/std.ixx"),
"and the index now knows which file an `import std;` reaches"
);
let after = session
.index()
.definition("std::string", Path::new("src/main.cpp"));
assert!(
matches!(after, Known::Yes(_)),
"`std::string` is declared in the header the interface unit includes: {after:?}"
);
assert_eq!(
session.read_the_modules_a_file_imports(Path::new("src/main.cpp")),
0,
"an interface unit already in the index is not read again"
);
let _ = DiskFiles;
}
#[test]
fn a_module_name_is_offered_after_import() {
use cpp_code_analysis::file::coordinates::FileOffset;
use cpp_code_analysis::{OpenDocuments, SessionFiles, WatchFilter};
let text = "import \nimport sh\nint x = 0;\n";
let files = MemoryFiles::new()
.with_file("src/main.cpp", text)
.with_file("src/math.cppm", "export module math;\n")
.with_file("src/shapes.cppm", "export module shapes;\n")
.with_file("src/math.cpp", "module math;\n");
let providers = SessionFiles::new(OpenDocuments::new(), files);
let mut session =
Session::with_config(".", providers, WatchFilter::new("."), CompilerConfig::new());
session.add_project_files([
std::path::PathBuf::from("src/main.cpp"),
std::path::PathBuf::from("src/math.cppm"),
std::path::PathBuf::from("src/shapes.cppm"),
std::path::PathBuf::from("src/math.cpp"),
]);
session.index_everything();
let view = session
.view_of_the_file("src/main.cpp")
.expect("the file is held");
let cursor = text.find("import ").expect("the fixture writes it") + "import ".len();
let offered = session.completions(&view, FileOffset::new(cursor));
let labels: Vec<&str> = offered
.items
.iter()
.map(|item| item.label.as_str())
.collect();
assert!(
labels.contains(&"math") && labels.contains(&"shapes"),
"the two modules this project declares: {labels:?}"
);
assert!(
!labels.contains(&"main"),
"`main.cpp` is a source, not a module unit: {labels:?}"
);
assert_eq!(
labels.iter().filter(|label| **label == "math").count(),
1,
"one row per name, however many files mention it: {labels:?}"
);
let partial = text.find("import sh").expect("…") + "import sh".len();
let narrowed = session.completions(&view, FileOffset::new(partial));
let labels: Vec<&str> = narrowed
.items
.iter()
.map(|item| item.label.as_str())
.collect();
assert_eq!(labels, ["shapes"], "the prefix filters the list");
assert_eq!(
narrowed.replace,
cpp_parser::SourceRange::new(partial - 2, 2),
"and the client replaces the typed word"
);
}
#[test]
fn the_standard_module_is_found_in_gcc_include_tree() {
use cpp_code_analysis::{OpenDocuments, SessionFiles, WatchFilter};
let std_cc = "module;\n\
#include <bits/stdc++.h>\n\
export module std;\n\
export namespace std {\n\
using size_t = unsigned long long;\n\
template <class T> class vector {};\n\
}\n";
let files = MemoryFiles::new()
.with_file("/inc/bits/std.cc", std_cc)
.with_file("/inc/bits/stdc++.h", "namespace std { }\n")
.with_file(
"/p/std_demo.cpp",
"import std;\nint main() { std::vector<int> words; return 0; }\n",
);
let mut config = CompilerConfig::new();
config.include_paths = vec![cpp_code_analysis::IncludePath::system("/inc")];
let providers = SessionFiles::new(OpenDocuments::new(), files);
let mut session = Session::with_config("/p", providers, WatchFilter::new("/p"), config);
session.add_project_files([std::path::PathBuf::from("/p/std_demo.cpp")]);
session.index_everything();
assert!(
session.imports_a_module_that_is_not_read(std::path::Path::new("/p/std_demo.cpp")),
"the module is not in the index yet — the state a request finds when it arrives first"
);
assert_eq!(
session.read_the_modules_a_file_imports(std::path::Path::new("/p/std_demo.cpp")),
1,
"the request path resolves `std` and reads it in"
);
assert_eq!(
session.index().interface_unit_of("std"),
Some("/inc/bits/std.cc"),
"`std` resolves to libstdc++'s own interface unit"
);
let found = session
.index()
.definitions("std::vector", std::path::Path::new("/p/std_demo.cpp"));
assert!(
matches!(found, cpp_code_analysis::Known::Yes(_)),
"so a name the module exports is in the index for the importer: {found:?}"
);
}
#[test]
fn an_exported_namespace_and_its_concept_are_exported_facts() {
use cpp_code_analysis::file::coordinates::FileOffset;
use cpp_code_analysis::{OpenDocuments, SessionFiles, WatchFilter};
let unit = "export module math;\n\
export namespace math {\n\
template <typename T> concept Number = true;\n\
int add(int lhs, int rhs);\n\
}\n";
let caller = "import math;\nint value = mat;\n";
let files = MemoryFiles::new()
.with_file("src/math.cppm", unit)
.with_file("src/main.cpp", caller);
let providers = SessionFiles::new(OpenDocuments::new(), files);
let mut session =
Session::with_config(".", providers, WatchFilter::new("."), CompilerConfig::new());
session.add_project_files([
std::path::PathBuf::from("src/math.cppm"),
std::path::PathBuf::from("src/main.cpp"),
]);
session.index_everything();
let summary = session
.index()
.summary(std::path::Path::new("src/math.cppm"))
.expect("the unit is indexed");
let exported = |name: &str| {
summary
.declarations
.iter()
.find(|fact| fact.name == name)
.map(|fact| fact.exported)
};
assert_eq!(exported("math"), Some(true), "the namespace's own name");
assert_eq!(exported("Number"), Some(true), "and a concept inside it");
assert_eq!(
exported("add"),
Some(true),
"the function, which always was"
);
let view = session
.view_of_the_file("src/main.cpp")
.expect("the caller is held");
let cursor = caller.find("mat").expect("the fixture writes it") + "mat".len();
let offered = session.completions(&view, FileOffset::new(cursor));
let labels: Vec<&str> = offered
.items
.iter()
.map(|item| item.label.as_str())
.collect();
assert!(
labels.contains(&"math"),
"`math` is a name an importer can write: {labels:?}"
);
}
#[test]
fn a_module_beside_the_importing_file_is_read_in() {
use cpp_code_analysis::{DiskFiles, Known, OpenDocuments, Session, SessionFiles, WatchFilter};
let files = MemoryFiles::new()
.with_file(
"src/math.cppm",
"export module math;\nexport int add(int lhs, int rhs);\n",
)
.with_file(
"src/impl.cpp",
"module math;\nint add(int lhs, int rhs) { return lhs + rhs; }\n",
)
.with_file("src/main.cpp", "import math;\nint x = add(1, 2);\n");
let providers = SessionFiles::new(OpenDocuments::new(), files);
let mut session =
Session::with_config(".", providers, WatchFilter::new("."), CompilerConfig::new());
session.add_project_files([std::path::PathBuf::from("src/main.cpp")]);
session.index_everything();
assert!(
session.imports_a_module_that_is_not_read(Path::new("src/main.cpp")),
"the module is not in the index yet — the state a request finds when it arrives first"
);
assert_eq!(
session.read_the_modules_a_file_imports(Path::new("src/main.cpp")),
1,
"the interface unit beside the importing file is read in"
);
assert_eq!(
session.index().interface_unit_of("math"),
Some("src/math.cppm"),
"and the index now knows where `import math;` goes"
);
assert!(
!session.imports_a_module_that_is_not_read(Path::new("src/main.cpp")),
"so the next request does not take the write lock to ask again"
);
let found = session
.index()
.definitions("add", Path::new("src/main.cpp"));
assert!(
matches!(found, Known::Yes(_)),
"and the name the module exports is now in the index: {found:?}"
);
let _ = (DiskFiles, OpenDocuments::new());
}
#[test]
fn a_names_imported_from_the_standard_module_are_offered_after_the_scope() {
use cpp_code_analysis::{DiskFiles, OpenDocuments, Session, SessionFiles, WatchFilter};
let main = "import std;\n\nstd::string greeting{\"hi\"};\n";
let files = MemoryFiles::new()
.with_file(
"lib/modules/std.ixx",
"export module std;\n#include <string>\n#include <vector>\n",
)
.with_file("lib/include/string", "namespace std { class string {}; }\n")
.with_file(
"lib/include/vector",
"namespace std { template <class T> class vector {}; }\n",
)
.with_file("src/main.cpp", main);
let config = CompilerConfig::new().with_include_path("lib/include");
let providers = SessionFiles::new(OpenDocuments::new(), files);
let mut session = Session::with_config(".", providers, WatchFilter::new("."), config);
session.add_project_files([std::path::PathBuf::from("src/main.cpp")]);
session.want_cooked_reading(std::path::Path::new("src/main.cpp"));
session.index_everything();
assert_eq!(
session.read_the_modules_a_file_imports(Path::new("src/main.cpp")),
1,
"the standard module's interface unit is read in"
);
let view = session.view("src/main.cpp").expect("the file is held");
let after_the_scope = main.find("std::").expect("the fixture writes `std::`") + "std::".len();
let found = session.completions(
&view,
cpp_code_analysis::file::coordinates::FileOffset::new(after_the_scope),
);
let labels: Vec<&str> = found.items.iter().map(|item| item.label.as_str()).collect();
assert_eq!(
found.scope, "std",
"the answer is the names of that scope, and saying which scope it listed is part of the answer"
);
assert!(
labels.contains(&"string") && labels.contains(&"vector"),
"both headers `std.ixx` includes contribute their names: {labels:?}"
);
let _ = DiskFiles;
}
#[test]
fn an_import_nothing_declares_gets_a_note_on_the_line_that_wrote_it() {
use cpp_code_analysis::{DiskFiles, OpenDocuments, Session, SessionFiles, WatchFilter};
let source = "// a file whose module is not in this project\nimport mylib;\n\nint main() { return 0; }\n";
let files = MemoryFiles::new().with_file("src/main.cpp", source);
let providers = SessionFiles::new(OpenDocuments::new(), files);
let mut session =
Session::with_config(".", providers, WatchFilter::new("."), CompilerConfig::new());
session.add_project_files([std::path::PathBuf::from("src/main.cpp")]);
session.want_cooked_reading(std::path::Path::new("src/main.cpp"));
session.index_everything();
let view = session.view("src/main.cpp").expect("the file is held");
let notes = session.notes_about_the_modules(&view);
assert_eq!(notes.len(), 1, "one import, one note: {notes:?}");
let note = ¬es[0];
assert!(
note.message.contains("mylib"),
"the note names the module: {}",
note.message
);
assert!(
note.message.contains("unknown rather than absent"),
"and never claims its names do not exist: {}",
note.message
);
let line = "import mylib;";
let start = source.find(line).expect("the fixture writes it");
assert_eq!(note.start, start, "the note starts at the `import`");
assert!(
source[note.start..note.end].starts_with(line),
"and the range covers the declaration: {:?}",
&source[note.start..note.end]
);
assert!(
!source[note.start..note.end].contains("int main"),
"and stops before the next declaration: {:?}",
&source[note.start..note.end]
);
let files = MemoryFiles::new()
.with_file("src/main.cpp", "import mylib;\n")
.with_file("src/mylib.ixx", "export module mylib;\n");
let providers = SessionFiles::new(OpenDocuments::new(), files);
let mut session =
Session::with_config(".", providers, WatchFilter::new("."), CompilerConfig::new());
session.add_project_files([
std::path::PathBuf::from("src/main.cpp"),
std::path::PathBuf::from("src/mylib.ixx"),
]);
session.want_cooked_reading(std::path::Path::new("src/main.cpp"));
session.index_everything();
let view = session.view("src/main.cpp").expect("the file is held");
assert!(
session.notes_about_the_modules(&view).is_empty(),
"a module this project declares is not a note"
);
let _ = DiskFiles;
}
#[test]
fn a_partitions_names_reach_a_file_that_imports_the_module() {
use cpp_code_analysis::{DiskFiles, Known, OpenDocuments, Session, SessionFiles, WatchFilter};
let files = MemoryFiles::new()
.with_file(
"shapes.cppm",
"export module shapes;\n\
export import :area;\n\
export int perimeter(int, int);\n",
)
.with_file(
"shapes-area.cppm",
"export module shapes:area;\n\
export int rectangle(int, int);\n\
export int square(int);\n",
)
.with_file(
"partitions.cpp",
"import shapes;\nint area = rectangle(3, 4);\n",
);
let providers = SessionFiles::new(OpenDocuments::new(), files);
let mut session =
Session::with_config(".", providers, WatchFilter::new("."), CompilerConfig::new());
session.add_project_files([
std::path::PathBuf::from("shapes.cppm"),
std::path::PathBuf::from("shapes-area.cppm"),
std::path::PathBuf::from("partitions.cpp"),
]);
session.index_everything();
let perimeter = session
.index()
.definition("perimeter", Path::new("partitions.cpp"));
assert!(
matches!(perimeter, Known::Yes(_)),
"`perimeter` is declared in the interface unit itself: {perimeter:?}"
);
for name in ["rectangle", "square"] {
let found = session
.index()
.definition(name, Path::new("partitions.cpp"));
assert!(
matches!(found, Known::Yes(_)),
"`{name}` is declared in `shapes:area`, which `shapes.cppm` re-exports: {found:?}"
);
}
let _ = DiskFiles;
}
#[test]
fn a_partition_the_project_does_not_have_gets_a_note_about_its_module() {
use cpp_code_analysis::{DiskFiles, OpenDocuments, Session, SessionFiles, WatchFilter};
let interface = "export module shapes;\nexport import :area;\n";
let files = MemoryFiles::new().with_file("src/shapes.cppm", interface);
let providers = SessionFiles::new(OpenDocuments::new(), files);
let mut session =
Session::with_config(".", providers, WatchFilter::new("."), CompilerConfig::new());
session.add_project_files([std::path::PathBuf::from("src/shapes.cppm")]);
session.want_cooked_reading(std::path::Path::new("src/shapes.cppm"));
session.index_everything();
let view = session.view("src/shapes.cppm").expect("the file is held");
let notes = session.notes_about_the_modules(&view);
assert_eq!(notes.len(), 1, "one partition import, one note: {notes:?}");
assert!(
notes[0].message.contains(":area") && notes[0].message.contains("`shapes`"),
"the note names the partition and the module it belongs to: {}",
notes[0].message
);
assert!(
!notes[0].message.contains("declares module `area`"),
"and never reads a partition as a module: {}",
notes[0].message
);
let files = MemoryFiles::new()
.with_file("src/shapes.cppm", interface)
.with_file(
"src/shapes-area.cppm",
"export module shapes:area;\nexport int rectangle(int, int);\n",
);
let providers = SessionFiles::new(OpenDocuments::new(), files);
let mut session =
Session::with_config(".", providers, WatchFilter::new("."), CompilerConfig::new());
session.add_project_files([
std::path::PathBuf::from("src/shapes.cppm"),
std::path::PathBuf::from("src/shapes-area.cppm"),
]);
session.want_cooked_reading(std::path::Path::new("src/shapes.cppm"));
session.index_everything();
let view = session.view("src/shapes.cppm").expect("the file is held");
assert!(
session.notes_about_the_modules(&view).is_empty(),
"the partition's file is in the project, so there is nothing to be told"
);
let _ = DiskFiles;
}
#[test]
fn an_import_makes_the_modules_names_visible() {
use cpp_code_analysis::{DiskFiles, Known, OpenDocuments, Session, SessionFiles, WatchFilter};
let mut memory = MemoryFiles::new()
.with_file(
"mathlib.ixx",
"export module mathlib;\nexport namespace mathlib { int add(int, int); }\n",
)
.with_file(
"main.cpp",
"import mathlib;\nint f() { return mathlib::add(1, 2); }\n",
);
memory = memory.with_file("unrelated.h", "int elsewhere;\n");
let providers = SessionFiles::new(OpenDocuments::new(), memory);
let mut session = Session::with_config(
".",
providers,
WatchFilter::new("."),
CompilerConfig::default(),
);
session.add_project_files([
std::path::PathBuf::from("mathlib.ixx"),
std::path::PathBuf::from("main.cpp"),
]);
session.index_everything();
let found = session
.index()
.definition("mathlib::add", Path::new("main.cpp"));
assert!(
matches!(found, Known::Yes(_)),
"`main.cpp` says `import mathlib;`, so what the module exports is in view: {found:?}"
);
let _ = DiskFiles;
}
#[test]
fn the_gcc_standard_module_is_found_at_bits_std_cc() {
use cpp_code_analysis::{DiskFiles, Known, OpenDocuments, Session, SessionFiles, WatchFilter};
let files = MemoryFiles::new()
.with_file(
"gcc/include/c++/bits/std.cc",
"module;\n#define _GLIBCXX_USE_TBB_PAR_BACKEND 0\n\
export module std;\n\n\
export namespace std { class string {}; }\n\
export namespace std::inline literals::inline chrono_literals { int size(); }\n",
)
.with_file("src/main.cpp", "import std;\nstd::string greeting;\n");
let config = CompilerConfig::new().with_include_path("gcc/include/c++");
let providers = SessionFiles::new(OpenDocuments::new(), files);
let mut session = Session::with_config(".", providers, WatchFilter::new("."), config);
session.add_project_files([std::path::PathBuf::from("src/main.cpp")]);
session.index_everything();
assert!(
!matches!(
session
.index()
.definition("std::string", Path::new("src/main.cpp")),
Known::Yes(_)
),
"before the read, nothing has seen `bits/std.cc`"
);
assert_eq!(
session.read_the_modules_a_file_imports(Path::new("src/main.cpp")),
1,
"one interface unit: the module source"
);
assert_eq!(
session.index().interface_unit_of("std"),
Some("gcc/include/c++/bits/std.cc"),
"and the index now knows which file `import std;` reaches"
);
assert!(
matches!(
session
.index()
.definition("std::string", Path::new("src/main.cpp")),
Known::Yes(_)
),
"`std::string` is declared inside the exported namespace the module source writes"
);
assert_eq!(
session.read_the_modules_a_file_imports(Path::new("src/main.cpp")),
0,
"the second ask costs nothing — the property the request-driven design rests on"
);
let _ = DiskFiles;
}
#[test]
fn the_interface_unit_of_an_imported_module_can_be_queued_instead_of_read() {
use cpp_code_analysis::{DiskFiles, Known, OpenDocuments, Session, SessionFiles, WatchFilter};
let files = MemoryFiles::new()
.with_file(
"gcc/include/c++/bits/std.cc",
"module;\nexport module std;\nexport namespace std { class string {}; }\n",
)
.with_file("src/main.cpp", "import std;\nstd::string greeting;\n");
let config = CompilerConfig::new().with_include_path("gcc/include/c++");
let providers = SessionFiles::new(OpenDocuments::new(), files);
let mut session = Session::with_config(".", providers, WatchFilter::new("."), config);
session.add_project_files([std::path::PathBuf::from("src/main.cpp")]);
session.index_everything();
assert_eq!(
session.queue_the_modules_the_open_files_import(),
0,
"no open document imports anything yet"
);
session.did_open(
std::path::Path::new("src/main.cpp"),
"import std;\nstd::string greeting;\n",
);
session.index_everything();
assert_eq!(
session.queue_the_modules_the_open_files_import(),
1,
"the open file's `import std;` names one interface unit"
);
assert!(
session.pending_work() > 0,
"queued rather than read: the unit is work for the pump, not a write hold taken here"
);
assert_eq!(
session.index().interface_unit_of("std"),
None,
"and the index does not know it yet — that is what the slices are for"
);
session.index_everything();
assert_eq!(
session.index().interface_unit_of("std"),
Some("gcc/include/c++/bits/std.cc"),
"after the drain the module is in the index"
);
assert!(
matches!(
session
.index()
.definition("std::string", Path::new("src/main.cpp")),
Known::Yes(_)
),
"and the names it exports resolve from the importing file"
);
assert_eq!(
session.queue_the_modules_the_open_files_import(),
0,
"the second ask finds the module already in the index and queues nothing"
);
let _ = DiskFiles;
}
#[test]
fn a_module_unit_is_read_as_a_translation_unit() {
use cpp_code_analysis::{OpenDocuments, SessionFiles, WatchFilter};
let std_cc = "module;\n#include <bits/thing.h>\nexport module std;\nexport namespace std { }\n";
let thing = "namespace std {\nstruct Widget {\n void go();\n};\n}\n";
let demo = "import std;\nint main() { return 0; }\n";
let files = MemoryFiles::new()
.with_file("/inc/bits/std.cc", std_cc)
.with_file("/inc/bits/thing.h", thing)
.with_file("/p/std_demo.cpp", demo);
let mut config = CompilerConfig::new();
config.include_paths = vec![cpp_code_analysis::IncludePath::system("/inc")];
let mut session = Session::with_config(
"/p",
SessionFiles::new(OpenDocuments::new(), files),
WatchFilter::new("/p"),
config,
);
session.add_project_files([std::path::PathBuf::from("/p/std_demo.cpp")]);
session.did_open("/p/std_demo.cpp", demo);
session.index_everything();
let mut rounds = 0;
while session.pending() > 0 && rounds < 200 {
session.advance(16);
rounds += 1;
}
assert_eq!(
session.read_the_modules_a_file_imports(std::path::Path::new("/p/std_demo.cpp")),
1,
"the request path finds the interface unit"
);
let mut rounds = 0;
while session.pending() > 0 && rounds < 400 {
session.advance(16);
rounds += 1;
}
let unit = session
.read_a_looked_at_unit()
.expect("the interface unit is a program — that is what this test is about");
assert!(
unit.files >= 2,
"a program read of `bits/std.cc` reads its closure, not one file: {unit:?}"
);
let _opened = session.read_a_looked_at_unit();
let opened = session.read_a_looked_at_unit();
assert!(
opened.is_none(),
"a file that imports a module is served by the module's reading: {opened:?}"
);
let header = std::path::Path::new("/inc/bits/thing.h");
let declarations = session
.index()
.cooked_declarations(header)
.expect("the header has a reading from the unit that included it");
assert!(
declarations
.iter()
.any(|declaration| &*declaration.name == "Widget"),
"and `Widget` is one of its declarations: {declarations:?}"
);
assert_eq!(
session
.index()
.the_program_that_read(std::path::Path::new("/p/std_demo.cpp")),
session
.index()
.the_program_that_read(std::path::Path::new("/inc/bits/std.cc")),
"a file that imports a module is compiled with the module's build"
);
}