Skip to content

Commit 18cb5f4

Browse files
committed
[Diagnostics] Update in_macro_expansion note for attached macros
Use the attached atttribute's location as the location of the macro, rather than the location of the declaration it's attached to. Also add the kind and name of that declaration to the note itself.
1 parent a5607cc commit 18cb5f4

File tree

6 files changed

+101
-86
lines changed

6 files changed

+101
-86
lines changed

include/swift/AST/DiagnosticsCommon.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ ERROR(unknown_attribute,none,
221221
// MARK: macro diagnostics
222222
//------------------------------------------------------------------------------
223223
NOTE(in_macro_expansion,none,
224-
"in expansion of macro %0 here", (DeclName))
224+
"in expansion of macro %0%select{| on %kind1}1 here", (DeclName, const Decl *))
225225
ERROR(macro_experimental,none,
226226
"%0 macros are an experimental feature that is not enabled %select{|(%1)}1",
227227
(StringRef, StringRef))

lib/AST/DiagnosticEngine.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,11 +1406,25 @@ DiagnosticEngine::getGeneratedSourceBufferNotes(SourceLoc loc) {
14061406
case GeneratedSourceInfo::PeerMacroExpansion:
14071407
case GeneratedSourceInfo::ConformanceMacroExpansion:
14081408
case GeneratedSourceInfo::ExtensionMacroExpansion: {
1409-
SourceRange origRange = expansionNode.getSourceRange();
14101409
DeclName macroName = getGeneratedSourceInfoMacroName(*generatedInfo);
14111410

1412-
Diagnostic expansionNote(diag::in_macro_expansion, macroName);
1413-
expansionNote.setLoc(origRange.Start);
1411+
// If it was an expansion of an attached macro, increase the range to
1412+
// include the decl's attributes. Also add the name of the decl the macro
1413+
// is attached to.
1414+
CustomAttr *attachedAttr = generatedInfo->attachedMacroCustomAttr;
1415+
Decl *attachedDecl =
1416+
attachedAttr ? expansionNode.dyn_cast<Decl *>() : nullptr;
1417+
SourceRange origRange = attachedDecl
1418+
? attachedDecl->getSourceRangeIncludingAttrs()
1419+
: expansionNode.getSourceRange();
1420+
1421+
Diagnostic expansionNote(diag::in_macro_expansion, macroName,
1422+
attachedDecl);
1423+
if (attachedAttr) {
1424+
expansionNote.setLoc(attachedAttr->getLocation());
1425+
} else {
1426+
expansionNote.setLoc(origRange.Start);
1427+
}
14141428
expansionNote.addRange(
14151429
Lexer::getCharSourceRangeFromSourceRange(SourceMgr, origRange));
14161430
expansionNote.setIsChildNote(true);

test/Macros/macro_expand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ macro Invalid() = #externalMacro(module: "MacroDefinition", type: "InvalidMacro"
7676

7777
@Invalid
7878
struct Bad {}
79-
// expected-note@-1 18 {{in expansion of macro 'Invalid' here}}
79+
// expected-note@-2 18 {{in expansion of macro 'Invalid' on struct 'Bad' here}}
8080

8181
// CHECK-DIAGS: error: macro expansion cannot introduce import
8282
// CHECK-DIAGS: error: macro expansion cannot introduce precedence group

test/Macros/macro_expand_extensions.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ macro UndocumentedNamesInExtension() = #externalMacro(module: "MacroDefinition",
124124

125125
@UndocumentedNamesInExtension
126126
struct S<Element> {}
127-
// expected-note@-1 {{in expansion of macro 'UndocumentedNamesInExtension' here}}
127+
// expected-note@-2 {{in expansion of macro 'UndocumentedNamesInExtension' on generic struct 'S' here}}
128128

129129
// CHECK-DIAGS: error: declaration name 'requirement()' is not covered by macro 'UndocumentedNamesInExtension'
130130

@@ -133,7 +133,7 @@ macro UndocumentedConformanceInExtension() = #externalMacro(module: "MacroDefini
133133

134134
@UndocumentedConformanceInExtension
135135
struct InvalidConformance<Element> {}
136-
// expected-note@-1 {{in expansion of macro 'UndocumentedConformanceInExtension' here}}
136+
// expected-note@-2 {{in expansion of macro 'UndocumentedConformanceInExtension' on generic struct 'InvalidConformance' here}}
137137

138138
// CHECK-DIAGS: error: conformance to 'P' is not covered by macro 'UndocumentedConformanceInExtension'
139139

@@ -142,7 +142,7 @@ macro UndocumentedCodable() = #externalMacro(module: "MacroDefinition", type: "A
142142

143143
@UndocumentedCodable
144144
struct TestUndocumentedCodable {}
145-
// expected-note@-1 {{in expansion of macro 'UndocumentedCodable' here}}
145+
// expected-note@-2 {{in expansion of macro 'UndocumentedCodable' on struct 'TestUndocumentedCodable' here}}
146146

147147
// CHECK-DIAGS: error: conformance to 'Codable' (aka 'Decodable & Encodable') is not covered by macro 'UndocumentedCodable'
148148

@@ -151,7 +151,7 @@ macro UndocumentedEncodable() = #externalMacro(module: "MacroDefinition", type:
151151

152152
@UndocumentedEncodable
153153
struct TestUndocumentedEncodable {}
154-
// expected-note@-1 {{in expansion of macro 'UndocumentedEncodable' here}}
154+
// expected-note@-2 {{in expansion of macro 'UndocumentedEncodable' on struct 'TestUndocumentedEncodable' here}}
155155

156156
// CHECK-DIAGS: error: conformance to 'Codable' (aka 'Decodable & Encodable') is not covered by macro 'UndocumentedEncodable'
157157

test/Macros/macro_expand_synthesized_members.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ print(ElementType.paper.unknown())
104104

105105
#if TEST_DIAGNOSTICS
106106
@addMembersQuotedInit
107-
struct S2 { // expected-note{{in expansion of macro 'addMembersQuotedInit' here}}
107+
struct S2 {
108+
// expected-note@-2 {{in expansion of macro 'addMembersQuotedInit' on struct 'S2' here}}
108109
func useSynthesized() {
109110
S.method()
110111
print(type(of: getStorage()))

0 commit comments

Comments
 (0)