Skip to content

[5.9][Index] Include generic macro arguments #66968

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions lib/AST/ASTWalker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,19 +442,33 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
return false;
}

bool visitFreestandingMacroArgs(FreestandingMacroExpansion *ME) {
for (TypeRepr *genArg : ME->getGenericArgs()) {
if (doIt(genArg))
return true;
}

if (ME->getArgs()) {
ArgumentList *args = doIt(ME->getArgs());
if (!args)
return true;
ME->setArgs(args);
}

return false;
}

bool visitMacroExpansionDecl(MacroExpansionDecl *MED) {
#ifndef NDEBUG
PrettyStackTraceDecl debugStack("walking into", MED);
#endif

bool shouldWalkArguments, shouldWalkExpansion;
std::tie(shouldWalkArguments, shouldWalkExpansion) =
Walker.shouldWalkMacroArgumentsAndExpansion();
if (shouldWalkArguments && MED->getArgs()) {
if (auto *argList = doIt(MED->getArgs()))
MED->setArgs(argList);
else
return true;
}

if (shouldWalkArguments && visitFreestandingMacroArgs(MED))
return true;

bool alreadyFailed = false;
if (shouldWalkExpansion) {
Expand Down Expand Up @@ -1369,11 +1383,8 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
shouldWalkArguments = false;
}

if (shouldWalkArguments && E->getArgs()) {
ArgumentList *args = doIt(E->getArgs());
if (!args) return nullptr;
E->setArgs(args);
}
if (shouldWalkArguments && visitFreestandingMacroArgs(E))
return nullptr;

if (shouldWalkExpansion) {
Expr *rewritten = nullptr;
Expand Down
22 changes: 11 additions & 11 deletions lib/IDE/SourceEntityWalker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -732,22 +732,22 @@ bool SemaAnnotator::handleCustomAttributes(Decl *D) {
if (auto *Repr = customAttr->getTypeRepr()) {
// It's a little weird that attached macros have a `TypeRepr` to begin
// with, but given they aren't types they then don't get bound. So check
// for a macro here and and pass a reference to it, or just walk the
// `TypeRepr` where we will then pull out the bound decl otherwise.
// for a macro here and and pass a reference to it.
auto *mutableAttr = const_cast<CustomAttr *>(customAttr);
if (auto macroDecl = D->getResolvedMacro(mutableAttr)) {
Type macroRefType = macroDecl->getDeclaredInterfaceType();
if (!passReference(
macroDecl, macroRefType, DeclNameLoc(Repr->getStartLoc()),
ReferenceMetaData(
SemaReferenceKind::DeclRef, None,
/*isImplicit=*/false,
std::make_pair(customAttr,
expansion ? expansion.get<Decl *>() : D))))
auto customAttrRef =
std::make_pair(customAttr, expansion ? expansion.get<Decl *>() : D);
auto refMetadata =
ReferenceMetaData(SemaReferenceKind::DeclRef, llvm::None,
/*isImplicit=*/false, customAttrRef);
if (!passReference(macroDecl, macroRefType,
DeclNameLoc(Repr->getStartLoc()), refMetadata))
return false;
} else if (!Repr->walk(*this)) {
return false;
}

if (!Repr->walk(*this))
return false;
}

if (auto *SemaInit = customAttr->getSemanticInit()) {
Expand Down
30 changes: 17 additions & 13 deletions test/Index/index_macros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@
// 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

// Check indexed symbols
// 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
// 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
// RUN: %FileCheck %s --input-file %t/index.out

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

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

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

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

@attached(peer, names: named(peerMember))
macro PeerMember() = #externalMacro(module: "IndexMacros", type: "SomePeerMemberMacro")
Expand Down Expand Up @@ -72,26 +73,30 @@ struct AddOne {
}
}

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

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

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

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


//--- IndexMacros.swift
import SwiftSyntax
import SwiftSyntaxBuilder
Expand Down