big-code-analysis-ast 2.6.0

Parse and classification layer behind big-code-analysis (internal, version-locked)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
//! The Doxygen family: one `Comment` row, a `DocComment` under the marker
//! gate, and a `DocTarget` row that replaced a *string-keyed* list
//! (`lang_helpers::doxygen::doxygen_entry` matched `node.kind()`, so every
//! alias id of a rule matched for free). The rows list ids, and
//! `old_names_are_the_row` below holds each dialect's row to the id set
//! the old names denote — exactly, with a hand-pinned copy of those
//! names, so a moved alias cannot hide in `excludes`. `ERROR` is the
//! Checker's clause (the enum sentinel owns no row), pinned on recovery
//! fixtures through a walk that tolerates them. The two kind lists the
//! helpers still compare by name are held to the generated tables, per
//! dialect.

use super::*;
use crate::ParserTrait;
use std::collections::BTreeSet;
use std::path::Path;

/// The names `doxygen_entry` matched before #1699 (its `match
/// node.kind()` arms, plus the specifier and field arms it gated). A
/// hand-pinned copy, not read from the rows, so this is an oracle.
const OLD_ENTRY_NAMES: &[&str] = &[
    "declaration",
    "function_definition",
    "type_definition",
    "preproc_def",
    "preproc_function_def",
    "linkage_specification",
    "enumerator",
    "namespace_definition",
    "using_declaration",
    "alias_declaration",
    "friend_declaration",
    "template_declaration",
    "class_interface",
    "class_implementation",
    "protocol_declaration",
    "qualified_protocol_interface_declaration",
    "property_declaration",
    "method_declaration",
    "method_definition",
    "instance_variable",
    "struct_specifier",
    "union_specifier",
    "enum_specifier",
    "class_specifier",
    "field_declaration",
    "expression_statement",
    "implementation_definition",
];
/// The five conditional names `preproc_conditional` matched by name
/// until #1795, hand-pinned like [`OLD_ENTRY_NAMES`].
const OLD_CONDITIONAL_NAMES: &[&str] = &[
    "preproc_if",
    "preproc_ifdef",
    "preproc_else",
    "preproc_elif",
    "preproc_elifdef",
];

/// Every runtime kind id of `L`'s enum whose `kind()` string is one
/// of `names` — the set the old string compare denoted. Read from
/// the generated `From<Enum> for &str` through the flags sidecar,
/// which lists every id but the `ERROR` sentinel.
fn ids_named<E: From<u16> + Into<&'static str>>(
    names: &[&str],
    flags: &[(u16, bool, bool)],
) -> BTreeSet<u16> {
    flags
        .iter()
        .map(|(id, ..)| *id)
        .filter(|&id| names.contains(&E::from(id).into()))
        .collect()
}

