Skip to content

Commit

Permalink
Version 2.15.0-24.0.dev
Browse files Browse the repository at this point in the history
Merge commit '5b3cadc7e6d7be94ef959e3733357980ff69c684' into 'dev'
  • Loading branch information
Dart CI committed Aug 17, 2021
2 parents fecde13 + 5b3cadc commit ba50855
Show file tree
Hide file tree
Showing 23 changed files with 678 additions and 75 deletions.
20 changes: 20 additions & 0 deletions benchmarks/SDKArtifactSizes/dart/SDKArtifactSizes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,24 @@ Future<void> main() async {
'$rootDir/dart-sdk/bin/snapshots/$snapshot.dart.snapshot';
await reportArtifactSize(snapshotPath, snapshot);
}

// Measure the (compressed) sdk size.
final tempDir = Directory.systemTemp.createTempSync('dartdev');
final sdkArchive =
compress(File(Platform.resolvedExecutable).parent.parent, tempDir);
await reportArtifactSize(sdkArchive?.path, 'sdk');
tempDir.deleteSync(recursive: true);
}

File compress(Directory sourceDir, Directory targetDir) {
final outFile = File('${targetDir.path}/sdk.zip');

if (Platform.isMacOS || Platform.isLinux) {
Process.runSync(
'zip', ['-r', outFile.absolute.path, sourceDir.absolute.path]);
} else {
return null;
}

return outFile;
}
20 changes: 20 additions & 0 deletions benchmarks/SDKArtifactSizes/dart2/SDKArtifactSizes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,24 @@ Future<void> main() async {
'$rootDir/dart-sdk/bin/snapshots/$snapshot.dart.snapshot';
await reportArtifactSize(snapshotPath, snapshot);
}

// Measure the (compressed) sdk size.
final tempDir = Directory.systemTemp.createTempSync('dartdev');
final sdkArchive =
compress(File(Platform.resolvedExecutable).parent.parent, tempDir);
await reportArtifactSize(sdkArchive?.path ?? '', 'sdk');
tempDir.deleteSync(recursive: true);
}

