Skip to content

fix(npa,npm): keep typed C++ macro calls methods and count a split virtual method once - #1928

Merged
dekobon merged 13 commits into
mainfrom
fix/1829-macro-call-evidence
Nov 15, 2026
Merged

dekobon merged 13 commits into
mainfrom
fix/1829-macro-call-evidence

Conversation

@dekobon

@dekobon dekobon commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

Refs #1829, #1885, #1929

Three changes to how npa and npm read C++ class members, on both grammars (cpp and mozcpp). The first eleven commits are the first change; the last two are the second and third.

1. A typed or [[noreturn]] macro call stays a method

#1885 (merged with #1909) reads a function_declarator whose parameter list is a recovered member pointer as a data member, because the pinned C++ grammars parse T (S::*pd); as a function named T. A class-scope macro call with a member-pointer argument is the same node, so a call that showed no other sign of being one became an attribute. Each read as a method before #1885:

#define DECL(P) method(int P)
#define FAIL(ignored) void fail()
struct A { int DECL(S::*p); };          // int method(int S::*p);
struct B { [[noreturn]] FAIL(S::*p); }; // [[noreturn]] void fail();

Both classes read 1 attribute / 0 methods on main b8a8910c. The truth, and the reading before #1885, is 0 / 1.

The reader now takes the word before the parameter list for the declarator's own name when the declaration:

  • writes a type the reader takes for nothing else: a keyword type, a qualified or template name, an elaborated or dependent type, auto or decltype(…); or
  • opens with the standard attribute [[noreturn]], in a list the grammars parse without error.

A declarator so named is read as a function when it is that word and one parameter list, and the word is no word of a type. npa and npm ask the same predicate, cpp_is_named_function, in the new module src/metrics/npa/cpp_first_word.rs. Four limits keep data members data:

  • A keyword type is told by its word, not its node kind. The Mozcpp fork lists the Windows typedef names (DWORD) and the macro APIENTRY as primitive_type, and tree-sitter-cpp reads wchar_t as a type_identifier.
  • A type's last word is not a name. The grammars give it to the declarator when something splits the type: unsigned const int (S::*pd); is a declarator named int after the type unsigned. A type keyword, typedef or a reserved identifier (__int128, _Complex) names no function.
  • A declarator that nests another is read as before. int MOZ_X (S::*pf)(int); is a pointer to member function and stays data.
  • A declarator with an ERROR of its own is no named function. int MOZ_X long (S::*pd); strands the long of its type between the macro and the parentheses.

2. Seventeen standard typedef names count as a type (2172d23d)

size_t DECL(S::*p); and uint32_t DECL(S::*p); read as attributes while int DECL(S::*p); and std::size_t DECL(S::*p); read as methods. The reader now also takes these names for a type, matched by word and only in the type slot: size_t, ssize_t, ptrdiff_t, nullptr_t, max_align_t, intptr_t, uintptr_t, intmax_t, uintmax_t, and int8_t to int64_t with their unsigned twins. The least and fast families of <cstdint> are not listed.

The cost is the one a keyword type already has: size_t MOZ_X (S::*pd);, a data member with an annotation macro before its parentheses, reads as a method, as it did before #1885.

