use cpp_code_analysis::{
CompilerConfig, MemoryFiles, OpenDocuments, Session, SessionFiles, WatchFilter,
};
fn facts_of(source: &str) -> Vec<cpp_code_analysis::DeclFact> {
let files = MemoryFiles::new().with_file("/p/a.c", source);
let providers = SessionFiles::new(OpenDocuments::new(), files);
let mut session = Session::with_config(
"/p",
providers,
WatchFilter::new("/p"),
CompilerConfig::default(),
);
session.add_project_files([std::path::PathBuf::from("/p/a.c")]);
session.want_cooked_reading(std::path::Path::new("/p/a.c"));
session.index_everything();
session
.index()
.summary(std::path::Path::new("/p/a.c"))
.map(|summary| summary.declarations.to_vec())
.unwrap_or_default()
}
#[test]
fn a_parenthesised_declarator_keeps_its_parameters() {
let facts = facts_of(
"\
void (foo) (int a, char *b);
void (*bar)(int a);
void plain (int a, char *b);
",
);
let foo = facts
.iter()
.find(|fact| fact.name == "foo")
.expect("foo is declared");
assert_eq!(foo.kind, cpp_code_analysis::DeclKind::Function);
assert_eq!(
foo.parameter_list(),
Some("(int a, char *b)"),
"the list is beside the declarator, and it is the function's"
);
let bar = facts
.iter()
.find(|fact| fact.name == "bar")
.expect("bar is declared");
assert_eq!(
bar.kind,
cpp_code_analysis::DeclKind::Variable,
"a function pointer variable declares a variable"
);
assert_eq!(
bar.parameter_list(),
None,
"and its parameter list belongs to the pointed-to type"
);
let plain = facts
.iter()
.find(|fact| fact.name == "plain")
.expect("plain is declared");
assert_eq!(plain.parameter_list(), Some("(int a, char *b)"));
}
#[test]
fn a_parenthesised_declarators_parameters_are_locals() {
use cpp_code_analysis::Known;
use cpp_code_analysis::file::coordinates::ReadingOffset;
let source = "void (foo) (int a, char *b);\n";
let files = MemoryFiles::new().with_file("/p/a.c", source);
let providers = SessionFiles::new(OpenDocuments::new(), files);
let mut session = Session::with_config(
"/p",
providers,
WatchFilter::new("/p"),
CompilerConfig::default(),
);
session.add_project_files([std::path::PathBuf::from("/p/a.c")]);
session.index_everything();
session.load(std::path::Path::new("/p/a.c"));
let held = session
.files()
.held(std::path::Path::new("/p/a.c"))
.expect("the file is readable")
.clone();
let view =
cpp_code_analysis::FileView::parse_with(&held, &cpp_parser::MacroEnvironment::default());
let at = source.find("int a").expect("the parameter") + "int ".len();
match session.definitions(&view, ReadingOffset::new(at)) {
Known::Yes(found) => {
let parameter = &found.found[0].fact;
assert_eq!(parameter.name, "a");
assert!(
parameter.local,
"a parameter is a local of the function's scope: {parameter:?}"
);
}
other => panic!("the parameter resolves: {other:?}"),
}
}
#[test]
fn a_typedef_of_a_pointer_is_followed_for_a_member() {
use cpp_code_analysis::file::coordinates::ReadingOffset;
let source = "\
struct StackValue { int val; };
typedef struct StackValue *StkId;
struct CallInfo { union { StkId p; long offset; } func; };
int f(struct CallInfo *ci) { return ci->func.p->val; }
int g(StkId s) { return s->val; }
";
let files = MemoryFiles::new().with_file("/p/a.c", source);
let providers = SessionFiles::new(OpenDocuments::new(), files);
let mut session = Session::with_config(
"/p",
providers,
WatchFilter::new("/p"),
CompilerConfig::default(),
);
session.add_project_files([std::path::PathBuf::from("/p/a.c")]);
session.want_cooked_reading(std::path::Path::new("/p/a.c"));
session.index_everything();
session.load(std::path::Path::new("/p/a.c"));
let held = session
.files()
.held(std::path::Path::new("/p/a.c"))
.expect("the file is readable")
.clone();
let view =
cpp_code_analysis::FileView::parse_with(&held, &cpp_parser::MacroEnvironment::default());
for (needle, expected) in [("ci->func.p->val", "int"), ("return s->val", "int")] {
let at = source.find(needle).expect("the chain") + needle.find("val").expect("the member");
let found = session.type_at(&view, ReadingOffset::new(at));
match found {
cpp_code_analysis::Known::Yes(found) => assert_eq!(found.type_of, expected),
other => panic!("{needle}: {other:?}"),
}
}
}
#[test]
fn a_member_of_an_elaborated_cast_is_found() {
use cpp_code_analysis::file::coordinates::ReadingOffset;
let source = "\
struct Proto { int nup; };
union Closure { struct LClosure { struct Proto *p; int n; } l; };
union GCUnion { void *gc; union Closure cl; };
int f(void *object) {
return ((union GCUnion *)object)->cl.l.p->nup;
}
";
let files = MemoryFiles::new().with_file("/p/a.c", source);
let providers = SessionFiles::new(OpenDocuments::new(), files);
let mut session = Session::with_config(
"/p",
providers,
WatchFilter::new("/p"),
CompilerConfig::default(),
);
session.add_project_files([std::path::PathBuf::from("/p/a.c")]);
session.want_cooked_reading(std::path::Path::new("/p/a.c"));
session.index_everything();
session.load(std::path::Path::new("/p/a.c"));
let held = session
.files()
.held(std::path::Path::new("/p/a.c"))
.expect("the file is readable")
.clone();
let view =
cpp_code_analysis::FileView::parse_with(&held, &cpp_parser::MacroEnvironment::default());
let at = source.find("nup;").expect("the member") + 1;
match session.type_at(&view, ReadingOffset::new(at)) {
cpp_code_analysis::Known::Yes(found) => assert_eq!(found.type_of, "int"),
other => panic!("`->nup` through an elaborated cast: {other:?}"),
}
}