use cpp_parser::{CppParser, CppSyntaxKind, CppSyntaxTree, ParserConfig};
fn tree(source: &str) -> CppSyntaxTree {
CppParser::parse(source, ParserConfig::default())
}
fn parses(source: &str) {
let parsed = tree(source);
assert_eq!(
parsed.get_errors(),
[],
"{source:?} must parse cleanly, got {:?}",
parsed.get_errors()
);
let unclaimed = parsed.get_red_root().descendants().any(|node| {
matches!(
CppSyntaxKind::from(node.kind()),
CppSyntaxKind::ErrorNode | CppSyntaxKind::MissingNode
)
});
assert!(!unclaimed, "{source:?} leaves an ErrorNode behind");
assert_eq!(parsed.to_source_text(), source, "{source:?} stays lossless");
}
fn count(source: &str, kind: CppSyntaxKind) -> usize {
tree(source)
.get_red_root()
.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == kind)
.count()
}
fn text_of(source: &str, kind: CppSyntaxKind) -> String {
tree(source)
.get_red_root()
.descendants()
.find(|node| CppSyntaxKind::from(node.kind()) == kind)
.unwrap_or_else(|| panic!("no {kind:?} in {source:?}"))
.text()
.to_string()
}
#[test]
fn a_define_in_the_file_does_not_change_a_reading() {
for with_define in [
"void f() {\n NUMBER_OPTION(tab_width)\n}\n",
"#define NUMBER_OPTION(op) if (auto v = Get(op); !v.empty()) { }\nvoid f() {\n NUMBER_OPTION(tab_width)\n}\n",
] {
assert_ne!(
tree(with_define).get_errors(),
[],
"{with_define:?}: a body that is a whole statement is what would make this legal, and reading it is \
the preprocessor's job"
);
}
for with_define in [
"void f() {\n FOO(x);\n}\n",
"#define FOO(a) a\nvoid f() {\n FOO(x);\n}\n",
] {
parses(with_define);
}
}
#[test]
fn the_declaration_level_shape_reads_without_any_table() {
parses("TEST(A, B) { }");
parses("TEST(FormatPerformance, 1k_row) { int x = 1; }");
parses("namespace n { TEST(A, B) { } }");
assert_eq!(count("TEST(A, B) { }", CppSyntaxKind::CompoundStat), 1);
assert_ne!(
tree("void f() {\n g(x) { }\n}\n").get_errors(),
[],
"a lowercase name followed by a block is a call with its `;` missing"
);
}
#[test]
fn a_macros_arguments_are_raw_tokens() {
for source in [
"TEST(1k_row, x.y) { }",
"TEST(std::vector<int> *) { }",
"TEST(a, b, c) { }",
] {
parses(source);
assert_eq!(
count(source, CppSyntaxKind::ArgumentList),
1,
"{source:?}: the group is kept whole"
);
assert_eq!(
count(source, CppSyntaxKind::ParameterList),
0,
"{source:?}: and nothing tried to read it as parameters"
);
}
assert_eq!(
text_of(
"TEST(FormatPerformance, 1k_row) { }",
CppSyntaxKind::ArgumentList
)
.trim_end(),
"(FormatPerformance, 1k_row)",
"…and the trivia the group swallowed is the only thing trimmed"
);
}
#[test]
fn a_defined_name_is_still_just_a_name() {
parses("#define FOO(a) a\nvoid f() {\n FOO(x);\n}\n");
parses("#define FOO(a) a\n#undef FOO\nvoid f() {\n FOO(x);\n}\n");
let with = tree("#define asm(x) g(x)\nvoid f() { asm(1); }\n");
let without = tree("void f() { asm(1); }\n");
assert_eq!(
with.get_red_root()
.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::AsmStat)
.count(),
without
.get_red_root()
.descendants()
.filter(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::AsmStat)
.count(),
"the directive makes no difference to the reading"
);
assert_eq!(
text_of(
"#define asm(x) g(x)\nvoid f() { asm(1); }\n",
CppSyntaxKind::AsmStat
)
.trim_end(),
"asm(1);",
"the trivia after the `;` belongs to the enclosing node, so a node's text may end in it"
);
}