Skip to content

Commit 0e569f5

Browse files
committed
Add support for #fileID
This temporarily breaks -enable-experimental-concise-pound-file. fixup adding #fileID
1 parent d4ebd5d commit 0e569f5

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
@@ -118,11 +118,11 @@ ERROR(sdk_node_unrecognized_decl_kind,none,
118118
ERROR(sdk_node_unrecognized_accessor_kind,none,
119119
"unrecognized accessor kind '%0' in SDK node", (StringRef))
120120

121-
// Emitted from ModuleDecl::computeMagicFileStringMap()
122-
WARNING(pound_source_location_creates_pound_file_conflicts,none,
123-
"'#sourceLocation' directive produces '#file' string of '%0', which "
124-
"conflicts with '#file' strings produced by other paths in the module",
125-
(StringRef))
121+
// Emitted from ModuleDecl::computeFileIDMap()
122+
WARNING(source_location_creates_file_id_conflicts,none,
123+
"'#sourceLocation' directive produces '#fileID' string of '%0', which "
124+
"conflicts with '#fileID' strings produced by other paths in the "
125+
"module", (StringRef))
126126
NOTE(fixit_correct_source_location_file,none,
127127
"change file in '#sourceLocation' to '%0'", (StringRef))
128128

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
@@ -370,21 +370,18 @@ class ModuleDecl : public DeclContext, public TypeDecl {
370370
bool isClangModule() const;
371371
void addFile(FileUnit &newFile);
372372

373-
/// Creates a map from \c #filePath strings to corresponding \c #file
373+
/// Creates a map from \c #filePath strings to corresponding \c #fileID
374374
/// strings, diagnosing any conflicts.
375375
///
376-
/// A given \c #filePath string always maps to exactly one \c #file string,
376+
/// A given \c #filePath string always maps to exactly one \c #fileID string,
377377
/// but it is possible for \c #sourceLocation directives to introduce
378378
/// duplicates in the opposite direction. If there are such conflicts, this
379379
/// method will diagnose the conflict and choose a "winner" among the paths
380-
/// in a reproducible way. The \c bool paired with the \c #file string is
380+
/// in a reproducible way. The \c bool paired with the \c #fileID string is
381381
/// \c true for paths which did not have a conflict or won a conflict, and
382382
/// \c false for paths which lost a conflict. Thus, if you want to generate a
383-
/// reverse mapping, you should drop or special-case the \c #file strings that
384-
/// are paired with \c false.
385-
///
386-
/// Note that this returns an empty StringMap if concise \c #file strings are
387-
/// disabled. Users should fall back to using the file path in this case.
383+
/// reverse mapping, you should drop or special-case the \c #fileID strings
384+
/// that are paired with \c false.
388385
llvm::StringMap<std::pair<std::string, /*isWinner=*/bool>>
389386
computeFileIDMap(bool shouldDiagnose) const;
390387

lib/AST/Module.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2153,7 +2153,6 @@ resolveFileIDConflicts(const ModuleDecl *module, StringRef fileString,
21532153
const llvm::StringMap<SourceFilePathInfo> &paths,
21542154
bool shouldDiagnose) {
21552155
assert(paths.size() > 1);
2156-
assert(module->getASTContext().LangOpts.EnableConcisePoundFile);
21572156

21582157
/// The path we consider to be "correct"; we will emit fix-its changing the
21592158
/// other paths to match this one.
@@ -2203,7 +2202,7 @@ resolveFileIDConflicts(const ModuleDecl *module, StringRef fileString,
22032202

22042203
for (auto loc : pathPair.second.virtualFileLocs) {
22052204
diags.diagnose(loc,
2206-
diag::pound_source_location_creates_pound_file_conflicts,
2205+
diag::source_location_creates_file_id_conflicts,
22072206
fileString);
22082207

22092208
// Offer a fix-it unless it would be tautological.
@@ -2221,18 +2220,15 @@ ModuleDecl::computeFileIDMap(bool shouldDiagnose) const {
22212220
llvm::StringMap<std::pair<std::string, bool>> result;
22222221
SmallString<64> scratch;
22232222

2224-
if (!getASTContext().LangOpts.EnableConcisePoundFile)
2225-
return result;
2226-
22272223
for (auto &namePair : getInfoForUsedFileNames(this)) {
22282224
computeFileID(this, namePair.first(), scratch);
22292225
auto &infoForPaths = namePair.second;
22302226

22312227
assert(!infoForPaths.empty());
22322228

22332229
// TODO: In the future, we'd like to handle these conflicts gracefully by
2234-
// generating a unique `#file` string for each conflicting name. For now, we
2235-
// will simply warn about conflicts.
2230+
// generating a unique `#fileID` string for each conflicting name. For now,
2231+
// we will simply warn about conflicts.
22362232
StringRef winner = infoForPaths.begin()->first();
22372233
if (infoForPaths.size() > 1)
22382234
winner = resolveFileIDConflicts(this, scratch, infoForPaths,

lib/SIL/IR/SILPrinter.cpp

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

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

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

lib/SILGen/SILGenApply.cpp

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

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

4860+
case MagicIdentifierLiteralExpr::File:
48604861
case MagicIdentifierLiteralExpr::FilePath: {
48614862
StringRef value = loc.isValid() ? getMagicFilePathString(loc) : "";
48624863
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) =
149150
ctx.SourceMgr.getPresumedLineAndColumnForLoc(sourceLoc);
150151
}

lib/Serialization/Deserialization.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ getActualDefaultArgKind(uint8_t raw) {
216216
CASE(Inherited)
217217
CASE(Column)
218218
CASE(File)
219+
CASE(FileID)
219220
CASE(FilePath)
220221
CASE(Line)
221222
CASE(Function)

lib/Serialization/ModuleFormat.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ enum class DefaultArgumentKind : uint8_t {
482482
None = 0,
483483
Normal,
484484
File,
485+
FileID,
485486
FilePath,
486487
Line,
487488
Column,

lib/Serialization/Serialization.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,7 @@ static uint8_t getRawStableDefaultArgumentKind(swift::DefaultArgumentKind kind)
11221122
CASE(Inherited)
11231123
CASE(Column)
11241124
CASE(File)
1125+
CASE(FileID)
11251126
CASE(FilePath)
11261127
CASE(Line)
11271128
CASE(Function)

0 commit comments

Comments
 (0)