/// Walks `source` with the real chain, error-recovered trees
/// included, and answers `(kind, text, row, checker)` for every node
/// the table names or that either reading answers true for.
#[cfg(feature = "c")]
fn targets_with_recovery<P: ParserTrait>(
    source: &[u8],
    row: fn(&Node<'_>, &[u8], Ancestors<'_, '_>) -> bool,
) -> Vec<(String, String, bool, bool)> {
    let parser = P::new(source.to_vec(), Path::new("fixture"), None);
    let root = parser.root();
    let mut chain: Vec<Node<'_>> = Vec::new();
    let mut stack = vec![(root, 0_usize)];
    let mut seen = Vec::new();
    while let Some((node, depth)) = stack.pop() {
        chain.truncate(depth);
        let ancestors = Ancestors::known(&chain);
        let r = row(&node, source, ancestors);
        let c = P::Checker::is_doc_target(&node, source, ancestors);
        if r || c {
            let text = node.utf8_text(source).unwrap_or("<non-utf8>");
            let text = text.lines().next().unwrap_or("").to_owned();
            seen.push((node.kind().to_owned(), text, r, c));
        }
        chain.push(node);
        let first = stack.len();
        stack.extend(node.children().map(|child| (child, depth + 1)));
        stack[first..].reverse();
    }
    seen
}

macro_rules! old_names_are_the_row {
    ($name:ident, $lang:ident, $roles:ident, $flags:ident) => {
        /// The id set the old names denote equals the row — the
        /// migration from strings to ids lost and gained nothing.
        #[test]
        fn $name() {
            let entries = ids_named::<crate::$lang>(OLD_ENTRY_NAMES, crate::$flags);
            let conditionals = ids_named::<crate::$lang>(OLD_CONDITIONAL_NAMES, crate::$flags);
            let row: BTreeSet<u16> = crate::roles::$roles::doc_target::OWNERS
                .iter()
                .copied()
                .collect();
            assert_eq!(
                row,
                &entries | &conditionals,
                "row minus old: {:?}; old minus row: {:?}",
                row.difference(&(&entries | &conditionals))
                    .collect::<Vec<_>>(),
                (&entries | &conditionals)
                    .difference(&row)
                    .collect::<Vec<_>>(),
            );
            assert!(!entries.is_empty() && !conditionals.is_empty());
        }
    };
}

#[cfg(feature = "c")]
old_names_are_the_row!(c_old_names_are_the_row, C, c, C_KIND_FLAGS);
#[cfg(feature = "cpp")]
old_names_are_the_row!(cpp_old_names_are_the_row, Cpp, cpp, CPP_KIND_FLAGS);
#[cfg(feature = "mozcpp")]
old_names_are_the_row!(
    mozcpp_old_names_are_the_row,
    Mozcpp,
    mozcpp,
    MOZCPP_KIND_FLAGS
);
#[cfg(feature = "objc")]
old_names_are_the_row!(objc_old_names_are_the_row, Objc, objc, OBJC_KIND_FLAGS);

macro_rules! old_names_are_the_conditional_kinds {
    ($name:ident, $lang:ident, $code:ident, $flags:ident) => {
        /// The conditional kind test `preproc_conditional` reads through
        /// the dialect's `DoxygenRow` binding (#1795) answers, over every
        /// runtime id, exactly the ids the old conditional names denote:
        /// every alias of the five rules and nothing else — the runtime
        /// `ERROR` id and the enum's sentinel included.
        #[test]
        fn $name() {
            use crate::lang_helpers::doxygen::DoxygenRow;
            let conditional = <crate::$code as DoxygenRow>::conditional_kind;
            let old = ids_named::<crate::$lang>(OLD_CONDITIONAL_NAMES, crate::$flags);
            let new: BTreeSet<u16> = crate::$flags
                .iter()
                .map(|(id, ..)| *id)
                .filter(|&id| conditional(id))
                .collect();
            assert_eq!(new, old);
            assert!(!old.is_empty());
            assert!(!conditional(u16::MAX) && !conditional(crate::$lang::Error as u16));
        }
    };
}

#[cfg(feature = "c")]
old_names_are_the_conditional_kinds!(
    c_old_names_are_the_conditional_kinds,
    C,
    CCode,
    C_KIND_FLAGS
);
#[cfg(feature = "cpp")]
old_names_are_the_conditional_kinds!(
    cpp_old_names_are_the_conditional_kinds,
    Cpp,
    CppCode,
    CPP_KIND_FLAGS
);
#[cfg(feature = "mozcpp")]
old_names_are_the_conditional_kinds!(
    mozcpp_old_names_are_the_conditional_kinds,
    Mozcpp,
    MozcppCode,
    MOZCPP_KIND_FLAGS
);
#[cfg(feature = "objc")]
old_names_are_the_conditional_kinds!(
    objc_old_names_are_the_conditional_kinds,
    Objc,
    ObjcCode,
    OBJC_KIND_FLAGS
);

const ALL_FOUR: &[&str] = &["c", "cpp", "mozcpp", "objc"];
const CXX_ONLY: &[&str] = &["cpp", "mozcpp"];
const OBJC_ONLY: &[&str] = &["objc"];

/// Each entry of `DOXYGEN_CONTAINERS` then `DOXYGEN_SKIP_KINDS`, in
/// order, with the dialects whose generated table holds it as a visible
/// kind. Hand-pinned, like [`OLD_ENTRY_NAMES`]: the four grammars are
/// pinned and bumped apart, so a kind one of them renames still names a
/// kind of the others, and only a per-dialect answer sees it go.
const DOXYGEN_KIND_DIALECTS: &[(&str, &[&str])] = &[
    ("translation_unit", ALL_FOUR),
    ("template_parameter_list", CXX_ONLY),
    ("declaration_list", ALL_FOUR),
    ("field_declaration_list", ALL_FOUR),
    ("enumerator_list", ALL_FOUR),
    ("class_interface", OBJC_ONLY),
    ("class_implementation", OBJC_ONLY),
    ("protocol_declaration", OBJC_ONLY),
    ("qualified_protocol_interface_declaration", OBJC_ONLY),
    ("instance_variables", OBJC_ONLY),
    ("preproc_include", ALL_FOUR),
    ("preproc_call", ALL_FOUR),
    ("static_assert_declaration", CXX_ONLY),
    ("namespace_alias_definition", CXX_ONLY),
    ("access_specifier", CXX_ONLY),
    ("alone_macro", &["mozcpp"]),
    (":", ALL_FOUR),
    (";", ALL_FOUR),
];

/// Every kind string `doxygen_container` and `doxygen_skips` compare
/// `node.kind()` against is a kind of exactly the dialects
/// [`DOXYGEN_KIND_DIALECTS`] pins for it — a visible id, so a hidden
/// `_rule` or an auxiliary `*_repeat1` does not count (#1834). A kind
/// renamed or dropped by one grammar's bump, or a typo, otherwise
/// matches nothing in that dialect in silence, and one a grammar gains
/// changes what that dialect binds. The other names these helpers
/// compare are pinned at build time: `expression_statement` and
/// `comment` are owners of the `DocTarget` and `Comment` rows, so a
/// rename fails to build the role tables, and `ERROR` is the runtime's
/// sentinel, which no grammar names.
#[test]
fn doxygen_kind_strings_name_an_emitted_kind() {
    use crate::lang_helpers::doxygen::{DOXYGEN_CONTAINERS, DOXYGEN_SKIP_KINDS};
    fn visible_kinds<E: From<u16> + Into<&'static str>>(
        flags: &[(u16, bool, bool)],
    ) -> BTreeSet<&'static str> {
        flags
            .iter()
            .filter(|&&(.., visible)| visible)
            .map(|&(id, ..)| E::from(id).into())
            .collect()
    }
    // In `ALL_FOUR`'s order, which the pinned sets are written in.
    let tables = [
        ("c", visible_kinds::<crate::C>(crate::C_KIND_FLAGS)),
        ("cpp", visible_kinds::<crate::Cpp>(crate::CPP_KIND_FLAGS)),
        (
            "mozcpp",
            visible_kinds::<crate::Mozcpp>(crate::MOZCPP_KIND_FLAGS),
        ),
        ("objc", visible_kinds::<crate::Objc>(crate::OBJC_KIND_FLAGS)),
    ];
    let listed: Vec<&str> = DOXYGEN_CONTAINERS
        .iter()
        .chain(DOXYGEN_SKIP_KINDS)
        .copied()
        .collect();
    let pinned: Vec<&str> = DOXYGEN_KIND_DIALECTS
        .iter()
        .map(|&(kind, _)| kind)
        .collect();
    assert_eq!(
        listed, pinned,
        "the two lists and DOXYGEN_KIND_DIALECTS name different kinds"
    );
    let drifted: Vec<String> = DOXYGEN_KIND_DIALECTS
        .iter()
        .filter_map(|&(kind, expected)| {
            let actual: Vec<&str> = tables
                .iter()
                .filter(|(_, kinds)| kinds.contains(kind))
                .map(|&(dialect, _)| dialect)
                .collect();
            (actual.is_empty() || actual != expected)
                .then(|| format!("{kind:?}: pinned to {expected:?}, a kind of {actual:?}"))
        })
        .collect();
    assert!(drifted.is_empty(), "{drifted:#?}");
}

