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
//! `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], stats: &mut Stats) {
use Mozcpp::*;
// Mark class / struct spaces as class spaces so the metric is
// emitted on them.
if matches!(node.kind_id().into(), ClassSpecifier | StructSpecifier) && stats.is_disabled()
{
stats.is_class_space = true;
}
if !matches!(node.kind_id().into(), FieldDeclarationList) {
return;
}
let Some(parent) = node.parent() 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;
}
}
// Declaration-only member function. The wrapping node
// 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 templated member fns.
//
// The shared `cpp_has_function_declarator` helper walks
// the declarator subtree (including `declaration`
// wrappers) so all three shapes collapse into one arm;
// the guard avoids counting non-method declarations
// (e.g. nested type aliases) under the same parent.
FieldDeclaration | Declaration | Declaration2 | Declaration3 | Declaration4
| TemplateDeclaration
if super::npa::cpp_has_function_declarator(&child) =>
{
stats.class_nm += 1;
if current_is_public {
stats.class_npm += 1;
}
}
_ => {}
}
}
}
}