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:
parent
f172136752
commit
5fee5b0e22
3 changed files with 37 additions and 32 deletions
|
@ -1,5 +1,6 @@
|
||||||
const { GitProcess } = require('dugite');
|
const { GitProcess } = require('dugite');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
const os = require('os');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
const ELECTRON_DIR = path.resolve(__dirname, '..', '..');
|
const ELECTRON_DIR = path.resolve(__dirname, '..', '..');
|
||||||
|
@ -95,7 +96,35 @@ async function getCurrentBranch (gitDir) {
|
||||||
return branch.trim();
|
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 = {
|
module.exports = {
|
||||||
|
chunkFilenames,
|
||||||
getCurrentBranch,
|
getCurrentBranch,
|
||||||
getElectronExec,
|
getElectronExec,
|
||||||
getOutDir,
|
getOutDir,
|
||||||
|
|
|
@ -9,6 +9,8 @@ const klaw = require('klaw');
|
||||||
const minimist = require('minimist');
|
const minimist = require('minimist');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
|
const { chunkFilenames } = require('./lib/utils');
|
||||||
|
|
||||||
const ELECTRON_ROOT = path.normalize(path.dirname(__dirname));
|
const ELECTRON_ROOT = path.normalize(path.dirname(__dirname));
|
||||||
const SOURCE_ROOT = path.resolve(ELECTRON_ROOT, '..');
|
const SOURCE_ROOT = path.resolve(ELECTRON_ROOT, '..');
|
||||||
const DEPOT_TOOLS = path.resolve(SOURCE_ROOT, 'third_party', 'depot_tools');
|
const DEPOT_TOOLS = path.resolve(SOURCE_ROOT, 'third_party', 'depot_tools');
|
||||||
|
@ -69,12 +71,11 @@ const LINTERS = [{
|
||||||
roots: ['shell'],
|
roots: ['shell'],
|
||||||
test: filename => filename.endsWith('.cc') || (filename.endsWith('.h') && !isObjCHeader(filename)),
|
test: filename => filename.endsWith('.cc') || (filename.endsWith('.h') && !isObjCHeader(filename)),
|
||||||
run: (opts, filenames) => {
|
run: (opts, filenames) => {
|
||||||
if (opts.fix) {
|
const clangFormatFlags = opts.fix ? ['--fix'] : [];
|
||||||
spawnAndCheckExitCode('python3', ['script/run-clang-format.py', '-r', '--fix', ...filenames]);
|
for (const chunk of chunkFilenames(filenames)) {
|
||||||
} else {
|
spawnAndCheckExitCode('python3', ['script/run-clang-format.py', ...clangFormatFlags, ...chunk]);
|
||||||
spawnAndCheckExitCode('python3', ['script/run-clang-format.py', '-r', ...filenames]);
|
cpplint(chunk);
|
||||||
}
|
}
|
||||||
cpplint(filenames);
|
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
key: 'objc',
|
key: 'objc',
|
||||||
|
|
|
@ -9,6 +9,8 @@ import * as streamJson from 'stream-json';
|
||||||
import { ignore as streamJsonIgnore } from 'stream-json/filters/Ignore';
|
import { ignore as streamJsonIgnore } from 'stream-json/filters/Ignore';
|
||||||
import { streamArray as streamJsonStreamArray } from 'stream-json/streamers/StreamArray';
|
import { streamArray as streamJsonStreamArray } from 'stream-json/streamers/StreamArray';
|
||||||
|
|
||||||
|
import { chunkFilenames } from './lib/utils';
|
||||||
|
|
||||||
const SOURCE_ROOT = path.normalize(path.dirname(__dirname));
|
const SOURCE_ROOT = path.normalize(path.dirname(__dirname));
|
||||||
const LLVM_BIN = path.resolve(
|
const LLVM_BIN = path.resolve(
|
||||||
SOURCE_ROOT,
|
SOURCE_ROOT,
|
||||||
|
@ -108,33 +110,6 @@ function getDepotToolsEnv (): NodeJS.ProcessEnv {
|
||||||
return depotToolsEnv;
|
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 (
|
async function runClangTidy (
|
||||||
outDir: string,
|
outDir: string,
|
||||||
filenames: string[],
|
filenames: string[],
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue