Skip to content

Commit 8dedfb3

Browse files
committed
Add support for #file/#line, etc according to SE-0028. __FILE__ and friends
are still accepted without deprecation warning as of this patch.
1 parent ddc05a3 commit 8dedfb3

22 files changed

+109
-94
lines changed

include/swift/AST/Decl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4199,7 +4199,7 @@ class ParamDecl : public VarDecl {
41994199
/// resolve the type.
42004200
bool IsTypeLocImplicit = false;
42014201

4202-
/// Information about a symbolic default argument, like __FILE__.
4202+
/// Information about a symbolic default argument, like #file.
42034203
DefaultArgumentKind defaultArgumentKind = DefaultArgumentKind::None;
42044204

42054205
public:

include/swift/AST/DefaultArgumentKind.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ enum class DefaultArgumentKind : unsigned {
3434
/// The default argument is inherited from the corresponding argument of the
3535
/// overridden declaration.
3636
Inherited,
37-
/// The __FILE__ default argument, which is expanded at the call site.
37+
/// The #file default argument, which is expanded at the call site.
3838
File,
39-
/// The __LINE__ default argument, which is expanded at the call site.
39+
/// The #line default argument, which is expanded at the call site.
4040
Line,
41-
/// The __COLUMN__ default argument, which is expanded at the call site.
41+
/// The #column default argument, which is expanded at the call site.
4242
Column,
43-
/// The __FUNCTION__ default argument, which is expanded at the call site.
43+
/// The #function default argument, which is expanded at the call site.
4444
Function,
45-
/// The __DSO_HANDLE__ default argument, which is expanded at the call site.
45+
/// The #dsohandle default argument, which is expanded at the call site.
4646
DSOHandle,
4747
/// The "nil" literal.
4848
Nil,

include/swift/AST/Expr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ class InterpolatedStringLiteralExpr : public LiteralExpr {
761761
}
762762
};
763763

764-
/// MagicIdentifierLiteralExpr - A magic identifier like __FILE__ which expands
764+
/// MagicIdentifierLiteralExpr - A magic identifier like #file which expands
765765
/// out to a literal at SILGen time.
766766
class MagicIdentifierLiteralExpr : public LiteralExpr {
767767
public:

include/swift/IDE/CodeCompletion.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ enum class CodeCompletionLiteralKind {
420420
enum class CodeCompletionKeywordKind {
421421
None,
422422
#define KEYWORD(X) kw_##X,
423+
#define POUND_KEYWORD(X) pound_##X,
423424
#include "swift/Parse/Tokens.def"
424425
};
425426

include/swift/Parse/Tokens.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ POUND_KEYWORD(line)
171171
POUND_KEYWORD(available)
172172
POUND_KEYWORD(selector)
173173

174+
POUND_KEYWORD(file)
175+
POUND_KEYWORD(column)
176+
POUND_KEYWORD(function)
177+
POUND_KEYWORD(dsohandle)
178+
174179

175180
#undef KEYWORD
176181
#undef DECL_KEYWORD

lib/AST/ASTDumper.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -802,22 +802,22 @@ namespace {
802802
switch (P->getDefaultArgumentKind()) {
803803
case DefaultArgumentKind::None: break;
804804
case DefaultArgumentKind::Column:
805-
printField("default_arg", "__COLUMN__");
805+
printField("default_arg", "#column");
806806
break;
807807
case DefaultArgumentKind::DSOHandle:
808-
printField("default_arg", "__DSO_HANDLE__");
808+
printField("default_arg", "#dsohandle");
809809
break;
810810
case DefaultArgumentKind::File:
811-
printField("default_arg", "__FILE__");
811+
printField("default_arg", "#file");
812812
break;
813813
case DefaultArgumentKind::Function:
814-
printField("default_arg", "__FUNCTION__");
814+
printField("default_arg", "#function");
815815
break;
816816
case DefaultArgumentKind::Inherited:
817817
printField("default_arg", "inherited");
818818
break;
819819
case DefaultArgumentKind::Line:
820-
printField("default_arg", "__LINE__");
820+
printField("default_arg", "#line");
821821
break;
822822
case DefaultArgumentKind::Nil:
823823
printField("default_arg", "nil");
@@ -1603,18 +1603,18 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
16031603
printCommon(E, "magic_identifier_literal_expr") << " kind=";
16041604
switch (E->getKind()) {
16051605
case MagicIdentifierLiteralExpr::File:
1606-
OS << "__FILE__ encoding=";
1606+
OS << "#file encoding=";
16071607
printStringEncoding(E->getStringEncoding());
16081608
break;
16091609

16101610
case MagicIdentifierLiteralExpr::Function:
1611-
OS << "__FUNCTION__ encoding=";
1611+
OS << "#function encoding=";
16121612
printStringEncoding(E->getStringEncoding());
16131613
break;
16141614

1615-
case MagicIdentifierLiteralExpr::Line: OS << "__LINE__"; break;
1616-
case MagicIdentifierLiteralExpr::Column: OS << "__COLUMN__"; break;
1617-
case MagicIdentifierLiteralExpr::DSOHandle: OS << "__DSO_HANDLE__"; break;
1615+
case MagicIdentifierLiteralExpr::Line: OS << "#line"; break;
1616+
case MagicIdentifierLiteralExpr::Column: OS << "#column"; break;
1617+
case MagicIdentifierLiteralExpr::DSOHandle: OS << "#dsohandle"; break;
16181618
}
16191619
OS << ')';
16201620
}
@@ -2557,27 +2557,27 @@ namespace {
25572557
break;
25582558

25592559
case DefaultArgumentKind::Column:
2560-
printField("default_arg", "__COLUMN__");
2560+
printField("default_arg", "#column");
25612561
break;
25622562

25632563
case DefaultArgumentKind::DSOHandle:
2564-
printField("default_arg", "__DSO_HANDLE__");
2564+
printField("default_arg", "#dsohandle");
25652565
break;
25662566

25672567
case DefaultArgumentKind::File:
2568-
printField("default_arg", "__FILE__");
2568+
printField("default_arg", "#file");
25692569
break;
25702570

25712571
case DefaultArgumentKind::Function:
2572-
printField("default_arg", "__FUNCTION__");
2572+
printField("default_arg", "#function");
25732573
break;
25742574

25752575
case DefaultArgumentKind::Inherited:
25762576
printField("default_arg", "inherited");
25772577
break;
25782578

25792579
case DefaultArgumentKind::Line:
2580-
printField("default_arg", "__LINE__");
2580+
printField("default_arg", "#line");
25812581
break;
25822582

25832583
case DefaultArgumentKind::Nil:

lib/AST/DefaultArgumentKind.cpp

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,14 @@ StringRef swift::getDefaultArgumentSpelling(DefaultArgumentKind kind) {
2525
case DefaultArgumentKind::Normal:
2626
case DefaultArgumentKind::Inherited:
2727
return StringRef();
28-
29-
case DefaultArgumentKind::File:
30-
return "__FILE__";
31-
32-
case DefaultArgumentKind::Line:
33-
return "__LINE__";
34-
35-
case DefaultArgumentKind::Column:
36-
return "__COLUMN__";
37-
38-
case DefaultArgumentKind::Function:
39-
return "__FUNCTION__";
40-
41-
case DefaultArgumentKind::DSOHandle:
42-
return "__DSO_HANDLE__";
43-
44-
case DefaultArgumentKind::Nil:
45-
return "nil";
46-
47-
case DefaultArgumentKind::EmptyArray:
48-
return "[]";
49-
50-
case DefaultArgumentKind::EmptyDictionary:
51-
return "[:]";
28+
case DefaultArgumentKind::File: return "#file";
29+
case DefaultArgumentKind::Line: return "#line";
30+
case DefaultArgumentKind::Column: return "#column";
31+
case DefaultArgumentKind::Function: return "#function";
32+
case DefaultArgumentKind::DSOHandle: return "#dsohandle";
33+
case DefaultArgumentKind::Nil: return "nil";
34+
case DefaultArgumentKind::EmptyArray: return "[]";
35+
case DefaultArgumentKind::EmptyDictionary: return "[:]";
5236
}
5337
}
5438

@@ -59,7 +43,7 @@ DefaultArgumentKind swift::inferDefaultArgumentKind(Expr *expr) {
5943
if (auto ctor = dyn_cast<ConstructorDecl>(ctorRef->getDecl())) {
6044
auto ctorArg = call->getArg()->getSemanticsProvidingExpr();
6145

62-
// __FILE__, __LINE__, __COLUMN__, __FUNCTION__, __DSO_HANDLE__.
46+
// #file, #line, #column, #function, #dsohandle.
6347
if (auto magic = dyn_cast<MagicIdentifierLiteralExpr>(ctorArg)) {
6448
switch (magic->getKind()) {
6549
case MagicIdentifierLiteralExpr::File:

lib/IDE/CodeCompletion.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,9 @@ void CodeCompletionResult::print(raw_ostream &OS) const {
597597
#define KEYWORD(X) case CodeCompletionKeywordKind::kw_##X: \
598598
Prefix.append("[" #X "]"); \
599599
break;
600+
#define POUND_KEYWORD(X) case CodeCompletionKeywordKind::pound_##X: \
601+
Prefix.append("[#" #X "]"); \
602+
break;
600603
#include "swift/Parse/Tokens.def"
601604
}
602605
break;
@@ -4161,12 +4164,12 @@ static void addExprKeywords(CodeCompletionResultSink &Sink) {
41614164
AddKeyword("try?", StringRef(), CodeCompletionKeywordKind::kw_try);
41624165
// FIXME: The pedantically correct way to find the type is to resolve the
41634166
// Swift.StringLiteralType type.
4164-
AddKeyword("__FUNCTION__", "String", CodeCompletionKeywordKind::kw___FUNCTION__);
4165-
AddKeyword("__FILE__", "String", CodeCompletionKeywordKind::kw___FILE__);
4167+
AddKeyword("#function", "String", CodeCompletionKeywordKind::pound_function);
4168+
AddKeyword("#file", "String", CodeCompletionKeywordKind::pound_file);
41664169
// Same: Swift.IntegerLiteralType.
4167-
AddKeyword("__LINE__", "Int", CodeCompletionKeywordKind::kw___LINE__);
4168-
AddKeyword("__COLUMN__", "Int", CodeCompletionKeywordKind::kw___COLUMN__);
4169-
AddKeyword("__DSO_HANDLE__", "UnsafeMutablePointer<Void>", CodeCompletionKeywordKind::kw___DSO_HANDLE__);
4170+
AddKeyword("#line", "Int", CodeCompletionKeywordKind::pound_line);
4171+
AddKeyword("#column", "Int", CodeCompletionKeywordKind::pound_column);
4172+
AddKeyword("#dsohandle", "UnsafeMutablePointer<Void>", CodeCompletionKeywordKind::pound_dsohandle);
41704173
}
41714174

41724175

lib/IDE/SyntaxModel.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,20 @@ SyntaxModelContext::SyntaxModelContext(SourceFile &SrcFile)
9898
#define KEYWORD(X) case tok::kw_##X: Kind = SyntaxNodeKind::Keyword; break;
9999
#include "swift/Parse/Tokens.def"
100100
#undef KEYWORD
101-
case tok::pound_selector: Kind = SyntaxNodeKind::Keyword; break;
101+
case tok::pound_selector:
102+
case tok::pound_file:
103+
case tok::pound_column:
104+
case tok::pound_function:
105+
case tok::pound_dsohandle:
106+
Kind = SyntaxNodeKind::Keyword;
107+
break;
102108
case tok::pound_line:
103-
case tok::pound_available: Kind =
104-
SyntaxNodeKind::BuildConfigKeyword; break;
109+
Kind = Tok.isAtStartOfLine() ? SyntaxNodeKind::BuildConfigKeyword :
110+
SyntaxNodeKind::Keyword;
111+
break;
112+
case tok::pound_available:
113+
Kind = SyntaxNodeKind::BuildConfigKeyword;
114+
break;
105115
case tok::identifier:
106116
if (Tok.getText().startswith("<#"))
107117
Kind = SyntaxNodeKind::EditorPlaceholder;

lib/Parse/ParseDecl.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1698,9 +1698,13 @@ static bool isKeywordPossibleDeclStart(const Token &Tok) {
16981698
case tok::kw_associatedtype:
16991699
case tok::kw_var:
17001700
case tok::pound_if:
1701-
case tok::pound_line:
17021701
case tok::identifier:
17031702
return true;
1703+
case tok::pound_line:
1704+
// #line at the start of the line is a directive, #line within a line is
1705+
// an expression.
1706+
return Tok.isAtStartOfLine();
1707+
17041708
case tok::kw_try:
17051709
// 'try' is not a valid way to start a decl, but we special-case 'try let'
17061710
// and 'try var' for better recovery.

lib/Parse/ParseExpr.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -746,19 +746,24 @@ static bool isStartOfGetSetAccessor(Parser &P) {
746746
P.Tok.isContextualKeyword("willSet");
747747
}
748748

749-
/// Map magic literal tokens such as __FILE__ to their
749+
/// Map magic literal tokens such as #file to their
750750
/// MagicIdentifierLiteralExpr kind.
751751
MagicIdentifierLiteralExpr::Kind getMagicIdentifierLiteralKind(tok Kind) {
752752
switch (Kind) {
753753
case tok::kw___COLUMN__:
754+
case tok::pound_column:
754755
return MagicIdentifierLiteralExpr::Kind::Column;
755756
case tok::kw___FILE__:
757+
case tok::pound_file:
756758
return MagicIdentifierLiteralExpr::Kind::File;
757759
case tok::kw___FUNCTION__:
760+
case tok::pound_function:
758761
return MagicIdentifierLiteralExpr::Kind::Function;
759762
case tok::kw___LINE__:
763+
case tok::pound_line:
760764
return MagicIdentifierLiteralExpr::Kind::Line;
761765
case tok::kw___DSO_HANDLE__:
766+
case tok::pound_dsohandle:
762767
return MagicIdentifierLiteralExpr::Kind::DSOHandle;
763768

764769
default:
@@ -775,11 +780,11 @@ MagicIdentifierLiteralExpr::Kind getMagicIdentifierLiteralKind(tok Kind) {
775780
/// nil
776781
/// true
777782
/// false
778-
/// '__FILE__'
779-
/// '__LINE__'
780-
/// '__COLUMN__'
781-
/// '__FUNCTION__'
782-
/// '__DSO_HANDLE__'
783+
/// #file
784+
/// #line
785+
/// #column
786+
/// #function
787+
/// #dsohandle
783788
///
784789
/// expr-primary:
785790
/// expr-literal
@@ -875,7 +880,12 @@ ParserResult<Expr> Parser::parseExprPostfix(Diag<> ID, bool isExprBasic) {
875880
new (Context) BooleanLiteralExpr(isTrue, consumeToken()));
876881
break;
877882
}
878-
883+
884+
case tok::pound_column:
885+
case tok::pound_file:
886+
case tok::pound_function:
887+
case tok::pound_line:
888+
case tok::pound_dsohandle:
879889
case tok::kw___FILE__:
880890
case tok::kw___LINE__:
881891
case tok::kw___COLUMN__:

lib/Parse/ParseStmt.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,12 @@ bool Parser::isStartOfStmt() {
4747
case tok::kw_case:
4848
case tok::kw_default:
4949
case tok::pound_if:
50-
case tok::pound_line:
5150
return true;
5251

52+
case tok::pound_line:
53+
// #line at the start of a line is a directive, when within, it is an expr.
54+
return Tok.isAtStartOfLine();
55+
5356
case tok::kw_try: {
5457
// "try" cannot actually start any statements, but we parse it there for
5558
// better recovery.

lib/Parse/Parser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,16 +444,16 @@ SourceLoc Parser::skipUntilGreaterInTypeList(bool protocolComposition) {
444444
case tok::eof:
445445
case tok::l_brace:
446446
case tok::r_brace:
447-
case tok::pound_endif:
448447
case tok::code_complete:
449448
return lastLoc;
450449

451450
#define KEYWORD(X) case tok::kw_##X:
451+
#define POUND_KEYWORD(X) case tok::pound_##X:
452452
#include "swift/Parse/Tokens.def"
453453
// 'Self' can appear in types, skip it.
454454
if (Tok.is(tok::kw_Self))
455455
break;
456-
if (isStartOfStmt() || isStartOfDecl())
456+
if (isStartOfStmt() || isStartOfDecl() || Tok.is(tok::pound_endif))
457457
return lastLoc;
458458
break;
459459

lib/SILGen/SILGenExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1989,7 +1989,7 @@ RValue RValueEmitter::visitObjCSelectorExpr(ObjCSelectorExpr *e, SGFContext C) {
19891989
static StringRef
19901990
getMagicFunctionString(SILGenFunction &gen) {
19911991
assert(gen.MagicFunctionName
1992-
&& "asking for __FUNCTION__ but we don't have a function name?!");
1992+
&& "asking for #function but we don't have a function name?!");
19931993
if (gen.MagicFunctionString.empty()) {
19941994
llvm::raw_string_ostream os(gen.MagicFunctionString);
19951995
gen.MagicFunctionName.printPretty(os);

lib/SILGen/SILGenFunction.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ SILGenFunction::~SILGenFunction() {
5959
// Function emission
6060
//===----------------------------------------------------------------------===//
6161

62-
// Get the __FUNCTION__ name for a declaration.
62+
// Get the #function name for a declaration.
6363
DeclName SILGenModule::getMagicFunctionName(DeclContext *dc) {
6464
// For closures, use the parent name.
6565
if (auto closure = dyn_cast<AbstractClosureExpr>(dc)) {
@@ -93,7 +93,7 @@ DeclName SILGenModule::getMagicFunctionName(DeclContext *dc) {
9393
assert(e->getExtendedType()->getAnyNominal() && "extension for nonnominal");
9494
return e->getExtendedType()->getAnyNominal()->getName();
9595
}
96-
llvm_unreachable("unexpected __FUNCTION__ context");
96+
llvm_unreachable("unexpected #function context");
9797
}
9898

9999
DeclName SILGenModule::getMagicFunctionName(SILDeclRef ref) {
@@ -753,7 +753,7 @@ void SILGenFunction::emitGeneratorFunction(SILDeclRef function, Expr *value) {
753753
RegularLocation Loc(value);
754754
Loc.markAutoGenerated();
755755

756-
// Override location for __FILE__ __LINE__ etc. to an invalid one so that we
756+
// Override location for #file, #line etc. to an invalid one so that we
757757
// don't put extra strings into the default argument generator function that
758758
// is not going to be ever used anyway.
759759
overrideLocationForMagicIdentifiers = SourceLoc();

0 commit comments

Comments
 (0)