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)

0 commit comments

Comments
 (0)