Skip to content

Commit cb3a24e

Browse files
committed
[Macros] Note when diagnostics came from the implementation of a macro
1 parent 387a79c commit cb3a24e

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

lib/ASTGen/Sources/ASTGen/Diagnostics.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ fileprivate func emitDiagnosticParts(
8888
func emitDiagnostic(
8989
diagEnginePtr: UnsafeMutablePointer<UInt8>,
9090
sourceFileBuffer: UnsafeMutableBufferPointer<UInt8>,
91-
diagnostic: Diagnostic
91+
diagnostic: Diagnostic,
92+
messageSuffix: String? = nil
9293
) {
9394
// Collect all of the Fix-It changes based on their Fix-It ID.
9495
var fixItChangesByID: [MessageID : [FixIt.Change]] = [:]
@@ -101,7 +102,7 @@ func emitDiagnostic(
101102
emitDiagnosticParts(
102103
diagEnginePtr: diagEnginePtr,
103104
sourceFileBuffer: sourceFileBuffer,
104-
message: diagnostic.diagMessage.message,
105+
message: diagnostic.diagMessage.message + (messageSuffix ?? ""),
105106
severity: diagnostic.diagMessage.severity,
106107
position: diagnostic.position,
107108
highlights: diagnostic.highlights,

lib/ASTGen/Sources/ASTGen/Macros.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,13 @@ func evaluateMacro(
176176
}
177177

178178
// Emit diagnostics accumulated in the context.
179+
let macroName = parentExpansion.macro.withoutTrivia().description
179180
for diag in context.diagnostics {
180-
// FIXME: Consider tacking on a note that says that this diagnostic
181-
// came from a macro expansion.
182181
emitDiagnostic(
183182
diagEnginePtr: diagEnginePtr,
184183
sourceFileBuffer: .init(mutating: sourceFile.pointee.buffer),
185-
diagnostic: diag
184+
diagnostic: diag,
185+
messageSuffix: " (from macro '\(macroName)')"
186186
)
187187
}
188188

test/Macros/macro_expand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@ macro addBlocker<T>(_ value: T) -> T = MacroDefinition.AddBlocker
5353
func testAddBlocker(a: Int, b: Int, c: Int) {
5454
_ = #addBlocker(a * b * c)
5555
#if TEST_DIAGNOSTICS
56-
_ = #addBlocker(a + b * c) // expected-error{{blocked an add; did you mean to subtract?}}{{21-22=-}}
56+
_ = #addBlocker(a + b * c) // expected-error{{blocked an add; did you mean to subtract? (from macro 'addBlocker')}}{{21-22=-}}
5757
#endif
5858
}

0 commit comments

Comments
 (0)