use std::path::{Path, PathBuf};
use cpp_code_analysis::{
CompilerConfig, MemoryFiles, OpenDocuments, Session, SessionFiles, WatchFilter,
};
const HEADER: &str = r#"#pragma once
#ifdef A_FEATURE_THIS_PROJECT_DOES_NOT_DEFINE
#define SHAPE(x) struct x { int from_if; };
#else
#define SHAPE(x) struct x { int from_else; };
#endif
"#;
const INTERFACE: &str = "module;\n#include <shape.h>\nexport module m;\n\nSHAPE(Widget)\n";
fn session_with(files: MemoryFiles) -> Session<MemoryFiles> {
let config = CompilerConfig::new().with_include_path("lib/include");
let mut session = Session::with_config(
".",
SessionFiles::new(OpenDocuments::new(), files),
WatchFilter::new("."),
config,
);
session.add_project_files([PathBuf::from("src/main.cpp")]);
session.index_everything();
session
}
fn facts_named(session: &Session<MemoryFiles>, name: &str) -> Vec<(Option<String>, String)> {
session
.index()
.every_fact_named(name)
.into_iter()
.map(|(_, fact)| (fact.scope.clone(), format!("{:?}", fact.kind)))
.collect()
}
#[test]
fn a_definition_inside_a_conditional_branch_is_in_force_for_the_file_that_uses_it() {
let files = MemoryFiles::new()
.with_file("lib/modules/m.ixx", INTERFACE)
.with_file("lib/include/shape.h", HEADER)
.with_file("src/main.cpp", "import m;\n");
let mut session = session_with(files);
let read = session.read_the_modules_a_file_imports(Path::new("src/main.cpp"));
assert_eq!(read, 1, "one interface unit: `m.ixx`, and nothing else");
let widgets = facts_named(&session, "Widget");
assert!(
widgets.iter().any(|(scope, _)| scope.is_none()),
"`SHAPE(Widget)` expanded into a declaration: {widgets:?}"
);
let members = facts_named(&session, "from_else");
assert!(
members
.iter()
.any(|(scope, _)| scope.as_deref() == Some("Widget")),
"and it was the branch that was taken — `#else`, since the feature is undefined: {members:?}"
);
}
#[test]
fn an_unconditional_definition_from_the_same_header_does_expand() {
let files = MemoryFiles::new()
.with_file(
"lib/modules/m.ixx",
"module;\n#include <shape.h>\nexport module m;\n\nSHAPE_ALWAYS(Gadget)\n",
)
.with_file(
"lib/include/shape.h",
"#pragma once\n#define SHAPE_ALWAYS(x) struct x { int from_always; };\n",
)
.with_file("src/main.cpp", "import m;\n");
let mut session = session_with(files);
let read = session.read_the_modules_a_file_imports(Path::new("src/main.cpp"));
assert_eq!(read, 1, "one interface unit: `m.ixx`, and nothing else");
let gadgets = facts_named(&session, "Gadget");
assert!(
gadgets.iter().any(|(scope, _)| scope.is_none()),
"the unconditional macro expanded into a declaration: {gadgets:?}"
);
}
#[ignore = "known: an empty-bodied macro in a `#if` expression evaluates as false, not `Unknown`"]
#[test]
fn a_definition_in_a_branch_nobody_can_decide_is_still_in_force() {
let header = r#"#pragma once
#if A_FEATURE_VALUE == 1
#define SHAPE(x) struct x { int from_if; };
#else
#define SHAPE(x) struct x { int from_else; };
#endif
"#;
let files = MemoryFiles::new()
.with_file("lib/modules/m.ixx", INTERFACE)
.with_file("lib/include/shape.h", header)
.with_file("lib/include/value.h", "#define A_FEATURE_VALUE\n")
.with_file("src/main.cpp", "import m;\n");
let interface =
"module;\n#include <value.h>\n#include <shape.h>\nexport module m;\n\nSHAPE(Widget)\n";
let files = files.with_file("lib/modules/m.ixx", interface);
let mut session = session_with(files);
let read = session.read_the_modules_a_file_imports(Path::new("src/main.cpp"));
assert_eq!(read, 1, "one interface unit: `m.ixx`, and nothing else");
let widgets = facts_named(&session, "Widget");
assert!(
widgets.iter().any(|(scope, _)| scope.is_none()),
"the macro from an undecidable branch expanded at the use site: {widgets:?}"
);
let members = facts_named(&session, "from_else");
assert!(
members
.iter()
.any(|(scope, _)| scope.as_deref() == Some("Widget")),
"and it was the `#else` branch — the one a compiler takes when the value is absent: {members:?}"
);
}