Builder / executor for macros in AnalysisContextCollectionImpl by default.

Change-Id: Ie5455bbdd3f55f78f6acf54a617365da0467926d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/241866
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Konstantin Shcheglov 2022-04-21 20:56:59 +00:00 committed by Commit Bot
parent 0c762283a4
commit b12e5e16de
6 changed files with 47 additions and 72 deletions

View file

@ -16,6 +16,7 @@ import 'package:analyzer/src/dart/analysis/driver_based_analysis_context.dart';
import 'package:analyzer/src/dart/analysis/file_content_cache.dart';
import 'package:analyzer/src/dart/analysis/performance_logger.dart';
import 'package:analyzer/src/generated/engine.dart' show AnalysisOptionsImpl;
import 'package:analyzer/src/summary2/kernel_compilation_service.dart';
import 'package:analyzer/src/summary2/macro.dart';
import 'package:analyzer/src/util/sdk.dart';
@ -25,7 +26,10 @@ class AnalysisContextCollectionImpl implements AnalysisContextCollection {
final ResourceProvider resourceProvider;
/// The instance of macro executor that is used for all macros.
final macro.MultiMacroExecutor? macroExecutor = macro.MultiMacroExecutor();
final macro.MultiMacroExecutor macroExecutor = macro.MultiMacroExecutor();
/// The instance of the macro kernel builder.
final MacroKernelBuilder macroKernelBuilder = MacroKernelBuilder();
/// The list of analysis contexts.
@override
@ -48,7 +52,6 @@ class AnalysisContextCollectionImpl implements AnalysisContextCollection {
AnalysisDriverScheduler? scheduler,
FileContentCache? fileContentCache,
void Function(AnalysisOptionsImpl)? updateAnalysisOptions,
MacroKernelBuilder? macroKernelBuilder,
}) : resourceProvider =
resourceProvider ?? PhysicalResourceProvider.INSTANCE {
sdkPath ??= getSdkPath();
@ -119,7 +122,9 @@ class AnalysisContextCollectionImpl implements AnalysisContextCollection {
for (var analysisContext in contexts) {
analysisContext.driver.dispose();
}
macroExecutor?.close();
macroExecutor.close();
// If there are other collections, they will have to start it again.
KernelCompilationService.dispose();
}
/// Check every element with [_throwIfNotAbsoluteNormalizedPath].

View file

@ -68,34 +68,6 @@ class BundleMacroExecutor {
}
}
/// Implementation of [MacroKernelBuilder] using `frontend_server`.
class FrontEndServerMacroKernelBuilder implements MacroKernelBuilder {
@override
Future<Uint8List> build({
required MacroFileSystem fileSystem,
required List<MacroLibrary> libraries,
}) async {
final macroMainContent = macro.bootstrapMacroIsolate(
{
for (final library in libraries)
library.uri.toString(): {
for (final c in library.classes) c.name: c.constructors
},
},
macro.SerializationMode.byteDataClient,
);
final macroMainPath = '${libraries.first.path}.macro';
final overlayFileSystem = _OverlayMacroFileSystem(fileSystem);
overlayFileSystem.overlays[macroMainPath] = macroMainContent;
return KernelCompilationService.compile(
fileSystem: overlayFileSystem,
path: macroMainPath,
);
}
}
class MacroClass {
final String name;
final List<String> constructors;
@ -140,11 +112,32 @@ abstract class MacroFileSystem {
MacroFileEntry getFile(String path);
}
abstract class MacroKernelBuilder {
class MacroKernelBuilder {
const MacroKernelBuilder();
Future<Uint8List> build({
required MacroFileSystem fileSystem,
required List<MacroLibrary> libraries,
});
}) async {
final macroMainContent = macro.bootstrapMacroIsolate(
{
for (final library in libraries)
library.uri.toString(): {
for (final c in library.classes) c.name: c.constructors
},
},
macro.SerializationMode.byteDataClient,
);
final macroMainPath = '${libraries.first.path}.macro';
final overlayFileSystem = _OverlayMacroFileSystem(fileSystem);
overlayFileSystem.overlays[macroMainPath] = macroMainContent;
return KernelCompilationService.compile(
fileSystem: overlayFileSystem,
path: macroMainPath,
);
}
}
class MacroLibrary {

View file

@ -12,7 +12,6 @@ import 'package:analyzer/src/dart/analysis/driver.dart';
import 'package:analyzer/src/dart/analysis/driver_based_analysis_context.dart';
import 'package:analyzer/src/dart/analysis/experiments.dart';
import 'package:analyzer/src/generated/engine.dart' show AnalysisOptionsImpl;
import 'package:analyzer/src/summary2/macro.dart';
import 'package:analyzer/src/test_utilities/mock_packages.dart';
import 'package:analyzer/src/test_utilities/mock_sdk.dart';
import 'package:analyzer/src/test_utilities/package_config_file_builder.dart';
@ -141,8 +140,6 @@ abstract class ContextResolutionTest
_declaredVariables = map;
}
MacroKernelBuilder? get macroKernelBuilder => null;
bool get retainDataForTesting => false;
Folder get sdkRoot => newFolder('/sdk');
@ -257,7 +254,6 @@ abstract class ContextResolutionTest
retainDataForTesting: retainDataForTesting,
sdkPath: sdkRoot.path,
updateAnalysisOptions: updateAnalysisOptions,
macroKernelBuilder: macroKernelBuilder,
);
verifyCreatedCollection();