// Every C entry kind with its gate's two sides, the three conditional
// forms with and without an entry, a nested conditional, a macro call
// and an unterminated one, and a doc comment in a body (Doxygen's
// in-body description: still a `DocComment`).
#[cfg(feature = "c")]
pub(super) const C_SOURCE: &[u8] = b"/** file */\n#include <stdio.h>\n/** macro */\n#define X 1\n/** fn macro */\n#define F(a) (a)\n/*! decl */\nint x;\n/// fn\nint f(void) { /** in body */ return x; }\n//! typedef\ntypedef int T;\n// plain\n/* block */\n/**/\n/** struct with body */\nstruct S { /** field */ int a; struct In; };\n/** forward */\nstruct Fwd;\nunion UF;\nenum EF;\n/** union */\nunion U { int b; };\n/** enum */\nenum E { /** enumerator */ A, B };\n/** macro call */\nFOO(bar);\n/** empty stmt */\n;\n#if X\n/** in if */\nint in_if;\n#elif Y\nint in_elif;\n#else\nint in_else;\n#endif\n#ifdef Z\n#endif\n#ifdef Y\n#elifdef YY\nint yy;\n#endif\n#if A\n# if B\nint deep;\n# endif\n#endif\nextern \"C\" { int c; }\n";

#[cfg(feature = "c")]
#[test]
fn c_rows_answer_the_table() {
    use crate::CCode;
    let source = C_SOURCE;
    let expected: &[(&str, &str, Answers)] = &[
        ("comment", "/** file */", DOC),
        ("comment", "/** macro */", DOC),
        ("preproc_def", "#define X 1", TARGET),
        ("comment", "/** fn macro */", DOC),
        ("preproc_function_def", "#define F(a) (a)", TARGET),
        ("comment", "/*! decl */", DOC),
        ("declaration", "int x;", TARGET),
        ("comment", "/// fn", DOC),
        (
            "function_definition",
            "int f(void) { /** in body */ return x; }",
            TARGET,
        ),
        ("comment", "/** in body */", DOC),
        ("comment", "//! typedef", DOC),
        ("type_definition", "typedef int T;", TARGET),
        ("comment", "// plain", PLAIN),
        ("comment", "/* block */", PLAIN),
        ("comment", "/**/", PLAIN),
        ("comment", "/** struct with body */", DOC),
        (
            "struct_specifier",
            "struct S { /** field */ int a; struct In; }",
            TARGET,
        ),
        ("comment", "/** field */", DOC),
        ("field_declaration", "int a;", TARGET),
        ("field_declaration", "struct In;", NONE),
        ("struct_specifier", "struct In", NONE),
        ("comment", "/** forward */", DOC),
        ("struct_specifier", "struct Fwd", NONE),
        ("union_specifier", "union UF", NONE),
        ("enum_specifier", "enum EF", NONE),
        ("comment", "/** union */", DOC),
        ("union_specifier", "union U { int b; }", TARGET),
        ("field_declaration", "int b;", TARGET),
        ("comment", "/** enum */", DOC),
        (
            "enum_specifier",
            "enum E { /** enumerator */ A, B }",
            TARGET,
        ),
        ("comment", "/** enumerator */", DOC),
        ("enumerator", "A", TARGET),
        ("enumerator", "B", TARGET),
        ("comment", "/** macro call */", DOC),
        ("expression_statement", "FOO(bar);", TARGET),
        ("comment", "/** empty stmt */", DOC),
        ("expression_statement", ";", NONE),
        ("preproc_if", "#if X", TARGET),
        ("comment", "/** in if */", DOC),
        ("declaration", "int in_if;", TARGET),
        ("preproc_elif", "#elif Y", TARGET),
        ("declaration", "int in_elif;", TARGET),
        ("preproc_else", "#else", TARGET),
        ("declaration", "int in_else;", TARGET),
        ("preproc_ifdef", "#ifdef Z", NONE),
        ("preproc_ifdef", "#ifdef Y", TARGET),
        ("preproc_elifdef", "#elifdef YY", TARGET),
        ("declaration", "int yy;", TARGET),
        ("preproc_if", "#if A", TARGET),
        ("preproc_if", "# if B", TARGET),
        ("declaration", "int deep;", TARGET),
        ("linkage_specification", "extern \"C\" { int c; }", TARGET),
        ("declaration", "int c;", TARGET),
    ];
    check::<CCode>(
        source,
        answers!(crate::roles::c, CCode),
        expected,
        &declared!(crate::roles::c),
        // The second `function_definition` id and the conditionals'
        // later aliases are what the old string compare matched for
        // free; the fixture spells one of each rule and the rest are
        // defensive. `expression_statement` is the other way round:
        // every statement the pinned grammar emits carries the
        // *second* id, so the first is the defensive one.
        &defensive_c(),
    );
}

