Skip to content

Commit e948699

Browse files
authored
Merge pull request #32811 from brentdax/magical-and-evolutionary-5.3
[5.3] Revise #file changes from SE-0274
2 parents cb76d0a + a71fe1c commit e948699

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+598
-351
lines changed

include/swift/AST/DefaultArgumentKind.h

+4-13
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,6 @@ enum class DefaultArgumentKind : uint8_t {
3636
/// The default argument is inherited from the corresponding argument of the
3737
/// overridden declaration.
3838
Inherited,
39-
/// The #file default argument, which is expanded at the call site.
40-
File,
41-
/// The #filePath default argument, which is expanded at the call site.
42-
FilePath,
43-
/// The #line default argument, which is expanded at the call site.
44-
Line,
45-
/// The #column default argument, which is expanded at the call site.
46-
Column,
47-
/// The #function default argument, which is expanded at the call site.
48-
Function,
49-
/// The #dsohandle default argument, which is expanded at the call site.
50-
DSOHandle,
5139
/// The "nil" literal.
5240
NilLiteral,
5341
/// An empty array literal.
@@ -56,8 +44,11 @@ enum class DefaultArgumentKind : uint8_t {
5644
EmptyDictionary,
5745
/// A reference to the stored property. This is a special default argument
5846
/// kind for the synthesized memberwise constructor to emit a call to the
59-
// property's initializer.
47+
/// property's initializer.
6048
StoredProperty,
49+
// Magic identifier literals expanded at the call site:
50+
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) NAME,
51+
#include "swift/AST/MagicIdentifierKinds.def"
6152
};
6253
enum { NumDefaultArgumentKindBits = 4 };
6354

include/swift/AST/DiagnosticsCommon.def