3. A split virtual method counts once (12f312aa, #1929)

A word neither grammar knows between a function's specifiers and its name ends the declaration early:

struct A { virtual HRESULT STDMETHODCALLTYPE f(long* n); };
// field_declaration(virtual, HRESULT, STDMETHODCALLTYPE, ";")   <- the ";" is invented
// declaration(function_declarator f(long* n))

Since #1909 a declaration holding virtual, explicit or consteval is a function, so npm counted both halves: 0 attributes / 2 methods on main for one method (1 / 1 before #1909). The method walk now counts the halves once. A first half is a member it counted that ends in the invented ; and whose own declarator takes no parameters; the next member that declares a method is its second half.

In 2,687 C++ files of mozilla/gecko-dev at 5836a062 (3,755 classes), 34 classes differ between e689d658 and b8a8910c. This pull request returns class_methods to its e689d658 value in the 32 that counted a half twice (1,330 to 1,066 over the 34; 1,064 at e689d658) and leaves class_attributes at 165 (426 at e689d658). No class reads fewer methods than at e689d658.

What moves

  • npa and npm for a class-scope macro call after such a type or under [[noreturn]]: class_methods rises and class_attributes falls by one, with the values derived from them.
  • npm for a virtual, explicit or consteval method the grammars split: class_methods falls by one against main. Against the last release only npa moves for these methods (the first half no longer counts as an attribute).
  • No corpus snapshot moves.

Not fixed, pinned in the tests

Macro calls that stay attributes (FIXME(#1829)):

  • T DECL(S::*p); and DWORD DECL(S::*p);: a type written as one other identifier is token for token the annotated data member MOZ_X T (S::*pd);.
  • __attribute__((noreturn)) FAIL(S::*p); and [[gnu::noreturn]]: GCC and clang accept those spellings on a data member.
  • void (*GET(S::*p))(int);: a call inside a declarator's parentheses.
  • int __DECL(S::*p);: a call named by a reserved identifier.

Data members that read as methods (FIXME(#1829)):

Split methods (FIXME(#1929)):

  • HRESULT STDMETHODCALLTYPE f(long* n);, with no function-only specifier, still reads 1 / 1. Its tree is also that of a data member before a thread-safety annotation, Mutex other_mutex_instance_ GUARDED_BY(mu_); (fix(npa,npm/cpp): a field with a trailing annotation macro is a method #1904).
  • A virtual method cut off twice (virtual MOZ_A MOZ_B HRESULT STDMETHODCALLTYPE f();) counts one method and one attribute.
  • Where a macro hides a function's parameter list before the cut (virtual HRESULT WHOLE ~C(); with #define WHOLE f();), the method directly after it is read as a second half, and the two count as one. virtual MOZ_A MOZ_B ~C();, one method, is the same tree.
  • Where an annotation macro after virtual takes an argument that looks like a call (virtual ANN(foo()) int f();), the first half looks whole and the method counts twice, as on main. virtual Ret (f()) END_DECL int g();, two methods, has the same first half.

Review

No person has reviewed this pull request.

  • Commits 1 to 11 (cb93145e to c49b913d): astra-review twice, argon-review twice, four agents of the code-review skill, and sol-review on the last two commits. Their findings are fixed in those commits.
  • Commits 12 and 13: the review skill checklist, then astra-review and argon-review, four passes each (the first version and three rounds of fixes).
    • Pass 1. astra-review: commit 13 skipped a separate method after a whole virtual member cut off by a macro (virtual VGET(S::*x) END_DECL C();, virtual int (*f()) END_DECL C();). The first-half test was rewritten to read parameter lists off the tree. argon-review: four false or loose statements and one untested condition.
    • Pass 2. astra-review: the rewrite brought two double counts back (a function declarator inside the return type; a second half with a specifier of its own). The walk was narrowed to declarator wrappers and the specifier rule removed.
    • Pass 3. argon-review: attributed_declarator was missing from the wrappers. astra-review: the annotation-call case above, which no tree-based rule separates from a whole method; it is pinned.
    • Pass 4, on 12f312aa: neither reviewer reports a finding.

Evidence

  • On 12f312aa: make pre-commit gives BCA_GATE: pass (gate=pre-commit); make check-test-lang-gates-compare COMPARE_REF=b8a8910c passes; make bench-scaling: all 139 probes within their complexity bound.
  • Each fix has a test that fails without it. Hand mutations of the conditions commits 12 and 13 add: 27 of 27 fail the expected test. cargo mutants on c49b913d for the first change: 68 mutants, 67 caught, 1 unviable.
  • Release builds of c49b913d and 12f312aa over a local set of 6,630 review fixtures and probes (12,387 runs, both grammars): 291 runs differ, none changes status. All 863 class readings that moved are of the two intended kinds: 631 lose a duplicate method with attributes unchanged, and 232 go from attribute to method after a standard typedef name.
  • 1,330 generated spellings of a whole method under a function-only specifier before another member, all accepted by g++ and clang++: 12f312aa counts fewer methods than c49b913d in 27, the 13 that hold the split method above and 14 where one macro hides the parameter list and a second the ; (virtual DECLARE_FOO END_DECL C();).
  • 2,980 real files (293 system headers that contain ::*, from Boost, LLVM 14, libstdc++, googletest and Abseil, and the Mozilla files above; 8,244 classes): b8a8910c and c49b913d agree on every class. 12f312aa differs from them in 32 classes, all in the Mozilla files, each of which counted a split method twice.
  • Generated grids of compiler-accepted member-pointer spellings, against e689d658: of 781 macro calls, main misreads 498 that were right and c49b913d 136; of 1,737 data members, neither misreads one that was right. On 339 spellings with a standard typedef name, commit 12 raises calls read right from 112 to 202 of 240 and lowers data members read right from 87 to 77 of 99.

The #1885 reader takes a `function_declarator` whose parameter list is
a recovered member pointer for a data member: the grammars read
`T (S::*pd);` as a function named `T`. A class-scope macro call with a
member-pointer argument is the same node, and #1885 turned it into an
attribute unless something on it showed a call (function-only syntax, a
second argument, a pointer that names nothing). Calls that showed none
had read as methods before:

    #define DECL(P) method(int P)
    struct A { int DECL(S::*p); };          // int method(int S::*p);
    struct B { [[noreturn]] FAIL(S::*p); }; // [[noreturn]] void fail();
    // truth 0 attributes / 1 method each; before #1885 0 / 1; after 1 / 0

Two things settle that the word before the parameter list is the
declarator's own name (`cpp_declared_name`):

- The declaration writes a type the grammars read as nothing else: a
  keyword type, a qualified or template name, an elaborated or
  dependent type, `auto` or `decltype(…)`. The recovery takes the
  member's type for the function's name, so a declaration that still
  writes one holds no recovery; the grammars read `int (S::*pd);` as a
  `parenthesized_declarator`. The answer reaches a declarator under a
  pointer, a reference or a template header, and the head of a comma
  list.
- The declaration opens with the standard attribute `[[noreturn]]`,
  which only a function's declaration takes.

Either settles a declarator that is the word and one parameter list,
and both consumers ask that through `cpp_is_named_function`. A
declarator that nests another is read as before: its second parameter
list is a pointee's (`int MOZ_X (S::*pf)(int);` is a pointer to member
function after an annotation macro), or its levels are a comma list's.
The attribute makes no method of anything else either: GCC accepts it
on a pointer to function, which stays data.

A type written as a lone identifier settles nothing, because an
annotation macro before the real type is one too: `MOZ_X T (S::*pd);`
stays the data member #1885 made it, and the macro call
`T DECL(S::*p);`, the same tree, stays misread as data. `noreturn`
under a namespace or in the GNU spelling settles nothing either: both
compilers accept `[[gnu::noreturn]] T (S::*pn);`, a data member. One
reading is given up: an annotation macro between a written type and the
parentheses of a pointer to data member, `int MOZ_X (S::*pd);`, is the
tree of `int DECL(S::*p);` and counts as a method again, as it did
before #1885. All three are pinned FIXME(#1829).

Reading the attribute's name needs the source bytes, so the readers in
`cpp_declarators.rs` take `code`.

Tests: `TYPED_MACRO_CALLS_WITH_MEMBER_POINTERS` and
`NORETURN_MACRO_CALLS_WITH_MEMBER_POINTERS` (methods), with the controls
`MEMBER_POINTERS_NO_FIRST_WORD_SETTLES` and
`NORETURN_THAT_SETTLES_NOTHING` (data), on both grammars. With the
production change stashed the first reads 17 attributes where 3 are
expected. Twenty-three mutations of the new conditions each fail a
test.

Of 6,892 runs of the review's C++ fixtures over both grammars (a debug
build of this commit against a release build of its parent), 120 differ
in value. Each is a macro call after a written type or under
`[[noreturn]]`, or the annotation spelling above, and every class that
moved reads as it did before #1885.

`cpp_child_verdict` reaches the cyclomatic limit of 15, which the
refreshed baseline records.

Refs #1829 #1885
@codecov

codecov Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.57%. Comparing base (b8a8910) to head (12f312a).
⚠️ Report is 13 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff            @@
##             main    #1928    +/-   ##
========================================
  Coverage   98.57%   98.57%            
========================================
  Files         406      407     +1     
  Lines      126965   127233   +268     
  Branches   126530   126798   +268     
========================================
+ Hits       125151   125420   +269     
+ Misses       1089     1088     -1     
  Partials      725      725            
Flag Coverage Δ
python 100.00% <ø> (ø)
rust 98.57% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/metrics/npa.rs 100.00% <ø> (ø)
src/metrics/npa/cpp.rs 98.27% <100.00%> (ø)
src/metrics/npa/cpp_declarators.rs 98.78% <100.00%> (+0.56%) ⬆️
src/metrics/npa/cpp_first_word.rs 100.00% <100.00%> (ø)
src/metrics/npm.rs 100.00% <100.00%> (ø)
src/metrics/npm/cpp.rs 97.72% <100.00%> (+0.35%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

The previous commit takes the word before a parameter list for the
declarator's own name when the declaration writes a type that is no
lone `type_identifier`. Two things about the grammars break that, and
the reviews of the pull request found both.

A written type does not show the next word is a name. The grammars give
a type's last word to the declarator when something splits it from the
first: a qualifier or specifier (`unsigned const int (S::*pd);`), an
annotation macro joined to the first word (`MOZ_X unsigned int
(S::*pd);`), or a word they do not know (`unsigned __int128 (S::*pd);`,
`double _Complex (S::*pd);`). Each is a `function_declarator` named
`int`, `__int128` or `_Complex` after the type `unsigned` or `double`,
and each is a valid data member that #1885 had made an attribute and
the previous commit made a method again:

    struct A { unsigned const int (S::*pd); };
    // truth 1 attribute / 0 methods; parent of the previous commit 1 / 0;
    // previous commit 0 / 1

`cpp_is_named_function` now declines a declarator whose name is a word
of a type: a type keyword, `typedef`, or an identifier the
implementation reserves, which is how the extension types and
qualifiers are spelled. `int typedef (S::*pm_t);` therefore reads as
`T typedef (S::*pm_t);` does, and both are pinned FIXME(#1829).

The node kind does not say what a keyword type is. The Mozcpp fork
lists the Windows typedef names and the macro `APIENTRY` as
`primitive_type`, and tree-sitter-cpp reads `wchar_t` as a
`type_identifier`, so the two grammars disagreed: `APIENTRY T
(S::*pd);` was a method on Mozcpp alone, and `wchar_t DECL(S::*p);` an
attribute on the upstream grammar alone. A one-word type now settles
the first word only when the word is a type keyword. A typedef name is
a lone identifier whichever kind a grammar gives it, so
`DWORD DECL(S::*p);` reads as data on both, as `T DECL(S::*p);` does
(pinned).

Tests: `MEMBER_POINTERS_NO_FIRST_WORD_SETTLES` gains `pi`, `pj`, `pk`,
`pl` and `pw` (na 15 / nm 0; the previous commit reads four of them as
methods on the upstream grammar and five on Mozcpp),
`TYPED_MACRO_CALLS_WITH_MEMBER_POINTERS` gains the `wchar_t` call, and
the two pins gain `DWORD DECL(R::*p);` and `int typedef (Q::*pm_t);`.

Refs #1829 #1885
`cpp_opens_with_standard_noreturn` takes an `attribute` with no
namespace before its name for the standard one. Neither grammar parses
the namespace a list can state once for all its attributes: `[[using
gnu: unused, noreturn]]` is an `attribute_declaration` with `using gnu:`
in an `ERROR` and the attributes after it bare. The `noreturn` there is
`gnu::noreturn`, which both compilers accept on a data member, and the
reader took it for the standard attribute:

    struct A { [[using gnu: unused, noreturn]] T (S::*pu); };
    // truth 1 attribute / 0 methods; parent of the pull request 1 / 0;
    // before this commit 0 / 1

A list with a parse error is now skipped, so what it holds settles
nothing.

Test: `MEMBER_POINTERS_NO_FIRST_WORD_SETTLES` gains `pu` (na 16 /
nm 0); without the change it reads na 15 / nm 1.

Refs #1829 #1885
The reviews of the pull request found one rule the code claims with no
test, and three readings it changes or leaves that no fixture names.

- A dependent type is a type the grammars read as nothing else, as the
  code and its comment say, but no fixture wrote one.
  `TYPED_MACRO_CALLS_WITH_MEMBER_POINTERS` gains
  `typename V<int>::Y DECL(S::*p);` (nm 16).
- What stands inside a declarator's parentheses is asked with no first
  word settled (`DeclaratorQuestion::any_child`), and no fixture line
  depended on that. Every shape it decides is a typed macro call that
  stays data: `void (*GETTER(S::*p))(int);`, a method returning a
  pointer to function, reads na 3 / nm 0 where the truth is na 2 /
  nm 1. Pinned FIXME(#1829) as
  `PARENTHESISED_TYPED_MACRO_CALL_WITH_MEMBER_POINTER`.
- A typed macro call named by a reserved identifier reads as data,
  because the reader takes such a word for part of the type (the rule
  that keeps `unsigned __int128 (S::*pk);` data): `int __DECL(S::*p);`
  reads na 3 / nm 0 where the truth is na 2 / nm 1. Pinned
  FIXME(#1829) as `RESERVED_NAMED_MACRO_CALL_WITH_MEMBER_POINTER`.
- `[[noreturn]] T (S::*pd);` is the tree of the macro call
  `[[noreturn]] FAIL(S::*p);` and reads as a method. GCC accepts it as
  a data member and warns that it ignores the attribute; clang rejects
  it. Pinned as `NORETURN_MEMBER_POINTER_ONLY_GCC_ACCEPTS`.

No reading changes.

Refs #1829 #1885
…error

`cpp_declared_name` ran for every class member, once in each consumer:
a `type` field lookup, and for a one-word type a walk of the leading
attribute lists with two more field lookups per attribute. Its answer
is read only by `cpp_is_named_function`, which both consumers reach
only for a `function_declarator` holding a parse error. A declaration
with no parse error never used it.

The function now answers `Unknown` at once for such a declaration,
through the cached error flag the consumers already test. No reading
changes.

The review measured the cost on the pull request's first commit against
its parent, for one class of 100,000 members with no parse error,
`--metrics npa,npm`, as a ratio of minimum CPU time: 1.11 to 1.15 for
`int x;`, 1.20 to 1.22 for `T x;` and 1.39 to 1.40 for
`[[maybe_unused]] T x;`. Over the 1,081 headers of an LLVM 14 include
tree the ratio was 0.998 to 1.010.

Refs #1829 #1885
…ointer

What a declaration settles about its first word reaches a declarator
under a `pointer_declarator`, a `reference_declarator` or an
`init_declarator`, but was dropped at the fourth wrapper: a member
pointer outside parentheses, which the grammars parse cleanly under a
template header as a `qualified_identifier` holding the declarator.
`cpp_member_pointer_verdict` asked what stands under it with nothing
settled, so a typed macro call there stayed the attribute #1885 made
it:

    #define DECL(P) method(int P)
    struct A { template<class U> int S::*DECL(S::*p); };
    // truth 0 attributes / 1 method; before #1885 0 / 1; since 1 / 0
    struct B { template<class U> int *DECL(S::*p); };   // already 0 / 1

The verdict now takes what its caller knows. The caller directly under
the declaration passes it on; the two that stand inside parentheses or
a recovered bit-field pass nothing, as before.

Test: `TYPED_MACRO_CALLS_WITH_MEMBER_POINTERS` gains the first class
above (nm 17); without the change it reads one attribute more and one
method fewer.

Refs #1829 #1885
…ction

`cpp_child_verdict` is a dispatch on a child's kind. Its
`function_declarator` arm was the only one with control flow of its
own, a three-way match under a nineteen-line comment, where the
`qualified_identifier` and `bitfield_clause` arms already hand their
question to a function. The arm is now
`cpp_function_declarator_verdict`, and its comment that function's.
`cpp_child_verdict` falls from the cyclomatic limit of 15 to 10, so the
baseline entry that recorded it goes.

`cpp_is_named_function` spelled again the test `cpp_recovered_level`
calls `innermost`: a `function_declarator` that nests no other. Both
now ask `cpp_is_innermost_level`.

No reading changes.

Refs #1829 #1885
…ainty

The reader's evidence that a declarator's first word is its own name
was `DeclaredName::Certain`, described as "known to be" and "surely" a
name, while the tests of the same pull request pin a spelling it
misreads: `int MOZ_X (S::*pd);`, an annotation macro between a written
type and a pointer to data member, is the tree of `int DECL(S::*p);`
and reads as a method. A maintainer reading the rule where it lives
was told it is proof and had to find the exception in another file.

The type is now `FirstWord` with the variants `Name` (taken for the
declarator's name) and `Unsettled`, the function `cpp_first_word`, and
each variant's comment names the spelling it misreads. The comment on
`DeclaratorQuestion::any_child` says its answer inside parentheses is a
guess and names the pinned call it leaves as data. The values that
carried the answer were called `name` beside `segment.names`, a count,
and are `first_word`.

No reading changes.

Refs #1829 #1885
The reviews of the pull request found statements that are false or
incomplete. No code changes.

CHANGELOG, the #1885 entry:

- It listed two macro calls among the spellings "still misread". Each
  counted as a method before #1885 and counts as an attribute since,
  so users see the value move: a call after a type written
  as one identifier (`T DECL(S::*p);`), and one under `noreturn` in a
  GNU spelling. They now stand with `FOO(S::*x);` among the calls that
  "now count as attributes where each counted as a method", together
  with three the entry did not name: a call after a typedef name
  (`DWORD DECL(S::*p);`), one inside a declarator's parentheses
  (`void (*GET(S::*p))(int);`) and one named by a reserved identifier
  (`int __DECL(S::*p);`).
- "An annotation macro between a written type and its parentheses ...
  still counts as a method" was false for a type written as one
  identifier: `T MOZ_X (S::*pd);` counts as an attribute. The sentence
  now says "a type the grammars read as nothing else".

Comments:

- `cpp_opens_with_standard_noreturn` records the one spelling only GCC
  accepts that the attribute turns into a method, `[[noreturn]] T
  (S::*pd);`, and the control fixture no longer says the attribute
  "makes no method of whatever follows it".
- The comment on `cpp_recovered_declaration_data_members` named a test,
  `assert_no_declarator_is_claimed_twice`, that does not exist, and
  listed three things the two consumers share where there are four.
- The #1885 test comments said "`origin/main`" for the tree before
  #1885. #1885 has landed, so `origin/main` now names a tree that reads
  those spellings differently. The ten places say "before #1885".

Each spelling the entry names was run under both grammars on builds of
the tree before #1885, of the pull request's parent and of this commit.

Refs #1829 #1885
… function

A type keyword that opens a multi-word type fills the `type` field
alone. When an annotation macro then splits the type, the grammars take
the macro for the declarator's name and leave the type's last word in
an `ERROR` between that name and the parameter list:

    #define MOZ_X
    struct A { int MOZ_X long (S::*pd); };   // long int (S::*pd);
    // function_declarator(field_identifier MOZ_X, ERROR "long",
    //                     parameter_list "(S::*pd)")
    // truth 1 attribute / 0 methods; parent of the pull request 1 / 0;
    // before this commit 0 / 1

The name is no word of a type, so `cpp_is_named_function` took the
declarator for the function `MOZ_X`. On the upstream grammar the same
happened under an annotation the Mozcpp fork knows as a token
(`int MOZ_NON_OWNING_REF unsigned (S::*pd);`), so the two grammars
disagreed where the pull request's parent had them agree.

Nothing stands between a function's name and its parameter list, so a
declarator with an `ERROR` of its own is now left to the recovered
chain. A macro call keeps its `ERROR` inside the parameter list, and an
annotation after the call is the declaration's child, so
`int DECL(S::*p) MOZ_X;` stays a method.

Found by the confirmation review of the fixes. Test:
`MEMBER_POINTERS_NO_FIRST_WORD_SETTLES` gains `pq` (na 17 / nm 0);
without the change it reads na 16 / nm 1.

Refs #1829 #1885
`cpp_declarators.rs` had grown to 540 physical lines of code against
the file limit of 550, most of the growth being what this pull request
adds: the evidence for what the first word of a recovered declarator
is. That evidence has two entry points (`cpp_first_word`,
`cpp_is_named_function`) and nothing else of the reader depends on its
insides, so it moves to `cpp_first_word.rs`, beside `cpp_declarators.rs`
as `ruby_body.rs` and `ruby_ledger.rs` stand beside `ruby.rs`.

Moved unchanged: `FirstWord`, `cpp_first_word`,
`cpp_opens_with_standard_noreturn`, `cpp_is_named_function`,
`CPP_TYPE_KEYWORDS`, `cpp_is_type_keyword` and `cpp_is_type_word`.
`cpp_is_innermost_level` stays, since the chain reader asks it too.
`cpp_declarators.rs` is back under the soft tier, so its baseline entry
goes.

No reading changes: debug builds of this commit and its parent agree on
5,946 runs of the review's C++ fixtures over both grammars.

Refs #1829 #1885
…cro call

A class-scope macro call with a member-pointer argument is a method
when the declaration writes a type before it. One word was taken for a
type only when it is a keyword, so the two grammars read

    #define DECL(P) method(int P)
    struct A { size_t DECL(S::*p); };     // size_t method(int S::*p);
    // truth 0 attributes / 1 method; before this commit 1 / 0

as an attribute, where `int DECL(S::*p);` and `std::size_t
DECL(S::*p);` read as methods.

Seventeen typedef names now settle the first word as a keyword does:
those of <cstddef>, the exact-width, pointer-width and greatest-width
integers of <cstdint>, and POSIX `ssize_t`. The `least` and `fast`
families of <cstdint> are not listed. The names are matched by word:
tree-sitter-cpp reads `intmax_t` as a `type_identifier` and the Mozcpp
fork as a `primitive_type`, and both read `charptr_t`, which no header
declares, as a `primitive_type`. Only the type slot asks;
`cpp_is_type_word` does not list them.

The spelling is evidence and no proof, and it costs what a keyword
type already costs. `size_t MOZ_X (S::*pd);`, a data member with an
annotation macro before its parentheses, is the same tree as the call
and reads as a method, as it did before #1885. Pinned FIXME(#1829)
beside `int MOZ_X (S::*pd);`. A program that defines one of the names
as an annotation macro has `size_t T (S::*pd);` read as a method too.

On 339 generated spellings that g++ and clang++ accept, calls read
right rise from 112 to 202 of 240 and data members read right fall
from 87 to 77 of 99; every one of the ten was read as a method before
#1885 too.

Tests: `STANDARD_TYPEDEF_MACRO_CALLS_WITH_MEMBER_POINTERS` (na 5 /
nm 5; without the change na 10 / nm 0), and
`*_each_standard_typedef_name_is_a_type`, which writes the seventeen
names out and two the reader must not take.

Refs #1829 #1885
A word neither grammar knows between a function's specifiers and its
name ends the `field_declaration` there, with a `;` the grammars
invent, and the name and parameter list follow as a member of their
own:

    struct A { virtual HRESULT STDMETHODCALLTYPE f(long* n); };
    // field_declaration(virtual, HRESULT, STDMETHODCALLTYPE, ";")
    // declaration(function_declarator f(long* n))
    // truth 0 attributes / 1 method; before #1885's reader 1 / 1;
    // before this commit 0 / 2

Since dc6c93f a declaration holding `virtual`, `explicit` or
`consteval` is a function to both consumers, so the first half stopped
counting as an attribute and started counting as a second method. An
annotation macro before `virtual` and a template return type
(`MOZ_X virtual RefPtr<T> g();`) splits the same way and read 0 / 1
before.

The method consumer now counts the halves once. A first half is a
member it counted that ends in the invented `;` and whose own
declarator takes no parameters, so that its specifier is what makes it
a function (`cpp_is_split_function_head`). The next member that
declares a method is its second half and is not counted again. A
cut-off member between the two, which two annotation macros beside the
return type and the calling convention leave, is read as more of the
first half.

"Takes no parameters" is read off the tree (`cpp_holds_parameter_list`)
and not asked of the declarator reader, which takes two parameter
lists for data: a macro call's and one inside parentheses (#1891). The
member's declarator takes parameters when it is a `function_declarator`,
alone or under a pointer, reference, array, parenthesised or
attributed declarator, the `operator_cast` of a conversion function,
or the `bitfield_clause` of a recovered `int S::*get();`. Only those
wrappers are looked into: a parameter list inside the member's type
(`RefPtr<void(int cb())>`, a lambda under `decltype`) is another
function's.

Neighbours that keep their reading, each pinned:

- A method with a parameter list of its own before an invented `;` is
  whole. `virtual int f() END_DECL C();`, with `END_DECL` expanding to
  `;`, is two methods; so are `virtual int (*f())[2] END_DECL C();`,
  `virtual operator int() END_DECL C();` and the macro call
  `virtual VGET(S::*x) END_DECL C();`.
- A macro that expands to a whole declarator ends in a `;` of its own.
  `virtual DECLARE_FOO; int g();` is two methods.
- A cut-off member with no such specifier. `HRESULT STDMETHODCALLTYPE
  f();` still reads 1 / 1, because `Mutex other_mutex_instance_
  GUARDED_BY(mu_);`, a data member before a thread-safety annotation
  (#1904), is the same tree. FIXME(#1929).

One spelling reads one method for two, as it did before #1885 and not
as its parent did. `virtual HRESULT WHOLE ~C();`, with `WHOLE`
expanding to `f();`, is two methods and the tree of `virtual MOZ_A
MOZ_B ~C();`, which is one; the reader takes the second reading. So
does a method directly after any parameter list a macro hides before
a cut. Pinned FIXME(#1929).

One spelling reads two methods for one, as its parent did and not as
it did before #1885. An annotation macro whose argument looks like a
call, `virtual ANN(foo()) int f();`, is the tree of the whole method
`virtual T (f()) END_DECL int g();`, so its first half is taken for
whole. Pinned FIXME(#1929).

On 1,330 generated spellings of a whole method under such a specifier
before another member, all accepted by g++ and clang++, the count of
methods falls against c49b913 in 27: the 13 with the split method
above, and 14 where one macro hides the parameter list and a second
the `;` (`virtual DECLARE_FOO END_DECL C();`).

In 2,687 C++ files of mozilla/gecko-dev at 5836a062 (3,755 classes
under `-l cpp`), 34 classes differ between e689d65 and b8a8910. This
commit returns `class_methods` to its e689d65 value in the 32 that
counted a half twice (1,330 to 1,066 over the 34; 1,064 at e689d65)
and leaves `class_attributes` at 165 (426 at e689d65).

Tests: `*_split_virtual_methods_count_once`. Without the change
`VIRTUAL_METHODS_SPLIT_IN_TWO` reads na 0 / nm 32 for nm 16.

Refs #1929 #1885
@dekobon dekobon changed the title fix(npa,npm): keep a typed or [[noreturn]] C++ macro call a method fix(npa,npm): keep typed C++ macro calls methods and count a split virtual method once Nov 15, 2026
@dekobon
dekobon merged commit 12f312a into main Nov 15, 2026
54 checks passed

This branch was previously deployed

1 inactive deployment
github-pages — 12f312aa Deployed Nov 15, 2026 by dekobon via deploy to GitHub Pages #1086
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant