Skip to content

Commit 1114d0c

Browse files
johnniwintherCommit Queue
authored and
Commit Queue
committed
[cfe] Add expectation test for file offsets
In preparation for fixing #51812 Change-Id: I810be0a0330f0bf90fc1230b5a2d17f695d6e46b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/292007 Reviewed-by: Jens Johansen <[email protected]> Commit-Queue: Johnni Winther <[email protected]>
1 parent f1dcdd0 commit 1114d0c

19 files changed

+515
-12
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ class CompilerOptions {
194194
/// Dumped data is printed in stdout.
195195
bool debugDump = false;
196196

197+
/// Whether to show file offsets when [debugDump] is `true`.
198+
bool debugDumpShowOffsets = false;
199+
197200
/// Whether to omit the platform when serializing the result from a `fasta
198201
/// compile` run.
199202
bool omitPlatform = false;
@@ -350,6 +353,7 @@ class CompilerOptions {
350353
}
351354
if (!ignoreDebugDump) {
352355
if (debugDump != other.debugDump) return false;
356+
if (debugDumpShowOffsets != other.debugDumpShowOffsets) return false;
353357
}
354358
if (omitPlatform != other.omitPlatform) return false;
355359
if (setExitCodeOnProblem != other.setExitCodeOnProblem) return false;

pkg/front_end/lib/src/base/command_line_options.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class Flags {
4545
static const String sdk = "--sdk";
4646
static const String singleRootBase = "--single-root-base";
4747
static const String singleRootScheme = "--single-root-scheme";
48+
static const String showOffsets = "--show-offsets";
4849
static const String verbose = "--verbose";
4950
static const String verbosity = "--verbosity";
5051
static const String verify = "--verify";
@@ -59,6 +60,8 @@ class Options {
5960
const Option(Flags.compileSdk, const UriValue());
6061
static const Option<bool> dumpIr =
6162
const Option(Flags.dumpIr, const BoolValue(false));
63+
static const Option<bool> showOffsets =
64+
const Option(Flags.showOffsets, const BoolValue(false));
6265
static const Option<List<String>?> enableExperiment =
6366
const Option(Flags.enableExperiment, const StringListValue());
6467
static const Option<bool> enableUnscheduledExperiments =

pkg/front_end/lib/src/base/processed_options.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ class ProcessedOptions {
173173

174174
bool get debugDump => _raw.debugDump;
175175

176+
bool get debugDumpShowOffsets => _raw.debugDumpShowOffsets;
177+
176178
bool get omitPlatform => _raw.omitPlatform;
177179

178180
bool get setExitCodeOnProblem => _raw.setExitCodeOnProblem;
@@ -787,6 +789,7 @@ class ProcessedOptions {
787789
sb.writeln('exit on problem: ${setExitCodeOnProblem}');
788790
sb.writeln('Embed sources: ${embedSourceText}');
789791
sb.writeln('debugDump: ${debugDump}');
792+
sb.writeln('debugDumpShowOffsets: ${debugDumpShowOffsets}');
790793
sb.writeln('verbose: ${verbose}');
791794
sb.writeln('verify: ${verify}');
792795
return '$sb';

pkg/front_end/lib/src/fasta/kernel/utils.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ void printQualifiedNameOn(Member? member, StringSink sink) {
7373
/// [libraryFilter] is provided, then only libraries that satisfy it are
7474
/// printed.
7575
void printComponentText(Component? component,
76-
{bool Function(Library library)? libraryFilter}) {
76+
{bool Function(Library library)? libraryFilter, bool showOffsets = false}) {
7777
if (component == null) return;
7878
StringBuffer sb = new StringBuffer();
79-
Printer printer = new Printer(sb);
79+
Printer printer = new Printer(sb, showOffsets: showOffsets);
8080
printer.writeComponentProblems(component);
8181
for (Library library in component.libraries) {
8282
if (libraryFilter != null && !libraryFilter(library)) continue;

pkg/front_end/lib/src/kernel_generator_impl.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ Future<CompilerResult> _buildInternal(
152152
}
153153
if (options.debugDump) {
154154
printComponentText(summaryComponent,
155-
libraryFilter: kernelTarget.isSourceLibraryForDebugging);
155+
libraryFilter: kernelTarget.isSourceLibraryForDebugging,
156+
showOffsets: options.debugDumpShowOffsets);
156157
}
157158

158159
// Create the requested component ("truncating" or not).
@@ -209,7 +210,8 @@ Future<CompilerResult> _buildInternal(
209210
component = buildResult.component;
210211
if (options.debugDump) {
211212
printComponentText(component,
212-
libraryFilter: kernelTarget.isSourceLibraryForDebugging);
213+
libraryFilter: kernelTarget.isSourceLibraryForDebugging,
214+
showOffsets: options.debugDumpShowOffsets);
213215
}
214216
options.ticker.logMs("Generated component");
215217
}

pkg/front_end/test/fasta/testing/suite.dart

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ const List<Option> folderOptionsSpecification = [
224224
noVerifyCmd,
225225
Options.target,
226226
Options.defines,
227+
Options.showOffsets,
227228
];
228229

229230
const Option<bool> fixNnbdReleaseVersion =
@@ -260,6 +261,7 @@ class FolderOptions {
260261
final bool noVerify;
261262
final String target;
262263
final String? overwriteCurrentSdkVersion;
264+
final bool showOffsets;
263265

264266
FolderOptions(this._explicitExperimentalFlags,
265267
{this.enableUnscheduledExperiments,
@@ -273,7 +275,8 @@ class FolderOptions {
273275
this.noVerify = false,
274276
this.target = "vm",
275277
// can be null
276-
this.overwriteCurrentSdkVersion})
278+
this.overwriteCurrentSdkVersion,
279+
this.showOffsets = false})
277280
// ignore: unnecessary_null_comparison
278281
: assert(nnbdAgnosticMode != null),
279282
assert(
@@ -447,6 +450,7 @@ class FastaContext extends ChainContext with MatchContext {
447450
bool noVerify = false;
448451
Map<String, String>? defines = {};
449452
String target = "vm";
453+
bool showOffsets = false;
450454
if (directory.uri == baseUri) {
451455
folderOptions = new FolderOptions({},
452456
enableUnscheduledExperiments: enableUnscheduledExperiments,
@@ -458,7 +462,8 @@ class FastaContext extends ChainContext with MatchContext {
458462
nnbdAgnosticMode: nnbdAgnosticMode,
459463
defines: defines,
460464
noVerify: noVerify,
461-
target: target);
465+
target: target,
466+
showOffsets: showOffsets);
462467
} else {
463468
File optionsFile =
464469
new File.fromUri(directory.uri.resolve('folder.options'));
@@ -484,6 +489,7 @@ class FastaContext extends ChainContext with MatchContext {
484489
Options.forceConstructorTearOffLowering.read(parsedOptions);
485490
nnbdAgnosticMode = Options.nnbdAgnosticMode.read(parsedOptions);
486491
defines = parsedOptions.defines;
492+
showOffsets = Options.showOffsets.read(parsedOptions);
487493
if (Options.noDefines.read(parsedOptions)) {
488494
if (defines.isNotEmpty) {
489495
throw "Can't have no defines and specific defines "
@@ -509,7 +515,8 @@ class FastaContext extends ChainContext with MatchContext {
509515
defines: defines,
510516
noVerify: noVerify,
511517
target: target,
512-
overwriteCurrentSdkVersion: overwriteCurrentSdkVersionArgument);
518+
overwriteCurrentSdkVersion: overwriteCurrentSdkVersionArgument,
519+
showOffsets: showOffsets);
513520
} else {
514521
folderOptions = _computeFolderOptions(directory.parent);
515522
}

pkg/front_end/test/utils/kernel_chain.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ class Print extends Step<ComponentResult, ComponentResult, ChainContext> {
158158
await CompilerContext.runWithDefaultOptions((compilerContext) async {
159159
compilerContext.uriToSource.addAll(component.uriToSource);
160160

161-
Printer printer = new Printer(sb);
161+
Printer printer = new Printer(sb,
162+
showOffsets: result.compilationSetup.folderOptions.showOffsets);
162163
for (Library library in component.libraries) {
163164
if (result.userLibraries.contains(library.importUri)) {
164165
printer.writeLibraryFile(library);
@@ -223,6 +224,12 @@ class MatchExpectation
223224

224225
Component componentToText = component;
225226
if (serializeFirst) {
227+
if (result.compilationSetup.folderOptions.showOffsets) {
228+
// Not all offsets are serialized so the output won't match and there is
229+
// currently no reason to check this.
230+
// TODO(johnniwinther): Find a way to avoid or verify the discrepancies.
231+
return new Future.value(new Result<ComponentResult>.pass(result));
232+
}
226233
// TODO(johnniwinther): Use library filter instead.
227234
List<Library> sdkLibraries =
228235
component.libraries.where((l) => !result.isUserLibrary(l)).toList();
@@ -296,7 +303,8 @@ class MatchExpectation
296303
// non-serialized step.
297304
errors.clear();
298305
}
299-
Printer printer = new Printer(buffer)
306+
Printer printer = new Printer(buffer,
307+
showOffsets: result.compilationSetup.folderOptions.showOffsets)
300308
..writeProblemsAsJson(
301309
"Problems in component", componentToText.problemsAsJson);
302310
libraries.forEach((Library library) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--show-offsets
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:math' show pi;
6+
7+
abstract class Shape {}
8+
9+
class Square implements Shape {
10+
final double length;
11+
Square(this.length);
12+
}
13+
14+
class Circle implements Shape {
15+
final double radius;
16+
Circle(this.radius);
17+
}
18+
19+
double calculateArea(Shape shape) => switch (shape) {
20+
Square(length: var l) when l >= 0 => l * l,
21+
Circle(radius: var r) when r >= 0 => pi * r * r,
22+
Square(length: var l) when l < 0 => -1,
23+
Circle(radius: var r) when r < 0 => -1,
24+
Shape() => 0
25+
};
26+
27+
testMain() {
28+
calculateArea(Circle(-123));
29+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
[217]import "dart:math" show pi;
6+
7+
[261]abstract class Shape extends core::Object {
8+
[261] synthetic constructor •() → self::Shape
9+
: [-1] super core::Object::• [-1]()
10+
[-1] ;
11+
}
12+
[277]class Square extends core::Object implements self::Shape {
13+
[318] final field core::double length;
14+
[328] constructor •([340] core::double length) → self::Square
15+
: [340] self::Square::length = [-1] [-1] length, [328] super core::Object::• [-1]()
16+
[-1] ;
17+
}
18+
[358]class Circle extends core::Object implements self::Shape {
19+
[399] final field core::double radius;
20+
[409] constructor •([421] core::double radius) → self::Circle
21+
: [421] self::Circle::radius = [-1] [-1] radius, [409] super core::Object::• [-1]()
22+
[-1] ;
23+
}
24+
[440]static method calculateArea([460] self::Shape shape) → core::double
25+
[470] return [470] [470] block {
26+
[470] [470] core::double #t1;
27+
[478] [478] final synthesized self::Shape #0#0 = [478] [478] shape;
28+
[506] [506] late final synthesized core::double #0#2 = [506] [506] [478] [478] #0#0{self::Square}.{self::Square::length}{core::double};
29+
[556] [556] late final synthesized core::double #0#4 = [556] [556] [478] [478] #0#0{self::Circle}.{self::Circle::radius}{core::double};
30+
[470] #L1:
31+
[470] {
32+
[527] {
33+
[512] [512] hoisted core::double l;
34+
[527] if([522] [522] [493] [493] [493] [493] [478] [478] #0#0 is{ForNonNullableByDefault} self::Square && [512]([512] let [512] final dynamic #t2 = [512] [512] l = [506] [506] #0#2 in [512] [512] true) && [522] [522] [520] [520] l.{core::num::>=}[522]([525] 0){(core::num) → core::bool}) {
35+
[470] [470] [470] #t1 = [532] [532] [530] [530] l.{core::double::*}[532]([534] l){(core::num) → core::double};
36+
[527] break #L1;
37+
}
38+
}
39+
[577] {
40+
[562] [562] hoisted core::double r;
41+
[577] if([572] [572] [543] [543] [543] [543] [478] [478] #0#0 is{ForNonNullableByDefault} self::Circle && [562]([562] let [562] final dynamic #t3 = [562] [562] r = [556] [556] #0#4 in [562] [562] true) && [572] [572] [570] [570] r.{core::num::>=}[572]([575] 0){(core::num) → core::bool}) {
42+
[470] [470] [470] #t1 = [587] [587] [583] [583] [580] [580] #C1.{core::double::*}[583]([585] r){(core::num) → core::double}.{core::double::*}[587]([589] r){(core::num) → core::double};
43+
[577] break #L1;
44+
}
45+
}
46+
[631] {
47+
[617] [617] hoisted core::double l;
48+
[631] if([627] [627] [598] [598] [493] [493] [478] [478] #0#0 is{ForNonNullableByDefault} self::Square && [617]([617] let [617] final dynamic #t4 = [617] [617] l = [506] [506] #0#2 in [617] [617] true) && [627] [627] [625] [625] l.{core::num::<}[627]([629] 0){(core::num) → core::bool}) {
49+
[470] [470] [470] #t1 = [634] [634] -1.0;
50+
[631] break #L1;
51+
}
52+
}
53+
[677] {
54+
[663] [663] hoisted core::double r;
55+
[677] if([673] [673] [644] [644] [543] [543] [478] [478] #0#0 is{ForNonNullableByDefault} self::Circle && [663]([663] let [663] final dynamic #t5 = [663] [663] r = [556] [556] #0#4 in [663] [663] true) && [673] [673] [671] [671] r.{core::num::<}[673]([675] 0){(core::num) → core::bool}) {
56+
[470] [470] [470] #t1 = [680] [680] -1.0;
57+
[677] break #L1;
58+
}
59+
}
60+
[698] {
61+
[698] if([690] [690] true) {
62+
[470] [470] [470] #t1 = [701] [701] 0.0;
63+
[698] break #L1;
64+
}
65+
}
66+
}
67+
} =>[470] [470] #t1;
68+
[711]static method testMain() → dynamic {
69+
[753] [726] [726] self::calculateArea [739]([740] new self::Circle::• [746]([747] -123.0));
70+
}
71+
72+
constants {
73+
#C1 = 3.141592653589793
74+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
[217]import "dart:math" show pi;
6+
7+
[261]abstract class Shape extends core::Object {
8+
[261] synthetic constructor •() → self::Shape
9+
: [-1] super core::Object::• [-1]()
10+
[-1] ;
11+
}
12+
[277]class Square extends core::Object implements self::Shape {
13+
[318] final field core::double length;
14+
[328] constructor •([340] core::double length) → self::Square
15+
: [340] self::Square::length = [-1] [-1] length, [328] super core::Object::• [-1]()
16+
[-1] ;
17+
}
18+
[358]class Circle extends core::Object implements self::Shape {
19+
[399] final field core::double radius;
20+
[409] constructor •([421] core::double radius) → self::Circle
21+
: [421] self::Circle::radius = [-1] [-1] radius, [409] super core::Object::• [-1]()
22+
[-1] ;
23+
}
24+
[440]static method calculateArea([460] self::Shape shape) → core::double
25+
[470] return [470] [470] block {
26+
[470] [470] core::double #t1;
27+
[478] [478] final synthesized self::Shape #0#0 = [478] [478] shape;
28+
[-1] function ##0#2#initializer() → core::double
29+
[-1] return [506] [506] [478] [478] #0#0{self::Square}.{self::Square::length}{core::double};
30+
[506] [506] late final synthesized core::double #0#2 = [-1] [-1] ##0#2#initializer [-1](){() → core::double};
31+
[-1] function ##0#4#initializer() → core::double
32+
[-1] return [556] [556] [478] [478] #0#0{self::Circle}.{self::Circle::radius}{core::double};
33+
[556] [556] late final synthesized core::double #0#4 = [-1] [-1] ##0#4#initializer [-1](){() → core::double};
34+
[470] #L1:
35+
[470] {
36+
[527] {
37+
[512] [512] hoisted core::double l;
38+
[527] if([522] [522] [493] [493] [493] [493] [478] [478] #0#0 is{ForNonNullableByDefault} self::Square && [512]([512] let [512] final core::double #t2 = [512] [512] l = [506] [506] #0#2 in [512] [512] true) && [522] [522] [520] [520] l.{core::num::>=}[522]([525] 0){(core::num) → core::bool}) {
39+
[470] [470] [470] #t1 = [532] [532] [530] [530] l.{core::double::*}[532]([534] l){(core::num) → core::double};
40+
[527] break #L1;
41+
}
42+
}
43+
[577] {
44+
[562] [562] hoisted core::double r;
45+
[577] if([572] [572] [543] [543] [543] [543] [478] [478] #0#0 is{ForNonNullableByDefault} self::Circle && [562]([562] let [562] final core::double #t3 = [562] [562] r = [556] [556] #0#4 in [562] [562] true) && [572] [572] [570] [570] r.{core::num::>=}[572]([575] 0){(core::num) → core::bool}) {
46+
[470] [470] [470] #t1 = [587] [587] [583] [583] [580] [580] #C1.{core::double::*}[583]([585] r){(core::num) → core::double}.{core::double::*}[587]([589] r){(core::num) → core::double};
47+
[577] break #L1;
48+
}
49+
}
50+
[631] {
51+
[617] [617] hoisted core::double l;
52+
[631] if([627] [627] [598] [598] [493] [493] [478] [478] #0#0 is{ForNonNullableByDefault} self::Square && [617]([617] let [617] final core::double #t4 = [617] [617] l = [506] [506] #0#2 in [617] [617] true) && [627] [627] [625] [625] l.{core::num::<}[627]([629] 0){(core::num) → core::bool}) {
53+
[470] [470] [470] #t1 = [634] [634] -1.0;
54+
[631] break #L1;
55+
}
56+
}
57+
[677] {
58+
[663] [663] hoisted core::double r;
59+
[677] if([673] [673] [644] [644] [543] [543] [478] [478] #0#0 is{ForNonNullableByDefault} self::Circle && [663]([663] let [663] final core::double #t5 = [663] [663] r = [556] [556] #0#4 in [663] [663] true) && [673] [673] [671] [671] r.{core::num::<}[673]([675] 0){(core::num) → core::bool}) {
60+
[470] [470] [470] #t1 = [680] [680] -1.0;
61+
[677] break #L1;
62+
}
63+
}
64+
[698] {
65+
[698] if([690] [690] true) {
66+
[470] [470] [470] #t1 = [701] [701] 0.0;
67+
[698] break #L1;
68+
}
69+
}
70+
}
71+
} =>[470] [470] #t1;
72+
[711]static method testMain() → dynamic {
73+
[753] [726] [726] self::calculateArea [739]([740] new self::Circle::• [746]([747] -123.0));
74+
}
75+
76+
constants {
77+
#C1 = 3.141592653589793
78+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import 'dart:math' show pi;
2+
3+
abstract class Shape {}
4+
5+
class Square implements Shape {
6+
final double length;
7+
Square(this.length);
8+
}
9+
10+
class Circle implements Shape {
11+
final double radius;
12+
Circle(this.radius);
13+
}
14+
15+
double calculateArea(Shape shape) => switch (shape) { };
16+
testMain() {}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import 'dart:math' show pi;
2+
abstract class Shape {}
3+
class Circle implements Shape {
4+
Circle(this.radius);
5+
final double radius;
6+
}
7+
class Square implements Shape {
8+
Square(this.length);
9+
final double length;
10+
}
11+
double calculateArea(Shape shape) =>
12+
switch (shape) {}
13+
---- unknown chunk starts ----
14+
;
15+
---- unknown chunk ends ----
16+
testMain() {}

0 commit comments

Comments
 (0)