Skip to content

Commit c8819bd

Browse files
committed
[Indexing] Index macro expansion expressions.
1 parent bbbc346 commit c8819bd

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

lib/IDE/SourceEntityWalker.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,30 @@ ASTWalker::PreWalkResult<Expr *> SemaAnnotator::walkToExprPre(Expr *E) {
590590
return Action::Stop();
591591
// We already visited the children.
592592
return doSkipChildren();
593+
} else if (auto ME = dyn_cast<MacroExpansionExpr>(E)) {
594+
// The macro itself.
595+
auto macroRef = ME->getMacroRef();
596+
auto macroDecl = macroRef.getDecl();
597+
auto macroRefType =
598+
macroDecl->getInterfaceType().subst(macroRef.getSubstitutions());
599+
if (!passReference(
600+
macroDecl, macroRefType, ME->getMacroNameLoc(),
601+
ReferenceMetaData(SemaReferenceKind::DeclRef, None)))
602+
return Action::Stop();
603+
604+
// Walk the arguments, since they were written directly by the user.
605+
if (auto argList = ME->getArgs()) {
606+
if (!argList->walk(*this))
607+
return Action::Stop();
608+
}
609+
610+
if (auto rewritten = ME->getRewritten()) {
611+
if (!rewritten->walk(*this))
612+
return Action::Stop();
613+
}
614+
615+
// Already walked the children.
616+
return doSkipChildren();
593617
}
594618

595619
return Action::Continue(E);

test/Index/index_macros.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %empty-directory(%t)
2+
//
3+
// RUN: %target-swift-ide-test -print-indexed-symbols -source-filename %s -enable-experimental-feature Macros | %FileCheck %s
4+
5+
6+
macro myLine: Int = _SwiftSyntaxMacros.LineMacro
7+
macro myFilename<T: ExpressibleByStringLiteral>: T = _SwiftSyntaxMacros.FilePathMacro
8+
macro myStringify<T>(_: T) -> (T, String) = _SwiftSyntaxMacros.StringifyMacro
9+
10+
func test(x: Int) {
11+
_ = #myLine
12+
let _: String = #myFilename
13+
_ = #myStringify(x + x)
14+
}
15+
16+
// CHECK: 6:7 | macro/Swift | myLine | s:14swift_ide_test6myLineSifm | Def | rel: 0
17+
// CHECK: 6:15 | struct/Swift | Int | s:Si | Ref | rel: 0
18+
// CHECK: 7:7 | macro/Swift | myFilename | s:14swift_ide_test10myFilenamexfm | Def | rel: 0
19+
// CHECK: 7:21 | protocol/Swift | ExpressibleByStringLiteral | s:s26ExpressibleByStringLiteralP | Ref | rel: 0
20+
// CHECK: 8:7 | macro/Swift | myStringify(_:) | s:14swift_ide_test11myStringifyyx_SStxcfm | Def | rel: 0
21+
22+
// CHECK: 11:8 | macro/Swift | myLine | s:14swift_ide_test6myLineSifm | Ref,RelCont | rel: 1
23+
// CHECK: 12:20 | macro/Swift | myFilename | s:14swift_ide_test10myFilenamexfm | Ref,RelCont | rel: 1
24+
// CHECK: 13:8 | macro/Swift | myStringify(_:) | s:14swift_ide_test11myStringifyyx_SStxcfm | Ref,RelCont | rel: 1

0 commit comments

Comments
 (0)