Skip to content

Commit e646fc5

Browse files
committed
[Index] Include generic macro arguments
`ASTWalker` was missing a walk into the generic arguments for freestanding declarations and expressions. `SemaAnnotator` was missing the walk into the `TypeRepr` when walking over custom attributes. Resolves rdar://110856428.
1 parent d230a1a commit e646fc5

File tree

3 files changed

+39
-24
lines changed

3 files changed

+39
-24
lines changed

lib/AST/ASTWalker.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,12 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
446446
#ifndef NDEBUG
447447
PrettyStackTraceDecl debugStack("walking into", MED);
448448
#endif
449+
450+
for (TypeRepr *genArg : MED->getGenericArgs()) {
451+
if (doIt(genArg))
452+
return true;
453+
}
454+
449455
bool shouldWalkArguments, shouldWalkExpansion;
450456
std::tie(shouldWalkArguments, shouldWalkExpansion) =
451457
Walker.shouldWalkMacroArgumentsAndExpansion();
@@ -1357,6 +1363,11 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
13571363
}
13581364

13591365
Expr *visitMacroExpansionExpr(MacroExpansionExpr *E) {
1366+
for (TypeRepr *genArg : E->getGenericArgs()) {
1367+
if (doIt(genArg))
1368+
return nullptr;
1369+
}
1370+
13601371
bool shouldWalkArguments, shouldWalkExpansion;
13611372
std::tie(shouldWalkArguments, shouldWalkExpansion) =
13621373
Walker.shouldWalkMacroArgumentsAndExpansion();

lib/IDE/SourceEntityWalker.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -740,22 +740,22 @@ bool SemaAnnotator::handleCustomAttributes(Decl *D) {
740740
if (auto *Repr = customAttr->getTypeRepr()) {
741741
// It's a little weird that attached macros have a `TypeRepr` to begin
742742
// with, but given they aren't types they then don't get bound. So check
743-
// for a macro here and and pass a reference to it, or just walk the
744-
// `TypeRepr` where we will then pull out the bound decl otherwise.
743+
// for a macro here and and pass a reference to it.
745744
auto *mutableAttr = const_cast<CustomAttr *>(customAttr);
746745
if (auto macroDecl = D->getResolvedMacro(mutableAttr)) {
747746
Type macroRefType = macroDecl->getDeclaredInterfaceType();
748-
if (!passReference(
749-
macroDecl, macroRefType, DeclNameLoc(Repr->getStartLoc()),
750-
ReferenceMetaData(
751-
SemaReferenceKind::DeclRef, None,
752-
/*isImplicit=*/false,
753-
std::make_pair(customAttr,
754-
expansion ? expansion.get<Decl *>() : D))))
747+
auto customAttrRef =
748+
std::make_pair(customAttr, expansion ? expansion.get<Decl *>() : D);
749+
auto refMetadata =
750+
ReferenceMetaData(SemaReferenceKind::DeclRef, None,
751+
/*isImplicit=*/false, customAttrRef);
752+
if (!passReference(macroDecl, macroRefType,
753+
DeclNameLoc(Repr->getStartLoc()), refMetadata))
755754
return false;
756-
} else if (!Repr->walk(*this)) {
757-
return false;
758755
}
756+
757+
if (!Repr->walk(*this))
758+
return false;
759759
}
760760

761761
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)