use cpp_parser::features::{LanguageConfig, Standard};
use cpp_parser::{
CppAstNode, CppDeclaration, CppDeclarator, CppParser, CppSyntaxKind, CppTranslationUnit,
ParserConfig,
};
fn errors_in(source: &str, language: LanguageConfig) -> Vec<String> {
let tree = CppParser::parse(source, ParserConfig::default().with_language(language));
tree.get_errors()
.iter()
.map(|error| format!("{error:?}"))
.collect()
}
fn c11() -> LanguageConfig {
LanguageConfig::new().with_standard(Standard::C11)
}
fn cpp23() -> LanguageConfig {
LanguageConfig::new().with_standard(Standard::Cpp23)
}
#[test]
fn names_cpp_reserves_are_names_in_c() {
let sources = [
(
"a variable called new",
"struct T { int x; }; struct T *new = 0; void g(void) { new->x = 1; }",
),
(
"an object called new",
"struct O { int x; }; struct O new = { 0 };",
),
(
"a parameter called delete",
"void f(int delete) { (void)delete; }",
),
(
"a variable called compl",
"int compl = 0; int g(void) { compl = compl + 1; return compl; }",
),
(
"a variable called this",
"int this = 0; int g(void) { return this; }",
),
("a variable called operator", "int operator = 0;"),
("a variable called template", "int template = 0;"),
("a variable called class", "int class = 0;"),
];
for (name, source) in sources {
let errors = errors_in(source, c11());
assert!(errors.is_empty(), "{name} does not parse in C: {errors:?}");
}
}
#[test]
fn a_parenthesised_declarator_names_what_it_holds() {
let declarations = [
("a keyword type", "static char *(lines[]) = { 0 };", "lines"),
(
"a keyword type, sized",
"static char *(names[3]) = { 0 };",
"names",
),
(
"a named type",
"typedef int T; static T *(lines[3]) = { 0 };",
"lines",
),
(
"an elaborated type",
"struct S { int a; }; static struct S *(lines[2]) = { 0 };",
"lines",
),
(
"a qualified type",
"typedef struct { int a; } S; static S *const (lines[2]) = { 0 };",
"lines",
),
(
"a type from a header",
"static histentry_T *(history[HIST_COUNT]) = { 0 };",
"history",
),
("no storage class", "char *(lines[]) = { 0 };", "lines"),
(
"a pointer with no suffix",
"static char *(line) = 0;",
"line",
),
];
for (what, source, declared) in declarations {
let tree = CppParser::parse(source, ParserConfig::default().with_language(c11()));
assert_eq!(
tree.get_errors(),
[],
"{what} does not parse: {source} — {:?}",
tree.get_errors()
);
let root = CppTranslationUnit::cast(tree.get_red_root()).expect("a translation unit");
let name = root
.get_declarations()
.last()
.and_then(|declaration: CppDeclaration| declaration.get_name_text());
assert_eq!(
name.as_deref(),
Some(declared),
"{what} declares the wrong thing: {source}"
);
}
}
#[test]
fn a_parenthesised_parameter_names_what_it_holds() {
let source = "static void createcat (int (catf) (int)) { }";
let tree = CppParser::parse(source, ParserConfig::default().with_language(c11()));
assert_eq!(tree.get_errors(), [], "{source} does not parse");
let root = CppTranslationUnit::cast(tree.get_red_root()).expect("a translation unit");
let declaration = root
.get_declarations()
.next()
.expect("the function is a declaration");
assert_eq!(declaration.get_name_text().as_deref(), Some("createcat"));
let mut names: Vec<String> = declaration
.syntax()
.descendants()
.filter(|node| node.kind() == cpp_parser::CppKind::Syntax(CppSyntaxKind::Parameter))
.flat_map(|parameter| parameter.descendants().filter_map(CppDeclarator::cast))
.filter_map(|declarator| declarator.get_name_text())
.collect();
names.sort();
names.dedup();
assert_eq!(names, ["catf"], "the parameter is named by the group");
}
#[test]
fn a_product_is_not_a_parenthesised_declarator() {
let source = "int b; void f(int a) { a * (b); }";
let tree = CppParser::parse(source, ParserConfig::default().with_language(c11()));
assert_eq!(tree.get_errors(), [], "{source} does not parse");
let root = CppTranslationUnit::cast(tree.get_red_root()).expect("a translation unit");
let declarations: Vec<String> = root
.syntax()
.descendants()
.filter_map(CppDeclaration::cast)
.filter_map(|declaration| declaration.get_name_text())
.collect();
assert_eq!(
declarations,
["b", "f"],
"`b` must not be declared a second time inside `f`"
);
let source = "void f(int *p, int q) { *(p) = q; }";
let tree = CppParser::parse(source, ParserConfig::default().with_language(c11()));
assert_eq!(tree.get_errors(), [], "{source} does not parse");
let root = CppTranslationUnit::cast(tree.get_red_root()).expect("a translation unit");
let declared: Vec<String> = root
.syntax()
.descendants()
.filter_map(CppDeclaration::cast)
.filter_map(|declaration| declaration.get_name_text())
.collect();
assert_eq!(
declared,
["f"],
"{source} declares nothing but the function"
);
}
#[test]
fn the_same_names_are_keywords_in_cpp() {
assert!(!errors_in("int new = 0;", cpp23()).is_empty());
assert!(!errors_in("void f(int delete);", cpp23()).is_empty());
let errors = errors_in("void f() { int *p = new int(1); (void)p; }", cpp23());
assert!(errors.is_empty(), "{errors:?}");
}