Skip to content

Commit c896794

Browse files
authored
Merge pull request swiftlang#29135 from brentdax/concise-for-thee-but-not-for-me
Use #file string for force-unwrap and force-try
2 parents 13b4259 + 0f47628 commit c896794

File tree

4 files changed

+38
-29
lines changed

4 files changed

+38
-29
lines changed

lib/SILGen/SILGenApply.cpp

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4865,34 +4865,30 @@ SILGenFunction::emitApplyOfLibraryIntrinsic(SILLocation loc,
48654865
finalArgs, calleeTypeInfo, ApplyOptions::None, ctx);
48664866
}
48674867

4868-
static StringRef
4869-
getMagicFunctionString(SILGenFunction &SGF) {
4870-
assert(SGF.MagicFunctionName
4868+
StringRef SILGenFunction::getMagicFunctionString() {
4869+
assert(MagicFunctionName
48714870
&& "asking for #function but we don't have a function name?!");
4872-
if (SGF.MagicFunctionString.empty()) {
4873-
llvm::raw_string_ostream os(SGF.MagicFunctionString);
4874-
SGF.MagicFunctionName.print(os);
4871+
if (MagicFunctionString.empty()) {
4872+
llvm::raw_string_ostream os(MagicFunctionString);
4873+
MagicFunctionName.print(os);
48754874
}
4876-
return SGF.MagicFunctionString;
4875+
return MagicFunctionString;
48774876
}
48784877

4879-
static StringRef
4880-
getMagicFilePathString(SILGenFunction &SGF, SourceLoc loc) {
4881-
if (!loc.isValid())
4882-
return "";
4883-
4884-
return SGF.getASTContext().SourceMgr.getDisplayNameForLoc(loc);
4878+
StringRef SILGenFunction::getMagicFilePathString(SourceLoc loc) {
4879+
assert(loc.isValid());
4880+
return getSourceManager().getDisplayNameForLoc(loc);
48854881
}
48864882

4887-
static std::string
4888-
getConciseMagicFileString(SILGenFunction &SGF, SourceLoc loc) {
4889-
if (!loc.isValid())
4890-
return "";
4883+
std::string SILGenFunction::getMagicFileString(SourceLoc loc) {
4884+
auto path = getMagicFilePathString(loc);
4885+
4886+
if (!getASTContext().LangOpts.EnableConcisePoundFile)
4887+
return path;
48914888

4892-
auto path = getMagicFilePathString(SGF, loc);
48934889
auto value = llvm::sys::path::filename(path).str();
48944890
value += " (";
4895-
value += SGF.getModule().getSwiftModule()->getNameStr();
4891+
value += getModule().getSwiftModule()->getNameStr();
48964892
value += ")";
48974893
return value;
48984894
}
@@ -5152,9 +5148,7 @@ RValue SILGenFunction::emitLiteral(LiteralExpr *literal, SGFContext C) {
51525148
auto magicLiteral = cast<MagicIdentifierLiteralExpr>(literal);
51535149
switch (magicLiteral->getKind()) {
51545150
case MagicIdentifierLiteralExpr::File: {
5155-
std::string value = getASTContext().LangOpts.EnableConcisePoundFile
5156-
? getConciseMagicFileString(*this, loc)
5157-
: getMagicFilePathString(*this, loc).str();
5151+
std::string value = loc.isValid() ? getMagicFileString(loc) : "";
51585152
builtinLiteralArgs = emitStringLiteral(*this, literal, value, C,
51595153
magicLiteral->getStringEncoding());
51605154
builtinInit = magicLiteral->getBuiltinInitializer();
@@ -5163,7 +5157,7 @@ RValue SILGenFunction::emitLiteral(LiteralExpr *literal, SGFContext C) {
51635157
}
51645158

51655159
case MagicIdentifierLiteralExpr::FilePath: {
5166-
StringRef value = getMagicFilePathString(*this, loc);
5160+
StringRef value = loc.isValid() ? getMagicFilePathString(loc) : "";
51675161
builtinLiteralArgs = emitStringLiteral(*this, literal, value, C,
51685162
magicLiteral->getStringEncoding());
51695163
builtinInit = magicLiteral->getBuiltinInitializer();
@@ -5172,9 +5166,7 @@ RValue SILGenFunction::emitLiteral(LiteralExpr *literal, SGFContext C) {
51725166
}
51735167

51745168
case MagicIdentifierLiteralExpr::Function: {
5175-
StringRef value = "";
5176-
if (loc.isValid())
5177-
value = getMagicFunctionString(*this);
5169+
StringRef value = loc.isValid() ? getMagicFunctionString() : "";
51785170
builtinLiteralArgs = emitStringLiteral(*this, literal, value, C,
51795171
magicLiteral->getStringEncoding());
51805172
builtinInit = magicLiteral->getBuiltinInitializer();

lib/SILGen/SILGenConvert.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@ auto SILGenFunction::emitSourceLocationArgs(SourceLoc sourceLoc,
140140
-> SourceLocArgs {
141141
auto &ctx = getASTContext();
142142

143-
StringRef filename = "";
143+
std::string filename = "";
144144
unsigned line = 0;
145145
unsigned column = 0;
146146
if (sourceLoc.isValid()) {
147-
filename = ctx.SourceMgr.getDisplayNameForLoc(sourceLoc);
147+
filename = getMagicFileString(sourceLoc);
148148
std::tie(line, column) = ctx.SourceMgr.getLineAndColumn(sourceLoc);
149149
}
150150

@@ -160,7 +160,7 @@ auto SILGenFunction::emitSourceLocationArgs(SourceLoc sourceLoc,
160160
auto i1Ty = SILType::getBuiltinIntegerType(1, ctx);
161161

162162
SourceLocArgs result;
163-
SILValue literal = B.createStringLiteral(emitLoc, filename,
163+
SILValue literal = B.createStringLiteral(emitLoc, StringRef(filename),
164164
StringLiteralInst::Encoding::UTF8);
165165
result.filenameStartPointer = ManagedValue::forUnmanaged(literal);
166166
// File length

lib/SILGen/SILGenFunction.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,9 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
557557
Optional<SILAccessEnforcement> getUnknownEnforcement(VarDecl *var = nullptr);
558558

559559
SourceManager &getSourceManager() { return SGM.M.getASTContext().SourceMgr; }
560+
std::string getMagicFileString(SourceLoc loc);
561+
StringRef getMagicFilePathString(SourceLoc loc);
562+
StringRef getMagicFunctionString();
560563

561564
/// Push a new debug scope and set its parent pointer.
562565
void enterDebugScope(SILLocation Loc) {

test/SILGen/magic_identifier_file.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,17 @@ func indirectUse() {
1717
// ABSOLUTE: string_literal utf8 "SOURCE_DIR/test/SILGen/magic_identifier_file.swift"
1818
// CONCISE: string_literal utf8 "magic_identifier_file.swift (Foo)"
1919
}
20+
21+
func forceUnwrap(_ x: ()?) {
22+
// BOTH-LABEL: sil {{.*}} @$s3Foo11forceUnwrapyyytSgF
23+
_ = x!
24+
// ABSOLUTE: string_literal utf8 "SOURCE_DIR/test/SILGen/magic_identifier_file.swift"
25+
// CONCISE: string_literal utf8 "magic_identifier_file.swift (Foo)"
26+
}
27+
28+
func forceTry(_ fn: () throws -> ()) {
29+
// BOTH-LABEL: sil {{.*}} @$s3Foo8forceTryyyyyKXEF
30+
try! fn()
31+
// ABSOLUTE: string_literal utf8 "SOURCE_DIR/test/SILGen/magic_identifier_file.swift"
32+
// CONCISE: string_literal utf8 "magic_identifier_file.swift (Foo)"
33+
}

0 commit comments

Comments
 (0)