View file

@ -3,7 +3,6 @@
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/src/summary2/kernel_compilation_service.dart';
import 'package:analyzer/src/summary2/macro.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../../summary/macros_environment.dart';
@ -24,11 +23,6 @@ main() {
@reflectiveTest
class MacroResolutionTest extends PubPackageResolutionTest {
@override
MacroKernelBuilder? get macroKernelBuilder {
return FrontEndServerMacroKernelBuilder();
}
@override
void setUp() {
super.setUp();

View file

@ -23,6 +23,7 @@ import 'package:analyzer/src/generated/utilities_dart.dart';
import 'package:analyzer/src/source/package_map_resolver.dart';
import 'package:analyzer/src/summary2/bundle_reader.dart';
import 'package:analyzer/src/summary2/informative_data.dart';
import 'package:analyzer/src/summary2/kernel_compilation_service.dart';
import 'package:analyzer/src/summary2/link.dart';
import 'package:analyzer/src/summary2/linked_element_factory.dart';
import 'package:analyzer/src/summary2/macro.dart';
@ -30,6 +31,7 @@ import 'package:analyzer/src/summary2/reference.dart';
import 'package:analyzer/src/test_utilities/mock_sdk.dart';
import 'package:analyzer/src/test_utilities/resource_provider_mixin.dart';
import 'package:analyzer/src/util/uri.dart';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as package_path;
import 'package:test_reflective_loader/test_reflective_loader.dart';
@ -42,8 +44,8 @@ abstract class ElementsBaseTest with ResourceProviderMixin {
/// The shared SDK bundle, computed once and shared among test invocations.
static _SdkBundle? _sdkBundle;
MacroKernelBuilder? macroKernelBuilder;
macro.MultiMacroExecutor? macroExecutor;
/// The instance of macro executor that is used for all macros.
final macro.MultiMacroExecutor _macroExecutor = macro.MultiMacroExecutor();
/// The set of features enabled in this test.
FeatureSet featureSet = FeatureSets.latestWithExperiments;
@ -180,7 +182,7 @@ abstract class ElementsBaseTest with ResourceProviderMixin {
var linkResult = await link(
elementFactory,
inputLibraries,
macroExecutor: macroExecutor,
macroExecutor: _macroExecutor,
);
for (var macroUnit in linkResult.macroGeneratedUnits) {
@ -204,6 +206,14 @@ abstract class ElementsBaseTest with ResourceProviderMixin {
return elementFactory.libraryOfUri2('$testUri');
}
@mustCallSuper
Future<void> tearDown() async {
await _macroExecutor.close();
KernelCompilationService.disposeDelayed(
const Duration(milliseconds: 100),
);
}
void _addLibraryUnits(
Source definingSource,
CompilationUnit definingUnit,
@ -315,23 +325,14 @@ abstract class ElementsBaseTest with ResourceProviderMixin {
return;
}
final macroKernelBuilder = this.macroKernelBuilder;
if (macroKernelBuilder == null) {
return;
}
final macroExecutor = this.macroExecutor;
if (macroExecutor == null) {
return;
}
final macroKernelBuilder = const MacroKernelBuilder();
var macroKernelBytes = await macroKernelBuilder.build(
fileSystem: _MacroFileSystem(resourceProvider),
libraries: macroLibraries,
);
var bundleMacroExecutor = BundleMacroExecutor(
macroExecutor: macroExecutor,
macroExecutor: _macroExecutor,
kernelBytes: macroKernelBytes,
libraries: macroLibraries.map((e) => e.uri).toSet(),
);
@ -368,7 +369,7 @@ abstract class ElementsBaseTest with ResourceProviderMixin {
await link(
elementFactory,
cycleInputLibraries,
macroExecutor: macroExecutor,
macroExecutor: _macroExecutor,
);
await _buildMacroLibraries(elementFactory, macroLibraries);

View file

@ -2,13 +2,9 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:_fe_analyzer_shared/src/macros/executor/multi_executor.dart'
as macro;
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/summary2/kernel_compilation_service.dart';
import 'package:analyzer/src/summary2/macro.dart';
import 'package:analyzer/src/test_utilities/package_config_file_builder.dart';
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
@ -64,16 +60,6 @@ class MacroElementsTest extends ElementsBaseTest {
PackageConfigFileBuilder(),
macrosEnvironment: MacrosEnvironment.instance,
);
macroKernelBuilder = FrontEndServerMacroKernelBuilder();
macroExecutor = macro.MultiMacroExecutor();
}
Future<void> tearDown() async {
await macroExecutor?.close();
KernelCompilationService.disposeDelayed(
const Duration(milliseconds: 100),
);
}
test_arguments_error() async {