Skip to content

Commit 799d086

Browse files
committed
[SILGen] Expand magic literals like #file and #line to values in the outermost
source file. Values inside macro expansion buffers are not useful. (cherry picked from commit 9eb9ba2)
1 parent 6ba52b7 commit 799d086

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed

lib/SILGen/SILGenApply.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2124,6 +2124,28 @@ buildBuiltinLiteralArgs(SILGenFunction &SGF, SGFContext C,
21242124
return args;
21252125
}
21262126

2127+
/// Returns the source location in the outermost source file
2128+
/// for the given source location.
2129+
///
2130+
/// If the given source loc is in a macro expansion buffer, this
2131+
/// method walks up the macro expansion buffer tree to the outermost
2132+
/// source file. Otherwise, the method returns the given loc.
2133+
static SourceLoc
2134+
getLocInOutermostSourceFile(SourceManager &sourceManager, SourceLoc loc) {
2135+
auto outermostLoc = loc;
2136+
auto bufferID = sourceManager.findBufferContainingLoc(outermostLoc);
2137+
2138+
// Walk up the macro expansion buffer tree to the outermost
2139+
// source file.
2140+
while (auto generated = sourceManager.getGeneratedSourceInfo(bufferID)) {
2141+
auto node = ASTNode::getFromOpaqueValue(generated->astNode);
2142+
outermostLoc = node.getStartLoc();
2143+
bufferID = sourceManager.findBufferContainingLoc(outermostLoc);
2144+
}
2145+
2146+
return outermostLoc;
2147+
}
2148+
21272149
static inline PreparedArguments
21282150
buildBuiltinLiteralArgs(SILGenFunction &SGF, SGFContext C,
21292151
MagicIdentifierLiteralExpr *magicLiteral) {
@@ -2153,7 +2175,8 @@ buildBuiltinLiteralArgs(SILGenFunction &SGF, SGFContext C,
21532175

21542176
case MagicIdentifierLiteralExpr::Line:
21552177
case MagicIdentifierLiteralExpr::Column: {
2156-
SourceLoc Loc = magicLiteral->getStartLoc();
2178+
auto Loc = getLocInOutermostSourceFile(SGF.getSourceManager(),
2179+
magicLiteral->getStartLoc());
21572180
unsigned Value = 0;
21582181
if (Loc.isValid()) {
21592182
Value = magicLiteral->getKind() == MagicIdentifierLiteralExpr::Line
@@ -6002,7 +6025,10 @@ StringRef SILGenFunction::getMagicFunctionString() {
60026025

60036026
StringRef SILGenFunction::getMagicFilePathString(SourceLoc loc) {
60046027
assert(loc.isValid());
6005-
return getSourceManager().getDisplayNameForLoc(loc);
6028+
auto &sourceManager = getSourceManager();
6029+
auto outermostLoc = getLocInOutermostSourceFile(sourceManager, loc);
6030+
6031+
return getSourceManager().getDisplayNameForLoc(outermostLoc);
60066032
}
60076033

60086034
std::string SILGenFunction::getMagicFileIDString(SourceLoc loc) {

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,3 +1838,36 @@ public struct PeerValueWithSuffixNameMacro: PeerMacro {
18381838
return ["var \(raw: identified.identifier.text)_peer: Int { 1 }"]
18391839
}
18401840
}
1841+
1842+
public struct MagicFileMacro: ExpressionMacro {
1843+
public static func expansion(
1844+
of node: some FreestandingMacroExpansionSyntax,
1845+
in context: some MacroExpansionContext
1846+
) -> ExprSyntax {
1847+
return "#file"
1848+
}
1849+
}
1850+
1851+
public struct MagicLineMacro: ExpressionMacro {
1852+
public static func expansion(
1853+
of node: some FreestandingMacroExpansionSyntax,
1854+
in context: some MacroExpansionContext
1855+
) -> ExprSyntax {
1856+
return "(#line)"
1857+
}
1858+
}
1859+
1860+
public struct NestedMagicLiteralMacro: ExpressionMacro {
1861+
public static func expansion(
1862+
of node: some FreestandingMacroExpansionSyntax,
1863+
in context: some MacroExpansionContext
1864+
) -> ExprSyntax {
1865+
return
1866+
"""
1867+
{
1868+
print(#MagicFile)
1869+
print(#MagicLine)
1870+
}()
1871+
"""
1872+
}
1873+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// REQUIRES: swift_swift_parser, executable_test
2+
3+
// RUN: %empty-directory(%t)
4+
// RUN: %host-build-swift -swift-version 5 -emit-library -o %t/%target-library-name(MacroDefinition) -module-name=MacroDefinition %S/Inputs/syntax_macro_definitions.swift -g -no-toolchain-stdlib-rpath
5+
6+
// RUN: %target-build-swift -enable-experimental-feature ExtensionMacros -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) %s -o %t/main -module-name MacroUser -swift-version 5 -emit-tbd -emit-tbd-path %t/MacroUser.tbd -I %t
7+
// RUN: %target-codesign %t/main
8+
// RUN: %target-run %t/main | %FileCheck %s
9+
10+
11+
@freestanding(expression)
12+
macro MagicFile() -> String = #externalMacro(module: "MacroDefinition", type: "MagicFileMacro")
13+
14+
@freestanding(expression)
15+
macro MagicLine() -> Int = #externalMacro(module: "MacroDefinition", type: "MagicLineMacro")
16+
17+
// CHECK: magic_literal_expansion.swift
18+
print(#MagicFile)
19+
20+
// CHECK: 21
21+
print(#MagicLine)
22+
23+
@freestanding(expression)
24+
macro Nested() -> Void = #externalMacro(module: "MacroDefinition", type: "NestedMagicLiteralMacro")
25+
26+
// CHECK: magic_literal_expansion.swift
27+
// CHECK: 28
28+
#Nested

0 commit comments

Comments
 (0)