use cpp_code_analysis::{
CompilerConfig, MemoryFiles, OpenDocuments, Session, SessionFiles, WatchFilter,
};
const SOURCE: &str = "\
struct Foo {
template <typename Fn>
void call(Fn &&fn) {
fn(*this);
}
template <typename Fn>
void call(Fn &&fn) const {
fn(*this);
}
};
struct Bar {
template <typename Fn>
void call(Fn &&fn)
requires requires { fn(*this); }
{
fn(*this);
}
template <typename Fn>
void call(Fn &&fn) const
requires requires { fn(*this); }
{
fn(*this);
}
};
void run() {
Foo foo;
foo.call([](auto &&a) {});
const Foo cfoo;
cfoo.call([](auto &&b) {});
Bar bar;
bar.call([](auto &&c) {});
const Bar cbar;
cbar.call([](auto &&d) {});
// **Not a call argument at all**, so there is no call site to read a type from.
auto alone = [](auto e) {};
alone(1);
}
";
fn session_with(source: &str) -> Session<MemoryFiles> {
let memory = MemoryFiles::new().with_file("/p/a.cpp", source);
let providers = SessionFiles::new(OpenDocuments::new(), memory);
let mut session = Session::with_config(
"/p",
providers,
WatchFilter::new("/p"),
CompilerConfig::default(),
);
session.add_project_files([std::path::PathBuf::from("/p/a.cpp")]);
session.index_everything();
session
}
fn parameter_of(source: &str, offset: usize) -> String {
let before = &source[..offset.min(source.len())];
before
.split(|character: char| !character.is_alphanumeric() && character != '_')
.next_back()
.unwrap_or_default()
.to_string()
}
#[test]
fn a_generic_lambdas_parameter_gets_the_type_the_call_site_gives_it() {
let session = session_with(SOURCE);
let view = session
.view_of_the_file("/p/a.cpp")
.expect("the file is readable");
let range = cpp_parser::SourceRange::new(0, SOURCE.len());
let found: std::collections::HashMap<String, String> = session
.type_hints(&view, range)
.into_iter()
.filter(|hint| hint.kind == cpp_code_analysis::TypeHintKind::LambdaParameter)
.map(|hint| (parameter_of(SOURCE, hint.offset), hint.label))
.collect();
assert_eq!(
found.get("a").map(String::as_str),
Some("Foo&"),
"a non-const object: the non-const `call`, and `*this` is an lvalue: {found:?}"
);
assert_eq!(
found.get("b").map(String::as_str),
Some("const Foo&"),
"a const object reaches only the const overload, where `*this` is `const Foo`: {found:?}"
);
assert_eq!(
found.get("c"),
None,
"two constrained candidates: which one runs is not decidable, so nothing is claimed: {found:?}"
);
assert_eq!(
found.get("d").map(String::as_str),
Some("const Bar&"),
"one candidate is reachable, so its constraint is not what chose it: {found:?}"
);
assert_eq!(
found.get("e"),
None,
"a lambda that is not a call argument has no call site to read: {found:?}"
);
}