Skip to content

Commit 910064b

Browse files
johnniwinthercommit-bot@chromium.org
authored andcommitted
[cfe] Prepare compiler_common for use in annotated code tests
Change-Id: I99b52ad25fdbde7b2211f29795cfaad32b90c09f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/108405 Reviewed-by: Jens Johansen <[email protected]>
1 parent 84c7929 commit 910064b

File tree

5 files changed

+75
-39
lines changed

5 files changed

+75
-39
lines changed

pkg/front_end/lib/src/api_prototype/kernel_generator.dart

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import '../fasta/fasta_codes.dart' show messageMissingMain, noLength;
1818
import '../fasta/severity.dart' show Severity;
1919

2020
import '../kernel_generator_impl.dart'
21-
show generateKernel, generateKernelInternal;
21+
show CompilerResult, generateKernel, generateKernelInternal;
2222

2323
import 'compiler_options.dart' show CompilerOptions;
2424

@@ -43,9 +43,17 @@ import 'compiler_options.dart' show CompilerOptions;
4343
/// an error is reported.
4444
// TODO(sigmund): rename to kernelForScript?
4545
Future<Component> kernelForProgram(Uri source, CompilerOptions options) async {
46+
return (await kernelForProgramInternal(source, options))?.component;
47+
}
48+
49+
Future<CompilerResult> kernelForProgramInternal(
50+
Uri source, CompilerOptions options,
51+
{bool retainDataForTesting: false}) async {
4652
var pOptions = new ProcessedOptions(options: options, inputs: [source]);
4753
return await CompilerContext.runWithOptions(pOptions, (context) async {
48-
var component = (await generateKernelInternal())?.component;
54+
CompilerResult result = await generateKernelInternal(
55+
retainDataForTesting: retainDataForTesting);
56+
var component = result?.component;
4957
if (component == null) return null;
5058

5159
if (component.mainMethod == null) {
@@ -54,7 +62,7 @@ Future<Component> kernelForProgram(Uri source, CompilerOptions options) async {
5462
Severity.error);
5563
return null;
5664
}
57-
return component;
65+
return result;
5866
});
5967
}
6068

pkg/front_end/lib/src/kernel_generator_impl.dart

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ Future<CompilerResult> generateKernelInternal(
5050
{bool buildSummary: false,
5151
bool buildComponent: true,
5252
bool truncateSummary: false,
53-
bool includeOffsets: true}) async {
53+
bool includeOffsets: true,
54+
bool retainDataForTesting: false}) async {
5455
var options = CompilerContext.current.options;
5556
var fs = options.fileSystem;
5657

@@ -159,7 +160,8 @@ Future<CompilerResult> generateKernelInternal(
159160
return new CompilerResult(
160161
summary: summary,
161162
component: component,
162-
deps: new List<Uri>.from(CompilerContext.current.dependencies));
163+
deps: new List<Uri>.from(CompilerContext.current.dependencies),
164+
kernelTargetForTesting: retainDataForTesting ? kernelTarget : null);
163165
}, () => sourceLoader?.currentUriForCrashReporting ?? options.inputs.first);
164166
}
165167

@@ -177,5 +179,11 @@ class CompilerResult {
177179
/// using the compiler itself.
178180
final List<Uri> deps;
179181

180-
CompilerResult({this.summary, this.component, this.deps});
182+
/// The [KernelTarget] used to generated the component.
183+
///
184+
/// This is only provided for use in testing.
185+
final KernelTarget kernelTargetForTesting;
186+
187+
CompilerResult(
188+
{this.summary, this.component, this.deps, this.kernelTargetForTesting});
181189
}

pkg/front_end/lib/src/testing/compiler_common.dart

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import 'dart:async' show Future;
1010
import 'package:kernel/ast.dart' show Library, Component;
1111

1212
import '../api_prototype/front_end.dart'
13-
show CompilerOptions, kernelForComponent, kernelForProgram, summaryFor;
13+
show
14+
CompilerOptions,
15+
kernelForComponent,
16+
kernelForProgramInternal,
17+
summaryFor;
1418

1519
import '../api_prototype/memory_file_system.dart' show MemoryFileSystem;
1620

@@ -19,18 +23,21 @@ import '../compute_platform_binaries_location.dart'
1923

2024
import '../fasta/hybrid_file_system.dart' show HybridFileSystem;
2125

