Skip to content

Commit 18fc3b4

Browse files
authored
Allow engine variants other than "host_debug" to be clang-tidy linted (flutter#29668)
1 parent ed93b0f commit 18fc3b4

File tree

8 files changed

+83
-67
lines changed

8 files changed

+83
-67
lines changed

ci/lint.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,5 @@ cd "$SCRIPT_DIR"
4141
"$DART" \
4242
--disable-dart-dev \
4343
"$SRC_DIR/flutter/tools/clang_tidy/bin/main.dart" \
44-
--compile-commands="$COMPILE_COMMANDS" \
45-
--repo="$SRC_DIR/flutter" \
44+
--src-dir="$SRC_DIR" \
4645
"$@"

tools/clang_tidy/README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
# clang_tidy
22

3-
This is a Dart program/library that runs clang_tidy over modified files. It
4-
takes two mandatory arguments that point at a compile_commands.json command
5-
and the root of the Flutter engine repo:
3+
This is a Dart program/library that runs clang_tidy over modified files in the Flutter engine repo.
4+
5+
By default the linter runs on the repo files changed contained in `src/out/host_debug/compile_commands.json` command.
6+
To check files other than in `host_debug` use `--target-variant android_debug_unopt`,
7+
`--target-variant ios_debug_sim_unopt`, etc.
8+
9+
Alternatively, use `--compile-commands` to specify a path to a `compile_commands.json` file.
610

711
```
8-
$ bin/main.dart --compile-commands <compile_commands.json-path> --repo <path-to-repo>
12+
$ bin/main.dart --target-variant <engine-variant>
13+
$ bin/main.dart --compile-commands <compile_commands.json-path>
14+
$ bin/main.dart --help
915
```

tools/clang_tidy/bin/main.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
// Runs clang-tidy on files with changes.
88
//
99
// Basic Usage:
10-
// dart bin/main.dart --compile-commands <path to compile_commands.json> \
11-
// --repo <path to git repository> \
10+
// dart bin/main.dart --compile-commands <path to compile_commands.json>
11+
// dart bin/main.dart --target-variant <engine-variant>
1212
//
1313
// User environment variable FLUTTER_LINT_ALL to run on all files.
1414

tools/clang_tidy/lib/clang_tidy.dart

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ class ClangTidy {
4545
/// will otherwise go to stderr.
4646
ClangTidy({
4747
required io.File buildCommandsPath,
48-
required io.Directory repoPath,
4948
String checksArg = '',
5049
bool lintAll = false,
5150
bool fix = false,
@@ -54,7 +53,6 @@ class ClangTidy {
5453
}) :
5554
options = Options(
5655
buildCommandsPath: buildCommandsPath,
57-
repoPath: repoPath,
5856
checksArg: checksArg,
5957
lintAll: lintAll,
6058
fix: fix,
@@ -229,16 +227,8 @@ class ClangTidy {
229227
if (job.result.exitCode == 0) {
230228
continue;
231229
}
232-
if (job.exception != null) {
233-
_errSink.writeln(
234-
'\n❗ A clang-tidy job failed to run, aborting:\n${job.exception}',
235-
);
236-
result = 1;
237-
break;
238-
} else {
239-
_errSink.writeln('❌ Failures for ${job.name}:');
240-
_errSink.writeln(job.result.stdout);
241-
}
230+
_errSink.writeln('❌ Failures for ${job.name}:');
231+
_errSink.writeln(job.result.stdout);
242232
result = 1;
243233
}
244234
return result;

tools/clang_tidy/lib/src/command.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,10 @@ class Command {
134134
filePath,
135135
if (checks != null)
136136
checks,
137-
if (fix)
137+
if (fix) ...<String>[
138138
'--fix',
139+
'--format-style=file',
140+
],
139141
'--',
140142
];
141143
args.addAll(tidyArgs.split(' '));

tools/clang_tidy/lib/src/options.dart

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55
import 'dart:io' as io show Directory, File, Platform, stderr;
66

77
import 'package:args/args.dart';
8+
import 'package:path/path.dart' as path;
9+
10+
// Path to root of the flutter/engine repository containing this script.
11+
final String _engineRoot = path.dirname(path.dirname(path.dirname(path.dirname(path.fromUri(io.Platform.script)))));
812

913
/// A class for organizing the options to the Engine linter, and the files
1014
/// that it operates on.
1115
class Options {
1216
/// Builds an instance of [Options] from the arguments.
1317
Options({
1418
required this.buildCommandsPath,
15-
required this.repoPath,
1619
this.help = false,
1720
this.verbose = false,
1821
this.checksArg = '',
@@ -30,7 +33,6 @@ class Options {
3033
return Options(
3134
errorMessage: message,
3235
buildCommandsPath: io.File('none'),
33-
repoPath: io.Directory('none'),
3436
errSink: errSink,
3537
);
3638
}
@@ -41,21 +43,20 @@ class Options {
4143
return Options(
4244
help: true,
4345
buildCommandsPath: io.File('none'),
44-
repoPath: io.Directory('none'),
4546
errSink: errSink,
4647
);
4748
}
4849

4950
/// Builds an [Options] instance with an [ArgResults] instance.
5051
factory Options._fromArgResults(
5152
ArgResults options, {
53+
required io.File buildCommandsPath,
5254
StringSink? errSink,
5355
}) {
5456
return Options(
5557
help: options['help'] as bool,
5658
verbose: options['verbose'] as bool,
57-
buildCommandsPath: io.File(options['compile-commands'] as String),
58-
repoPath: io.Directory(options['repo'] as String),
59+
buildCommandsPath: buildCommandsPath,
5960
checksArg: options.wasParsed('checks') ? options['checks'] as String : '',
6061
lintAll: io.Platform.environment['FLUTTER_LINT_ALL'] != null ||
6162
options['lint-all'] as bool,
@@ -70,7 +71,17 @@ class Options {
7071
StringSink? errSink,
7172
}) {
7273
final ArgResults argResults = _argParser.parse(arguments);
73-
final String? message = _checkArguments(argResults);
74+
75+
String? buildCommandsPath = argResults['compile-commands'] as String?;
76+
// path/to/engine/src/out/variant/compile_commands.json
77+
buildCommandsPath ??= path.join(
78+
argResults['src-dir'] as String,
79+
'out',
80+
argResults['target-variant'] as String,
81+
'compile_commands.json',
82+
);
83+
final io.File buildCommands = io.File(buildCommandsPath);
84+
final String? message = _checkArguments(argResults, buildCommands);
7485
if (message != null) {
7586
return Options._error(message, errSink: errSink);
7687
}
@@ -79,14 +90,17 @@ class Options {
7990
}
8091
return Options._fromArgResults(
8192
argResults,
93+
buildCommandsPath: buildCommands,
8294
errSink: errSink,
8395
);
8496
}
8597

8698
static final ArgParser _argParser = ArgParser()
8799
..addFlag(
88100
'help',
101+
abbr: 'h',
89102
help: 'Print help.',
103+
negatable: false,
90104
)
91105
..addFlag(
92106
'lint-all',
@@ -103,14 +117,25 @@ class Options {
103117
help: 'Print verbose output.',
104118
defaultsTo: false,
105119
)
106-
..addOption(
107-
'repo',
108-
help: 'Use the given path as the repo path',
109-
)
110120
..addOption(
111121
'compile-commands',
112122
help: 'Use the given path as the source of compile_commands.json. This '
113-
'file is created by running tools/gn',
123+
'file is created by running "tools/gn". Cannot be used with --target-variant '
124+
'or --src-dir.',
125+
)
126+
..addOption(
127+
'target-variant',
128+
aliases: <String>['variant'],
129+
help: 'The engine variant directory containing compile_commands.json '
130+
'created by running "tools/gn". Cannot be used with --compile-commands.',
131+
valueHelp: 'host_debug|android_debug_unopt|ios_debug|ios_debug_sim_unopt',
132+
defaultsTo: 'host_debug',
133+
)
134+
..addOption(
135+
'src-dir',
136+
help: 'Path to the engine src directory. Cannot be used with --compile-commands.',
137+
valueHelp: 'path/to/engine/src',
138+
defaultsTo: path.dirname(_engineRoot),
114139
)
115140
..addOption(
116141
'checks',
@@ -129,7 +154,7 @@ class Options {
129154
final io.File buildCommandsPath;
130155

131156
/// The root of the flutter/engine repository.
132-
final io.Directory repoPath;
157+
final io.Directory repoPath = io.Directory(_engineRoot);
133158

134159
/// Arguments to plumb through to clang-tidy formatted as a command line
135160
/// argument.
@@ -156,35 +181,30 @@ class Options {
156181
_errSink.writeln(message);
157182
}
158183
_errSink.writeln(
159-
'Usage: bin/main.dart [--help] [--lint-all] [--fix] [--verbose] [--diff-branch]',
184+
'Usage: bin/main.dart [--help] [--lint-all] [--fix] [--verbose] [--diff-branch] [--target-variant variant] [--src-dir path/to/engine/src]',
160185
);
161186
_errSink.writeln(_argParser.usage);
162187
}
163188

164189
/// Command line argument validation.
165-
static String? _checkArguments(ArgResults argResults) {
190+
static String? _checkArguments(ArgResults argResults, io.File buildCommandsPath) {
166191
if (argResults.wasParsed('help')) {
167192
return null;
168193
}
169194

170-
if (!argResults.wasParsed('compile-commands')) {
171-
return 'ERROR: The --compile-commands argument is required.';
195+
final bool compileCommandsParsed = argResults.wasParsed('compile-commands');
196+
if (compileCommandsParsed && argResults.wasParsed('target-variant')) {
197+
return 'ERROR: --compile-commands option cannot be used with --target-variant.';
172198
}
173199

174-
if (!argResults.wasParsed('repo')) {
175-
return 'ERROR: The --repo argument is required.';
200+
if (compileCommandsParsed && argResults.wasParsed('src-dir')) {
201+
return 'ERROR: --compile-commands option cannot be used with --src-dir.';
176202
}
177203

178-
final io.File buildCommandsPath = io.File(argResults['compile-commands'] as String);
179204
if (!buildCommandsPath.existsSync()) {
180205
return "ERROR: Build commands path ${buildCommandsPath.absolute.path} doesn't exist.";
181206
}
182207

183-
final io.Directory repoPath = io.Directory(argResults['repo'] as String);
184-
if (!repoPath.existsSync()) {
185-
return "ERROR: Repo path ${repoPath.absolute.path} doesn't exist.";
186-
}
187-
188208
return null;
189209
}
190210
}

0 commit comments

Comments
 (0)