#[cfg(feature = "c")]
fn defensive_c() -> Vec<u16> {
    use crate::C;
    vec![
        C::FunctionDefinition2 as u16,
        C::ExpressionStatement as u16,
        C::PreprocIf2 as u16,
        C::PreprocIf3 as u16,
        C::PreprocIf4 as u16,
        C::PreprocIfdef2 as u16,
        C::PreprocIfdef3 as u16,
        C::PreprocIfdef4 as u16,
        C::PreprocElse2 as u16,
        C::PreprocElse3 as u16,
        C::PreprocElse4 as u16,
        C::PreprocElif2 as u16,
        C::PreprocElif3 as u16,
        C::PreprocElif4 as u16,
        C::PreprocElifdef2 as u16,
        C::PreprocElifdef3 as u16,
        C::PreprocElifdef4 as u16,
    ]
}

/// The `ERROR` clause is the Checker's, never the row's. The outer
/// error here holds several named children (a type, a name, an
/// inner error, a parameter), which `no_entry_inside` reads as "too
/// much to be nothing" — Doxygen's benefit of the doubt — so it is
/// a target by the Checker's clause while the row answers `false`;
/// the inner one holds exactly one named non-error child, a
/// `compound_statement`, no entry, so it is not. Recovery shapes are
/// the grammar's and pinned as observed; the claim is the row /
/// Checker split, not the shape.
#[cfg(feature = "c")]
#[test]
fn c_error_clause_is_the_checkers_not_the_rows() {
    use crate::CParser;
    let source = b"int ok;\nint broken( { int inside; }\n}\nint a( int b( ;\nint after;\n";
    let seen = targets_with_recovery::<CParser>(source, crate::roles::c::doc_target::owns);
    let rows: Vec<_> = seen
        .iter()
        .map(|(k, t, r, c)| (k.as_str(), t.as_str(), *r, *c))
        .collect();
    assert_eq!(
        rows,
        [
            ("declaration", "int ok;", true, true),
            ("ERROR", "int broken( { int inside; }", false, true),
            ("declaration", "int inside;", true, true),
        ]
    );
}

