Skip to content

Commit a0fc51a

Browse files
committed
Add support for #fileID
This temporarily breaks -enable-experimental-concise-pound-file. fixup adding #fileID # Conflicts: # lib/SILGen/SILGenConvert.cpp
1 parent 8f9a40d commit a0fc51a

17 files changed

+51
-37
lines changed

include/swift/AST/DiagnosticsCommon.def

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ 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

include/swift/AST/MagicIdentifierKinds.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ MAGIC_STRING_IDENTIFIER(File, "#file", PoundFileExpr)
5757
MAGIC_IDENTIFIER_TOKEN(File, pound_file)
5858
MAGIC_IDENTIFIER_DEPRECATED_TOKEN(File, kw___FILE__)
5959

60+
/// The \c #fileID magic identifier literal.
61+
MAGIC_STRING_IDENTIFIER(FileID, "#fileID", PoundFileIDExpr)
62+
MAGIC_IDENTIFIER_TOKEN(FileID, pound_fileID)
63+
6064
/// The \c #filePath magic identifier literal.
6165
MAGIC_STRING_IDENTIFIER(FilePath, "#filePath", PoundFilePathExpr)
6266
MAGIC_IDENTIFIER_TOKEN(FilePath, pound_filePath)

include/swift/AST/Module.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -294,21 +294,18 @@ 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>>
313310
computeFileIDMap(bool shouldDiagnose) const;
314311