26+
import '../kernel_generator_impl.dart' show CompilerResult;
27+
2228
/// Generate kernel for a script.
2329
///
2430
/// [scriptOrSources] can be a String, in which case it is the script to be
2531
/// compiled, or a Map containing source files. In which case, this function
2632
/// compiles the entry whose name is [fileName].
2733
///
2834
/// Wraps [kernelForProgram] with some default testing options (see [setup]).
29-
Future<Component> compileScript(dynamic scriptOrSources,
35+
Future<CompilerResult> compileScript(dynamic scriptOrSources,
3036
{fileName: 'main.dart',
3137
List<String> inputSummaries: const [],
3238
List<String> linkedDependencies: const [],
33-
CompilerOptions options}) async {
39+
CompilerOptions options,
40+
bool retainDataForTesting: false}) async {
3441
options ??= new CompilerOptions();
3542
Map<String, dynamic> sources;
3643
if (scriptOrSources is String) {
@@ -41,10 +48,11 @@ Future<Component> compileScript(dynamic scriptOrSources,
4148
}
4249
await setup(options, sources,
4350
inputSummaries: inputSummaries, linkedDependencies: linkedDependencies);
44-
return await kernelForProgram(toTestUri(fileName), options);
51+
return await kernelForProgramInternal(toTestUri(fileName), options,
52+
retainDataForTesting: retainDataForTesting);
4553
}
4654

47-
/// Generate a component for a modular complation unit.
55+
/// Generate a component for a modular compilation unit.
4856
///
4957
/// Wraps [kernelForComponent] with some default testing options (see [setup]).
5058
Future<Component> compileUnit(List<String> inputs, Map<String, dynamic> sources,

pkg/front_end/test/fasta/assert_locations_test.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,9 @@ void main() {
137137
Expect.fail(
138138
"Unexpected message: ${message.plainTextFormatted.join('\n')}");
139139
};
140-
Component p = await compileScript(test.source,
141-
options: options, fileName: 'synthetic-test.dart');
140+
Component p = (await compileScript(test.source,
141+
options: options, fileName: 'synthetic-test.dart'))
142+
?.component;
142143
Expect.isNotNull(p);
143144
VerifyingVisitor visitor = new VerifyingVisitor(test);
144145
p.mainMethod.enclosingLibrary.accept(visitor);

pkg/front_end/test/kernel_generator_test.dart

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ main() {
4343
..compileSdk = true // To prevent FE from loading an sdk-summary.
4444
..onDiagnostic = errors.add;
4545

46-
var component =
47-
await compileScript('main() => print("hi");', options: options);
46+
Component component =
47+
(await compileScript('main() => print("hi");', options: options))
48+
?.component;
4849
expect(component, isNotNull);
4950
expect(errors, isNotEmpty);
5051
});
@@ -56,8 +57,9 @@ main() {
5657
Uri.parse('org-dartlang-test:///not_existing_summary_file')
5758
..onDiagnostic = errors.add;
5859

59-
var component =
60-
await compileScript('main() => print("hi");', options: options);
60+
Component component =
61+
(await compileScript('main() => print("hi");', options: options))
62+
?.component;
6163
expect(component, isNotNull);
6264
expect(errors, isNotEmpty);
6365
});
@@ -69,8 +71,9 @@ main() {
6971
// contains broken URIs to ensure we do not attempt to lookup for
7072
// sources of the sdk directly.
7173
..librariesSpecificationUri = invalidCoreLibsSpecUri;
72-
var component =
73-
await compileScript('main() => print("hi");', options: options);
74+
Component component =
75+
(await compileScript('main() => print("hi");', options: options))
76+
?.component;
7477
var core = component.libraries.firstWhere(isDartCoreLibrary);
7578
var printMember = core.members.firstWhere((m) => m.name.name == 'print');
7679

@@ -87,8 +90,10 @@ main() {
8790
});
8891

8992
test('generated program contains source-info', () async {
90-
var component = await compileScript('a() => print("hi"); main() {}',
91-
fileName: 'a.dart');
93+
Component component = (await compileScript(
94+
'a() => print("hi"); main() {}',
95+
fileName: 'a.dart'))
96+
?.component;
9297
// Kernel always store an empty '' key in the map, so there is always at
9398
// least one. Having more means that source-info is added.
9499
expect(component.uriToSource.keys.length, greaterThan(1));
@@ -98,8 +103,10 @@ main() {
98103
});
99104

100105
test('code from summary dependencies are marked external', () async {
101-
var component = await compileScript('a() => print("hi"); main() {}',
102-
fileName: 'a.dart');
106+
Component component = (await compileScript(
107+
'a() => print("hi"); main() {}',
108+
fileName: 'a.dart'))
109+
?.component;
103110
for (var lib in component.libraries) {
104111
if (lib.importUri.scheme == 'dart') {
105112
expect(lib.isExternal, isTrue);
@@ -108,36 +115,40 @@ main() {
108115

109116
// Pretend that the compiled code is a summary
110117
var bytes = serializeComponent(component);
111-
component = await compileScript(
112-
{
113-
'b.dart': 'import "a.dart" as m; b() => m.a(); main() {}',
114-
'summary.dill': bytes
115-
},
116-
fileName: 'b.dart',
117-
inputSummaries: ['summary.dill']);
118+
component = (await compileScript(
119+
{
120+
'b.dart': 'import "a.dart" as m; b() => m.a(); main() {}',
121+
'summary.dill': bytes
122+
},
123+
fileName: 'b.dart',
124+
inputSummaries: ['summary.dill']))
125+
?.component;
118126

119127
var aLib = component.libraries
120128
.firstWhere((lib) => lib.importUri.path == '/a/b/c/a.dart');
121129
expect(aLib.isExternal, isTrue);
122130
});
123131

124132
test('code from linked dependencies are not marked external', () async {
125-
var component = await compileScript('a() => print("hi"); main() {}',
126-
fileName: 'a.dart');
133+
Component component = (await compileScript(
134+
'a() => print("hi"); main() {}',
135+
fileName: 'a.dart'))
136+
?.component;
127137
for (var lib in component.libraries) {
128138
if (lib.importUri.scheme == 'dart') {
129139
expect(lib.isExternal, isTrue);
130140
}
131141
}
132142

133143
var bytes = serializeComponent(component);
134-
component = await compileScript(
135-
{
136-
'b.dart': 'import "a.dart" as m; b() => m.a(); main() {}',
137-
'link.dill': bytes
138-
},
139-
fileName: 'b.dart',
140-
linkedDependencies: ['link.dill']);
144+
component = (await compileScript(
145+
{
146+
'b.dart': 'import "a.dart" as m; b() => m.a(); main() {}',
147+
'link.dill': bytes
148+
},
149+
fileName: 'b.dart',
150+
linkedDependencies: ['link.dill']))
151+
?.component;
141152

142153
var aLib = component.libraries
143154
.firstWhere((lib) => lib.importUri.path == '/a/b/c/a.dart');

0 commit comments

Comments
 (0)