// C++'s own entries: a namespace, `using`, alias, friend, template
// (a forward one included), a class with a body against one without,
// and `#elifdef`; then the C-shaped entries, so every owner is met.
// One fixture for the two C++ grammars: Mozilla's names its nodes as
// tree-sitter-cpp does and owns no file extension, so its table is
// pinned on the same source (grammar-dispatch, "a LANG variant that
// owns no file extension").
#[cfg(any(feature = "cpp", feature = "mozcpp"))]
pub(super) const CPP_SOURCE: &[u8] = b"/** ns */\nnamespace N {\n/** using */\nusing std::string;\n/** alias */\nusing A = int;\n/** class */\nclass C {\n  /** friend */\n  friend class D;\n  /** member fn */\n  void m();\n  /** field */\n  int f;\n};\n/** template */\ntemplate <class T> class Tpl { };\n/** template fwd */\ntemplate <class T> class TplFwd;\n/** fn def */\nvoid C::m() { }\n}\n/** class fwd */\nclass Fwd;\nunion UF;\nenum EF;\n#if X\n/** in if */\nint in_if;\n#elifdef Y\nint y;\n#endif\n/** typedef */\ntypedef int T;\n/** macro */\n#define X 1\n/** fn macro */\n#define F(a) (a)\n/** linkage */\nextern \"C\" { int c; }\n/** enum */\nenum E { /** a */ A };\n/** struct */\nstruct S { int a; };\n/** union */\nunion U { int b; };\n/** macro call */\nFOO(bar);\n;\n#ifdef Z\nint z;\n#elif W\nint w;\n#else\nint e;\n#endif\n#ifdef EMPTY\n#endif\n";

