Skip to content

Commit 0613fca

Browse files
[Caching] Fix a bug when emitting diagnostics from nested macros
Fix a bug when emitting cached diagnostics from nested macros. When building a new source manager for cached diagnostics, there is a bug when creating mapping between FileIDs between two different source managers. If the newly created file in source manager requires another file to be mapped, e.g. when emitting a diagnostic from nested macros, the returning FileID can be updated by the second request before returning. Fix the bug by making sure two different FileIDs are returned in this case. rdar://144810862
1 parent 3745dd1 commit 0613fca

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

lib/Frontend/CachedDiagnostics.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ unsigned DiagnosticSerializer::getFileIDFromBufferID(SourceManager &SM,
348348

349349
Files.emplace_back(std::move(File));
350350
Allocated.insert({Idx, ++CurrentFileID});
351-
351+
unsigned NewFileID = CurrentFileID;
352352

353353
auto Info = SM.getGeneratedSourceInfo(Idx);
354354
auto convertGeneratedFileInfo =
@@ -364,7 +364,7 @@ unsigned DiagnosticSerializer::getFileIDFromBufferID(SourceManager &SM,
364364
GeneratedFileInfo.emplace_back(std::move(GI));
365365
}
366366

367-
return CurrentFileID;
367+
return NewFileID;
368368
}
369369

370370
SerializedSourceLoc

test/CAS/cached_diagnostics_macro.swift

+11
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,23 @@ public struct CallDeprecatedMacro: ExpressionMacro {
4949
}
5050
}
5151

52+
public struct ToMyWarningMacro: ExpressionMacro {
53+
public static func expansion(
54+
of macro: some FreestandingMacroExpansionSyntax,
55+
in context: some MacroExpansionContext
56+
) throws -> ExprSyntax {
57+
return "#myWarning(\"\")"
58+
}
59+
}
60+
5261
//--- main.swift
5362
@freestanding(expression) macro myWarning(_ message: String) = #externalMacro(module: "MacroDefinition", type: "CallDeprecatedMacro")
63+
@freestanding(expression) macro toMyWarning(_ message: String) = #externalMacro(module: "MacroDefinition", type: "ToMyWarningMacro")
5464

5565
@available(*, deprecated)
5666
func testDeprecated() {}
5767

5868
func testDiscardableStringify(x: Int) {
69+
#toMyWarning("this is a warning")
5970
#myWarning("this is a warning")
6071
}

0 commit comments

Comments
 (0)