lib/AST/Module.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2379,7 +2379,6 @@ resolveFileIDConflicts(const ModuleDecl *module, StringRef fileString,
23792379
const llvm::StringMap<SourceFilePathInfo> &paths,
23802380
bool shouldDiagnose) {
23812381
assert(paths.size() > 1);
2382-
assert(module->getASTContext().LangOpts.EnableConcisePoundFile);
23832382

23842383
/// The path we consider to be "correct"; we will emit fix-its changing the
23852384
/// other paths to match this one.
@@ -2429,7 +2428,7 @@ resolveFileIDConflicts(const ModuleDecl *module, StringRef fileString,
24292428

24302429
for (auto loc : pathPair.second.virtualFileLocs) {
24312430
diags.diagnose(loc,
2432-
diag::pound_source_location_creates_pound_file_conflicts,
2431+
diag::source_location_creates_file_id_conflicts,
24332432
fileString);
24342433

24352434
// Offer a fix-it unless it would be tautological.
@@ -2447,18 +2446,15 @@ ModuleDecl::computeFileIDMap(bool shouldDiagnose) const {
24472446
llvm::StringMap<std::pair<std::string, bool>> result;
24482447
SmallString<64> scratch;
24492448

2450-
if (!getASTContext().LangOpts.EnableConcisePoundFile)
2451-
return result;
2452-
24532449
for (auto &namePair : getInfoForUsedFileNames(this)) {
24542450
computeFileID(this, namePair.first(), scratch);
24552451
auto &infoForPaths = namePair.second;
24562452

24572453
assert(!infoForPaths.empty());
24582454

24592455
// TODO: In the future, we'd like to handle these conflicts gracefully by
2460-
// generating a unique `#file` string for each conflicting name. For now, we
2461-
// will simply warn about conflicts.
2456+
// generating a unique `#fileID` string for each conflicting name. For now,
2457+
// we will simply warn about conflicts.
24622458
StringRef winner = infoForPaths.begin()->first();
24632459
if (infoForPaths.size() > 1)
24642460
winner = resolveFileIDConflicts(this, scratch, infoForPaths,

lib/SIL/IR/SILPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2888,7 +2888,7 @@ static void printFileIDMap(SILPrintContext &Ctx, const FileIDMap map) {
28882888
if (map.empty())
28892889
return;
28902890

2891-
Ctx.OS() << "\n\n// Mappings from '#file' to '#filePath':\n";
2891+
Ctx.OS() << "\n\n// Mappings from '#fileID' to '#filePath':\n";
28922892

28932893
if (Ctx.sortSIL()) {
28942894
llvm::SmallVector<llvm::StringRef, 16> keys;

lib/SILGen/SILGenApply.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4849,7 +4849,7 @@ RValue SILGenFunction::emitLiteral(LiteralExpr *literal, SGFContext C) {
48494849

48504850
auto magicLiteral = cast<MagicIdentifierLiteralExpr>(literal);
48514851
switch (magicLiteral->getKind()) {
4852-
case MagicIdentifierLiteralExpr::File: {
4852+
case MagicIdentifierLiteralExpr::FileID: {
48534853
std::string value = loc.isValid() ? getMagicFileIDString(loc) : "";
48544854
builtinLiteralArgs = emitStringLiteral(*this, literal, value, C,
48554855
magicLiteral->getStringEncoding());
@@ -4858,6 +4858,7 @@ RValue SILGenFunction::emitLiteral(LiteralExpr *literal, SGFContext C) {
48584858
break;
48594859
}
48604860

4861+
case MagicIdentifierLiteralExpr::File:
48614862
case MagicIdentifierLiteralExpr::FilePath: {
48624863
StringRef value = loc.isValid() ? getMagicFilePathString(loc) : "";
48634864
builtinLiteralArgs = emitStringLiteral(*this, literal, value, C,

lib/SILGen/SILGenConvert.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ auto SILGenFunction::emitSourceLocationArgs(SourceLoc sourceLoc,
144144
unsigned line = 0;
145145
unsigned column = 0;
146146
if (sourceLoc.isValid()) {
147-
filename = getMagicFileIDString(sourceLoc);
147+
// FIXME: Should be getMagicFileIDString()
148+
filename = getMagicFilePathString(sourceLoc);
148149
std::tie(line, column) = ctx.SourceMgr.getLineAndColumn(sourceLoc);
149150
}
150151

lib/Serialization/Deserialization.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ getActualDefaultArgKind(uint8_t raw) {
219219
CASE(Inherited)
220220
CASE(Column)
221221
CASE(File)
222+
CASE(FileID)
222223
CASE(FilePath)
223224
CASE(Line)
224225
CASE(Function)

lib/Serialization/ModuleFormat.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@ enum class DefaultArgumentKind : uint8_t {
460460
None = 0,
461461
Normal,
462462
File,
463+
FileID,
463464
FilePath,
464465
Line,
465466
Column,

lib/Serialization/Serialization.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,6 +1120,7 @@ static uint8_t getRawStableDefaultArgumentKind(swift::DefaultArgumentKind kind)
11201120
CASE(Inherited)
11211121
CASE(Column)
11221122
CASE(File)
1123+
CASE(FileID)
11231124
CASE(FilePath)
11241125
CASE(Line)
11251126
CASE(Function)

test/IDE/complete_pound_expr.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,31 @@ func test1() {
1515
let _ = useSelector(##^POUND_EXPR_3^#)
1616
}
1717

18-
// POUND_EXPR_INTCONTEXT: Begin completions, 6 items
18+
// POUND_EXPR_INTCONTEXT: Begin completions, 7 items
1919
// POUND_EXPR_INTCONTEXT-DAG: Keyword[#function]/None: function[#String#]; name=function
2020
// POUND_EXPR_INTCONTEXT-DAG: Keyword[#file]/None: file[#String#]; name=file
21+
// POUND_EXPR_INTCONTEXT-DAG: Keyword[#fileID]/None: fileID[#String#]; name=fileID
2122
// POUND_EXPR_INTCONTEXT-DAG: Keyword[#filePath]/None: filePath[#String#]; name=filePath
2223
// POUND_EXPR_INTCONTEXT-DAG: Keyword[#line]/None/TypeRelation[Identical]: line[#Int#]; name=line
2324
// POUND_EXPR_INTCONTEXT-DAG: Keyword[#column]/None/TypeRelation[Identical]: column[#Int#]; name=column
2425
// POUND_EXPR_INTCONTEXT-DAG: Keyword[#dsohandle]/None: dsohandle[#UnsafeRawPointer#]; name=dsohandle
2526
// POUND_EXPR_INTCONTEXT: End completions
2627

27-
// POUND_EXPR_STRINGCONTEXT: Begin completions, 7 items
28+
// POUND_EXPR_STRINGCONTEXT: Begin completions, 8 items
2829
// POUND_EXPR_STRINGCONTEXT-DAG: Keyword[#function]/None/TypeRelation[Identical]: function[#String#];
2930
// POUND_EXPR_STRINGCONTEXT-DAG: Keyword[#file]/None/TypeRelation[Identical]: file[#String#];
31+
// POUND_EXPR_STRINGCONTEXT-DAG: Keyword[#fileID]/None/TypeRelation[Identical]: fileID[#String#];
3032
// POUND_EXPR_STRINGCONTEXT-DAG: Keyword[#filePath]/None/TypeRelation[Identical]: filePath[#String#];
3133
// POUND_EXPR_STRINGCONTEXT-DAG: Keyword[#line]/None: line[#Int#];
3234
// POUND_EXPR_STRINGCONTEXT-DAG: Keyword[#column]/None: column[#Int#];
3335
// POUND_EXPR_STRINGCONTEXT-DAG: Keyword[#dsohandle]/None: dsohandle[#UnsafeRawPointer#];
3436
// POUND_EXPR_STRINGCONTEXT-DAG: Keyword/None/TypeRelation[Identical]: keyPath({#@objc property sequence#})[#String#];
3537
// POUND_EXPR_STRINGCONTEXT: End completions
3638

37-
// POUND_EXPR_SELECTORCONTEXT: Begin completions, 7 items
39+
// POUND_EXPR_SELECTORCONTEXT: Begin completions, 8 items
3840
// POUND_EXPR_SELECTORCONTEXT-DAG: Keyword[#function]/None/TypeRelation[Identical]: function[#Selector#];
3941
// POUND_EXPR_SELECTORCONTEXT-DAG: Keyword[#file]/None/TypeRelation[Identical]: file[#Selector#];
42+
// POUND_EXPR_SELECTORCONTEXT-DAG: Keyword[#fileID]/None/TypeRelation[Identical]: fileID[#Selector#];
4043
// POUND_EXPR_SELECTORCONTEXT-DAG: Keyword[#filePath]/None/TypeRelation[Identical]: filePath[#Selector#];
4144
// POUND_EXPR_SELECTORCONTEXT-DAG: Keyword[#line]/None: line[#Int#];
4245
// POUND_EXPR_SELECTORCONTEXT-DAG: Keyword[#column]/None: column[#Int#];

test/SILGen/Inputs/magic_identifier_file_conflicting_other.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// It should be compiled with -verify.
33

44
// We should diagnose cross-file conflicts.
5-
// expected-warning@+2 {{'#sourceLocation' directive produces '#file' string of 'Foo/other_file_c.swift', which conflicts with '#file' strings produced by other paths in the module}}
5+
// expected-warning@+2 {{'#sourceLocation' directive produces '#fileID' string of 'Foo/other_file_c.swift', which conflicts with '#fileID' strings produced by other paths in the module}}
66
// expected-note@+1 {{change file in '#sourceLocation' to 'first/other_file_c.swift'}} {{23-50="first/other_file_c.swift"}}
77
#sourceLocation(file: "second/other_file_c.swift", line: 1)
88
#sourceLocation()

test/SILGen/magic_identifier_file.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ func forceTry(_ fn: () throws -> ()) {
3232
// CONCISE: string_literal utf8 "Foo/magic_identifier_file.swift"
3333
}
3434

35-
// CONCISE-LABEL: // Mappings from '#file' to '#filePath':
35+
// CONCISE-LABEL: // Mappings from '#fileID' to '#filePath':
3636
// CONCISE: // 'Foo/magic_identifier_file.swift' => 'SOURCE_DIR/test/SILGen/magic_identifier_file.swift'
3737

test/SILGen/magic_identifier_file_conflicting.swift.gyb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ def fixit_loc(start_col, orig_suffix):
3333
#sourceLocation(file: "${TEMPDIR_ESC}/magic_identifier_file_conflicting.swift", line: 10)
3434
#sourceLocation()
3535

36-
// expected-warning@+2 {{'#sourceLocation' directive produces '#file' string of 'Foo/magic_identifier_file_conflicting.swift', which conflicts with '#file' strings produced by other paths in the module}}
36+
// expected-warning@+2 {{'#sourceLocation' directive produces '#fileID' string of 'Foo/magic_identifier_file_conflicting.swift', which conflicts with '#fileID' strings produced by other paths in the module}}
3737
// expected-note@+1 {{change file in '#sourceLocation' to '${TEMPDIR_ESC}/magic_identifier_file_conflicting.swift'}} {{${fixit_loc(23, "other_path_b/magic_identifier_file_conflicting.swift")}="${TEMPDIR_ESC}/magic_identifier_file_conflicting.swift"}}
3838
#sourceLocation(file: "${TEMPDIR_ESC}/other_path_b/magic_identifier_file_conflicting.swift", line: 20)
3939
#sourceLocation()
4040

41-
// expected-warning@+2 {{'#sourceLocation' directive produces '#file' string of 'Foo/magic_identifier_file_conflicting.swift', which conflicts with '#file' strings produced by other paths in the module}}
41+
// expected-warning@+2 {{'#sourceLocation' directive produces '#fileID' string of 'Foo/magic_identifier_file_conflicting.swift', which conflicts with '#fileID' strings produced by other paths in the module}}
4242
// expected-note@+1 {{change file in '#sourceLocation' to '${TEMPDIR_ESC}/magic_identifier_file_conflicting.swift'}} {{23-64="${TEMPDIR_ESC}/magic_identifier_file_conflicting.swift"}}
4343
#sourceLocation(file: "magic_identifier_file_conflicting.swift", line: 30)
4444
#sourceLocation()
@@ -60,38 +60,38 @@ def fixit_loc(start_col, orig_suffix):
6060
// But there should be warnings for different-path, same-name virtual files.
6161
// The lexicographically first path should be treated as canonical--we diagnose
6262
// but don't offer a fix-it.
63-
// expected-warning@+1 {{'#sourceLocation' directive produces '#file' string of 'Foo/other_file_b.swift', which conflicts with '#file' strings produced by other paths in the module}}
63+
// expected-warning@+1 {{'#sourceLocation' directive produces '#fileID' string of 'Foo/other_file_b.swift', which conflicts with '#fileID' strings produced by other paths in the module}}
6464
#sourceLocation(file: "first/other_file_b.swift", line: 60)
6565
#sourceLocation()
6666

6767
// Subsequent paths should fix-it to the first one.
68-
// expected-warning@+2 {{'#sourceLocation' directive produces '#file' string of 'Foo/other_file_b.swift', which conflicts with '#file' strings produced by other paths in the module}}
68+
// expected-warning@+2 {{'#sourceLocation' directive produces '#fileID' string of 'Foo/other_file_b.swift', which conflicts with '#fileID' strings produced by other paths in the module}}
6969
// expected-note@+1 {{change file in '#sourceLocation' to 'first/other_file_b.swift'}} {{23-50="first/other_file_b.swift"}}
7070
#sourceLocation(file: "second/other_file_b.swift", line: 70)
7171
#sourceLocation()
7272

7373
// Even if there's more than one.
74-
// expected-warning@+2 {{'#sourceLocation' directive produces '#file' string of 'Foo/other_file_b.swift', which conflicts with '#file' strings produced by other paths in the module}}
74+
// expected-warning@+2 {{'#sourceLocation' directive produces '#fileID' string of 'Foo/other_file_b.swift', which conflicts with '#fileID' strings produced by other paths in the module}}
7575
// expected-note@+1 {{change file in '#sourceLocation' to 'first/other_file_b.swift'}} {{23-49="first/other_file_b.swift"}}
7676
#sourceLocation(file: "third/other_file_b.swift", line: 80)
7777
#sourceLocation()
7878

7979
// Even if one is duplicated.
80-
// expected-warning@+2 {{'#sourceLocation' directive produces '#file' string of 'Foo/other_file_b.swift', which conflicts with '#file' strings produced by other paths in the module}}
80+
// expected-warning@+2 {{'#sourceLocation' directive produces '#fileID' string of 'Foo/other_file_b.swift', which conflicts with '#fileID' strings produced by other paths in the module}}
8181
// expected-note@+1 {{change file in '#sourceLocation' to 'first/other_file_b.swift'}} {{23-49="first/other_file_b.swift"}}
8282
#sourceLocation(file: "third/other_file_b.swift", line: 90)
8383
#sourceLocation()
8484

8585
// We should diagnose cross-file conflicts.
86-
// expected-warning@+1 {{'#sourceLocation' directive produces '#file' string of 'Foo/other_file_c.swift', which conflicts with '#file' strings produced by other paths in the module}}
86+
// expected-warning@+1 {{'#sourceLocation' directive produces '#fileID' string of 'Foo/other_file_c.swift', which conflicts with '#fileID' strings produced by other paths in the module}}
8787
#sourceLocation(file: "first/other_file_c.swift", line: 100)
8888
#sourceLocation()
8989

9090
//
91-
// Check '#file' => '#filePath' mapping table
91+
// Check '#fileID' => '#filePath' mapping table
9292
//
9393

94-
// CHECK-LABEL: // Mappings from '#file' to '#filePath':
94+
// CHECK-LABEL: // Mappings from '#fileID' to '#filePath':
9595
// CHECK-NEXT: // 'Foo/magic_identifier_file_conflicting.swift' => 'BUILD_DIR{{[/\\]}}test-{{[^/]+}}{{[/\\]}}SILGen{{[/\\]}}Output{{[/\\]}}magic_identifier_file_conflicting.swift.gyb.tmp{{[/\\]}}magic_identifier_file_conflicting.swift'
9696
// CHECK-NEXT: // 'Foo/magic_identifier_file_conflicting.swift' => 'BUILD_DIR{{[/\\]}}test-{{[^/]+}}{{[/\\]}}SILGen{{[/\\]}}Output{{[/\\]}}magic_identifier_file_conflicting.swift.gyb.tmp{{[/\\]}}other_path_b{{[/\\]}}magic_identifier_file_conflicting.swift' (alternate)
9797
// CHECK-NEXT: // 'Foo/magic_identifier_file_conflicting.swift' => 'magic_identifier_file_conflicting.swift' (alternate)

utils/gyb_syntax_support/ExprNodes.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@
130130
Child('PoundFile', kind='PoundFileToken'),
131131
]),
132132

133+
# A #fileID expression.
134+
Node('PoundFileIDExpr', kind='Expr',
135+
children=[
136+
Child('PoundFileID', kind='PoundFileIDToken'),
137+
]),
138+
133139
# A #filePath expression.
134140
Node('PoundFilePathExpr', kind='Expr',
135141
children=[

utils/gyb_syntax_support/NodeSerializationCodes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@
248248
'CatchItemList': 244,
249249
'MultipleTrailingClosureElementList': 245,
250250
'MultipleTrailingClosureElement': 246,
251+
'PoundFileIDExpr': 247,
251252
}
252253

253254

utils/gyb_syntax_support/Token.py

Lines changed: 2 additions & 0 deletions
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('PoundFileID', 'fileID', text='#fileID',
269+
serialization_code=122),
268270
PoundKeyword('PoundFilePath', 'filePath', text='#filePath',
269271
serialization_code=121),
270272
PoundKeyword('PoundColumn', 'column', text='#column',

0 commit comments

Comments
 (0)