Const finder NNBD (flutter/engine#26883)
This commit is contained in:
@@ -4,18 +4,14 @@
|
||||
|
||||
import 'dart:collection';
|
||||
|
||||
import 'package:kernel/kernel.dart' hide MapEntry;
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:kernel/kernel.dart';
|
||||
|
||||
class _ConstVisitor extends RecursiveVisitor<void> {
|
||||
_ConstVisitor(
|
||||
this.kernelFilePath,
|
||||
this.classLibraryUri,
|
||||
this.className,
|
||||
) : assert(kernelFilePath != null),
|
||||
assert(classLibraryUri != null),
|
||||
assert(className != null),
|
||||
_visitedInstances = <String>{},
|
||||
) : _visitedInstances = <String>{},
|
||||
constantInstances = <Map<String, dynamic>>[],
|
||||
nonConstantLocations = <Map<String, dynamic>>[];
|
||||
|
||||
@@ -35,16 +31,16 @@ class _ConstVisitor extends RecursiveVisitor<void> {
|
||||
// A cache of previously evaluated classes.
|
||||
static Map<Class, bool> _classHeirarchyCache = <Class, bool>{};
|
||||
bool _matches(Class node) {
|
||||
assert(node != null);
|
||||
final bool result = _classHeirarchyCache[node];
|
||||
final bool? result = _classHeirarchyCache[node];
|
||||
if (result != null) {
|
||||
return result;
|
||||
}
|
||||
final bool exactMatch = node.name == className
|
||||
&& node.enclosingLibrary.importUri.toString() == classLibraryUri;
|
||||
_classHeirarchyCache[node] = exactMatch
|
||||
final bool match = exactMatch
|
||||
|| node.supers.any((Supertype supertype) => _matches(supertype.classNode));
|
||||
return _classHeirarchyCache[node];
|
||||
_classHeirarchyCache[node] = match;
|
||||
return match;
|
||||
}
|
||||
|
||||
// Avoid visiting the same constant more than once.
|
||||
@@ -64,12 +60,13 @@ class _ConstVisitor extends RecursiveVisitor<void> {
|
||||
|
||||
@override
|
||||
void visitConstructorInvocation(ConstructorInvocation node) {
|
||||
final Class parentClass = node.target.parent as Class;
|
||||
final Class parentClass = node.target.parent! as Class;
|
||||
if (_matches(parentClass)) {
|
||||
final Location location = node.location!;
|
||||
nonConstantLocations.add(<String, dynamic>{
|
||||
'file': node.location.file.toString(),
|
||||
'line': node.location.line,
|
||||
'column': node.location.column,
|
||||
'file': location.file.toString(),
|
||||
'line': location.line,
|
||||
'column': location.column,
|
||||
});
|
||||
}
|
||||
super.visitConstructorInvocation(node);
|
||||
@@ -82,7 +79,7 @@ class _ConstVisitor extends RecursiveVisitor<void> {
|
||||
return;
|
||||
}
|
||||
final Map<String, dynamic> instance = <String, dynamic>{};
|
||||
for (MapEntry<Reference, Constant> kvp in node.fieldValues.entries) {
|
||||
for (final MapEntry<Reference, Constant> kvp in node.fieldValues.entries) {
|
||||
if (kvp.value is! PrimitiveConstant<dynamic>) {
|
||||
continue;
|
||||
}
|
||||
@@ -102,9 +99,9 @@ class ConstFinder {
|
||||
///
|
||||
/// The `kernelFilePath` is the path to a dill (kernel) file to process.
|
||||
ConstFinder({
|
||||
@required String kernelFilePath,
|
||||
@required String classLibraryUri,
|
||||
@required String className,
|
||||
required String kernelFilePath,
|
||||
required String classLibraryUri,
|
||||
required String className,
|
||||
}) : _visitor = _ConstVisitor(
|
||||
kernelFilePath,
|
||||
classLibraryUri,
|
||||
@@ -116,7 +113,7 @@ class ConstFinder {
|
||||
/// Finds all instances
|
||||
Map<String, dynamic> findInstances() {
|
||||
_visitor._visitedInstances.clear();
|
||||
for (Library library in loadComponentFromBinary(_visitor.kernelFilePath).libraries) {
|
||||
for (final Library library in loadComponentFromBinary(_visitor.kernelFilePath).libraries) {
|
||||
library.visitChildren(_visitor);
|
||||
}
|
||||
return <String, dynamic>{
|
||||
|
||||
@@ -6,7 +6,7 @@ name: const_finder
|
||||
publish_to: none
|
||||
|
||||
environment:
|
||||
sdk: ">=2.4.0 <3.0.0"
|
||||
sdk: ">=2.12.0 <3.0.0"
|
||||
|
||||
# Do not add any dependencies that require more than what is provided in
|
||||
# //third_party/dart/pkg or //third_party/dart/third_party/pkg.
|
||||
@@ -19,7 +19,6 @@ environment:
|
||||
dependencies:
|
||||
args: any
|
||||
collection: any
|
||||
meta: any
|
||||
kernel: any
|
||||
|
||||
dev_dependencies:
|
||||
@@ -32,7 +31,5 @@ dependency_overrides:
|
||||
path: ../../../third_party/dart/third_party/pkg/collection
|
||||
kernel:
|
||||
path: ../../../third_party/dart/pkg/kernel
|
||||
meta:
|
||||
path: ../../../third_party/dart/pkg/meta
|
||||
path:
|
||||
path: ../../../third_party/dart/third_party/pkg/path
|
||||
|
||||
@@ -155,7 +155,7 @@ void _checkNonConsts() {
|
||||
},
|
||||
<String, dynamic>{
|
||||
'file': 'file://$fixtures/pkg/package.dart',
|
||||
'line': 13,
|
||||
'line': 14,
|
||||
'column': 25,
|
||||
}
|
||||
]
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// ignore_for_file: prefer_const_constructors, unused_local_variable
|
||||
import 'dart:core';
|
||||
|
||||
import 'package:const_finder_fixtures_package/package.dart';
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// ignore_for_file: prefer_const_constructors
|
||||
// ignore_for_file: prefer_const_constructors, unused_local_variable
|
||||
import 'dart:core';
|
||||
|
||||
import 'package:const_finder_fixtures_package/package.dart';
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// ignore_for_file: prefer_const_constructors
|
||||
import 'package:const_finder_fixtures/target.dart';
|
||||
|
||||
void createTargetInPackage() {
|
||||
|
||||
Reference in New Issue
Block a user