Skip to content

Commit 6abcced

Browse files
jensjohaCommit Queue
authored andcommitted
[CFE] Coverage merger adds braces on dangling if, if adding an ignore comment
Fixes #61171 Change-Id: Ie08492636fdf4f73d261c0b744e8b5978c0f6b40 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/443980 Reviewed-by: Johnni Winther <[email protected]> Commit-Queue: Jens Johansen <[email protected]>
1 parent 82fd327 commit 6abcced

File tree

5 files changed

+68
-6
lines changed

5 files changed

+68
-6
lines changed
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
void main() {
22
print("hello");
3-
if (1 + 1 == 3)
4-
// Coverage-ignore(suite): Not run.
3+
if (1 + 1 == 3) {
4+
// Coverage-ignore-block(suite): Not run.
55
print("no?!?");
6+
}
67
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
void main() {
2+
print("hello");
3+
Forest forest = new Forest();
4+
if (1 + 1 == 3) push(forest.createBlock(noLocation, noLocation, []));
5+
push(forest.createBlock(42, 42, [42]));
6+
}
7+
8+
const int noLocation = -1;
9+
10+
void push(dynamic whatever) {}
11+
12+
class Forest {
13+
dynamic createBlock(int a, int b, List c) {
14+
return "$a$b$c";
15+
}
16+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
void main() {
2+
print("hello");
3+
Forest forest = new Forest();
4+
if (1 + 1 == 3) {
5+
// Coverage-ignore-block(suite): Not run.
6+
push(forest.createBlock(noLocation, noLocation, []));
7+
}
8+
push(forest.createBlock(42, 42, [42]));
9+
}
10+
11+
const int noLocation = -1;
12+
13+
void push(dynamic whatever) {}
14+
15+
class Forest {
16+
dynamic createBlock(int a, int b, List c) {
17+
return "$a$b$c";
18+
}
19+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package:front_end/main.dart: 79% (3 misses)
2+
package:front_end/main.dart:4:
3+
In 'main':
4+
if (1 + 1 == 3) push(forest.createBlock(noLocation, noLocation, []));
5+
^^^^ ^^^^^^^^^^^ ^

pkg/front_end/tool/coverage_merger.dart

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,12 @@ CoverageInfo _process(
362362
commentOn.commentOnToken.lexeme == "?.") {
363363
// A comment on the actual call isn't pretty.
364364
// Allow the "bigger one".
365+
} else if (prevAdded.beginToken.charOffset ==
366+
commentOn.beginToken.charOffset &&
367+
prevAdded.endToken.charEnd == commentOn.endToken.charEnd &&
368+
prevAdded.allowToWrapInBlock) {
369+
// They have the same size and the previous one should be wrapped.
370+
// Keep that one so we do wrap.
365371
} else if (prevAdded.canBeReplaced) {
366372
// Though if there aren't any possible extra coverage in the
367373
// previous block compared to this one, we do prefer the smaller
@@ -514,14 +520,23 @@ CoverageInfo _process(
514520

515521
// The extra spaces at the end makes the formatter format better if for
516522
// instance there's comments after this.
523+
bool addedStartBrace = false;
517524
if (entry.isBlock) {
518525
sb.write("$extra// Coverage-ignore-block(suite): Not run.\n ");
526+
} else if (entry.allowToWrapInBlock) {
527+
sb.write("$extra {\n // Coverage-ignore-block(suite): Not run.\n ");
528+
addedStartBrace = true;
519529
} else {
520530
sb.write("$extra// Coverage-ignore(suite): Not run.\n ");
521531
}
522532
ignoredIntervalsBuilder?.addIntervalIncludingEnd(
523533
entry.beginToken.charOffset, entry.endToken.charEnd);
524534
from = token.charOffset;
535+
if (addedStartBrace) {
536+
addChunk(from, entry.endToken.charEnd);
537+
from = entry.endToken.charEnd;
538+
sb.write("\n }");
539+
}
525540
}
526541
addChunk(from, sourceText.length);
527542
f.writeAsStringSync(sb.toString());
@@ -888,10 +903,10 @@ class AstIndexerAndIgnoreCollector extends AstIndexer {
888903
/// there is possible coverage but no actual coverage).
889904
bool _checkCommentAndIgnoreCoverage(
890905
Token tokenWithPossibleComment, BeginAndEndTokenParserAstNode ignoreRange,
891-
{required bool allowReplace}) {
906+
{required bool allowReplace, bool allowToWrapInBlock = false}) {
892907
return _checkCommentAndIgnoreCoverageWithBeginAndEnd(
893908
tokenWithPossibleComment, ignoreRange.beginToken, ignoreRange.endToken,
894-
allowReplace: allowReplace);
909+
allowReplace: allowReplace, allowToWrapInBlock: allowToWrapInBlock);
895910
}
896911

897912
/// Check if there is an ignore comment on [tokenWithPossibleComment] and
@@ -905,7 +920,8 @@ class AstIndexerAndIgnoreCollector extends AstIndexer {
905920
final Token endToken,
906921
{required bool allowReplace,
907922
bool isBlock = false,
908-
bool allowOnBraceStart = false}) {
923+
bool allowOnBraceStart = false,
924+
bool allowToWrapInBlock = false}) {
909925
if (_hasIgnoreComment(tokenWithPossibleComment, isBlock: isBlock)) {
910926
ignoredStartEnd.addIntervalIncludingEnd(
911927
beginToken.charOffset, endToken.charEnd);
@@ -976,6 +992,7 @@ class AstIndexerAndIgnoreCollector extends AstIndexer {
976992
endToken: endToken,
977993
canBeReplaced: allowReplace,
978994
isBlock: isBlock,
995+
allowToWrapInBlock: allowToWrapInBlock,
979996
));
980997
}
981998

@@ -1302,7 +1319,9 @@ class _AstIndexerAndIgnoreCollectorBody extends RecursiveParserAstVisitor {
13021319
@override
13031320
void visitThenStatementEnd(ThenStatementEnd node) {
13041321
if (_collector._checkCommentAndIgnoreCoverage(node.beginToken, node,
1305-
allowReplace: true)) {
1322+
allowReplace: true,
1323+
allowToWrapInBlock:
1324+
!node.beginToken.isA(TokenType.OPEN_CURLY_BRACKET))) {
13061325
return;
13071326
}
13081327
super.visitThenStatementEnd(node);
@@ -1556,13 +1575,15 @@ class _CommentOn implements Comparable<_CommentOn> {
15561575
final Token endToken;
15571576
final bool canBeReplaced;
15581577
final bool isBlock;
1578+
final bool allowToWrapInBlock;
15591579

15601580
_CommentOn({
15611581
required this.commentOnToken,
15621582
required this.beginToken,
15631583
required this.endToken,
15641584
required this.canBeReplaced,
15651585
required this.isBlock,
1586+
required this.allowToWrapInBlock,
15661587
});
15671588

15681589
@override

0 commit comments

Comments
 (0)