[cfe] Use macro file uri on re-offset FileUriNode

The file URI used for the nodes in merged augmentation library should
not use the import URI of the synthesized library but instead the
corresponding file URI. This ensures the their source can be looked up
directly in the `Component.uriToSource` map like normal nodes.

The test for re-offset node have been updated to ensure that macro
sources are always found through the FileUriNode. Previously these
nodes where implictly skipped by the wrongful use of the import URI.

Change-Id: Id7d99137d6683d2a8106cd9c62129a574a47810e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/356404
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
This commit is contained in:
Johnni Winther 2024-03-12 14:02:38 +00:00 committed by Commit Queue
parent 4b9dd9d586
commit 0a68ca97b9
7 changed files with 47 additions and 18 deletions

View file

@ -999,9 +999,7 @@ class MacroApplications {
}
if (spans.isNotEmpty) {
reOffsetMaps[intermediateAugmentationUri] = new ReOffset(
intermediateAugmentationUri,
augmentationImportUri,
reOffsetMap);
intermediateAugmentationUri, augmentationFileUri, reOffsetMap);
}
}
}

View file

@ -59,25 +59,19 @@ class MacroOffsetChecker extends MacroOffsetCheckerHook {
}
abstract class OffsetVisitor extends FileUriVisitor {
final List<Source> _currentSources = [];
final List<(Uri, Source?)> _currentSources = [];
final Map<Uri, Source> _sources;
OffsetVisitor(this._sources);
@override
void enterFileUri(FileUriNode node) {
Source? source = _sources[node.fileUri];
if (source != null) {
_currentSources.add(source);
}
_currentSources.add((node.fileUri, _sources[node.fileUri]));
}
@override
void exitFileUri(FileUriNode node) {
Source? source = _sources[node.fileUri];
if (source != null) {
_currentSources.removeLast();
}
_currentSources.removeLast();
}
@override
@ -94,7 +88,13 @@ abstract class OffsetVisitor extends FileUriVisitor {
void _collect(TreeNode node) {
if (_currentSources.isNotEmpty) {
Source currentSource = _currentSources.last;
var (Uri currentUri, Source? currentSource) = _currentSources.last;
if (currentSource == null) {
if (isMacroLibraryUri(currentUri)) {
throw "Missing source for macro library uri ${currentUri}.";
}
return;
}
String sourceText = currentSource.text;
List<OffsetInfo?> offsetInfoList = [];
for (int offset in node.fileOffsetsIfMultiple ?? [node.fileOffset]) {

View file

@ -10,17 +10,17 @@ import 'package:kernel/ast.dart';
/// merged augmentation library.
class ReOffset {
final Uri intermediateAugmentationUri;
final Uri augmentationUri;
final Uri augmentationFileUri;
final List<MapEntry<int, int?>> _offsets;
/// Creates a [ReOffset] from the intermediate augmentation library
/// [intermediateAugmentationUri] to the merged augmentation library
/// [augmentationUri] using [reOffsetMap] which maps the start of an
/// [augmentationFileUri] using [reOffsetMap] which maps the start of an
/// offset range in [intermediateAugmentationUri] to the start of the
/// corresponding offset range in [augmentationUri].
/// corresponding offset range in [augmentationFileUri].
///
/// The keys of [reOffsetMap] are assumed to be sorted.
ReOffset(this.intermediateAugmentationUri, this.augmentationUri,
ReOffset(this.intermediateAugmentationUri, this.augmentationFileUri,
Map<int, int?> reOffsetMap)
: _offsets = reOffsetMap.entries.toList(),
assert(() {
@ -213,7 +213,7 @@ class ReOffsetVisitor extends FileUriVisitor {
void exitFileUri(FileUriNode node) {
ReOffset? reOffset = _reOffsetMaps[node.fileUri];
if (reOffset != null) {
node.fileUri = reOffset.augmentationUri;
node.fileUri = reOffset.augmentationFileUri;
_currentReOffsets.removeLast();
}
}

View file

@ -5,6 +5,10 @@
"name": "macro",
"rootUri": "pkgs/macro/lib/"
},
{
"name": "example",
"rootUri": "pkgs/example/lib/"
},
{
"name": "meta",
"rootUri": "../../../../../meta/",

View file

@ -0,0 +1,8 @@
// Copyright (c) 2024, 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:macro/macro.dart';
//@FunctionDefinitionMacro1()
external String example();

View file

@ -0,0 +1,9 @@
// Copyright (c) 2024, 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:example/example.dart';
method() {
example();
}

View file

@ -0,0 +1,10 @@
library;
import self as self;
import "package:example/example.dart" as exa;
import "package:example/example.dart";
static method method() → dynamic {
exa::example();
}