-
Notifications
You must be signed in to change notification settings - Fork 969
/
Copy pathlint-with-clang-tidy.js
147 lines (128 loc) · 4 KB
/
lint-with-clang-tidy.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
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
const { spawn } = require('child_process');
const fs = require('fs');
const path = require('path');
const { makeSimplePromisePool } = require('./utils/SimplePromisePool');
const gdevelopRootPath = path.resolve(__dirname, '../../');
const coreSourcesRootPath = path.join(gdevelopRootPath, 'Core/GDCore');
const extensionSourcesRootPath = path.join(gdevelopRootPath, 'Extensions');
const excludedPaths = [
'Core/GDCore/Tools/Localization.cpp', // emscripten code which can't be linted
'Core/GDCore/Serialization/Serializer.cpp', // Diagnostic that can't be ignored in rapidjson.
];
const supportedExtensions = ['.cpp', '.h', '.hpp'];
async function findClangTidy() {
const tryClangTidy = (clangTidyCommandName) =>
new Promise((resolve, reject) => {
const process = spawn(clangTidyCommandName, ['--version'], {
stdio: 'inherit',
});
process.on('error', (error) => {
resolve(false);
});
process.on('close', (code) => {
if (code === 0) {
resolve(true);
} else {
resolve(false);
}
});
});
const hasClangTidy19 = await tryClangTidy('clang-tidy-19');
if (hasClangTidy19) {
return 'clang-tidy-19';
}
const hasClangTidy = await tryClangTidy('clang-tidy');
if (hasClangTidy) {
return 'clang-tidy';
}
return null;
}
function runClangTidy(commandName, filePath) {
return new Promise((resolve, reject) => {
const process = spawn(
commandName,
[
filePath,
`-p=${gdevelopRootPath}/Binaries/embuild/compile_commands.json`,
`-header-filter=".*"`,
`--allow-no-checks`,
`--quiet`,
],
{ stdio: 'inherit' }
);
process.on('error', (error) => {
reject({ hasErrors: false });
});
process.on('close', (code) => {
if (code === 0) {
resolve({ hasErrors: false });
} else {
resolve({ hasErrors: true });
}
});
});
}
// Function to find all files in directory recursively excluding specified paths
function findFiles(directoryPath) {
let results = [];
const list = fs.readdirSync(directoryPath);
list.forEach((file) => {
const filePath = path.resolve(directoryPath, file);
const relativePath = path.relative(gdevelopRootPath, filePath);
const stat = fs.statSync(filePath);
if (stat && stat.isDirectory() && !excludedPaths.includes(relativePath)) {
results = results.concat(findFiles(filePath));
} else {
if (
(!supportedExtensions.includes(path.extname(filePath))) ||
path.basename(filePath) === '.gitignore' ||
excludedPaths.includes(relativePath)
) {
// Ignore the file.
} else {
results.push(filePath);
}
}
});
return results;
}
// Main function to run clang-tidy
async function main() {
console.log('Checking if clang-tidy is installed and works:');
const clangTidyCommand = await findClangTidy();
if (!clangTidyCommand) {
console.error(`❌ clang-tidy is not installed or not working.`);
process.exit(1);
}
const coreFilesToCheck = findFiles(coreSourcesRootPath);
const extensionFilesToCheck = findFiles(extensionSourcesRootPath);
const filesToCheck = [...coreFilesToCheck, ...extensionFilesToCheck];
// Run clang-tidy on each file.
const filesWithErrors = [];
let fileIndex = 0;
await makeSimplePromisePool(
filesToCheck.map((filePath) => async () => {
const { hasErrors } = await runClangTidy(clangTidyCommand, filePath);
if (hasErrors) {
filesWithErrors.push(filePath);
}
fileIndex++;
if (fileIndex % 10 === 0) {
console.log(
`ℹ️ Checked ${fileIndex} out of ${filesToCheck.length} files.`
);
}
}),
30
);
if (filesWithErrors.length > 0) {
console.error(`❌ clang-tidy found errors in the following files:`);
for (let filePath of filesWithErrors) {
console.error(` - ${filePath}`);
}
process.exit(1);
} else {
console.log(`✅ All files passed clang-tidy checks.`);
}
}
main();