#[cfg(any(feature = "cpp", feature = "mozcpp"))]
const CPP_EXPECTED: &[(&str, &str, Answers)] = &[
    ("comment", "/** ns */", DOC),
    ("namespace_definition", "namespace N {", TARGET),
    ("comment", "/** using */", DOC),
    ("using_declaration", "using std::string;", TARGET),
    ("comment", "/** alias */", DOC),
    ("alias_declaration", "using A = int;", TARGET),
    ("comment", "/** class */", DOC),
    ("class_specifier", "class C {", TARGET),
    ("comment", "/** friend */", DOC),
    ("friend_declaration", "friend class D;", TARGET),
    ("comment", "/** member fn */", DOC),
    ("field_declaration", "void m();", TARGET),
    ("comment", "/** field */", DOC),
    ("field_declaration", "int f;", TARGET),
    ("comment", "/** template */", DOC),
    (
        "template_declaration",
        "template <class T> class Tpl { };",
        TARGET,
    ),
    ("class_specifier", "class Tpl { }", TARGET),
    ("comment", "/** template fwd */", DOC),
    (
        "template_declaration",
        "template <class T> class TplFwd;",
        TARGET,
    ),
    ("class_specifier", "class TplFwd", NONE),
    ("comment", "/** fn def */", DOC),
    ("function_definition", "void C::m() { }", TARGET),
    ("comment", "/** class fwd */", DOC),
    ("class_specifier", "class Fwd", NONE),
    ("union_specifier", "union UF", NONE),
    ("enum_specifier", "enum EF", NONE),
    ("preproc_if", "#if X", TARGET),
    ("comment", "/** in if */", DOC),
    ("declaration", "int in_if;", TARGET),
    ("preproc_elifdef", "#elifdef Y", TARGET),
    ("declaration", "int y;", TARGET),
    ("comment", "/** typedef */", DOC),
    ("type_definition", "typedef int T;", TARGET),
    ("comment", "/** macro */", DOC),
    ("preproc_def", "#define X 1", TARGET),
    ("comment", "/** fn macro */", DOC),
    ("preproc_function_def", "#define F(a) (a)", TARGET),
    ("comment", "/** linkage */", DOC),
    ("linkage_specification", "extern \"C\" { int c; }", TARGET),
    ("declaration", "int c;", TARGET),
    ("comment", "/** enum */", DOC),
    ("enum_specifier", "enum E { /** a */ A }", TARGET),
    ("comment", "/** a */", DOC),
    ("enumerator", "A", TARGET),
    ("comment", "/** struct */", DOC),
    ("struct_specifier", "struct S { int a; }", TARGET),
    ("field_declaration", "int a;", TARGET),
    ("comment", "/** union */", DOC),
    ("union_specifier", "union U { int b; }", TARGET),
    ("field_declaration", "int b;", TARGET),
    ("comment", "/** macro call */", DOC),
    ("expression_statement", "FOO(bar);", TARGET),
    ("expression_statement", ";", NONE),
    ("preproc_ifdef", "#ifdef Z", TARGET),
    ("declaration", "int z;", TARGET),
    ("preproc_elif", "#elif W", TARGET),
    ("declaration", "int w;", TARGET),
    ("preproc_else", "#else", TARGET),
    ("declaration", "int e;", TARGET),
    ("preproc_ifdef", "#ifdef EMPTY", NONE),
];

