mod common;
use common::assert_all_parse;
const CONSTRUCTS: &[(&str, &str)] = &[
("__attribute__", "__attribute__((always_inline)) void f();"),
(
"__attribute__ after the declarator",
"int f() __attribute__((pure));",
),
("__typeof__", "void f() { __typeof__(1) n = 1; }"),
("typeof", "void f() { typeof(1) n = 1; }"),
("__extension__", "__extension__ typedef int T;"),
("an asm block", "void f() { __asm__ volatile (\"nop\"); }"),
("__int128", "__int128 big = 0;"),
(
"__real__ in an expression",
"void f() { double d = __real__ 1.0; }",
),
(
"__imag__ in an expression",
"void f() { double d = __imag__ 1.0; }",
),
("__restrict__", "void f(int* __restrict__ a);"),
(
"a case range",
"void f(int n) { switch (n) { case 1 ... 5: break; default: break; } }",
),
(
"__builtin_expect",
"void f(int n) { if (__builtin_expect(n, 1)) { return; } }",
),
("#include_next", "#include_next <stdio.h>\n"),
(
"__asm__ as a declaration's specifier",
"__asm__(\".globl f\");",
),
];
#[test]
fn every_gnu_extension_parses_without_an_error() {
assert_all_parse("GNU", CONSTRUCTS);
}
#[test]
fn the_gnu_gaps_are_named() {
assert_all_parse(
"GNU gaps",
&[
(
"a statement expression",
"void f() { int n = ({ int a = 1; a; }); (void)n; }",
),
(
"an elvis operator",
"void f(int a, int b) { int n = a ?: b; (void)n; }",
),
],
);
}
#[test]
fn a_computed_goto_and_a_negative_case_range_parse() {
assert_all_parse(
"GNU control flow",
&[
(
"the address of a label",
"void f(void) { static void *const table[] = {&&here}; goto *table[0]; here: return; }",
),
(
"a case range with a negative end",
"void f(int n) { switch (n) { case -5 ... -1: break; default: break; } }",
),
],
);
}
#[test]
fn an_array_typed_compound_literal_parses() {
assert_all_parse(
"compound literals",
&[
(
"an array type",
"void f(void) { const char *text = (const char[]){\"compound\"}; (void)text; }",
),
(
"an array of ints, subscripted",
"void f(int index) { int n = ((int[]){1, 2})[index]; (void)n; }",
),
],
);
}
#[test]
fn a_typeof_specifier_takes_a_type() {
assert_all_parse(
"typeof",
&[
(
"typeof with an elaborated type",
"void f(void) { typeof(struct point) origin = {3, 4}; (void)origin; }",
),
(
"__typeof__ with a qualified type",
"void f(void) { __typeof__(const int) frozen = 1; (void)frozen; }",
),
(
"typeof with an expression",
"void f(int value) { typeof(value) copy = value; (void)copy; }",
),
],
);
}
#[test]
fn a_bitint_type_is_read_as_a_type_in_every_standard() {
use cpp_parser::features::LanguageConfig;
use cpp_parser::{CppAstNode, CppTranslationUnit};
const BITS: &str = "\
struct bits {
unsigned _BitInt(3) three : 3;
_BitInt(13) signed_wide : 13;
_BitInt(5) plain;
unsigned _BitInt(4) other : 2;
};
";
let mut languages: Vec<(&str, Option<LanguageConfig>)> = vec![("the default", None)];
for standard in ["c99", "c11", "c17", "c23", "c++17", "gnu2x"] {
languages.push((
standard,
LanguageConfig::from_flag(&format!("-std={standard}")),
));
}
for (name, language) in languages {
let mut config = cpp_parser::ParserConfig::default();
if let Some(language) = language {
config = config.with_language(language);
}
let tree = cpp_parser::CppParser::parse(BITS, config);
assert!(
tree.get_errors().is_empty(),
"{name}: the struct did not parse: {:?}",
tree.get_errors()
);
let root = CppTranslationUnit::cast(tree.get_red_root()).expect("a translation unit");
let declaration = root.get_declarations().next().expect("the struct");
let class = declaration.get_class_def().expect("a struct definition");
let members: Vec<String> = class
.get_body()
.expect("a body")
.get_members()
.filter_map(|member| member.get_name_text())
.collect();
assert_eq!(
members,
vec!["three", "signed_wide", "plain", "other"],
"{name}: the bit-fields are members"
);
}
}
#[test]
fn dump_a_function_pointer_declaration() {
let source = "void f() {\n int (*fp)(int) = negate;\n}\n";
let tree = cpp_parser::CppParser::parse(source, cpp_parser::ParserConfig::default());
fn walk(node: &cpp_parser::CppSyntaxNode, depth: usize) {
eprintln!(
"{}{:?} {:?}",
" ".repeat(depth),
node.kind(),
node.text().to_string().chars().take(28).collect::<String>()
);
for child in node.children() {
walk(&child, depth + 1);
}
}
walk(&tree.get_red_root(), 0);
}
#[test]
fn the_complex_operators_do_not_swallow_the_rest_of_the_class() {
use cpp_parser::CppAstNode;
let text = "struct C {\n float real() const { return __real__ 1; }\n float imag() const { return __imag__ 1; }\n void next();\n};\n";
let tree = cpp_parser::CppParser::parse(text, cpp_parser::ParserConfig::default());
let names: Vec<String> = tree
.get_red_root()
.descendants()
.filter_map(cpp_parser::CppDeclaration::cast)
.filter_map(|declaration| declaration.get_name_text())
.collect();
assert!(
names.iter().any(|name| name == "imag"),
"the member after the first `__real__` survives: {names:?}"
);
assert!(
names.iter().any(|name| name == "next"),
"and so does the one after that: {names:?}"
);
}