Skip to content

Commit 8cb488d

Browse files
authored
Merge pull request #66968 from bnbarham/cherry-index-macro-generics
[5.9][Index] Include generic macro arguments
2 parents 74c6c47 + 71b651b commit 8cb488d

File tree

3 files changed

+50
-35
lines changed

3 files changed

+50
-35
lines changed

lib/AST/ASTWalker.cpp

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -442,19 +442,33 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
442442
return false;
443443
}
444444

445+
bool visitFreestandingMacroArgs(FreestandingMacroExpansion *ME) {
446+
for (TypeRepr *genArg : ME->getGenericArgs()) {
447+
if (doIt(genArg))
448+
return true;
449+
}
450+
451+
if (ME->getArgs()) {
452+
ArgumentList *args = doIt(ME->getArgs());
453+
if (!args)
454+
return true;
455+
ME->setArgs(args);
456+
}
457+
458+
return false;
459+
}
460+
445461
bool visitMacroExpansionDecl(MacroExpansionDecl *MED) {
446462
#ifndef NDEBUG
447463
PrettyStackTraceDecl debugStack("walking into", MED);
448464
#endif
465+
449466
bool shouldWalkArguments, shouldWalkExpansion;
450467
std::tie(shouldWalkArguments, shouldWalkExpansion) =
451468
Walker.shouldWalkMacroArgumentsAndExpansion();
452-
if (shouldWalkArguments && MED->getArgs()) {
453-
if (auto *argList = doIt(MED->getArgs()))
454-
MED->setArgs(argList);
455-
else
456-
return true;
457-
}
469+
470+
if (shouldWalkArguments && visitFreestandingMacroArgs(MED))
471+
return true;
458472

459473
bool alreadyFailed = false;
460474
if (shouldWalkExpansion) {
@@ -1369,11 +1383,8 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
13691383
shouldWalkArguments = false;
13701384
}
13711385

1372-
if (shouldWalkArguments && E->getArgs()) {
1373-
ArgumentList *args = doIt(E->getArgs());
1374-
if (!args) return nullptr;
1375-
E->setArgs(args);
1376-
}
1386+
if (shouldWalkArguments && visitFreestandingMacroArgs(E))
1387+
return nullptr;
13771388

13781389
if (shouldWalkExpansion) {
13791390
Expr *rewritten = nullptr;

lib/IDE/SourceEntityWalker.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -732,22 +732,22 @@ bool SemaAnnotator::handleCustomAttributes(Decl *D) {
732732
if (auto *Repr = customAttr->getTypeRepr()) {
733733
// It's a little weird that attached macros have a `TypeRepr` to begin
734734
// with, but given they aren't types they then don't get bound. So check
735-
// for a macro here and and pass a reference to it, or just walk the
736-
// `TypeRepr` where we will then pull out the bound decl otherwise.
735+
// for a macro here and and pass a reference to it.
737736
auto *mutableAttr = const_cast<CustomAttr *>(customAttr);
738737
if (auto macroDecl = D->getResolvedMacro(mutableAttr)) {
739738
Type macroRefType = macroDecl->getDeclaredInterfaceType();
740-
if (!passReference(
741-
macroDecl, macroRefType, DeclNameLoc(Repr->getStartLoc()),
742-
ReferenceMetaData(
743-
SemaReferenceKind::DeclRef, None,
744-
/*isImplicit=*/false,
745-
std::make_pair(customAttr,
746-
expansion ? expansion.get<Decl *>() : D))))
739+
auto customAttrRef =
740+
std::make_pair(customAttr, expansion ? expansion.get<Decl *>() : D);
741+
auto refMetadata =
742+
ReferenceMetaData(SemaReferenceKind::DeclRef, llvm::None,
743+
/*isImplicit=*/false, customAttrRef);
744+
if (!passReference(macroDecl, macroRefType,
745+
DeclNameLoc(Repr->getStartLoc()), refMetadata))
747746
return false;
748-
} else if (!Repr->walk(*this)) {
749-
return false;
750747
}
748+
749+
if (!Repr->walk(*this))
750+
return false;
751751
}
752752

753753
if (auto *SemaInit = customAttr->getSemanticInit()) {

test/Index/index_macros.swift

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@
1010
// RUN: %host-build-swift -swift-version 5 -emit-library -o %t/%target-library-name(IndexMacros) -module-name=IndexMacros %t/IndexMacros.swift -g -no-toolchain-stdlib-rpath
1111

1212
// Check indexed symbols
13-
// RUN: %target-swift-ide-test -print-indexed-symbols -source-filename %t/IndexTest.swift -load-plugin-library %t/%target-library-name(IndexMacros) -parse-as-library 2>&1 | tee %t/test.idx | %FileCheck %s
13+
// RUN: %target-swift-ide-test -print-indexed-symbols -source-filename %t/IndexTest.swift -load-plugin-library %t/%target-library-name(IndexMacros) -parse-as-library > %t/index.out
14+
// RUN: %FileCheck %s --input-file %t/index.out
1415

1516
//--- IndexTest.swift
1617
@freestanding(expression)
17-
macro freestandingExpr() = #externalMacro(module: "IndexMacros", type: "FreestandingExprMacro")
18-
// CHECK: [[@LINE-1]]:7 | macro/Swift | freestandingExpr() | [[EXPR_USR:.*]] | Def
18+
macro freestandingExpr<T>(arg: T) = #externalMacro(module: "IndexMacros", type: "FreestandingExprMacro")
19+
// CHECK: [[@LINE-1]]:7 | macro/Swift | freestandingExpr(arg:) | [[EXPR_USR:.*]] | Def
1920

2021
@freestanding(declaration, names: named(TestFree))
21-
macro freestandingDecl() = #externalMacro(module: "IndexMacros", type: "FreestandingDeclMacro")
22-
// CHECK: [[@LINE-1]]:7 | macro/Swift | freestandingDecl() | [[DECL_USR:.*]] | Def
22+
macro freestandingDecl<T>(arg: T) = #externalMacro(module: "IndexMacros", type: "FreestandingDeclMacro")
23+
// CHECK: [[@LINE-1]]:7 | macro/Swift | freestandingDecl(arg:) | [[DECL_USR:.*]] | Def
2324

2425
@attached(accessor)
2526
macro Accessor() = #externalMacro(module: "IndexMacros", type: "SomeAccessorMacro")
@@ -38,8 +39,8 @@ macro MemberAttribute() = #externalMacro(module: "IndexMacros", type: "SomeMembe
3839
// CHECK: [[@LINE-1]]:7 | macro/Swift | MemberAttribute() | [[MEMBER_ATTRIBUTE_USR:.*]] | Def
3940

4041
@attached(peer, names: named(TestPeer))
41-
macro Peer() = #externalMacro(module: "IndexMacros", type: "SomePeerMacro")
42-
// CHECK: [[@LINE-1]]:7 | macro/Swift | Peer() | [[PEER_USR:.*]] | Def
42+
macro Peer<T>(arg: T) = #externalMacro(module: "IndexMacros", type: "SomePeerMacro")
43+
// CHECK: [[@LINE-1]]:7 | macro/Swift | Peer(arg:) | [[PEER_USR:.*]] | Def
4344

4445
@attached(peer, names: named(peerMember))
4546
macro PeerMember() = #externalMacro(module: "IndexMacros", type: "SomePeerMemberMacro")
@@ -72,26 +73,30 @@ struct AddOne {
7273
}
7374
}
7475

76+
// CHECK: [[@LINE+2]]:2 | macro/Swift | freestandingDecl(arg:) | [[DECL_USR]] | Ref
77+
// CHECK: [[@LINE+1]]:19 | struct/Swift | Double | s:Sd | Ref
78+
#freestandingDecl<Double>(arg: 1.0)
7579
// Creates a `TestFree` struct with `freeFunc` calling `freeLog`
76-
#freestandingDecl
77-
// CHECK: [[@LINE-1]]:2 | macro/Swift | freestandingDecl() | [[DECL_USR]] | Ref
7880
// CHECK: [[@LINE-2]]:1 | struct/Swift | TestFree | [[FREE_STRUCT_USR:.*]] | Def,Impl
7981
// CHECK: [[@LINE-3]]:1 | instance-method/Swift | freeFunc() | [[FREE_FUNC_USR:.*]] | Def,Impl,RelChild
8082
// CHECK-NEXT: RelChild | struct/Swift | TestFree | [[FREE_STRUCT_USR]]
8183
// CHECK: [[@LINE-5]]:1 | function/Swift | freeLog() | [[FREE_LOG_USR]] | Ref,Call,Impl,RelCall,RelCont
8284
// CHECK-NEXT: RelCall,RelCont | instance-method/Swift | freeFunc() | [[FREE_FUNC_USR]]
8385

8486
func testExpr() {
85-
#freestandingExpr
87+
// CHECK: [[@LINE+2]]:4 | macro/Swift | freestandingExpr(arg:) | [[EXPR_USR]] | Ref
88+
// CHECK: [[@LINE+1]]:21 | struct/Swift | Double | s:Sd | Ref
89+
#freestandingExpr<Double>(arg: 1.0)
8690
// CHECK: [[@LINE-1]]:3 | function/Swift | exprLog() | [[EXPR_LOG_USR]] | Ref,Call,Impl,RelCall,RelCont
8791
// CHECK-NEXT: RelCall,RelCont | function/Swift | testExpr()
8892
}
8993

90-
// CHECK: [[@LINE+4]]:40 | macro/Swift | Peer() | [[PEER_USR]] | Ref
94+
// CHECK: [[@LINE+5]]:40 | macro/Swift | Peer(arg:) | [[PEER_USR]] | Ref
95+
// CHECK: [[@LINE+4]]:45 | struct/Swift | Double | s:Sd | Ref
9196
// CHECK: [[@LINE+3]]:23 | macro/Swift | MemberAttribute() | [[MEMBER_ATTRIBUTE_USR]] | Ref
9297
// CHECK: [[@LINE+2]]:15 | macro/Swift | Member() | [[MEMBER_USR]] | Ref
9398
// CHECK: [[@LINE+1]]:2 | macro/Swift | Conformance() | [[CONFORMANCE_USR]] | Ref
94-
@Conformance @Member @MemberAttribute @Peer
99+
@Conformance @Member @MemberAttribute @Peer<Double>(arg: 1.0)
95100
struct TestAttached {
96101
var attachedMember: Int
97102

@@ -151,7 +156,6 @@ struct Outer {
151156
// CHECK: [[@LINE-19]]:3 | protocol/Swift | TestProto | [[PROTO_USR]] | Ref,Impl,RelBase
152157
// CHECK-NEXT: RelBase | extension/ext-struct/Swift | TestInner
153158

154-
155159
//--- IndexMacros.swift
156160
import SwiftSyntax
157161
import SwiftSyntaxBuilder

0 commit comments

Comments
 (0)