Skip to content

Commit 87aaec4

Browse files
authored
[SILGen] Make #file to respect #sourceLocation (#5425)
Code func foo(f: String = #file) { print(f) } #sourceLocation(file: "virtual.swift", line: 1) foo() should print "virtual.swift"
1 parent 68f53dc commit 87aaec4

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

lib/SILGen/SILGenApply.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4764,10 +4764,8 @@ RValue SILGenFunction::emitLiteral(LiteralExpr *literal, SGFContext C) {
47644764
switch (magicLiteral->getKind()) {
47654765
case MagicIdentifierLiteralExpr::File: {
47664766
StringRef value = "";
4767-
if (loc.isValid()) {
4768-
unsigned bufferID = ctx.SourceMgr.findBufferContainingLoc(loc);
4769-
value = ctx.SourceMgr.getIdentifierForBuffer(bufferID);
4770-
}
4767+
if (loc.isValid())
4768+
value = ctx.SourceMgr.getBufferIdentifierForLoc(loc);
47714769
builtinLiteralArgs = emitStringLiteral(*this, literal, value, C,
47724770
magicLiteral->getStringEncoding());
47734771
builtinInit = magicLiteral->getBuiltinInitializer();

test/SILGen/source_location.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %target-swift-frontend -Xllvm -sil-full-demangle -emit-silgen %s | %FileCheck %s
2+
3+
func printSourceLocation(file: String = #file, line: Int = #line) {}
4+
5+
#sourceLocation(file: "caller.swift", line: 10000)
6+
_ = printSourceLocation()
7+
// CHECK: [[PRINT_SOURCE_LOCATION:%.*]] = function_ref @_TF15source_location19printSourceLocationFT4fileSS4lineSi_T_
8+
// CHECK: [[CALLER_FILE_VAL:%.*]] = string_literal utf16 "caller.swift",
9+
// CHECK: [[CALLER_FILE:%.*]] = apply {{.*}}([[CALLER_FILE_VAL]],
10+
// CHECK: [[CALLER_LINE_VAL:%.*]] = integer_literal $Builtin.Int{{[0-9]+}}, 10000,
11+
// CHECK: [[CALLER_LINE:%.*]] = apply {{.*}}([[CALLER_LINE_VAL]],
12+
// CHECK: apply [[PRINT_SOURCE_LOCATION]]([[CALLER_FILE]], [[CALLER_LINE]])
13+
14+
#sourceLocation(file: "inplace.swift", line: 20000)
15+
let FILE = #file, LINE = #line
16+
// CHECK: [[FILE_ADDR:%.*]] = global_addr @_Tv15source_location4FILESS
17+
// CHECK: [[INPLACE_FILE_VAL:%.*]] = string_literal utf16 "inplace.swift",
18+
// CHECK: [[INPLACE_FILE:%.*]] = apply {{.*}}([[INPLACE_FILE_VAL]],
19+
// CHECK: store [[INPLACE_FILE]] to [[FILE_ADDR]]
20+
// CHECK: [[LINE_ADDR:%.*]] = global_addr @_Tv15source_location4LINESi
21+
// CHECK: [[INPLACE_LINE_VAL:%.*]] = integer_literal $Builtin.Int{{[0-9]+}}, 20000,
22+
// CHECK: [[INPLACE_LINE:%.*]] = apply {{.*}}([[INPLACE_LINE_VAL]],
23+
// CHECK: store [[INPLACE_LINE]] to [[LINE_ADDR]]

0 commit comments

Comments
 (0)