Skip to content

Commit 724cb50

Browse files
stereotype441Commit Queue
authored andcommitted
[messages] Move cfeToAnalyzerErrorCodeTables to package:analyzer_utilities
Moves the class `CfeToAnalyzerErrorCodeTables`, and its associated top level variable, `cfeToAnalyzerErrorCodeTables` from `pkg/analyzer/tool/messages/error_code_info.dart` to `pkg/analyzer_utilities/lib/messages.dart`, so that they can be used by the front end diagnostic code generator. In the process, they are renamed to `SharedToAnalyzerErrorCodeTables` and `sharedToAnalyzerErrorCodeTables`, to more precisely reflect the fact that they deal with _shared_ diagnostic codes. In a follow-up CL I plan to rework `SharedToAnalyzerErrorCodeTables` so that it automatically generates the `index` values that map between CFE and analyzer errors. This will make it easier to maintain the entries in `pkg/_fe_analyzer_shared/messages.yaml`, since we won't have to manually figure out the next index value and assign it. Change-Id: I6a6a696432d9f0b26fd86f753be3083a4de881b0 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/449606 Reviewed-by: Johnni Winther <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent 25cf5e4 commit 724cb50

File tree

4 files changed

+91
-89
lines changed

4 files changed

+91
-89
lines changed

pkg/analyzer/test/verify_diagnostics_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ class DocumentationValidator {
232232
errorClassIncludingCfeMessages = errorClass;
233233
await _validateMessages(
234234
errorClass.name,
235-
cfeToAnalyzerErrorCodeTables.analyzerCodeToInfo,
235+
sharedToAnalyzerErrorCodeTables.analyzerCodeToInfo,
236236
);
237237
}
238238
}

pkg/analyzer/tool/messages/error_code_info.dart

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,6 @@ final String analyzerPkgPath = normalize(
166166
join(pkg_root.packageRoot, 'analyzer'),
167167
);
168168

169-
/// A set of tables mapping between front end and analyzer error codes.
170-
final CfeToAnalyzerErrorCodeTables cfeToAnalyzerErrorCodeTables =
171-
CfeToAnalyzerErrorCodeTables._(frontEndAndSharedMessages);
172-
173169
/// The path to the `linter` package.
174170
final String linterPkgPath = normalize(join(pkg_root.packageRoot, 'linter'));
175171

@@ -306,81 +302,3 @@ class AnalyzerErrorCodeInfo extends ErrorCodeInfo {
306302

307303
AnalyzerErrorCodeInfo._fromYaml(super.yaml) : super.fromYaml();
308304
}
309-
310-
/// Data tables mapping between CFE errors and their corresponding automatically
311-
/// generated analyzer errors.
312-
class CfeToAnalyzerErrorCodeTables {
313-
/// List of CFE errors for which analyzer errors should be automatically
314-
/// generated, organized by their `index` property.
315-
final List<ErrorCodeInfo?> indexToInfo = [];
316-
317-
/// Map whose values are the CFE errors for which analyzer errors should be
318-
/// automatically generated, and whose keys are the corresponding analyzer
319-
/// error name. (Names are simple identifiers; they are not prefixed by the
320-
/// class name `ParserErrorCode`)
321-
final Map<String, ErrorCodeInfo> analyzerCodeToInfo = {};
322-
323-
/// Map whose values are the CFE errors for which analyzer errors should be
324-
/// automatically generated, and whose keys are the front end error name.
325-
final Map<String, ErrorCodeInfo> frontEndCodeToInfo = {};
326-
327-
/// Map whose keys are the CFE errors for which analyzer errors should be
328-
/// automatically generated, and whose values are the corresponding analyzer
329-
/// error name. (Names are simple identifiers; they are not prefixed by the
330-
/// class name `ParserErrorCode`)
331-
final Map<ErrorCodeInfo, String> infoToAnalyzerCode = {};
332-
333-
/// Map whose keys are the CFE errors for which analyzer errors should be
334-
/// automatically generated, and whose values are the front end error name.
335-
final Map<ErrorCodeInfo, String> infoToFrontEndCode = {};
336-
337-
CfeToAnalyzerErrorCodeTables._(Map<String, CfeStyleErrorCodeInfo> messages) {
338-
for (var entry in messages.entries) {
339-
var errorCodeInfo = entry.value;
340-
var index = errorCodeInfo.index;
341-
if (index == null || errorCodeInfo.analyzerCodes.length != 1) {
342-
continue;
343-
}
344-
var frontEndCode = entry.key;
345-
if (index < 1) {
346-
throw '''
347-
$frontEndCode specifies index $index but indices must be 1 or greater.
348-
For more information run:
349-
dart pkg/front_end/tool/generate_messages.dart
350-
''';
351-
}
352-
if (indexToInfo.length <= index) {
353-
indexToInfo.length = index + 1;
354-
}
355-
var previousEntryForIndex = indexToInfo[index];
356-
if (previousEntryForIndex != null) {
357-
throw 'Index $index used by both '
358-
'${infoToFrontEndCode[previousEntryForIndex]} and $frontEndCode';
359-
}
360-
indexToInfo[index] = errorCodeInfo;
361-
frontEndCodeToInfo[frontEndCode] = errorCodeInfo;
362-
infoToFrontEndCode[errorCodeInfo] = frontEndCode;
363-
var analyzerCodeLong = errorCodeInfo.analyzerCodes.single;
364-
var expectedPrefix = 'ParserErrorCode.';
365-
if (!analyzerCodeLong.startsWith(expectedPrefix)) {
366-
throw 'Expected all analyzer error codes to be prefixed with '
367-
'${json.encode(expectedPrefix)}. Found '
368-
'${json.encode(analyzerCodeLong)}.';
369-
}
370-
var analyzerCode = analyzerCodeLong.substring(expectedPrefix.length);
371-
infoToAnalyzerCode[errorCodeInfo] = analyzerCode;
372-
var previousEntryForAnalyzerCode = analyzerCodeToInfo[analyzerCode];
373-
if (previousEntryForAnalyzerCode != null) {
374-
throw 'Analyzer code $analyzerCode used by both '
375-
'${infoToFrontEndCode[previousEntryForAnalyzerCode]} and '
376-
'$frontEndCode';
377-
}
378-
analyzerCodeToInfo[analyzerCode] = errorCodeInfo;
379-
}
380-
for (int i = 1; i < indexToInfo.length; i++) {
381-
if (indexToInfo[i] == null) {
382-
throw 'Indices are not consecutive; no error code has index $i.';
383-
}
384-
}
385-
}
386-
}

