Skip to content

Commit ae8fc9b

Browse files
Merge pull request #6181 from adrian-prantl/macro-test
Add an LLDB test for Swift macros.
2 parents b3cfef4 + 54302f9 commit ae8fc9b

File tree

5 files changed

+87
-0
lines changed

5 files changed

+87
-0
lines changed

lldb/packages/Python/lldbsuite/test/make/Makefile.rules

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,12 @@ SWIFT_BRIDGING_PCH :=
304304
SWIFT_HFLAGS :=
305305
endif
306306

307+
ifeq "$(OS)" "Linux"
308+
SWIFT_HOSTDIR = $(shell dirname $(SWIFTC))/../lib/swift/linux
309+
else
310+
SWIFT_HOSTDIR = $(shell dirname $(SWIFTC))/../lib/swift/host
311+
endif
312+
307313
# Use this one if you want to build one part of the result without debug information:
308314
ifeq "$(OS)" "Darwin"
309315
CFLAGS_NO_DEBUG = -O0 $(ARCHFLAG) $(ARCH) $(FRAMEWORK_INCLUDES) $(ARCH_CFLAGS) $(CFLAGS_EXTRAS) -isysroot "$(SDKROOT)"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import SwiftSyntax
2+
import SwiftSyntaxBuilder
3+
import SwiftSyntaxMacros
4+
5+
public struct StringifyMacro: ExpressionMacro {
6+
public static func expansion(
7+
of macro: some FreestandingMacroExpansionSyntax,
8+
in context: some MacroExpansionContext
9+
) -> ExprSyntax {
10+
guard let argument = macro.argumentList.first?.expression else {
11+
fatalError("boom")
12+
}
13+
14+
return "(\(argument), \(StringLiteralExprSyntax(content: argument.description)))"
15+
}
16+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
SWIFT_SOURCES := main.swift
2+
SWIFTFLAGS_EXTRAS = -enable-experimental-feature Macros \
3+
-load-plugin-library $(BUILDDIR)/libMacroImpl.dylib
4+
5+
all: libMacroImpl.dylib $(EXE)
6+
7+
include Makefile.rules
8+
9+
libMacroImpl.dylib:
10+
$(MAKE) MAKE_DSYM=$(MAKE_DSYM) CC=$(CC) SWIFTC=$(SWIFTC) \
11+
ARCH=$(ARCH) DSYMUTIL=$(DSYMUTIL) \
12+
VPATH=$(SRCDIR) -I $(SRCDIR) \
13+
-f $(THIS_FILE_DIR)/Makefile.rules \
14+
DYLIB_SWIFT_SOURCES=MacroImpl.swift \
15+
DYLIB_NAME=MacroImpl \
16+
DYLIB_ONLY=YES \
17+
SWIFTFLAGS_EXTRAS="-I$(SWIFT_HOSTDIR)" \
18+
LD_EXTRAS="-L$(SWIFT_HOSTDIR)" \
19+
SWIFT_SOURCES= \
20+
all
21+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import lldb
2+
from lldbsuite.test.decorators import *
3+
import lldbsuite.test.lldbtest as lldbtest
4+
import lldbsuite.test.lldbutil as lldbutil
5+
import unittest2
6+
7+
8+
class TestSwiftMacro(lldbtest.TestBase):
9+
10+
@swiftTest
11+
# At the time of writing swift/test/Macros/macro_expand.swift is also disabled.
12+
@expectedFailureAll(oslist=['linux'])
13+
def test(self):
14+
"""Test Swift macros"""
15+
self.build()
16+
target, process, thread, bkpt = lldbutil.run_to_source_breakpoint(
17+
self, 'break here', lldb.SBFileSpec('main.swift'))
18+
19+
thread.StepOver()
20+
thread.StepInto()
21+
# This is the expanded macro source, we should be able to step into it.
22+
self.expect('reg read pc', substrs=[
23+
'[inlined] stringify at',
24+
'13testStringify'
25+
])
26+
27+
# Macros are not supported in the expression evaluator.
28+
self.expect('expression -- #stringify(1)', error=True,
29+
substrs=["external macro implementation",
30+
'MacroImpl.StringifyMacro',
31+
"could not be found"])
32+
33+
# Make sure we can set a symbolic breakpoint on a macro.
34+
b = target.BreakpointCreateByName("stringify")
35+
self.assertGreaterEqual(b.GetNumLocations(), 1)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@freestanding(expression) macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroImpl", type: "StringifyMacro")
2+
3+
func testStringify(a: Int, b: Int) {
4+
print("break here")
5+
let s = #stringify(a + b)
6+
print(s.1)
7+
}
8+
9+
testStringify(a: 23, b: 42)

0 commit comments

Comments
 (0)