+13-5
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,22 @@ ERROR(sdk_node_unrecognized_decl_kind,none,
114114
ERROR(sdk_node_unrecognized_accessor_kind,none,
115115
"unrecognized accessor kind '%0' in SDK node", (StringRef))
116116

117-
// Emitted from ModuleDecl::computeMagicFileStringMap()
118-
WARNING(pound_source_location_creates_pound_file_conflicts,none,
119-
"'#sourceLocation' directive produces '#file' string of '%0', which "
120-
"conflicts with '#file' strings produced by other paths in the module",
121-
(StringRef))
117+
// Emitted from ModuleDecl::computeFileIDMap()
118+
WARNING(source_location_creates_file_id_conflicts,none,
119+
"'#sourceLocation' directive produces '#fileID' string of '%0', which "
120+
"conflicts with '#fileID' strings produced by other paths in the "
121+
"module", (StringRef))
122122
NOTE(fixit_correct_source_location_file,none,
123123
"change file in '#sourceLocation' to '%0'", (StringRef))
124124

125+
// Usually, but not always, emitted from the driver
126+
ERROR(error_two_files_same_name,none,
127+
"filename \"%0\" used twice: '%1' and '%2'",
128+
(StringRef, StringRef, StringRef))
129+
NOTE(note_explain_two_files_same_name,none,
130+
"filenames are used to distinguish private declarations with the same "
131+
"name", ())
132+
125133
//------------------------------------------------------------------------------
126134
// MARK: Circular reference diagnostics
127135
//------------------------------------------------------------------------------

include/swift/AST/DiagnosticsDriver.def

-7
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,6 @@ WARNING(warn_arclite_not_found_when_link_objc_runtime,none,
104104
"unable to find Objective-C runtime support library 'arclite'; "
105105
"pass '-no-link-objc-runtime' to silence this warning", ())
106106

107-
ERROR(error_two_files_same_name,none,
108-
"filename \"%0\" used twice: '%1' and '%2'",
109-
(StringRef, StringRef, StringRef))
110-
NOTE(note_explain_two_files_same_name,none,
111-
"filenames are used to distinguish private declarations with the same "
112-
"name", ())
113-
114107
WARNING(warn_cannot_stat_input,none,
115108
"unable to determine when '%0' was last modified: %1",
116109
(StringRef, StringRef))

include/swift/AST/Expr.h

+9-18
Original file line numberDiff line numberDiff line change
@@ -1074,17 +1074,14 @@ class InterpolatedStringLiteralExpr : public LiteralExpr {
10741074
class MagicIdentifierLiteralExpr : public LiteralExpr {
10751075
public:
10761076
enum Kind : unsigned {
1077-
File, FilePath, Line, Column, Function, DSOHandle
1077+
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) NAME,
1078+
#include "swift/AST/MagicIdentifierKinds.def"
10781079
};
10791080

10801081
static StringRef getKindString(MagicIdentifierLiteralExpr::Kind value) {
10811082
switch (value) {
1082-
case File: return "#file";
1083-
case FilePath: return "#filePath";
1084-
case Function: return "#function";
1085-
case Line: return "#line";
1086-
case Column: return "#column";
1087-
case DSOHandle: return "#dsohandle";
1083+
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) case NAME: return STRING;
1084+
#include "swift/AST/MagicIdentifierKinds.def"
10881085
}
10891086

10901087
llvm_unreachable("Unhandled MagicIdentifierLiteralExpr in getKindString.");
@@ -1107,21 +1104,15 @@ class MagicIdentifierLiteralExpr : public LiteralExpr {
11071104
return static_cast<Kind>(Bits.MagicIdentifierLiteralExpr.Kind);
11081105
}
11091106

1110-
bool isFile() const { return getKind() == File; }
1111-
bool isFunction() const { return getKind() == Function; }
1112-
bool isLine() const { return getKind() == Line; }
1113-
bool isColumn() const { return getKind() == Column; }
1114-
11151107
bool isString() const {
11161108
switch (getKind()) {
1117-
case File:
1118-
case FilePath:
1119-
case Function:
1109+
#define MAGIC_STRING_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
1110+
case NAME: \
11201111
return true;
1121-
case Line:
1122-
case Column:
1123-
case DSOHandle:
1112+
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
1113+
case NAME: \
11241114
return false;
1115+
#include "swift/AST/MagicIdentifierKinds.def"
11251116
}
11261117
llvm_unreachable("bad Kind");
11271118
}
+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
//===--- MagicIdentifierKinds.def - Swift #ident metaprogramming -*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This file defines macros used for macro-metaprogramming with magic
14+
// identifier literals.
15+
//
16+
//===----------------------------------------------------------------------===//
17+
18+
// Used for any magic identifier.
19+
#ifndef MAGIC_IDENTIFIER
20+
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND)
21+
#endif
22+
23+
// Used for magic identifiers which produce string literals.
24+
#ifndef MAGIC_STRING_IDENTIFIER
25+
#define MAGIC_STRING_IDENTIFIER(NAME, STRING, SYNTAX_KIND) MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND)
26+
#endif
27+
28+
// Used for magic identifiers which produce integer literals.
29+
#ifndef MAGIC_INT_IDENTIFIER
30+
#define MAGIC_INT_IDENTIFIER(NAME, STRING, SYNTAX_KIND) MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND)
31+
#endif
32+
33+
// Used for magic identifiers which produce raw pointers.
34+
#ifndef MAGIC_POINTER_IDENTIFIER
35+
#define MAGIC_POINTER_IDENTIFIER(NAME, STRING, SYNTAX_KIND) MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND)
36+
#endif
37+
38+
// Used when a given token always maps to a particular magic identifier kind.
39+
#ifndef MAGIC_IDENTIFIER_TOKEN
40+
#define MAGIC_IDENTIFIER_TOKEN(NAME, TOKEN)
41+
#endif
42+
43+
// Used when a given token always maps to a particular magic identifier kind,
44+
// but that token is deprecated.
45+
#ifndef MAGIC_IDENTIFIER_DEPRECATED_TOKEN
46+
#define MAGIC_IDENTIFIER_DEPRECATED_TOKEN(NAME, TOKEN) MAGIC_IDENTIFIER_TOKEN(NAME, TOKEN)
47+
#endif
48+
49+
50+
51+
//
52+
// Magic string literals
53+
//
54+
55+
/// The \c #fileID magic identifier literal.
56+
MAGIC_STRING_IDENTIFIER(FileID, "#fileID", PoundFileIDExpr)
57+
MAGIC_IDENTIFIER_TOKEN(FileID, pound_fileID)
58+
59+
/// The \c #file magic identifier literal, written in code where it is
60+
/// a synonym for \c #fileID (i.e. "Swift 6 mode" code).
61+
MAGIC_STRING_IDENTIFIER(FileIDSpelledAsFile, "#file", PoundFileExpr)
62+
// tok::pound_file is shared with FilePathSpelledAsFile; please write custom
63+
// code paths for it.
64+
65+
/// The \c #filePath magic identifier literal.
66+
MAGIC_STRING_IDENTIFIER(FilePath, "#filePath", PoundFilePathExpr)
67+
MAGIC_IDENTIFIER_TOKEN(FilePath, pound_filePath)
68+
69+
/// The \c #file magic identifier literal, written in code where it is
70+
/// a synonym for \c #filePath (i.e. Swift 5 mode code).
71+
MAGIC_STRING_IDENTIFIER(FilePathSpelledAsFile, "#file", PoundFileExpr)
72+
// tok::pound_file is shared with FileIDSpelledAsFile; please write custom
73+
// code paths for it.
74+
MAGIC_IDENTIFIER_DEPRECATED_TOKEN(FilePathSpelledAsFile, kw___FILE__)
75+
76+
/// The \c #function magic identifier literal.
77+
MAGIC_STRING_IDENTIFIER(Function, "#function", PoundFunctionExpr)
78+
MAGIC_IDENTIFIER_TOKEN(Function, pound_function)
79+
MAGIC_IDENTIFIER_DEPRECATED_TOKEN(Function, kw___FUNCTION__)
80+
81+
82+
83+
//
84+
// Magic integer literals
85+
//
86+
87+
/// The \c #line magic identifier literal.
88+
MAGIC_INT_IDENTIFIER(Line, "#line", PoundLineExpr)
89+
MAGIC_IDENTIFIER_TOKEN(Line, pound_line)
90+
MAGIC_IDENTIFIER_DEPRECATED_TOKEN(Line, kw___LINE__)
91+
92+
/// The \c #column magic identifier literal.
93+
MAGIC_INT_IDENTIFIER(Column, "#column", PoundColumnExpr)
94+
MAGIC_IDENTIFIER_TOKEN(Column, pound_column)
95+
MAGIC_IDENTIFIER_DEPRECATED_TOKEN(Column, kw___COLUMN__)
96+
97+
98+
99+
//
100+
// Magic raw pointer literals
101+
//
102+
103+
/// The \c #dsohandle magic identifier literal.
104+
MAGIC_POINTER_IDENTIFIER(DSOHandle, "#dsohandle", PoundDsohandleExpr)
105+
MAGIC_IDENTIFIER_TOKEN(DSOHandle, pound_dsohandle)
106+
MAGIC_IDENTIFIER_DEPRECATED_TOKEN(DSOHandle, kw___DSO_HANDLE__)
107+
108+
109+
110+
111+
#undef MAGIC_IDENTIFIER
112+
#undef MAGIC_STRING_IDENTIFIER
113+
#undef MAGIC_INT_IDENTIFIER
114+
#undef MAGIC_POINTER_IDENTIFIER
115+
#undef MAGIC_IDENTIFIER_TOKEN
116+
#undef MAGIC_IDENTIFIER_DEPRECATED_TOKEN

include/swift/AST/Module.h

+6-9
Original file line numberDiff line numberDiff line change
@@ -294,23 +294,20 @@ class ModuleDecl : public DeclContext, public TypeDecl {
294294
void addFile(FileUnit &newFile);
295295
void removeFile(FileUnit &existingFile);
296296

297-
/// Creates a map from \c #filePath strings to corresponding \c #file
297+
/// Creates a map from \c #filePath strings to corresponding \c #fileID
298298
/// strings, diagnosing any conflicts.
299299
///
300-
/// A given \c #filePath string always maps to exactly one \c #file string,
300+
/// A given \c #filePath string always maps to exactly one \c #fileID string,
301301
/// but it is possible for \c #sourceLocation directives to introduce
302302
/// duplicates in the opposite direction. If there are such conflicts, this
303303
/// method will diagnose the conflict and choose a "winner" among the paths
304-
/// in a reproducible way. The \c bool paired with the \c #file string is
304+
/// in a reproducible way. The \c bool paired with the \c #fileID string is
305305
/// \c true for paths which did not have a conflict or won a conflict, and
306306
/// \c false for paths which lost a conflict. Thus, if you want to generate a
307-
/// reverse mapping, you should drop or special-case the \c #file strings that
308-
/// are paired with \c false.
309-
///
310-
/// Note that this returns an empty StringMap if concise \c #file strings are
311-
/// disabled. Users should fall back to using the file path in this case.
307+
/// reverse mapping, you should drop or special-case the \c #fileID strings
308+
/// that are paired with \c false.
312309
llvm::StringMap<std::pair<std::string, /*isWinner=*/bool>>
313-
computeMagicFileStringMap(bool shouldDiagnose) const;
310+
computeFileIDMap(bool shouldDiagnose) const;
314311

315312
/// Add a file declaring a cross-import overlay.
316313
void addCrossImportOverlayFile(StringRef file);

include/swift/Option/Options.td

+1-1
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ def enable_experimental_additive_arithmetic_derivation :
516516

517517
def enable_experimental_concise_pound_file : Flag<["-"],
518518
"enable-experimental-concise-pound-file">,
519-
Flags<[FrontendOption]>,
519+
Flags<[FrontendOption, ModuleInterfaceOption]>,
520520
HelpText<"Enable experimental concise '#file' identifier">;
521521

522522
// Diagnostic control options

lib/AST/ASTDumper.cpp

+3-6
Original file line numberDiff line numberDiff line change
@@ -321,13 +321,10 @@ getForeignErrorConventionKindString(ForeignErrorConvention::Kind value) {
321321
static StringRef getDefaultArgumentKindString(DefaultArgumentKind value) {
322322
switch (value) {
323323
case DefaultArgumentKind::None: return "none";
324-
case DefaultArgumentKind::Column: return "#column";
325-
case DefaultArgumentKind::DSOHandle: return "#dsohandle";
326-
case DefaultArgumentKind::File: return "#file";
327-
case DefaultArgumentKind::FilePath: return "#filePath";
328-
case DefaultArgumentKind::Function: return "#function";
324+
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
325+
case DefaultArgumentKind::NAME: return STRING;
326+
#include "swift/AST/MagicIdentifierKinds.def"
329327
case DefaultArgumentKind::Inherited: return "inherited";
330-
case DefaultArgumentKind::Line: return "#line";
331328
case DefaultArgumentKind::NilLiteral: return "nil";
332329
case DefaultArgumentKind::EmptyArray: return "[]";
333330
case DefaultArgumentKind::EmptyDictionary: return "[:]";

lib/AST/ASTPrinter.cpp

+3-6
Original file line numberDiff line numberDiff line change
@@ -2741,12 +2741,9 @@ void PrintAST::printOneParameter(const ParamDecl *param,
27412741
Printer << " = ";
27422742

27432743
switch (param->getDefaultArgumentKind()) {
2744-
case DefaultArgumentKind::File:
2745-
case DefaultArgumentKind::Line:
2746-
case DefaultArgumentKind::Column:
2747-
case DefaultArgumentKind::Function:
2748-
case DefaultArgumentKind::DSOHandle:
2749-
case DefaultArgumentKind::NilLiteral:
2744+
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
2745+
case DefaultArgumentKind::NAME:
2746+
#include "swift/AST/MagicIdentifierKinds.def"
27502747
Printer.printKeyword(defaultArgStr, Options);
27512748
break;
27522749
default:

lib/AST/Decl.cpp

+9-18
Original file line numberDiff line numberDiff line change
@@ -6321,12 +6321,9 @@ bool ParamDecl::hasDefaultExpr() const {
63216321
case DefaultArgumentKind::StoredProperty:
63226322
return false;
63236323
case DefaultArgumentKind::Normal:
6324-
case DefaultArgumentKind::File:
6325-
case DefaultArgumentKind::FilePath:
6326-
case DefaultArgumentKind::Line:
6327-
case DefaultArgumentKind::Column:
6328-
case DefaultArgumentKind::Function:
6329-
case DefaultArgumentKind::DSOHandle:
6324+
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
6325+
case DefaultArgumentKind::NAME:
6326+
#include "swift/AST/MagicIdentifierKinds.def"
63306327
case DefaultArgumentKind::NilLiteral:
63316328
case DefaultArgumentKind::EmptyArray:
63326329
case DefaultArgumentKind::EmptyDictionary:
@@ -6344,12 +6341,9 @@ bool ParamDecl::hasCallerSideDefaultExpr() const {
63446341
case DefaultArgumentKind::StoredProperty:
63456342
case DefaultArgumentKind::Normal:
63466343
return false;
6347-
case DefaultArgumentKind::File:
6348-
case DefaultArgumentKind::FilePath:
6349-
case DefaultArgumentKind::Line:
6350-
case DefaultArgumentKind::Column:
6351-
case DefaultArgumentKind::Function:
6352-
case DefaultArgumentKind::DSOHandle:
6344+
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
6345+
case DefaultArgumentKind::NAME:
6346+
#include "swift/AST/MagicIdentifierKinds.def"
63536347
case DefaultArgumentKind::NilLiteral:
63546348
case DefaultArgumentKind::EmptyArray:
63556349
case DefaultArgumentKind::EmptyDictionary:
@@ -6585,12 +6579,9 @@ ParamDecl::getDefaultValueStringRepresentation(
65856579
scratch);
65866580
}
65876581
case DefaultArgumentKind::Inherited: return "super";
6588-
case DefaultArgumentKind::File: return "#file";
6589-
case DefaultArgumentKind::FilePath: return "#filePath";
6590-
case DefaultArgumentKind::Line: return "#line";
6591-
case DefaultArgumentKind::Column: return "#column";
6592-
case DefaultArgumentKind::Function: return "#function";
6593-
case DefaultArgumentKind::DSOHandle: return "#dsohandle";
6582+
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
6583+
case DefaultArgumentKind::NAME: return STRING;
6584+
#include "swift/AST/MagicIdentifierKinds.def"
65946585
case DefaultArgumentKind::NilLiteral: return "nil";
65956586
case DefaultArgumentKind::EmptyArray: return "[]";
65966587
case DefaultArgumentKind::EmptyDictionary: return "[:]";

0 commit comments

Comments
 (0)