pkg/analyzer/tool/messages/generate.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ part of ${json.encode(file.parentLibrary)};
131131
var entries = [
132132
...analyzerMessages[errorClass.name]!.entries,
133133
if (errorClass.includeCfeMessages)
134-
...cfeToAnalyzerErrorCodeTables.analyzerCodeToInfo.entries,
134+
...sharedToAnalyzerErrorCodeTables.analyzerCodeToInfo.entries,
135135
].where((error) => !error.value.isRemoved);
136136
for (var entry in entries) {
137137
var errorName = entry.key;
@@ -200,8 +200,8 @@ DiagnosticType get type => ${errorClass.typeCode};
200200

201201
void _generateFastaAnalyzerErrorCodeList() {
202202
out.writeln('final fastaAnalyzerErrorCodes = <DiagnosticCode?>[');
203-
for (var entry in cfeToAnalyzerErrorCodeTables.indexToInfo) {
204-
var name = cfeToAnalyzerErrorCodeTables.infoToAnalyzerCode[entry]
203+
for (var entry in sharedToAnalyzerErrorCodeTables.indexToInfo) {
204+
var name = sharedToAnalyzerErrorCodeTables.infoToAnalyzerCode[entry]
205205
?.toCamelCase();
206206
out.writeln('${name == null ? 'null' : 'ParserErrorCode.$name'},');
207207
}
@@ -332,7 +332,7 @@ class _SyntacticErrorGenerator {
332332
// error_converter.dart now that those ParserErrorCodes are auto generated.
333333
int converterCount = 0;
334334
for (var errorCode
335-
in cfeToAnalyzerErrorCodeTables.infoToAnalyzerCode.values) {
335+
in sharedToAnalyzerErrorCodeTables.infoToAnalyzerCode.values) {
336336
if (errorConverterSource.contains('"$errorCode"')) {
337337
if (converterCount == 0) {
338338
print('');
@@ -376,14 +376,14 @@ class _SyntacticErrorGenerator {
376376

377377
// Remove entries that have already been translated
378378
for (ErrorCodeInfo entry
379-
in cfeToAnalyzerErrorCodeTables.infoToAnalyzerCode.keys) {
379+
in sharedToAnalyzerErrorCodeTables.infoToAnalyzerCode.keys) {
380380
messageToName.remove(messageFromEntryTemplate(entry));
381381
}
382382

383383
// Print the # of autogenerated ParserErrorCodes.
384384
print(
385385
'${messageToName.length} of '
386-
'${cfeToAnalyzerErrorCodeTables.infoToAnalyzerCode.length} ParserErrorCodes generated.',
386+
'${sharedToAnalyzerErrorCodeTables.infoToAnalyzerCode.length} ParserErrorCodes generated.',
387387
);
388388

389389
// List the ParserErrorCodes that could easily be auto generated

pkg/analyzer_utilities/lib/messages.dart

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ final RegExp placeholderPattern = RegExp(
6363
'#([-a-zA-Z0-9_]+)(?:%([0-9]*).([0-9]+))?',
6464
);
6565

66+
/// A set of tables mapping between shared and analyzer error codes.
67+
final SharedToAnalyzerErrorCodeTables sharedToAnalyzerErrorCodeTables =
68+
SharedToAnalyzerErrorCodeTables._(frontEndAndSharedMessages);
69+
6670
/// Convert a template string (which uses placeholders matching
6771
/// [placeholderPattern]) to an analyzer internal template string (which uses
6872
/// placeholders like `{0}`).
@@ -982,6 +986,86 @@ class SharedErrorCodeInfo extends CfeStyleErrorCodeInfo {
982986
int get index => super.index!;
983987
}
984988

989+
/// Data tables mapping between shared errors and their corresponding
990+
/// automatically generated analyzer errors.
991+
class SharedToAnalyzerErrorCodeTables {
992+
/// List of CFE errors for which analyzer errors should be automatically
993+
/// generated, organized by their `index` property.
994+
final List<ErrorCodeInfo?> indexToInfo = [];
995+
996+
/// Map whose values are the CFE errors for which analyzer errors should be
997+
/// automatically generated, and whose keys are the corresponding analyzer
998+
/// error name. (Names are simple identifiers; they are not prefixed by the
999+
/// class name `ParserErrorCode`)
1000+
final Map<String, ErrorCodeInfo> analyzerCodeToInfo = {};
1001+
1002+
/// Map whose values are the CFE errors for which analyzer errors should be
1003+
/// automatically generated, and whose keys are the front end error name.
1004+
final Map<String, ErrorCodeInfo> frontEndCodeToInfo = {};
1005+
1006+
/// Map whose keys are the CFE errors for which analyzer errors should be
1007+
/// automatically generated, and whose values are the corresponding analyzer
1008+
/// error name. (Names are simple identifiers; they are not prefixed by the
1009+
/// class name `ParserErrorCode`)
1010+
final Map<ErrorCodeInfo, String> infoToAnalyzerCode = {};
1011+
1012+
/// Map whose keys are the CFE errors for which analyzer errors should be
1013+
/// automatically generated, and whose values are the front end error name.
1014+
final Map<ErrorCodeInfo, String> infoToFrontEndCode = {};
1015+
1016+
SharedToAnalyzerErrorCodeTables._(
1017+
Map<String, CfeStyleErrorCodeInfo> messages,
1018+
) {
1019+
for (var entry in messages.entries) {
1020+
var errorCodeInfo = entry.value;
1021+
var index = errorCodeInfo.index;
1022+
if (index == null || errorCodeInfo.analyzerCodes.length != 1) {
1023+
continue;
1024+
}
1025+
var frontEndCode = entry.key;
1026+
if (index < 1) {
1027+
throw '''
1028+
$frontEndCode specifies index $index but indices must be 1 or greater.
1029+
For more information run:
1030+
dart pkg/front_end/tool/generate_messages.dart
1031+
''';
1032+
}
1033+
if (indexToInfo.length <= index) {
1034+
indexToInfo.length = index + 1;
1035+
}
1036+
var previousEntryForIndex = indexToInfo[index];
1037+
if (previousEntryForIndex != null) {
1038+
throw 'Index $index used by both '
1039+
'${infoToFrontEndCode[previousEntryForIndex]} and $frontEndCode';
1040+
}
1041+
indexToInfo[index] = errorCodeInfo;
1042+
frontEndCodeToInfo[frontEndCode] = errorCodeInfo;
1043+
infoToFrontEndCode[errorCodeInfo] = frontEndCode;
1044+
var analyzerCodeLong = errorCodeInfo.analyzerCodes.single;
1045+
var expectedPrefix = 'ParserErrorCode.';
1046+
if (!analyzerCodeLong.startsWith(expectedPrefix)) {
1047+
throw 'Expected all analyzer error codes to be prefixed with '
1048+
'${json.encode(expectedPrefix)}. Found '
1049+
'${json.encode(analyzerCodeLong)}.';
1050+
}
1051+
var analyzerCode = analyzerCodeLong.substring(expectedPrefix.length);
1052+
infoToAnalyzerCode[errorCodeInfo] = analyzerCode;
1053+
var previousEntryForAnalyzerCode = analyzerCodeToInfo[analyzerCode];
1054+
if (previousEntryForAnalyzerCode != null) {
1055+
throw 'Analyzer code $analyzerCode used by both '
1056+
'${infoToFrontEndCode[previousEntryForAnalyzerCode]} and '
1057+
'$frontEndCode';
1058+
}
1059+
analyzerCodeToInfo[analyzerCode] = errorCodeInfo;
1060+
}
1061+
for (int i = 1; i < indexToInfo.length; i++) {
1062+
if (indexToInfo[i] == null) {
1063+
throw 'Indices are not consecutive; no error code has index $i.';
1064+
}
1065+
}
1066+
}
1067+
}
1068+
9851069
/// A [Conversion] that invokes a top level function via the `conversions`
9861070
/// import prefix.
9871071
class SimpleConversion implements Conversion {

0 commit comments

Comments
 (0)