-
Notifications
You must be signed in to change notification settings - Fork 0
/
count_destructors.js
111 lines (93 loc) · 3.43 KB
/
count_destructors.js
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
/* Purpose of this script is to check whether count of constructor calls is the
same as destructors in the just run C++ app.
Input:
Stdout from the C++ app.
Requirements:
The scripts looks for output lines starting with either `+` (for
constructors) or `~` (for destructors) and identifies them by the memory
address included in those lines as well, in a format of `0x<hex_digits>`.
*/
const rl = require('readline').createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
const logEntries = [];
rl.on('line', (line) => {
logEntries.push(parseLine(line));
});
rl.once('close', () => {
const analysis = analyzeConstructorsDeconstructors(logEntries);
console.log(`\nFound ${analysis.matched.length} constructors with matching destructors.`);
if (analysis.unmatched.length > 0) {
console.log(`\nFound ${analysis.unmatched.length} constructors WITHOUT matching destructors:\n`);
for (const item of analysis.unmatched) {
console.log(item);
}
} else {
console.log(`\nFound 0 constructors without matching destructors.`);
}
});
////////////////////////////////////////////////////////////////////////////////
function parseLine(line) {
const logEntry = {
type: "other",
address: null,
originalLine: line,
};
if (line.startsWith("+ ")) {
logEntry.type = "constructor";
}
if (line.startsWith("~ ")) {
logEntry.type = "deconstructor";
}
if (logEntry.type !== "other") {
const matches = /0x[0-9a-f]+/.exec(line);
logEntry.address = matches?.[0] ?? null;
}
return logEntry;
}
function analyzeConstructorsDeconstructors(logEntries) {
const analysis = {
matched: [],
unmatched: [],
};
const entriesByAddresses = new Map();
// Try to match constructors with deconstructors first:
//
for (const entry of logEntries) {
if (entry.type === "other") {
// do nothing
} else if (entry.type === "constructor") {
if (!entry.address) {
console.log(`Failed to extract an address: '${entry.originalLine}'`);
} else {
if (entriesByAddresses.has(entry.address)) {
console.log(`Found a new constructor on the already taken address: '${entry.originalLine}'`);
} else {
entriesByAddresses.set(entry.address, entry);
}
}
} else if (entry.type === "deconstructor") {
if (!entry.address) {
console.log(`Failed to extract an address: '${entry.originalLine}'`);
} else {
if (entriesByAddresses.has(entry.address)) {
const matchingConstructorEntry = entriesByAddresses.get(entry.address);
analysis.matched.push(matchingConstructorEntry.originalLine);
entriesByAddresses.delete(entry.address);
} else {
console.log(`Unexpected deconstructor without a matching constructor: '${entry.originalLine}'`)
}
}
} else {
console.log(`Unexpected entry type '${entry.type}': '${entry.originalLine}'`);
}
}
// Then gather all unmatched constructors:
//
for (const entry of entriesByAddresses.values()) {
analysis.unmatched.push(entry.originalLine);
}
return analysis;
}