File? compress(Directory sourceDir, Directory targetDir) {
final outFile = File('${targetDir.path}/sdk.zip');

if (Platform.isMacOS || Platform.isLinux) {
Process.runSync(
'zip', ['-r', outFile.absolute.path, sourceDir.absolute.path]);
} else {
return null;
}

return outFile;
}
7 changes: 6 additions & 1 deletion pkg/analysis_server/lib/src/analysis_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import 'package:analysis_server/src/domain_diagnostic.dart';
import 'package:analysis_server/src/domain_execution.dart';
import 'package:analysis_server/src/domain_kythe.dart';
import 'package:analysis_server/src/domain_server.dart';
import 'package:analysis_server/src/domains/analysis/macro_files.dart';
import 'package:analysis_server/src/domains/analysis/occurrences.dart';
import 'package:analysis_server/src/domains/analysis/occurrences_dart.dart';
import 'package:analysis_server/src/edit/edit_domain.dart';
Expand Down Expand Up @@ -54,6 +55,7 @@ import 'package:analyzer/src/generated/sdk.dart';
import 'package:analyzer/src/util/file_paths.dart' as file_paths;
import 'package:analyzer_plugin/protocol/protocol_common.dart' hide Element;
import 'package:analyzer_plugin/src/utilities/navigation/navigation.dart';
import 'package:analyzer_plugin/utilities/analyzer_converter.dart';
import 'package:analyzer_plugin/utilities/navigation/navigation_dart.dart';
import 'package:http/http.dart' as http;
import 'package:telemetry/crash_reporting.dart';
Expand Down Expand Up @@ -718,7 +720,10 @@ class ServerContextManagerCallbacks extends ContextManagerCallbacks {
server.AnalysisNavigationParams _computeNavigationParams(
String path, CompilationUnit unit) {
var collector = NavigationCollectorImpl();
computeDartNavigation(resourceProvider, collector, unit, null, null);
computeDartNavigation(resourceProvider, collector, unit, null, null,
analyzerConverter: AnalyzerConverter(
locationProvider:
MacroElementLocationProvider(MacroFiles(resourceProvider))));
collector.createRegions();
return server.AnalysisNavigationParams(
path, collector.regions, collector.targets, collector.files);
Expand Down
121 changes: 121 additions & 0 deletions pkg/analysis_server/lib/src/domains/analysis/macro_files.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// 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:analyzer/dart/element/element.dart';
import 'package:analyzer/file_system/file_system.dart';
import 'package:analyzer/source/line_info.dart';
import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/util/file_paths.dart' as file_paths;
import 'package:analyzer_plugin/protocol/protocol_common.dart' as protocol;
import 'package:analyzer_plugin/utilities/analyzer_converter.dart';

class MacroElementLocationProvider implements ElementLocationProvider {
final MacroFiles _macroFiles;

MacroElementLocationProvider(this._macroFiles);

@override
protocol.Location? forElement(Element element) {
if (element is HasMacroGenerationData) {
var macro = (element as HasMacroGenerationData).macro;
if (macro != null) {
return _forElement(element, macro);
}
}
}

protocol.Location? _forElement(Element element, MacroGenerationData macro) {
var unitElement = element.thisOrAncestorOfType<CompilationUnitElement>();
if (unitElement is! CompilationUnitElementImpl) {
return null;
}

var generatedFile = _macroFiles.generatedFile(unitElement);
if (generatedFile == null) {
return null;
}

var nameOffset = element.nameOffset;
var nameLength = element.nameLength;

var lineInfo = generatedFile.lineInfo;
var offsetLocation = lineInfo.getLocation(nameOffset);
var endLocation = lineInfo.getLocation(nameOffset + nameLength);

return protocol.Location(generatedFile.path, nameOffset, nameLength,
offsetLocation.lineNumber, offsetLocation.columnNumber,
endLine: endLocation.lineNumber, endColumn: endLocation.columnNumber);
}
}

/// Note, this class changes the file system.
class MacroFiles {
final ResourceProvider _resourceProvider;

/// Keys are source paths.
final Map<String, _MacroGeneratedFile> _files = {};

MacroFiles(this._resourceProvider);

/// If [unitElement] has macro-generated elements, write the combined
/// content into a new file in `.dart_tool`, and return the description of
/// this file.
_MacroGeneratedFile? generatedFile(CompilationUnitElementImpl unitElement) {
var sourcePath = unitElement.source.fullName;

var result = _files[sourcePath];
if (result != null) {
return result;
}

var sourceFile = _resourceProvider.getFile(sourcePath);

// TODO(scheglov) Use workspace?
Folder? packageRoot;
for (var parent in sourceFile.parent2.withAncestors) {
if (parent.getChildAssumingFile(file_paths.pubspecYaml).exists) {
packageRoot = parent;
break;
}
}
if (packageRoot == null) {
return null;
}

var pathContext = _resourceProvider.pathContext;
var relativePath = pathContext.relative(
sourcePath,
from: packageRoot.path,
);
var generatedPath = pathContext.join(
packageRoot.path, '.dart_tool', 'analyzer', 'macro', relativePath);

var generatedContent = unitElement.macroGeneratedContent;
if (generatedContent == null) {
return null;
}

try {
_resourceProvider.getFile(generatedPath)
..parent2.create()
..writeAsStringSync(generatedContent);
} on FileSystemException {
return null;
}

return _files[sourcePath] = _MacroGeneratedFile(
generatedPath,
generatedContent,
);
}
}

class _MacroGeneratedFile {
final String path;
final String content;
final LineInfo lineInfo;

_MacroGeneratedFile(this.path, this.content)
: lineInfo = LineInfo.fromContent(content);
}
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,64 @@ library my.lib;
assertHasTargetString('my.lib');
}

Future<void> test_macro_simpleIdentifier_getter() async {
// TODO(scheglov) Use PubPackageResolutionTest?
newFile('$projectPath/pubspec.yaml', content: '');

newFile('$projectPath/bin/macro_annotations.dart', content: r'''
library analyzer.macro.annotations;
const observable = 0;
''');

addTestFile(r'''
import 'macro_annotations.dart';
class A {
@observable
int _foo = 0;
}
void f(A a) {
a.foo;
}
''');

await prepareNavigation();
assertHasRegionString('foo;', 3);

var generatedFile = getFile(
'/project/.dart_tool/analyzer/macro/bin/test.dart',
);

var generatedContent = generatedFile.readAsStringSync();
// TODO(scheglov) Improve macro to be more formatted.
expect(generatedContent, r'''
import 'macro_annotations.dart';
class A {
@observable
int _foo = 0;
int get foo => _foo;
set foo(int val) {
print('Setting foo to ${val}');
_foo = val;
}
}
void f(A a) {
a.foo;
}
''');

assertHasFileTarget(
generatedFile.path,
generatedContent.indexOf('foo =>'),
'foo'.length,
);
}

Future<void> test_multiplyDefinedElement() async {
newFile('$projectPath/bin/libA.dart', content: 'library A; int TEST = 1;');
newFile('$projectPath/bin/libB.dart', content: 'library B; int TEST = 2;');
Expand Down
19 changes: 19 additions & 0 deletions pkg/analyzer_plugin/lib/utilities/analyzer_converter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ import 'package:analyzer_plugin/protocol/protocol_common.dart' as plugin;
///
/// Clients may not extend, implement or mix-in this class.
class AnalyzerConverter {
/// Optional provider for [analyzer.Element] location, used for example to
/// override the location of macro-generated elements.
final ElementLocationProvider? _locationProvider;

AnalyzerConverter({ElementLocationProvider? locationProvider})
: _locationProvider = locationProvider;

/// Convert the analysis [error] from the 'analyzer' package to an analysis
/// error defined by the plugin API. If a [lineInfo] is provided then the
/// error's location will have a start line and start column. If a [severity]
Expand Down Expand Up @@ -199,6 +206,12 @@ class AnalyzerConverter {
if (element == null || element.source == null) {
return null;
}

var result = _locationProvider?.forElement(element);
if (result != null) {
return result;
}

offset ??= element.nameOffset;
length ??= element.nameLength;
if (element is analyzer.CompilationUnitElement ||
Expand Down Expand Up @@ -412,3 +425,9 @@ class AnalyzerConverter {
endLine: endLine, endColumn: endColumn);
}
}

abstract class ElementLocationProvider {
/// Return the location of [element] that this provider wants to override,
/// or `null` if the default location should be used.
plugin.Location? forElement(analyzer.Element element);
}
35 changes: 21 additions & 14 deletions pkg/analyzer_plugin/lib/utilities/navigation/navigation_dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ import 'package:analyzer_plugin/utilities/analyzer_converter.dart';
import 'package:analyzer_plugin/utilities/navigation/navigation.dart';

NavigationCollector computeDartNavigation(
ResourceProvider resourceProvider,
NavigationCollector collector,
CompilationUnit unit,
int? offset,
int? length) {
var dartCollector = _DartNavigationCollector(collector, offset, length);
ResourceProvider resourceProvider,
NavigationCollector collector,
CompilationUnit unit,
int? offset,
int? length, {
AnalyzerConverter? analyzerConverter,
}) {
var dartCollector = _DartNavigationCollector(
collector, offset, length, analyzerConverter ?? AnalyzerConverter());
var visitor = _DartNavigationComputerVisitor(resourceProvider, dartCollector);
if (offset == null || length == null) {
unit.accept(visitor);
Expand Down Expand Up @@ -66,9 +69,14 @@ class _DartNavigationCollector {
final NavigationCollector collector;
final int? requestedOffset;
final int? requestedLength;
final AnalyzerConverter _analyzerConverter;

_DartNavigationCollector(
this.collector, this.requestedOffset, this.requestedLength);
this.collector,
this.requestedOffset,
this.requestedLength,
this._analyzerConverter,
);

void _addRegion(int offset, int length, Element? element) {
element = element?.nonSynthetic;
Expand All @@ -85,15 +93,14 @@ class _DartNavigationCollector {
if (!_isWithinRequestedRange(offset, length)) {
return;
}
var converter = AnalyzerConverter();
var kind = converter.convertElementKind(element.kind);
var location = converter.locationFromElement(element);
var kind = _analyzerConverter.convertElementKind(element.kind);
var location = _analyzerConverter.locationFromElement(element);
if (location == null) {
return;
}

var codeLocation = collector.collectCodeLocations
? _getCodeLocation(element, location, converter)
? _getCodeLocation(element, location)
: null;

collector.addRegion(offset, length, kind, location,
Expand Down Expand Up @@ -122,8 +129,8 @@ class _DartNavigationCollector {
}

/// Get the location of the code (excluding leading doc comments) for this element.
protocol.Location? _getCodeLocation(Element element,
protocol.Location location, AnalyzerConverter converter) {
protocol.Location? _getCodeLocation(
Element element, protocol.Location location) {
var codeElement = element;
// For synthetic getters created for fields, we need to access the associated
// variable to get the codeOffset/codeLength.
Expand Down Expand Up @@ -162,7 +169,7 @@ class _DartNavigationCollector {
codeOffset = offsetAfterDocs;
}

return converter.locationFromElement(element,
return _analyzerConverter.locationFromElement(element,
offset: codeOffset, length: codeLength);
}

Expand Down
Loading

0 comments on commit ba50855

Please sign in to comment.