use cpp_code_analysis::file::coordinates::{FileOffset, ReadingOffset};
use cpp_code_analysis::{
CompilerConfig, Known, MemoryFiles, OpenDocuments, Session, SessionFiles, WatchFilter,
};
fn session_of(files: &[(&str, &str)]) -> Session<MemoryFiles> {
let mut memory = MemoryFiles::new();
let mut paths = Vec::new();
for (path, text) in files {
memory = memory.with_file(*path, *text);
paths.push(std::path::PathBuf::from(path));
}
let providers = SessionFiles::new(OpenDocuments::new(), memory);
let mut session = Session::with_config(
"/p",
providers,
WatchFilter::new("/p"),
CompilerConfig::default(),
);
session.add_project_files(paths.clone());
for path in &paths {
session.load(path);
session.want_cooked_reading(path);
}
session.index_everything();
session
}
fn where_at(session: &Session<MemoryFiles>, path: &str, needle: &str) -> String {
let path = std::path::Path::new(path);
let view = session.view(path).expect("the file has a view");
let at = view
.written_text()
.expect("the file is its own text")
.find(needle)
.unwrap_or_else(|| panic!("`{needle}` is in the file"));
let reading = view
.reading_offset_of(FileOffset::new(at))
.expect("the file offset is in the reading");
match session.definition(&view, ReadingOffset::new(reading.get())) {
Known::Yes(found) => match &found.fact.scope {
Some(scope) => format!("{scope}::{}", found.fact.name),
None => found.fact.name.to_string(),
},
Known::Unknown(reason) => format!("unknown: {reason:?}"),
Known::No => "no".to_string(),
}
}
const LIBRARY: &str = "\
namespace ns {
template <class T = int>
struct basic {
static basic make();
T value;
};
using simple = basic<int>;
using defaulted = basic<>;
}
";
#[test]
fn a_member_through_an_alias_is_the_aliased_templates_member() {
let written = "\
#include \"lib.h\"
int main() {
auto made = ns::simple::make();
return 0;
}
";
let session = session_of(&[("/p/lib.h", LIBRARY), ("/p/main.cpp", written)]);
let found = where_at(&session, "/p/main.cpp", "make");
assert!(
found.ends_with("::make") && found.contains("basic"),
"the alias resolves to basic<int>::make, not nothing: {found}"
);
}
#[test]
fn a_member_through_an_alias_with_default_arguments_is_the_same_member() {
let written = "\
#include \"lib.h\"
int main() {
auto made = ns::defaulted::make();
return 0;
}
";
let session = session_of(&[("/p/lib.h", LIBRARY), ("/p/main.cpp", written)]);
let found = where_at(&session, "/p/main.cpp", "make");
assert!(
found.ends_with("::make") && found.contains("basic"),
"basic<> is basic<int>, and make is its member: {found}"
);
}
const BASIC: &str = "\
namespace ns {
template <class T = int>
struct basic {
static basic make();
T value;
};
}
";
const ALIAS: &str = "\
#pragma once
#include \"basic.h\"
namespace ns {
using simple = basic<>;
}
";
#[test]
fn an_alias_in_one_file_resolves_a_member_in_another() {
let written = "\
#include \"alias.h\"
int main() {
auto made = ns::simple::make();
return 0;
}
";
let session = session_of(&[
("/p/basic.h", BASIC),
("/p/alias.h", ALIAS),
("/p/main.cpp", written),
]);
let found = where_at(&session, "/p/main.cpp", "make");
println!("found: {found}");
assert!(
found.ends_with("::make") && found.contains("basic"),
"the alias in another file resolves to basic<>::make: {found}"
);
}
const PROBE_SHAPE: &str = "\
#include \"alias.h\"
struct Holder {
void probe();
};
void Holder::probe() {
auto made = ns::simple::make();
}
";
#[test]
fn a_qualified_name_inside_a_member_definition_is_not_the_classes_member() {
let session = session_of(&[
("/p/basic.h", BASIC),
("/p/alias.h", ALIAS),
("/p/probe.cpp", PROBE_SHAPE),
]);
let found = where_at(&session, "/p/probe.cpp", "make");
println!("found: {found}");
assert!(
found.ends_with("::make") && found.contains("basic"),
"the qualified name is resolved through the alias, not as Holder's member: {found}"
);
}
#[test]
#[ignore = "a probe: prints the alias fact the index holds"]
fn the_alias_fact_fields() {
let session = session_of(&[
("/p/basic.h", BASIC),
("/p/alias.h", ALIAS),
(
"/p/main.cpp",
"#include \"alias.h\"\nint main() { return 0; }\n",
),
]);
for name in ["ns::simple", "ns::basic", "ns::simple::make"] {
match session
.index()
.definition(name, std::path::Path::new("/p/main.cpp"))
{
Known::Yes(found) => println!(
"[fact] {name}: kind {:?}, scope {:?}, type_of {:?}, returns {:?}",
found.fact.kind, found.fact.scope, found.fact.type_of, found.fact.returns
),
other => println!("[fact] {name}: {:?}", other),
}
}
}