cpp_parser 0.4.0

A lossless C++ parser over the preprocessed text: the tree every layer above it reads.
Documentation
//! **GNU extensions** — what `-std=gnu++` accepts and a project's headers use.
//!
//! Three of the fourteen do not parse today; they are in the ignored test at the bottom, so the table above stays a
//! statement of what does, and the gaps are a statement of what does not.

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);
}

/// **The GNU constructs that used to be gaps** — now part of the table, kept as their own test because each was a
/// deliberate decision rather than a spelling.
///
/// ```text
///   int n = ({ int a = 1; a; });   statement expression: a compound statement where an expression would be
///   typeof(1) n = 1;               `typeof` is lexed as `decltype`
///   int n = a ?: b;                the elvis operator: the middle operand is the condition, written once
/// ```
///
/// The first and the third are what glibc's and the Linux headers' macros are built on, so a parse of a real
/// project on a GNU toolchain meets them. The statement expression is read as the **compound statement it is**, and
/// the elvis keeps its two operands with no middle one — nothing is synthesised into the tree, because the value
/// `a ?: b` yields is `a` and a copy of its tokens would be text the file wrote once.
#[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; }",
            ),
        ],
    );
}

/// **The corpus's own GNU shapes** — a label as a value, a computed `goto`, and a case range with a negative end.
///
/// ```c
/// static void *const TABLE[] = {&&state_zero, &&state_one};   // `&&label`: the address of a label
/// goto *TABLE[state];                                        // computed `goto`
/// case -5 ... -1:                                            // a range whose left end is negative
/// ```
///
/// Measured on a nine-file C corpus: four syntax errors in `gnu.c`, every one of them one of these three shapes (and
/// the fourth a `typeof(struct point)`, which the other test covers).
#[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; } }",
            ),
        ],
    );
}

/// **An array-typed compound literal** — `(const char[]){"compound"}` and `(int[]){1, 2}`.
///
/// C99's compound literal with an *array* type: the type name ends in `[]`, and the braced list that follows is the
/// value. The object form (`(point){.x = 3}`) has been read since neovim's `CSTR_AS_OBJ` was reported, so the array
/// spelling is the part that was missing — measured on a nine-file C corpus, `aggregates.c`'s one syntax error, and the
/// reason every member of that file went unoffered.
#[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; }",
            ),
        ],
    );
}

/// **`typeof` and `__typeof__` take a *type*, not only an expression** — C23 and GNU.
///
/// ```c
/// typeof(struct point) origin = {3, 4};
/// __typeof__(const int) frozen = 1;
/// typeof(value) copy = value;          // the expression form, which already parsed
/// ```
///
/// The specifier rule read `decltype`'s payload as an expression, and a `struct` cannot begin one, so the declaration
/// failed — measured on a nine-file C corpus, six syntax errors over two files (`c23.c`, `gnu.c`), and with them every
/// member and type answer about those files.
#[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; }",
            ),
        ],
    );
}

/// **`_BitInt` is read in every C mode, and in C++ too.**
///
/// ```c
/// struct bits {
///     unsigned _BitInt(3) three : 3;
///     _BitInt(13) signed_wide : 13;
///     _BitInt(5) plain;
///     unsigned _BitInt(4) other : 2;
/// };
/// ```
///
/// The grammar gated `_BitInt` on C23 alone, and no compiler does: measured with gcc 15, every one of these members
/// compiles with `-std=c99`, `-std=c11`, `-std=c17` and `-std=gnu89` (exit 0, no diagnostic), and clang 18 accepts the
/// same struct in `-std=c++23`. A project whose standard was **not detected** — no compile database, a header opened on
/// its own — was therefore read as a call: `_BitInt(3) three : 3` became an initialiser and a macro call, and the struct
/// had no members at all. Asked by a reader of such a project, and reproduced here under the default configuration and
/// under `-std=c17`.
///
/// The comment this replaces called the default reading "supposed to fail"; that was true of the gate and wrong about
/// the language.
#[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"
        );
    }
}

/// Doctor: a function-pointer declarator's tree.
#[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);
}

/// **A body using `__real__`/`__imag__` must not swallow the rest of the class.**
///
/// They are GCC's unary operators for `_Complex`, and libstdc++ writes them in `<complex>`'s members:
///
/// ```cpp
/// constexpr float real() const { return __real__ _M_value; }
/// ```
///
/// To this parser they were ordinary identifiers, so `return __real__ _M_value;` was a return of `__real__` followed
/// by a second expression, and the recovery from that error **swallowed the rest of the class body**. On the unit read
/// of `bits/std.cc` the class was `<complex>` and every file rendered after it lost its declarations — `iostream`'s
/// `std::cout` among them, which is what `docs/buglist.md` section 12 is about:
///
/// ```text
/// before:  [names] insert_a_reading .../iostream: declarations 0
/// after:   [names] insert_a_reading .../iostream: declarations 9
/// ```
///
/// The check is on the **later members**, not on the error count: a parser that recovers by swallowing still reports
/// one error, and the members are what a reader loses.
#[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:?}"
    );
}