Skip to content

Commit e1e7a3f

Browse files
authored
Merge pull request #25656 from brentdax/file-name-basis
Shorten #file and add #filePath (behind an experimental flag)
2 parents c93d509 + 63ec1cf commit e1e7a3f

27 files changed

+145
-5
lines changed

include/swift/AST/DefaultArgumentKind.h

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ enum class DefaultArgumentKind : uint8_t {
3838
Inherited,
3939
/// The #file default argument, which is expanded at the call site.
4040
File,
41+
/// The #filePath default argument, which is expanded at the call site.
42+
FilePath,
4143
/// The #line default argument, which is expanded at the call site.
4244
Line,
4345
/// The #column default argument, which is expanded at the call site.

include/swift/AST/Expr.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,7 @@ class InterpolatedStringLiteralExpr : public LiteralExpr {
10401040
class MagicIdentifierLiteralExpr : public LiteralExpr {
10411041
public:
10421042
enum Kind : unsigned {
1043-
File, Line, Column, Function, DSOHandle
1043+
File, FilePath, Line, Column, Function, DSOHandle
10441044
};
10451045
private:
10461046
SourceLoc Loc;
@@ -1067,6 +1067,7 @@ class MagicIdentifierLiteralExpr : public LiteralExpr {
10671067
bool isString() const {
10681068
switch (getKind()) {
10691069
case File:
1070+
case FilePath:
10701071
case Function:
10711072
return true;
10721073
case Line:

include/swift/Basic/LangOptions.h

+4
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ namespace swift {
9494
/// when using RequireExplicitAvailability.
9595
std::string RequireExplicitAvailabilityTarget;
9696

97+
/// If false, '#file' evaluates to the full path rather than a
98+
/// human-readable string.
99+
bool EnableConcisePoundFile = false;
100+
97101
///
98102
/// Support for alternate usage modes
99103
///

include/swift/Option/Options.td

+5
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,11 @@ def enable_experimental_differentiable_programming : Flag<["-"], "enable-experim
454454
Flags<[FrontendOption]>,
455455
HelpText<"Enable experimental differentiable programming features">;
456456

457+
def enable_experimental_concise_pound_file : Flag<["-"],
458+
"enable-experimental-concise-pound-file">,
459+
Flags<[FrontendOption]>,
460+
HelpText<"Enable experimental concise '#file' identifier and '#filePath' alternative">;
461+
457462
// Diagnostic control options
458463
def suppress_warnings : Flag<["-"], "suppress-warnings">,
459464
Flags<[FrontendOption]>,

lib/AST/ASTDumper.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ static StringRef getDefaultArgumentKindString(DefaultArgumentKind value) {
300300
case DefaultArgumentKind::Column: return "#column";
301301
case DefaultArgumentKind::DSOHandle: return "#dsohandle";
302302
case DefaultArgumentKind::File: return "#file";
303+
case DefaultArgumentKind::FilePath: return "#filePath";
303304
case DefaultArgumentKind::Function: return "#function";
304305
case DefaultArgumentKind::Inherited: return "inherited";
305306
case DefaultArgumentKind::Line: return "#line";
@@ -316,6 +317,7 @@ static StringRef
316317
getMagicIdentifierLiteralExprKindString(MagicIdentifierLiteralExpr::Kind value) {
317318
switch (value) {
318319
case MagicIdentifierLiteralExpr::File: return "#file";
320+
case MagicIdentifierLiteralExpr::FilePath: return "#filePath";
319321
case MagicIdentifierLiteralExpr::Function: return "#function";
320322
case MagicIdentifierLiteralExpr::Line: return "#line";
321323
case MagicIdentifierLiteralExpr::Column: return "#column";

lib/AST/Decl.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -6071,6 +6071,7 @@ bool ParamDecl::hasDefaultExpr() const {
60716071
return false;
60726072
case DefaultArgumentKind::Normal:
60736073
case DefaultArgumentKind::File:
6074+
case DefaultArgumentKind::FilePath:
60746075
case DefaultArgumentKind::Line:
60756076
case DefaultArgumentKind::Column:
60766077
case DefaultArgumentKind::Function:
@@ -6093,6 +6094,7 @@ bool ParamDecl::hasCallerSideDefaultExpr() const {
60936094
case DefaultArgumentKind::Normal:
60946095
return false;
60956096
case DefaultArgumentKind::File:
6097+
case DefaultArgumentKind::FilePath:
60966098
case DefaultArgumentKind::Line:
60976099
case DefaultArgumentKind::Column:
60986100
case DefaultArgumentKind::Function:
@@ -6402,6 +6404,7 @@ ParamDecl::getDefaultValueStringRepresentation(
64026404
}
64036405
case DefaultArgumentKind::Inherited: return "super";
64046406
case DefaultArgumentKind::File: return "#file";
6407+
case DefaultArgumentKind::FilePath: return "#filePath";
64056408
case DefaultArgumentKind::Line: return "#line";
64066409
case DefaultArgumentKind::Column: return "#column";
64076410
case DefaultArgumentKind::Function: return "#function";

lib/Driver/ToolChains.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ static void addCommonFrontendArgs(const ToolChain &TC, const OutputInfo &OI,
238238
inputArgs.AddLastArg(arguments, options::OPT_enable_astscope_lookup);
239239
inputArgs.AddLastArg(arguments, options::OPT_disable_astscope_lookup);
240240
inputArgs.AddLastArg(arguments, options::OPT_disable_parser_lookup);
241+
inputArgs.AddLastArg(arguments,
242+
options::OPT_enable_experimental_concise_pound_file);
241243

242244
// Pass on any build config options
243245
inputArgs.AddAllArgs(arguments, options::OPT_D);

lib/Frontend/CompilerInvocation.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,9 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
453453
Opts.OptimizationRemarkMissedPattern =
454454
generateOptimizationRemarkRegex(Diags, Args, A);
455455

456+
Opts.EnableConcisePoundFile =
457+
Args.hasArg(OPT_enable_experimental_concise_pound_file);
458+
456459
llvm::Triple Target = Opts.Target;
457460
StringRef TargetArg;
458461
if (const Arg *A = Args.getLastArg(OPT_target)) {

lib/IDE/CodeCompletion.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -2244,6 +2244,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
22442244
return !includeDefaultArgs;
22452245

22462246
case DefaultArgumentKind::File:
2247+
case DefaultArgumentKind::FilePath:
22472248
case DefaultArgumentKind::Line:
22482249
case DefaultArgumentKind::Column:
22492250
case DefaultArgumentKind::Function:
@@ -3653,6 +3654,10 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
36533654
CodeCompletionLiteralKind::StringLiteral, "String");
36543655
addFromProto("#file", CodeCompletionKeywordKind::pound_file,
36553656
CodeCompletionLiteralKind::StringLiteral, "String");
3657+
if (Ctx.LangOpts.EnableConcisePoundFile) {
3658+
addFromProto("#filePath", CodeCompletionKeywordKind::pound_file,
3659+
CodeCompletionLiteralKind::StringLiteral, "String");
3660+
}
36563661
addFromProto("#line", CodeCompletionKeywordKind::pound_line,
36573662
CodeCompletionLiteralKind::IntegerLiteral, "Int");
36583663
addFromProto("#column", CodeCompletionKeywordKind::pound_column,

lib/Parse/ParseExpr.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,8 @@ getMagicIdentifierLiteralKind(tok Kind) {
10141014
case tok::kw___FILE__:
10151015
case tok::pound_file:
10161016
return MagicIdentifierLiteralExpr::Kind::File;
1017+
case tok::pound_filePath:
1018+
return MagicIdentifierLiteralExpr::Kind::FilePath;
10171019
case tok::kw___FUNCTION__:
10181020
case tok::pound_function:
10191021
return MagicIdentifierLiteralExpr::Kind::Function;
@@ -1446,6 +1448,15 @@ ParserResult<Expr> Parser::parseExprPrimary(Diag<> ID, bool isExprBasic) {
14461448
.fixItReplace(Tok.getLoc(), replacement);
14471449
LLVM_FALLTHROUGH;
14481450
}
1451+
1452+
case tok::pound_filePath:
1453+
// Check twice because of fallthrough--this is ugly but temporary.
1454+
if (Tok.is(tok::pound_filePath) && !Context.LangOpts.EnableConcisePoundFile)
1455+
diagnose(Tok.getLoc(), diag::unknown_pound_expr, "filePath");
1456+
// Continue since we actually do know how to handle it. This avoids extra
1457+
// diagnostics.
1458+
LLVM_FALLTHROUGH;
1459+
14491460
case tok::pound_column:
14501461
case tok::pound_file:
14511462
case tok::pound_function:
@@ -1455,6 +1466,7 @@ ParserResult<Expr> Parser::parseExprPrimary(Diag<> ID, bool isExprBasic) {
14551466
switch (Tok.getKind()) {
14561467
case tok::pound_column: SKind = SyntaxKind::PoundColumnExpr; break;
14571468
case tok::pound_file: SKind = SyntaxKind::PoundFileExpr; break;
1469+
case tok::pound_filePath: SKind = SyntaxKind::PoundFilePathExpr; break;
14581470
case tok::pound_function: SKind = SyntaxKind::PoundFunctionExpr; break;
14591471
// FIXME: #line was renamed to #sourceLocation
14601472
case tok::pound_line: SKind = SyntaxKind::PoundLineExpr; break;

lib/Parse/ParsePattern.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ static DefaultArgumentKind getDefaultArgKind(Expr *init) {
4848
return DefaultArgumentKind::Column;
4949
case MagicIdentifierLiteralExpr::File:
5050
return DefaultArgumentKind::File;
51+
case MagicIdentifierLiteralExpr::FilePath:
52+
return DefaultArgumentKind::FilePath;
5153
case MagicIdentifierLiteralExpr::Line:
5254
return DefaultArgumentKind::Line;
5355
case MagicIdentifierLiteralExpr::Function:

lib/SILGen/SILGen.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,7 @@ void SILGenModule::emitDefaultArgGenerator(SILDeclRef constant,
10901090
case DefaultArgumentKind::Inherited:
10911091
case DefaultArgumentKind::Column:
10921092
case DefaultArgumentKind::File:
1093+
case DefaultArgumentKind::FilePath:
10931094
case DefaultArgumentKind::Line:
10941095
case DefaultArgumentKind::Function:
10951096
case DefaultArgumentKind::DSOHandle:

lib/SILGen/SILGenApply.cpp

+33-3
Original file line numberDiff line numberDiff line change
@@ -4876,6 +4876,27 @@ getMagicFunctionString(SILGenFunction &SGF) {
48764876
return SGF.MagicFunctionString;
48774877
}
48784878

4879+
static StringRef
4880+
getMagicFilePathString(SILGenFunction &SGF, SourceLoc loc) {
4881+
if (!loc.isValid())
4882+
return "";
4883+
4884+
return SGF.getASTContext().SourceMgr.getDisplayNameForLoc(loc);
4885+
}
4886+
4887+
static std::string
4888+
getConciseMagicFileString(SILGenFunction &SGF, SourceLoc loc) {
4889+
if (!loc.isValid())
4890+
return "";
4891+
4892+
auto path = getMagicFilePathString(SGF, loc);
4893+
auto value = llvm::sys::path::filename(path).str();
4894+
value += " (";
4895+
value += SGF.getModule().getSwiftModule()->getNameStr();
4896+
value += ")";
4897+
return value;
4898+
}
4899+
48794900
/// Emit an application of the given allocating initializer.
48804901
RValue SILGenFunction::emitApplyAllocatingInitializer(SILLocation loc,
48814902
ConcreteDeclRef init,
@@ -5131,9 +5152,18 @@ RValue SILGenFunction::emitLiteral(LiteralExpr *literal, SGFContext C) {
51315152
auto magicLiteral = cast<MagicIdentifierLiteralExpr>(literal);
51325153
switch (magicLiteral->getKind()) {
51335154
case MagicIdentifierLiteralExpr::File: {
5134-
std::string value;
5135-
if (loc.isValid())
5136-
value = ctx.SourceMgr.getDisplayNameForLoc(loc);
5155+
std::string value = getASTContext().LangOpts.EnableConcisePoundFile
5156+
? getConciseMagicFileString(*this, loc)
5157+
: getMagicFilePathString(*this, loc).str();
5158+
builtinLiteralArgs = emitStringLiteral(*this, literal, value, C,
5159+
magicLiteral->getStringEncoding());
5160+
builtinInit = magicLiteral->getBuiltinInitializer();
5161+
init = magicLiteral->getInitializer();
5162+
break;
5163+
}
5164+
5165+
case MagicIdentifierLiteralExpr::FilePath: {
5166+
StringRef value = getMagicFilePathString(*this, loc);
51375167
builtinLiteralArgs = emitStringLiteral(*this, literal, value, C,
51385168
magicLiteral->getStringEncoding());
51395169
builtinInit = magicLiteral->getBuiltinInitializer();

lib/SILGen/SILGenExpr.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -3681,6 +3681,7 @@ RValue RValueEmitter::
36813681
visitMagicIdentifierLiteralExpr(MagicIdentifierLiteralExpr *E, SGFContext C) {
36823682
switch (E->getKind()) {
36833683
case MagicIdentifierLiteralExpr::File:
3684+
case MagicIdentifierLiteralExpr::FilePath:
36843685
case MagicIdentifierLiteralExpr::Function:
36853686
case MagicIdentifierLiteralExpr::Line:
36863687
case MagicIdentifierLiteralExpr::Column:

lib/Sema/CSApply.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -2157,6 +2157,7 @@ namespace {
21572157
Expr *visitMagicIdentifierLiteralExpr(MagicIdentifierLiteralExpr *expr) {
21582158
switch (expr->getKind()) {
21592159
case MagicIdentifierLiteralExpr::File:
2160+
case MagicIdentifierLiteralExpr::FilePath:
21602161
case MagicIdentifierLiteralExpr::Function:
21612162
return handleStringLiteralExpr(expr);
21622163

lib/Sema/CSGen.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,7 @@ namespace {
12431243
switch (expr->getKind()) {
12441244
case MagicIdentifierLiteralExpr::Column:
12451245
case MagicIdentifierLiteralExpr::File:
1246+
case MagicIdentifierLiteralExpr::FilePath:
12461247
case MagicIdentifierLiteralExpr::Function:
12471248
case MagicIdentifierLiteralExpr::Line:
12481249
return visitLiteralExpr(expr);

lib/Sema/TypeCheckExpr.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,11 @@ static Expr *synthesizeCallerSideDefault(const ParamDecl *param,
730730
MagicIdentifierLiteralExpr(MagicIdentifierLiteralExpr::File, loc,
731731
/*implicit=*/true);
732732

733+
case DefaultArgumentKind::FilePath:
734+
return new (ctx)
735+
MagicIdentifierLiteralExpr(MagicIdentifierLiteralExpr::FilePath, loc,
736+
/*implicit=*/true);
737+
733738
case DefaultArgumentKind::Line:
734739
return new (ctx)
735740
MagicIdentifierLiteralExpr(MagicIdentifierLiteralExpr::Line, loc,

lib/Sema/TypeCheckStmt.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1531,6 +1531,7 @@ static void diagnoseIgnoredLiteral(ASTContext &Ctx, LiteralExpr *LE) {
15311531
case ExprKind::MagicIdentifierLiteral:
15321532
switch (cast<MagicIdentifierLiteralExpr>(LE)->getKind()) {
15331533
case MagicIdentifierLiteralExpr::Kind::File: return "#file";
1534+
case MagicIdentifierLiteralExpr::Kind::FilePath: return "#filePath";
15341535
case MagicIdentifierLiteralExpr::Kind::Line: return "#line";
15351536
case MagicIdentifierLiteralExpr::Kind::Column: return "#column";
15361537
case MagicIdentifierLiteralExpr::Kind::Function: return "#function";

lib/Sema/TypeChecker.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ ProtocolDecl *TypeChecker::getLiteralProtocol(ASTContext &Context, Expr *expr) {
131131
if (auto E = dyn_cast<MagicIdentifierLiteralExpr>(expr)) {
132132
switch (E->getKind()) {
133133
case MagicIdentifierLiteralExpr::File:
134+
case MagicIdentifierLiteralExpr::FilePath:
134135
case MagicIdentifierLiteralExpr::Function:
135136
return TypeChecker::getProtocol(
136137
Context, expr->getLoc(),

lib/Serialization/Deserialization.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ getActualDefaultArgKind(uint8_t raw) {
207207
return swift::DefaultArgumentKind::Column;
208208
case serialization::DefaultArgumentKind::File:
209209
return swift::DefaultArgumentKind::File;
210+
case serialization::DefaultArgumentKind::FilePath:
211+
return swift::DefaultArgumentKind::FilePath;
210212
case serialization::DefaultArgumentKind::Line:
211213
return swift::DefaultArgumentKind::Line;
212214
case serialization::DefaultArgumentKind::Function:

lib/Serialization/ModuleFormat.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5555
/// describe what change you made. The content of this comment isn't important;
5656
/// it just ensures a conflict if two people change the module format.
5757
/// Don't worry about adhering to the 80-column limit for this line.
58-
const uint16_t SWIFTMODULE_VERSION_MINOR = 526; // @_dynamicReplacement adjustments
58+
const uint16_t SWIFTMODULE_VERSION_MINOR = 527; // #filePath
5959

6060
/// A standard hash seed used for all string hashes in a serialized module.
6161
///
@@ -440,6 +440,7 @@ enum class DefaultArgumentKind : uint8_t {
440440
None = 0,
441441
Normal,
442442
File,
443+
FilePath,
443444
Line,
444445
Column,
445446
Function,

lib/Serialization/Serialization.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,7 @@ static uint8_t getRawStableDefaultArgumentKind(swift::DefaultArgumentKind kind)
10651065
CASE(Inherited)
10661066
CASE(Column)
10671067
CASE(File)
1068+
CASE(FilePath)
10681069
CASE(Line)
10691070
CASE(Function)
10701071
CASE(DSOHandle)
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %target-swift-emit-silgen -module-name Foo %s | %FileCheck --check-prefixes=BOTH,ABSOLUTE %s
2+
// RUN: %target-swift-emit-silgen -enable-experimental-concise-pound-file -DNEEDS_CONCISE -module-name Foo %s | %FileCheck --check-prefixes=BOTH,CONCISE %s
3+
4+
// FIXME: Once this feature becomes non-experimental, we should update existing
5+
// tests and delete this file.
6+
7+
func directUse() {
8+
// BOTH-LABEL: sil {{.*}} @$s3Foo9directUseyyF
9+
print(#file)
10+
// ABSOLUTE: string_literal utf8 "SOURCE_DIR/test/SILGen/magic_identifier_file.swift"
11+
// CONCISE: string_literal utf8 "magic_identifier_file.swift (Foo)"
12+
}
13+
14+
func indirectUse() {
15+
// BOTH-LABEL: sil {{.*}} @$s3Foo11indirectUseyyF
16+
fatalError()
17+
// ABSOLUTE: string_literal utf8 "SOURCE_DIR/test/SILGen/magic_identifier_file.swift"
18+
// CONCISE: string_literal utf8 "magic_identifier_file.swift (Foo)"
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Check that we generate the right code with the flag.
2+
// RUN: %target-swift-emit-silgen -enable-experimental-concise-pound-file -module-name Foo %s | %FileCheck %s
3+
4+
// Check that we give errors for use of #filePath if concise #file isn't enabled.
5+
// FIXME: Drop if we stop rejecting this.
6+
// RUN: %target-typecheck-verify-swift -module-name Foo %s
7+
8+
// FIXME: Once this feature becomes non-experimental, we should duplicate
9+
// existing #file tests and delete this file.
10+
11+
func directUse() {
12+
print(#filePath) // expected-error {{use of unknown directive '#filePath'}}
13+
14+
// CHECK-LABEL: sil {{.*}} @$s3Foo9directUseyyF
15+
// CHECK: string_literal utf8 "SOURCE_DIR/test/SILGen/magic_identifier_filepath.swift"
16+
}
17+
18+
func indirectUse() {
19+
functionWithFilePathDefaultArgument()
20+
21+
// CHECK-LABEL: sil {{.*}} @$s3Foo11indirectUseyyF
22+
// CHECK: string_literal utf8 "SOURCE_DIR/test/SILGen/magic_identifier_filepath.swift"
23+
}
24+
25+
func functionWithFilePathDefaultArgument(file: String = #filePath) {}
26+
// expected-error@-1 {{use of unknown directive '#filePath'}}

utils/gyb_syntax_support/ExprNodes.py

+6
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@
130130
Child('PoundFile', kind='PoundFileToken'),
131131
]),
132132

133+
# A #filePath expression.
134+
Node('PoundFilePathExpr', kind='Expr',
135+
children=[
136+
Child('PoundFilePath', kind='PoundFilePathToken'),
137+
]),
138+
133139
# A #function expression.
134140
Node('PoundFunctionExpr', kind='Expr',
135141
children=[

utils/gyb_syntax_support/NodeSerializationCodes.py

+1
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@
241241
'DifferentiationParam': 237,
242242
'DifferentiableAttributeFuncSpecifier': 238,
243243
'FunctionDeclName': 239,
244+
'PoundFilePathExpr': 240,
244245
}
245246

246247

utils/gyb_syntax_support/Token.py

+2
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@ def macro_name(self):
265265
serialization_code=73),
266266
PoundKeyword('PoundFile', 'file', text='#file',
267267
serialization_code=68),
268+
PoundKeyword('PoundFilePath', 'filePath', text='#filePath',
269+
serialization_code=121),
268270
PoundKeyword('PoundColumn', 'column', text='#column',
269271
serialization_code=70),
270272
PoundKeyword('PoundFunction', 'function', text='#function',

0 commit comments

Comments
 (0)