/// The C++ aliases the pinned grammar gives no spelling in the
/// fixture, in `$lang`'s ids: `declaration` and `function_definition`
/// are aliased four ways and every spelling takes the first; every
/// conditional takes its first alias; `expression_statement` the
/// *second*, as in C. All declared, none emitted — the `defensive`
/// contract.
#[cfg(any(feature = "cpp", feature = "mozcpp"))]
macro_rules! cpp_doc_defensive {
    ($lang:ident) => {
        [
            crate::$lang::Declaration2 as u16,
            crate::$lang::Declaration3 as u16,
            crate::$lang::Declaration4 as u16,
            crate::$lang::FunctionDefinition2 as u16,
            crate::$lang::FunctionDefinition3 as u16,
            crate::$lang::FunctionDefinition4 as u16,
            crate::$lang::ExpressionStatement as u16,
            crate::$lang::PreprocIf2 as u16,
            crate::$lang::PreprocIf3 as u16,
            crate::$lang::PreprocIf4 as u16,
            crate::$lang::PreprocIfdef2 as u16,
            crate::$lang::PreprocIfdef3 as u16,
            crate::$lang::PreprocIfdef4 as u16,
            crate::$lang::PreprocElse2 as u16,
            crate::$lang::PreprocElse3 as u16,
            crate::$lang::PreprocElse4 as u16,
            crate::$lang::PreprocElif2 as u16,
            crate::$lang::PreprocElif3 as u16,
            crate::$lang::PreprocElif4 as u16,
            crate::$lang::PreprocElifdef2 as u16,
            crate::$lang::PreprocElifdef3 as u16,
            crate::$lang::PreprocElifdef4 as u16,
        ]
    };
}

#[cfg(feature = "cpp")]
#[test]
fn cpp_rows_answer_the_table() {
    check::<crate::CppCode>(
        CPP_SOURCE,
        answers!(crate::roles::cpp, crate::CppCode),
        CPP_EXPECTED,
        &declared!(crate::roles::cpp),
        &cpp_doc_defensive!(Cpp),
    );
}

#[cfg(feature = "mozcpp")]
#[test]
fn mozcpp_rows_answer_the_table() {
    check::<crate::MozcppCode>(
        CPP_SOURCE,
        answers!(crate::roles::mozcpp, crate::MozcppCode),
        CPP_EXPECTED,
        &declared!(crate::roles::mozcpp),
        &cpp_doc_defensive!(Mozcpp),
    );
}

// Objective-C's own entries: the three containers and a protocol's
// `@optional` section, a property, an instance variable, a method
// declaration and definition — the `implementation_definition`
// wrapper of a definition is a target, of a `@synthesize` not.
#[cfg(feature = "objc")]
pub(super) const OBJC_SOURCE: &[u8] = b"/** interface */\n@interface C : NSObject {\n  /** ivar */\n  int iv;\n}\n/** property */\n@property int p;\n/** method decl */\n- (void)m;\n+ (void)c;\n@end\n/** impl */\n@implementation C\n/** method def */\n- (void)m { }\n@synthesize p;\n@end\n/** protocol */\n@protocol P\n- (void)q;\n@end\n/** fn */\nint f(void) { return 1; }\n#if X\n/** in if */\nint in_if;\n#else\nint e;\n#endif\n@protocol Q\n/** d */\n@optional\n- (void)r;\n@end\n/** typedef */\ntypedef int T;\n/** macro */\n#define X 1\n/** fn macro */\n#define F(a) (a)\n/** linkage */\nextern \"C\" { int c; }\n/** enum */\nenum E { /** a */ A };\n/** struct */\nstruct S { int a; };\n/** union */\nunion U { int b; };\nstruct SF;\nunion UF;\nenum EF;\n/** macro call */\nFOO(bar);\n;\n#ifdef Z\nint z;\n#elif W\nint w;\n#endif\n#ifdef Y\n#elifdef YY\nint yy;\n#endif\n#ifdef EMPTY\n#endif\n";

