Skip to content

Commit b882e0c

Browse files
committed
Add SourceKit support for expanding peer macros
1 parent 2d4b6ca commit b882e0c

File tree

5 files changed

+45
-17
lines changed

5 files changed

+45
-17
lines changed

include/swift/AST/TypeCheckRequests.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3911,15 +3911,15 @@ class CompilerPluginLoadRequest
39113911
/// Expand peer macros attached to the given declaration.
39123912
class ExpandPeerMacroRequest
39133913
: public SimpleRequest<ExpandPeerMacroRequest,
3914-
bool(Decl *),
3914+
ArrayRef<unsigned>(Decl *),
39153915
RequestFlags::Cached> {
39163916
public:
39173917
using SimpleRequest::SimpleRequest;
39183918

39193919
private:
39203920
friend SimpleRequest;
39213921

3922-
bool evaluate(Evaluator &evaluator, Decl *decl) const;
3922+
ArrayRef<unsigned> evaluate(Evaluator &evaluator, Decl *decl) const;
39233923

39243924
public:
39253925
bool isCached() const { return true; }

lib/Refactoring/Refactoring.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8514,6 +8514,12 @@ getMacroExpansionBuffers(MacroDecl *macro, const CustomAttr *attr, Decl *decl) {
85148514
allBufferIDs.append(bufferIDs.begin(), bufferIDs.end());
85158515
}
85168516

8517+
if (roles.contains(MacroRole::Peer)) {
8518+
auto bufferIDs = evaluateOrDefault(
8519+
ctx.evaluator, ExpandPeerMacroRequest{decl}, { });
8520+
allBufferIDs.append(bufferIDs.begin(), bufferIDs.end());
8521+
}
8522+
85178523
// Drop any buffers that come from other macros. We could eliminate this
85188524
// step by adding more fine-grained requests above, which only expand for a
85198525
// single custom attribute.

lib/Sema/TypeCheckMacros.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ static std::string adjustMacroExpansionBufferName(StringRef name) {
391391
ArrayRef<unsigned> ExpandMemberAttributeMacros::evaluate(Evaluator &evaluator,
392392
Decl *decl) const {
393393
if (decl->isImplicit())
394-
return { }};
394+
return { };
395395

396396
auto *parentDecl = decl->getDeclContext()->getAsDecl();
397397
if (!parentDecl)
@@ -423,14 +423,16 @@ ArrayRef<unsigned> ExpandSynthesizedMemberMacroRequest::evaluate(
423423
return decl->getASTContext().AllocateCopy(bufferIDs);
424424
}
425425

426-
bool ExpandPeerMacroRequest::evaluate(Evaluator &evaluator, Decl *decl) const {
427-
bool addedPeers = false;
426+
ArrayRef<unsigned>
427+
ExpandPeerMacroRequest::evaluate(Evaluator &evaluator, Decl *decl) const {
428+
SmallVector<unsigned, 2> bufferIDs;
428429
decl->forEachAttachedMacro(MacroRole::Peer,
429430
[&](CustomAttr *attr, MacroDecl *macro) {
430-
addedPeers |= expandPeers(attr, macro, decl);
431+
if (auto bufferID = expandPeers(attr, macro, decl))
432+
bufferIDs.push_back(*bufferID);
431433
});
432434

433-
return addedPeers;
435+
return decl->getASTContext().AllocateCopy(bufferIDs);
434436
}
435437

436438
/// Determine whether the given source file is from an expansion of the given
@@ -1034,7 +1036,7 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
10341036
case MacroRole::Peer: {
10351037
generatedSourceKind = GeneratedSourceInfo::PeerMacroExpansion;
10361038
SourceLoc afterDeclLoc =
1037-
Lexer::getLocForEndOfToken(sourceMgr, decl->getEndLoc());
1039+
Lexer::getLocForEndOfToken(sourceMgr, attachedTo->getEndLoc());
10381040
generatedOriginalSourceRange = CharSourceRange(afterDeclLoc, 0);
10391041
break;
10401042
}
@@ -1175,16 +1177,16 @@ swift::expandMembers(CustomAttr *attr, MacroDecl *macro, Decl *decl) {
11751177
return macroSourceFile->getBufferID();
11761178
}
11771179

1178-
bool swift::expandPeers(CustomAttr *attr, MacroDecl *macro, Decl *decl) {
1180+
Optional<unsigned>
1181+
swift::expandPeers(CustomAttr *attr, MacroDecl *macro, Decl *decl) {
11791182
auto macroSourceFile = evaluateAttachedMacro(macro, decl, attr,
11801183
/*passParentContext*/false,
11811184
MacroRole::Peer);
11821185
if (!macroSourceFile)
1183-
return false;
1186+
return None;
11841187

11851188
PrettyStackTraceDecl debugStack("applying expanded peer macro", decl);
11861189

1187-
bool addedPeers = false;
11881190
auto *parent = decl->getDeclContext();
11891191
auto topLevelDecls = macroSourceFile->getTopLevelDecls();
11901192
for (auto peer : topLevelDecls) {
@@ -1196,11 +1198,9 @@ bool swift::expandPeers(CustomAttr *attr, MacroDecl *macro, Decl *decl) {
11961198
// TODO: Add peers to global or local contexts.
11971199
continue;
11981200
}
1199-
1200-
addedPeers = true;
12011201
}
12021202

1203-
return addedPeers;
1203+
return macroSourceFile->getBufferID();
12041204
}
12051205

12061206
MacroDecl *

lib/Sema/TypeCheckMacros.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ expandMembers(CustomAttr *attr, MacroDecl *macro, Decl *decl);
7070
/// Expand the peer declarations for the given declaration based on
7171
/// the custom attribute that references the given macro.
7272
///
73-
/// Returns \c true if the macro added new peers, \c false otherwise.
74-
bool expandPeers(CustomAttr *attr, MacroDecl *macro, Decl *decl);
73+
/// If expansion occurred, returns the macro expansion buffer ID.
74+
Optional<unsigned> expandPeers(CustomAttr *attr, MacroDecl *macro, Decl *decl);
7575

7676
} // end namespace swift
7777

test/SourceKit/Macros/macro_basic.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ struct S2 {
3434
var y: Int = 17
3535
}
3636

37+
@attached(peer)
38+
macro addCompletionHandler() = #externalMacro(module: "MacroDefinition", type: "AddCompletionHandler")
39+
40+
@available(macOS 10.15, *)
41+
struct S3 {
42+
@addCompletionHandler
43+
func f(a: Int, for b: String, _ value: Double) async -> String {
44+
return b
45+
}
46+
}
47+
3748
// FIXME: Swift parser is not enabled on Linux CI yet.
3849
// REQUIRES: OS=macosx
3950

@@ -113,7 +124,7 @@ struct S2 {
113124
// ACCESSOR1_EXPAND: source.edit.kind.active:
114125
// ACCESSOR1_EXPAND: 30:3-30:20 ""
115126

116-
//##-- Refactoring expanding the first accessor macro
127+
//##-- Refactoring expanding the second accessor macro
117128
// RUN: %sourcekitd-test -req=refactoring.expand.macro -pos=33:13 %s -- ${COMPILER_ARGS[@]} | %FileCheck -check-prefix=ACCESSOR2_EXPAND %s
118129
// ACCESSOR2_EXPAND: source.edit.kind.active:
119130
// ACCESSOR2_EXPAND: 34:14-34:18 "{
@@ -122,3 +133,14 @@ struct S2 {
122133
// ACCESSOR2_EXPAND: }"
123134
// ACCESSOR2_EXPAND: source.edit.kind.active:
124135
// ACCESSOR2_EXPAND: 33:3-33:20 ""
136+
137+
//##-- Refactoring expanding the second accessor macro
138+
// RUN: %sourcekitd-test -req=refactoring.expand.macro -pos=42:5 %s -- ${COMPILER_ARGS[@]} | %FileCheck -check-prefix=PEER_EXPAND %s
139+
// PEER_EXPAND: source.edit.kind.active:
140+
// PEER_EXPAND: 45:4-45:4 "func f(a: Int, for b: String, _ value: Double, completionHandler: (String) -> Void) {
141+
// PEER_EXPAND: Task {
142+
// PEER_EXPAND: completionHandler(await f(a: a, for: b, value))
143+
// PEER_EXPAND: }
144+
// PEER_EXPAND: }"
145+
// PEER_EXPAND: source.edit.kind.active:
146+
// PEER_EXPAND: 42:3-42:24 ""

0 commit comments

Comments
 (0)