use cpp_parser::{
CppLexer, CppParser, CppSyntaxKind, CppSyntaxTree, CppTokenKind, Dialect, LexerConfig,
ParserConfig,
};
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum Where {
File,
Body,
Class,
}
fn reads(fragment: &str, place: Where) -> Result<(), String> {
let source = match place {
Where::File => fragment.to_string(),
Where::Body => format!("void probe() {{ {fragment} }}"),
Where::Class => format!("struct Probe {{ {fragment} }};"),
};
let tree = CppParser::parse(&source, ParserConfig::default());
report(&source, &tree)
}
fn condition_is_a_declaration(fragment: &str) -> bool {
let source = format!("void probe() {{ {fragment} }}");
let tree = CppParser::parse(&source, ParserConfig::default());
tree.get_red_root()
.descendants()
.filter(|node| {
matches!(
CppSyntaxKind::from(node.kind()),
CppSyntaxKind::IfStat | CppSyntaxKind::WhileStat | CppSyntaxKind::SwitchStat
)
})
.flat_map(|statement| statement.children())
.filter(|child| CppSyntaxKind::from(child.kind()) == CppSyntaxKind::ParenExpr)
.flat_map(|paren| paren.children())
.any(|held| CppSyntaxKind::from(held.kind()) == CppSyntaxKind::Declaration)
}
#[test]
fn a_condition_may_declare_a_variable() {
assert_reads(
Where::Body,
&[
"if (int n = g()) { }",
"if (const size_t n = g()) { }",
"if (Foo p = get()) { }",
"if (Foo* p = get()) { }",
"if (auto p = get()) { }",
"if (const auto& r = f()) { }",
"while (auto n = g()) { }",
"switch (int n = g()) { }",
"if (v.size()) { }",
"if (i++) { }",
"if (a && b) { }",
"if (f(1, 2)) { }",
"if (int n = g(); n > 0) { }",
],
);
for source in [
"if (int n = g()) { }",
"if (Foo p = get()) { }",
"if (Foo* p = get()) { }",
"while (const auto n = g()) { }",
] {
assert!(
condition_is_a_declaration(source),
"{source} must read its condition as a declaration"
);
}
for source in ["if (v.size()) { }", "if (i++) { }", "if (a && b) { }"] {
assert!(
!condition_is_a_declaration(source),
"{source} declares nothing, and the expression reading is the one it must keep"
);
}
}
#[test]
fn an_allocation_initialiser_is_not_a_parameter_list() {
assert_reads(
Where::Body,
&[
"auto p = new T(a, *q);",
"auto p = new T(a, b);",
"auto p = new T(*q);",
"auto p = new T(::x);",
"auto p = ::new (buf) T(a, *q);",
"auto p = new (buf) T(a, *q, c);",
"auto p = new T[4];",
"auto p = new (int(*)(int))();",
"void g(int, char);",
"auto f = [](int, char) { };",
],
);
assert_statement_kind(&[
forbidding(
shape(
"auto p = new T(a, *q);",
Where::Body,
CppSyntaxKind::Declaration,
),
CppSyntaxKind::FunctionType,
),
forbidding(
shape(
"auto p = new (buf) T(a, *q);",
Where::Body,
CppSyntaxKind::Declaration,
),
CppSyntaxKind::FunctionType,
),
forbidding(
shape(
"auto p = new T(*q);",
Where::Body,
CppSyntaxKind::Declaration,
),
CppSyntaxKind::FunctionType,
),
]);
assert_does_not_read_yet(
Where::Body,
&[(
"auto p = new (Widget)(1);",
"an allocation of a **parenthesised** type used as an initialiser: the note on \
`parse_a_type_here` says this shape is why the placement/type split exists, and it reads \
cleanly as a *statement* (`new (Widget)(1);`) but not here. Found while writing this test",
)],
);
}
#[test]
fn a_split_member_head_may_name_its_type_in_the_else_branch() {
assert_reads(
Where::Class,
&[
"template<typename _Tp>\n#if X\n static void\n#else\n static C\n#endif\n f() { }",
"template<typename _Tp, typename... _Args>\n#if X\n\trequires C<_Tp, _Args...>\n\tstatic constexpr void\n\
#else\n\tstatic __enable_if_t<C<_Tp, _Args...>>\n#endif\n\tconstruct(_Tp* __p)\n\tnoexcept(g())\n\t{ }",
"template<typename _Tp>\n#if X\n static void\n#elif Y\n static C\n#endif\n f() { }",
"template<typename _Tp>\n#if X\n static void\n#else\n static\n#endif\n C f() { }",
"template<typename _Tp>\n#if X\n static void\n#else\n static int\n#endif\n f() { }",
"template<typename _Tp>\n#if X\n static C\n#else\n static D\n#endif\n f() { }",
"template<typename _Tp>\n static C\n f() { }",
"template<typename _Tp>\n static C\n f();",
"template<typename _Tp>\n#if X\n static C\n#else\n static void\n#endif\n f() { }",
"template<typename _Tp>\n#if X\n static void\n#else\n static C\n#endif\n int f() { }",
],
);
let source = "struct Probe { template<typename _Tp>\n#if X\n static void\n#else\n static C\n#endif\n f() { } };";
let tree = CppParser::parse(source, ParserConfig::default());
let declarator = tree
.get_red_root()
.descendants()
.find(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::InitDeclarator)
.expect("the member has a declarator");
assert_eq!(
declarator
.descendants()
.find(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::NameExpr)
.map(|node| node.text().to_string())
.as_deref(),
Some("f"),
"the member's own name is the declarator, not the name written in the `#else` branch"
);
assert_eq!(
direct_members(source),
1,
"one head written in two branches is one member"
);
assert_eq!(
declarator
.parent()
.and_then(|member| member
.children()
.find(|child| CppSyntaxKind::from(child.kind()) == CppSyntaxKind::DeclSpecifierSeq))
.map(|specifiers| specifiers.text().to_string())
.unwrap_or_default()
.matches('#')
.count(),
2,
"both directives belong to the type's own node — the head is one sequence, not two"
);
assert_reads(
Where::Class,
&[
"template<typename _Tp>\n#if X\n static MY_API C\n#else\n static MY_API D\n#endif\n f() { }",
],
);
assert_does_not_read_yet(
Where::Class,
&[(
"template<typename _Tp>\n static const int n\n#if X\n = 1;\n#else\n = 2;\n#endif\n f() { }",
"a per-branch **initialiser**: open, and a different seam from the split head — the `=` after the \
directive is `finish_init_declarator`'s to read",
)],
);
}
#[test]
fn the_gnu_spellings_of_decltype_are_that_keyword() {
assert_reads(
Where::File,
&[
"typedef __typeof__(x) T;",
"typedef __decltype(y) U;",
"typedef __typeof(x) V;",
"typedef decltype(z) W;",
"__typeof__(x) v = 1;",
"auto w = __typeof__(x)();",
],
);
for spelling in ["decltype", "__typeof__", "__typeof", "__decltype"] {
assert_eq!(
CppLexer::new(spelling, LexerConfig::default(), &mut Vec::new())
.tokenize()
.first()
.map(|token| token.kind),
Some(CppTokenKind::DecltypeKeyword),
"`{spelling}` must lex as the keyword it is"
);
}
}
#[test]
fn a_class_head_may_be_written_in_pieces() {
assert_reads(
Where::File,
&[
"class MACRO Name : public Base { };",
"class MACRO Name { };",
"class MACRO Name;",
"struct MACRO Name\n{\n};",
"class MACRO Name\n#ifdef X\n : public Base\n#endif\n{ };",
"typedef struct DECLSPEC_ALIGN (8) Header { int i; } Header;",
"class Name\n#ifdef X\n : public Base\n#endif\n{ int member; };",
"template<typename T>\nclass Name\n#if X\n : public Base<T>\n#endif\n{ T value; };",
"class A final : public B { };",
"class A final { };",
"class MACRO final : public B { };",
],
);
for (source, name) in [
("class MACRO Widget { };", "Widget"),
("class Widget\n#ifdef X\n: public B\n#endif\n{ };", "Widget"),
] {
assert!(
contains_a_class_named(source, name),
"{source} must declare a class called `{name}`"
);
}
}
fn contains_a_class_named(source: &str, name: &str) -> bool {
let tree = CppParser::parse(source, ParserConfig::default());
let root = tree.get_red_root();
root.descendants().any(|node| {
matches!(
CppSyntaxKind::from(node.kind()),
CppSyntaxKind::ClassDef | CppSyntaxKind::StructDef | CppSyntaxKind::UnionDef
) && node
.descendants_with_tokens()
.filter_map(|element| element.into_token())
.any(|token| {
token.kind() == cpp_parser::CppKind::Token(CppTokenKind::Identifier)
&& token.text() == name
})
})
}
#[test]
fn a_functional_conversion_may_have_no_arguments() {
assert_reads(
Where::Body,
&[
"return int();",
"return bool();",
"return double();",
"return typename A::type();",
"return typename A<int>::type();",
"return typename iterator_traits<_Iter>::iterator_category();",
"return int(1);",
"return typename A::type{};",
"return typename A::type(1);",
"return f();",
"return a.b();",
"typename A::type();",
],
);
let kinds = |source: &str| {
let tree = CppParser::parse(
&format!("void probe() {{ {source} }}"),
ParserConfig::default(),
);
let root = tree.get_red_root();
(
root.descendants()
.any(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::CastExpr),
root.descendants()
.any(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::IdentifierExpr),
)
};
assert_eq!(
kinds("return int();"),
(true, false),
"`int()` is a conversion"
);
assert_eq!(
kinds("return typename A::type();"),
(true, false),
"and so is a `typename`-qualified one"
);
assert_eq!(kinds("return f();"), (false, true), "`f()` is a call");
}
#[test]
fn a_template_argument_read_as_an_expression_stops_at_the_comma() {
assert_reads(
Where::File,
&[
"S<3, 4> x;",
"S<3, long> x;",
"S<long, 3> x;",
"S<N - 1, M> x;",
"S<A::b, 2> x;",
"S<f(1), 2> x;",
"integer_sequence<int, 0, 1, 2> s;",
"array<int, 3> a;",
"Grid<T, 3>::fill x;",
"template<typename T> using Not = X<!C<T>, int>;",
"X<!C<T>, bool> f();",
"S<(a, b)> x;",
"S<(a, b), 2> x;",
],
);
let arguments = |source: &str| {
CppParser::parse(source, ParserConfig::default())
.get_red_root()
.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::TemplateArgument)
.count()
};
assert_eq!(arguments("S<3, 4> x;"), 2, "two arguments, not one");
assert_eq!(
arguments("S<3, long> x;"),
2,
"and the same when the second is a type"
);
assert_eq!(
arguments("S<(a, b)> x;"),
1,
"parentheses make a comma an operator again"
);
let tree = CppParser::parse("S<3, 4> x;", ParserConfig::default());
assert!(
tree.get_red_root()
.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::TemplateArgument)
.all(|argument| argument
.descendants()
.any(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::LiteralExpr)),
"each argument holds its own literal"
);
}
#[test]
fn a_conditional_may_decide_a_functions_qualifier() {
assert_reads(
Where::Class,
&[
"void f()\n#if A\n noexcept(true)\n#else\n noexcept(false)\n#endif\n { }",
"void f()\n#if A\n noexcept(true)\n#else\n noexcept(false)\n#endif\n {\n#if A\n g();\n#endif\n }",
"void f()\n#if A\n const\n#else\n const volatile\n#endif\n { }",
"template<typename _Up, typename... _Args>\n void f(_Up* __p, _Args&&... __args)\n#if A\n\
noexcept(noexcept(g(__p)))\n#else\n noexcept(h<_Up>()) \n#endif\n { }",
"auto f()\n#if A\n -> int\n#else\n -> long\n#endif\n { return 0; }",
"#if defined(A)\nvoid g(void)\n#else\nvoid g()\n#endif\n{ }",
"void f()\n#if A\n _GLIBCXX_NOEXCEPT\n#endif\n { }",
"void f() noexcept(true) { }",
"void f() const { }",
],
);
let source = "struct Probe { void f()\n#if A\n noexcept(true)\n#else\n noexcept(false)\n#endif\n { } };";
let tree = CppParser::parse(source, ParserConfig::default());
let body = tree
.get_red_root()
.descendants()
.find(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::CompoundStat)
.expect("the `{ }` is the function's body");
assert_eq!(
body.parent()
.map(|parent| CppSyntaxKind::from(parent.kind())),
Some(CppSyntaxKind::Declaration),
"the body belongs to the declaration rather than being an initialiser"
);
assert_eq!(
direct_members(source),
1,
"the qualifier and the body are one member, not two"
);
assert_eq!(
body.parent().map(|member| member
.children()
.filter(|child| CppSyntaxKind::from(child.kind()) == CppSyntaxKind::DeclSpecifierSeq)
.count()),
Some(1),
"`noexcept(true)` is not a type: the member has one specifier sequence, the one holding `void`"
);
}
#[test]
fn a_braced_conversion_is_a_value_not_a_type() {
assert_reads(
Where::File,
&[
"X<int{}> m;",
"X<size_t{}> m;",
"X<int{}, long{}> m;",
"X<A{1, 2}> m;",
"X<Y<int{}>> m;",
"using A3 = X<int{}>;",
"using A4 = X<int{}, 1>;",
"template<typename T> struct Q<T, int{}> { };",
"template<typename _Alloc> struct __is_allocator<_Alloc, __void_t<typename _Alloc::value_type, \
decltype(std::declval<_Alloc&>().allocate(size_t{}))>> : true_type { };",
"auto a = int{};",
"int f() { return int{}; }",
"void h() { g(int{}); }",
"void h() { bool b{true}; g(b); }",
"void h() { if (a < b) { g(); } }",
"void h() { bool r = a < b > c; }",
"void h() { T x{a < b}; }",
"void h() { auto l = [](int y) { return y < z; }; }",
"struct S : B<C> { };",
"template<typename T> struct Q<T, int> { };",
"X<int> m;",
"X<int, long> m;",
"X<3, 4> m;",
"void h() { int x{1}; }",
],
);
let tree = CppParser::parse("X<int{}> m;", ParserConfig::default());
let root = tree.get_red_root();
assert!(
root.descendants()
.all(|node| CppSyntaxKind::from(node.kind()) != CppSyntaxKind::ExpressionStat),
"a declaration is not an expression statement"
);
let declarator = root
.descendants()
.find(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::InitDeclarator)
.expect("the declaration has a declarator");
assert_eq!(
declarator
.descendants()
.find(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::NameExpr)
.map(|node| node.text().to_string())
.as_deref(),
Some("m"),
"the declarator is the name written after the type"
);
assert_eq!(
root.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::TemplateArgument)
.count(),
1,
"the braces are inside **one** argument of the type"
);
let source = "template<typename T> struct Q<T, int{}> : B { };";
let tree = CppParser::parse(source, ParserConfig::default());
assert!(
contains(source, CppSyntaxKind::ClassBody),
"the head has a body, so the `{{` was not taken for a braced-init-list"
);
assert_eq!(
tree.get_red_root()
.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::TemplateArgument)
.count(),
2,
"and the name's argument list holds both arguments"
);
assert!(
contains("void h() { auto a = int{}; }", CppSyntaxKind::CastExpr),
"`int{{}}` is a functional conversion like `int()`"
);
assert!(
contains("void h() { int(x); }", CppSyntaxKind::Declaration),
"`int(x);` stays the declaration it is — the parenthesis spelling is a declarator's"
);
}
#[test]
fn a_conditional_may_decide_a_requirement() {
assert_reads(
Where::Class,
&[
"static constexpr bool ok = requires (T t) {\n#if X\n t.f();\n#endif\n };",
"static constexpr bool ok = requires (T t) {\n#if X\n t.f();\n#else\n t.g();\n#endif\n };",
"static constexpr bool ok = requires (T t) {\n#if X\n t.f();\n#endif\n#if Y\n t.h();\n#endif\n };",
"static constexpr bool ok = requires (T t) {\n#if X\n typename T::value_type;\n#else\n { t.f() } noexcept -> int;\n#endif\n };",
"static constexpr bool ok = requires (T t) {\n#if X\n t.f();\n#endif\n t.g();\n };",
"template<typename T>\n static constexpr bool ok = requires (T t) {\n#if X\n t.f();\n#else\n t.g();\n#endif\n };",
"static constexpr bool ok = requires (T t) { t.f(); };",
"static constexpr bool ok = requires (T t) { typename T::value_type; { t.f() } noexcept -> int; };",
],
);
assert!(
reads(
"struct Probe {\n#if X\n static constexpr bool ok = requires (T t) { t.f(); };\n#else\n \
static constexpr bool ok = requires (T t) { t.g(); };\n#endif\n};",
Where::File,
)
.is_ok(),
"a member written once per branch reads, with each directive on a line of its own"
);
let source = "struct Probe { static constexpr bool ok = requires (T t) {\n#if X\n t.f();\n#else\n t.g();\n#endif\n }; };";
let tree = CppParser::parse(source, ParserConfig::default());
let requires = tree
.get_red_root()
.descendants()
.find(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::RequiresExpr)
.expect("the member holds a requires-expression");
let inside = |kind| {
requires
.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == kind)
.count()
};
assert_eq!(
inside(CppSyntaxKind::Requirement),
2,
"one requirement per branch"
);
assert_eq!(
inside(CppSyntaxKind::PreprocessorDirective),
3,
"the `#if`, `#else` and `#endif` are the expression's own children"
);
assert_eq!(
direct_members(
"struct Probe { static constexpr bool ok = requires (T t) {\n#if X\n t.f();\n#else\n t.g();\n#endif\n };\n int after;\n};"
),
2,
"the member with the conditional in it is a member, and so is the one after it"
);
}
#[test]
fn a_failed_member_with_an_unbalanced_brace_keeps_the_members_after_it_members() {
for (source, note) in [
(
"struct Probe {\n static constexpr bool ok = requires (T t) {\n#if X\n t.f()\n#endif\n ;\n };\n int after;\n};",
"a requires-expression whose requirement is split by a directive",
),
(
"struct Probe {\n void f() {\n#if X\n g()\n#endif\n ;\n };\n int after;\n};",
"a function body whose statement is split by a directive",
),
(
"struct Probe {\n int x = 1\n int after;\n};",
"a member with no `;` at all — the case the sibling test already pins, kept here so the two halves \
are checked by one loop",
),
] {
assert!(
has_a_member_containing(source, "after"),
"the member after a broken one is still a member: {note}\n{source}"
);
}
}
#[test]
fn a_typedef_may_carry_an_attribute() {
assert_reads(
Where::File,
&[
"typedef int v4 __attribute__ ((__vector_size__ (16)));",
"typedef int v4 [[deprecated]];",
"typedef int v4 __attribute__ ((__vector_size__ (8), __may_alias__));",
"typedef int __m64 __attribute__ ((__vector_size__ (8), __may_alias__));",
"typedef int __v4si_u __attribute__ ((__vector_size__ (16),\t\\\n\t\t\t\t __may_alias__, __aligned__ (1)));",
"typedef int __attribute__((aligned(8))) v4;",
"typedef WCHAR *PWCHAR, *LPWCH;",
"typedef int Integer;",
"int x [[maybe_unused]] = 1;",
"void f(void) __attribute__((noreturn));",
],
);
let source = "typedef int v4 [[deprecated]];";
let tree = CppParser::parse(source, ParserConfig::default());
let typedef = tree
.get_red_root()
.descendants()
.find(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::TypedefDecl)
.expect("the declaration is a typedef");
assert_eq!(
typedef
.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::AttributeList)
.count(),
1,
"the attribute belongs to the typedef rather than being rubble"
);
assert!(
contains(
"typedef int v4 [[deprecated]]; v4 x;",
CppSyntaxKind::Declaration
),
"and the name it declares is a type name afterwards"
);
}
#[test]
fn a_conditional_may_decide_an_attribute() {
assert_reads(
Where::File,
&[
"template<typename T>\n#if X\n[[attr]]\n#endif\ninline void f() { }",
"template<typename T>\n[[attr]]\n#if X\n#endif\ninline void f() { }",
"template<typename _Ex>\n# if ! __cpp_rtti\n [[__gnu__::__always_inline__]]\n#endif\n \
inline void\n rethrow_if_nested(const _Ex& __ex)\n { }",
"template<typename T>\n[[a]]\n#if X\n[[b]]\n#endif\nvoid f() { }",
"template<typename T>\n[[nodiscard]] T p();",
"template<typename T>\n#if X\n#endif\ninline void f() { }",
"[[nodiscard]] int x;",
"int x [[maybe_unused]];",
],
);
let source = "template<typename _Ex>\n# if ! __cpp_rtti\n [[__gnu__::__always_inline__]]\n#endif\n \
inline void\n rethrow_if_nested(const _Ex& __ex)\n { }";
let tree = CppParser::parse(source, ParserConfig::default());
let declaration = tree
.get_red_root()
.descendants()
.find(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::Declaration)
.expect("the head and the declaration are one declaration");
assert_eq!(
declaration
.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::AttributeList)
.count(),
1,
"the attribute is inside the declaration it belongs to"
);
assert!(
declaration
.descendants()
.any(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::TemplateDecl),
"and so is the template head"
);
}
#[test]
fn a_type_may_be_spelled_by_the_compiler() {
assert_reads(
Where::File,
&[
"unsigned __int128 x;",
"__int128 x;",
"void f() { auto r = (unsigned __int128) 1; }",
"using T = unsigned __int128;",
"typedef unsigned __int128 u128;",
"struct S { unsigned __int128 big; };",
"void f(unsigned __int128 v);",
"void f() { _Float16 h = 1; }",
"void f() { __bf16 b = 1; }",
"void f() { __float128 q = 1; }",
"void f(int *__restrict p);",
"__extension__ inline int g();",
],
);
let tree = CppParser::parse("unsigned __int128 x;", ParserConfig::default());
let root = tree.get_red_root();
let builtin = root
.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::BuiltinType)
.map(|node| node.text().to_string())
.collect::<Vec<_>>();
assert_eq!(
builtin,
vec!["unsigned".to_string(), "__int128".to_string()],
"both words are type specifiers"
);
assert!(
root.descendants()
.all(|node| CppSyntaxKind::from(node.kind()) != CppSyntaxKind::MacroCall),
"and the declarator is not read as a macro suffix"
);
assert_eq!(
root.descendants()
.find(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::InitDeclarator)
.and_then(|declarator| declarator
.descendants()
.find(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::NameExpr))
.map(|name| name.text().to_string())
.as_deref(),
Some("x"),
"the declared name is the one written last"
);
let msvc = |source: &str| {
CppParser::parse(source, ParserConfig::default().with_dialect(Dialect::Msvc))
};
assert!(
msvc("__int64 big;").get_errors().is_empty(),
"`__int64` is a type to MSVC"
);
assert!(
msvc("typedef int __int128 __attribute__ ((__mode__ (TI)));")
.get_errors()
.is_empty(),
"and `__int128` is a name there, which is what MinGW's typedef branch needs"
);
assert!(
!msvc("struct S { __int64 big; };").get_errors().is_empty()
|| msvc("struct S { __int64 big; };")
.get_red_root()
.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::BuiltinType)
.any(|node| node.text().to_string().trim() == "__int64"),
"`__int64` is read as a type to MSVC, not as a variable's name"
);
assert!(
CppParser::parse("typedef int __int64;", ParserConfig::default())
.get_errors()
.is_empty(),
"under GNU `__int64` is an ordinary name, so a typedef of it declares one"
);
}
#[test]
fn a_body_settles_whether_the_group_was_a_parameter_list() {
assert_reads(
Where::File,
&[
"T f(U) { return X(); }",
"inline T f(U) { return X(); }",
"T f(U, V) { return X(); }",
"T f(U) noexcept { return X(); }",
"auto f(U) -> T { return X(); }",
"MACRO\ninline T\nf(U)\n{ return X(); }",
"_GLIBCXX20_CONSTEXPR\ninline _Iter_less_val\n__iter_comp_val(_Iter_less_iter)\n\
{ return _Iter_less_val(); }",
"template<typename _Ex>\n __attribute__ ((__always_inline__))\n inline exception_ptr\n \
make_exception_ptr(_Ex) _GLIBCXX_USE_NOEXCEPT\n { return exception_ptr(); }",
"T x(y);",
"Max(a, b);",
"T f(U);",
"void g() { T x(y); }",
"void g() { T x(y); { h(); } }",
"void g() { h(x); }",
"struct S { T f(U) { return X(); } };",
],
);
let parts = |source: &str| {
let tree = CppParser::parse(source, ParserConfig::default());
let root = tree.get_red_root();
let has = |kind| {
root.descendants()
.any(|node| CppSyntaxKind::from(node.kind()) == kind)
};
(
has(CppSyntaxKind::ParameterList),
has(CppSyntaxKind::Initializer),
has(CppSyntaxKind::CompoundStat),
root.descendants()
.find(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::InitDeclarator)
.and_then(|declarator| {
declarator
.descendants()
.find(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::NameExpr)
})
.map(|name| name.text().to_string())
.unwrap_or_default(),
)
};
assert_eq!(
parts("T f(U) { return X(); }"),
(true, false, true, "f".to_string()),
"a group followed by a body is a parameter list, and the declarator is the function's name"
);
assert_eq!(
parts("T x(y);"),
(false, true, false, "x".to_string()),
"and a group that ends the declaration is an initializer"
);
}
#[test]
fn a_cast_of_a_cast_is_still_a_cast() {
assert_reads(
Where::Body,
&[
"(T)(U) 1;",
"(T)(U) g();",
"(T)(U)(V) 1;",
"auto r = (T)(U) 1;",
"auto r = (T)(U)(V) g ();",
"h((T)(U) g(), 1);",
"return (A) __builtin ((B) x, (C)(D)\n g (), (E) -1);",
"(int)(char) 1;",
"(int)(char)(long) 1;",
"(f)(a);",
"(f)(a)(b);",
"(f)(a) + 1;",
"(a)[b];",
"(a) - b;",
"(a) *b;",
],
);
let counts = |source: &str| {
let tree = CppParser::parse(
&format!("void probe() {{ {source} }}"),
ParserConfig::default(),
);
let root = tree.get_red_root();
let count = |kind| {
root.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == kind)
.count()
};
(
count(CppSyntaxKind::CastExpr),
count(CppSyntaxKind::CallExpr),
)
};
assert_eq!(counts("(T)(U) 1;"), (2, 0), "two casts, no call");
assert_eq!(counts("(T)(U)(V) 1;"), (3, 0), "…and the run can be longer");
assert_eq!(
counts("(f)(a);"),
(0, 1),
"a call through parentheses stays a call"
);
assert_eq!(
counts("(f)(a)(b);"),
(0, 2),
"and a chain of calls stays a chain"
);
assert_eq!(
counts("(f)(a) + 1;"),
(0, 1),
"`+` is ambiguous, so the call reading is the one kept"
);
}
#[test]
fn a_conditional_may_decide_a_template_head() {
assert_reads(
Where::File,
&[
"template<int N =\n#if X\n 1\n#endif\n > void f();",
"template<int N =\n#if X\n 1\n#else\n 2\n#endif\n > void f();",
"template<typename T, bool B =\n#if X\n A<T>::value\n#else\n B<T>::value\n#endif\n > struct C { };",
"template<typename T,\n#if X\n typename U\n#else\n typename U\n#endif\n > void f();",
"X<\n#if A\n 1\n#else\n 2\n#endif\n > x;",
"void S::\n#if X\nf()\n#else\ng()\n#endif\n{ }",
"template<typename T>\ntypename V<T>::iterator\nV<T>::\n#if X\ninsert(int x)\n#else\ninsert(long x)\n#endif\n{ }",
"template<typename T, int N = 3, typename... R> void f();",
"template<typename T = int> struct X { };",
"std::map<K, std::less<>> m;",
"X<1, 2> x;",
],
);
let argument_list_holds = |source: &str| {
let tree = CppParser::parse(source, ParserConfig::default());
tree.get_red_root()
.descendants()
.find(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::TemplateArgumentList)
.map(|list| {
(
list.descendants()
.filter(|node| {
CppSyntaxKind::from(node.kind()) == CppSyntaxKind::PreprocessorDirective
})
.count(),
list.descendants()
.filter(|node| {
CppSyntaxKind::from(node.kind()) == CppSyntaxKind::TemplateArgument
})
.count(),
)
})
.unwrap_or_default()
};
assert_eq!(
argument_list_holds("X<\n#if A\n 1\n#else\n 2\n#endif\n > x;"),
(3, 2),
"the three directives are the argument list's, and **each branch contributes its own argument** — the \
list is what the conditional spells twice"
);
let source = "template<typename I>\n using K = remove_const_t<\n#if X\n tuple_element_t<0, T>>;\n#else\n \
typename I::first_type>;\n#endif";
assert_reads(Where::File, &[source]);
assert_eq!(
argument_list_holds(source),
(2, 4),
"one argument list: the `#if` and the `#else` are its own, and each branch contributes its own arguments \
(`tuple_element_t<0, T>` with its two nested ones, and the second branch's one). The `#endif` stands after \
the `;` that ends the declaration, so it is the enclosing declaration's directive, not the list's"
);
}
#[test]
fn a_deduction_guide_is_a_declarator_with_a_trailing_return_type() {
assert_reads(
Where::File,
&[
"template<typename I> M(I) -> M<I>;",
"template<typename I> M(I, I) -> M<int>;",
"template<typename I>\n M(I, I)\n -> M<K<I>, V<I>>;",
"template<typename I>\n multimap(I, I)\n -> multimap<K<I>, V<I>,\n less<K<I>>, A>;",
"template<typename _InputIterator, typename _Allocator, typename = _RequireInputIter<_InputIterator>>\n\
\x20 multimap(_InputIterator, _InputIterator, _Allocator)\n\
\x20 -> multimap<__iter_key_t<_InputIterator>, __iter_val_t<_InputIterator>,\n\
\x20 less<__iter_key_t<_InputIterator>>, _Allocator>;",
"template<typename _It> basic_string_view(_It, _It) -> basic_string_view<iter_value_t<_It>>;",
"auto f(int) -> int;",
"struct S { auto f() -> int { return 1; } };",
"void f() { auto r = (a)->b; }",
"void f() { auto l = []() -> int { return 1; }; }",
"void f() { T x(y); }",
],
);
let parts = |source: &str| {
let tree = CppParser::parse(source, ParserConfig::default());
let root = tree.get_red_root();
let count = |kind| {
root.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == kind)
.count()
};
(
count(CppSyntaxKind::ParameterList),
count(CppSyntaxKind::TrailingReturnType),
count(CppSyntaxKind::Initializer),
)
};
assert_eq!(
parts("template<typename I> M(I) -> M<I>;"),
(1, 1, 0),
"a guide is a function declarator: one parameter list, one trailing return type, no initializer"
);
assert_eq!(
parts("void f() { auto r = (a)->b; }"),
(1, 0, 1),
"and `(a)->b` is an initializer — the outer function's parameter list, and no trailing return type"
);
}
#[test]
fn an_asm_statement_keeps_its_payload_as_tokens() {
assert_reads(
Where::Body,
&[
"__asm__ volatile (\"tilerelease\" ::);",
"__asm__ __volatile__(\"int {$}3\":);",
"__asm__ __volatile__ (\"pconfig\\n\\t\" : \"=a\" (retval) : \"a\" (leaf) : \"cc\");",
"__asm__ (\"nop\");",
"asm(\"nop\");",
"asm volatile (\"dmb\" ::: \"memory\");",
"asm goto (\"jmp %l0\" :::: label);",
"__asm__ __volatile__ (\"pconfig\\n\\t\"\t\\\n\t: \"=a\" (retval)\t\t\t\\\n\t: \"a\" (leaf), \"b\" (b)\t\t\\\n\
\t: \"cc\");",
"__asm { mov eax, 1 }",
"g(1, 2);",
"asmbl(1);",
],
);
let tree = CppParser::parse(
"void f() { __asm__ volatile (\"pconfig\\n\\t\" : \"=a\" (retval) : \"a\" (leaf) : \"cc\"); }",
ParserConfig::default(),
);
let root = tree.get_red_root();
let asm = root
.descendants()
.find(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::AsmStat)
.expect("the statement is an asm statement");
let text = asm.text().to_string();
assert!(
text.starts_with("__asm__ volatile (") && text.trim_end().ends_with(");"),
"the node spans the whole statement: {text:?}"
);
assert!(
text.contains("\"=a\" (retval)") && text.contains("\"cc\""),
"and every operand is still in it, as text: {text:?}"
);
assert!(
!root
.descendants()
.any(|node| { CppSyntaxKind::from(node.kind()) == CppSyntaxKind::ErrorNode }),
"nothing in an asm statement is rubble"
);
let tree = CppParser::parse(
"#define asm(x) g(x)\nvoid f() { asm(1); }",
ParserConfig::default(),
);
assert!(
tree.get_red_root()
.descendants()
.any(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::AsmStat),
"the parser sees `asm`, not the macro that would have replaced it"
);
}
#[test]
fn a_group_holding_a_call_is_an_expression_not_a_cast() {
assert_reads(
Where::Body,
&[
"auto r = (T(2) * c);",
"auto r = (a - b) / (T(2) * c);",
"z = (a - b) / (T(2) * c);",
"auto r = ((T(1) << (n / 2)) - 1);",
"auto r = (T(2 * j - 1) * x * y);",
"auto r = (x + T(1)) / (T(2) * y);",
"auto r = (T(2));",
"template<typename T>\n void f() { T x = (a - b) / (T(2) * c); }",
"template<typename _Tp>\n _Tp g(_Tp a, _Tp b, _Tp c)\n {\n _Tp x;\n x = (a - b) / (_Tp(2) * c);\n \
return x;\n }",
"auto r = (T)x;",
"auto r = (int)x;",
"auto r = (T(*)(int))x;",
"auto r = (const T&)x;",
"auto r = (a + b);",
],
);
let kinds = |source: &str| {
let tree = CppParser::parse(
&format!("void probe() {{ {source} }}"),
ParserConfig::default(),
);
let root = tree.get_red_root();
let found = |kind| {
root.descendants()
.any(|node| CppSyntaxKind::from(node.kind()) == kind)
};
(
found(CppSyntaxKind::CastExpr),
found(CppSyntaxKind::CallExpr),
)
};
assert_eq!(kinds("auto r = (T)x;"), (true, false), "`(T)x` is a cast");
assert_eq!(
kinds("auto r = (T(*)(int))x;"),
(true, false),
"…and so is one whose type has an abstract declarator"
);
assert_eq!(
kinds("auto r = (T(2) * c);"),
(false, true),
"`(T(2) * c)` is a product, and `T(2)` inside it is a call"
);
assert_eq!(
kinds("auto r = (T(2));"),
(false, true),
"…and a lone `(T(2))` is the same call, not a cast of `2` to `T`"
);
assert!(
CppParser::parse(
"void probe() { auto r = (T(2) * c); }",
ParserConfig::default()
)
.get_errors()
.is_empty(),
"the cast attempt reported `expected ), but get (` — the rewind must take that back"
);
}
#[test]
fn a_directive_may_decide_a_parameter_or_stand_between_two() {
assert_reads(
Where::File,
&[
"void f(int a,\n#if X\n int b,\n#endif\n int c);",
"void f(\n#if X\n int a,\n#endif\n int b);",
"void f(int a,\n#if X\n int b\n#else\n long b\n#endif\n );",
"void f(int a,\n#if X\n int b\n#elif Y\n long b\n#else\n short b\n#endif\n );",
"void f(int a,\n#if X\n int b\n#else\n long b\n#endif\n ) { }",
],
);
let parameters = |source: &str| {
let tree = CppParser::parse(source, ParserConfig::default());
let root = tree.get_red_root();
let lists: Vec<_> = root
.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::ParameterList)
.collect();
assert_eq!(lists.len(), 1, "one parameter list, not one per branch");
lists[0]
.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::Parameter)
.count()
};
assert_eq!(
parameters("void f(int a,\n#if X\n int b,\n#endif\n int c);"),
3,
"the parameter between two directives is a parameter"
);
assert_eq!(
parameters("void f(int a,\n#if X\n int b\n#else\n long b\n#endif\n );"),
3,
"two parameters written with an alternation are `int a`, `int b` and `long b`: the parser has no table \
that says the branches are exclusive, so each **spelling** is a parameter and keeping both is the honest \
tree. What would be wrong is a second list, or the `long b` disappearing."
);
assert_reads(
Where::File,
&[
"void random_shuffle(_RAIter, _RAIter,\n#if X\n _RandomNumberGenerator&&);\n#else\n \
_RandomNumberGenerator&);\n#endif",
],
);
assert_eq!(
parameters(
"void random_shuffle(_RAIter, _RAIter,\n#if X\n _RandomNumberGenerator&&);\n#else\n \
_RandomNumberGenerator&);\n#endif"
),
4,
"one list, and both branches' spellings of the last parameter in it"
);
assert_reads(
Where::File,
&["#if X\nvoid f(int a);\n#else\nvoid g(long a);\n#endif"],
);
let source = "#if X\nvoid f(int a);\n#else\nvoid g(long a);\n#endif";
assert_eq!(
count_of(source, CppSyntaxKind::ParameterList),
2,
"each branch writes a whole declaration, so each has its own list"
);
assert_eq!(
count_of(source, CppSyntaxKind::Parameter),
2,
"and one parameter in each — the `;` before the `#else` was the declaration's, not the list's"
);
}
#[test]
fn a_macros_arguments_that_are_not_expressions_stay_tokens() {
assert_reads(
Where::Body,
&[
"_MM_REDUCE_OPERATOR_BASIC_EPI16 (+);",
"if constexpr (__is_same(const volatile _Tp, const volatile void)) { }",
"return __reference_constructs_from_temporary(_Elements, _Up&&);",
"_GLIBCXX_TYPEID(typename std::iterator_traits<_Iterator>::value_type);",
"if (TlsSetValue (__key, CONST_CAST2(void *, const void *, __ptr))) { }",
"auto n = __glibcxx_min(char);",
"auto v = __builtin_shuffle ((__attribute__((__vector_size__ (16))) int) __A, __B);",
],
);
assert_reads(
Where::File,
&["SHSTDAPI_(WINBOOL) InitNetworkAddressControl (void);"],
);
let arguments_are_tokens = |statement: &str| {
let source = format!("void probe() {{ {statement} }}");
let tree = CppParser::parse(&source, ParserConfig::default());
assert!(
tree.get_errors().is_empty(),
"`{statement}` is reported: {:?}",
tree.get_errors()
.iter()
.map(|e| &e.message)
.collect::<Vec<_>>()
);
tree.get_red_root().descendants().any(|node| {
matches!(CppSyntaxKind::from(node.kind()), CppSyntaxKind::CallExpr)
&& node
.children()
.any(|child| CppSyntaxKind::from(child.kind()) == CppSyntaxKind::ArgumentList)
})
};
assert!(
arguments_are_tokens("_MM_REDUCE_OPERATOR_BASIC_EPI16 (+);"),
"an argument that is a bare operator is kept as a token group"
);
assert!(
!arguments_are_tokens("g(1, 2);"),
"an ordinary call's arguments are expressions, and an `ArgumentList` node here would say they are not"
);
assert!(
!arguments_are_tokens("g(h(x), {1, 2});"),
"…including the braced-init-list the call arm already reads"
);
assert!(
arguments_are_tokens("g(1 +);"),
"a broken call is read as a macro's arguments — the documented cost of the fallback"
);
}
#[test]
fn a_using_enum_declaration_and_the_register_specifier_read() {
assert_reads(
Where::File,
&[
"enum class E { a };\nusing enum E;",
"namespace ns { enum class E { a }; }\nusing enum ns::E;",
"enum class E { a };\ntemplate<class T> void f() { using enum E; }",
],
);
assert_reads(Where::Body, &["using enum E;", "using enum ns::E;"]);
assert_reads(Where::Class, &["using enum E;"]);
assert_reads(
Where::Body,
&[
"register int x = 0; (void)x;",
"register const char *p = nullptr; (void)p;",
"for (register int i = 0; i < 3; ++i) { }",
],
);
assert_reads(
Where::File,
&[
"register int counter;",
"register unsigned int r0 __asm__(\"r0\") = code;",
],
);
let specifier = |source: &str, kind: CppSyntaxKind| {
let tree = CppParser::parse(source, ParserConfig::default());
assert!(
tree.get_errors().is_empty(),
"`{source}` is reported: {:?}",
tree.get_errors()
.iter()
.map(|e| &e.message)
.collect::<Vec<_>>()
);
tree.get_red_root()
.descendants()
.any(|node| CppSyntaxKind::from(node.kind()) == kind)
};
assert!(
specifier(
"void probe() { register int x = 0; }",
CppSyntaxKind::RegisterSpec
),
"`register` is a storage-class specifier, so it gets a specifier node"
);
assert!(
specifier(
"enum class E { a };\nusing enum E;",
CppSyntaxKind::UsingDecl
),
"`using enum E;` is a using-declaration"
);
assert!(
!specifier(
"enum class E { a };\nusing enum E;",
CppSyntaxKind::MissingNode
),
"and nothing in it is missing"
);
let label = CppParser::parse(
"register unsigned int r0 __asm__(\"r0\") = code;",
ParserConfig::default(),
);
assert!(
label.get_errors().is_empty(),
"the asm label on a declarator is not an error: {:?}",
label
.get_errors()
.iter()
.map(|e| &e.message)
.collect::<Vec<_>>()
);
assert!(
label
.get_red_root()
.descendants()
.any(|node| { CppSyntaxKind::from(node.kind()) == CppSyntaxKind::MacroCall }),
"…and its tokens are kept, under the macro reading, with nothing interpreted"
);
}
#[test]
fn a_macro_may_stand_between_the_type_and_the_declarator() {
assert_reads(
Where::File,
&[
"unsigned __int64 x;",
"typedef unsigned __int64 POINTER_64_INT;",
"signed __int64 y;",
"void f(void HUGEP **ppvData);",
"WINOLEAUTAPI SafeArrayAccessData(SAFEARRAY *psa, void HUGEP **ppvData);",
"unsigned __int64 f(unsigned __int64 a);",
"WINOLEAPI_(void) CoFreeLibrary (HINSTANCE hInst);",
"WINOLEAPI_ (void) OleUninitialize (void);",
"MY_API(x) int g(void);",
"MY_API Widget *p;",
"MY_API Widget const w;",
"__attribute__((__nonnull__)) void f(const bool __insert_left);",
"__attribute__((__nonnull__,__returns_nonnull__))\n _Rb_tree_node_base*\n _Rb_tree_rebalance_for_erase(_Rb_tree_node_base* const __z,\n _Rb_tree_node_base& __header) throw ();",
"__declspec(align(8)) int aligned;",
"_GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR bool empty() const;",
"int x;",
"T x;",
"int a, b;",
],
);
assert_reads(
Where::Body,
&[
"unsigned __int64 n = 0; (void)n;",
"void *HUGEP p = nullptr; (void)p;",
],
);
let shape = |source: &str| {
let tree = CppParser::parse(source, ParserConfig::default());
assert!(
tree.get_errors().is_empty(),
"`{source}` is reported: {:?}",
tree.get_errors()
.iter()
.map(|e| &e.message)
.collect::<Vec<_>>()
);
let root = tree.get_red_root();
let specifiers = root
.descendants()
.find(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::DeclSpecifierSeq)
.map(|sequence| sequence.text().to_string().trim().to_string())
.unwrap_or_default();
let macros = root
.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::MacroCall)
.count();
let declarator = root
.descendants()
.find(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::Declarator)
.map(|node| node.text().to_string().trim().to_string())
.unwrap_or_default();
(specifiers, macros, declarator)
};
assert_eq!(
shape("unsigned __int64 x;"),
("unsigned __int64".to_string(), 0, "x".to_string()),
"the macro is a word of the *type*, and `x` is the declarator — the reading that was silently wrong"
);
assert_eq!(
shape("int x MY_DECL_SUFFIX;"),
("int".to_string(), 1, "x".to_string()),
"…and here the macro is a suffix: the declarator is `x` and the macro is its own node"
);
assert_eq!(
shape("MY_API Widget *p;"),
("MY_API Widget".to_string(), 0, "*p".to_string()),
"the shape the rule was written for is unchanged: the macro is in the type"
);
let declared = |source: &str| {
let tree = CppParser::parse(source, ParserConfig::default());
assert!(
tree.get_errors().is_empty(),
"`{source}` is reported: {:?}",
tree.get_errors()
.iter()
.map(|e| &e.message)
.collect::<Vec<_>>()
);
tree.get_red_root()
.descendants()
.find(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::InitDeclarator)
.map(|node| node.text().to_string().trim().to_string())
.unwrap_or_default()
};
assert_eq!(
declared("WINOLEAPI_(void) CoFreeLibrary (HINSTANCE hInst);"),
"CoFreeLibrary (HINSTANCE hInst)".to_string(),
"the declarator after the macro is the declaration's declarator"
);
assert_eq!(
declared("WINOLEAPI_ (void) OleUninitialize (void);"),
"OleUninitialize (void)".to_string(),
"…including the spelling with a space before the macro's parenthesis"
);
}
#[test]
fn a_macro_shaped_name_inside_a_type_id_and_a_parenthesised_declarator() {
assert_reads(
Where::Body,
&[
"auto v = ((unsigned __LONG32) h);",
"auto v = ((void *) (LONG_PTR) (__LONG32) h);",
"auto n = sizeof(unsigned __LONG32);",
"auto p = (unsigned __int64 *) h;",
],
);
assert_reads(
Where::File,
&[
"typedef DWORD (WINAPI PM_OPEN_PROC)(LPWSTR);",
"typedef DWORD (WINAPI PM_COLLECT_PROC)(LPWSTR,LPVOID *,LPDWORD,LPDWORD);",
"typedef DWORD (WINAPI PM_CLOSE_PROC)(void);",
"static int HandleToULong (const void *h) { return ((unsigned __LONG32) h); }",
"void C::f(_Predicate __pred) { }",
"void C::g(_Predicate __pred);",
"void f(int (_Predicate __pred));",
"typedef void (*fp)(int);",
"int x MY_DECL_SUFFIX;",
],
);
let parameter_list = |source: &str| {
let tree = CppParser::parse(source, ParserConfig::default());
assert!(
tree.get_errors().is_empty(),
"`{source}` is reported: {:?}",
tree.get_errors()
.iter()
.map(|e| &e.message)
.collect::<Vec<_>>()
);
let root = tree.get_red_root();
let parameters = root
.descendants()
.find(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::ParameterList)
.map(|list| {
list.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::Parameter)
.map(|parameter| parameter.text().to_string().trim().to_string())
.collect::<Vec<_>>()
})
.unwrap_or_default();
let macros = root
.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::MacroCall)
.count();
(parameters, macros)
};
assert_eq!(
parameter_list("void C::f(_Predicate __pred) { }"),
(vec!["_Predicate __pred".to_string()], 0),
"a parameter list with a macro-shaped type and a name is a parameter list"
);
assert_eq!(
parameter_list("void C::g(_Predicate __pred);"),
(vec!["_Predicate __pred".to_string()], 0),
"…and the same in a declaration"
);
let declared = |source: &str| {
let tree = CppParser::parse(source, ParserConfig::default());
assert!(
tree.get_errors().is_empty(),
"`{source}` is reported: {:?}",
tree.get_errors()
.iter()
.map(|e| &e.message)
.collect::<Vec<_>>()
);
let root = tree.get_red_root();
let macros = root
.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::MacroCall)
.map(|node| node.text().to_string().trim().to_string())
.collect::<Vec<_>>();
let declarator = root
.descendants()
.find(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::Declarator)
.map(|node| node.text().to_string().trim().to_string())
.unwrap_or_default();
(macros, declarator)
};
assert_eq!(
declared("typedef DWORD (WINAPI PM_OPEN_PROC)(LPWSTR);"),
(
vec!["WINAPI".to_string()],
"(WINAPI PM_OPEN_PROC)(LPWSTR)".to_string()
),
"the calling convention is a macro, the name after it is the declarator's, and the parameter list binds \
to that same declarator — which is what makes this a function-pointer typedef"
);
}
#[test]
fn a_name_after_an_anonymous_class_definition_is_the_declarator() {
assert_reads(
Where::Body,
&[
"union { int a; } u;",
"union { int a; } u = { 1 };",
"struct { T a; } x = { 1 };",
"struct { __m128h a[2]; __m256h v; } __u = { .__v = __A };",
"union { __m128h __a[2]; __m256h __v; } __u;",
"struct { int a; } *p = nullptr;",
],
);
assert_reads(
Where::File,
&[
"typedef struct { int a; } Alias;",
"enum E { A } e;",
"struct S { int a; } x;",
"struct { int a; } arr[] = { { 1 }, { 2 } };",
"static union { int a; float b; } value = { .b = 1.0f };",
],
);
let declarator = |source: &str| {
let tree = CppParser::parse(source, ParserConfig::default());
assert!(
tree.get_errors().is_empty(),
"`{source}` is reported: {:?}",
tree.get_errors()
.iter()
.map(|e| &e.message)
.collect::<Vec<_>>()
);
let root = tree.get_red_root();
let all: Vec<_> = root
.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::InitDeclarator)
.map(|node| node.text().to_string().trim().to_string())
.collect();
all.last().cloned().unwrap_or_default()
};
assert_eq!(
declarator("void probe() { union { int a; } u = { 1 }; }"),
"u = { 1 }".to_string(),
"the initializer's declarator is `u`"
);
assert_eq!(
declarator("void probe() { union { int a; } u; }"),
"u".to_string(),
"…and with nothing after it, `u` is still the declarator — the silence was the defect"
);
let tree = CppParser::parse(
"void probe() { union { int a; } u = { 1 }; }",
ParserConfig::default(),
);
let root = tree.get_red_root();
let specifiers = root
.descendants()
.find(|node| {
CppSyntaxKind::from(node.kind()) == CppSyntaxKind::DeclSpecifierSeq
&& node
.children()
.any(|child| CppSyntaxKind::from(child.kind()) == CppSyntaxKind::UnionDef)
})
.map(|node| node.text().to_string().trim().to_string())
.unwrap_or_default();
assert_eq!(
specifiers, "union { int a; }",
"the type is the definition, and `u` is not a word of it"
);
}
#[test]
fn a_macro_from_a_header_can_be_a_statement_of_its_own() {
assert_reads(
Where::Body,
&[
"__glibcxx_function_requires(_Concept<T>) if (a < b) return;",
"#if X\n __glibcxx_function_requires(_Concept<T>)\n#endif\n g();",
"__glibcxx_function_requires(_ConvertibleConcept<A, B>) __glibcxx_function_requires(_ConvertibleConcept<B, A>) typedef int T1;",
"__glibcxx_function_requires(_Concept<T>) return;",
],
);
assert_reads(
Where::File,
&[
"class B { } _GLIBCXX11_DEPRECATED_SUGGEST(\"std::bind\");",
"__glibcxx_function_requires(_Concept<T>) void g();",
],
);
let reported = |source: &str| {
let tree = CppParser::parse(source, ParserConfig::default());
!tree.get_errors().is_empty()
};
assert!(
reported("void f() { FOO(x) }"),
"`FOO(x)` is not reserved: a call with its `;` missing is an error"
);
assert!(
reported("void f() { g(x)\n return; }"),
"…and a lowercase name is not a macro either"
);
assert!(
reported("void f() { g(x) { } }"),
"a block after a call is the mistake the block form of this rule weighs"
);
let tree = CppParser::parse(
"WINOLEAPI_(void) CoFreeLibrary (HINSTANCE hInst);",
ParserConfig::default(),
);
assert!(
tree.get_errors().is_empty(),
"the macro-headed declaration still reads"
);
assert!(
tree.get_red_root()
.descendants()
.any(|node| { CppSyntaxKind::from(node.kind()) == CppSyntaxKind::Declaration })
&& !tree
.get_red_root()
.children()
.any(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::MacroCall),
"it is one declaration whose specifiers hold the macro — not a macro statement and a second declaration"
);
}
#[test]
fn a_directive_may_stand_between_enumerators() {
assert_reads(
Where::File,
&[
"enum E {\n a = 1,\n#ifdef X\n b = 2,\n#endif\n c = 3\n};",
"enum E {\n a,\n#if X\n b,\n#else\n c,\n#endif\n d\n};",
"enum class E : int {\n a = 1,\n#ifdef X\n b = 2\n#endif\n};",
"enum E { a = 1, b = 2 };",
],
);
let enumerators = |source: &str| {
let tree = CppParser::parse(source, ParserConfig::default());
assert!(
tree.get_errors().is_empty(),
"`{source}` is reported: {:?}",
tree.get_errors()
.iter()
.map(|e| &e.message)
.collect::<Vec<_>>()
);
tree.get_red_root()
.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::EnumeratorDecl)
.map(|node| node.text().to_string().trim().to_string())
.collect::<Vec<_>>()
};
assert_eq!(
enumerators("enum E {\n a = 1,\n#ifdef X\n b = 2,\n#endif\n c = 3\n};"),
vec![
"a = 1".to_string(),
"b = 2".to_string(),
"c = 3".to_string()
],
"all three, with the directive between the second and the third"
);
}
#[test]
fn a_compound_requirement_may_hold_a_braced_temporary() {
assert_reads(
Where::File,
&[
"struct Fn { template<class T> int operator()(T) const; };\ninline constexpr Fn _Begin{};\ntemplate<class T> concept B = true;\ntemplate<class T> concept R = requires(T& t) { { _Begin{}(t) } -> B; };",
"template<class T> concept B = true;\ntemplate<class T> concept R = requires(T& t) { { T{1} } -> B; };",
"template<class T> concept B = true;\ntemplate<class T> concept R = requires(T& t) { { t.f() } noexcept -> B; };",
"template<class T> concept B = true;\ntemplate<class T> concept R = requires(T& t) { { _Begin{}(t) } -> B; { _Begin{}(t) } -> B; };",
"template<class T> concept C = true;\ntemplate<class T> void f(T t) requires C<T> { }",
"template<class T> concept C = true;\nint g(T t) requires C<T> { return 0; }",
],
);
let tree = CppParser::parse(
"struct Fn { template<class T> int operator()(T) const; };\ninline constexpr Fn _Begin{};\ntemplate<class T> concept B = true;\ntemplate<class T> concept R = requires(T& t) { { _Begin{}(t) } -> B; };",
ParserConfig::default(),
);
assert!(tree.get_errors().is_empty(), "no diagnostics");
let kinds: Vec<CppSyntaxKind> = tree
.get_red_root()
.descendants()
.map(|node| CppSyntaxKind::from(node.kind()))
.collect();
assert!(
kinds.contains(&CppSyntaxKind::InitListExpr),
"`_Begin{{}}` is a list-initialised temporary"
);
assert!(
kinds.contains(&CppSyntaxKind::Requirement),
"…inside the requirement"
);
let constrained = CppParser::parse(
"template<class T> concept C = true;\ntemplate<class T> void f(T t) requires C<T> { }",
ParserConfig::default(),
);
assert!(
constrained
.get_red_root()
.descendants()
.any(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::CompoundStat),
"the body of a constrained function is a body, not an initializer"
);
assert!(
!CppParser::parse(
"template<class T> concept C = true;\ntemplate<class T> struct S requires C<T> { int member; };",
ParserConfig::default()
)
.get_errors()
.is_empty(),
"the refusal the constraint marker exists for is untouched"
);
}
#[test]
fn a_base_may_be_a_decltype_and_an_argument_list_holds_directives() {
assert_reads(
Where::File,
&[
"namespace d { template<class T> int f(int); }\nstruct O : decltype(d::f<int>(0)) { };",
"namespace d { template<class... B> int f(int); }\ntemplate<class... B> struct O : decltype(d::f<B...>(0)) { };",
"struct S : public decltype(0) { };",
"struct B { };\nstruct D : public B { };",
"struct B { };\ntemplate<class T> struct D : B, T { };",
"struct B { };\ntemplate<class... T> struct D : B, T... { };",
"template<class T> struct O { };\ntemplate<class T> using A = O<\n int\n#if X\n , long\n#endif\n>;",
"template<class T> struct O { };\ntemplate<class T> using A = O<\n int,\n#if X\n long\n#endif\n >;",
],
);
let tree = CppParser::parse(
"namespace d { template<class T> int f(int); }\nstruct O : decltype(d::f<int>(0)) { };",
ParserConfig::default(),
);
assert!(tree.get_errors().is_empty(), "no diagnostics");
assert!(
tree.get_red_root().descendants().any(|node| {
CppSyntaxKind::from(node.kind()) == CppSyntaxKind::BaseSpecifier
&& node
.descendants()
.any(|inner| CppSyntaxKind::from(inner.kind()) == CppSyntaxKind::TypeId)
}),
"the base specifier holds a type-id — `decltype(d::f<int>(0))` — rather than a bare name"
);
let list = CppParser::parse(
"template<class T> struct O { };\ntemplate<class T> using A = O<\n int\n#if X\n , long\n#endif\n>;",
ParserConfig::default(),
);
assert_eq!(
list.get_red_root()
.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::TemplateArgument)
.count(),
2,
"both arguments, with the directive between them"
);
}
#[test]
fn a_placement_new_initialiser_may_hold_an_expression() {
assert_reads(
Where::File,
&[
"struct T { T(int); };\nstruct A { int b; int c(); };\nvoid f(void* p, A& a) { ::new (p) T(a.b); }",
"struct T { T(int); };\nstruct A { int c(); };\nvoid f(void* p, A& a) { ::new (p) T(a.c()); }",
"struct T { T(int); };\nvoid f(void* p, int x) { ::new (p) T(x + 1); }",
"struct N { N(int, int); };\nstruct A { int c(); };\nvoid f(A& a) { new N(a.c(), 2); }",
"struct T { T(int); };\nvoid f(void* p) { ::new (p) T(1); }",
"auto n = sizeof(void(int));",
"struct Widget { Widget(int); };\nWidget* w = new (Widget)(1);",
"void g(int);",
],
);
let tree = CppParser::parse(
"struct T { T(int); };\nstruct A { int c(); };\nvoid f(void* p, A& a) { ::new (p) T(a.c()); }",
ParserConfig::default(),
);
assert!(tree.get_errors().is_empty(), "no diagnostics");
let type_id: Vec<String> = tree
.get_red_root()
.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::TypeId)
.map(|node| node.text().to_string().trim().to_string())
.collect();
assert!(
type_id.iter().any(|text| text == "T"),
"the allocated type is `T`, not `T(a.c())`: {type_id:?}"
);
assert!(
tree.get_red_root().descendants().any(|node| {
CppSyntaxKind::from(node.kind()) == CppSyntaxKind::NewExpr
&& node
.descendants()
.any(|inner| CppSyntaxKind::from(inner.kind()) == CppSyntaxKind::Initializer)
}),
"the parentheses are the allocation's initializer"
);
}
fn report(source: &str, tree: &CppSyntaxTree) -> Result<(), String> {
if let Some(error) = tree.get_errors().first() {
return Err(format!("{} at {:?}", error.message, error.range));
}
for node in tree.get_red_root().descendants() {
match CppSyntaxKind::from(node.kind()) {
CppSyntaxKind::ErrorNode => {
return Err(format!("ErrorNode over {:?}", node.text_range()));
}
CppSyntaxKind::MissingNode => {
return Err(format!("MissingNode at {:?}", node.text_range()));
}
_ => {}
}
}
let _ = source;
Ok(())
}
#[track_caller]
fn assert_reads(place: Where, constructs: &[&str]) {
let failures: Vec<String> = constructs
.iter()
.filter_map(|construct| {
reads(construct, place)
.err()
.map(|why| format!(" {construct}\n {why}"))
})
.collect();
assert!(
failures.is_empty(),
"{} of {} constructs no longer parse:{}",
failures.len(),
constructs.len(),
failures
.iter()
.map(|failure| format!("\n{failure}"))
.collect::<String>()
);
}
#[test]
fn a_construct_the_grammar_refuses_does_not_take_the_rest_with_it() {
let shapes = [
"[repeatable] [source_annotation_attribute(All)] struct Pre { int unreferenced; };",
"struct Holder { [source_annotation_attribute(All)] int member; };",
"template <class... T> constexpr int folded = (f<T>() | ... | 1);",
"int called() { return (f<int>() | ... | 1); }",
"class [[nodiscard]] Warned { int x; };",
"template <class T> requires C<T> [[nodiscard]] constexpr T identity(T t) { return t; }",
"int branch(int n) { if (n) { [[msvc::constexpr]] return 1; } return 0; }",
];
for shape in shapes {
let source = format!(
"namespace outer {{\n{shape}\n}}\nnamespace inner {{\nstruct Inside {{ int y; }};\n}}\n"
);
let tree = CppParser::parse(&source, ParserConfig::default());
let errors = tree.get_errors().len();
let namespaces: Vec<String> = tree
.get_red_root()
.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::NamespaceDecl)
.map(|node| {
node.children()
.next()
.map(|name| name.text().to_string().trim().to_string())
.unwrap_or_default()
})
.collect();
assert_eq!(
namespaces,
vec!["outer".to_string(), "inner".to_string()],
"two namespaces of the file, neither inside the other — a leaked scope makes the second one a child: {shape}"
);
assert_eq!(errors, 0, "`{shape}` must parse cleanly");
}
}
#[track_caller]
fn assert_does_not_read_yet(place: Where, constructs: &[(&str, &str)]) {
let now_working: Vec<&str> = constructs
.iter()
.filter(|(construct, _)| reads(construct, place).is_ok())
.map(|(construct, _)| *construct)
.collect();
assert!(
now_working.is_empty(),
"these constructs parse now, so move them into the list of what is read:\n {}",
now_working.join("\n ")
);
}
struct Shape {
construct: &'static str,
place: Where,
kind: CppSyntaxKind,
expression: Option<(CppSyntaxKind, Presence)>,
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum Presence {
Required,
Forbidden,
}
fn statement_kind(source: &str) -> Option<CppSyntaxKind> {
let tree = CppParser::parse(source, ParserConfig::default());
let root = tree.get_red_root();
fn statement_kind_of(kind: CppSyntaxKind) -> Option<CppSyntaxKind> {
matches!(
kind,
CppSyntaxKind::Declaration
| CppSyntaxKind::ExpressionStat
| CppSyntaxKind::ReturnStat
| CppSyntaxKind::IfStat
| CppSyntaxKind::ForStat
| CppSyntaxKind::RangeForStat
| CppSyntaxKind::WhileStat
| CppSyntaxKind::DoWhileStat
| CppSyntaxKind::SwitchStat
| CppSyntaxKind::TryStat
| CppSyntaxKind::ThrowStat
| CppSyntaxKind::BreakStat
| CppSyntaxKind::ContinueStat
| CppSyntaxKind::CompoundStat
| CppSyntaxKind::LabelStat
| CppSyntaxKind::EmptyStat
)
.then_some(kind)
}
root.descendants()
.find(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::CompoundStat)
.and_then(|body| {
body.children()
.filter_map(|child| statement_kind_of(CppSyntaxKind::from(child.kind())))
.last()
})
.or_else(|| {
root.children()
.next()
.and_then(|node| statement_kind_of(CppSyntaxKind::from(node.kind())))
})
}
fn contains(source: &str, kind: CppSyntaxKind) -> bool {
CppParser::parse(source, ParserConfig::default())
.get_red_root()
.descendants()
.any(|node| CppSyntaxKind::from(node.kind()) == kind)
}
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 a_macro_from_a_header_can_stand_where_a_declaration_goes() {
let header =
"namespace std _GLIBCXX_VISIBILITY(default)\n{\n struct Widget { int size; };\n}\n";
assert!(
contains(header, CppSyntaxKind::NamespaceDecl),
"the namespace is still a namespace"
);
assert!(
contains(header, CppSyntaxKind::CompoundStat),
"and it still opens a body — the macro is between the name and the brace, not instead of either"
);
assert!(
CppParser::parse(header, ParserConfig::default())
.get_errors()
.is_empty(),
"and the head no longer costs the whole file"
);
let bare = "_GLIBCXX_BEGIN_NAMESPACE_VERSION\ntemplate <typename T> struct S { T x; };\n";
assert!(contains(bare, CppSyntaxKind::MacroCall));
assert!(
CppParser::parse(bare, ParserConfig::default())
.get_errors()
.is_empty(),
"and what follows it is read as the declaration it is"
);
assert!(contains(
"_GLIBCXX_BEGIN_INLINE_ABI_NAMESPACE(__cxx11)\nint x;\n",
CppSyntaxKind::MacroCall
));
let in_a_linkage_block = "extern \"C++\"\n{\nnamespace std\n{\n_GLIBCXX_BEGIN_NAMESPACE_VERSION\n\
struct Widget { int size; };\n}\n}\n";
assert!(
contains(in_a_linkage_block, CppSyntaxKind::MacroCall),
"the macro is still a macro inside a linkage block"
);
assert!(
CppParser::parse(in_a_linkage_block, ParserConfig::default())
.get_errors()
.is_empty(),
"and the declaration behind it is read as one"
);
assert!(
!contains("x = 1;\n", CppSyntaxKind::MacroCall),
"an assignment: no declarator follows an `=`, and asking about declarators is what read this as a macro"
);
assert!(
contains("FOO(x);\n", CppSyntaxKind::Declaration),
"the most vexing parse is a declaration, and the `;` is what says so"
);
assert!(
contains("TEST(A, B) { int x = 1; }\n", CppSyntaxKind::Declaration),
"a macro used as a definition: the block is the declaration's body"
);
assert!(
!contains("void f() { COUNT\n return; }\n", CppSyntaxKind::MacroCall),
"inside a body a missing `;` is the likelier story, and an error is the honest answer"
);
assert!(
!contains("void f() { FOO(x) }\n", CppSyntaxKind::MacroCall),
"and a call inside a body is a call, however it is spelled"
);
}
fn shape_body_of_the_file_reads(source: &str) -> Vec<String> {
let config = ParserConfig::default();
let tree = CppParser::parse(source, config);
assert_eq!(tree.to_source_text(), source, "losslessness");
assert_eq!(tree.get_errors(), [], "and no diagnostics: {source:?}");
tree.get_red_root()
.children()
.map(|node| {
format!(
"{:?}:{}",
CppSyntaxKind::from(node.kind()),
node.first_token()
.map(|token| token.text().trim().to_string())
.unwrap_or_default()
)
})
.collect()
}
#[test]
fn the_compilers_attribute_spellings_are_attributes() {
let parses = |source: &str| {
let tree = CppParser::parse(source, ParserConfig::default());
assert_eq!(tree.get_errors(), [], "this must parse cleanly: {source:?}");
tree
};
let wrapped = parses(
"template <typename T>\n__attribute__((__always_inline__))\ninline T* addressof(T& r);\n",
);
assert!(
wrapped
.get_red_root()
.descendants()
.any(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::AttributeList),
"and it is an attribute node, not a name the specifier sequence took for a type"
);
parses(
"extern \"C++\" __attribute__ ((__noreturn__, __always_inline__))\ninline void f() noexcept { }\n",
);
parses("void f() __attribute__ ((__noreturn__));\n");
parses("__declspec(dllexport) void g();\n");
parses(
"namespace n { }\n_GLIBCXX_BEGIN_NAMESPACE_VERSION\n_GLIBCXX_BEGIN_NAMESPACE_CONTAINER\ntemplate <typename> struct S;\n",
);
let declaration = parses("namespace std {\nsize_t\n_Hash_bytes(const void* p);\n}\n");
assert!(
!declaration
.get_red_root()
.descendants()
.any(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::MacroCall),
"`size_t _Hash_bytes(const void*);` is a declaration with a macro-looking type, not two macros"
);
}
#[test]
fn a_macro_can_stand_among_a_declarators_suffixes() {
let parses = |source: &str| {
let tree = CppParser::parse(source, ParserConfig::default());
assert_eq!(tree.get_errors(), [], "this must parse cleanly: {source:?}");
tree
};
let suffixes = parses(
"inline void __terminate() _GLIBCXX_USE_NOEXCEPT { }\n\
T* addressof(T& r) _GLIBCXX_NOEXCEPT { return nullptr; }\n\
bool before(const type_info& a) const _GLIBCXX_NOEXCEPT;\n\
void f() _GLIBCXX_NOEXCEPT_IF(noexcept(g()));\n\
extern \"C\" void abort(void) _GLIBCXX_NOTHROW _GLIBCXX_NORETURN;\n",
);
assert_eq!(
suffixes
.get_red_root()
.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::MacroCall)
.count(),
6,
"each one is a macro invocation, including the two in a row"
);
let variable = parses("int x MY_DECL_SUFFIX;\n");
assert!(
variable
.get_red_root()
.descendants()
.any(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::MacroCall),
"and it is a macro there too"
);
let keywords = parses("struct S {\n void f() override;\n void g() final;\n};\n");
assert!(
!keywords
.get_red_root()
.descendants()
.any(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::MacroCall),
"`override` and `final` are contextual keywords, not macros"
);
let clause = parses("template <typename T>\nvoid f(T) requires C<T>;\n");
assert!(
clause
.get_red_root()
.descendants()
.any(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::RequiresClause),
"and `requires` still begins a clause: the constraint is read, not swallowed as a macro"
);
}
#[test]
fn a_directive_inside_a_declaration_keeps_the_members_after_it_members() {
let shapes: &[(&str, usize)] = &[
(
"S()\n_GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Alloc>::value)\n\
#if __cpp_concepts\nrequires is_default_constructible_v<_Alloc>\n#endif\n\
: _M_dataplus(_M_local_data())\n{ }\nint after;",
2,
),
(
"int a;\n#if __cpp_deduction_guides\ntemplate<typename = _RequireAllocator<_Alloc>>\n#endif\n\
S(const _CharT* __s);\nint after;",
3,
),
(
"int a;\n#if __cplusplus >= 201103L\ntemplate<typename _InputIterator,\n\
typename = std::_RequireInputIter<_InputIterator>>\n#else\ntemplate<typename _InputIterator>\n\
#endif\nS(const _InputIterator& __beg)\n{ }\nint after;",
3,
),
(
"int a;\n#if __cplusplus >= 201103L\ntemplate<class _InputIterator,\n\
typename = std::_RequireInputIter<_InputIterator>>\n_GLIBCXX20_CONSTEXPR\n\
#else\ntemplate<class _InputIterator>\n#endif\nS&\nappend(_InputIterator __first)\n\
{ return *this; }\nint after;",
3,
),
(
"int a;\n#if __cplusplus > 201703L\n[[deprecated(\"use shrink_to_fit() instead\")]]\n#endif\n\
void reserve();\nint after;",
3,
),
(
"int a;\n_GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR\nbool\nempty() const\n{ return true; }\nint after;",
3,
),
(
"int a;\ntemplate<typename _Tp>\n_GLIBCXX20_CONSTEXPR\n_If_sv<_Tp, S&>\n\
operator=(const _Tp& __svt)\n{ return *this; }\nint after;",
3,
),
(
"S()\n#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0\n_GLIBCXX_NOEXCEPT\n#endif\n\
#if __cpp_concepts\nrequires is_default_constructible_v<_Alloc>\n#endif\n\
#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0\n: _M_dataplus(_S_construct(_Alloc()))\n#else\n\
: _M_dataplus(_S_construct(_Alloc()))\n#endif\n{ }\nint after;",
2,
),
];
for (fragment, members) in shapes {
let source = format!("struct S {{\n{fragment}\n}};\n");
assert_eq!(
reads(&source, Where::File),
Ok(()),
"this must parse cleanly: {fragment:?}"
);
assert_eq!(
direct_members(&source),
*members,
"every member written after the seam must still be a member: {fragment:?}"
);
}
}
#[test]
fn a_member_the_parser_gives_up_on_keeps_the_members_after_it_members() {
let rubble = [
" _GLIBCXX20_CONSTEXPR void f(size_type) { }\n",
" int x = 1\n",
" void g(int)\n",
" __glibcxx_class_requires(_Tp, _SGIAssignableConcept)\n",
];
for text in rubble {
let source = format!("struct Base {{\n{text} int after;\n}};\n");
assert!(
has_a_member_containing(&source, "int after;"),
"the member after the rubble must still be a member of the class: {text:?}"
);
}
}
fn direct_members(source: &str) -> usize {
let Some(body) = class_body(source) else {
return 0;
};
body.children()
.filter(|child| CppSyntaxKind::from(child.kind()) == CppSyntaxKind::Declaration)
.count()
}
fn has_a_member_containing(source: &str, needle: &str) -> bool {
let Some(body) = class_body(source) else {
return false;
};
body.children()
.filter(|child| CppSyntaxKind::from(child.kind()) == CppSyntaxKind::Declaration)
.any(|child| child.text().to_string().contains(needle))
}
fn class_body(source: &str) -> Option<cpp_parser::CppSyntaxNode> {
CppParser::parse(source, ParserConfig::default())
.get_red_root()
.descendants()
.find(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::ClassBody)
}
#[test]
fn a_declarator_takes_only_one_initializer() {
let source =
"struct S {\n int a;\n _GLIBCXX20_CONSTEXPR void f(size_type) { }\n int after;\n};\n";
assert_eq!(
direct_members(source),
3,
"the macro becomes an error node, and the two real members are still members"
);
assert!(
has_a_member_containing(source, "void f(size_type) { }"),
"and the member the macro stood in front of is read as the function it is"
);
assert!(
has_a_member_containing(source, "int after;"),
"with the member written after it untouched"
);
}
#[test]
fn a_declaration_written_once_per_branch_is_read_as_one_declaration() {
let shapes: &[(&str, CppSyntaxKind)] = &[
(
"template<typename _Tp, _Tp _Num>\n using make_integer_sequence\n#if B\n \
= __make_integer_seq<integer_sequence, _Tp, _Num>;\n#else\n \
= integer_sequence<_Tp, __integer_pack(_Num)...>;\n#endif\n",
CppSyntaxKind::UsingDecl,
),
(
"template<typename _Tp>\n concept __is_signed_int128\n#if B\n\t= same_as<_Tp, __int128>;\n#else\n\t\
= false;\n#endif\n",
CppSyntaxKind::ConceptDecl,
),
(
"template<typename _Tp, typename _Up>\n#if C\n \
requires requires { typename _Tp::template rebind<_Up>::other; }\n \
struct __rebind<_Tp, _Up>\n#else\n \
struct __rebind<_Tp, _Up, __void_t<typename _Tp::template rebind<_Up>::other>>\n#endif\n \
{ using type = typename _Tp::template rebind<_Up>::other; };\n",
CppSyntaxKind::ClassBody,
),
];
for (source, kind) in shapes {
assert_eq!(
reads(source, Where::File),
Ok(()),
"must parse cleanly: {source:?}"
);
assert!(
contains(source, *kind),
"and the construct is one node, not rubble: {source:?}"
);
}
}
#[test]
fn a_template_argument_may_be_a_call_or_a_function_type() {
assert_reads(
Where::File,
&[
"using F = std::function<void()>;",
"using G = std::vector<std::function<void()>>;",
"using H = Fn<void(), int>;",
"using C = BoolConstant<_S_use_relocate()>;",
"using D = Other<iter_value_t<_Tp>(iter_move(x)), sizeof(int)>;",
"using E = T<f()>;",
"using I = T<f(1, 2), g(x)>;",
"using J = T<void(int)>;",
"void f() { if (typename Alloc::is_always_equal{}) g(); }",
"void f() { x = typename A::x{}; }",
],
);
}
#[test]
fn a_statement_the_parser_gives_up_on_keeps_the_block_after_it() {
let rubble = [
" __glibcxx_function_requires(_DefaultConstructibleConcept<T>)\n iterator i = lower_bound(k);\n",
" int x = 1\n int y = 2;\n",
" g(1, 2) h();\n",
" S s{\n#if X\n 1\n#endif\n ;\n };\n",
" auto l = [] {\n#if X\n g()\n#endif\n ;\n };\n",
" {\n#if X\n g()\n#endif\n ;\n",
];
for text in rubble {
let source = format!("struct Base {{\n void f() {{\n{text} }}\n int after;\n}};\n");
assert!(
has_a_member_containing(&source, "int after;"),
"the member after the block must still be a member of the class: {text:?}"
);
}
}
#[test]
fn a_member_access_without_a_name_keeps_the_block_after_it() {
let source = "void f() {\n Widget w;\n w.\n}\nint after;\nvoid g() { }\n";
let tree = CppParser::parse(source, ParserConfig::default());
assert!(
tree.get_errors()
.iter()
.any(|error| error.message.contains("member access")),
"the missing member name is still reported: {:?}",
tree.get_errors()
);
assert!(
tree.get_red_root().descendants().any(|node| {
CppSyntaxKind::from(node.kind()) == CppSyntaxKind::Declaration
&& node.text().to_string().starts_with("int after;")
}),
"`int after;` is still a declaration: {}",
tree.to_source_text()
);
let local = tree
.get_red_root()
.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::CompoundStat)
.any(|block| block.text().to_string().contains("int after;"));
assert!(
!local,
"and it is not inside the function body — the block kept its own `}}`: {}",
tree.to_source_text()
);
}
#[test]
fn a_rolled_back_reading_takes_its_diagnostics_with_it() {
let valid = [
"void f() { a.b < c > d; }\n",
"void f() { a.template b<int>(1); }\n",
"void f() { (Widget*)p; }\n",
"struct S { int m; };\nvoid f() { int S::*p = &S::m; }\n",
];
for source in valid {
let tree = CppParser::parse(source, ParserConfig::default());
assert!(
tree.get_errors().is_empty(),
"{source:?} is valid and must read without diagnostics, got {:?}",
tree.get_errors()
.iter()
.map(|error| error.message.clone())
.collect::<Vec<_>>()
);
}
}
#[test]
fn valid_cpp_in_the_shapes_recovery_eats() {
assert_reads(
Where::Body,
&[
"(Widget*)p;",
"(void)p;",
"int x = (int)y;",
"g((int)x);",
"(a)(b)(c);",
"p = (T*)q;",
"A<B> c;",
"a < b > c;",
"a.b < c > d;",
"x = y < z > w;",
"g<int, double>(x);",
"T::template f<int>();",
"typename T::type y;",
"X x = {1, 2};",
"int a[] = {1, 2};",
"T t{};",
"A a = A();",
"new (p) T(1);",
"decltype(x) y;",
"sizeof(T);",
"alignas(16) int x;",
"static_assert(sizeof(T) > 0);",
"auto l = [](int a) { return a; };",
"g([](){ });",
"for (auto& x : v) { }",
"while (a) { }",
"do { } while (a);",
"switch (a) { case 1: break; }",
"switch (0) case 0: default: if (a) ; else g();",
"switch (a) f();",
"switch (a) case 1 ... 5: break;",
"Object a = (Object){ .type = kObjectTypeString };",
"int n = (int){ 3 };",
"struct S s = (struct S){ 1, 2 };",
"(*((a).items + ((a).size++))) = (Object){ 1 };",
"goto end; end: ;",
"if (a) { }",
"throw X();",
"int x = 1; int y = 2;",
"struct S { int x; } s;",
"enum E { A };",
"a->*b;",
"a.*b;",
"int (S::*p) = &S::m;",
],
);
assert_reads(
Where::File,
&[
"using T = int;",
"template<class T> void f(T t) { g<T>(t); }",
"auto f() -> int { return 0; }",
"void f(int a, int b) { }",
"void g() { h(); } void f() { g(); }",
"void f() { (Widget*)p; }",
"struct S { S() : m(1) { } int m; };",
"class C { public: C() = default; ~C(); };",
"struct S { int x : 3; };",
"template<class T> concept C = requires (T t) { t.f(); };",
],
);
}
#[test]
fn a_linkage_block_keeps_the_directives_written_inside_it() {
let source = "\
#ifdef __cplusplus
extern \"C\" {
#endif
int x;
#ifdef __cplusplus
}
#endif
int after;
";
let tree = CppParser::parse(source, ParserConfig::default());
assert!(
tree.get_errors().is_empty(),
"the shape is valid C++ and must read clean: {:?}",
tree.get_errors()
);
let block = tree
.get_red_root()
.descendants()
.find(|node| {
CppSyntaxKind::from(node.kind()) == CppSyntaxKind::CompoundStat
&& node.text().to_string().trim_start().starts_with('{')
})
.expect("the linkage block is a node");
let directives = block
.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::PreprocessorDirective)
.count();
assert_eq!(
directives,
2,
"both directives written inside the block are directives, not error nodes: {}",
block.text()
);
assert!(
!block
.descendants()
.any(|node| { CppSyntaxKind::from(node.kind()) == CppSyntaxKind::ErrorNode }),
"and nothing in it is rubble: {}",
block.text()
);
assert!(
!block.text().to_string().contains("int after;"),
"and the block ends at its own `}}`: {}",
block.text()
);
let source = "extern \"C\" {\n S s{\n#if X\n 1\n#endif\n ;\n };\n int after;\n}\n";
let block = CppParser::parse(source, ParserConfig::default())
.get_red_root()
.descendants()
.find(|node| {
CppSyntaxKind::from(node.kind()) == CppSyntaxKind::CompoundStat
&& node.text().to_string().trim_start().starts_with('{')
})
.expect("the linkage block is a node");
assert!(
block.text().to_string().contains("int after;"),
"the declaration after a broken one is still inside the linkage block: {}",
block.text()
);
}
#[test]
fn a_typedef_declares_every_name_it_lists() {
let source = "\
typedef WCHAR *PWCHAR, *LPWCH;
PWCHAR p;
LPWCH q;
";
let tree = CppParser::parse(source, ParserConfig::default());
assert!(
tree.get_errors().is_empty(),
"a typedef with two declarators is valid C++: {:?}",
tree.get_errors()
);
let declaration_of = |name: &str| {
tree.get_red_root().descendants().any(|node| {
CppSyntaxKind::from(node.kind()) == CppSyntaxKind::Declaration
&& node.text().to_string().starts_with(name)
})
};
assert!(
declaration_of("PWCHAR p;"),
"the first name is a type: {}",
tree.to_source_text()
);
assert!(
declaration_of("LPWCH q;"),
"**and so is the second** — the whole point of the list: {}",
tree.to_source_text()
);
}
#[test]
fn a_class_head_may_carry_a_macro_before_its_name() {
let source = "\
typedef struct DECLSPEC_ALIGN (8) _XSAVE_AREA_HEADER {
DWORD64 Mask;
} XSAVE_AREA_HEADER, *PXSAVE_AREA_HEADER;
XSAVE_AREA_HEADER h;
";
let tree = CppParser::parse(source, ParserConfig::default());
assert!(
tree.get_errors().is_empty(),
"the shape is how mingw-w64 writes every aligned structure: {:?}",
tree.get_errors()
);
assert!(
tree.get_red_root().descendants().any(|node| {
CppSyntaxKind::from(node.kind()) == CppSyntaxKind::Declaration
&& node.text().to_string().starts_with("XSAVE_AREA_HEADER h;")
}),
"and the tag is a type name the file can use: {}",
tree.to_source_text()
);
}
#[test]
fn an_operator_name_is_a_name_in_an_expression_too() {
let parses = |source: &str| {
let tree = CppParser::parse(source, ParserConfig::default());
assert_eq!(tree.get_errors(), [], "this must parse cleanly: {source:?}");
tree
};
parses("template <typename T>\nconcept C = requires(T a) { operator<(a, a); };\n");
parses(
"template <typename T, typename U>\nconcept D = requires(T t, U u) { operator<=>(t, u); };\n",
);
parses(
"template <typename T, typename U>\nconcept E = requires(T t, U u) { t.operator<(u); };\n",
);
parses("template <typename T>\nvoid f(T* p) { p->~T(); }\n");
parses("struct S { ~S(); };\nvoid g(S s) { s.~S(); }\n");
parses("void h() { operator~(); operator new(1); }\n");
let member = parses("void f(A a, B b) { a.operator<(b); }\n");
assert!(
member
.get_red_root()
.descendants_with_tokens()
.filter_map(|element| element.into_token())
.any(|token| CppTokenKind::from(token.kind()) == CppTokenKind::OperatorKeyword),
"the operator name is in the tree, read by the same rule a declaration's operator name goes through"
);
}
fn shape(construct: &'static str, place: Where, kind: CppSyntaxKind) -> Shape {
Shape {
construct,
place,
kind,
expression: None,
}
}
fn requiring(shape: Shape, expression: CppSyntaxKind) -> Shape {
Shape {
expression: Some((expression, Presence::Required)),
..shape
}
}
fn forbidding(shape: Shape, expression: CppSyntaxKind) -> Shape {
Shape {
expression: Some((expression, Presence::Forbidden)),
..shape
}
}
#[track_caller]
fn assert_statement_kind(shapes: &[Shape]) {
let wrong: Vec<String> = shapes
.iter()
.filter_map(|shape| {
let source = match shape.place {
Where::File => shape.construct.to_string(),
Where::Body => format!("void probe() {{ {} }}", shape.construct),
Where::Class => format!("struct Probe {{ {} }};", shape.construct),
};
match statement_kind(&source) {
Some(kind) if kind != shape.kind => Some(format!(
" {}\n read as {kind:?}, must be {:?}",
shape.construct, shape.kind
)),
None => Some(format!(
" {}\n no statement node at all",
shape.construct
)),
Some(_) => match shape.expression {
None => None,
Some((want, Presence::Required)) if contains(&source, want) => None,
Some((want, Presence::Required)) => {
Some(format!(" {}\n no {want:?} in it", shape.construct))
}
Some((unwanted, Presence::Forbidden)) if contains(&source, unwanted) => {
Some(format!(
" {}\n contains {unwanted:?}, which belongs to something else",
shape.construct
))
}
Some(_) => None,
},
}
})
.collect();
assert!(
wrong.is_empty(),
"{} of {} constructs were read as the wrong node:{}",
wrong.len(),
shapes.len(),
wrong
.iter()
.map(|failure| format!("\n{failure}"))
.collect::<String>()
);
}
#[test]
fn constructs_the_parser_reads() {
assert_reads(
Where::File,
&[
"Widget w(1, 2, 3);",
"Widget w(1);",
"std::string s(\"hi\");",
"std::vector<std::vector<int>> vv;",
"struct P { int x; } origin;",
"struct F; struct G { struct H { int y; }; };",
"int (*signal(int sig))(int);",
"using Callback = void (*)(int);",
"typedef void (*Old)(int);",
"typedef char *(*LineGetter)(int, void *, int, bool);",
"char *(*pick)(int);",
"void *(*start)(void *, unsigned long);",
"void f(exarg_T *eap FUNC_ATTR_UNUSED) { }",
"void g(int a _In_, int b _Out_opt_);",
"void h(const char *s FUNC_ATTR_NONNULL_ALL) FUNC_ATTR_NONNULL_RET;",
"extern \"C\" void f();",
"extern \"C\" { void f(); }",
"bool g = true;",
"struct S final : B { };",
"struct S { explicit operator bool() const; };",
"struct S { bool operator==(const S&) const = default; };",
"struct S { S& operator=(const S&) = delete; };",
"struct S { operator int(); };",
"struct S { operator bool() const; };",
"struct S { operator const char*(); };",
"struct S { operator std::string() const; };",
"struct S { operator std::vector<int>(); };",
"struct S { operator T&(); };",
"struct S { operator T&&(); };",
"struct S { operator unsigned long long(); };",
"struct S { operator int() { return 1; } };",
"struct S { operator int() = delete; };",
"struct S { operator int() const &; };",
"struct S { operator bool() &&; };",
"struct S { virtual operator bool() const noexcept; };",
"using Arr = int[4];",
"using Fn = int(char);",
"using Ptr = int(*)[4];",
"[[nodiscard]] int attributed();",
"int attributed2 [[gnu::aligned(16)]];",
"int attributed3() [[carries_dependency]];",
"void attributed4() [[noreturn]] { for (;;) {} }",
"void attributed5(int x [[maybe_unused]]);",
"void attributed6([[maybe_unused]] int x);",
"using Alias [[deprecated]] = int;",
"template <typename T> [[nodiscard]] T attributed7();",
"enum class Attributed8 { A [[deprecated]] = 1, B };",
"alignas(16) struct Aligned { int x; };",
"alignas(16) int aligned_global;",
"alignas(int) int aligned_to_int;",
"alignas(16) alignas(32) int doubly_aligned;",
"struct Aligned2 { alignas(16) int member; };",
"alignas(16) Aligned2 aligned_object;",
"void aligned_param(alignas(16) int x);",
"alignas(16) char aligned_buffer[64];",
"enum class E : unsigned char { A, B };",
"static_assert(sizeof(int) == 4, \"int\");",
"[[nodiscard]] int f();",
"using namespace std;",
"namespace fs = std::filesystem;",
"namespace A::B { int x; }",
"auto f() -> int { return 1; }",
"int f() noexcept { return 1; }",
"constexpr int k = 1;",
"inline constexpr int k = 1;",
"thread_local int t = 0;",
],
);
assert_reads(
Where::File,
&[
"template <typename... Ts> struct S { };",
"template <class... Ts> struct S { };",
"template <typename T, typename... R> void f(T t, R... r);",
"template <typename... A> void f(A&&... a);",
"template <typename T = int> struct R { };",
"template <template <class> class C> struct Q { };",
"template <typename U> using rebind = Rebind<U>;",
"template <typename... Us> using Pack = std::tuple<int, char>;",
"template <typename... Ts> using All = std::tuple<Ts...>;",
"TEST(A, B) { }",
"TEST(FormatPerformance, 1k_row) { }",
"TEST(A, B) { int x = 1; }",
"namespace n { TEST(A, B) { } }",
"namespace std _GLIBCXX_VISIBILITY(default) { int x; }",
"_GLIBCXX_BEGIN_NAMESPACE_VERSION\nint x;\n",
"_GLIBCXX_BEGIN_INLINE_ABI_NAMESPACE(__cxx11)\nint x;\n",
"_GLIBCXX_BEGIN_NAMESPACE_VERSION\n_GLIBCXX_BEGIN_NAMESPACE_CONTAINER\ntemplate <typename> struct S;\n",
"template <typename T>\n__attribute__((__always_inline__))\ninline T* addressof(T& r);\n",
"extern \"C++\" __attribute__ ((__noreturn__, __always_inline__))\ninline void f() noexcept { }\n",
"void f() __attribute__ ((__noreturn__));\n",
"__declspec(dllexport) void g();\n",
"inline void f() _GLIBCXX_USE_NOEXCEPT { }\n",
"T* addressof(T& r) _GLIBCXX_NOEXCEPT { return nullptr; }\n",
"bool before(const type_info& a) const _GLIBCXX_NOEXCEPT;\n",
"void f() _GLIBCXX_NOEXCEPT_IF(noexcept(g()));\n",
"extern \"C\" void abort(void) _GLIBCXX_NOTHROW _GLIBCXX_NORETURN;\n",
"int x MY_DECL_SUFFIX;\n",
"template <typename T>\nconcept C = requires(T a) { operator<(a, a); };\n",
"template <typename T, typename U>\nconcept D = requires(T t, U u) { t.operator<(u); };\n",
"template <typename T>\nvoid f(T* p) { p->~T(); }\n",
"#if !defined(_DEBUG)\ntry\n#endif\n{\n g();\n}\n#if !defined(_DEBUG)\ncatch (const E& e) {\n h();\n}\n#endif",
"void f() {\ntry {\n g();\n}\n#if !defined(_DEBUG)\ncatch (const E& e) {\n#endif\n h();\n}\n}",
"if (n > 0)\n g(n);\n#if FEATURE\nelse if (n < 0)\n h(n);\n#endif",
"if (n > 0) {\n g(n);\n}\n#if FEATURE\nelse {\n h(n);\n}\n#endif",
"if (n > 0)\n g(n);\nelse\n#if FEATURE\n h(n);\n#endif",
"struct S {\n int a;\nprotected:\n#if FEATURE\n int b;\n#else\n int c;\n#endif\nprivate:\n int d;\n};",
"struct S {\n#if FEATURE\n int a;\n#endif\n int b;\n};",
"struct S {\n S()\n noexcept(is_nothrow_default_constructible<A>::value)\n#if CONCEPTS\n requires is_default_constructible_v<A>\n#endif\n : _M_dataplus(_M_local_data())\n { }\n};",
"struct S {\n#if GUIDES\n template<typename = _RequireAllocator<_Alloc>>\n#endif\n S(const char* s);\n};",
"struct S {\n#if CXX11\n template<typename It, typename = std::_RequireInputIter<It>>\n#else\n template<typename It>\n#endif\n S(const It& first) { }\n};",
"struct S {\n#if CXX11\n template<class It, typename = std::_RequireInputIter<It>>\n _GLIBCXX20_CONSTEXPR\n#else\n template<class It>\n#endif\n S& append(It first) { return *this; }\n};",
"struct S {\n#if CXX20\n [[deprecated(\"use shrink_to_fit() instead\")]]\n#endif\n void reserve();\n};",
"struct S {\n _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR\n bool\n empty() const { return true; }\n};",
"struct S {\n template<typename _Tp>\n _If_sv<_Tp, S&>\n operator=(const _Tp& __svt) { return *this; }\n};",
],
);
assert_reads(
Where::Body,
&[
"Widget w(1, 2, 3);",
"Widget w(g(), h());",
"Widget w{a, b};",
"int n = sizeof(int);",
"auto n = alignof(int);",
"auto d = static_cast<int>(1.5);",
"auto d = reinterpret_cast<int*>(p);",
"auto d = (int)1.5;",
"auto [a, b] = pair;",
"auto [a, b] = std::pair<int, int>{1, 2};",
"for (auto&& [k, v] : m) { }",
"auto g = [v = 1] { return v; };",
"auto g = [v = total + 1] { return v; };",
"auto g = [p = std::move(q)] { };",
"auto g = []<typename T>(T t) { return t; };",
"auto g = []<class T>(T t) { return t; };",
"auto g = []<typename... Ts>(Ts... ts) { };",
"auto g = []<typename T> { };",
"auto x = co_await f();",
"co_await f();",
"co_return 1;",
"co_return;",
"co_yield 1;",
"auto x = T::template f<int>();",
"auto x = obj.template f<int>();",
"auto x = p->template f<int>();",
"g(args...);",
"g(h(ts)...);",
"g(args..., more);",
"auto n = sizeof...(Ts);",
"auto t = std::tuple<Ts...>();",
"auto t = std::tuple<Ts..., Us...>();",
"auto s = static_cast<Ts&&>(args)...;",
"auto n = (ts + ...);",
"auto n = (... + ts);",
"auto b = (ts && ...);",
"auto b = (... && ts);",
"auto g = [args...] { return g(args...); };",
"auto v = Vec<int>{1, 2};",
"auto s = std::string{\"x\"};",
"auto e = Vec<int>{};",
"return Vec<int>{1, 2};",
"g(Vec<int>{1});",
"auto sum = Vec<int>{1} + other;",
"auto p = new Widget(1, 2);",
"auto p = new Widget;",
"auto p = new Widget();",
"auto p = new int[4];",
"auto p = new int[4]{1, 2, 3, 4};",
"auto p = new int[];",
"auto p = new int[2][3];",
"auto p = new std::string(\"x\");",
"auto p = new (buf) Widget();",
"auto p = new (buf, size) Widget();",
"auto p = new (1) Widget();",
"auto q = new Widget(1)->run();",
"auto p = ::new Widget(1, 2);",
"auto p = ::new (buf) Widget();",
"(void) ::new T;",
"::new (__loc) _Tp[1]();",
"__try { g(); } __catch (...) { h(); }",
"__try { g(); } __catch (const E& e) { h(e); }",
"void f() throw();",
"void g() throw(int);",
"void h() const throw(std::exception&, int);",
"int __cdecl f(void) throw();",
"if _GLIBCXX17_CONSTEXPR (x) { }",
"if (_GLIBCXX17_CONSTEXPR (x)) { }",
"if (x) { } else if _GLIBCXX17_CONSTEXPR (y) { }",
"if (x) [[likely]] { }",
"if (x) [[unlikely]] y();",
"if (x) [[likely]] { } else [[likely]] { }",
"template <typename T> constexpr bool v = false;",
"template <typename T> constexpr bool v<T*> = true;",
"template <> constexpr bool v<int> = true;",
"template <typename T, int N> struct S<T[N]> { };",
"template <typename T> struct S<T[]> { };",
"template <typename T, int N> constexpr bool d<T[N]> = true;",
"template <typename T> void f(T[4]);",
"template <typename T> void f(T[]);",
"template <template <class> class C, class T> struct S<C<T>> { };",
"template <class T> struct Outer { template <class U> void f(T t, U u[4]); };",
"extern \"C++\"\n{\nnamespace std\n{\n_GLIBCXX_BEGIN_NAMESPACE_VERSION\n int helper();\n}\n}",
"delete[] p;",
"int n = sizeof(void(int));",
"auto x = a ? b : c;",
"if (auto q = find(x); q != nullptr) { }",
"switch (auto q = f(); q) { }",
"if (x > 0) { }",
"if (Foo* p = get()) { }",
"if (auto q = f(); q) { } else { }",
"try { } catch (const E& e) { }",
"goto label; label: ;",
"int x = {1};",
"x = {1};",
"x = {1, 2};",
"x = {};",
"x += {1};",
"v.push_back({1, 2});",
"f({1});",
"f(a, {1});",
"auto r = requires { g(); };",
"if constexpr (requires { g(); }) { }",
"static_assert(requires { g(); });",
"x = (a);",
"x = (a + b);",
"x = ((a));",
"x = a * (b + c);",
"auto x = (a, b);",
"x = 1, y = 2;",
"return 1, 2;",
"for (a = 0, b = 0; ; ) { }",
"g(a, (b, c));",
"auto d = (T*)p;",
"auto d = (MyType*)p;",
"auto d = (T&)x;",
"auto d = (ns::T*)p;",
"auto d = (T<int>*)p;",
"auto d = (const T*)p;",
"auto d = (T*)p->q;",
"auto d = (T*)p + 1;",
"g((T*)p, (U*)q);",
"auto d = (MyType)1.5;",
"auto d = (MyType)x;",
"auto d = (size_t)size;",
"buf = (char *)malloc((size_t)size + 1);",
"auto d = (MyType)new T;",
"auto d = (MyType)sizeof(T);",
"auto s = \"a\" \"b\";",
"auto s = \"a\" \"b\" \"c\";",
"g(\"a\" \"b\", 1);",
"auto x = 1_km;",
"auto x = \"a\"_km;",
"for (;; i++) { }",
"for (;; i++, k++) { }",
"for (i = 0, k = 0; i < n; i++, k++) { }",
"auto n = sizeof(a[0]);",
"auto n = sizeof(a.b);",
"auto n = sizeof(a + b);",
"auto n = sizeof(a());",
"auto n = sizeof(int*);",
"auto n = sizeof(unsigned long);",
"auto n = typeid(a[0]);",
"auto n = sizeof(struct S);",
"auto n = sizeof(union U);",
"auto n = (struct S*)p;",
"using A = struct S;",
"auto n = sizeof(int[4]);",
"auto n = sizeof(char[256]);",
"auto n = sizeof(int[2][3]);",
"auto n = sizeof(int*[4]);",
"auto n = sizeof(int[]);",
"Vec<int[4]> v;",
"auto v = Vec<int[4]>{};",
"auto x = a[b < c];",
"char const w[] = { 'a' };",
"char const w[2] = { 'a' };",
"int const x = 1;",
"unsigned const int y = 1;",
"struct S const s;",
"static void A::f<int>(int);",
"inline void A::f<int>(int);",
"extern void A::f<int>(int);",
"static void A<int>::f<int>(int);",
"static void A::f<int>(int) { }",
"static void Widget::draw(T) { }",
"void Widget::draw(T) { }",
"void ns::C::method() { }",
"x = a < b > c;",
"x = a < b || c > d;",
"if (n < 0 || n > 100000) { }",
"while (i < n && j > k) { }",
"g(a < b, c > d);",
"auto n = A<B>::value;",
"g<int>(1);",
"auto p = new A<B>();",
"auto v = std::vector<int>{};",
"std::vector<int> v;",
"Foo<int> x;",
"auto x = a and b;",
"auto x = a or b;",
"auto x = not a;",
"auto x = a bitand b;",
"auto x = a bitor b;",
"auto x = a xor b;",
"auto x = compl a;",
"auto x = a not_eq b;",
"a and_eq b;",
"a or_eq b;",
"a xor_eq b;",
"if (a and b) { }",
"while (not done) { }",
"x = throw 1;",
"auto y = cond ? throw 1 : 2;",
"return throw 1;",
"g(throw 1);",
"extern template struct S<int>;",
"extern template class C<int>;",
"extern template void f<int>(int);",
"template void f<int>(int);",
"template void f<int>(int) { }",
"template class C<int>;",
"template struct S<int>;",
"template int v<int>;",
"template <> void f<int>(int);",
"template <> int v<int>;",
"template <> struct S<int>;",
"inline namespace v1 { }",
"inline namespace v1 { int x; }",
"inline namespace v1 = a::b;",
"decltype(x) y;",
"decltype(x) y = 1;",
"decltype(auto) y;",
"decltype(auto) x = f();",
"decltype(x)* p;",
"decltype(x) v[2];",
"noexcept(f()) g();",
"const decltype(x) y = 1;",
"template <typename T> concept C = true;",
"template <typename T> concept C = requires(T t) { t.f(); };",
"template <typename T> concept C = C2<T> && C3<T>;",
"template <C T> void f(T t);",
"template <C<T> U> void f(U u);",
"template <typename T> requires C<T> void f(T t);",
"template <typename T> requires C<T> && C2<T> void f(T t);",
"template <typename T> requires (C<T>) void f(T t);",
"template <typename T> void f(T t) requires C<T>;",
"template <typename T> void f(T t) requires C<T> { }",
"template <typename T> void f(T t) requires requires(T t) { t.f(); };",
"void f() requires true;",
"template <typename T> auto g(T t) -> int requires C<T>;",
"template <typename T> requires (sizeof(T) > 1) && (sizeof(T) < 64) void h(T t);",
"template <int N> requires ((N >> 1) > 0) void h();",
"template <int N> requires (N < 0 || N > 3) void f();",
"template <int N> requires (N > 0) void f();",
"template <typename T> requires (C<T>) T value = T{};",
"template <typename T> void f(T t) requires C<T> && C2<T> { }",
"void f(T);",
"void f(T) { }",
"template <typename T> void f(T) { }",
"template <typename T> void f(T) requires C<T> { }",
"void f(std::vector<T>);",
"int requires = 1;",
"int concept = 2;",
"void requires();",
"void f(int requires, int concept);",
"struct S { int requires; int concept; };",
"int main(argc, argv)\nint argc;\nchar *argv[];\n{ return argc; }",
"int main(argc, argv)\nint argc;\nchar *argv[];",
"int f(a)\nint a;\nfloat b;\n{ return a; }",
"enum E { A = 0, B };",
"enum E { A = 0, B, };",
"enum class E : unsigned { A = 1, B = 2, C };",
"enum E { A = 1 << 2, B = f(1, 2), C };",
"\u{feff}int x;",
"MY_API Widget *p;",
"EMMY_API Result f(int x);",
"EXPORT std::string g();",
"class D : public Base<K, std::shared_ptr<V>> { };",
"class D : public std::vector<std::pair<K, V>> { };",
"void f(const std::map<int, int, std::less<>> &m);",
"std::map<int, int, std::less<>> &Get();",
"void f(std::map<int, std::less<int>> m);",
"int C::*p;",
"typedef int (C::*fp)(int);",
"void g(int (C::*h)(int));",
"void h(int (C::*)(int));",
"struct C { };\nvoid f(C* c) { (c->*h)(1); }",
"struct C { };\nvoid f(C& c) { (c.*h)(1); }",
"auto x = 1k_row;",
"#define IF_EXIST(op) if (!Get(op).empty())\nIF_EXIST(a) { g(); }",
"IF_EXIST(k) { g(); }",
"TEST(A, B) { g(); }",
"A<bool(T)> x;",
"std::function<bool(TokenKind)> pred;",
"using F = A<bool(T)>;",
"auto n = sizeof(A<bool(T)>);",
"void h(A<bool(T)> x);",
"struct S { int a; } x;",
"struct { int a; } x[] = { { 1 } };",
"static const struct { unsigned char left; } priority[] = { { 1 } };",
"typedef struct { int a; } Alias;",
"enum E { A } e;",
"class C { int a; } c = {};",
"auto y = (::x);",
"auto y = (::abs(x) > 1);",
"auto y = bool(x);",
"g(\"key\", bool(lint[\"codeStyle\"]));",
"auto timeout = 100ms;",
"auto text = \"name\"sv;",
],
);
assert_reads(
Where::Class,
&[
"mutable int cache = 0;",
"[[nodiscard]] int pure() const noexcept;",
"friend void swap(S&, S&);",
"virtual ~S() = default;",
"void f() override;",
"static inline int count = 0;",
"void f() { for (auto &v: vec) { } }",
"void f() { again: g(); }",
"Probe(int v) : a(v) {}",
"Probe() : a(1) {}",
"Probe() : b{2} {}",
"~Probe();",
"~Probe() {}",
"virtual ~Probe() = default;",
"~Probe() = delete;",
"~Probe() noexcept {}",
"template <typename T> void f(T t);",
"template <typename U> using rebind = Probe<U>;",
"struct Nested { int x; };",
"enum Color { Red, Green };",
"int bits : 3;",
"unsigned flags : 1, spare : 7;",
"int : 0;",
"using Base::method;",
"using Base::Alias;",
"using Alias = int;",
"friend void swap(Probe&, Probe&); int after = 0;",
"friend class Other; int later = 0;",
"friend void swap(Probe&, Probe&); Probe& method();",
"friend class Other; Probe& method2();",
"void f(this Probe& self);",
"void f(this Probe&& self) &&;",
"void f(this auto&& self) {}",
"void g(this Probe& self, int x);",
"void h(this Probe& self) const;",
"void i(this Probe&);",
"void j(this Probe& self) { self.x = 1; }",
],
);
}
#[test]
fn constructs_the_parser_does_not_read_yet() {
assert_does_not_read_yet(
Where::Body,
&[
],
);
assert_does_not_read_yet(
Where::Body,
&[
(
"g(x) { }",
"a call with its `;` missing, followed by a block — not a macro, since the name is not spelled \
like one",
),
(
"NUMBER_OPTION(tab_width)\ng(x)",
"a call with its `;` missing: the macro reading needed the name to be one the tables knew, and \
the grammar no longer has tables; see `ParserConfig`",
),
(
"#define NUMBER_OPTION(op) if (auto v = Get(op); !v.empty()) { }\nNUMBER_OPTION(tab_width)",
"the same shape **with** the `#define` in the file, which used to be enough: the replacement \
list is the evidence, and reading it is the preprocessor's job rather than the grammar's",
),
],
);
assert_does_not_read_yet(
Where::File,
&[(
"void f() try { } catch (...) { }",
"a **function-try-block**: `try` written between the declarator and its body. Valid C++ (checked \
with g++ 15), read today as four errors starting at `void`. The `try` was given a rule as a \
*statement* (that is why the same tokens are fine inside a body) and not as a part of a function \
definition",
)],
);
assert_does_not_read_yet(
Where::Class,
&[
(
"void f() try { } catch (const E& e) { }",
"the declarator's tail has no rule for `try`",
),
],
);
}
#[test]
fn constructs_are_read_as_the_right_node() {
assert_statement_kind(&[
shape("x = 1;", Where::Body, CppSyntaxKind::ExpressionStat),
shape("x = a + b;", Where::Body, CppSyntaxKind::ExpressionStat),
shape("value = other;", Where::Body, CppSyntaxKind::ExpressionStat),
shape("x = f();", Where::Body, CppSyntaxKind::ExpressionStat),
shape("x = a ? b : c;", Where::Body, CppSyntaxKind::ExpressionStat),
shape("x = { 1 };", Where::Body, CppSyntaxKind::ExpressionStat),
shape("x = { 1, 2 };", Where::Body, CppSyntaxKind::ExpressionStat),
shape("C<T> && C2<T>;", Where::Body, CppSyntaxKind::ExpressionStat),
shape("x = 1;", Where::File, CppSyntaxKind::ExpressionStat),
shape("A<bool(T)> x;", Where::File, CppSyntaxKind::Declaration),
shape(
"std::function<bool(TokenKind)> pred;",
Where::File,
CppSyntaxKind::Declaration,
),
shape(
"struct S { int a; } x;",
Where::File,
CppSyntaxKind::Declaration,
),
shape(
"static const struct { int a; } priority[] = { { 1 } };",
Where::File,
CppSyntaxKind::Declaration,
),
shape("int x = 1;", Where::Body, CppSyntaxKind::Declaration),
shape(
"int ns::count = 0;",
Where::File,
CppSyntaxKind::Declaration,
),
shape(
"int ns::Widget::count = 0;",
Where::File,
CppSyntaxKind::Declaration,
),
shape("Widget w(1, 2);", Where::Body, CppSyntaxKind::Declaration),
shape(
"std::string s(\"x\");",
Where::Body,
CppSyntaxKind::Declaration,
),
shape("g(1, 2);", Where::Body, CppSyntaxKind::ExpressionStat),
shape("use(x);", Where::Body, CppSyntaxKind::ExpressionStat),
shape("++i;", Where::Body, CppSyntaxKind::ExpressionStat),
shape("delete p;", Where::Body, CppSyntaxKind::ExpressionStat),
shape("if (x) { }", Where::Body, CppSyntaxKind::IfStat),
shape("for (;;) { break; }", Where::Body, CppSyntaxKind::ForStat),
shape("return;", Where::Body, CppSyntaxKind::ReturnStat),
requiring(
shape("(a, b);", Where::Body, CppSyntaxKind::ExpressionStat),
CppSyntaxKind::BinaryExpr,
),
requiring(
shape("x = 1, y = 2;", Where::Body, CppSyntaxKind::ExpressionStat),
CppSyntaxKind::BinaryExpr,
),
requiring(
shape("return 1, 2;", Where::Body, CppSyntaxKind::ReturnStat),
CppSyntaxKind::BinaryExpr,
),
forbidding(
shape("g(a, b);", Where::Body, CppSyntaxKind::ExpressionStat),
CppSyntaxKind::BinaryExpr,
),
forbidding(
shape("g(a, b, c);", Where::Body, CppSyntaxKind::ExpressionStat),
CppSyntaxKind::BinaryExpr,
),
requiring(
shape("(T*)p;", Where::Body, CppSyntaxKind::ExpressionStat),
CppSyntaxKind::CastExpr,
),
requiring(
shape("(MyType*)p;", Where::Body, CppSyntaxKind::ExpressionStat),
CppSyntaxKind::CastExpr,
),
forbidding(
shape("(a * b);", Where::Body, CppSyntaxKind::ExpressionStat),
CppSyntaxKind::CastExpr,
),
forbidding(
shape("(a* b);", Where::Body, CppSyntaxKind::ExpressionStat),
CppSyntaxKind::CastExpr,
),
forbidding(
shape("(a);", Where::Body, CppSyntaxKind::ExpressionStat),
CppSyntaxKind::CastExpr,
),
forbidding(
shape("(f)(x);", Where::Body, CppSyntaxKind::ExpressionStat),
CppSyntaxKind::CastExpr,
),
requiring(
shape("x = throw 1;", Where::Body, CppSyntaxKind::ExpressionStat),
CppSyntaxKind::ThrowExpr,
),
requiring(
shape("throw 1;", Where::Body, CppSyntaxKind::ThrowStat),
CppSyntaxKind::ThrowStat,
),
forbidding(
shape("x = throw 1;", Where::Body, CppSyntaxKind::ExpressionStat),
CppSyntaxKind::ThrowStat,
),
requiring(
shape(
"::new (buf) Widget();",
Where::Body,
CppSyntaxKind::ExpressionStat,
),
CppSyntaxKind::NewExpr,
),
requiring(
shape("auto x = a and b;", Where::Body, CppSyntaxKind::Declaration),
CppSyntaxKind::BinaryExpr,
),
requiring(
shape("auto x = not a;", Where::Body, CppSyntaxKind::Declaration),
CppSyntaxKind::UnaryExpr,
),
forbidding(
shape("auto x = android;", Where::Body, CppSyntaxKind::Declaration),
CppSyntaxKind::BinaryExpr,
),
forbidding(
shape("g(this);", Where::Body, CppSyntaxKind::ExpressionStat),
CppSyntaxKind::Parameter,
),
requiring(
shape(
"decltype(x) y = 1;",
Where::File,
CppSyntaxKind::Declaration,
),
CppSyntaxKind::DeclSpecifierSeq,
),
requiring(
shape("decltype(auto) y;", Where::File, CppSyntaxKind::Declaration),
CppSyntaxKind::DeclSpecifierSeq,
),
]);
assert!(
!contains(
"template <int I> struct S<a[I]> { };",
CppSyntaxKind::ArrayType
),
"`a` is not a template parameter, so `a[I]` is a subscript"
);
let source = "void probe() { ::new (buf) Widget(); }";
let allocation = CppParser::parse(source, ParserConfig::default())
.get_red_root()
.descendants()
.find(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::NewExpr)
.expect("the allocation is a NewExpr");
assert!(
allocation
.children_with_tokens()
.filter_map(|child| child.into_token())
.any(|token| CppTokenKind::from(token.kind()) == CppTokenKind::Scope),
"the `::` is the NewExpr's own token"
);
}
#[track_caller]
fn assert_fragment_contains(fragments: &[(&str, CppSyntaxKind)]) {
let missing: Vec<String> = fragments
.iter()
.filter(|(fragment, kind)| !contains(&format!("void probe() {{ {fragment} }}"), *kind))
.map(|(fragment, kind)| format!(" {fragment}\n no {kind:?} in it"))
.collect();
assert!(
missing.is_empty(),
"{} of {} fragments are missing a node:{}",
missing.len(),
fragments.len(),
missing
.iter()
.map(|failure| format!("\n{failure}"))
.collect::<String>()
);
}
#[test]
fn a_header_name_is_one_token_whatever_is_in_it() {
let folds = |source: &str| {
let tree = CppParser::parse(source, ParserConfig::default());
tree.get_red_root()
.descendants_with_tokens()
.filter_map(|element| element.into_token())
.any(|token| CppTokenKind::from(token.kind()) == CppTokenKind::HeaderName)
};
assert!(folds("#include <vector>\n"), "the ordinary case");
assert!(folds("#include \"local.h\"\n"), "and the quoted one");
assert!(
folds("#include <bits/c++config.h>\n"),
"`++` splits the name into three tokens, and a header name is not limited to the tokens a whitelist \
happened to list"
);
assert!(
folds("#include <sys/a-b.h>\n"),
"`-` is a token of its own too"
);
assert!(folds("#include <a/b/c.hpp>\n"));
assert!(
!folds("#include <unterminated\nint x;\n"),
"a newline ends the directive, so there is no header name to fold"
);
assert!(
!folds("a < b;\n"),
"and outside a directive `<` is still less-than, which is why this is a parser rule and not a lexer one"
);
}
#[test]
fn modern_constructs_produce_the_right_nodes() {
assert_fragment_contains(&[
("void f(this S& self);", CppSyntaxKind::Parameter),
("void f(this S& self);", CppSyntaxKind::ThisExpr),
("void f(this auto&& self);", CppSyntaxKind::Parameter),
("throw 1;", CppSyntaxKind::ThrowStat),
("x = throw 1;", CppSyntaxKind::ThrowExpr),
("auto y = cond ? throw 1 : 2;", CppSyntaxKind::ThrowExpr),
("inline namespace v1 { }", CppSyntaxKind::NamespaceDecl),
(
"template <typename T> concept C = true;",
CppSyntaxKind::ConceptDecl,
),
(
"template <typename T> requires C<T> void f(T t);",
CppSyntaxKind::RequiresClause,
),
(
"template <typename T> void f(T t) requires C<T> { }",
CppSyntaxKind::RequiresClause,
),
(
"template <typename T> auto g(T t) -> int requires C<T>;",
CppSyntaxKind::RequiresClause,
),
(
"template <typename T> requires C<T> struct S { };",
CppSyntaxKind::RequiresClause,
),
(
"template <typename T> requires C<T> [[nodiscard]] T f(T t) { return t; }",
CppSyntaxKind::RequiresClause,
),
(
"[[msvc::constexpr]] return 1;",
CppSyntaxKind::AttributeList,
),
("[[msvc::constexpr]] return 1;", CppSyntaxKind::ReturnStat),
("[[likely]] if (n) { return 1; }", CppSyntaxKind::IfStat),
(
"template <typename T> requires C<T> [[nodiscard]] T f(T t) { return t; }",
CppSyntaxKind::AttributeList,
),
(
"template <typename T> concept C = requires(T t) { t.f(); };",
CppSyntaxKind::RequiresExpr,
),
("auto r = requires { g(); };", CppSyntaxKind::RequiresExpr),
(
"auto r = requires(T t) { t.f(); };",
CppSyntaxKind::Requirement,
),
("x = {1};", CppSyntaxKind::InitListExpr),
("v.push_back({1, 2});", CppSyntaxKind::InitListExpr),
("int x = {1};", CppSyntaxKind::Initializer),
("void f(T);", CppSyntaxKind::Parameter),
(
"template <typename T> void f(T) { }",
CppSyntaxKind::ParameterList,
),
(
"template <typename T> void f(T) { }",
CppSyntaxKind::CompoundStat,
),
("void f() { requires(); }", CppSyntaxKind::CallExpr),
("void f() { concept(); }", CppSyntaxKind::CallExpr),
("void f() { concept = 2; }", CppSyntaxKind::ExpressionStat),
("void f() { requires = 1; }", CppSyntaxKind::ExpressionStat),
("auto d = (MyType)1.5;", CppSyntaxKind::CastExpr),
("auto d = (size_t)size;", CppSyntaxKind::CastExpr),
(
"buf = (char *)malloc((size_t)size + 1);",
CppSyntaxKind::CastExpr,
),
("auto d = (a) - b;", CppSyntaxKind::ParenExpr),
("auto d = (a)(b);", CppSyntaxKind::CallExpr),
("void f() { x = a < b > c; }", CppSyntaxKind::BinaryExpr),
(
"void f() { auto n = A<B>::value; }",
CppSyntaxKind::TemplateArgumentList,
),
("auto s = \"a\" \"b\" \"c\";", CppSyntaxKind::LiteralExpr),
(
"void f() { for (;; i++, k++) { } }",
CppSyntaxKind::ExpressionStat,
),
("auto n = sizeof(struct S);", CppSyntaxKind::TypeId),
("auto n = (struct S*)p;", CppSyntaxKind::CastExpr),
("static void A::f<int>(int);", CppSyntaxKind::ParameterList),
(
"static void Widget::draw(T) { }",
CppSyntaxKind::ParameterList,
),
(
"static void Widget::draw(T) { }",
CppSyntaxKind::CompoundStat,
),
("char const w[2];", CppSyntaxKind::InitDeclarator),
("char const w[2];", CppSyntaxKind::ArrayType),
("auto n = sizeof(int[4]);", CppSyntaxKind::ArrayType),
("auto n = sizeof(a[0]);", CppSyntaxKind::IndexExpr),
("auto p = new int[4];", CppSyntaxKind::TypeId),
("auto p = new int[4];", CppSyntaxKind::ArrayType),
(
"void f() { __try { g(); } __catch (...) { h(); } }",
CppSyntaxKind::CatchStat,
),
("void f() throw(std::exception&);", CppSyntaxKind::TypeId),
(
"void f() { if _GLIBCXX17_CONSTEXPR (x) { } }",
CppSyntaxKind::ParenExpr,
),
(
"void f() { if (x) [[likely]] { } }",
CppSyntaxKind::AttributeList,
),
("int __cdecl g(void);", CppSyntaxKind::ParameterList),
("int *__cdecl _errno(void);", CppSyntaxKind::ParameterList),
(
"__forceinline size_t f(const char * _src) { return 0; }",
CppSyntaxKind::ParameterList,
),
(
"int f(const char * __restrict__ _Src);",
CppSyntaxKind::RestrictQual,
),
(
"__forceinline int f(void) { return 0; }",
CppSyntaxKind::InlineSpec,
),
(
"template <typename _Tp, int _Nm> constexpr bool d<_Tp[_Nm]> = true;",
CppSyntaxKind::ArrayType,
),
(
"template <typename T> void f(T[4]);",
CppSyntaxKind::ArrayType,
),
]);
let source = "struct Probe { void f(this S& self); };";
assert_eq!(
CppParser::parse(source, ParserConfig::default())
.get_red_root()
.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::Parameter)
.count(),
1,
"an explicit object parameter is one parameter"
);
let source = "template <typename T> requires C<T> && C2<T> void f(T t);";
assert_eq!(
CppParser::parse(source, ParserConfig::default())
.get_red_root()
.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::RequiresClause)
.count(),
1,
"a conjunction of two concepts is one clause"
);
let source = "int main(argc, argv)\nint argc;\nchar *argv[];\n{ return argc; }";
assert!(
contains(source, CppSyntaxKind::OldStyleParameterList),
"the parameters declared after the parenthesis get a list of their own"
);
assert!(
contains(source, CppSyntaxKind::CompoundStat),
"and the body is still the definition's"
);
let source = "class D : public Base<K, std::shared_ptr<V>> { };";
assert!(
contains(source, CppSyntaxKind::BaseSpecifier),
"the nested base clause is read as a base clause"
);
let source = "void f(const std::map<int, int, std::less<>> &m);";
assert_eq!(
CppParser::parse(source, ParserConfig::default())
.get_red_root()
.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::TemplateArgumentList)
.count(),
2,
"two template argument lists, one of them empty"
);
let source = "int C::*p;";
let specifiers = CppParser::parse(source, ParserConfig::default())
.get_red_root()
.descendants()
.find(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::DeclSpecifierSeq)
.expect("a specifier sequence")
.text()
.to_string();
assert_eq!(
specifiers, "int",
"the member-pointer operator belongs to the declarator, not to the type"
);
let source = "TEST(FormatPerformance, 1k_row) { int x = 1; }";
let parsed = CppParser::parse(source, ParserConfig::default());
let body = parsed
.get_red_root()
.descendants()
.find(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::CompoundStat)
.expect("the block is in the tree");
assert_eq!(
body.parent()
.map(|parent| CppSyntaxKind::from(parent.kind())),
Some(CppSyntaxKind::Declaration),
"the block is the definition's body, not a statement after it"
);
assert_eq!(
body.text().to_string().trim_start(),
"{ int x = 1; }",
"and its own declarations are inside it"
);
let source = "static const struct { int a; } priority[] = { { 1 } };";
let named = CppParser::parse(source, ParserConfig::default())
.get_red_root()
.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::InitDeclarator)
.flat_map(|declarator| declarator.descendants())
.filter(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::NameExpr)
.map(|name| name.text().to_string())
.any(|name| name == "priority");
assert!(
named,
"the declarator is named `priority`, not part of the type"
);
let source = "struct S { void f() { for (auto &v: vec) { } } };";
assert!(
contains(source, CppSyntaxKind::RangeForStat),
"a range-for inside a member function is a range-for, not a bit-field"
);
let source = "struct S { void f() { again: g(); } };";
assert!(
contains(source, CppSyntaxKind::LabelStat),
"a label inside a member function is a label"
);
let source = "struct S { int bits : 3; };";
assert!(
contains(source, CppSyntaxKind::Initializer),
"a member's `:` is still a bit-field width"
);
for (source, expected) in [
("enum E { A = 0, B };", 2),
("enum E { A = 0, B, };", 2),
("enum class E : unsigned { A = 1, B = 2, C };", 3),
("enum E { A, B, C };", 3),
("enum E { A = 0 };", 1),
] {
assert_eq!(
CppParser::parse(source, ParserConfig::default())
.get_red_root()
.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::EnumeratorDecl)
.count(),
expected,
"{source:?} has {expected} enumerators"
);
}
}
#[test]
fn a_template_parameter_may_default_to_a_conditional_expression() {
let source = "enum class subrange_kind { sized, unsized };\n\
template<typename A, typename B> struct sized_sentinel_for { };\n\
template<typename _It, typename _Sent = _It,\n\
subrange_kind _Kind = sized_sentinel_for<_Sent, _It>\n\
? subrange_kind::sized : subrange_kind::unsized>\n\
struct subrange { };\n";
assert_eq!(
reads(source, Where::File),
Ok(()),
"a conditional default reads as a value, not as a type that stops at the `?`"
);
let source = "template<typename T> struct V { };\n\
template<typename T, V<T> Arg = V<T>()> struct W { };\n";
assert!(
reads(source, Where::File).is_ok(),
"a type-id default keeps its reading"
);
}
#[test]
fn a_directive_may_stand_on_either_side_of_a_declarations_initializer() {
let source = "const int x =\n#ifndef Y\n 1;\n#else\n 2;\n#endif\n";
assert_eq!(
reads(source, Where::File),
Ok(()),
"a directive between the `=` and the value is a seam, not an initializer"
);
assert!(
contains(source, CppSyntaxKind::Initializer),
"and the value after the directive is still that declarator's initializer"
);
assert_eq!(
count_of(source, CppSyntaxKind::PreprocessorDirective),
3,
"all three directives stay nodes rather than becoming rubble"
);
let source = "template<typename _Tp> void f() {\n\
bool b =\n\
#if __has_builtin(__is_trivially_constructible) \\\n\
&& __has_builtin(__is_trivially_assignable)\n\
1\n\
#endif\n\
;\n\
(void)b;\n\
}\n";
assert_eq!(
reads(source, Where::File),
Ok(()),
"a directive between the value and the `;` is the same seam"
);
assert!(
!contains("int x\n#if 1\n 1\n#endif\n;\n", CppSyntaxKind::Initializer),
"a directive after a declarator does not create an initializer"
);
}
#[test]
fn the_attribute_spelling_with_one_trailing_underscore_is_an_attribute() {
let source = "extern \"C\"\n__attribute((dllimport)) void __attribute__((stdcall)) Sleep (unsigned long);\n";
assert_eq!(
reads(source, Where::File),
Ok(()),
"both attribute spellings read, so the declaration they modify is read"
);
assert_eq!(
count_of(source, CppSyntaxKind::AttributeList),
2,
"each of the two is one attribute list, whichever spelling it uses"
);
assert_eq!(
count_of("int __attribute = 1;\n", CppSyntaxKind::AttributeList),
0,
"`__attribute` with no parenthesis after it is a name, not an attribute"
);
assert_eq!(
reads("int __attribute = 1;\n", Where::File),
Ok(()),
"…and the declaration it names still reads"
);
}
#[test]
fn an_enumerator_may_carry_a_macro_before_its_value() {
let source = "#define DEP __attribute__((__deprecated__))\n\
typedef enum E {\n\
A DEP\n\
= 1,\n\
B = 2\n\
} E;\n";
assert_eq!(
reads(source, Where::File),
Ok(()),
"a macro between an enumerator's name and its `=` is a seam, not the end of the enumerator"
);
assert_eq!(
count_of(source, CppSyntaxKind::EnumeratorDecl),
2,
"and both enumerators are still enumerators"
);
assert_eq!(
count_of(source, CppSyntaxKind::MacroCall),
1,
"the macro is read as the invocation it is"
);
let source = "enum E { A [[deprecated]] = 1, B = 2 };\n";
assert_eq!(reads(source, Where::File), Ok(()), "`[[…]]` still reads");
assert_eq!(
count_of(source, CppSyntaxKind::MacroCall),
0,
"an attribute is not a macro invocation"
);
}
#[test]
fn a_template_argument_may_be_a_comparison_in_parentheses() {
let source = "template<bool B, typename T = void> struct C { };\n\
using size_t = unsigned long long;\n\
template<size_t __i, typename... _Types>\n\
C<(__i >= sizeof...(_Types))>\n\
f(const C<true>&);\n";
assert_eq!(
reads(source, Where::File),
Ok(()),
"the argument is the parenthesised comparison, not the two tokens the failed reading stopped between"
);
let source = "template<bool B> struct C { };\n\
template<typename T> struct S : public C<T(0) < T(0)> { };\n";
assert_eq!(
reads(source, Where::File),
Ok(()),
"a comparison in a base clause reads"
);
assert_eq!(
count_of(source, CppSyntaxKind::BaseSpecifier),
1,
"and the base clause is a base clause"
);
for argument in [
"1 < 2",
"sizeof(int) < 3",
"(T(0) < T(0))",
"::std::vector<int>",
] {
let source = format!(
"template<bool B> struct C {{ }};\n\
template<typename T> struct S : public C<{argument}> {{ }};\n"
);
assert_eq!(
reads(&source, Where::File),
Ok(()),
"`{argument}` is one argument"
);
}
}
#[test]
fn a_condition_declaration_ends_at_the_paren_that_closes_the_condition() {
let source = "namespace NS { template<typename T> T fmod(T a, T b); }\n\
template<typename T> T f(T s) {\n\
if (NS::fmod(s,T(2)) == T(0))\n\
return T(0);\n\
return T(1);\n\
}\n";
assert_eq!(
reads(source, Where::File),
Ok(()),
"the call the file wrote is read as a call"
);
assert!(
contains(source, CppSyntaxKind::CallExpr),
"and the call is in the tree"
);
assert_eq!(
count_of(source, CppSyntaxKind::Initializer),
0,
"the condition has no initializer — the misreading was a *parameter* with a direct-initialiser, and that \
is the node it produced"
);
for condition in ["Foo* p = get()", "const auto n = g()", "Foo p = get()"] {
let source = format!(
"struct Foo {{ }};\n\
Foo* get();\n\
void h() {{ if ({condition}) {{ }} }}\n"
);
assert_eq!(
reads(&source, Where::File),
Ok(()),
"`if ({condition})` still reads"
);
assert_eq!(
count_of(&source, CppSyntaxKind::Initializer),
1,
"`if ({condition})` declares its variable, and that is the one initializer in the file"
);
}
}
#[test]
fn an_attribute_may_stand_between_the_star_and_the_name() {
let source = "extern void * __attribute__((__always_inline__)) __slwpcb (void) { }\n";
assert_eq!(reads(source, Where::File), Ok(()), "the declaration reads");
assert_eq!(
count_of(source, CppSyntaxKind::AttributeList),
1,
"the attribute is an attribute list, in its own node"
);
assert_eq!(
count_of(source, CppSyntaxKind::ParameterList),
1,
"and `(void)` is the declarator's parameter list, which is what makes this a function"
);
let source = "void * __attribute__((x)) f;\n";
assert_eq!(reads(source, Where::File), Ok(()), "`f` reads");
assert_eq!(
count_of(source, CppSyntaxKind::Initializer),
0,
"nothing here is an initializer — the attribute belongs to the pointer"
);
assert_eq!(
count_of(source, CppSyntaxKind::MacroCall),
0,
"and the name is the declarator's, not a macro standing after one"
);
let source = "void * const f (void) { }\n";
assert_eq!(reads(source, Where::File), Ok(()), "`* const` still reads");
assert_eq!(
count_of(source, CppSyntaxKind::AttributeList),
0,
"`const` is a qualifier, not an attribute"
);
}
#[test]
fn a_conditional_may_be_written_with_a_directive_before_its_colon() {
let source = "int f(int n) {\n\
return n > 2\n\
? 3\n\
#if 1\n\
: 4\n\
#endif\n\
;\n\
}\n";
assert_eq!(
reads(source, Where::File),
Ok(()),
"a directive between the two branches of a conditional is a seam"
);
assert_eq!(
count_of(source, CppSyntaxKind::TernaryExpr),
1,
"the conditional is still one conditional"
);
assert_eq!(
reads(
"int f(int n) {\n return n\n#if 1\n ? 1\n#endif\n : 2;\n}\n",
Where::File
),
Ok(()),
"a directive before the `?` is read by the expression's own seam"
);
let source = "int x =\n#if 1\n 1\n#else\n 2\n#endif\n ;\n";
assert_eq!(
reads(source, Where::File),
Ok(()),
"a value per branch reads"
);
assert_eq!(
count_of(source, CppSyntaxKind::LiteralExpr),
2,
"both branches' values are in the initializer — the expression handed the `#else` back"
);
}
#[test]
fn a_macro_invocation_at_the_head_of_a_declaration_is_a_specifier() {
let source = "template<typename T> struct B { };\n\
template<typename T>\n\
DEP_SUGGEST(\"std::bind\")\n\
inline B<T>\n\
f(const T& x)\n\
{ return B<T>(); }\n";
assert_eq!(
reads(source, Where::File),
Ok(()),
"an attribute macro with an argument list reads as a specifier, not as a name with a stray group"
);
assert_eq!(
count_of(source, CppSyntaxKind::MacroCall),
1,
"and the invocation is one node, with its arguments kept as tokens"
);
assert_eq!(
count_of(source, CppSyntaxKind::ArgumentList),
1,
"the group after the name is the macro's argument list"
);
let source = "int main() { CHECK(1); return 0; }\n";
assert_eq!(
reads(source, Where::File),
Ok(()),
"`CHECK(1);` still reads"
);
assert_eq!(
count_of(source, CppSyntaxKind::MacroCall),
0,
"a call whose group ends the statement is not a macro invocation standing for a specifier"
);
assert_eq!(
count_of(source, CppSyntaxKind::CallExpr),
1,
"it is the call the expression rules own"
);
}
#[test]
fn a_term_of_an_expression_may_be_written_once_per_branch() {
let source = "double f(double x) {\n\
double z = 1;\n\
z *= pow(x)\n\
#if 1\n\
* exp(x)\n\
#else\n\
* exp(-x)\n\
#endif\n\
/ pi();\n\
return z;\n\
}\n";
assert_eq!(
reads(source, Where::File),
Ok(()),
"a directive where the next operator goes is a seam"
);
assert_eq!(
count_of(source, CppSyntaxKind::BinaryExpr),
4,
"the chain is folded as one expression: `*=`, and the `*`, `*`, `/` of its right side"
);
assert_eq!(
count_of(source, CppSyntaxKind::PreprocessorDirective),
3,
"and all three directives are in the tree rather than swallowed"
);
let source = "bool f(bool a, bool b) {\n\
return ((a\n\
#if 1\n\
|| b\n\
#endif\n\
#if 1\n\
|| true\n\
#endif\n\
) && a);\n\
}\n";
assert_eq!(
reads(source, Where::File),
Ok(()),
"the closers are read before the `)` the caller expects"
);
}
#[test]
fn a_failed_cast_is_a_parenthesised_expression() {
let source = "template<typename T> struct V { };\n\
template<typename T> T g(T);\n\
template<typename T> bool f(T x) { bool b = (V<T>(g(x))); return b; }\n";
assert_eq!(
reads(source, Where::File),
Ok(()),
"a cast with no operand is a parenthesised expression"
);
assert_eq!(
count_of(source, CppSyntaxKind::ParenExpr),
1,
"and it is read as one"
);
let source = "template<typename T> bool f(T x) { bool b = (bool)(x); return b; }\n";
assert_eq!(reads(source, Where::File), Ok(()), "a real cast reads");
assert_eq!(
count_of(source, CppSyntaxKind::CastExpr),
1,
"and it is a cast"
);
}
#[test]
fn an_attribute_may_stand_between_a_declarator_ids_operators() {
let source = "bool operator== [[nodiscard]] (int a, int b) { return a == b; }\n";
assert_eq!(reads(source, Where::File), Ok(()), "the shape reads");
assert_eq!(
count_of(source, CppSyntaxKind::AttributeList),
1,
"the attribute is an attribute"
);
assert_eq!(
count_of(source, CppSyntaxKind::ParameterList),
1,
"and the `(int, int)` is the declarator's parameter list"
);
let source = "int x [[maybe_unused]] = 1;\n";
assert_eq!(
reads(source, Where::File),
Ok(()),
"the other position reads"
);
assert_eq!(
count_of(source, CppSyntaxKind::AttributeList),
1,
"still one attribute"
);
assert_eq!(
count_of(source, CppSyntaxKind::ParameterList),
0,
"and no parameter list was invented for a variable"
);
}
#[test]
fn a_conditional_inside_template_arguments_keeps_its_colon() {
let source = "enum class K { sized, unsized };\n\
template<typename A, typename B, K k> struct sub { };\n\
template<typename T> struct it { };\n\
using A = it<int>;\n\
using B = it<long>;\n\
auto f() -> sub<A, B, (true) ? K::sized : K::unsized>;\n";
assert_eq!(
reads(source, Where::File),
Ok(()),
"a conditional is an argument like any other value"
);
let source = "template<bool B> struct C { };\n\
struct S : C<true> { };\n";
assert_eq!(reads(source, Where::File), Ok(()), "a base clause reads");
assert_eq!(
count_of(source, CppSyntaxKind::BaseSpecifier),
1,
"and it is a base specifier"
);
}
#[test]
fn a_declarations_tail_may_be_written_once_per_branch() {
let cases = [
"void random_shuffle(_RAIter, _RAIter,\n#if X\n _RandomNumberGenerator&&);\n#else\n \
_RandomNumberGenerator&);\n#endif",
"using K = remove_const_t<\n#if X\n T<0, I>>;\n#else\n I::first_type>;\n#endif",
"int f(int n) {\n return n > 2\n ? 3\n#if X\n : 1 << (n - 1);\n#else\n : 1 << (n - 2);\n#endif\n}",
];
for source in cases {
assert_eq!(
reads(source, Where::File),
Ok(()),
"the tail written once per branch reads: {source:?}"
);
}
assert_eq!(
count_of(cases[0], CppSyntaxKind::ParameterList),
1,
"one parameter list, with both branches' spellings of the tail in it"
);
assert_eq!(
count_of(cases[0], CppSyntaxKind::Parameter),
4,
"and four parameters: `_RAIter` twice, then both spellings of the last one"
);
assert_eq!(
count_of(cases[1], CppSyntaxKind::TemplateArgumentList),
2,
"one argument list for the alias and one for the `T<0, I>` inside it"
);
assert_eq!(
count_of(cases[2], CppSyntaxKind::TernaryExpr),
1,
"one conditional, with both spellings of its `:` branch"
);
let source = "void random_shuffle(int __first, int __last,\n#if X\n long&& __rand)\n#else\n long& __rand)\n\
#endif\n{ }";
assert_eq!(
reads(source, Where::File),
Ok(()),
"a per-branch parameter list whose terminator is shared reads"
);
assert_eq!(
count_of(source, CppSyntaxKind::ParameterList),
1,
"one list, with both branches' spellings in it"
);
assert_eq!(
count_of(source, CppSyntaxKind::CompoundStat),
1,
"and the body after the `#endif` is still the function's body"
);
let source = "#if X\nvoid f(int a);\n#else\nvoid g(long a);\n#endif";
assert_eq!(
reads(source, Where::File),
Ok(()),
"a declaration per branch reads"
);
assert_eq!(
count_of(source, CppSyntaxKind::ParameterList),
2,
"with one parameter list in each branch"
);
}
#[test]
fn an_implementation_keyword_may_stand_where_an_operand_goes() {
let source = "template<typename T> int nd(T a, T b);\n\
void f(unsigned long a, unsigned long b, int& r) {\n\
r = __extension__ nd<unsigned long>(a, b);\n\
}\n";
assert_eq!(reads(source, Where::File), Ok(()), "the operand reads");
assert_eq!(
count_of(source, CppSyntaxKind::CallExpr),
1,
"and the call after the keyword is one call"
);
let source = "int g(int);\nvoid f(int a) { int r = g(a); }\n";
assert_eq!(reads(source, Where::File), Ok(()), "an ordinary call reads");
assert_eq!(count_of(source, CppSyntaxKind::CallExpr), 1, "as one call");
}
#[test]
fn a_qualified_head_is_a_type_even_when_it_ends_in_template_arguments() {
let source = "enum class K { a };\n\
template<typename T, typename U, K k> struct sub { };\n\
template<typename I, typename S, K k>\n\
inline constexpr bool n::is_sub<sub<I, S, k>> = true;\n";
assert_eq!(
reads(source, Where::File),
Ok(()),
"the qualified head is the definition's name, and `= true` initialises it"
);
assert_eq!(
count_of(source, CppSyntaxKind::Declaration),
3,
"the enum, the primary template and the specialization"
);
assert_eq!(
reads("void f(int n) { x = 1; }\n", Where::File),
Ok(()),
"an assignment reads"
);
assert_eq!(
count_of("void f(int n) { x = 1; }\n", CppSyntaxKind::Initializer),
0,
"and it is not a declaration with an initializer"
);
}
#[test]
fn a_run_of_annotations_may_stand_in_front_of_the_declaration_it_annotates() {
let source = "_Check_return_wat_\n_Success_(return == 0)\n_ACRTIMP errno_t __cdecl fopen_s(FILE** _Stream);\n";
assert_eq!(
shape_body_of_the_file_reads(source),
vec!["MacroCall:_Check_return_wat_", "Declaration:_Success_"],
"with no evidence, the run's remaining names are specifiers of the declaration"
);
assert_eq!(
shape_body_of_the_file_reads("_Check_return_ _Ret_notnull_ int f(void);\n"),
vec![
"MacroCall:_Check_return_",
"MacroCall:_Ret_notnull_",
"Declaration:int",
],
"a run with no group keeps the reading it already had"
);
assert_eq!(
shape_body_of_the_file_reads("_Check_return_ _Success_(x)\n"),
vec!["MacroCall:_Check_return_", "MacroCall:_Success_"],
"reserved names with a group and nothing after them are two invocations, not a run before a declaration"
);
}
#[test]
fn annotations_may_stand_in_runs_before_the_type_they_annotate() {
for source in [
"int f(_Inout_updates_opt_(BUFSIZ) _Post_readable_size_(0) char* _Buffer);\n",
"int f(Wat(1) Wobble(2) Qux(3) int x);\n",
"int f(Wat(1) char* x);\n",
"int f(Wat(1) Wobble char* x);\n",
] {
assert_eq!(reads(source, Where::File), Ok(()), "{source:?}");
}
assert_eq!(
shape_body_of_the_file_reads("WINOLEAPI_(HINSTANCE) CoFreeLibrary (HINSTANCE hInst);\n"),
vec!["Declaration:WINOLEAPI_"],
"a name after the group is the declaration's head, and its `(…)` is the parameter list"
);
assert!(
reads("int f(Wat Wobble(2) char const* _Format);\n", Where::File).is_err(),
"a bare annotation before a grouped one is the next shape, not this one"
);
}
#[test]
fn a_function_may_be_spelled_with_its_name_in_parentheses_behind_an_operator() {
for source in [
"const Wat&(max) (const Wat& _Left);\n",
"int &(f)(void);\n",
"int *(f)(void);\n",
"int &&(f)(void);\n",
"template <class _Ty> const _Ty&(max) (const _Ty& _Left, const _Ty& _Right) { return _Left; }\n",
] {
assert_eq!(reads(source, Where::File), Ok(()), "{source:?}");
}
assert_eq!(
shape_body_of_the_file_reads("const Wat&(max) (const Wat& _Left);\n"),
vec!["Declaration:const"],
"the operators, the parenthesized name and the parameter list are one declaration"
);
assert_eq!(
reads("Max(a);\n", Where::Body),
Ok(()),
"a call is still a call"
);
assert_eq!(
count_of("void g() { a * (b); }\n", CppSyntaxKind::ExpressionStat),
1,
"and a product is still an expression"
);
assert_eq!(reads("int *p(void);\n", Where::File), Ok(()));
assert_eq!(reads("int (*pf)(void);\n", Where::File), Ok(()));
}
#[test]
fn an_annotation_may_follow_a_specifier_the_loop_mistook_for_a_type() {
let source = "template <class T> _NODISCARD _Post_equal_to_(x) int f(T x) { return x; }\n";
let tree = CppParser::parse(source, ParserConfig::default());
assert_eq!(tree.to_source_text(), source, "losslessness");
assert!(
!tree.get_errors().is_empty(),
"an empty-bodied macro in front of a specifier is only readable if the parser knows the name is a macro"
);
{
let source = "template <class T> _Post_equal_to_(x) int f(T x) { return x; }\n";
let tree = CppParser::parse(source, ParserConfig::default());
assert_eq!(tree.get_errors(), [], "{source:?}");
}
assert_eq!(
shape_body_of_the_file_reads(
"_Check_return_wat_\n_Success_(return == 0)\n_ACRTIMP errno_t __cdecl fopen_s(FILE** _Stream);\n"
),
vec!["MacroCall:_Check_return_wat_", "Declaration:_Success_"],
"an identifier after the group keeps the run-of-invocations reading"
);
}
#[test]
fn a_form_feed_is_whitespace_and_not_a_character_error() {
for source in [
"\u{c}\r\nint x;\n",
"int \u{c} x;\n",
"#define A \u{c}\nint x;\n",
"int f(int a, \u{c}\nint b);\n",
"int \u{b} y;\n",
] {
let tree = CppParser::parse(source, ParserConfig::default());
assert_eq!(tree.to_source_text(), source, "losslessness: {source:?}");
assert_eq!(
tree.get_errors(),
[],
"a form feed is whitespace: {source:?}"
);
}
}
#[test]
fn a_replacement_list_is_not_cpp_text() {
for source in [
"#define API_SET_BY_ORDINAL(X,O,PO) X @##O NONAME PRIVATE\nint x;\n",
"#define A param\tt)\nint x;\n",
] {
let tree = CppParser::parse(source, ParserConfig::default());
assert_eq!(tree.to_source_text(), source, "losslessness: {source:?}");
assert_eq!(
tree.get_errors(),
[],
"a character C++ has no token for, inside a directive: {source:?}"
);
}
assert!(
!CppParser::parse("int @ x;\n", ParserConfig::default())
.get_errors()
.is_empty(),
"an `@` in an expression is a gap in the lexer and stays one"
);
assert!(
!CppParser::parse("int x = 1 \\ 2;\n", ParserConfig::default())
.get_errors()
.is_empty(),
"and so is a stray backslash outside a directive"
);
}
#[test]
fn a_requires_expression_may_be_a_template_argument() {
let source = "template<bool B> struct bool_constant { };\n\
template<typename T> struct is_scoped_enum\n\
: bool_constant<!requires(T t, void(*f)(int)) { f(t); }> { };\n";
assert_eq!(
reads(source, Where::File),
Ok(()),
"the requires-expression is an argument, and the base clause reads"
);
assert_eq!(
count_of(source, CppSyntaxKind::BaseSpecifier),
1,
"as a base specifier"
);
assert_eq!(
count_of(source, CppSyntaxKind::RequiresExpr),
1,
"with the requires-expression in the tree"
);
assert!(
reads(
"template<bool B> struct C { };\nstruct S : C<true>;\n",
Where::File
)
.is_err(),
"a class head with no body is still an error"
);
}
#[test]
fn an_alias_may_carry_an_attribute_macro_before_its_equals() {
let source = "template<class T> struct storage { using type = T; };\n\
template<class T>\n\
using storage_t DEPRECATED23\n\
= typename storage<T>::type;\n";
assert_eq!(reads(source, Where::File), Ok(()), "the alias reads");
assert_eq!(
count_of(source, CppSyntaxKind::MacroCall),
1,
"and the macro between the name and the `=` is read as an invocation"
);
let source = "using T [[deprecated]] = int;\n";
assert_eq!(reads(source, Where::File), Ok(()), "the attribute reads");
assert_eq!(
count_of(source, CppSyntaxKind::AttributeList),
1,
"it is an attribute list"
);
assert_eq!(
count_of(source, CppSyntaxKind::MacroCall),
0,
"and not a macro invocation"
);
}
#[test]
fn a_macro_may_supply_the_rest_of_a_template_parameter_list() {
let source =
"template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>\nstruct S;\n";
assert_eq!(
reads(source, Where::File),
Ok(()),
"a macro-shaped name may stand where the `,` of the list goes"
);
assert!(
contains(source, CppSyntaxKind::MacroCall),
"and it reads as an invocation, not as the parameter's name"
);
assert!(
reads("template<typename... _Args foo> struct S;\n", Where::File).is_err(),
"an ordinary name there is not a macro"
);
assert!(
reads("template<typename... _Args int> struct S;\n", Where::File).is_err(),
"a keyword there is not a macro either"
);
}
#[test]
fn a_literal_operator_may_be_spelled_with_a_space_before_its_suffix() {
let spaced = "constexpr int operator\"\" _km(double) noexcept { return 0; }\n";
assert_eq!(
reads(spaced, Where::File),
Ok(()),
"the spaced spelling is the same name as the unspaced one"
);
let tree = CppParser::parse(spaced, ParserConfig::default());
let named: Vec<String> = tree
.get_red_root()
.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::NameExpr)
.map(|node| node.text().to_string())
.collect();
assert!(
named.iter().any(|name| name == "operator\"\" _km"),
"the operator's name is `operator\"\" _km`, suffix and all: {named:?}"
);
assert!(
named.iter().all(|name| !name.contains("noexcept")),
"and nothing after the parameter list is inside it: {named:?}"
);
let unspaced = "constexpr int operator\"\"_km(double) noexcept { return 0; }\n";
assert_eq!(reads(unspaced, Where::File), Ok(()));
assert_eq!(
reads(
"int operator\"\" _km(double) noexcept { return 0; }",
Where::Class
),
Ok(())
);
}
#[test]
fn a_template_declaration_may_spell_its_functions_name_in_parentheses() {
let declaration = "template <class _Ty> constexpr _Ty (max)(_Ty a);\n";
assert_reads(Where::File, &[declaration]);
assert_eq!(count_of(declaration, CppSyntaxKind::Declaration), 1);
let definition = "template <class _Ty> constexpr _Ty (max)(_Ty a) { return a; }\n";
assert_reads(Where::File, &[definition]);
assert_reads(Where::File, &["Widget w(1, 2);\n"]);
assert_reads(Where::Body, &["g(1, 2);\n"]);
assert_reads(Where::File, &["int x = a * (b);\n"]);
let knr = "int main(argc, argv)\nint argc;\nchar **argv;\n{\n return 0;\n}\n";
assert_reads(Where::File, &[knr]);
assert_eq!(
count_of(knr, CppSyntaxKind::OldStyleParameterList),
1,
"a K&R parameter list is still a parameter list"
);
}
#[test]
fn an_abstract_declarator_may_carry_a_calling_convention_before_its_operator() {
assert_reads(
Where::File,
&[
"typedef void (__cdecl *)(void);\n",
"typedef int (__stdcall *)(int);\n",
"void (__cdecl *)(void);\n",
"typedef void (*)(void);\n",
"typedef void (__cdecl * p)(void);\n",
"typedef DWORD (WINAPI PM_OPEN_PROC)(LPWSTR);\n",
],
);
}
#[test]
fn a_template_argument_may_be_an_expression_whose_left_operand_is_a_type() {
assert_reads(
Where::File,
&[
"template <class D> constexpr bool v = F<g<D> == 1>;\n",
"template <class D> constexpr bool v = F<g<D>::value == 1>;\n",
"template <class D, class... S> constexpr bool v = F<g<D> == sizeof...(S), D, S...>;\n",
"template <class T> using p = S<T*>;\n",
"template <class T> using r = S<T&&>;\n",
"S<int> x;\n",
"S<A<B>> y;\n",
"bool b = n < 0 || n > 100000;\n",
],
);
}
#[test]
fn a_calling_convention_may_decorate_an_abstract_declarator_in_an_alias() {
assert_reads(
Where::File,
&[
"using h = void (__cdecl *)();\n",
"using h = void (__cdecl *)(void);\n",
"using h = void (__stdcall *)(int);\n",
"typedef void (__cdecl *)(void);\n",
"template <borrowed_range _Rng> void f(_Rng&&);\n",
"void g(_Rng&&);\n",
"template <class T> void h(T&& t);\n",
],
);
}
#[test]
fn a_namespace_definition_may_carry_an_attribute_before_its_name() {
assert_reads(
Where::File,
&[
"namespace [[deprecated]] tr1 { }\n",
"namespace [[deprecated(\"warning STL4002: gone\")]] tr1 { }\n",
"inline namespace [[deprecated(\"v1 is old\")]] v1 { }\n",
"namespace [[deprecated]] { int hidden; }\n",
"namespace [[deprecated]] a::b { }\n",
"namespace [[gnu::visibility(\"default\")]] c { }\n",
"namespace plain { }\n",
"namespace std _GLIBCXX_VISIBILITY(default) { }\n",
"namespace fs = std::filesystem;\n",
"namespace a::b { }\n",
],
);
let attributed = "namespace [[deprecated(\"x\")]] tr1 { }\n";
assert_eq!(count_of(attributed, CppSyntaxKind::NamespaceDecl), 1);
assert_eq!(count_of(attributed, CppSyntaxKind::AttributeList), 1);
}
#[test]
fn a_pointer_operator_may_carry_an_unexpanded_macro_before_the_name() {
assert_reads(
Where::File,
&[
"typedef struct S { int a; } S, *PS, *RESTRICTED_POINTER PRS;\n",
"typedef struct S { int a; } S, *HUGEP PS;\n",
"typedef struct S { int a; } S, *_SAL_ANNOTATION PS;\n",
"void f(int *RESTRICTED_POINTER p);\n",
"struct S { int *RESTRICTED_POINTER member; };\n",
"int *p;\n",
"int * const q = 0;\n",
"char **argv;\n",
"int *__cdecl _errno(void);\n",
"typedef void (__cdecl *)(void);\n",
"int * *pp;\n",
],
);
let names = |source: &str| -> Vec<String> {
let tree = CppParser::parse(source, ParserConfig::default());
tree.get_red_root()
.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::NameExpr)
.map(|node| node.text().to_string().trim().to_string())
.collect()
};
let read = names("typedef struct S { int a; } S, *PS, *RESTRICTED_POINTER PRS;\n");
assert!(
read.contains(&"PRS".to_string()),
"the declarator is named `PRS`: {read:?}"
);
assert!(
!read.contains(&"RESTRICTED_POINTER".to_string()),
"the macro is not a declarator's name: {read:?}"
);
let plain = names("int * p;\n");
assert_eq!(plain, vec!["p".to_string()]);
}
#[test]
fn a_parameter_type_may_be_spelled_like_an_implementation_macro() {
assert_reads(
Where::File,
&[
"struct C { }; void C::g(C& r) { }\n",
"struct C { }; void C::g(T& r) { }\n",
"struct C { }; void C::g(_Container_base12& _Right) noexcept { }\n",
"struct C { }; void C::g(C& r, C& s) { }\n",
"struct C { }; void C::g(C* r) { }\n",
"struct C { }; void C::g(C&);\n",
"template <class T> void f(T& x) { }\n",
"void h(_Ty& value);\n",
"void i(int& r) { }\n",
"void j(const C& r) { }\n",
"void k(C&) { }\n",
"typedef DWORD (WINAPI PM_OPEN_PROC)(LPWSTR);\n",
"typedef void (*OldCallback)(int);\n",
"typedef int (C::*fp)(int);\n",
],
);
}
#[test]
fn a_pointer_before_a_parenthesised_declarator_ends_at_its_semicolon() {
let first_kind = |declaration: &str| {
if declaration.starts_with("typedef") {
CppSyntaxKind::TypedefDecl
} else {
CppSyntaxKind::Declaration
}
};
for declaration in [
"typedef char *(*LineGetter)(int, void *, int, bool);",
"char *(*pick)(int);",
"void *(*start)(void *, unsigned long);",
"int *(*allocate)(size_t);",
] {
let source = format!("{declaration}\nint after;\n");
let tree = CppParser::parse(&source, ParserConfig::default());
let root = tree.get_red_root();
let top: Vec<CppSyntaxKind> = root
.children()
.map(|child| CppSyntaxKind::from(child.kind()))
.collect();
assert_eq!(
top,
[first_kind(declaration), CppSyntaxKind::Declaration],
"`{declaration}` is one declaration, and `int after;` is the next: {top:?}"
);
let written = root
.children()
.next()
.expect("the typedef")
.text()
.to_string();
assert_eq!(
written.trim(),
declaration,
"the declaration is exactly what was written"
);
let names: Vec<String> = root
.children()
.next()
.expect("the typedef")
.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::NameExpr)
.map(|node| node.text().to_string())
.collect();
assert_eq!(
names.len(),
1,
"one name, and it is the declarator's: {names:?}"
);
}
}
#[test]
fn a_macro_after_a_parameter_keeps_the_function_a_declaration() {
let source = "void ex_argdedupe(exarg_T *eap FUNC_ATTR_UNUSED) {\n for (int i = 0; i < 2; i++) { }\n}\nint after;\n";
let tree = CppParser::parse(source, ParserConfig::default());
let root = tree.get_red_root();
let top: Vec<CppSyntaxKind> = root
.children()
.map(|child| CppSyntaxKind::from(child.kind()))
.collect();
assert_eq!(
top,
[CppSyntaxKind::Declaration, CppSyntaxKind::Declaration],
"the function is a declaration and `int after;` is the next one: {top:?}"
);
let function = root.children().next().expect("the function");
assert!(
function
.text()
.to_string()
.starts_with("void ex_argdedupe("),
"the declaration is the function: {:?}",
function.text().to_string()
);
let parameter = function
.descendants()
.find(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::Parameter)
.expect("the function has a parameter");
assert_eq!(
parameter.text().to_string(),
"exarg_T *eap FUNC_ATTR_UNUSED",
"the parameter holds the attribute macro rather than losing it"
);
assert!(
parameter
.descendants()
.any(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::MacroCall),
"and the macro is read as one"
);
}
#[test]
fn a_class_head_may_be_longer_than_a_lookahead_window() {
assert_reads(
Where::File,
&[
"template <class _Ty, size_t... _Kx, size_t... _Ix, size_t _Ix_next, size_t... _Kx_next, class... _Rest>\n\
struct _Tuple_cat2<_Ty, index_sequence<_Kx...>, index_sequence<_Ix...>, _Ix_next, \
index_sequence<_Kx_next...>, _Rest...>\n\
\x20 : _Tuple_cat2<_Ty, index_sequence<_Kx..., _Kx_next...>,\n\
\x20 index_sequence<_Ix..., (_Ix_next + 0 * _Kx_next)...>,\n\
\x20 _Ix_next + 1, _Rest...> {};\n",
"template <class T, int... K, int... I, int N, int... KN, class... R>\n\
struct S<T, I2<K...>, I2<I...>, N, I2<KN...>, R...>\n\
\x20 : S<T, I2<K..., KN...>, I2<I..., (N + 0 * KN)...>, N + 1, R...> {};\n",
"template <class T> struct S<T> : B<C<1>, C<2>> { };\n",
"template <class T, class... R> struct S<T, R...> : B<C<T>, C<R...>> { };\n",
"template <class T, int N> struct S;\n",
"template <class T> struct S { using type = T; };\n",
"struct Plain : Base { };\n",
],
);
}
#[test]
fn a_calling_convention_may_precede_a_pointer_to_members_class_name() {
assert_reads(
Where::File,
&[
"void h(int (__cdecl A::*)(int));\n",
"typedef int (__cdecl A::*fp)(int);\n",
"void h(int (__stdcall A::*)(int));\n",
"template <class R, class A, class... T> struct X<R (__cdecl A::*)(T...)> { };\n",
"void h(int (C::*)(int));\n",
"typedef int (C::*fp)(int);\n",
"typedef int (C::*fp)(int) const;\n",
"void h(int (C::*p)(int));\n",
"template <class R, class A, class... T> struct X<R (__cdecl A::*)(T...) &> { };\n",
"using m = void (__cdecl A::*)() &;\n",
"using m = void (__cdecl A::*)(int) const;\n",
],
);
}
#[test]
fn a_comparison_inside_parentheses_is_not_an_angle_bracket() {
assert_reads(
Where::File,
&[
"template <size_t _First, size_t _Second, size_t... _Rest>\n\
struct _Maximum<_First, _Second, _Rest...> : _Maximum<(_First < _Second ? _Second : _First), \
_Rest...>::type {\n};\n",
"struct S : M<(A < B ? B : A), R...>::type { };\n",
"M<(A < B ? B : A)>::type x;\n",
"struct S : M<(A + B), R...>::type { };\n",
"struct S : M<A, R...>::type { };\n",
"struct S : M<(A ? B : C)>::type { };\n",
"C<1 < 2> c;\n",
"C<(T(0) < T(0))> c;\n",
"C<sizeof(T) < 3> c;\n",
],
);
}
#[test]
fn a_parse_error_follows_the_locale() {
let source = "int f() { return 1 }\n";
let errors = |locale: &str| {
cpp_parser::set_locale(locale);
let tree = cpp_parser::CppParser::parse(source, cpp_parser::ParserConfig::default());
tree.get_errors()
.iter()
.map(|error| error.message.clone())
.collect::<Vec<_>>()
};
let english = errors("en");
let chinese = errors("zh-CN");
cpp_parser::set_locale("en");
println!("en: {english:?}\nzh: {chinese:?}");
assert!(!english.is_empty(), "the fixture is a syntax error");
assert!(
english.iter().any(|message| message.contains("expected")),
"{english:?}"
);
assert!(
chinese.iter().any(|message| message.contains("需要")),
"the parse error is translated: {chinese:?}"
);
}
#[test]
fn a_nested_namespace_definition_may_spell_inline_on_an_inner_segment() {
for (source, name) in [
("namespace a::b::inline c { }", "a::b::c"),
("namespace a::inline b::inline c { int x; }", "a::b::c"),
(
"export namespace std::inline literals::inline chrono_literals { }",
"std::literals::chrono_literals",
),
("namespace a::b::c { }", "a::b::c"),
("inline namespace a::b { }", "a::b"),
] {
let tree = cpp_parser::CppParser::parse(source, cpp_parser::ParserConfig::default());
assert!(
tree.get_errors().is_empty(),
"{source}: {:?}",
tree.get_errors()
);
let root = tree.get_red_root();
let declaration = root
.descendants()
.find(|node| {
cpp_parser::CppSyntaxKind::from(node.kind())
== cpp_parser::CppSyntaxKind::NamespaceDecl
})
.unwrap_or_else(|| panic!("{source}: no NamespaceDecl"));
let name_node = declaration
.children()
.find(|node| {
cpp_parser::CppSyntaxKind::from(node.kind()) == cpp_parser::CppSyntaxKind::NameExpr
})
.unwrap_or_else(|| panic!("{source}: no NameExpr"));
let written: Vec<String> = name_node
.descendants_with_tokens()
.filter_map(|element| element.into_token())
.filter(|token| {
cpp_parser::CppTokenKind::from(token.kind()) == cpp_parser::CppTokenKind::Identifier
})
.map(|token| token.text().to_string())
.collect();
assert_eq!(written.join("::"), name, "{source}");
}
}
#[test]
fn a_templated_base_is_one_base_specifier() {
for (source, expected) in [
("struct D : B<T, U> { };", 1usize),
("struct D : A, B<T> { };", 2),
("struct D : protected B<T, U>, public C { };", 2),
("template<class T> struct D : B<T, T*> { };", 1),
("struct D : B { };", 1),
("struct D : virtual public B { };", 1),
("struct D { };", 0),
] {
let tree = cpp_parser::CppParser::parse(source, cpp_parser::ParserConfig::default());
assert!(
tree.get_errors().is_empty(),
"{source}: {:?}",
tree.get_errors()
);
let bases = tree
.get_red_root()
.descendants()
.filter(|node| {
cpp_parser::CppSyntaxKind::from(node.kind())
== cpp_parser::CppSyntaxKind::BaseSpecifier
})
.count();
assert_eq!(bases, expected, "{source}");
}
}