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;"),
("__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_in_a_declaration_and_in_a_cast_parses() {
use cpp_parser::features::LanguageConfig;
let language = LanguageConfig::from_flag("-std=c23").expect("a flag");
let mut failed = Vec::new();
for (name, source) in [
(
"a declaration and a cast",
"void f(void) { _BitInt(37) wide = (_BitInt(37))1 << 30; (void)wide; }",
),
(
"with an unsigned specifier in front",
"void f(void) { unsigned _BitInt(7) small = 100; (void)small; }",
),
(
"a bit-field in a struct",
"struct bits { unsigned _BitInt(3) three : 3; _BitInt(13) signed_wide : 13; };",
),
] {
let config = cpp_parser::ParserConfig::default().with_language(language);
let tree = cpp_parser::CppParser::parse(source, config);
if !tree.get_errors().is_empty() {
failed.push(format!("{name}: {:?}", tree.get_errors()));
}
}
assert!(failed.is_empty(), "C23 _BitInt: {failed:#?}");
}