Speed up clang-tidy script by skipping third_party files (flutter/engine#29640)

This commit is contained in:
Jenn Magder
2021-11-10 11:13:06 -08:00
committed by GitHub
parent c859ffd744
commit d5d8169a54
3 changed files with 40 additions and 26 deletions

View File

@@ -110,7 +110,7 @@ class ClangTidy {
final List<dynamic> buildCommandsData = jsonDecode(
options.buildCommandsPath.readAsStringSync(),
) as List<dynamic>;
final List<Command> changedFileBuildCommands = getLintCommandsForChangedFiles(
final List<Command> changedFileBuildCommands = await getLintCommandsForChangedFiles(
buildCommandsData,
changedFiles,
);
@@ -165,20 +165,20 @@ class ClangTidy {
/// Given a build commands json file, and the files with local changes,
/// compute the lint commands to run.
@visibleForTesting
List<Command> getLintCommandsForChangedFiles(
Future<List<Command>> getLintCommandsForChangedFiles(
List<dynamic> buildCommandsData,
List<io.File> changedFiles,
) {
final List<Command> buildCommands = <Command>[
for (final dynamic c in buildCommandsData)
Command.fromMap(c as Map<String, dynamic>),
];
return <Command>[
for (final Command c in buildCommands)
if (c.containsAny(changedFiles))
c,
];
) async {
final List<Command> buildCommands = <Command>[];
for (final dynamic data in buildCommandsData) {
final Command command = Command.fromMap(data as Map<String, dynamic>);
final LintAction lintAction = await command.lintAction;
// Short-circuit the expensive containsAny call for the many third_party files.
if (lintAction != LintAction.skipThirdParty && command.containsAny(changedFiles)) {
buildCommands.add(command);
}
}
return buildCommands;
}
Future<_ComputeJobsResult> _computeJobs(
@@ -212,6 +212,9 @@ class ClangTidy {
case LintAction.skipThirdParty:
_outSink.writeln('🔷 ignoring $relativePath (third_party)');
break;
case LintAction.skipMissing:
_outSink.writeln('🔷 ignoring $relativePath (missing)');
break;
}
}
return _ComputeJobsResult(jobs, sawMalformed);
@@ -239,5 +242,3 @@ class ClangTidy {
return result;
}
}

View File

@@ -26,6 +26,9 @@ enum LintAction {
/// Fail due to a malformed FLUTTER_NOLINT comment.
failMalformedNoLint,
/// Ignore because the file doesn't exist locally.
skipMissing,
}
/// A compilation command and methods to generate the lint command and job for
@@ -86,20 +89,20 @@ class Command {
r'//\s*FLUTTER_NOLINT(: https://github.com/flutter/flutter/issues/\d+)?',
);
LintAction? _lintAction;
/// The type of lint that is appropriate for this command.
Future<LintAction> get lintAction async =>
_lintAction ??= await getLintAction(filePath);
late final Future<LintAction> lintAction = getLintAction(filePath);
/// Determine the lint action for the file at `path`.
@visibleForTesting
static Future<LintAction> getLintAction(String filePath) {
static Future<LintAction> getLintAction(String filePath) async {
if (path.split(filePath).contains('third_party')) {
return Future<LintAction>.value(LintAction.skipThirdParty);
return LintAction.skipThirdParty;
}
final io.File file = io.File(filePath);
if (!file.existsSync()) {
return LintAction.skipMissing;
}
final Stream<String> lines = file.openRead()
.transform(utf8.decoder)
.transform(const LineSplitter());

View File

@@ -105,8 +105,8 @@ Future<int> main(List<String> args) async {
final ClangTidy clangTidy = ClangTidy.fromCommandLine(
<String>[
'--compile-commands',
// This just has to exist.
io.Platform.executable,
// This file needs to exist, and be UTF8 line-parsable.
io.Platform.script.path,
'--repo',
'/does/not/exist',
],
@@ -168,7 +168,7 @@ Future<int> main(List<String> args) async {
'file': filePath,
},
];
final List<Command> commands = clangTidy.getLintCommandsForChangedFiles(
final List<Command> commands = await clangTidy.getLintCommandsForChangedFiles(
buildCommandsData,
<io.File>[],
);
@@ -186,7 +186,9 @@ Future<int> main(List<String> args) async {
outSink: outBuffer,
errSink: errBuffer,
);
const String filePath = '/path/to/a/source_file.cc';
// This file needs to exist, and be UTF8 line-parsable.
final String filePath = io.Platform.script.path;
final List<dynamic> buildCommandsData = <Map<String, dynamic>>[
<String, dynamic>{
'directory': '/unused',
@@ -194,7 +196,7 @@ Future<int> main(List<String> args) async {
'file': filePath,
},
];
final List<Command> commands = clangTidy.getLintCommandsForChangedFiles(
final List<Command> commands = await clangTidy.getLintCommandsForChangedFiles(
buildCommandsData,
<io.File>[io.File(filePath)],
);
@@ -220,6 +222,14 @@ Future<int> main(List<String> args) async {
expect(lintAction, equals(LintAction.skipThirdParty));
});
test('Command getLintAction flags missing files', () async {
final LintAction lintAction = await Command.getLintAction(
'/does/not/exist',
);
expect(lintAction, equals(LintAction.skipMissing));
});
test('Command getLintActionFromContents flags FLUTTER_NOLINT', () async {
final LintAction lintAction = await Command.lintActionFromContents(
Stream<String>.fromIterable(<String>[