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
//! `Npm` implementation for Mozilla C++.
#![allow(clippy::wildcard_imports, clippy::enum_glob_use)]
#![allow(
clippy::cast_precision_loss,
clippy::cast_possible_truncation,
clippy::cast_sign_loss
)]
use super::*;
impl Npm for MozcppCode {
fn compute<'a>(
node: &Node<'a>,
_code: &'a [u8],
ancestors: Ancestors<'a, '_>,
stats: &mut Stats,
) {
use Mozcpp::*;
if !matches!(node.kind_id().into(), FieldDeclarationList) {
return;
}
let Some(parent) = ancestors.parent(node) else {
return;
};
// C++ `class` defaults to private; `struct` defaults to public.
let mut current_is_public = match parent.kind_id().into() {
ClassSpecifier => false,
StructSpecifier => true,
_ => return,
};
for child in node.children() {
match child.kind_id().into() {
AccessSpecifier => {
current_is_public = child
.first_child(|id| {
id == Mozcpp::Public || id == Mozcpp::Protected || id == Mozcpp::Private
})
.is_some_and(|tok| tok.kind_id() == Mozcpp::Public);
}
// Inline-defined member function (with a body): regular
// methods, constructors, destructors, operator overloads,
// and conversion operators all share these aliased
// `function_definition` kind-ids.
FunctionDefinition | FunctionDefinition2 | FunctionDefinition3
| FunctionDefinition4 => {
stats.class_nm += 1;
if current_is_public {
stats.class_npm += 1;
}
}
// Member function reached through a wrapper node
// rather than surfacing as a `function_definition`
// directly. The wrapper varies by shape:
// - `field_declaration > function_declarator` for
// ordinary forward-declared methods (incl. pure
// virtual `= 0` and `Foo* operator->()` wrapped in
// `pointer_declarator`).
// - `declaration > function_declarator` for
// constructors / destructors (no return type).
// - `template_declaration > declaration >
// function_declarator` for a templated member fn
// declared without a body.
// - `template_declaration > function_definition` for a
// templated member fn *with* an inline body. This
// shape was missed until #1258, leaving `npm`
// disagreeing with `nom` and `wmc` on the same class.
// - `declaration > operator_cast` for a conversion
// operator declared without a body, and
// `template_declaration > declaration >
// operator_cast` for its templated form. Neither
// carries a `function_declarator` anywhere in its
// subtree. Missed until #1298, which left both forms
// invisible to `npm` *and* `npa`.
//
// The shared `cpp_declares_function` helper walks the
// declarator subtree (including `declaration` wrappers)
// so every shape above collapses into one arm. The guard
// is what keeps the `template_declaration` payloads
// that are *not* member functions out: a nested
// templated class (a `type_specifier` the helper does
// not descend into), an `alias_declaration`, a
// templated static data member (a `declaration` with
// no function declarator), and a `friend_declaration`,
// whose function is not a member of this class. The
// list is not closed: both grammars also admit a
// nested `template_declaration` as a child, which the
// helper declines because the two-header form it would
// spell is not valid C++ at class scope.
FieldDeclaration | Declaration | Declaration2 | Declaration3 | Declaration4
| TemplateDeclaration
if super::npa::cpp_declares_function(&child) =>
{
stats.class_nm += 1;
if current_is_public {
stats.class_npm += 1;
}
}
_ => {}
}
}
}
}