Skip to content

Commit 8e608e8

Browse files
stereotype441Commit Queue
authored andcommitted
[front_end] Rework diagnostic code generation.
This change reworks the logic for generating the files `pkg/_fe_analyzer_shared/lib/src/messages/codes_generated.dart` and `pkg/front_end/lib/src/codes/cfe_codes_generated.dart`. The following changes are made: - The generated code is changed so that each `withArgumentsOld` function forwards to a corresponding `withArguments` function, which accepts named arguments rather than positional ones. In later CLs these new `withArguments` functions will be made available to call directly from CFE code. - The code in the `withArguments` functions uses a uniform naming convention, where the parameter names match the parameter names specified in the error message templates, and the variables holding the result of string conversions are named by appending `_0` to the parameter names.* *In principle, variables suffixed with `_1`, `_2`, etc. might be generated in the circumstance where a single parameter gets converted to strings in multiple ways. But this doesn't happen in practice. - The code in the `withArguments` functions converts arguments to strings in a uniform way, by calling either top level functions in `pkg/_fe_analyzer_shared/lib/src/messages/conversions.dart` or methods on the `TypeLabeler` class. - The code generation logic in `pkg/front_end/tool/generate_messages_lib.dart` has been refactored considerably, with an eye toward allowing it to be further generalized in the future. In particular, in follow-up CLs I plan to allow each error message to define the precise set of placeholders it will use, and their types, as is done in the analyzer's `messages.yaml` file. There is no change to functionality. Change-Id: I6a6a69644e3bd3a5871bc4d156865f2ac499e8a6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/447823 Reviewed-by: Johnni Winther <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent 74d85c1 commit 8e608e8

File tree

8 files changed

+7375
-4604
lines changed

8 files changed

+7375
-4604
lines changed

pkg/_fe_analyzer_shared/lib/src/messages/codes.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import 'severity.dart' show CfeSeverity;
1414

1515
import '../util/relativize.dart' as util show isWindows, relativizeUri;
1616

17+
import 'conversions.dart' as conversions;
18+
import 'codes.dart' as conversions show relativizeUri;
19+
1720
part 'codes_generated.dart';
1821

1922
const int noLength = 1;

pkg/_fe_analyzer_shared/lib/src/messages/codes_generated.dart

Lines changed: 4669 additions & 2879 deletions
Large diffs are not rendered by default.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) 2025, 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 'package:_fe_analyzer_shared/src/messages/codes.dart';
6+
import 'package:_fe_analyzer_shared/src/scanner/scanner.dart';
7+
8+
String codePointToUnicode(int codePoint) {
9+
// Write unicode value using at least four (but otherwise no more than
10+
// necessary) hex digits, using uppercase letters.
11+
// http://www.unicode.org/versions/Unicode10.0.0/appA.pdf
12+
return "U+${codePoint.toRadixString(16).toUpperCase().padLeft(4, '0')}";
13+
}
14+
15+
String formatNumber(
16+
num num, {
17+
int? fractionDigits,
18+
int padWidth = 0,
19+
bool padWithZeros = false,
20+
}) {
21+
String s = fractionDigits == null
22+
? '$num'
23+
: num.toStringAsFixed(fractionDigits);
24+
return s.padLeft(padWidth, padWithZeros ? '0' : ' ');
25+
}
26+
27+
String nameOrUnnamed(String name) => name.isEmpty ? '(unnamed)' : name;
28+
29+
String stringOrEmpty(String string) => string.isEmpty ? '(empty)' : string;
30+
31+
String tokenToLexeme(Token token) => token.lexeme;
32+
33+
String validateAndDemangleName(String name) {
34+
if (name.isEmpty) throw 'No name provided';
35+
return demangleMixinApplicationName(name);
36+
}
37+
38+
String validateAndItemizeNames(List<String> names) {
39+
if (names.isEmpty) throw 'No names provided';
40+
return itemizeNames(names);
41+
}
42+
43+
String validateCharacter(String character) {
44+
if (character.runes.length != 1) throw "Not a character '${character}'";
45+
return character;
46+
}
47+
48+
String validateString(String string) {
49+
if (string.isEmpty) throw 'No string provided';
50+
return string;
51+
}

pkg/front_end/lib/src/codes/cfe_codes.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import 'package:_fe_analyzer_shared/src/messages/codes.dart';
66
import 'package:_fe_analyzer_shared/src/messages/severity.dart';
77
import 'package:kernel/ast.dart' show Constant, DartType;
88

9+
import 'package:_fe_analyzer_shared/src/messages/conversions.dart'
10+
as conversions;
911
import 'type_labeler.dart';
1012

1113
export 'package:_fe_analyzer_shared/src/messages/codes.dart';

0 commit comments

Comments
 (0)