#[cfg(feature = "objc")]
#[test]
fn objc_rows_answer_the_table() {
    use crate::{Objc, ObjcCode};
    let source = OBJC_SOURCE;
    let expected: &[(&str, &str, Answers)] = &[
        ("comment", "/** interface */", DOC),
        ("class_interface", "@interface C : NSObject {", TARGET),
        ("comment", "/** ivar */", DOC),
        ("instance_variable", "int iv;", TARGET),
        ("comment", "/** property */", DOC),
        ("property_declaration", "@property int p;", TARGET),
        ("comment", "/** method decl */", DOC),
        ("method_declaration", "- (void)m;", TARGET),
        ("method_declaration", "+ (void)c;", TARGET),
        ("comment", "/** impl */", DOC),
        ("class_implementation", "@implementation C", TARGET),
        ("comment", "/** method def */", DOC),
        ("implementation_definition", "- (void)m { }", TARGET),
        ("method_definition", "- (void)m { }", TARGET),
        ("implementation_definition", "@synthesize p;", NONE),
        ("comment", "/** protocol */", DOC),
        ("protocol_declaration", "@protocol P", TARGET),
        ("method_declaration", "- (void)q;", TARGET),
        ("comment", "/** fn */", DOC),
        ("function_definition", "int f(void) { return 1; }", TARGET),
        ("preproc_if", "#if X", TARGET),
        ("comment", "/** in if */", DOC),
        ("declaration", "int in_if;", TARGET),
        ("preproc_else", "#else", TARGET),
        ("declaration", "int e;", TARGET),
        ("protocol_declaration", "@protocol Q", TARGET),
        ("comment", "/** d */", DOC),
        (
            "qualified_protocol_interface_declaration",
            "@optional",
            TARGET,
        ),
        ("method_declaration", "- (void)r;", TARGET),
        ("comment", "/** typedef */", DOC),
        ("type_definition", "typedef int T;", TARGET),
        ("comment", "/** macro */", DOC),
        ("preproc_def", "#define X 1", TARGET),
        ("comment", "/** fn macro */", DOC),
        ("preproc_function_def", "#define F(a) (a)", TARGET),
        ("comment", "/** linkage */", DOC),
        ("linkage_specification", "extern \"C\" { int c; }", TARGET),
        ("declaration", "int c;", TARGET),
        ("comment", "/** enum */", DOC),
        ("enum_specifier", "enum E { /** a */ A }", TARGET),
        ("comment", "/** a */", DOC),
        ("enumerator", "A", TARGET),
        ("comment", "/** struct */", DOC),
        ("struct_specifier", "struct S { int a; }", TARGET),
        ("field_declaration", "int a;", TARGET),
        ("comment", "/** union */", DOC),
        ("union_specifier", "union U { int b; }", TARGET),
        ("field_declaration", "int b;", TARGET),
        ("struct_specifier", "struct SF", NONE),
        ("union_specifier", "union UF", NONE),
        ("enum_specifier", "enum EF", NONE),
        ("comment", "/** macro call */", DOC),
        ("expression_statement", "FOO(bar);", TARGET),
        ("expression_statement", ";", NONE),
        ("preproc_ifdef", "#ifdef Z", TARGET),
        ("declaration", "int z;", TARGET),
        ("preproc_elif", "#elif W", TARGET),
        ("declaration", "int w;", TARGET),
        ("preproc_ifdef", "#ifdef Y", TARGET),
        ("preproc_elifdef", "#elifdef YY", TARGET),
        ("declaration", "int yy;", TARGET),
        ("preproc_ifdef", "#ifdef EMPTY", NONE),
    ];
    check::<ObjcCode>(
        source,
        answers!(crate::roles::objc, ObjcCode),
        expected,
        &declared!(crate::roles::objc),
        // The aliases the pinned grammar gives no spelling: the second
        // `function_definition` id, the first `expression_statement`
        // (every statement takes the second, as in C), and every
        // conditional's later aliases (`#elifdef` under `#ifdef` takes
        // the first; under `#if` it is a bare directive).
        &[
            Objc::FunctionDefinition2 as u16,
            Objc::ExpressionStatement as u16,
            Objc::PreprocIf2 as u16,
            Objc::PreprocIf3 as u16,
            Objc::PreprocIf4 as u16,
            Objc::PreprocIfdef2 as u16,
            Objc::PreprocIfdef3 as u16,
            Objc::PreprocElse2 as u16,
            Objc::PreprocElse3 as u16,
            Objc::PreprocElse4 as u16,
            Objc::PreprocElse5 as u16,
            Objc::PreprocElif2 as u16,
            Objc::PreprocElif3 as u16,
            Objc::PreprocElif4 as u16,
            Objc::PreprocElif5 as u16,
            Objc::PreprocElifdef2 as u16,
        ],
    );
}