use cpp_parser::{CppParser, CppSyntaxKind, ParserConfig};
struct Parsed {
calls: usize,
initializers: usize,
errors: Vec<String>,
}
fn parse(source: &str) -> Parsed {
let tree = CppParser::parse(source, ParserConfig::default());
let root = tree.get_red_root();
let count = |kind: CppSyntaxKind| {
root.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == kind)
.count()
};
Parsed {
calls: count(CppSyntaxKind::CallExpr),
initializers: count(CppSyntaxKind::Initializer),
errors: tree
.get_errors()
.iter()
.map(|error| error.message.to_string())
.collect(),
}
}
fn assert_declaration(source: &str) {
let parsed = parse(source);
assert!(
parsed.errors.is_empty(),
"{source:?} should parse cleanly, got {:?}",
parsed.errors
);
assert_eq!(
parsed.initializers, 1,
"{source:?} should be a declaration with a direct initialiser"
);
}
fn assert_plain_declaration(source: &str) {
assert_declaration(source);
assert_eq!(
parse(source).calls,
0,
"{source:?} should not contain a call expression"
);
}
fn assert_call(source: &str) {
let parsed = parse(source);
assert!(
parsed.errors.is_empty(),
"{source:?} should parse cleanly, got {:?}",
parsed.errors
);
assert_eq!(parsed.calls, 1, "{source:?} should be a call expression");
assert_eq!(
parsed.initializers, 0,
"{source:?} should not contain a direct initialiser"
);
}
#[test]
fn a_keyword_type_makes_it_a_declaration() {
assert_declaration("void f() { int a(1); }\n");
assert_declaration("void f() { double d(1.5); }\n");
assert_declaration("void f() { unsigned long n(0); }\n");
assert_declaration("void f() { char c('x'); }\n");
}
#[test]
fn a_type_the_file_declares_makes_it_a_declaration() {
assert_declaration("struct Widget {};\nvoid f() { Widget w(1, 2); }\n");
assert_declaration("class Widget {};\nvoid f() { Widget w(1, 2); }\n");
assert_declaration("union Widget {};\nvoid f() { Widget w(1, 2); }\n");
assert_declaration("enum class Widget {};\nvoid f() { Widget w(1, 2); }\n");
}
#[test]
fn an_alias_makes_it_a_declaration() {
assert_declaration("using Widget = int;\nvoid f() { Widget w(1, 2); }\n");
assert_declaration("typedef int Integer;\nvoid f() { Integer i(1); }\n");
assert_declaration("typedef struct Point { int x; } Point;\nvoid f() { Point p(1); }\n");
}
#[test]
fn a_nested_type_is_declared_where_it_is_written() {
assert_declaration("struct Outer { struct Inner {}; void m() { Inner i(1); } };\n");
}
#[test]
fn a_type_name_stays_a_type_after_its_scope_closes() {
assert_declaration("void g() { struct Widget {}; Widget w(1); }\n");
}
#[test]
fn a_type_the_file_never_declares_is_still_a_declaration() {
assert_declaration("void f() { Widget w(1, 2, 3); }\n");
assert_declaration("void f() { Widget w(1); }\n");
assert_declaration("void f() { Widget w(\"name\"); }\n");
assert_declaration("void f() { std::string s(\"x\"); }\n");
assert_declaration("Widget w(1, 2, 3);\n");
}
#[test]
fn a_declarator_with_no_name_leaves_the_parentheses_alone() {
let parsed = parse("namespace ns { A(B); }\n");
assert!(
parsed.errors.is_empty(),
"the namespace should close cleanly, got {:?}",
parsed.errors
);
}
#[test]
fn a_bare_name_call_stays_a_call() {
assert_call("void f() { g(1); }\n");
assert_call("void f() { g(1, 2); }\n");
assert_call("void f() { g(); }\n");
assert_call("void f() { obj.m(1); }\n");
assert_call("void f() { A(B); }\n");
assert_call("void f() { A(B + C); }\n");
}
#[test]
fn a_function_declaration_is_not_a_direct_initialiser() {
let parsed = parse("struct Widget {};\nWidget make();\n");
assert!(parsed.errors.is_empty(), "got {:?}", parsed.errors);
assert_eq!(parsed.initializers, 0, "`T x()` is a function declaration");
}
#[test]
fn a_parameter_list_is_not_a_value_list() {
for source in [
"void f() { int a(b); }\n",
"void f() { int a(b, c); }\n",
"void f(int (*fp)(int, double));\n",
"void f(int* p, int& r, const int* q);\n",
] {
assert_eq!(
parse(source).initializers,
0,
"{source:?} holds a parameter list, not a value list"
);
}
let with_default = parse("void f(int a = 1);\n");
assert!(
with_default.errors.is_empty(),
"a defaulted parameter should parse, got {:?}",
with_default.errors
);
assert_eq!(with_default.initializers, 1);
}
#[test]
fn a_value_list_is_not_a_parameter_list() {
assert_plain_declaration("void f() { Widget w(1, 2, 3); }\n");
assert_plain_declaration("void f() { Widget w(!flag); }\n");
}
#[test]
fn a_call_in_the_arguments_is_not_a_call_statement() {
assert_declaration("void f() { Widget w(g(), h()); }\n");
assert_eq!(parse("void f() { Widget w(g(), h()); }\n").calls, 2);
assert_declaration("void f() { Widget w(Inner(1)); }\n");
assert_eq!(parse("void f() { Widget w(Inner(1)); }\n").calls, 1);
}
#[test]
fn an_element_is_judged_by_the_token_after_the_name() {
assert_plain_declaration("void f() { Widget w(a + 1); }\n");
assert_plain_declaration("void f() { Widget w(a == b); }\n");
let multiplication = parse("void f() { Widget w(a * b); }\n");
assert!(
multiplication.errors.is_empty(),
"`a * b` should still parse, got {:?}",
multiplication.errors
);
assert_eq!(multiplication.initializers, 0, "`a * b` is a product");
}
#[test]
fn a_qualified_name_is_judged_after_the_whole_chain() {
for source in [
"void f() { Widget w(a, B::C); }\n",
"void f() { Widget w(a, B::C()); }\n",
"void f() { Widget w(a, std::move(b)); }\n",
"void f() { Widget w(std::vector<int> v); }\n",
] {
let parsed = parse(source);
assert!(
parsed.errors.is_empty(),
"{source:?} should parse cleanly, got {:?}",
parsed.errors
);
assert_eq!(
parsed.initializers, 0,
"{source:?} is a parameter list, not a direct initialiser"
);
}
for source in [
"void f() { Widget w(a, B::C(1)); }\n",
"void f() { Widget w(a, std::move(b.c())); }\n",
"void f() { Widget w(a, n::m(b.c())); }\n",
"void f() { LuaParser p(file, std::move(luaLexer.GetTokens())); }\n",
"void f() { std::fstream fin(newPath, std::ios::in | std::ios::binary); }\n",
] {
assert_declaration(source);
}
}
#[test]
fn an_unnamed_pointer_parameter_is_the_one_reading_left_to_the_type_table() {
for source in [
"void f() { Widget w(*p); }\n",
"void f() { Widget w(&r); }\n",
"void f() { Widget w(a, &b); }\n",
] {
let parsed = parse(source);
assert!(
parsed.errors.is_empty(),
"{source:?} should parse cleanly, got {:?}",
parsed.errors
);
assert_eq!(
parsed.initializers, 1,
"{source:?} is a declaration with a direct initialiser — `cl.exe` refuses the parameter reading"
);
}
}
#[test]
fn unknown_leading_names_become_declarations_at_file_scope() {
for source in [
"Max(a, b);\n",
"Widget w(mode);\n",
"Result r(first, second, third);\n",
] {
assert_declaration(source);
}
}
#[test]
fn an_argument_that_is_not_a_bare_name_keeps_it_a_call() {
for source in [
"Max(a, 1);\n",
"Max(p.first, q);\n",
"Max(a::b, c);\n",
"Max(a + 1, b);\n",
"Max(*p, b);\n",
"Max(g(a), b);\n",
] {
assert_eq!(
parse(source).initializers,
0,
"{source:?} has an element that is not a bare name"
);
}
}
#[test]
fn the_file_scope_guess_does_not_reach_inside_a_body() {
assert_call("void f() { Max(a, b); }\n");
assert_call("void f() { Max(a); }\n");
}
#[test]
fn a_nested_construction_is_an_initializer() {
assert_declaration("struct Inner {};\nstruct Widget {};\nWidget w(Inner(1));\n");
}
#[test]
fn a_call_nested_inside_an_element_is_not_a_construction() {
assert_eq!(
parse("Max(g(a), b);\n").initializers,
0,
"a call nested inside an element should not make the list a declarator list"
);
}
#[test]
fn an_assignment_is_not_a_declaration() {
for source in [
"void f() { x = 1; }\n",
"void f() { x = a; }\n",
"void f() { x = a + b; }\n",
"void f() { x = a ? b : c; }\n",
"void f() { x = f(); }\n",
"void f() { value = other; }\n",
"void f() { count = 0; }\n",
"void f() { result = compute(); }\n",
"void f() { cache = table[key]; }\n",
"x = 1;\n",
] {
let tree = CppParser::parse(source, ParserConfig::default());
assert_eq!(
tree.get_errors(),
[],
"{source:?} is valid code and must parse cleanly"
);
let statement = CppParser::parse(source, ParserConfig::default())
.get_red_root()
.descendants()
.find(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::CompoundStat)
.and_then(|body| {
body.children()
.filter(|child| child.children().next().is_some())
.last()
})
.map(|node| CppSyntaxKind::from(node.kind()))
.or_else(|| {
CppParser::parse(source, ParserConfig::default())
.get_red_root()
.children()
.next()
.map(|node| CppSyntaxKind::from(node.kind()))
});
assert_eq!(
statement,
Some(CppSyntaxKind::ExpressionStat),
"{source:?} is an assignment, so its statement must be an expression"
);
assert_eq!(
count_of(source, CppSyntaxKind::Initializer),
0,
"{source:?} has no initialiser — an initialiser needs a declarator name"
);
}
}
#[test]
fn a_qualified_declarator_keeps_its_reading() {
for source in [
"int ns::count = 0;\n",
"int ns::Widget::count = 0;\n",
"int A::b = 1;\n",
"int Outer::Inner::value = 42;\n",
] {
let tree = CppParser::parse(source, ParserConfig::default());
assert_eq!(
tree.get_errors(),
[],
"{source:?} must parse cleanly, got {:?}",
tree.get_errors()
);
assert_eq!(tree.to_source_text(), source, "{source:?} stays lossless");
assert!(
tree.get_red_root()
.descendants()
.any(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::Declaration),
"{source:?} declares something"
);
}
}
#[test]
fn a_cv_qualifier_after_the_type_does_not_swallow_the_name() {
for source in [
"char const w[] = { 'a' };\n",
"char const w[2] = { 'a' };\n",
"char const w[2];\n",
"int const x = 1;\n",
"int const x;\n",
"static char const w[2] = { 'a' };\n",
"unsigned const int y = 1;\n",
"struct S const s;\n",
"char const *p = 0;\n",
"const char w[] = { 'a' };\n",
"alignas(16) const MyType value;\n",
"void f() { char const buf[4] = { 0 }; }\n",
] {
let tree = CppParser::parse(source, ParserConfig::default());
assert_eq!(
tree.get_errors(),
[],
"{source:?} must parse cleanly, got {:?}",
tree.get_errors()
);
assert!(
count_of(source, CppSyntaxKind::InitDeclarator) >= 1,
"{source:?}: the declaration has a declarator"
);
assert_eq!(
count_of(source, CppSyntaxKind::StructuredBinding),
0,
"{source:?}: and the declarator is not a structured binding of nothing"
);
}
assert_eq!(count_of("char const w[2];\n", CppSyntaxKind::ArrayType), 1);
assert_eq!(count_of("char const w[2];\n", CppSyntaxKind::Declarator), 1);
}
fn count_of(source: &str, kind: CppSyntaxKind) -> usize {
CppParser::parse(source, ParserConfig::default())
.get_red_root()
.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == kind)
.count()
}
#[test]
fn an_unnamed_parameter_of_an_unknown_type_is_a_parameter() {
for source in [
"void f(T);\n",
"void f(T) { }\n",
"int f(T);\n",
"template <typename T> void f(T);\n",
"template <typename T> void f(T) { }\n",
"template <typename T> void g(T, T);\n",
"void f(std::vector<T>);\n",
"struct S { void f(T); };\n",
] {
let tree = CppParser::parse(source, ParserConfig::default());
assert_eq!(
tree.get_errors(),
[],
"{source:?} must parse cleanly, got {:?}",
tree.get_errors()
);
assert_eq!(
count_of(source, CppSyntaxKind::ParameterList),
1,
"{source:?}: the parentheses are a parameter list"
);
assert_eq!(
count_of(source, CppSyntaxKind::Initializer),
0,
"{source:?}: the declaration is not a variable initialised from a value"
);
}
assert_eq!(
count_of("void f(T);\n", CppSyntaxKind::Parameter),
1,
"the parameter is read even though it has no name"
);
assert_declaration("Widget w(1, 2);\n");
assert_declaration("Widget w(Inner(1));\n");
assert_declaration("std::string s(other);\n");
assert_declaration("Max(a, b);\n");
assert_eq!(
count_of("int a(b);\n", CppSyntaxKind::ParameterList),
1,
"at file scope"
);
assert_eq!(
count_of("void f() { int a(b); }\n", CppSyntaxKind::ParameterList),
2,
"the same reading inside a body — one for `f`, one for `a`"
);
for source in [
"void Widget::draw(T) { }\n",
"static void Widget::draw(T) { }\n",
"void Widget::draw(int) { }\n",
"void Widget::draw(Canvas&) { }\n",
"void ns::C::method() { }\n",
] {
let tree = CppParser::parse(source, ParserConfig::default());
assert_eq!(
tree.get_errors(),
[],
"{source:?} must parse cleanly, got {:?}",
tree.get_errors()
);
assert_eq!(
count_of(source, CppSyntaxKind::ParameterList),
1,
"{source:?}: the parentheses are a parameter list"
);
}
assert_eq!(
count_of("void Widget::draw(T) { }\n", CppSyntaxKind::Parameter),
1,
"the unnamed parameter is read"
);
}
#[test]
fn a_qualified_declarator_behind_a_storage_specifier_is_still_a_definition() {
for source in [
"static void A::f<int>(int);\n",
"inline void A::f<int>(int);\n",
"extern void A::f<int>(int);\n",
"constexpr void A::f<int>(int) { }\n",
"static void A<int>::f<int>(int);\n",
"static void A::f<int>(int) { }\n",
"void A::f<int>(int) { }\n",
"static void A::f(int);\n",
"static void Widget::draw(T) { }\n",
"static void Widget::draw(Canvas&) { }\n",
] {
let tree = CppParser::parse(source, ParserConfig::default());
assert_eq!(
tree.get_errors(),
[],
"{source:?} must parse cleanly, got {:?}",
tree.get_errors()
);
assert_eq!(
count_of(source, CppSyntaxKind::ParameterList),
1,
"{source:?}: the parentheses are a parameter list"
);
}
assert_eq!(
count_of(
"static void A::f<int>(int);\n",
CppSyntaxKind::TemplateArgumentList
),
1
);
assert_eq!(
count_of(
"static void A::f<int>(int);\n",
CppSyntaxKind::InitDeclarator
),
1
);
}