chore: chunk filenames when linting C++ files (#34237)

* chore: chunk filenames when linting C++ files

* chore: refactor code for better readability

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* chore: further tweak

* chore: limit all platforms to 4095 characters on command line

* chore: use python3

* Revert "chore: use python3"

Co-authored-by: Charles Kerr <charles@charleskerr.com>
Co-authored-by: Cheng Zhao <zcbenz@gmail.com>
This commit is contained in:
David Sanders 2022-06-22 03:23:11 -07:00 committed by GitHub
parent f172136752
commit 5fee5b0e22
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 32 deletions

View file

@ -1,5 +1,6 @@
const { GitProcess } = require('dugite');
const fs = require('fs');
const os = require('os');
const path = require('path');
const ELECTRON_DIR = path.resolve(__dirname, '..', '..');
@ -95,7 +96,35 @@ async function getCurrentBranch (gitDir) {
return branch.trim();
}
function chunkFilenames (filenames, offset = 0) {
// Windows has a max command line length of 2047 characters, so we can't
// provide too many filenames without going over that. To work around that,
// chunk up a list of filenames such that it won't go over that limit when
// used as args. Other platforms may have higher limits, but 4095 might be
// the limit on Linux systems according to `termios(3)`, so cap it there.
const MAX_FILENAME_ARGS_LENGTH =
(os.platform() === 'win32' ? 2047 : 4095) - offset;
return filenames.reduce(
(chunkedFilenames, filename) => {
const currChunk = chunkedFilenames[chunkedFilenames.length - 1];
const currChunkLength = currChunk.reduce(
(totalLength, _filename) => totalLength + _filename.length + 1,
0
);
if (currChunkLength + filename.length + 1 > MAX_FILENAME_ARGS_LENGTH) {
chunkedFilenames.push([filename]);
} else {
currChunk.push(filename);
}
return chunkedFilenames;
},
[[]]
);
}
module.exports = {
chunkFilenames,
getCurrentBranch,
getElectronExec,
getOutDir,

View file

@ -9,6 +9,8 @@ const klaw = require('klaw');
const minimist = require('minimist');
const path = require('path');
const { chunkFilenames } = require('./lib/utils');
const ELECTRON_ROOT = path.normalize(path.dirname(__dirname));
const SOURCE_ROOT = path.resolve(ELECTRON_ROOT, '..');
const DEPOT_TOOLS = path.resolve(SOURCE_ROOT, 'third_party', 'depot_tools');
@ -69,12 +71,11 @@ const LINTERS = [{
roots: ['shell'],
test: filename => filename.endsWith('.cc') || (filename.endsWith('.h') && !isObjCHeader(filename)),
run: (opts, filenames) => {
if (opts.fix) {
spawnAndCheckExitCode('python3', ['script/run-clang-format.py', '-r', '--fix', ...filenames]);
} else {
spawnAndCheckExitCode('python3', ['script/run-clang-format.py', '-r', ...filenames]);
const clangFormatFlags = opts.fix ? ['--fix'] : [];
for (const chunk of chunkFilenames(filenames)) {
spawnAndCheckExitCode('python3', ['script/run-clang-format.py', ...clangFormatFlags, ...chunk]);
cpplint(chunk);
}
cpplint(filenames);
}
}, {
key: 'objc',

View file

@ -9,6 +9,8 @@ import * as streamJson from 'stream-json';
import { ignore as streamJsonIgnore } from 'stream-json/filters/Ignore';
import { streamArray as streamJsonStreamArray } from 'stream-json/streamers/StreamArray';
import { chunkFilenames } from './lib/utils';
const SOURCE_ROOT = path.normalize(path.dirname(__dirname));
const LLVM_BIN = path.resolve(
SOURCE_ROOT,
@ -108,33 +110,6 @@ function getDepotToolsEnv (): NodeJS.ProcessEnv {
return depotToolsEnv;
}
function chunkFilenames (filenames: string[], offset: number = 0): string[][] {
// Windows has a max command line length of 2047 characters, so we can't
// provide too many filenames without going over that. To work around that,
// chunk up a list of filenames such that it won't go over that limit when
// used as args. Use a much higher limit on other platforms which will
// effectively be a no-op.
const MAX_FILENAME_ARGS_LENGTH =
PLATFORM === 'win32' ? 2047 - offset : 100 * 1024;
return filenames.reduce(
(chunkedFilenames: string[][], filename) => {
const currChunk = chunkedFilenames[chunkedFilenames.length - 1];
const currChunkLength = currChunk.reduce(
(totalLength, _filename) => totalLength + _filename.length + 1,
0
);
if (currChunkLength + filename.length + 1 > MAX_FILENAME_ARGS_LENGTH) {
chunkedFilenames.push([filename]);
} else {
currChunk.push(filename);
}
return chunkedFilenames;
},
[[]]
);
}
async function runClangTidy (
outDir: string,
filenames: string[],